-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add GET /_cluster/master endpoint #40047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
DaveCTurner
wants to merge
2
commits into
elastic:master
from
DaveCTurner:2019-03-14-cluster-master-api
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
21 changes: 21 additions & 0 deletions
21
rest-api-spec/src/main/resources/rest-api-spec/api/cluster.master.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
rest-api-spec/src/main/resources/rest-api-spec/test/cluster.master/10_basic.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| - do: | ||
| cluster.master: {} | ||
|
|
||
| - is_true: master_node_id | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...er/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterMasterAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
skipcan be removed, and all the REST tests that get the master ID from the cluster state can migrate to using this API instead.