Skip to content

Commit

Permalink
Rename CopycatState to RaftState.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Feb 9, 2015
1 parent 0d721a4 commit 9ea96c1
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 31 deletions.
Expand Up @@ -36,7 +36,7 @@ abstract class AbstractState implements RaftProtocol {
protected MessageHandler<AppendRequest, AppendResponse> appendHandler;
protected MessageHandler<CommitRequest, CommitResponse> commitHandler;
protected MessageHandler<QueryRequest, QueryResponse> queryHandler;
protected MessageHandler<CopycatState, CopycatState> transitionHandler;
protected MessageHandler<RaftState, RaftState> transitionHandler;
private volatile boolean open;

protected AbstractState(CopycatStateContext context) {
Expand All @@ -57,7 +57,7 @@ protected <T> CompletableFuture<T> exceptionalFuture(Throwable t) {
*
* @return The Copycat state represented by this state.
*/
public abstract CopycatState state();
public abstract RaftState state();

/**
* Logs a request.
Expand Down Expand Up @@ -144,7 +144,7 @@ public CompletableFuture<CommitResponse> commit(CommitRequest request) {
/**
* Sets a transition registerHandler on the state.
*/
public AbstractState transitionHandler(MessageHandler<CopycatState, CopycatState> handler) {
public AbstractState transitionHandler(MessageHandler<RaftState, RaftState> handler) {
this.transitionHandler = handler;
return this;
}
Expand Down
Expand Up @@ -36,7 +36,7 @@ protected ActiveState(CopycatStateContext context) {
/**
* Transitions to a new state.
*/
protected CompletableFuture<CopycatState> transition(CopycatState state) {
protected CompletableFuture<RaftState> transition(RaftState state) {
if (transitionHandler != null) {
return transitionHandler.apply(state);
}
Expand All @@ -50,7 +50,7 @@ public CompletableFuture<AppendResponse> append(final AppendRequest request) {
// If a transition is required then transition back to the follower state.
// If the node is already a follower then the transition will be ignored.
if (transition) {
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
transition = false;
}
return future;
Expand Down
Expand Up @@ -42,8 +42,8 @@ class CandidateState extends ActiveState {
}

@Override
public CopycatState state() {
return CopycatState.CANDIDATE;
public RaftState state() {
return RaftState.CANDIDATE;
}

@Override
Expand Down Expand Up @@ -100,7 +100,7 @@ private void sendVoteRequests() {
final Quorum quorum = new Quorum((int) Math.ceil(context.getActiveMembers().size() / 2.0), (elected) -> {
complete.set(true);
if (elected) {
transition(CopycatState.LEADER);
transition(RaftState.LEADER);
}
});

Expand Down Expand Up @@ -132,7 +132,7 @@ private void sendVoteRequests() {
LOGGER.debug("{} - Received greater term from {}", context.getLocalMember(), member);
context.setTerm(response.term());
complete.set(true);
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
} else if (!response.voted()) {
LOGGER.info("{} - Received rejected vote from {}", context.getLocalMember(), member);
quorum.fail();
Expand All @@ -156,7 +156,7 @@ public CompletableFuture<AppendResponse> append(AppendRequest request) {
// assign that term and leader to the current context and step down as a candidate.
if (request.term() >= context.getTerm()) {
context.setTerm(request.term());
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
}
return super.append(request);
}
Expand All @@ -169,7 +169,7 @@ public CompletableFuture<VoteResponse> vote(VoteRequest request) {
// assign that term and leader to the current context and step down as a candidate.
if (request.term() > context.getTerm()) {
context.setTerm(request.term());
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
return super.vote(request);
}

Expand Down
Expand Up @@ -443,7 +443,7 @@ public long getHeartbeatInterval() {
*
* @return The current Copycat state.
*/
public CopycatState state() {
public RaftState state() {
return state.state();
}

Expand Down Expand Up @@ -580,7 +580,7 @@ void checkThread() {
/**
* Transition registerHandler.
*/
CompletableFuture<CopycatState> transition(CopycatState state) {
CompletableFuture<RaftState> transition(RaftState state) {
checkThread();

if (this.state != null && state == this.state.state()) {
Expand Down Expand Up @@ -684,7 +684,7 @@ public synchronized CompletableFuture<Void> open() {
try {
open = true;
log.open();
transition(activeMembers.contains(localMember) ? CopycatState.FOLLOWER : CopycatState.PASSIVE);
transition(activeMembers.contains(localMember) ? RaftState.FOLLOWER : RaftState.PASSIVE);
} catch (Exception e) {
openFuture.completeExceptionally(e);
openFuture = null;
Expand All @@ -709,7 +709,7 @@ public synchronized CompletableFuture<Void> close() {

CompletableFuture<Void> future = new CompletableFuture<>();
executor.execute(() -> {
transition(CopycatState.START).whenComplete((result, error) -> {
transition(RaftState.START).whenComplete((result, error) -> {
if (error == null) {
try {
log.close();
Expand Down
Expand Up @@ -65,7 +65,7 @@ public CoordinatedResourceConfig config() {
}

@Override
public CopycatState state() {
public RaftState state() {
return context.state();
}

Expand Down
Expand Up @@ -38,8 +38,8 @@ class FollowerState extends ActiveState {
}

@Override
public CopycatState state() {
return CopycatState.FOLLOWER;
public RaftState state() {
return RaftState.FOLLOWER;
}

@Override
Expand Down Expand Up @@ -101,7 +101,7 @@ private void sendPollRequests() {
// If a majority of the cluster indicated they would vote for us then transition to candidate.
complete.set(true);
if (elected) {
transition(CopycatState.CANDIDATE);
transition(RaftState.CANDIDATE);
} else {
resetHeartbeatTimeout();
}
Expand Down
Expand Up @@ -40,8 +40,8 @@ class LeaderState extends ActiveState {
}

@Override
public CopycatState state() {
return CopycatState.LEADER;
public RaftState state() {
return RaftState.LEADER;
}

@Override
Expand Down Expand Up @@ -101,7 +101,7 @@ private void heartbeatMembers() {
public CompletableFuture<VoteResponse> vote(final VoteRequest request) {
if (request.term() > context.getTerm()) {
LOGGER.debug("{} - Received greater term", context.getLocalMember());
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
return super.vote(request);
} else {
return CompletableFuture.completedFuture(logResponse(VoteResponse.builder()
Expand All @@ -125,7 +125,7 @@ public CompletableFuture<AppendResponse> append(final AppendRequest request) {
.withLogIndex(context.log().lastIndex())
.build()));
} else {
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
return super.append(request);
}
}
Expand Down Expand Up @@ -507,7 +507,7 @@ private void commit(Long prevIndex, ByteBuffer prevEntry, List<ByteBuffer> entri
commit();
}
} else if (response.term() > context.getTerm()) {
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
} else {
resetMatchIndex(response);
resetNextIndex();
Expand All @@ -519,7 +519,7 @@ private void commit(Long prevIndex, ByteBuffer prevEntry, List<ByteBuffer> entri
}
} else if (response.term() > context.getTerm()) {
LOGGER.debug("{} - Received higher term from {}", context.getLocalMember(), member);
transition(CopycatState.FOLLOWER);
transition(RaftState.FOLLOWER);
} else {
LOGGER.warn("{} - {}", context.getLocalMember(), response.error() != null ? response.error().getMessage() : "");
}
Expand Down
Expand Up @@ -39,8 +39,8 @@ public PassiveState(CopycatStateContext context) {
}

@Override
public CopycatState state() {
return CopycatState.PASSIVE;
public RaftState state() {
return RaftState.PASSIVE;
}

@Override
Expand Down
Expand Up @@ -19,7 +19,7 @@
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public enum CopycatState {
public enum RaftState {

/**
* Start state.
Expand Down
Expand Up @@ -54,7 +54,7 @@ public interface ResourceContext extends Managed<ResourceContext> {
*
* @return The current Copycat state.
*/
CopycatState state();
RaftState state();

/**
* Returns the Copycat cluster.
Expand Down
Expand Up @@ -26,8 +26,8 @@ class StartState extends AbstractState {
}

@Override
public CopycatState state() {
return CopycatState.START;
public RaftState state() {
return RaftState.START;
}

}

0 comments on commit 9ea96c1

Please sign in to comment.