-
Notifications
You must be signed in to change notification settings - Fork 24.2k
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
Zen2: Add leader-side join handling logic #33013
Merged
Merged
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ef976f8
Node joining
ywelsch 348fb4e
start testing
ywelsch 554263a
request serialozatono
ywelsch 00463fe
more testing
ywelsch 391989b
factor out into separate class
ywelsch 7055a70
Smaller stuff
ywelsch 36b4b39
own joincallback
ywelsch 6b59200
simplify logic in handleJoinRequestUnderLock
ywelsch 5fb85fb
minor stuff
ywelsch 544560a
licenses
ywelsch 85a272a
moar licenses
ywelsch 2757c07
Add assertion about source node to join request
ywelsch 1ce4fdc
enhance javadoc of randomSubsetOf
ywelsch 4676116
add toString()
ywelsch 7dcd656
pick more variable term and version
ywelsch bd242ed
simplify cases
ywelsch 6f58a8c
try moving code
ywelsch a4b63f5
More separation between JoinHelper and Coordinator
DaveCTurner a2e9fc3
Cleanup
DaveCTurner 5a0efb5
toString()
DaveCTurner aa9ce25
rename and clean-up
ywelsch 64e3cd4
remove handleJoinRequest method from Coordinator
ywelsch d45cc11
checkstyle T_T
ywelsch 49623cb
explicitly catch exception and handle it
ywelsch 06ab9e1
strengthen test
ywelsch 5b390a8
add TODO comment to coordinator
ywelsch 1c522ce
Move handleJoinRequest to Coordinator
ywelsch 66825c7
only use close method
ywelsch 6566efb
add assertions
ywelsch File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
196 changes: 196 additions & 0 deletions
196
server/src/main/java/org/elasticsearch/cluster/coordination/Coordinator.java
This file contains 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,196 @@ | ||
/* | ||
* 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.lucene.util.SetOnce; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.cluster.routing.allocation.AllocationService; | ||
import org.elasticsearch.cluster.service.MasterService; | ||
import org.elasticsearch.common.component.AbstractLifecycleComponent; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.transport.TransportService; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
public class Coordinator extends AbstractLifecycleComponent { | ||
|
||
private final TransportService transportService; | ||
private final JoinHelper joinHelper; | ||
private final Supplier<CoordinationState.PersistedState> persistedStateSupplier; | ||
final Object mutex = new Object(); | ||
final SetOnce<CoordinationState> coordinationState = new SetOnce<>(); // initialized on start-up (see doStart) | ||
|
||
private Mode mode; | ||
private Optional<DiscoveryNode> lastKnownLeader; | ||
private Optional<Join> lastJoin; | ||
|
||
public Coordinator(Settings settings, TransportService transportService, AllocationService allocationService, | ||
MasterService masterService, Supplier<CoordinationState.PersistedState> persistedStateSupplier) { | ||
super(settings); | ||
this.transportService = transportService; | ||
this.joinHelper = new JoinHelper(settings, allocationService, masterService, transportService, this::getCurrentTerm, | ||
this::handleJoin); | ||
this.persistedStateSupplier = persistedStateSupplier; | ||
this.lastKnownLeader = Optional.empty(); | ||
this.lastJoin = Optional.empty(); | ||
} | ||
|
||
private Optional<Join> ensureTermAtLeast(DiscoveryNode sourceNode, long targetTerm) { | ||
assert Thread.holdsLock(mutex) : "Coordinator mutex not held"; | ||
if (getCurrentTerm() < targetTerm) { | ||
return Optional.of(joinLeaderInTerm(new StartJoinRequest(sourceNode, targetTerm))); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private Join joinLeaderInTerm(StartJoinRequest startJoinRequest) { | ||
assert Thread.holdsLock(mutex) : "Coordinator mutex not held"; | ||
logger.debug("joinLeaderInTerm: from [{}] with term {}", startJoinRequest.getSourceNode(), startJoinRequest.getTerm()); | ||
Join join = coordinationState.get().handleStartJoin(startJoinRequest); | ||
lastJoin = Optional.of(join); | ||
if (mode != Mode.CANDIDATE) { | ||
becomeCandidate("joinLeaderInTerm"); | ||
} | ||
return join; | ||
} | ||
|
||
private boolean handleJoin(final Join join) { | ||
assert Thread.holdsLock(mutex) == false; | ||
|
||
synchronized (mutex) { | ||
logger.trace("handleJoin: as {}, handling {}", mode, join); | ||
|
||
final CoordinationState coordState = coordinationState.get(); | ||
final boolean prevElectionWon = coordState.electionWon(); | ||
|
||
// if someone thinks we should be master, let's add our vote and try to become one | ||
// note that the following line should never throw an exception | ||
ensureTermAtLeast(getLocalNode(), join.getTerm()).ifPresent(coordState::handleJoin); | ||
|
||
if (coordState.electionWon()) { | ||
// if we have already won the election then the actual join does not matter for election purposes, so swallow any exception | ||
try { | ||
coordState.handleJoin(join); | ||
} catch (CoordinationStateRejectedException e) { | ||
logger.trace("failed to add join, ignoring", e); | ||
} | ||
} else { | ||
coordState.handleJoin(join); // this might fail and bubble up the exception | ||
} | ||
|
||
if (prevElectionWon == false && coordState.electionWon()) { | ||
becomeLeader("handleJoin"); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void becomeCandidate(String method) { | ||
assert Thread.holdsLock(mutex) : "Legislator mutex not held"; | ||
logger.debug("{}: becoming CANDIDATE (was {}, lastKnownLeader was [{}])", method, mode, lastKnownLeader); | ||
|
||
if (mode != Mode.CANDIDATE) { | ||
mode = Mode.CANDIDATE; | ||
joinHelper.becomeCandidate(); | ||
} | ||
} | ||
|
||
void becomeLeader(String method) { | ||
assert Thread.holdsLock(mutex) : "Legislator mutex not held"; | ||
assert mode == Mode.CANDIDATE : "expected candidate but was " + mode; | ||
logger.debug("{}: becoming LEADER (was {}, lastKnownLeader was [{}])", method, mode, lastKnownLeader); | ||
|
||
mode = Mode.LEADER; | ||
lastKnownLeader = Optional.of(getLocalNode()); | ||
} | ||
|
||
void becomeFollower(String method, DiscoveryNode leaderNode) { | ||
assert Thread.holdsLock(mutex) : "Legislator mutex not held"; | ||
logger.debug("{}: becoming FOLLOWER of [{}] (was {}, lastKnownLeader was [{}])", method, leaderNode, mode, lastKnownLeader); | ||
|
||
if (mode != Mode.FOLLOWER) { | ||
mode = Mode.FOLLOWER; | ||
joinHelper.becomeFollower(leaderNode); | ||
} | ||
|
||
lastKnownLeader = Optional.of(leaderNode); | ||
} | ||
|
||
// package-visible for testing | ||
long getCurrentTerm() { | ||
synchronized (mutex) { | ||
return coordinationState.get().getCurrentTerm(); | ||
} | ||
} | ||
|
||
// package-visible for testing | ||
Mode getMode() { | ||
synchronized (mutex) { | ||
return mode; | ||
} | ||
} | ||
|
||
// package-visible for testing | ||
DiscoveryNode getLocalNode() { | ||
return transportService.getLocalNode(); | ||
} | ||
|
||
@Override | ||
protected void doStart() { | ||
CoordinationState.PersistedState persistedState = persistedStateSupplier.get(); | ||
coordinationState.set(new CoordinationState(settings, getLocalNode(), persistedState)); | ||
} | ||
|
||
public void startInitialJoin() { | ||
synchronized (mutex) { | ||
becomeCandidate("startInitialJoin"); | ||
} | ||
} | ||
|
||
@Override | ||
protected void doStop() { | ||
|
||
} | ||
|
||
@Override | ||
protected void doClose() { | ||
|
||
} | ||
|
||
public void invariant() { | ||
synchronized (mutex) { | ||
if (mode == Mode.LEADER) { | ||
assert coordinationState.get().electionWon(); | ||
assert lastKnownLeader.isPresent() && lastKnownLeader.get().equals(getLocalNode()); | ||
} else if (mode == Mode.FOLLOWER) { | ||
assert coordinationState.get().electionWon() == false : getLocalNode() + " is FOLLOWER so electionWon() should be false"; | ||
assert lastKnownLeader.isPresent() && (lastKnownLeader.get().equals(getLocalNode()) == false); | ||
} else { | ||
assert mode == Mode.CANDIDATE; | ||
} | ||
} | ||
} | ||
|
||
public enum Mode { | ||
CANDIDATE, LEADER, FOLLOWER | ||
} | ||
} |
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.
We can restore those assertions about the state of the join helper here - i.e. no accumulated joins when leader or follower.