Skip to content

Commit 9626abb

Browse files
committed
Only verify global checkpoint if translog sync occurred (#45980)
We only sync translog if the given offset hasn't synced yet. We can't verify the global checkpoint from the latest translog checkpoint unless a sync has occurred. Closes #46065 Relates #45634
1 parent d1a69cd commit 9626abb

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.elasticsearch.common.xcontent.XContentType;
3232
import org.elasticsearch.index.IndexService;
3333
import org.elasticsearch.index.IndexSettings;
34+
import org.elasticsearch.index.shard.IndexShard;
3435
import org.elasticsearch.index.translog.Translog;
36+
import org.elasticsearch.indices.IndicesService;
3537
import org.elasticsearch.plugins.Plugin;
3638
import org.elasticsearch.test.ESIntegTestCase;
3739
import org.elasticsearch.test.InternalSettingsPlugin;
@@ -226,4 +228,35 @@ private void runGlobalCheckpointSyncTest(
226228
}
227229
}
228230

231+
public void testPersistGlobalCheckpoint() throws Exception {
232+
internalCluster().ensureAtLeastNumDataNodes(2);
233+
Settings.Builder indexSettings = Settings.builder()
234+
.put(IndexService.GLOBAL_CHECKPOINT_SYNC_INTERVAL_SETTING.getKey(), randomTimeValue(100, 1000, "ms"))
235+
.put("index.number_of_replicas", randomIntBetween(0, 1));
236+
if (randomBoolean()) {
237+
indexSettings.put(IndexSettings.INDEX_TRANSLOG_DURABILITY_SETTING.getKey(), Translog.Durability.ASYNC)
238+
.put(IndexSettings.INDEX_TRANSLOG_SYNC_INTERVAL_SETTING.getKey(), randomTimeValue(100, 1000, "ms"));
239+
}
240+
prepareCreate("test", indexSettings).get();
241+
if (randomBoolean()) {
242+
ensureGreen("test");
243+
}
244+
int numDocs = randomIntBetween(1, 20);
245+
for (int i = 0; i < numDocs; i++) {
246+
client().prepareIndex("test", "test", Integer.toString(i)).setSource("{}", XContentType.JSON).get();
247+
}
248+
ensureGreen("test");
249+
assertBusy(() -> {
250+
for (IndicesService indicesService : internalCluster().getDataNodeInstances(IndicesService.class)) {
251+
for (IndexService indexService : indicesService) {
252+
for (IndexShard shard : indexService) {
253+
final SeqNoStats seqNoStats = shard.seqNoStats();
254+
assertThat(seqNoStats.getLocalCheckpoint(), equalTo(seqNoStats.getMaxSeqNo()));
255+
assertThat(shard.getLastKnownGlobalCheckpoint(), equalTo(seqNoStats.getMaxSeqNo()));
256+
assertThat(shard.getLastSyncedGlobalCheckpoint(), equalTo(seqNoStats.getMaxSeqNo()));
257+
}
258+
}
259+
}
260+
});
261+
}
229262
}

server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,21 +3290,25 @@ public void testSyncConcurrently() throws Exception {
32903290
}
32913291
assertNotNull(location);
32923292
long globalCheckpoint = lastGlobalCheckpoint.get();
3293+
final boolean synced;
32933294
if (randomBoolean()) {
3294-
translog.ensureSynced(location);
3295+
synced = translog.ensureSynced(location);
32953296
} else {
32963297
translog.sync();
3298+
synced = true;
32973299
}
32983300
for (Translog.Operation op : ops) {
32993301
assertThat("seq# " + op.seqNo() + " was not marked as persisted", persistedSeqNos, hasItem(op.seqNo()));
33003302
}
33013303
Checkpoint checkpoint = translog.getLastSyncedCheckpoint();
33023304
assertThat(checkpoint.offset, greaterThanOrEqualTo(location.translogLocation));
3303-
assertThat(checkpoint.globalCheckpoint, greaterThanOrEqualTo(globalCheckpoint));
33043305
for (Translog.Operation op : ops) {
33053306
assertThat(checkpoint.minSeqNo, lessThanOrEqualTo(op.seqNo()));
33063307
assertThat(checkpoint.maxSeqNo, greaterThanOrEqualTo(op.seqNo()));
33073308
}
3309+
if (synced) {
3310+
assertThat(checkpoint.globalCheckpoint, greaterThanOrEqualTo(globalCheckpoint));
3311+
}
33083312
} catch (Exception e) {
33093313
throw new AssertionError(e);
33103314
}

0 commit comments

Comments
 (0)