Skip to content

Commit d22f136

Browse files
committed
remove synchronization
1 parent a5302e1 commit d22f136

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

server/src/main/java/org/elasticsearch/index/seqno/LocalCheckpointTracker.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,19 @@ public synchronized void waitForOpsToComplete(final long seqNo) throws Interrupt
161161
/**
162162
* Checks if the given sequence number was marked as completed in this tracker.
163163
*/
164-
public synchronized boolean contains(long seqNo) {
164+
public boolean contains(long seqNo) {
165+
assert seqNo >= 0 : "invalid seq_no [" + seqNo + "]";
165166
if (seqNo >= nextSeqNo) {
166167
return false;
167168
}
168169
if (seqNo <= checkpoint) {
169170
return true;
170171
}
171172
final long bitSetKey = getBitSetKey(seqNo);
172-
final CountedBitSet bitSet = processedSeqNo.get(bitSetKey);
173+
final CountedBitSet bitSet;
174+
synchronized (this) {
175+
bitSet = processedSeqNo.get(bitSetKey);
176+
}
173177
return bitSet != null && bitSet.get(seqNoToBitSetOffset(seqNo));
174178
}
175179

@@ -221,7 +225,6 @@ private long lastSeqNoInBitSet(final long bitSetKey) {
221225
* @return the bit set corresponding to the provided sequence number
222226
*/
223227
private long getBitSetKey(final long seqNo) {
224-
assert Thread.holdsLock(this);
225228
return seqNo / BIT_SET_SIZE;
226229
}
227230

@@ -247,7 +250,6 @@ private CountedBitSet getBitSetForSeqNo(final long seqNo) {
247250
* @return the position in the bit set corresponding to the provided sequence number
248251
*/
249252
private int seqNoToBitSetOffset(final long seqNo) {
250-
assert Thread.holdsLock(this);
251253
return Math.toIntExact(seqNo % BIT_SET_SIZE);
252254
}
253255

server/src/test/java/org/elasticsearch/index/seqno/LocalCheckpointTrackerTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,23 @@ public void describeTo(Description description) {
295295
});
296296
assertThat(tracker.generateSeqNo(), equalTo((long) (maxSeqNo + 1)));
297297
}
298+
299+
public void testContains() {
300+
final long maxSeqNo = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, 100);
301+
final long localCheckpoint = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, maxSeqNo);
302+
final LocalCheckpointTracker tracker = new LocalCheckpointTracker(maxSeqNo, localCheckpoint);
303+
if (localCheckpoint >= 0) {
304+
assertThat(tracker.contains(randomLongBetween(0, localCheckpoint)), equalTo(true));
305+
}
306+
assertThat(tracker.contains(randomLongBetween(localCheckpoint + 1, Long.MAX_VALUE)), equalTo(false));
307+
final int numOps = between(1, 100);
308+
final List<Long> seqNos = new ArrayList<>();
309+
for (int i = 0; i < numOps; i++) {
310+
long seqNo = randomLongBetween(0, 1000);
311+
seqNos.add(seqNo);
312+
tracker.markSeqNoAsCompleted(seqNo);
313+
}
314+
final long seqNo = randomNonNegativeLong();
315+
assertThat(tracker.contains(seqNo), equalTo(seqNo <= localCheckpoint || seqNos.contains(seqNo)));
316+
}
298317
}

0 commit comments

Comments
 (0)