Skip to content

Commit

Permalink
Add equals/hashCode to SeqNoStats (#35223)
Browse files Browse the repository at this point in the history
This commit adds equals/hashCode to SeqNoStats so we can verify it wholly in tests.
  • Loading branch information
dnhatn authored Nov 3, 2018
1 parent 44f0871 commit 855ab3f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
16 changes: 16 additions & 0 deletions server/src/main/java/org/elasticsearch/index/seqno/SeqNoStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

public class SeqNoStats implements ToXContentFragment, Writeable {

Expand Down Expand Up @@ -83,6 +84,21 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
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
public String toString() {
return "SeqNoStats{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.index.seqno;

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -57,4 +58,11 @@ public void testMax() {
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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1308,12 +1308,7 @@ public void assertSeqNos() throws Exception {
} catch (AlreadyClosedException e) {
continue; // shard is closed - just ignore
}
assertThat(replicaShardRouting + " local checkpoint mismatch",
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()));
assertThat(replicaShardRouting + " seq_no_stats mismatch", seqNoStats, equalTo(primarySeqNoStats));
// 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(),
equalTo(syncGlobalCheckpoints.get(replicaShardRouting.allocationId().getId())));
Expand Down

0 comments on commit 855ab3f

Please sign in to comment.