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());
}
}
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());
}
}
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);
}
});
}
});
}
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()));
}
});
}
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);
}
Aggregations