Skip to content

Commit 042ceb4

Browse files
ywelschdnhatn
authored andcommitted
Noop peer recoveries on closed index
1 parent 38bd362 commit 042ceb4

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,10 @@ public Translog.Snapshot newChangesSnapshot(String source, MapperService mapperS
25582558
@Override
25592559
public boolean hasCompleteOperationHistory(String source, MapperService mapperService, long startingSeqNo) throws IOException {
25602560
final long currentLocalCheckpoint = getLocalCheckpointTracker().getCheckpoint();
2561+
// avoid scanning translog if not necessary
2562+
if (startingSeqNo > currentLocalCheckpoint) {
2563+
return true;
2564+
}
25612565
final LocalCheckpointTracker tracker = new LocalCheckpointTracker(startingSeqNo, startingSeqNo - 1);
25622566
try (Translog.Snapshot snapshot = getTranslog().newSnapshotFromMinSeqNo(startingSeqNo)) {
25632567
Translog.Operation operation;

server/src/main/java/org/elasticsearch/index/engine/ReadOnlyEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public int estimateNumberOfHistoryOperations(String source, MapperService mapper
300300

301301
@Override
302302
public boolean hasCompleteOperationHistory(String source, MapperService mapperService, long startingSeqNo) throws IOException {
303-
return false;
303+
return startingSeqNo > seqNoStats.getLocalCheckpoint();
304304
}
305305

306306
@Override

server/src/test/java/org/elasticsearch/indices/state/CloseIndexIT.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.index.IndexNotFoundException;
3535
import org.elasticsearch.index.IndexSettings;
3636
import org.elasticsearch.indices.IndexClosedException;
37+
import org.elasticsearch.indices.recovery.RecoveryState;
3738
import org.elasticsearch.test.BackgroundIndexer;
3839
import org.elasticsearch.test.ESIntegTestCase;
3940

@@ -50,6 +51,7 @@
5051
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
5152
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
5253
import static org.hamcrest.Matchers.containsString;
54+
import static org.hamcrest.Matchers.empty;
5355
import static org.hamcrest.Matchers.equalTo;
5456
import static org.hamcrest.Matchers.hasSize;
5557
import static org.hamcrest.Matchers.is;
@@ -338,6 +340,33 @@ public void testCloseIndexWaitForActiveShards() throws Exception {
338340
assertIndexIsClosed(indexName);
339341
}
340342

343+
public void testNoopPeerRecoveriesWhenIndexClosed() throws Exception {
344+
final String indexName = "peer-recovery-test";
345+
int numberOfReplicas = between(1, 2);
346+
internalCluster().ensureAtLeastNumDataNodes(numberOfReplicas + between(1, 2));
347+
createIndex(indexName, Settings.builder()
348+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
349+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, numberOfReplicas)
350+
.put("index.routing.rebalance.enable", "none")
351+
.build());
352+
indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, randomIntBetween(0, 50))
353+
.mapToObj(i -> client().prepareIndex(indexName, "_doc").setSource("num", i)).collect(toList()));
354+
ensureGreen(indexName);
355+
356+
// Closing an index should execute noop peer recovery
357+
assertAcked(client().admin().indices().prepareClose(indexName).get());
358+
assertIndexIsClosed(indexName);
359+
ensureGreen(indexName);
360+
assertNoFileBasedRecovery(indexName);
361+
362+
// Open a closed index should execute noop recovery
363+
assertAcked(client().admin().indices().prepareOpen(indexName).get());
364+
assertIndexIsOpened(indexName);
365+
ensureGreen(indexName);
366+
assertNoFileBasedRecovery(indexName);
367+
internalCluster().assertSameDocIdsOnShards();
368+
}
369+
341370
static void assertIndexIsClosed(final String... indices) {
342371
final ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
343372
for (String index : indices) {
@@ -383,4 +412,12 @@ static void assertException(final Throwable throwable, final String indexName) {
383412
fail("Unexpected exception: " + t);
384413
}
385414
}
415+
416+
void assertNoFileBasedRecovery(String indexName) {
417+
for (RecoveryState recovery : client().admin().indices().prepareRecoveries(indexName).get().shardRecoveryStates().get(indexName)) {
418+
if (recovery.getPrimary() == false) {
419+
assertThat(recovery.getIndex().fileDetails(), empty());
420+
}
421+
}
422+
}
386423
}

0 commit comments

Comments
 (0)