-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Zen2] Introduce ClusterBootstrapService #35488
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
Merged
DaveCTurner
merged 15 commits into
elastic:zen2
from
DaveCTurner:2018-11-13-simple-unsafe-test-bootstrapping
Nov 15, 2018
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
69c66fe
[Zen2] Introduce ClusterBootstrapService
DaveCTurner bb41ca9
Add tests for stopping
DaveCTurner 63631fc
Bogus assertion
DaveCTurner 6e13fda
Merge branch 'zen2' into 2018-11-13-simple-unsafe-test-bootstrapping
DaveCTurner d56d314
Allow _system to bootstrap a cluster
DaveCTurner 5c8f24d
Merge branch 'zen2' into 2018-11-13-simple-unsafe-test-bootstrapping
DaveCTurner 150f47b
Bootstrap on all nodes but only if autoManageMinMasterNodes
DaveCTurner 9f7e951
Wait indefinitely, do not retry
DaveCTurner 85daaf1
Merge branch 'zen2' into 2018-11-13-simple-unsafe-test-bootstrapping
DaveCTurner 12d90d2
Fix message
DaveCTurner b5bed1d
Inline once-called method
DaveCTurner 49e19ec
More inlining
DaveCTurner 843f62d
Merge branch 'zen2' into 2018-11-13-simple-unsafe-test-bootstrapping
DaveCTurner a6221d4
Import
DaveCTurner e480d00
Fix initial master node count - data nodes are not masters if there a…
DaveCTurner 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
176 changes: 176 additions & 0 deletions
176
server/src/main/java/org/elasticsearch/cluster/coordination/ClusterBootstrapService.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,176 @@ | ||
| /* | ||
| * 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.cluster.coordination; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.message.ParameterizedMessage; | ||
| import org.elasticsearch.ElasticsearchTimeoutException; | ||
| import org.elasticsearch.action.admin.cluster.bootstrap.BootstrapClusterAction; | ||
| import org.elasticsearch.action.admin.cluster.bootstrap.BootstrapClusterRequest; | ||
| import org.elasticsearch.action.admin.cluster.bootstrap.BootstrapClusterResponse; | ||
| import org.elasticsearch.action.admin.cluster.bootstrap.BootstrapConfiguration; | ||
| import org.elasticsearch.action.admin.cluster.bootstrap.GetDiscoveredNodesAction; | ||
| import org.elasticsearch.action.admin.cluster.bootstrap.GetDiscoveredNodesRequest; | ||
| import org.elasticsearch.action.admin.cluster.bootstrap.GetDiscoveredNodesResponse; | ||
| import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.settings.Setting; | ||
| import org.elasticsearch.common.settings.Setting.Property; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.unit.TimeValue; | ||
| import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
| import org.elasticsearch.threadpool.ThreadPool.Names; | ||
| import org.elasticsearch.transport.TransportException; | ||
| import org.elasticsearch.transport.TransportResponseHandler; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class ClusterBootstrapService { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(ClusterBootstrapService.class); | ||
|
|
||
| // The number of master-eligible nodes which, if discovered, can be used to bootstrap the cluster. This setting is unsafe in the event | ||
| // that more master nodes are started than expected. | ||
| public static final Setting<Integer> INITIAL_MASTER_NODE_COUNT_SETTING = | ||
| Setting.intSetting("cluster.unsafe_initial_master_node_count", 0, 0, Property.NodeScope); | ||
|
|
||
| private final int initialMasterNodeCount; | ||
| private final TransportService transportService; | ||
| private volatile boolean running; | ||
|
|
||
| public ClusterBootstrapService(Settings settings, TransportService transportService) { | ||
| initialMasterNodeCount = INITIAL_MASTER_NODE_COUNT_SETTING.get(settings); | ||
| this.transportService = transportService; | ||
| } | ||
|
|
||
| public void start() { | ||
| assert running == false; | ||
| running = true; | ||
|
|
||
| if (initialMasterNodeCount > 0 && transportService.getLocalNode().isMasterNode()) { | ||
| logger.debug("unsafely waiting for discovery of [{}] master-eligible nodes", initialMasterNodeCount); | ||
|
|
||
| final ThreadContext threadContext = transportService.getThreadPool().getThreadContext(); | ||
| try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { | ||
| threadContext.markAsSystemContext(); | ||
| awaitDiscovery(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void stop() { | ||
| assert running == true; | ||
| running = false; | ||
| } | ||
|
|
||
| private void awaitDiscovery() { | ||
| if (running == false) { | ||
| logger.debug("awaitDiscovery: not running"); | ||
| return; | ||
| } | ||
|
|
||
| final GetDiscoveredNodesRequest request = new GetDiscoveredNodesRequest(); | ||
| request.setWaitForNodes(initialMasterNodeCount); | ||
| logger.trace("sending {}", request); | ||
| transportService.sendRequest(transportService.getLocalNode(), GetDiscoveredNodesAction.NAME, request, | ||
| new TransportResponseHandler<GetDiscoveredNodesResponse>() { | ||
| @Override | ||
| public void handleResponse(GetDiscoveredNodesResponse response) { | ||
| assert response.getNodes().size() >= initialMasterNodeCount; | ||
| assert response.getNodes().stream().allMatch(DiscoveryNode::isMasterNode); | ||
|
|
||
| logger.debug("discovered {}, starting to bootstrap", response.getNodes()); | ||
| awaitBootstrap(response.getBootstrapConfiguration()); | ||
| } | ||
|
|
||
| @Override | ||
| public void handleException(TransportException exp) { | ||
| if (exp.getRootCause() instanceof ElasticsearchTimeoutException) { | ||
| logger.debug(new ParameterizedMessage("discovery attempt timed out, retrying, request={}", request), exp); | ||
| awaitDiscovery(); | ||
| } else { | ||
| // exceptions other than a timeout are fatal | ||
| logger.warn("discovery attempt failed, not retrying", exp); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String executor() { | ||
| return Names.SAME; | ||
| } | ||
|
|
||
| @Override | ||
| public GetDiscoveredNodesResponse read(StreamInput in) throws IOException { | ||
| return new GetDiscoveredNodesResponse(in); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void awaitBootstrap(final BootstrapConfiguration bootstrapConfiguration) { | ||
| if (running == false) { | ||
| logger.debug("awaitBootstrap: not running"); | ||
| return; | ||
| } | ||
|
|
||
| BootstrapClusterRequest request = new BootstrapClusterRequest(bootstrapConfiguration); | ||
| logger.trace("sending {}", request); | ||
| transportService.sendRequest(transportService.getLocalNode(), BootstrapClusterAction.NAME, request, | ||
| new TransportResponseHandler<BootstrapClusterResponse>() { | ||
| @Override | ||
| public void handleResponse(BootstrapClusterResponse response) { | ||
| logger.debug("bootstrapped successful: received {}", response); | ||
|
||
| } | ||
|
|
||
| @Override | ||
| public void handleException(TransportException exp) { | ||
| // log a warning since a failure here indicates a bad problem, such as: | ||
| // - bootstrap configuration resolution failed (e.g. discovered nodes no longer match those in the bootstrap config) | ||
| // - discovered nodes no longer form a quorum in the bootstrap config | ||
| logger.warn(new ParameterizedMessage("automatic cluster bootstrapping failed, retrying [{}]", | ||
| bootstrapConfiguration.getNodeDescriptions()), exp); | ||
|
|
||
| // There's not really much else we can do apart from retry and hope that the problem goes away. The retry is delayed | ||
| // since a tight loop here is unlikely to help. | ||
| transportService.getThreadPool().scheduleUnlessShuttingDown(TimeValue.timeValueSeconds(10), Names.SAME, new Runnable() { | ||
| @Override | ||
| public void run() { | ||
| awaitBootstrap(bootstrapConfiguration); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "retry bootstrapping with " + bootstrapConfiguration.getNodeDescriptions(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public String executor() { | ||
| return Names.SAME; | ||
| } | ||
|
|
||
| @Override | ||
| public BootstrapClusterResponse read(StreamInput in) throws IOException { | ||
| return new BootstrapClusterResponse(in); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
should we simply set the timeout to a very high value instead of adding this retry logic here?
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.
How high is high enough? I'd prefer to retry forever rather than have to remember that this might time out and stop in a future debugging session.
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.
We can also make the timeout optional, so that setting it to null makes it unbounded (i.e. does not schedule a timeout)
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.
Ok, introduced nullability to the timeout in 9f7e951.