-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fill missing sequence IDs up to max sequence ID when recovering from store #24238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
736be72
4f8b4c5
09fff06
28edc5c
1beb757
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,7 @@ | |
| import org.elasticsearch.index.mapper.RootObjectMapper; | ||
| import org.elasticsearch.index.mapper.SeqNoFieldMapper; | ||
| import org.elasticsearch.index.mapper.SourceFieldMapper; | ||
| import org.elasticsearch.index.mapper.SourceToParse; | ||
| import org.elasticsearch.index.mapper.Uid; | ||
| import org.elasticsearch.index.mapper.UidFieldMapper; | ||
| import org.elasticsearch.index.seqno.SequenceNumbers; | ||
|
|
@@ -191,7 +192,7 @@ | |
|
|
||
| public class InternalEngineTests extends ESTestCase { | ||
|
|
||
| protected final ShardId shardId = new ShardId(new Index("index", "_na_"), 1); | ||
| protected final ShardId shardId = new ShardId(new Index("index", "_na_"), 0); | ||
| private static final IndexSettings INDEX_SETTINGS = IndexSettingsModule.newIndexSettings("index", Settings.EMPTY); | ||
|
|
||
| protected ThreadPool threadPool; | ||
|
|
@@ -1961,7 +1962,7 @@ private static class MockAppender extends AbstractAppender { | |
| @Override | ||
| public void append(LogEvent event) { | ||
| final String formattedMessage = event.getMessage().getFormattedMessage(); | ||
| if (event.getLevel() == Level.TRACE && event.getMarker().getName().contains("[index][1] ")) { | ||
| if (event.getLevel() == Level.TRACE && event.getMarker().getName().contains("[index][0] ")) { | ||
| if (event.getLoggerName().endsWith(".IW") && | ||
| formattedMessage.contains("IW: apply all deletes during flush")) { | ||
| sawIndexWriterMessage = true; | ||
|
|
@@ -2341,7 +2342,7 @@ private Engine.Index indexForDoc(ParsedDocument doc) { | |
|
|
||
| private Engine.Index replicaIndexForDoc(ParsedDocument doc, long version, long seqNo, | ||
| boolean isRetry) { | ||
| return new Engine.Index(newUid(doc), doc, seqNo, 1, version, VersionType.EXTERNAL, | ||
| return new Engine.Index(newUid(doc), doc, seqNo, 1, version, VersionType.EXTERNAL, | ||
| Engine.Operation.Origin.REPLICA, System.nanoTime(), | ||
| IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, isRetry); | ||
| } | ||
|
|
@@ -3853,4 +3854,79 @@ private Tuple<Long, Long> getSequenceID(Engine engine, Engine.Get get) throws En | |
| } | ||
| } | ||
|
|
||
| public void testFillUpSequenceIdGapsOnRecovery() throws IOException { | ||
| final int docs = randomIntBetween(1, 32); | ||
| int numDocsOnReplica = 0; | ||
| long maxSeqIDOnReplica = -1; | ||
| long checkpointOnReplica; | ||
| try { | ||
| for (int i = 0; i < docs; i++) { | ||
| final String docId = Integer.toString(i); | ||
| final ParsedDocument doc = | ||
| testParsedDocument(docId, "test", null, testDocumentWithTextField(), SOURCE, null); | ||
| Engine.Index primaryResponse = indexForDoc(doc); | ||
| Engine.IndexResult indexResult = engine.index(primaryResponse); | ||
| if (randomBoolean()) { | ||
| numDocsOnReplica++; | ||
| maxSeqIDOnReplica = indexResult.getSeqNo(); | ||
| replicaEngine.index(replicaIndexForDoc(doc, 1, indexResult.getSeqNo(), false)); | ||
| } | ||
| } | ||
| checkpointOnReplica = replicaEngine.seqNoService().getLocalCheckpoint(); | ||
| } finally { | ||
| IOUtils.close(replicaEngine); | ||
| } | ||
|
|
||
|
|
||
| boolean flushed = false; | ||
| Engine recoveringEngine = null; | ||
| try { | ||
| assertEquals(docs-1, engine.seqNoService().getMaxSeqNo()); | ||
| assertEquals(docs-1, engine.seqNoService().getLocalCheckpoint()); | ||
| assertEquals(maxSeqIDOnReplica, replicaEngine.seqNoService().getMaxSeqNo()); | ||
| assertEquals(checkpointOnReplica, replicaEngine.seqNoService().getLocalCheckpoint()); | ||
| recoveringEngine = new InternalEngine(copy(replicaEngine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG)); | ||
| assertEquals(numDocsOnReplica, recoveringEngine.getTranslog().totalOperations()); | ||
| recoveringEngine.recoverFromTranslog(); | ||
| assertEquals(maxSeqIDOnReplica, recoveringEngine.seqNoService().getMaxSeqNo()); | ||
| assertEquals(checkpointOnReplica, recoveringEngine.seqNoService().getLocalCheckpoint()); | ||
| assertEquals((maxSeqIDOnReplica+1) - numDocsOnReplica, recoveringEngine.fillSequenceNumberHistory(2)); | ||
|
|
||
| // now snapshot the tlog and ensure the primary term is updated | ||
| Translog.Snapshot snapshot = recoveringEngine.getTranslog().newSnapshot(); | ||
| assertTrue((maxSeqIDOnReplica+1) - numDocsOnReplica <= snapshot.totalOperations()); | ||
| Translog.Operation operation; | ||
| while((operation = snapshot.next()) != null) { | ||
| if (operation.opType() == Translog.Operation.Type.NO_OP) { | ||
| assertEquals(2, operation.primaryTerm()); | ||
| } else { | ||
| assertEquals(1, operation.primaryTerm()); | ||
| } | ||
|
|
||
| } | ||
| assertEquals(maxSeqIDOnReplica, recoveringEngine.seqNoService().getMaxSeqNo()); | ||
| assertEquals(maxSeqIDOnReplica, recoveringEngine.seqNoService().getLocalCheckpoint()); | ||
| if ((flushed = randomBoolean())) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we snapshot the translog and assert that the noops have the right primary term?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah I had that but remvoed it... good catch... |
||
| recoveringEngine.flush(true, true); | ||
| } | ||
| } finally { | ||
| IOUtils.close(recoveringEngine); | ||
| } | ||
|
|
||
| // now do it again to make sure we preserve values etc. | ||
| try { | ||
| recoveringEngine = new InternalEngine(copy(replicaEngine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG)); | ||
| if (flushed) { | ||
| assertEquals(0, recoveringEngine.getTranslog().totalOperations()); | ||
| } | ||
| recoveringEngine.recoverFromTranslog(); | ||
| assertEquals(maxSeqIDOnReplica, recoveringEngine.seqNoService().getMaxSeqNo()); | ||
| assertEquals(maxSeqIDOnReplica, recoveringEngine.seqNoService().getLocalCheckpoint()); | ||
| assertEquals(0, recoveringEngine.fillSequenceNumberHistory(3)); | ||
| assertEquals(maxSeqIDOnReplica, recoveringEngine.seqNoService().getMaxSeqNo()); | ||
| assertEquals(maxSeqIDOnReplica, recoveringEngine.seqNoService().getLocalCheckpoint()); | ||
| } finally { | ||
| IOUtils.close(recoveringEngine); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| import org.elasticsearch.action.admin.indices.stats.CommonStats; | ||
| import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags; | ||
| import org.elasticsearch.action.admin.indices.stats.ShardStats; | ||
| import org.elasticsearch.action.index.IndexRequest; | ||
| import org.elasticsearch.action.support.PlainActionFuture; | ||
| import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
| import org.elasticsearch.cluster.metadata.MappingMetaData; | ||
|
|
@@ -77,6 +78,7 @@ | |
| import org.elasticsearch.index.mapper.ParseContext; | ||
| import org.elasticsearch.index.mapper.ParsedDocument; | ||
| import org.elasticsearch.index.mapper.SeqNoFieldMapper; | ||
| import org.elasticsearch.index.mapper.SourceToParse; | ||
| import org.elasticsearch.index.mapper.Uid; | ||
| import org.elasticsearch.index.mapper.UidFieldMapper; | ||
| import org.elasticsearch.index.seqno.SequenceNumbersService; | ||
|
|
@@ -896,6 +898,46 @@ public void testRecoverFromStore() throws IOException { | |
| closeShards(newShard); | ||
| } | ||
|
|
||
| /* This test just verifies that we fill up local checkpoint up to max seen seqID on primary recovery */ | ||
| public void testRecoverFromStoreWithNoOps() throws IOException { | ||
| final IndexShard shard = newStartedShard(true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can introduce a variant of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can do that in a sep PR |
||
| indexDoc(shard, "test", "0"); | ||
| Engine.Index test = indexDoc(shard, "test", "1"); | ||
| // start a replica shard and index the second doc | ||
| final IndexShard otherShard = newStartedShard(false); | ||
| test = otherShard.prepareIndexOnReplica( | ||
| SourceToParse.source(SourceToParse.Origin.PRIMARY, shard.shardId().getIndexName(), test.type(), test.id(), test.source(), | ||
|
||
| XContentType.JSON), | ||
| 1, 1, VersionType.EXTERNAL, IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, false); | ||
| otherShard.index(test); | ||
|
|
||
| final ShardRouting primaryShardRouting = shard.routingEntry(); | ||
| IndexShard newShard = reinitShard(otherShard, ShardRoutingHelper.initWithSameId(primaryShardRouting, | ||
| RecoverySource.StoreRecoverySource.EXISTING_STORE_INSTANCE)); | ||
| DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); | ||
| newShard.markAsRecovering("store", new RecoveryState(newShard.routingEntry(), localNode, null)); | ||
| assertTrue(newShard.recoverFromStore()); | ||
| assertEquals(1, newShard.recoveryState().getTranslog().recoveredOperations()); | ||
| assertEquals(1, newShard.recoveryState().getTranslog().totalOperations()); | ||
| assertEquals(1, newShard.recoveryState().getTranslog().totalOperationsOnStart()); | ||
| assertEquals(100.0f, newShard.recoveryState().getTranslog().recoveredPercent(), 0.01f); | ||
| Translog.Snapshot snapshot = newShard.getTranslog().newSnapshot(); | ||
| Translog.Operation operation; | ||
| int numNoops = 0; | ||
| while((operation = snapshot.next()) != null) { | ||
| if (operation.opType() == Translog.Operation.Type.NO_OP) { | ||
| numNoops++; | ||
| assertEquals(1, operation.primaryTerm()); | ||
| assertEquals(0, operation.seqNo()); | ||
| } | ||
| } | ||
| assertEquals(1, numNoops); | ||
| newShard.updateRoutingEntry(newShard.routingEntry().moveToStarted()); | ||
| assertDocCount(newShard, 1); | ||
| assertDocCount(shard, 2); | ||
| closeShards(newShard, shard); | ||
| } | ||
|
|
||
| public void testRecoverFromCleanStore() throws IOException { | ||
| final IndexShard shard = newStartedShard(true); | ||
| indexDoc(shard, "test", "0"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the local checkpoint must have advanced by at least one. We can assert on that after the noop was indexed.