use of org.apache.ratis.statemachine.StateMachine in project incubator-ratis by apache.
the class RaftServerImpl method submitClientRequestAsync.
@Override
public CompletableFuture<RaftClientReply> submitClientRequestAsync(RaftClientRequest request) throws IOException {
assertLifeCycleState(RUNNING);
LOG.debug("{}: receive client request({})", getId(), request);
if (request.is(RaftClientRequestProto.TypeCase.STALEREAD)) {
return staleReadAsync(request);
}
// first check the server's leader state
CompletableFuture<RaftClientReply> reply = checkLeaderState(request, null);
if (reply != null) {
return reply;
}
// let the state machine handle read-only request from client
final StateMachine stateMachine = getStateMachine();
if (request.is(RaftClientRequestProto.TypeCase.READ)) {
// See the RAFT paper section 8 (last part)
return processQueryFuture(stateMachine.query(request.getMessage()), request);
}
// query the retry cache
RetryCache.CacheQueryResult previousResult = retryCache.queryCache(request.getClientId(), request.getCallId());
if (previousResult.isRetry()) {
// future
return previousResult.getEntry().getReplyFuture();
}
final RetryCache.CacheEntry cacheEntry = previousResult.getEntry();
// TODO: this client request will not be added to pending requests until
// later which means that any failure in between will leave partial state in
// the state machine. We should call cancelTransaction() for failed requests
TransactionContext context = stateMachine.startTransaction(request);
if (context.getException() != null) {
RaftClientReply exceptionReply = new RaftClientReply(request, new StateMachineException(getId(), context.getException()), getCommitInfos());
cacheEntry.failWithReply(exceptionReply);
return CompletableFuture.completedFuture(exceptionReply);
}
return appendTransaction(request, context, cacheEntry);
}
use of org.apache.ratis.statemachine.StateMachine in project incubator-ratis by apache.
the class MiniRaftCluster method newRaftServer.
private RaftServerProxy newRaftServer(RaftPeerId id, RaftGroup group, boolean format) {
try {
final File dir = getStorageDir(id);
if (format) {
FileUtils.deleteFully(dir);
LOG.info("Formatted directory {}", dir);
}
final RaftProperties prop = new RaftProperties(properties);
RaftServerConfigKeys.setStorageDir(prop, dir);
final StateMachine stateMachine = getStateMachine4Test(properties);
return newRaftServer(id, stateMachine, group, prop);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.ratis.statemachine.StateMachine in project incubator-ratis by apache.
the class ServerState method installSnapshot.
void installSnapshot(InstallSnapshotRequestProto request) throws IOException {
// TODO: verify that we need to install the snapshot
StateMachine sm = server.getStateMachine();
// pause the SM to prepare for install snapshot
sm.pause();
snapshotManager.installSnapshot(sm, request);
log.syncWithSnapshot(request.getTermIndex().getIndex());
this.latestInstalledSnapshot = ServerProtoUtils.toTermIndex(request.getTermIndex());
}
use of org.apache.ratis.statemachine.StateMachine in project incubator-ratis by apache.
the class Server method run.
@Override
public void run() throws Exception {
RaftPeerId peerId = RaftPeerId.valueOf(id);
RaftProperties properties = new RaftProperties();
RaftPeer[] peers = getPeers();
final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
GrpcConfigKeys.Server.setPort(properties, port);
properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
RaftServerConfigKeys.setStorageDir(properties, storageDir);
StateMachine stateMachine = new ArithmeticStateMachine();
RaftGroup raftGroup = new RaftGroup(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), peers);
RaftServer raftServer = RaftServer.newBuilder().setServerId(RaftPeerId.valueOf(id)).setStateMachine(stateMachine).setProperties(properties).setGroup(raftGroup).build();
raftServer.start();
}
Aggregations