Search in sources :

Example 21 with Task

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();
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 22 with Task

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));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) CodecException(com.alipay.remoting.exception.CodecException)

Example 23 with Task

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task)

Example 24 with 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);
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) CommandType(com.alipay.sofa.jraft.test.atomic.server.CommandType) AtomicRangeGroup(com.alipay.sofa.jraft.test.atomic.server.AtomicRangeGroup)

Example 25 with 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);
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) LeaderTaskClosure(com.alipay.sofa.jraft.test.atomic.server.LeaderTaskClosure) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Task (com.alipay.sofa.jraft.entity.Task)27 PeerId (com.alipay.sofa.jraft.entity.PeerId)18 ByteBuffer (java.nio.ByteBuffer)17 Node (com.alipay.sofa.jraft.Node)16 Status (com.alipay.sofa.jraft.Status)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 Test (org.junit.Test)13 Endpoint (com.alipay.sofa.jraft.util.Endpoint)12 Configuration (com.alipay.sofa.jraft.conf.Configuration)10 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)7 ArrayList (java.util.ArrayList)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)6 RaftError (com.alipay.sofa.jraft.error.RaftError)6 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)6 List (java.util.List)5 JRaftUtils (com.alipay.sofa.jraft.JRaftUtils)4 TaskClosure (com.alipay.sofa.jraft.closure.TaskClosure)4 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)4 TimeUnit (java.util.concurrent.TimeUnit)4