Skip to content
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 29 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ef976f8
Node joining
ywelsch Aug 9, 2018
348fb4e
start testing
ywelsch Aug 9, 2018
554263a
request serialozatono
ywelsch Aug 9, 2018
00463fe
more testing
ywelsch Aug 9, 2018
391989b
factor out into separate class
ywelsch Aug 15, 2018
7055a70
Smaller stuff
ywelsch Aug 18, 2018
36b4b39
own joincallback
ywelsch Aug 19, 2018
6b59200
simplify logic in handleJoinRequestUnderLock
ywelsch Aug 19, 2018
5fb85fb
minor stuff
ywelsch Aug 21, 2018
544560a
licenses
ywelsch Aug 21, 2018
85a272a
moar licenses
ywelsch Aug 21, 2018
2757c07
Add assertion about source node to join request
ywelsch Aug 22, 2018
1ce4fdc
enhance javadoc of randomSubsetOf
ywelsch Aug 22, 2018
4676116
add toString()
ywelsch Aug 22, 2018
7dcd656
pick more variable term and version
ywelsch Aug 22, 2018
bd242ed
simplify cases
ywelsch Aug 22, 2018
6f58a8c
try moving code
ywelsch Aug 22, 2018
a4b63f5
More separation between JoinHelper and Coordinator
DaveCTurner Aug 22, 2018
a2e9fc3
Cleanup
DaveCTurner Aug 23, 2018
5a0efb5
toString()
DaveCTurner Aug 23, 2018
aa9ce25
rename and clean-up
ywelsch Aug 22, 2018
64e3cd4
remove handleJoinRequest method from Coordinator
ywelsch Aug 22, 2018
d45cc11
checkstyle T_T
ywelsch Aug 23, 2018
49623cb
explicitly catch exception and handle it
ywelsch Aug 23, 2018
06ab9e1
strengthen test
ywelsch Aug 23, 2018
5b390a8
add TODO comment to coordinator
ywelsch Aug 23, 2018
1c522ce
Move handleJoinRequest to Coordinator
ywelsch Aug 23, 2018
66825c7
only use close method
ywelsch Aug 23, 2018
6566efb
add assertions
ywelsch Aug 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -227,6 +227,7 @@ public boolean handleJoin(Join join) {
boolean added = joinVotes.addVote(join.getSourceNode());
boolean prevElectionWon = electionWon;
electionWon = isElectionQuorum(joinVotes);
assert !prevElectionWon || electionWon; // we cannot go from won to not won
logger.debug("handleJoin: added join {} from [{}] for election, electionWon={} lastAcceptedTerm={} lastAcceptedVersion={}", join,
join.getSourceNode(), electionWon, lastAcceptedTerm, getLastAcceptedVersion());

Expand Down
@@ -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) {
Copy link
Contributor

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.

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
}
}