use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class NodeTest method testLeaderTransferResumeOnFailure.
@Test
public void testLeaderTransferResumeOnFailure() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 300);
for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint(), false, 1));
}
cluster.waitLeader();
Node leader = cluster.getLeader();
assertNotNull(leader);
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
assertTrue(cluster.stop(targetPeer.getEndpoint()));
this.sendTestTaskAndWait(leader);
assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
final Node savedLeader = leader;
// try to apply task when transferring leadership
CountDownLatch latch = new CountDownLatch(1);
Task task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(RaftError.EBUSY, latch));
leader.apply(task);
waitLatch(latch);
Thread.sleep(100);
cluster.waitLeader();
leader = cluster.getLeader();
assertSame(leader, savedLeader);
// restart target peer
assertTrue(cluster.start(targetPeer.getEndpoint()));
Thread.sleep(100);
// retry apply task
latch = new CountDownLatch(1);
task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(latch));
leader.apply(task);
waitLatch(latch);
assertTrue(cluster.ensureSame(5));
cluster.stopAll();
}
use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class CounterServiceImpl method applyOperation.
private void applyOperation(final CounterOperation op, final CounterClosure closure) {
if (!isLeader()) {
handlerNotLeaderError(closure);
return;
}
try {
closure.setCounterOperation(op);
final Task task = new Task();
task.setData(ByteBuffer.wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(op)));
task.setDone(closure);
this.counterServer.getNode().apply(task);
} catch (CodecException e) {
String errorMsg = "Fail to encode CounterOperation";
LOG.error(errorMsg, e);
closure.failure(errorMsg, StringUtils.EMPTY);
closure.run(new Status(RaftError.EINTERNAL, errorMsg));
}
}
use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class RaftRawKVStore method applyOperation.
private void applyOperation(final KVOperation op, final KVStoreClosure closure) {
if (!isLeader()) {
closure.setError(Errors.NOT_LEADER);
closure.run(new Status(RaftError.EPERM, "Not leader"));
return;
}
final Task task = new Task();
task.setData(ByteBuffer.wrap(Serializers.getDefault().writeObject(op)));
task.setDone(new KVClosureAdapter(closure, op));
this.node.apply(task);
}
use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class BaseAsyncUserProcessor method handleRequest.
@Override
public void handleRequest(final RpcContext rpcCtx, final T request) {
final AtomicRangeGroup group = server.getGroupBykey(request.getKey());
if (!group.getFsm().isLeader()) {
rpcCtx.sendResponse(group.redirect());
return;
}
final CommandType cmdType = getCmdType();
final Task task = createTask(rpcCtx, request, cmdType);
group.getNode().apply(task);
}
use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class BaseAsyncUserProcessor method createTask.
private Task createTask(RpcContext asyncCtx, T request, CommandType cmdType) {
final LeaderTaskClosure closure = new LeaderTaskClosure();
closure.setCmd(request);
closure.setCmdType(cmdType);
closure.setDone(status -> {
if (status.isOk()) {
asyncCtx.sendResponse(closure.getResponse());
} else {
asyncCtx.sendResponse(new BooleanCommand(false, status.getErrorMsg()));
}
});
final byte[] cmdBytes = CommandCodec.encodeCommand(request);
final ByteBuffer data = ByteBuffer.allocate(cmdBytes.length + 1);
data.put(cmdType.toByte());
data.put(cmdBytes);
data.flip();
return new Task(data, closure);
}
Aggregations