Skip to content

Commit bbdebeb

Browse files
committed
add test
1 parent c1bbc08 commit bbdebeb

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import org.elasticsearch.index.store.FsDirectoryFactory;
134134
import org.elasticsearch.index.store.Store;
135135
import org.elasticsearch.index.translog.SnapshotMatchers;
136+
import org.elasticsearch.index.translog.TestTranslog;
136137
import org.elasticsearch.index.translog.Translog;
137138
import org.elasticsearch.index.translog.TranslogConfig;
138139
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
@@ -183,6 +184,7 @@
183184
import static org.elasticsearch.index.engine.Engine.Operation.Origin.PEER_RECOVERY;
184185
import static org.elasticsearch.index.engine.Engine.Operation.Origin.PRIMARY;
185186
import static org.elasticsearch.index.engine.Engine.Operation.Origin.REPLICA;
187+
import static org.elasticsearch.index.seqno.SequenceNumbers.NO_OPS_PERFORMED;
186188
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_PRIMARY_TERM;
187189
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
188190
import static org.elasticsearch.index.translog.TranslogDeletionPolicies.createTranslogDeletionPolicy;
@@ -5931,4 +5933,34 @@ public long addDocument(Iterable<? extends IndexableField> doc) throws IOExcepti
59315933
assertNotNull(engine.failedEngine.get());
59325934
}
59335935
}
5936+
5937+
/**
5938+
* We can trim translog on the primary promotion and peer recovery based on the fact we add operations with either
5939+
* REPLICA or PEER_RECOVERY origin to translog although they already exist in the engine (i.e. hasProcessed() == true).
5940+
* If we decide not to add those already-processed operations to translog, we need to study carefully the consequence
5941+
* of the translog trimming in these two places.
5942+
*/
5943+
public void testAlwaysRecordReplicaOrPeerRecoveryOperationsToTranslog() throws Exception {
5944+
List<Engine.Operation> operations = generateHistoryOnReplica(between(1, 100), randomBoolean(), randomBoolean(), randomBoolean());
5945+
applyOperations(engine, operations);
5946+
Set<Long> seqNos = operations.stream().map(Engine.Operation::seqNo).collect(Collectors.toSet());
5947+
try (Translog.Snapshot snapshot = getTranslog(engine).newSnapshot()) {
5948+
assertThat(snapshot.totalOperations(), equalTo(operations.size()));
5949+
assertThat(TestTranslog.drainSnapshot(snapshot, false).stream().map(Translog.Operation::seqNo).collect(Collectors.toSet()),
5950+
equalTo(seqNos));
5951+
}
5952+
primaryTerm.set(randomLongBetween(primaryTerm.get(), Long.MAX_VALUE));
5953+
engine.rollTranslogGeneration();
5954+
engine.trimOperationsFromTranslog(primaryTerm.get(), NO_OPS_PERFORMED);
5955+
try (Translog.Snapshot snapshot = getTranslog(engine).newSnapshot()) {
5956+
assertThat(snapshot.totalOperations(), equalTo(operations.size()));
5957+
assertNull(snapshot.next());
5958+
}
5959+
applyOperations(engine, operations);
5960+
try (Translog.Snapshot snapshot = getTranslog(engine).newSnapshot()) {
5961+
assertThat(snapshot.totalOperations(), equalTo(operations.size() * 2));
5962+
assertThat(TestTranslog.drainSnapshot(snapshot, false).stream().map(Translog.Operation::seqNo).collect(Collectors.toSet()),
5963+
equalTo(seqNos));
5964+
}
5965+
}
59345966
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129

130130
import static java.util.Collections.emptyList;
131131
import static java.util.Collections.shuffle;
132+
import static org.elasticsearch.index.engine.Engine.Operation.Origin.PEER_RECOVERY;
132133
import static org.elasticsearch.index.engine.Engine.Operation.Origin.PRIMARY;
133134
import static org.elasticsearch.index.engine.Engine.Operation.Origin.REPLICA;
134135
import static org.elasticsearch.index.translog.TranslogDeletionPolicies.createTranslogDeletionPolicy;
@@ -847,14 +848,15 @@ public List<Engine.Operation> generateHistoryOnReplica(int numOps, boolean allow
847848
switch (opType) {
848849
case INDEX:
849850
operations.add(new Engine.Index(EngineTestCase.newUid(doc), doc, seqNo, primaryTerm.get(),
850-
i, null, Engine.Operation.Origin.REPLICA, startTime, -1, true, SequenceNumbers.UNASSIGNED_SEQ_NO, 0));
851+
i, null, randomFrom(REPLICA, PEER_RECOVERY), startTime, -1, true, SequenceNumbers.UNASSIGNED_SEQ_NO, 0));
851852
break;
852853
case DELETE:
853854
operations.add(new Engine.Delete(doc.type(), doc.id(), EngineTestCase.newUid(doc), seqNo, primaryTerm.get(),
854-
i, null, Engine.Operation.Origin.REPLICA, startTime, SequenceNumbers.UNASSIGNED_SEQ_NO, 0));
855+
i, null, randomFrom(REPLICA, PEER_RECOVERY), startTime, SequenceNumbers.UNASSIGNED_SEQ_NO, 0));
855856
break;
856857
case NO_OP:
857-
operations.add(new Engine.NoOp(seqNo, primaryTerm.get(), Engine.Operation.Origin.REPLICA, startTime, "test-" + i));
858+
operations.add(new Engine.NoOp(seqNo, primaryTerm.get(),
859+
randomFrom(REPLICA, PEER_RECOVERY), startTime, "test-" + i));
858860
break;
859861
default:
860862
throw new IllegalStateException("Unknown operation type [" + opType + "]");

0 commit comments

Comments
 (0)