Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/cluster.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ include::cluster/health.asciidoc[]

include::cluster/state.asciidoc[]

include::cluster/master.asciidoc[]

include::cluster/stats.asciidoc[]

include::cluster/pending.asciidoc[]
Expand Down
16 changes: 16 additions & 0 deletions docs/reference/cluster/master.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[[cluster-master]]
== Cluster master

The cluster master API returns the identity of the currently elected master
node.

[source,js]
--------------------------------------------------
GET /_cluster/master
--------------------------------------------------
// CONSOLE

By default the coordinating node asks the master for its own identity to verify
that it is still the master. The timeout for this can be set with the
`?master_timeout` <<time-units,time value>> query parameter, which defaults to
`30s`. This check can be skipped by setting the `?local` query parameter.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"cluster.master": {
"documentation": "http://www.elastic.co/guide/en/elasticsearch/reference/master/cluster-master.html",
"methods": ["GET"],
"url": {
"path": "/_cluster/master",
"paths": ["/_cluster/master"],
"params": {
"local": {
"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"
}
}
},
"body": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"get cluster master":
- skip:
version: " - 7.99.99"
reason: Get cluster master API was introduced in 8.0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After backporting, as a follow up, this skip can be removed, and all the REST tests that get the master ID from the cluster state can migrate to using this API instead.


- do:
cluster.master: {}

- is_true: master_node_id
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
import org.elasticsearch.rest.action.admin.cluster.RestClusterAllocationExplainAction;
import org.elasticsearch.rest.action.admin.cluster.RestClusterGetSettingsAction;
import org.elasticsearch.rest.action.admin.cluster.RestClusterHealthAction;
import org.elasticsearch.rest.action.admin.cluster.RestClusterMasterAction;
import org.elasticsearch.rest.action.admin.cluster.RestClusterRerouteAction;
import org.elasticsearch.rest.action.admin.cluster.RestClusterSearchShardsAction;
import org.elasticsearch.rest.action.admin.cluster.RestClusterStateAction;
Expand Down Expand Up @@ -561,6 +562,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
registerHandler.accept(new RestClusterAllocationExplainAction(settings, restController));
registerHandler.accept(new RestClusterStatsAction(settings, restController));
registerHandler.accept(new RestClusterStateAction(settings, restController, settingsFilter));
registerHandler.accept(new RestClusterMasterAction(settings, restController));
registerHandler.accept(new RestClusterHealthAction(settings, restController));
registerHandler.accept(new RestClusterUpdateSettingsAction(settings, restController));
registerHandler.accept(new RestClusterGetSettingsAction(settings, restController, clusterSettings, settingsFilter));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.rest.action.admin.cluster;

import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.discovery.MasterNotDiscoveredException;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestBuilderListener;

import java.io.IOException;

public class RestClusterMasterAction extends BaseRestHandler {

public RestClusterMasterAction(Settings settings, RestController controller) {
super(settings);
controller.registerHandler(RestRequest.Method.GET, "/_cluster/master", this);
}

@Override
public String getName() {
return "cluster_master_action";
}

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
final ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest();
clusterStateRequest.clear();
clusterStateRequest.nodes(true);
clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
return channel -> client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {
@Override
public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception {
final DiscoveryNode masterNode = response.getState().nodes().getMasterNode();
if (masterNode == null) {
throw new MasterNotDiscoveredException();
}
builder.startObject();
builder.field("master_node_id", masterNode.getId());
builder.endObject();
return new BytesRestResponse(RestStatus.OK, builder);
}
});
}
}