|
28 | 28 | import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag; |
29 | 29 | import org.elasticsearch.action.support.DestructiveOperations; |
30 | 30 | import org.elasticsearch.action.support.RefCountingRunnable; |
| 31 | +import org.elasticsearch.action.support.SubscribableListener; |
31 | 32 | import org.elasticsearch.action.support.replication.TransportReplicationAction; |
32 | 33 | import org.elasticsearch.client.internal.Client; |
33 | 34 | import org.elasticsearch.cluster.ClusterName; |
@@ -2038,13 +2039,12 @@ public String getMasterName(@Nullable String viaNode) { |
2038 | 2039 | throw new AssertionError("Unable to get master name, no node found"); |
2039 | 2040 | } |
2040 | 2041 | try { |
2041 | | - ClusterServiceUtils.awaitClusterState(logger, state -> state.nodes().getMasterNode() != null, clusterService(viaNode)); |
2042 | | - final ClusterState state = client(viaNode).admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState(); |
2043 | | - final DiscoveryNode masterNode = state.nodes().getMasterNode(); |
2044 | | - if (masterNode == null) { |
2045 | | - throw new AssertionError("Master is not stable but the method expects a stable master node"); |
2046 | | - } |
2047 | | - return masterNode.getName(); |
| 2042 | + final var masterNameListener = new SubscribableListener<String>(); |
| 2043 | + return safeAwait(ClusterServiceUtils.addTemporaryStateListener(clusterService(viaNode), cs -> { |
| 2044 | + Optional.ofNullable(cs.nodes().getMasterNode()) |
| 2045 | + .ifPresent(masterNode -> masterNameListener.onResponse(masterNode.getName())); |
| 2046 | + return masterNameListener.isDone(); |
| 2047 | + }).andThen(masterNameListener::addListener)); |
2048 | 2048 | } catch (Exception e) { |
2049 | 2049 | logger.warn("Can't fetch cluster state", e); |
2050 | 2050 | throw new RuntimeException("Can't get master node " + e.getMessage(), e); |
|
0 commit comments