Skip to content

Commit fe574f8

Browse files
committed
CCR: Translog op on primary should have versionType
Normally translog operations will not be replayed on the primary. Following engine is an exception where we replay translog on both primary and replica as a non-primary strategy. Even though we won't use the version_type in the following engine, we still need to pass a valid value for the primary operation in order not to trip assertions in an engine. This commit passes version_type EXTERNAL for translog operation if its origin is primary. Relates #31945
1 parent a6b7497 commit fe574f8

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,21 +1214,23 @@ public void trimOperationOfPreviousPrimaryTerms(long aboveSeqNo) {
12141214
}
12151215

12161216
public Engine.Result applyTranslogOperation(Translog.Operation operation, Engine.Operation.Origin origin) throws IOException {
1217+
// If a translog op is replayed on the primary (eg. ccr), we need to use external instead of null for its version type.
1218+
final VersionType versionType = (origin == Engine.Operation.Origin.PRIMARY) ? VersionType.EXTERNAL : null;
12171219
final Engine.Result result;
12181220
switch (operation.opType()) {
12191221
case INDEX:
12201222
final Translog.Index index = (Translog.Index) operation;
12211223
// we set canHaveDuplicates to true all the time such that we de-optimze the translog case and ensure that all
12221224
// autoGeneratedID docs that are coming from the primary are updated correctly.
12231225
result = applyIndexOperation(index.seqNo(), index.primaryTerm(), index.version(),
1224-
null, index.getAutoGeneratedIdTimestamp(), true, origin,
1226+
versionType, index.getAutoGeneratedIdTimestamp(), true, origin,
12251227
source(shardId.getIndexName(), index.type(), index.id(), index.source(),
12261228
XContentHelper.xContentType(index.source())).routing(index.routing()));
12271229
break;
12281230
case DELETE:
12291231
final Translog.Delete delete = (Translog.Delete) operation;
12301232
result = applyDeleteOperation(delete.seqNo(), delete.primaryTerm(), delete.version(), delete.type(), delete.id(),
1231-
null, origin);
1233+
versionType, origin);
12321234
break;
12331235
case NO_OP:
12341236
final Translog.NoOp noOp = (Translog.NoOp) operation;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ private void preFlight(final Operation operation) {
4444
if (operation.seqNo() == SequenceNumbers.UNASSIGNED_SEQ_NO) {
4545
throw new IllegalStateException("a following engine does not accept operations without an assigned sequence number");
4646
}
47+
assert (operation.origin() == Operation.Origin.PRIMARY) == (operation.versionType() == VersionType.EXTERNAL) :
48+
"invalid version_type in a following engine; version_type=" + operation.versionType() + "origin=" + operation.origin();
4749
}
4850

4951
@Override

0 commit comments

Comments
 (0)