Add equals/hashCode to SeqNoStats (#35223)

This commit adds equals/hashCode to SeqNoStats so we can verify it wholly in tests.
This commit is contained in:
Nhat Nguyen 2018-11-02 21:31:36 -04:00 committed by GitHub
parent 44f08717ba
commit 855ab3fa1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 6 deletions

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
public class SeqNoStats implements ToXContentFragment, Writeable { public class SeqNoStats implements ToXContentFragment, Writeable {
@ -83,6 +84,21 @@ public class SeqNoStats implements ToXContentFragment, Writeable {
return builder; return builder;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final SeqNoStats that = (SeqNoStats) o;
return maxSeqNo == that.maxSeqNo &&
localCheckpoint == that.localCheckpoint &&
globalCheckpoint == that.globalCheckpoint;
}
@Override
public int hashCode() {
return Objects.hash(maxSeqNo, localCheckpoint, globalCheckpoint);
}
@Override @Override
public String toString() { public String toString() {
return "SeqNoStats{" + return "SeqNoStats{" +

View File

@ -20,6 +20,7 @@
package org.elasticsearch.index.seqno; package org.elasticsearch.index.seqno;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -57,4 +58,11 @@ public class SequenceNumbersTests extends ESTestCase {
assertThat(e, hasToString(containsString("sequence number must be assigned"))); assertThat(e, hasToString(containsString("sequence number must be assigned")));
} }
public void testSeqNoStatsEqualsAndHashCode() {
final long maxSeqNo = randomLongBetween(SequenceNumbers.UNASSIGNED_SEQ_NO, Long.MAX_VALUE);
final long localCheckpoint = randomLongBetween(SequenceNumbers.UNASSIGNED_SEQ_NO, maxSeqNo);
final long globalCheckpoint = randomLongBetween(SequenceNumbers.UNASSIGNED_SEQ_NO, localCheckpoint);
EqualsHashCodeTestUtils.checkEqualsAndHashCode(new SeqNoStats(maxSeqNo, localCheckpoint, globalCheckpoint),
stats -> new SeqNoStats(stats.getMaxSeqNo(), stats.getLocalCheckpoint(), stats.getGlobalCheckpoint()));
}
} }

View File

@ -1308,12 +1308,7 @@ public final class InternalTestCluster extends TestCluster {
} catch (AlreadyClosedException e) { } catch (AlreadyClosedException e) {
continue; // shard is closed - just ignore continue; // shard is closed - just ignore
} }
assertThat(replicaShardRouting + " local checkpoint mismatch", assertThat(replicaShardRouting + " seq_no_stats mismatch", seqNoStats, equalTo(primarySeqNoStats));
seqNoStats.getLocalCheckpoint(), equalTo(primarySeqNoStats.getLocalCheckpoint()));
assertThat(replicaShardRouting + " global checkpoint mismatch",
seqNoStats.getGlobalCheckpoint(), equalTo(primarySeqNoStats.getGlobalCheckpoint()));
assertThat(replicaShardRouting + " max seq no mismatch",
seqNoStats.getMaxSeqNo(), equalTo(primarySeqNoStats.getMaxSeqNo()));
// the local knowledge on the primary of the global checkpoint equals the global checkpoint on the shard // the local knowledge on the primary of the global checkpoint equals the global checkpoint on the shard
assertThat(replicaShardRouting + " global checkpoint syncs mismatch", seqNoStats.getGlobalCheckpoint(), assertThat(replicaShardRouting + " global checkpoint syncs mismatch", seqNoStats.getGlobalCheckpoint(),
equalTo(syncGlobalCheckpoints.get(replicaShardRouting.allocationId().getId()))); equalTo(syncGlobalCheckpoints.get(replicaShardRouting.allocationId().getId())));