Skip to content

Commit e42e4a5

Browse files
Fix Flaky Test SpecificClusterManagerNodesIT.testElectOnlyBetweenClusterManagerNodes (#16021)
Signed-off-by: kkewwei <kewei.11@bytedance.com> Signed-off-by: kkewwei <kkewwei@163.com> (cherry picked from commit 852011a) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 00ae831 commit e42e4a5

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

server/src/internalClusterTest/java/org/opensearch/cluster/SpecificClusterManagerNodesIT.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.opensearch.test.OpenSearchIntegTestCase.Scope;
4545

4646
import java.io.IOException;
47+
import java.util.function.Supplier;
4748

4849
import static org.opensearch.test.NodeRoles.clusterManagerNode;
4950
import static org.opensearch.test.NodeRoles.dataOnlyNode;
@@ -254,9 +255,9 @@ public void testElectOnlyBetweenClusterManagerNodes() throws Exception {
254255
logger.info("--> closing cluster-manager node (1)");
255256
client().execute(AddVotingConfigExclusionsAction.INSTANCE, new AddVotingConfigExclusionsRequest(clusterManagerNodeName)).get();
256257
// removing the cluster-manager from the voting configuration immediately triggers the cluster-manager to step down
257-
assertBusy(() -> {
258-
assertThat(
259-
internalCluster().nonClusterManagerClient()
258+
Supplier<String> getClusterManagerIfElected = () -> {
259+
try {
260+
return internalCluster().nonClusterManagerClient()
260261
.admin()
261262
.cluster()
262263
.prepareState()
@@ -265,9 +266,14 @@ public void testElectOnlyBetweenClusterManagerNodes() throws Exception {
265266
.getState()
266267
.nodes()
267268
.getClusterManagerNode()
268-
.getName(),
269-
equalTo(nextClusterManagerEligableNodeName)
270-
);
269+
.getName();
270+
} catch (ClusterManagerNotDiscoveredException e) {
271+
logger.debug("failed to get cluster-manager name", e);
272+
return null;
273+
}
274+
};
275+
assertBusy(() -> {
276+
assertThat(getClusterManagerIfElected.get(), equalTo(nextClusterManagerEligableNodeName));
271277
assertThat(
272278
internalCluster().clusterManagerClient()
273279
.admin()

test/framework/src/main/java/org/opensearch/test/InternalTestCluster.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import org.opensearch.core.index.shard.ShardId;
9090
import org.opensearch.core.indices.breaker.CircuitBreakerService;
9191
import org.opensearch.core.util.FileSystemUtils;
92+
import org.opensearch.discovery.ClusterManagerNotDiscoveredException;
9293
import org.opensearch.env.Environment;
9394
import org.opensearch.env.NodeEnvironment;
9495
import org.opensearch.env.ShardLockObtainFailedException;
@@ -2155,13 +2156,12 @@ public String getClusterManagerName() {
21552156
* in the viaNode parameter. If viaNode isn't specified a random node will be picked to the send the request to.
21562157
*/
21572158
public String getClusterManagerName(@Nullable String viaNode) {
2158-
try {
2159-
Client client = viaNode != null ? client(viaNode) : client();
2160-
return client.admin().cluster().prepareState().get().getState().nodes().getClusterManagerNode().getName();
2161-
} catch (Exception e) {
2162-
logger.warn("Can't fetch cluster state", e);
2163-
throw new RuntimeException("Can't get cluster-manager node " + e.getMessage(), e);
2159+
Client client = viaNode != null ? client(viaNode) : client();
2160+
DiscoveryNode clusterManagerNode = client.admin().cluster().prepareState().get().getState().nodes().getClusterManagerNode();
2161+
if (clusterManagerNode == null) {
2162+
throw new ClusterManagerNotDiscoveredException("Cluster manager node not discovered");
21642163
}
2164+
return clusterManagerNode.getName();
21652165
}
21662166

21672167
/**

0 commit comments

Comments
 (0)