net.kuujo.copycat.raft.RaftState Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014 the original author or authors.
*
* Licensed 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 net.kuujo.copycat.raft;
import net.kuujo.copycat.cluster.MessageHandler;
import net.kuujo.copycat.raft.protocol.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture;
/**
* Abstract state context.
*
* @author Jordan Halterman
*/
abstract class RaftState implements RaftProtocol {
protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
protected final RaftContext context;
protected MessageHandler syncHandler;
protected MessageHandler pollHandler;
protected MessageHandler voteHandler;
protected MessageHandler appendHandler;
protected MessageHandler commitHandler;
protected MessageHandler queryHandler;
protected MessageHandler transitionHandler;
private volatile boolean open;
protected RaftState(RaftContext context) {
this.context = context;
}
/**
* Raft state types.
*/
public static enum Type {
/**
* Start state.
*/
START(StartState.class),
/**
* Passive state.
*/
PASSIVE(PassiveState.class),
/**
* Follower state.
*/
FOLLOWER(FollowerState.class),
/**
* Candidate state.
*/
CANDIDATE(CandidateState.class),
/**
* Leader state.
*/
LEADER(LeaderState.class);
private final Class extends RaftState> type;
private Type(Class extends RaftState> type) {
this.type = type;
}
/**
* Returns the state type class.
*
* @return The state type clas.
*/
public Class extends RaftState> type() {
return type;
}
}
/**
* Returns an exceptional future with the given exception.
*/
protected CompletableFuture exceptionalFuture(Throwable t) {
CompletableFuture future = new CompletableFuture<>();
future.completeExceptionally(t);
return future;
}
/**
* Returns the Copycat state represented by this state.
*
* @return The Copycat state represented by this state.
*/
public abstract Type type();
/**
* Logs a request.
*/
protected final R logRequest(R request) {
LOGGER.debug("{} - Received {}", context.getLocalMember(), request);
return request;
}
/**
* Logs a response.
*/
protected final R logResponse(R response) {
LOGGER.debug("{} - Sent {}", context.getLocalMember(), response);
return response;
}
@Override
public CompletableFuture sync(SyncRequest request) {
return exceptionalFuture(new IllegalStateException("Invalid Copycat state"));
}
@Override
public RaftProtocol syncHandler(MessageHandler handler) {
this.syncHandler = handler;
return this;
}
@Override
public CompletableFuture poll(PollRequest request) {
return exceptionalFuture(new IllegalStateException("Invalid Copycat state"));
}
@Override
public RaftProtocol pollHandler(MessageHandler handler) {
this.pollHandler = handler;
return this;
}
@Override
public RaftState voteHandler(MessageHandler handler) {
this.voteHandler = handler;
return this;
}
@Override
public CompletableFuture vote(VoteRequest request) {
return exceptionalFuture(new IllegalStateException("Invalid Copycat state"));
}
@Override
public RaftState appendHandler(MessageHandler handler) {
this.appendHandler = handler;
return this;
}
@Override
public CompletableFuture append(AppendRequest request) {
return exceptionalFuture(new IllegalStateException("Invalid Copycat state"));
}
@Override
public RaftProtocol queryHandler(MessageHandler handler) {
this.queryHandler = handler;
return this;
}
@Override
public CompletableFuture query(QueryRequest request) {
return exceptionalFuture(new IllegalStateException("Invalid Copycat state"));
}
@Override
public RaftState commitHandler(MessageHandler handler) {
this.commitHandler = handler;
return this;
}
@Override
public CompletableFuture commit(CommitRequest request) {
return exceptionalFuture(new IllegalStateException("Invalid Copycat state"));
}
/**
* Sets a transition registerHandler on the state.
*/
public RaftState transitionHandler(MessageHandler handler) {
this.transitionHandler = handler;
return this;
}
@Override
public CompletableFuture open() {
context.checkThread();
open = true;
return CompletableFuture.completedFuture(null);
}
@Override
public boolean isOpen() {
return open;
}
@Override
public CompletableFuture close() {
context.checkThread();
open = false;
return CompletableFuture.completedFuture(null);
}
@Override
public boolean isClosed() {
return !open;
}
@Override
public String toString() {
return String.format("%s[context=%s]", getClass().getSimpleName(), context);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy