Skip to content

Commit b0e8696

Browse files
authored
Clarify global checkpoint recovery
Today when starting a new engine, we read the global checkpoint from the translog only if we are opening an existing translog. This commit clarifies this situation by distinguishing the three cases of engine creation in the constructor leading to clearer code. Relates #21934
1 parent 0afef53 commit b0e8696

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,29 @@ public InternalEngine(EngineConfig engineConfig) throws EngineException {
154154
throttle = new IndexThrottle();
155155
this.searcherFactory = new SearchFactory(logger, isClosed, engineConfig);
156156
try {
157-
writer = createWriter(openMode == EngineConfig.OpenMode.CREATE_INDEX_AND_TRANSLOG);
158-
final SeqNoStats seqNoStats = loadSeqNoStats(engineConfig, writer);
159-
if (logger.isTraceEnabled()) {
160-
logger.trace(
161-
"recovering max sequence number: [{}], local checkpoint: [{}], global checkpoint: [{}]",
162-
seqNoStats.getMaxSeqNo(),
163-
seqNoStats.getLocalCheckpoint(),
164-
seqNoStats.getGlobalCheckpoint());
157+
final SeqNoStats seqNoStats;
158+
switch (openMode) {
159+
case OPEN_INDEX_AND_TRANSLOG:
160+
writer = createWriter(false);
161+
seqNoStats = loadSeqNoStatsFromLuceneAndTranslog(engineConfig.getTranslogConfig(), writer);
162+
break;
163+
case OPEN_INDEX_CREATE_TRANSLOG:
164+
writer = createWriter(false);
165+
seqNoStats = loadSeqNoStatsLucene(SequenceNumbersService.UNASSIGNED_SEQ_NO, writer);
166+
break;
167+
case CREATE_INDEX_AND_TRANSLOG:
168+
writer = createWriter(true);
169+
seqNoStats = new SeqNoStats(
170+
SequenceNumbersService.NO_OPS_PERFORMED,
171+
SequenceNumbersService.NO_OPS_PERFORMED,
172+
SequenceNumbersService.UNASSIGNED_SEQ_NO);
173+
break;
174+
default:
175+
throw new IllegalArgumentException(openMode.toString());
165176
}
166-
seqNoService =
167-
new SequenceNumbersService(
168-
shardId,
169-
engineConfig.getIndexSettings(),
170-
seqNoStats.getMaxSeqNo(),
171-
seqNoStats.getLocalCheckpoint(),
172-
seqNoStats.getGlobalCheckpoint());
177+
logger.trace("recovered [{}]", seqNoStats);
173178
indexWriter = writer;
179+
seqNoService = sequenceNumberService(shardId, engineConfig.getIndexSettings(), seqNoStats);
174180
translog = openTranslog(engineConfig, writer, seqNoService::getGlobalCheckpoint);
175181
assert translog.getGeneration() != null;
176182
} catch (IOException | TranslogCorruptedException e) {
@@ -209,6 +215,18 @@ public InternalEngine(EngineConfig engineConfig) throws EngineException {
209215
logger.trace("created new InternalEngine");
210216
}
211217

218+
private static SequenceNumbersService sequenceNumberService(
219+
final ShardId shardId,
220+
final IndexSettings indexSettings,
221+
final SeqNoStats seqNoStats) {
222+
return new SequenceNumbersService(
223+
shardId,
224+
indexSettings,
225+
seqNoStats.getMaxSeqNo(),
226+
seqNoStats.getLocalCheckpoint(),
227+
seqNoStats.getGlobalCheckpoint());
228+
}
229+
212230
@Override
213231
public InternalEngine recoverFromTranslog() throws IOException {
214232
flushLock.lock();
@@ -326,18 +344,33 @@ private Translog.TranslogGeneration loadTranslogIdFromCommit(IndexWriter writer)
326344
}
327345

328346
/**
329-
* Reads the sequence number stats from the Lucene commit point (maximum sequence number and local checkpoint) and the Translog
347+
* Reads the sequence number stats from the Lucene commit point (maximum sequence number and local checkpoint) and the translog
330348
* checkpoint (global checkpoint).
331349
*
332-
* @param engineConfig the engine configuration (for the open mode and the translog path)
333-
* @param writer the index writer (for the Lucene commit point)
350+
* @param translogConfig the translog config (for the global checkpoint)
351+
* @param indexWriter the index writer (for the Lucene commit point)
334352
* @return the sequence number stats
335353
* @throws IOException if an I/O exception occurred reading the Lucene commit point or the translog checkpoint
336354
*/
337-
private static SeqNoStats loadSeqNoStats(final EngineConfig engineConfig, final IndexWriter writer) throws IOException {
355+
private static SeqNoStats loadSeqNoStatsFromLuceneAndTranslog(
356+
final TranslogConfig translogConfig,
357+
final IndexWriter indexWriter) throws IOException {
358+
long globalCheckpoint = Translog.readGlobalCheckpoint(translogConfig.getTranslogPath());
359+
return loadSeqNoStatsLucene(globalCheckpoint, indexWriter);
360+
}
361+
362+
/**
363+
* Reads the sequence number stats from the Lucene commit point (maximum sequence number and local checkpoint) and uses the
364+
* specified global checkpoint.
365+
*
366+
* @param globalCheckpoint the global checkpoint to use
367+
* @param indexWriter the index writer (for the Lucene commit point)
368+
* @return the sequence number stats
369+
*/
370+
private static SeqNoStats loadSeqNoStatsLucene(final long globalCheckpoint, final IndexWriter indexWriter) {
338371
long maxSeqNo = SequenceNumbersService.NO_OPS_PERFORMED;
339372
long localCheckpoint = SequenceNumbersService.NO_OPS_PERFORMED;
340-
for (Map.Entry<String, String> entry : writer.getLiveCommitData()) {
373+
for (Map.Entry<String, String> entry : indexWriter.getLiveCommitData()) {
341374
final String key = entry.getKey();
342375
if (key.equals(LOCAL_CHECKPOINT_KEY)) {
343376
assert localCheckpoint == SequenceNumbersService.NO_OPS_PERFORMED;
@@ -348,13 +381,6 @@ private static SeqNoStats loadSeqNoStats(final EngineConfig engineConfig, final
348381
}
349382
}
350383

351-
final long globalCheckpoint;
352-
if (engineConfig.getOpenMode() == EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG) {
353-
globalCheckpoint = Translog.readGlobalCheckpoint(engineConfig.getTranslogConfig().getTranslogPath());
354-
} else {
355-
globalCheckpoint = SequenceNumbersService.UNASSIGNED_SEQ_NO;
356-
}
357-
358384
return new SeqNoStats(maxSeqNo, localCheckpoint, globalCheckpoint);
359385
}
360386

0 commit comments

Comments
 (0)