Skip to content

Commit ad5a212

Browse files
committed
bootstrap primary term for an empty translog
1 parent d50d7ed commit ad5a212

File tree

8 files changed

+34
-30
lines changed

8 files changed

+34
-30
lines changed

server/src/main/java/org/elasticsearch/index/shard/StoreRecovery.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ private void internalRecoverFromStore(IndexShard indexShard) throws IndexShardRe
393393
store.bootstrapNewHistory();
394394
final SegmentInfos segmentInfos = store.readLastCommittedSegmentsInfo();
395395
final long maxSeqNo = Long.parseLong(segmentInfos.userData.get(SequenceNumbers.MAX_SEQ_NO));
396-
final String translogUUID = Translog.createEmptyTranslog(indexShard.shardPath().resolveTranslog(), maxSeqNo, shardId);
396+
final String translogUUID = Translog.createEmptyTranslog(
397+
indexShard.shardPath().resolveTranslog(), maxSeqNo, shardId, indexShard.getPrimaryTerm());
397398
store.associateIndexWithNewTranslog(translogUUID);
398399
} else if (indexShouldExists) {
399400
// since we recover from local, just fill the files and size
@@ -407,8 +408,8 @@ private void internalRecoverFromStore(IndexShard indexShard) throws IndexShardRe
407408
}
408409
} else {
409410
store.createEmpty();
410-
final String translogUUID = Translog.createEmptyTranslog(indexShard.shardPath().resolveTranslog(),
411-
SequenceNumbers.NO_OPS_PERFORMED, shardId);
411+
final String translogUUID = Translog.createEmptyTranslog(
412+
indexShard.shardPath().resolveTranslog(), SequenceNumbers.NO_OPS_PERFORMED, shardId, indexShard.getPrimaryTerm());
412413
store.associateIndexWithNewTranslog(translogUUID);
413414
}
414415
indexShard.openEngineAndRecoverFromTranslog();
@@ -456,7 +457,8 @@ private void restore(final IndexShard indexShard, final Repository repository, f
456457
store.bootstrapNewHistory();
457458
final SegmentInfos segmentInfos = store.readLastCommittedSegmentsInfo();
458459
final long maxSeqNo = Long.parseLong(segmentInfos.userData.get(SequenceNumbers.MAX_SEQ_NO));
459-
final String translogUUID = Translog.createEmptyTranslog(indexShard.shardPath().resolveTranslog(), maxSeqNo, shardId);
460+
final String translogUUID = Translog.createEmptyTranslog(
461+
indexShard.shardPath().resolveTranslog(), maxSeqNo, shardId, indexShard.getPrimaryTerm());
460462
store.associateIndexWithNewTranslog(translogUUID);
461463
assert indexShard.shardRouting.primary() : "only primary shards can recover from store";
462464
indexShard.openEngineAndRecoverFromTranslog();

server/src/main/java/org/elasticsearch/index/translog/Translog.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,13 +1716,14 @@ List<TranslogReader> getReaders() {
17161716
return readers;
17171717
}
17181718

1719-
public static String createEmptyTranslog(final Path location, final long initialGlobalCheckpoint, final ShardId shardId)
1720-
throws IOException {
1719+
public static String createEmptyTranslog(final Path location, final long initialGlobalCheckpoint,
1720+
final ShardId shardId, final long primaryTerm) throws IOException {
17211721
final ChannelFactory channelFactory = FileChannel::open;
1722-
return createEmptyTranslog(location, initialGlobalCheckpoint, shardId, channelFactory);
1722+
return createEmptyTranslog(location, initialGlobalCheckpoint, shardId, channelFactory, primaryTerm);
17231723
}
17241724

1725-
static String createEmptyTranslog(Path location, long initialGlobalCheckpoint, ShardId shardId, ChannelFactory channelFactory) throws IOException {
1725+
static String createEmptyTranslog(Path location, long initialGlobalCheckpoint, ShardId shardId,
1726+
ChannelFactory channelFactory, long primaryTerm) throws IOException {
17261727
IOUtils.rm(location);
17271728
Files.createDirectories(location);
17281729
final Checkpoint checkpoint = Checkpoint.emptyTranslogCheckpoint(0, 1, initialGlobalCheckpoint, 1);
@@ -1732,7 +1733,7 @@ static String createEmptyTranslog(Path location, long initialGlobalCheckpoint, S
17321733
final String translogUUID = UUIDs.randomBase64UUID();
17331734
TranslogWriter writer = TranslogWriter.create(shardId, translogUUID, 1, location.resolve(getFilename(1)), channelFactory,
17341735
new ByteSizeValue(10), 1, initialGlobalCheckpoint,
1735-
() -> { throw new UnsupportedOperationException(); }, () -> { throw new UnsupportedOperationException(); }, 0L
1736+
() -> { throw new UnsupportedOperationException(); }, () -> { throw new UnsupportedOperationException(); }, primaryTerm
17361737
);
17371738
writer.close();
17381739
return translogUUID;

server/src/main/java/org/elasticsearch/indices/recovery/RecoveryTarget.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ public void cleanFiles(int totalTranslogOps, Store.MetadataSnapshot sourceMetaDa
441441
store.ensureIndexHasHistoryUUID();
442442
}
443443
// TODO: Assign the global checkpoint to the max_seqno of the safe commit if the index version >= 6.2
444-
final String translogUUID =
445-
Translog.createEmptyTranslog(indexShard.shardPath().resolveTranslog(), SequenceNumbers.UNASSIGNED_SEQ_NO, shardId);
444+
final String translogUUID = Translog.createEmptyTranslog(
445+
indexShard.shardPath().resolveTranslog(), SequenceNumbers.UNASSIGNED_SEQ_NO, shardId, indexShard.getPrimaryTerm());
446446
store.associateIndexWithNewTranslog(translogUUID);
447447
} catch (CorruptIndexException | IndexFormatTooNewException | IndexFormatTooOldException ex) {
448448
// this is a fatal exception at this stage.

server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ public void testSyncedFlushSurvivesEngineRestart() throws IOException {
11471147
}
11481148
if (randomBoolean()) {
11491149
final String translogUUID = Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(),
1150-
SequenceNumbers.UNASSIGNED_SEQ_NO, shardId);
1150+
SequenceNumbers.UNASSIGNED_SEQ_NO, shardId, primaryTerm.get());
11511151
store.associateIndexWithNewTranslog(translogUUID);
11521152
}
11531153
engine = new InternalEngine(config);
@@ -2369,7 +2369,7 @@ public void testCurrentTranslogIDisCommitted() throws IOException {
23692369
{
23702370
store.createEmpty();
23712371
final String translogUUID =
2372-
Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId);
2372+
Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
23732373
store.associateIndexWithNewTranslog(translogUUID);
23742374
ParsedDocument doc = testParsedDocument(Integer.toString(0), null, testDocument(), new BytesArray("{}"), null);
23752375
Engine.Index firstIndexRequest = new Engine.Index(newUid(doc), doc, SequenceNumbers.UNASSIGNED_SEQ_NO, 0,
@@ -2409,7 +2409,7 @@ public void testCurrentTranslogIDisCommitted() throws IOException {
24092409
// open index with new tlog
24102410
{
24112411
final String translogUUID =
2412-
Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId);
2412+
Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
24132413
store.associateIndexWithNewTranslog(translogUUID);
24142414
try (InternalEngine engine = new InternalEngine(config)) {
24152415
Map<String, String> userData = engine.getLastCommittedSegmentInfos().getUserData();
@@ -2454,7 +2454,7 @@ public void testMissingTranslog() throws IOException {
24542454
// expected
24552455
}
24562456
// when a new translog is created it should be ok
2457-
final String translogUUID = Translog.createEmptyTranslog(primaryTranslogDir, SequenceNumbers.UNASSIGNED_SEQ_NO, shardId);
2457+
final String translogUUID = Translog.createEmptyTranslog(primaryTranslogDir, SequenceNumbers.UNASSIGNED_SEQ_NO, shardId, primaryTerm);
24582458
store.associateIndexWithNewTranslog(translogUUID);
24592459
EngineConfig config = config(defaultSettings, store, primaryTranslogDir, newMergePolicy(), null);
24602460
engine = new InternalEngine(config);
@@ -2519,7 +2519,7 @@ public void testTranslogCleanUpPostCommitCrash() throws Exception {
25192519
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
25202520
final LongSupplier globalCheckpointSupplier = () -> globalCheckpoint.get();
25212521
store.createEmpty();
2522-
final String translogUUID = Translog.createEmptyTranslog(translogPath, globalCheckpoint.get(), shardId);
2522+
final String translogUUID = Translog.createEmptyTranslog(translogPath, globalCheckpoint.get(), shardId, primaryTerm.get());
25232523
store.associateIndexWithNewTranslog(translogUUID);
25242524
try (InternalEngine engine =
25252525
new InternalEngine(config(indexSettings, store, translogPath, newMergePolicy(), null, null,
@@ -2681,7 +2681,7 @@ public void testRecoverFromForeignTranslog() throws IOException {
26812681
engine.close();
26822682

26832683
final Path badTranslogLog = createTempDir();
2684-
final String badUUID = Translog.createEmptyTranslog(badTranslogLog, SequenceNumbers.NO_OPS_PERFORMED, shardId);
2684+
final String badUUID = Translog.createEmptyTranslog(badTranslogLog, SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
26852685
Translog translog = new Translog(
26862686
new TranslogConfig(shardId, badTranslogLog, INDEX_SETTINGS, BigArrays.NON_RECYCLING_INSTANCE),
26872687
badUUID, createTranslogDeletionPolicy(INDEX_SETTINGS), () -> SequenceNumbers.NO_OPS_PERFORMED, primaryTerm::get);
@@ -3311,7 +3311,7 @@ public void testEngineMaxTimestampIsInitialized() throws IOException {
33113311
}
33123312
try (Store store = createStore(newFSDirectory(storeDir))) {
33133313
if (randomBoolean() || true) {
3314-
final String translogUUID = Translog.createEmptyTranslog(translogDir, SequenceNumbers.NO_OPS_PERFORMED, shardId);
3314+
final String translogUUID = Translog.createEmptyTranslog(translogDir, SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
33153315
store.associateIndexWithNewTranslog(translogUUID);
33163316
}
33173317
try (Engine engine = new InternalEngine(configSupplier.apply(store))) {
@@ -4114,7 +4114,7 @@ public void testKeepTranslogAfterGlobalCheckpoint() throws Exception {
41144114
store = createStore();
41154115
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
41164116
store.createEmpty();
4117-
final String translogUUID = Translog.createEmptyTranslog(translogPath, globalCheckpoint.get(), shardId);
4117+
final String translogUUID = Translog.createEmptyTranslog(translogPath, globalCheckpoint.get(), shardId, primaryTerm.get());
41184118
store.associateIndexWithNewTranslog(translogUUID);
41194119

41204120
final EngineConfig engineConfig = config(indexSettings, store, translogPath, NoMergePolicy.INSTANCE, null, null,

server/src/test/java/org/elasticsearch/index/shard/RefreshListenersTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ public void onFailedEngine(String reason, @Nullable Exception e) {
122122
}
123123
};
124124
store.createEmpty();
125+
final long primaryTerm = randomNonNegativeLong();
125126
final String translogUUID =
126-
Translog.createEmptyTranslog(translogConfig.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId);
127+
Translog.createEmptyTranslog(translogConfig.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm);
127128
store.associateIndexWithNewTranslog(translogUUID);
128-
final long primaryTerm = randomNonNegativeLong();
129129
EngineConfig config = new EngineConfig(shardId, allocationId, threadPool,
130130
indexSettings, null, store, newMergePolicy(), iwc.getAnalyzer(), iwc.getSimilarity(), new CodecService(null, logger),
131131
eventListener, IndexSearcher.getDefaultQueryCache(), IndexSearcher.getDefaultQueryCachingPolicy(), translogConfig,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
import java.util.concurrent.atomic.AtomicBoolean;
107107
import java.util.concurrent.atomic.AtomicInteger;
108108
import java.util.concurrent.atomic.AtomicLong;
109-
import java.util.function.LongSupplier;
110109
import java.util.stream.Collectors;
111110
import java.util.stream.LongStream;
112111
import java.util.stream.Stream;
@@ -155,7 +154,7 @@ protected void afterIfSuccessful() throws Exception {
155154

156155
protected Translog createTranslog(TranslogConfig config) throws IOException {
157156
String translogUUID =
158-
Translog.createEmptyTranslog(config.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId);
157+
Translog.createEmptyTranslog(config.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
159158
return new Translog(config, translogUUID, createTranslogDeletionPolicy(config.getIndexSettings()),
160159
() -> SequenceNumbers.NO_OPS_PERFORMED, primaryTerm::get);
161160
}
@@ -212,7 +211,7 @@ private Translog create(Path path) throws IOException {
212211
globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
213212
final TranslogConfig translogConfig = getTranslogConfig(path);
214213
final TranslogDeletionPolicy deletionPolicy = createTranslogDeletionPolicy(translogConfig.getIndexSettings());
215-
final String translogUUID = Translog.createEmptyTranslog(path, SequenceNumbers.NO_OPS_PERFORMED, shardId);
214+
final String translogUUID = Translog.createEmptyTranslog(path, SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
216215
return new Translog(translogConfig, translogUUID, deletionPolicy, () -> globalCheckpoint.get(), primaryTerm::get);
217216
}
218217

@@ -2032,7 +2031,7 @@ private Translog getFailableTranslog(final FailSwitch fail, final TranslogConfig
20322031
};
20332032
if (translogUUID == null) {
20342033
translogUUID = Translog.createEmptyTranslog(
2035-
config.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, channelFactory);
2034+
config.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, channelFactory, primaryTerm.get());
20362035
}
20372036
return new Translog(config, translogUUID, deletionPolicy, () -> SequenceNumbers.NO_OPS_PERFORMED, primaryTerm::get) {
20382037
@Override
@@ -2346,7 +2345,7 @@ public void testWithRandomException() throws IOException {
23462345
deletionPolicy.setMinTranslogGenerationForRecovery(minGenForRecovery);
23472346
if (generationUUID == null) {
23482347
// we never managed to successfully create a translog, make it
2349-
generationUUID = Translog.createEmptyTranslog(config.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId);
2348+
generationUUID = Translog.createEmptyTranslog(config.getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
23502349
}
23512350
try (Translog translog = new Translog(config, generationUUID, deletionPolicy, () -> SequenceNumbers.NO_OPS_PERFORMED, primaryTerm::get);
23522351
Translog.Snapshot snapshot = translog.newSnapshotFromGen(minGenForRecovery)) {

server/src/test/java/org/elasticsearch/indices/recovery/RecoveryTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ public void testDifferentHistoryUUIDDisablesOPsRecovery() throws Exception {
200200
final String historyUUIDtoUse = UUIDs.randomBase64UUID(random());
201201
if (randomBoolean()) {
202202
// create a new translog
203-
translogUUIDtoUse = Translog.createEmptyTranslog(replica.shardPath().resolveTranslog(), flushedDocs, replica.shardId());
203+
translogUUIDtoUse = Translog.createEmptyTranslog(replica.shardPath().resolveTranslog(), flushedDocs,
204+
replica.shardId(), replica.getPrimaryTerm());
204205
translogGenToUse = 1;
205206
} else {
206207
translogUUIDtoUse = translogGeneration.translogUUID;

test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ protected Translog createTranslog(LongSupplier primaryTermSupplier) throws IOExc
269269

270270
protected Translog createTranslog(Path translogPath, LongSupplier primaryTermSupplier) throws IOException {
271271
TranslogConfig translogConfig = new TranslogConfig(shardId, translogPath, INDEX_SETTINGS, BigArrays.NON_RECYCLING_INSTANCE);
272-
String translogUUID = Translog.createEmptyTranslog(translogPath, SequenceNumbers.NO_OPS_PERFORMED, shardId);
272+
String translogUUID = Translog.createEmptyTranslog(translogPath, SequenceNumbers.NO_OPS_PERFORMED, shardId,
273+
primaryTermSupplier.getAsLong());
273274
return new Translog(translogConfig, translogUUID, createTranslogDeletionPolicy(INDEX_SETTINGS),
274275
() -> SequenceNumbers.NO_OPS_PERFORMED, primaryTermSupplier);
275276
}
@@ -369,8 +370,8 @@ private InternalEngine createEngine(@Nullable IndexWriterFactory indexWriterFact
369370
final Directory directory = store.directory();
370371
if (Lucene.indexExists(directory) == false) {
371372
store.createEmpty();
372-
final String translogUuid =
373-
Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(), SequenceNumbers.NO_OPS_PERFORMED, shardId);
373+
final String translogUuid = Translog.createEmptyTranslog(config.getTranslogConfig().getTranslogPath(),
374+
SequenceNumbers.NO_OPS_PERFORMED, shardId, primaryTerm.get());
374375
store.associateIndexWithNewTranslog(translogUuid);
375376

376377
}

0 commit comments

Comments
 (0)