Search in sources :

Example 1 with StateMachine

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);
}
Also used : StateMachine(org.apache.ratis.statemachine.StateMachine) TransactionContext(org.apache.ratis.statemachine.TransactionContext)

Example 2 with StateMachine

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);
    }
}
Also used : StateMachine(org.apache.ratis.statemachine.StateMachine) BaseStateMachine(org.apache.ratis.statemachine.impl.BaseStateMachine) RaftProperties(org.apache.ratis.conf.RaftProperties) IOException(java.io.IOException) File(java.io.File)

Example 3 with StateMachine

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());
}
Also used : StateMachine(org.apache.ratis.statemachine.StateMachine)

Example 4 with StateMachine

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();
}
Also used : StateMachine(org.apache.ratis.statemachine.StateMachine) ArithmeticStateMachine(org.apache.ratis.examples.arithmetic.ArithmeticStateMachine) RaftServer(org.apache.ratis.server.RaftServer) RaftProperties(org.apache.ratis.conf.RaftProperties) ArithmeticStateMachine(org.apache.ratis.examples.arithmetic.ArithmeticStateMachine) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeer(org.apache.ratis.protocol.RaftPeer)

Aggregations

StateMachine (org.apache.ratis.statemachine.StateMachine)4 RaftProperties (org.apache.ratis.conf.RaftProperties)2 File (java.io.File)1 IOException (java.io.IOException)1 ArithmeticStateMachine (org.apache.ratis.examples.arithmetic.ArithmeticStateMachine)1 RaftGroup (org.apache.ratis.protocol.RaftGroup)1 RaftPeer (org.apache.ratis.protocol.RaftPeer)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 RaftServer (org.apache.ratis.server.RaftServer)1 TransactionContext (org.apache.ratis.statemachine.TransactionContext)1 BaseStateMachine (org.apache.ratis.statemachine.impl.BaseStateMachine)1