use of io.atomix.protocols.raft.protocol.JoinResponse in project atomix by atomix.
the class LeaderRole method onJoin.
@Override
public CompletableFuture<JoinResponse> onJoin(final JoinRequest request) {
raft.checkThread();
logRequest(request);
// See https://groups.google.com/forum/#!topic/raft-dev/t4xj6dJTP6E
if (configuring() || initializing()) {
return CompletableFuture.completedFuture(logResponse(JoinResponse.builder().withStatus(RaftResponse.Status.ERROR).build()));
}
// If the member is already a known member of the cluster, complete the join successfully.
if (raft.getCluster().getMember(request.member().nodeId()) != null) {
return CompletableFuture.completedFuture(logResponse(JoinResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(raft.getCluster().getConfiguration().index()).withTerm(raft.getCluster().getConfiguration().term()).withTime(raft.getCluster().getConfiguration().time()).withMembers(raft.getCluster().getMembers()).build()));
}
RaftMember member = request.member();
// Add the joining member to the members list. If the joining member's type is ACTIVE, join the member in the
// PROMOTABLE state to allow it to get caught up without impacting the quorum size.
Collection<RaftMember> members = raft.getCluster().getMembers();
members.add(new DefaultRaftMember(member.nodeId(), member.getType(), Instant.now()));
CompletableFuture<JoinResponse> future = new CompletableFuture<>();
configure(members).whenComplete((index, error) -> {
if (error == null) {
future.complete(logResponse(JoinResponse.builder().withStatus(RaftResponse.Status.OK).withIndex(index).withTerm(raft.getCluster().getConfiguration().term()).withTime(raft.getCluster().getConfiguration().time()).withMembers(members).build()));
} else {
future.complete(logResponse(JoinResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.PROTOCOL_ERROR).build()));
}
});
return future;
}
Aggregations