Skip to content

Commit 3321d5b

Browse files
committed
Relax maxSeqNoOfUpdates assertion in FollowingEngine (#47188)
We disable MSU optimization if the local checkpoint is smaller than max_seq_no_of_updates. Hence, we need to relax the MSU assertion in FollowingEngine for that scenario. Suppose the leader has three operations: index-0, delete-1, and index-2 for the same doc Id. MSU on the leader is 1 as index-2 is an append. If the follower applies index-0 then index-2, then the assertion is violated. Closes #47137
1 parent 5bf4376 commit 3321d5b

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2424
import org.elasticsearch.common.xcontent.XContentHelper;
2525
import org.elasticsearch.index.IndexSettings;
26+
import org.elasticsearch.index.VersionType;
2627
import org.elasticsearch.index.analysis.AnalysisRegistry;
2728
import org.elasticsearch.index.analysis.AnalyzerScope;
2829
import org.elasticsearch.index.analysis.IndexAnalyzers;
@@ -117,20 +118,23 @@ public int run(Engine engine, Translog.Snapshot snapshot) throws IOException {
117118
return opsRecovered;
118119
}
119120

120-
private Engine.Operation convertToEngineOp(Translog.Operation operation, Engine.Operation.Origin origin) {
121+
public Engine.Operation convertToEngineOp(Translog.Operation operation, Engine.Operation.Origin origin) {
122+
// If a translog op is replayed on the primary (eg. ccr), we need to use external instead of null for its version type.
123+
final VersionType versionType = (origin == Engine.Operation.Origin.PRIMARY) ? VersionType.EXTERNAL : null;
121124
switch (operation.opType()) {
122125
case INDEX:
123126
final Translog.Index index = (Translog.Index) operation;
124127
final String indexName = mapperService.index().getName();
125128
final Engine.Index engineIndex = IndexShard.prepareIndex(docMapper(index.type()),
126129
new SourceToParse(indexName, index.type(), index.id(), index.source(), XContentHelper.xContentType(index.source()),
127-
index.routing()), index.seqNo(), index.primaryTerm(),
128-
index.version(), null, origin, index.getAutoGeneratedIdTimestamp(), true, SequenceNumbers.UNASSIGNED_SEQ_NO, 0);
130+
index.routing()), index.seqNo(), index.primaryTerm(), index.version(), versionType, origin,
131+
index.getAutoGeneratedIdTimestamp(), true, SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
129132
return engineIndex;
130133
case DELETE:
131134
final Translog.Delete delete = (Translog.Delete) operation;
132135
final Engine.Delete engineDelete = new Engine.Delete(delete.type(), delete.id(), delete.uid(), delete.seqNo(),
133-
delete.primaryTerm(), delete.version(), null, origin, System.nanoTime(), SequenceNumbers.UNASSIGNED_SEQ_NO, 0);
136+
delete.primaryTerm(), delete.version(), versionType, origin, System.nanoTime(),
137+
SequenceNumbers.UNASSIGNED_SEQ_NO, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
134138
return engineDelete;
135139
case NO_OP:
136140
final Translog.NoOp noOp = (Translog.NoOp) operation;

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngine.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.apache.lucene.search.IndexSearcher;
1717
import org.apache.lucene.search.Query;
1818
import org.apache.lucene.search.TopDocs;
19+
import org.elasticsearch.Assertions;
1920
import org.elasticsearch.ElasticsearchStatusException;
2021
import org.elasticsearch.common.lucene.Lucene;
2122
import org.elasticsearch.index.VersionType;
@@ -117,7 +118,12 @@ protected long generateSeqNoForOperationOnPrimary(final Operation operation) {
117118

118119
@Override
119120
protected void advanceMaxSeqNoOfUpdatesOrDeletesOnPrimary(long seqNo) {
120-
assert getMaxSeqNoOfUpdatesOrDeletes() >= seqNo : seqNo + " < " + getMaxSeqNoOfUpdatesOrDeletes();
121+
if (Assertions.ENABLED) {
122+
final long localCheckpoint = getProcessedLocalCheckpoint();
123+
final long maxSeqNoOfUpdates = getMaxSeqNoOfUpdatesOrDeletes();
124+
assert localCheckpoint < maxSeqNoOfUpdates || maxSeqNoOfUpdates >= seqNo :
125+
"maxSeqNoOfUpdates is not advanced local_checkpoint=" + localCheckpoint + " msu=" + maxSeqNoOfUpdates + " seq_no=" + seqNo;
126+
}
121127
super.advanceMaxSeqNoOfUpdatesOrDeletesOnPrimary(seqNo); // extra safe in production code
122128
}
123129

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/index/engine/FollowingEngineTests.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ private void runFollowTest(CheckedBiConsumer<InternalEngine, FollowingEngine, Ex
484484
for (Thread thread : threads) {
485485
thread.join();
486486
}
487-
assertThat(follower.getMaxSeqNoOfUpdatesOrDeletes(), equalTo(leader.getMaxSeqNoOfUpdatesOrDeletes()));
487+
assertThat(follower.getMaxSeqNoOfUpdatesOrDeletes(), greaterThanOrEqualTo(leader.getMaxSeqNoOfUpdatesOrDeletes()));
488488
assertThat(getDocIds(follower, true), equalTo(getDocIds(leader, true)));
489489
EngineTestCase.assertConsistentHistoryBetweenTranslogAndLuceneIndex(follower, createMapperService("test"));
490490
EngineTestCase.assertAtMostOneLuceneDocumentPerSequenceNumber(follower);
@@ -535,7 +535,12 @@ private void fetchOperations(AtomicBoolean stopped, AtomicLong lastFetchedSeqNo,
535535
try (Translog.Snapshot snapshot =
536536
shuffleSnapshot(leader.newChangesSnapshot("test", mapperService, fromSeqNo, toSeqNo, true))) {
537537
follower.advanceMaxSeqNoOfUpdatesOrDeletes(leader.getMaxSeqNoOfUpdatesOrDeletes());
538-
translogHandler.run(follower, snapshot);
538+
Translog.Operation op;
539+
while ((op = snapshot.next()) != null) {
540+
EngineTestCase.applyOperation(follower,
541+
translogHandler.convertToEngineOp(op, randomFrom(Engine.Operation.Origin.values())));
542+
}
543+
follower.syncTranslog();
539544
}
540545
}
541546
}

0 commit comments

Comments
 (0)