Skip to content

Commit

Permalink
Run TransportClusterStateAction on local node
Browse files Browse the repository at this point in the history
This action solely needs the cluster state, it can run on any node.
  • Loading branch information
nielsbauman committed Jan 20, 2025
1 parent 1ba5d25 commit 303acb5
Show file tree
Hide file tree
Showing 38 changed files with 123 additions and 275 deletions.
8 changes: 3 additions & 5 deletions docs/reference/cluster/state.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ representation of this internal state for debugging or diagnostic purposes. You
may need to consult the {es} source code to determine the precise meaning of
the response.

By default the cluster state API will route requests to the elected master node
since this node is the authoritative source of cluster states. You can also
retrieve the cluster state held on the node handling the API request by adding
the query parameter `?local=true`.
The cluster state API will retrieve the cluster state held on the node handling
the API request.

{es} may need to expend significant effort to compute a response to this API in
larger clusters, and the response may comprise a very large quantity of data.
Expand Down Expand Up @@ -127,7 +125,7 @@ include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=flat-settings]
(Optional, Boolean) If `true`, unavailable indices (missing or closed) will
be ignored.

include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=local]
include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=local-deprecated-9.0.0]

include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,7 @@ public void testOneNodeShouldRunUsingPrivateIp() {

final String node1 = internalCluster().startNode(settings);
registerAzureNode(node1);
assertNotNull(
client().admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueSeconds(1))
.get()
.getState()
.nodes()
.getMasterNodeId()
);
assertNotNull(client().admin().cluster().prepareState(TimeValue.timeValueSeconds(1)).get().getState().nodes().getMasterNodeId());

// We expect having 1 node as part of the cluster, let's test that
assertNumberOfNodes(1);
Expand All @@ -50,16 +41,7 @@ public void testOneNodeShouldRunUsingPublicIp() {

final String node1 = internalCluster().startNode(settings);
registerAzureNode(node1);
assertNotNull(
client().admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueSeconds(1))
.get()
.getState()
.nodes()
.getMasterNodeId()
);
assertNotNull(client().admin().cluster().prepareState(TimeValue.timeValueSeconds(1)).get().getState().nodes().getMasterNodeId());

// We expect having 1 node as part of the cluster, let's test that
assertNumberOfNodes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,12 @@ public void testTwoNodesShouldRunUsingPrivateOrPublicIp() {
logger.info("--> start first node");
final String node1 = internalCluster().startNode(settings);
registerAzureNode(node1);
assertNotNull(
client().admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueSeconds(1))
.get()
.getState()
.nodes()
.getMasterNodeId()
);
assertNotNull(client().admin().cluster().prepareState(TimeValue.timeValueSeconds(1)).get().getState().nodes().getMasterNodeId());

logger.info("--> start another node");
final String node2 = internalCluster().startNode(settings);
registerAzureNode(node2);
assertNotNull(
client().admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueSeconds(1))
.get()
.getState()
.nodes()
.getMasterNodeId()
);
assertNotNull(client().admin().cluster().prepareState(TimeValue.timeValueSeconds(1)).get().getState().nodes().getMasterNodeId());

// We expect having 2 nodes as part of the cluster, let's test that
assertNumberOfNodes(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public void testJoin() {

ClusterStateResponse clusterStateResponse = client(masterNode).admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueSeconds(1))
.prepareState(TimeValue.timeValueSeconds(1))
.clear()
.setNodes(true)
.get();
Expand All @@ -80,11 +79,9 @@ public void testJoin() {
registerGceNode(secondNode);
clusterStateResponse = client(secondNode).admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueSeconds(1))
.prepareState(TimeValue.timeValueSeconds(1))
.clear()
.setNodes(true)
.setLocal(true)
.get();
assertNotNull(clusterStateResponse.getState().nodes().getMasterNodeId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@
},
"params":{
"local":{
"deprecated":true,
"type":"boolean",
"description":"Return local information, do not retrieve the state from master node (default: false)"
},
"master_timeout":{
"type":"time",
"description":"Specify timeout for connection to master"
"description":"Timeout for waiting for new cluster state in case it is blocked"
},
"flat_settings":{
"type":"boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {

public void testNonLocalRequestAlwaysFindsMaster() throws Exception {
runRepeatedlyWhileChangingMaster(() -> {
final ClusterStateRequestBuilder clusterStateRequestBuilder = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
final ClusterStateRequestBuilder clusterStateRequestBuilder = clusterAdmin().prepareState(TimeValue.timeValueMillis(100))
.clear()
.setNodes(true)
.setBlocks(true)
.setMasterNodeTimeout(TimeValue.timeValueMillis(100));
.setBlocks(true);
final ClusterStateResponse clusterStateResponse;
try {
clusterStateResponse = clusterStateRequestBuilder.get();
Expand All @@ -70,12 +69,10 @@ public void testLocalRequestAlwaysSucceeds() throws Exception {
final String node = randomFrom(internalCluster().getNodeNames());
final DiscoveryNodes discoveryNodes = client(node).admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.prepareState(TimeValue.timeValueMillis(100))
.clear()
.setLocal(true)
.setNodes(true)
.setBlocks(true)
.setMasterNodeTimeout(TimeValue.timeValueMillis(100))
.get()
.getState()
.nodes();
Expand All @@ -99,12 +96,11 @@ public void testNonLocalRequestAlwaysFindsMasterAndWaitsForMetadata() throws Exc
final long waitForMetadataVersion = randomLongBetween(Math.max(1, metadataVersion - 3), metadataVersion + 5);
final ClusterStateRequestBuilder clusterStateRequestBuilder = client(node).admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.prepareState(TimeValue.timeValueMillis(100))
.clear()
.setNodes(true)
.setMetadata(true)
.setBlocks(true)
.setMasterNodeTimeout(TimeValue.timeValueMillis(100))
.setWaitForTimeOut(TimeValue.timeValueMillis(100))
.setWaitForMetadataVersion(waitForMetadataVersion);
final ClusterStateResponse clusterStateResponse;
Expand Down Expand Up @@ -132,13 +128,11 @@ public void testLocalRequestWaitsForMetadata() throws Exception {
final long waitForMetadataVersion = randomLongBetween(Math.max(1, metadataVersion - 3), metadataVersion + 5);
final ClusterStateResponse clusterStateResponse = client(node).admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.prepareState(TimeValue.timeValueMillis(100))
.clear()
.setLocal(true)
.setMetadata(true)
.setBlocks(true)
.setWaitForMetadataVersion(waitForMetadataVersion)
.setMasterNodeTimeout(TimeValue.timeValueMillis(100))
.setWaitForTimeOut(TimeValue.timeValueMillis(100))
.get();
if (clusterStateResponse.isWaitForTimedOut() == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void testTwoNodesNoMasterBlock() throws Exception {
String node1Name = internalCluster().startNode(settings);

logger.info("--> should be blocked, no master...");
ClusterState state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
ClusterState state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(true));
assertThat(state.nodes().getSize(), equalTo(1)); // verify that we still see the local node in the cluster state

Expand All @@ -81,9 +81,9 @@ public void testTwoNodesNoMasterBlock() throws Exception {
.get();
assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));

state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(false));
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(false));

state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
Expand Down Expand Up @@ -124,11 +124,11 @@ public void testTwoNodesNoMasterBlock() throws Exception {
internalCluster().stopNode(masterNode);

assertBusy(() -> {
ClusterState clusterState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
ClusterState clusterState = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertTrue(clusterState.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
});

state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(true));
// verify that both nodes are still in the cluster state but there is no master
assertThat(state.nodes().getSize(), equalTo(2));
Expand All @@ -144,9 +144,9 @@ public void testTwoNodesNoMasterBlock() throws Exception {
.get();
assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));

state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(false));
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(false));

state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
Expand Down Expand Up @@ -177,7 +177,7 @@ public void testTwoNodesNoMasterBlock() throws Exception {
internalCluster().stopNode(otherNode);

assertBusy(() -> {
ClusterState state1 = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
ClusterState state1 = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state1.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(true));
});

Expand All @@ -192,9 +192,9 @@ public void testTwoNodesNoMasterBlock() throws Exception {
.get();
assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));

state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(false));
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(false));

state = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
Expand Down Expand Up @@ -222,7 +222,7 @@ public void testThreeNodesNoMasterBlock() throws Exception {

assertBusy(() -> {
for (Client client : clients()) {
ClusterState state1 = client.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
ClusterState state1 = client.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(state1.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(true));
}
});
Expand Down Expand Up @@ -272,7 +272,7 @@ public void testThreeNodesNoMasterBlock() throws Exception {
logger.info("--> verify that there is no master anymore on remaining node");
// spin here to wait till the state is set
assertBusy(() -> {
ClusterState st = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
ClusterState st = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertThat(st.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID), equalTo(true));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ public void testNoMasterActions() throws Exception {
final Client clientToMasterlessNode = client();

assertBusy(() -> {
ClusterState state = clientToMasterlessNode.admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setLocal(true)
.get()
.getState();
ClusterState state = clientToMasterlessNode.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
});

Expand Down Expand Up @@ -249,12 +244,7 @@ public void testNoMasterActionsWriteMasterBlock() throws Exception {
final Client clientToMasterlessNode = client();

assertBusy(() -> {
ClusterState state = clientToMasterlessNode.admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setLocal(true)
.get()
.getState();
ClusterState state = clientToMasterlessNode.admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
});

Expand Down Expand Up @@ -348,7 +338,7 @@ public void testNoMasterActionsMetadataWriteMasterBlock() throws Exception {

assertBusy(() -> {
for (String node : nodesWithShards) {
ClusterState state = client(node).admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
ClusterState state = client(node).admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).get().getState();
assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,7 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
logger.info("--> start data node / non master node");
internalCluster().startNode(Settings.builder().put(dataOnlyNode()).put("discovery.initial_state_timeout", "1s"));
try {
assertThat(
clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueMillis(100))
.get()
.getState()
.nodes()
.getMasterNodeId(),
nullValue()
);
assertThat(clusterAdmin().prepareState(TimeValue.timeValueMillis(100)).get().getState().nodes().getMasterNodeId(), nullValue());
fail("should not be able to find master");
} catch (MasterNotDiscoveredException e) {
// all is well, no master elected
Expand Down Expand Up @@ -82,15 +74,7 @@ public void testSimpleOnlyMasterNodeElection() throws IOException {
internalCluster().stopCurrentMasterNode();

try {
assertThat(
clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueMillis(100))
.get()
.getState()
.nodes()
.getMasterNodeId(),
nullValue()
);
assertThat(clusterAdmin().prepareState(TimeValue.timeValueMillis(100)).get().getState().nodes().getMasterNodeId(), nullValue());
fail("should not be able to find master");
} catch (MasterNotDiscoveredException e) {
// all is well, no master elected
Expand Down Expand Up @@ -131,15 +115,7 @@ public void testElectOnlyBetweenMasterNodes() throws Exception {
logger.info("--> start data node / non master node");
internalCluster().startNode(Settings.builder().put(dataOnlyNode()).put("discovery.initial_state_timeout", "1s"));
try {
assertThat(
clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
.setMasterNodeTimeout(TimeValue.timeValueMillis(100))
.get()
.getState()
.nodes()
.getMasterNodeId(),
nullValue()
);
assertThat(clusterAdmin().prepareState(TimeValue.timeValueMillis(100)).get().getState().nodes().getMasterNodeId(), nullValue());
fail("should not be able to find master");
} catch (MasterNotDiscoveredException e) {
// all is well, no master elected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {

private static void assertClusterUuid(boolean expectCommitted, String expectedValue) {
for (String nodeName : internalCluster().getNodeNames()) {
final Metadata metadata = client(nodeName).admin()
.cluster()
.prepareState(TEST_REQUEST_TIMEOUT)
.setLocal(true)
.get()
.getState()
.metadata();
final Metadata metadata = client(nodeName).admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).get().getState().metadata();
assertEquals(expectCommitted, metadata.clusterUUIDCommitted());
assertEquals(expectedValue, metadata.clusterUUID());

Expand Down
Loading

0 comments on commit 303acb5

Please sign in to comment.