Skip to content

Commit 0519016

Browse files
authored
Add replica to primary promotion test for closed indices (#39110)
This commit adds a simple test which verifies that a replica can be promoted as a primary when the index is closed. Relates to #33888
1 parent 538cdcd commit 0519016

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.indices.recovery;
20+
21+
import org.elasticsearch.cluster.ClusterState;
22+
import org.elasticsearch.cluster.metadata.IndexMetaData;
23+
import org.elasticsearch.cluster.node.DiscoveryNode;
24+
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
25+
import org.elasticsearch.cluster.routing.ShardRouting;
26+
import org.elasticsearch.test.BackgroundIndexer;
27+
import org.elasticsearch.test.ESIntegTestCase;
28+
import org.elasticsearch.test.InternalTestCluster;
29+
30+
import java.util.Locale;
31+
32+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
33+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
34+
import static org.hamcrest.Matchers.is;
35+
36+
@ESIntegTestCase.ClusterScope(numDataNodes = 2)
37+
public class ReplicaToPrimaryPromotionIT extends ESIntegTestCase {
38+
39+
@Override
40+
protected int numberOfReplicas() {
41+
return 1;
42+
}
43+
44+
public void testPromoteReplicaToPrimary() throws Exception {
45+
final String indexName = randomAlphaOfLength(5).toLowerCase(Locale.ROOT);
46+
createIndex(indexName);
47+
48+
final int numOfDocs = scaledRandomIntBetween(0, 200);
49+
try (BackgroundIndexer indexer = new BackgroundIndexer(indexName, "_doc", client(), numOfDocs)) {
50+
waitForDocs(numOfDocs, indexer);
51+
}
52+
refresh(indexName);
53+
54+
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs);
55+
ensureGreen(indexName);
56+
57+
// sometimes test with a closed index
58+
final IndexMetaData.State indexState = randomFrom(IndexMetaData.State.OPEN, IndexMetaData.State.CLOSE);
59+
if (indexState == IndexMetaData.State.CLOSE) {
60+
assertAcked(client().admin().indices().prepareClose(indexName));
61+
ensureGreen(indexName);
62+
}
63+
64+
// pick up a data node that contains a random primary shard
65+
ClusterState state = client(internalCluster().getMasterName()).admin().cluster().prepareState().get().getState();
66+
final int numShards = state.metaData().index(indexName).getNumberOfShards();
67+
final ShardRouting primaryShard = state.routingTable().index(indexName).shard(randomIntBetween(0, numShards - 1)).primaryShard();
68+
final DiscoveryNode randomNode = state.nodes().resolveNode(primaryShard.currentNodeId());
69+
70+
// stop the random data node, all remaining shards are promoted to primaries
71+
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(randomNode.getName()));
72+
ensureYellowAndNoInitializingShards(indexName);
73+
74+
state = client(internalCluster().getMasterName()).admin().cluster().prepareState().get().getState();
75+
for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(indexName)) {
76+
for (ShardRouting shardRouting : shardRoutingTable.activeShards()) {
77+
assertThat(shardRouting + " should be promoted as a primary", shardRouting.primary(), is(true));
78+
}
79+
}
80+
81+
if (indexState == IndexMetaData.State.CLOSE) {
82+
assertAcked(client().admin().indices().prepareOpen(indexName));
83+
ensureYellowAndNoInitializingShards(indexName);
84+
}
85+
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs);
86+
}
87+
}

0 commit comments

Comments
 (0)