Search in sources :

Example 86 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class CliServiceImpl method snapshot.

@Override
public Status snapshot(final String groupId, final PeerId peer) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(peer, "Null peer");
    if (!this.cliClientService.connect(peer.getEndpoint())) {
        return new Status(-1, "Fail to init channel to %s", peer);
    }
    final SnapshotRequest.Builder rb = // 
    SnapshotRequest.newBuilder().setGroupId(// 
    groupId).setPeerId(peer.toString());
    try {
        final Message result = this.cliClientService.snapshot(peer.getEndpoint(), rb.build(), null).get();
        return statusFromResponse(result);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) SnapshotRequest(com.alipay.sofa.jraft.rpc.CliRequests.SnapshotRequest) JRaftException(com.alipay.sofa.jraft.error.JRaftException)

Example 87 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class CliServiceImpl method resetLearners.

@Override
public Status resetLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
    checkLearnersOpParams(groupId, conf, learners);
    final PeerId leaderId = new PeerId();
    final Status st = getLeader(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    if (!this.cliClientService.connect(leaderId.getEndpoint())) {
        return new Status(-1, "Fail to init channel to leader %s", leaderId);
    }
    final ResetLearnersRequest.Builder rb = // 
    ResetLearnersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(leaderId.toString());
    for (final PeerId peer : learners) {
        rb.addLearners(peer.toString());
    }
    try {
        final Message result = this.cliClientService.resetLearners(leaderId.getEndpoint(), rb.build(), null).get();
        return processLearnersOpResponse(groupId, result, "resetting learners: %s", learners);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) ResetLearnersRequest(com.alipay.sofa.jraft.rpc.CliRequests.ResetLearnersRequest) JRaftException(com.alipay.sofa.jraft.error.JRaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 88 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class CounterServiceImpl method get.

@Override
public void get(final boolean readOnlySafe, final CounterClosure closure) {
    if (!readOnlySafe) {
        closure.success(getValue());
        closure.run(Status.OK());
        return;
    }
    this.counterServer.getNode().readIndex(BytesUtil.EMPTY_BYTES, new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            if (status.isOk()) {
                closure.success(getValue());
                closure.run(Status.OK());
                return;
            }
            CounterServiceImpl.this.readIndexExecutor.execute(() -> {
                if (isLeader()) {
                    LOG.debug("Fail to get value with 'ReadIndex': {}, try to applying to the state machine.", status);
                    applyOperation(CounterOperation.createGet(), closure);
                } else {
                    handlerNotLeaderError(closure);
                }
            });
        }
    });
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure)

Example 89 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class CounterStateMachine method onSnapshotSave.

@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
    final long currVal = this.value.get();
    Utils.runInThread(() -> {
        final CounterSnapshotFile snapshot = new CounterSnapshotFile(writer.getPath() + File.separator + "data");
        if (snapshot.save(currVal)) {
            if (writer.addFile("data")) {
                done.run(Status.OK());
            } else {
                done.run(new Status(RaftError.EIO, "Fail to add file to writer"));
            }
        } else {
            done.run(new Status(RaftError.EIO, "Fail to save counter snapshot %s", snapshot.getPath()));
        }
    });
}
Also used : Status(com.alipay.sofa.jraft.Status) CounterSnapshotFile(com.alipay.sofa.jraft.example.counter.snapshot.CounterSnapshotFile)

Example 90 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class GetValueRequestProcessor method handleRequest.

@Override
public void handleRequest(final RpcContext rpcCtx, final GetValueRequest request) {
    final CounterClosure closure = new CounterClosure() {

        @Override
        public void run(Status status) {
            rpcCtx.sendResponse(getValueResponse());
        }
    };
    this.counterService.get(request.getReadOnlySafe(), closure);
}
Also used : Status(com.alipay.sofa.jraft.Status) CounterClosure(com.alipay.sofa.jraft.example.counter.CounterClosure)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11