Skip to content

Commit dcb60ab

Browse files
committed
Initialize checkpoint tracker with allocation ID
This commit pushes the allocation ID down through to the global checkpoint tracker at construction rather than when activated as a primary. Relates #26630
1 parent c2acdcb commit dcb60ab

File tree

9 files changed

+99
-59
lines changed

9 files changed

+99
-59
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public abstract class Engine implements Closeable {
9797
public static final String SYNC_COMMIT_ID = "sync_id";
9898

9999
protected final ShardId shardId;
100+
protected final String allocationId;
100101
protected final Logger logger;
101102
protected final EngineConfig engineConfig;
102103
protected final Store store;
@@ -126,6 +127,7 @@ protected Engine(EngineConfig engineConfig) {
126127

127128
this.engineConfig = engineConfig;
128129
this.shardId = engineConfig.getShardId();
130+
this.allocationId = engineConfig.getAllocationId();
129131
this.store = engineConfig.getStore();
130132
this.logger = Loggers.getLogger(Engine.class, // we use the engine class directly here to make sure all subclasses have the same logger name
131133
engineConfig.getIndexSettings().getSettings(), engineConfig.getShardId());

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
*/
5252
public final class EngineConfig {
5353
private final ShardId shardId;
54+
private final String allocationId;
5455
private final IndexSettings indexSettings;
5556
private final ByteSizeValue indexingBufferSize;
5657
private volatile boolean enableGcDeletes = true;
@@ -109,7 +110,7 @@ public final class EngineConfig {
109110
/**
110111
* Creates a new {@link org.elasticsearch.index.engine.EngineConfig}
111112
*/
112-
public EngineConfig(OpenMode openMode, ShardId shardId, ThreadPool threadPool,
113+
public EngineConfig(OpenMode openMode, ShardId shardId, String allocationId, ThreadPool threadPool,
113114
IndexSettings indexSettings, Engine.Warmer warmer, Store store,
114115
MergePolicy mergePolicy, Analyzer analyzer,
115116
Similarity similarity, CodecService codecService, Engine.EventListener eventListener,
@@ -120,6 +121,7 @@ public EngineConfig(OpenMode openMode, ShardId shardId, ThreadPool threadPool,
120121
throw new IllegalArgumentException("openMode must not be null");
121122
}
122123
this.shardId = shardId;
124+
this.allocationId = allocationId;
123125
this.indexSettings = indexSettings;
124126
this.threadPool = threadPool;
125127
this.warmer = warmer == null ? (a) -> {} : warmer;
@@ -240,6 +242,15 @@ public IndexSettings getIndexSettings() {
240242
*/
241243
public ShardId getShardId() { return shardId; }
242244

245+
/**
246+
* Returns the allocation ID for the shard.
247+
*
248+
* @return the allocation ID
249+
*/
250+
public String getAllocationId() {
251+
return allocationId;
252+
}
253+
243254
/**
244255
* Returns the analyzer as the default analyzer in the engines {@link org.apache.lucene.index.IndexWriter}
245256
*/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public InternalEngine(EngineConfig engineConfig) throws EngineException {
192192
throw new IllegalArgumentException(openMode.toString());
193193
}
194194
logger.trace("recovered [{}]", seqNoStats);
195-
seqNoService = sequenceNumberService(shardId, engineConfig.getIndexSettings(), seqNoStats);
195+
seqNoService = sequenceNumberService(shardId, allocationId, engineConfig.getIndexSettings(), seqNoStats);
196196
updateMaxUnsafeAutoIdTimestampFromWriter(writer);
197197
indexWriter = writer;
198198
translog = openTranslog(engineConfig, writer, translogDeletionPolicy, () -> seqNoService().getGlobalCheckpoint());
@@ -283,10 +283,12 @@ private void updateMaxUnsafeAutoIdTimestampFromWriter(IndexWriter writer) {
283283

284284
private static SequenceNumbersService sequenceNumberService(
285285
final ShardId shardId,
286+
final String allocationId,
286287
final IndexSettings indexSettings,
287288
final SeqNoStats seqNoStats) {
288289
return new SequenceNumbersService(
289290
shardId,
291+
allocationId,
290292
indexSettings,
291293
seqNoStats.getMaxSeqNo(),
292294
seqNoStats.getLocalCheckpoint(),

core/src/main/java/org/elasticsearch/index/seqno/GlobalCheckpointTracker.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
*/
5151
public class GlobalCheckpointTracker extends AbstractIndexShardComponent {
5252

53+
private final String allocationId;
54+
5355
/**
5456
* The global checkpoint tracker can operate in two modes:
5557
* - primary: this shard is in charge of collecting local checkpoint information from all shard copies and computing the global
@@ -245,12 +247,18 @@ private boolean invariant() {
245247
* {@link SequenceNumbers#UNASSIGNED_SEQ_NO}.
246248
*
247249
* @param shardId the shard ID
250+
* @param allocationId the allocation ID
248251
* @param indexSettings the index settings
249252
* @param globalCheckpoint the last known global checkpoint for this shard, or {@link SequenceNumbers#UNASSIGNED_SEQ_NO}
250253
*/
251-
GlobalCheckpointTracker(final ShardId shardId, final IndexSettings indexSettings, final long globalCheckpoint) {
254+
GlobalCheckpointTracker(
255+
final ShardId shardId,
256+
final String allocationId,
257+
final IndexSettings indexSettings,
258+
final long globalCheckpoint) {
252259
super(shardId, indexSettings);
253260
assert globalCheckpoint >= SequenceNumbers.UNASSIGNED_SEQ_NO : "illegal initial global checkpoint: " + globalCheckpoint;
261+
this.allocationId = allocationId;
254262
this.primaryMode = false;
255263
this.handoffInProgress = false;
256264
this.appliedClusterStateVersion = -1L;
@@ -310,7 +318,7 @@ public synchronized void updateGlobalCheckpointOnReplica(final long globalCheckp
310318
/**
311319
* Initializes the global checkpoint tracker in primary mode (see {@link #primaryMode}. Called on primary activation or promotion.
312320
*/
313-
public synchronized void activatePrimaryMode(final String allocationId, final long localCheckpoint) {
321+
public synchronized void activatePrimaryMode(final long localCheckpoint) {
314322
assert invariant();
315323
assert primaryMode == false;
316324
assert localCheckpoints.get(allocationId) != null && localCheckpoints.get(allocationId).inSync &&

core/src/main/java/org/elasticsearch/index/seqno/SequenceNumbersService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ public class SequenceNumbersService extends AbstractIndexShardComponent {
5454
*/
5555
public SequenceNumbersService(
5656
final ShardId shardId,
57+
final String allocationId,
5758
final IndexSettings indexSettings,
5859
final long maxSeqNo,
5960
final long localCheckpoint,
6061
final long globalCheckpoint) {
6162
super(shardId, indexSettings);
6263
localCheckpointTracker = new LocalCheckpointTracker(indexSettings, maxSeqNo, localCheckpoint);
63-
globalCheckpointTracker = new GlobalCheckpointTracker(shardId, indexSettings, globalCheckpoint);
64+
globalCheckpointTracker = new GlobalCheckpointTracker(shardId, allocationId, indexSettings, globalCheckpoint);
6465
}
6566

6667
/**
@@ -201,7 +202,7 @@ public synchronized long getTrackedLocalCheckpointForShard(final String allocati
201202
* Called on primary activation or promotion.
202203
*/
203204
public void activatePrimaryMode(final String allocationId, final long localCheckpoint) {
204-
globalCheckpointTracker.activatePrimaryMode(allocationId, localCheckpoint);
205+
globalCheckpointTracker.activatePrimaryMode(localCheckpoint);
205206
}
206207

207208
/**

core/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,7 @@ private DocumentMapperForType docMapper(String type) {
20742074

20752075
private EngineConfig newEngineConfig(EngineConfig.OpenMode openMode) {
20762076
Sort indexSort = indexSortSupplier.get();
2077-
return new EngineConfig(openMode, shardId,
2077+
return new EngineConfig(openMode, shardId, shardRouting.allocationId().getId(),
20782078
threadPool, indexSettings, warmer, store, indexSettings.getMergePolicy(),
20792079
mapperService.indexAnalyzer(), similarityService.similarity(mapperService), codecService, shardEventListener,
20802080
indexCache.query(), cachingPolicy, translogConfig,

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@
7373
import org.elasticsearch.action.index.IndexRequest;
7474
import org.elasticsearch.action.support.TransportActions;
7575
import org.elasticsearch.cluster.metadata.IndexMetaData;
76+
import org.elasticsearch.cluster.routing.AllocationId;
7677
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
7778
import org.elasticsearch.cluster.routing.ShardRouting;
7879
import org.elasticsearch.cluster.routing.ShardRoutingState;
7980
import org.elasticsearch.cluster.routing.TestShardRouting;
8081
import org.elasticsearch.common.Nullable;
8182
import org.elasticsearch.common.Randomness;
8283
import org.elasticsearch.common.Strings;
84+
import org.elasticsearch.common.UUIDs;
8385
import org.elasticsearch.common.bytes.BytesArray;
8486
import org.elasticsearch.common.bytes.BytesReference;
8587
import org.elasticsearch.common.collect.Tuple;
@@ -198,6 +200,7 @@
198200
public class InternalEngineTests extends ESTestCase {
199201

200202
protected final ShardId shardId = new ShardId(new Index("index", "_na_"), 0);
203+
protected final AllocationId allocationId = AllocationId.newInitializing();
201204
private static final IndexSettings INDEX_SETTINGS = IndexSettingsModule.newIndexSettings("index", Settings.EMPTY);
202205

203206
protected ThreadPool threadPool;
@@ -264,11 +267,11 @@ public EngineConfig copy(EngineConfig config, EngineConfig.OpenMode openMode) {
264267
}
265268

266269
public EngineConfig copy(EngineConfig config, EngineConfig.OpenMode openMode, Analyzer analyzer) {
267-
return new EngineConfig(openMode, config.getShardId(), config.getThreadPool(), config.getIndexSettings(), config.getWarmer(),
268-
config.getStore(), config.getMergePolicy(), analyzer, config.getSimilarity(),
269-
new CodecService(null, logger), config.getEventListener(), config.getQueryCache(),
270-
config.getQueryCachingPolicy(), config.getTranslogConfig(),
271-
config.getFlushMergesAfter(), config.getRefreshListeners(), config.getIndexSort(), config.getTranslogRecoveryRunner());
270+
return new EngineConfig(openMode, config.getShardId(), config.getAllocationId(), config.getThreadPool(), config.getIndexSettings(),
271+
config.getWarmer(), config.getStore(), config.getMergePolicy(), analyzer, config.getSimilarity(),
272+
new CodecService(null, logger), config.getEventListener(), config.getQueryCache(), config.getQueryCachingPolicy(),
273+
config.getTranslogConfig(), config.getFlushMergesAfter(), config.getRefreshListeners(), config.getIndexSort(),
274+
config.getTranslogRecoveryRunner());
272275
}
273276

274277
@Override
@@ -447,7 +450,7 @@ public void onFailedEngine(String reason, @Nullable Exception e) {
447450
indexSettings.getSettings()));
448451
final List<ReferenceManager.RefreshListener> refreshListenerList =
449452
refreshListener == null ? emptyList() : Collections.singletonList(refreshListener);
450-
EngineConfig config = new EngineConfig(openMode, shardId, threadPool, indexSettings, null, store,
453+
EngineConfig config = new EngineConfig(openMode, shardId, allocationId.getId(), threadPool, indexSettings, null, store,
451454
mergePolicy, iwc.getAnalyzer(), iwc.getSimilarity(), new CodecService(null, logger), listener,
452455
IndexSearcher.getDefaultQueryCache(), IndexSearcher.getDefaultQueryCachingPolicy(), translogConfig,
453456
TimeValue.timeValueMinutes(5), refreshListenerList, indexSort, handler);
@@ -728,6 +731,7 @@ public void testCommitStats() throws IOException {
728731
Store store = createStore();
729732
InternalEngine engine = createEngine(store, createTempDir(), (config) -> new SequenceNumbersService(
730733
config.getShardId(),
734+
config.getAllocationId(),
731735
config.getIndexSettings(),
732736
maxSeqNo.get(),
733737
localCheckpoint.get(),
@@ -901,6 +905,7 @@ public void testTranslogRecoveryWithMultipleGenerations() throws IOException {
901905
initialEngine = createEngine(store, createTempDir(), (config) ->
902906
new SequenceNumbersService(
903907
config.getShardId(),
908+
config.getAllocationId(),
904909
config.getIndexSettings(),
905910
SequenceNumbers.NO_OPS_PERFORMED,
906911
SequenceNumbers.NO_OPS_PERFORMED,
@@ -2028,7 +2033,7 @@ public void testSeqNoAndCheckpoints() throws IOException {
20282033

20292034
try {
20302035
initialEngine = engine;
2031-
final ShardRouting primary = TestShardRouting.newShardRouting(shardId, "node1", true, ShardRoutingState.STARTED);
2036+
final ShardRouting primary = TestShardRouting.newShardRouting("test", shardId.id(), "node1", null, true, ShardRoutingState.STARTED, allocationId);
20322037
final ShardRouting replica = TestShardRouting.newShardRouting(shardId, "node2", false, ShardRoutingState.STARTED);
20332038
initialEngine.seqNoService().updateAllocationIdsFromMaster(1L, new HashSet<>(Arrays.asList(primary.allocationId().getId(),
20342039
replica.allocationId().getId())),
@@ -2788,12 +2793,11 @@ public void testRecoverFromForeignTranslog() throws IOException {
27882793
TranslogConfig translogConfig = new TranslogConfig(shardId, translog.location(), config.getIndexSettings(),
27892794
BigArrays.NON_RECYCLING_INSTANCE);
27902795

2791-
EngineConfig brokenConfig = new EngineConfig(EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG, shardId, threadPool,
2792-
config.getIndexSettings(), null, store, newMergePolicy(), config.getAnalyzer(),
2793-
config.getSimilarity(), new CodecService(null, logger), config.getEventListener(),
2794-
IndexSearcher.getDefaultQueryCache(), IndexSearcher.getDefaultQueryCachingPolicy(), translogConfig,
2795-
TimeValue.timeValueMinutes(5), config.getRefreshListeners(), null,
2796-
config.getTranslogRecoveryRunner());
2796+
EngineConfig brokenConfig = new EngineConfig(EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG, shardId, allocationId.getId(),
2797+
threadPool, config.getIndexSettings(), null, store, newMergePolicy(), config.getAnalyzer(), config.getSimilarity(),
2798+
new CodecService(null, logger), config.getEventListener(), IndexSearcher.getDefaultQueryCache(),
2799+
IndexSearcher.getDefaultQueryCachingPolicy(), translogConfig, TimeValue.timeValueMinutes(5), config.getRefreshListeners(),
2800+
null, config.getTranslogRecoveryRunner());
27972801

27982802
try {
27992803
InternalEngine internalEngine = new InternalEngine(brokenConfig);
@@ -3628,6 +3632,7 @@ private SequenceNumbersService getStallingSeqNoService(
36283632
final AtomicLong expectedLocalCheckpoint) {
36293633
return new SequenceNumbersService(
36303634
shardId,
3635+
allocationId.getId(),
36313636
defaultSettings,
36323637
SequenceNumbers.NO_OPS_PERFORMED,
36333638
SequenceNumbers.NO_OPS_PERFORMED,
@@ -3839,7 +3844,7 @@ public void testNoOps() throws IOException {
38393844
final int globalCheckpoint = randomIntBetween(0, localCheckpoint);
38403845
try {
38413846
final SequenceNumbersService seqNoService =
3842-
new SequenceNumbersService(shardId, defaultSettings, maxSeqNo, localCheckpoint, globalCheckpoint) {
3847+
new SequenceNumbersService(shardId, allocationId.getId(), defaultSettings, maxSeqNo, localCheckpoint, globalCheckpoint) {
38433848
@Override
38443849
public long generateSeqNo() {
38453850
throw new UnsupportedOperationException();
@@ -3986,6 +3991,7 @@ public void testRestoreLocalCheckpointFromTranslog() throws IOException {
39863991
final SequenceNumbersService seqNoService =
39873992
new SequenceNumbersService(
39883993
shardId,
3994+
allocationId.getId(),
39893995
defaultSettings,
39903996
SequenceNumbers.NO_OPS_PERFORMED,
39913997
SequenceNumbers.NO_OPS_PERFORMED,

0 commit comments

Comments
 (0)