Skip to content

Commit aec8fcb

Browse files
authored
Add support for closed unpromotable shards (#93240)
Today it doesn't work to close an index which holds unpromotable shards. This commit fixes that.
1 parent 48f9609 commit aec8fcb

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

server/src/internalClusterTest/java/org/elasticsearch/cluster/routing/ShardRoutingRoleIT.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.elasticsearch.index.engine.InternalEngine;
3232
import org.elasticsearch.index.engine.NoOpEngine;
3333
import org.elasticsearch.index.shard.IndexShard;
34-
import org.elasticsearch.index.translog.TranslogStats;
3534
import org.elasticsearch.indices.IndicesService;
3635
import org.elasticsearch.plugins.ClusterPlugin;
3736
import org.elasticsearch.plugins.EnginePlugin;
@@ -97,11 +96,7 @@ public Decision canForceAllocatePrimary(ShardRouting shardRouting, RoutingNode n
9796

9897
@Override
9998
public Optional<EngineFactory> getEngineFactory(IndexSettings indexSettings) {
100-
return Optional.of(
101-
config -> config.isPromotableToPrimary()
102-
? new InternalEngine(config)
103-
: new NoOpEngine(config, new TranslogStats(0, 0, 0, 0, 0))
104-
);
99+
return Optional.of(config -> config.isPromotableToPrimary() ? new InternalEngine(config) : new NoOpEngine(config));
105100
}
106101
}
107102

@@ -300,7 +295,8 @@ private void assertEngineTypes() {
300295
for (IndexShard indexShard : indexService) {
301296
final var engine = indexShard.getEngineOrNull();
302297
assertNotNull(engine);
303-
if (indexShard.routingEntry().isPromotableToPrimary()) {
298+
if (indexShard.routingEntry().isPromotableToPrimary()
299+
&& indexShard.indexSettings().getIndexMetadata().getState() == IndexMetadata.State.OPEN) {
304300
assertThat(engine, instanceOf(InternalEngine.class));
305301
} else {
306302
assertThat(engine, instanceOf(NoOpEngine.class));
@@ -453,4 +449,27 @@ public void testSearchRouting() {
453449
}
454450
}
455451

452+
public void testClosedIndex() {
453+
var routingTableWatcher = new RoutingTableWatcher();
454+
455+
var numDataNodes = routingTableWatcher.numReplicas + 2;
456+
internalCluster().ensureAtLeastNumDataNodes(numDataNodes);
457+
getMasterNodePlugin().numIndexingCopies = routingTableWatcher.numIndexingCopies;
458+
459+
final var masterClusterService = internalCluster().getCurrentMasterNodeInstance(ClusterService.class);
460+
try {
461+
// verify the correct number of shard copies of each role as the routing table evolves
462+
masterClusterService.addListener(routingTableWatcher);
463+
464+
createIndex(INDEX_NAME, routingTableWatcher.getIndexSettings());
465+
ensureGreen(INDEX_NAME);
466+
assertEngineTypes();
467+
468+
assertAcked(client().admin().indices().prepareClose(INDEX_NAME));
469+
ensureGreen(INDEX_NAME);
470+
assertEngineTypes();
471+
} finally {
472+
masterClusterService.removeListener(routingTableWatcher);
473+
}
474+
}
456475
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.common.lucene.Lucene;
1919
import org.elasticsearch.common.util.concurrent.ReleasableLock;
2020
import org.elasticsearch.core.Nullable;
21+
import org.elasticsearch.index.seqno.SeqNoStats;
2122
import org.elasticsearch.index.seqno.SequenceNumbers;
2223
import org.elasticsearch.index.shard.DocsStats;
2324
import org.elasticsearch.index.store.Store;
@@ -45,11 +46,21 @@ public final class NoOpEngine extends ReadOnlyEngine {
4546
private final DocsStats docsStats;
4647

4748
public NoOpEngine(EngineConfig config) {
48-
this(config, null);
49+
this(
50+
config,
51+
config.isPromotableToPrimary() ? null : new TranslogStats(0, 0, 0, 0, 0),
52+
config.isPromotableToPrimary()
53+
? null
54+
: new SeqNoStats(
55+
config.getGlobalCheckpointSupplier().getAsLong(),
56+
config.getGlobalCheckpointSupplier().getAsLong(),
57+
config.getGlobalCheckpointSupplier().getAsLong()
58+
)
59+
);
4960
}
5061

51-
public NoOpEngine(EngineConfig config, @Nullable TranslogStats translogStats) {
52-
super(config, null, translogStats, true, Function.identity(), true, true);
62+
public NoOpEngine(EngineConfig config, @Nullable TranslogStats translogStats, SeqNoStats seqNoStats) {
63+
super(config, seqNoStats, translogStats, true, Function.identity(), true, true);
5364
this.segmentsStats = new SegmentsStats();
5465
Directory directory = store.directory();
5566
try (DirectoryReader reader = openDirectory(directory)) {

0 commit comments

Comments
 (0)