Search in sources :

Example 26 with Closure

use of com.alipay.sofa.jraft.Closure in project jdchain-core by blockchain-jd-com.

the class RaftNodeServerServiceImpl method handleSubmitTxRequest.

@Override
public void handleSubmitTxRequest(SubmitTxRequest submitTxRequest, Closure done) {
    RpcResponseClosure responseClosure = (RpcResponseClosure) done;
    if (submitTxRequest.getTx() == null || submitTxRequest.getTx().length == 0) {
        done.run(new Status(RaftError.EREQUEST, "tx is empty"));
        return;
    }
    applyRequest(submitTxRequest, responseClosure, (req, closure) -> {
        int retryTimes = 0;
        try {
            final EventTranslator<SubmitTx> translator = (event, sequence) -> {
                event.reset();
                event.setDone(responseClosure);
                event.setValues(submitTxRequest.getTx());
            };
            while (true) {
                if (this.submitTxQueue.tryPublishEvent(translator)) {
                    break;
                } else {
                    retryTimes++;
                    if (retryTimes > MAX_SUBMIT_RETRY_TIMES) {
                        LOGGER.error("node {} submit request is overload.", nodeServer.getNode().getNodeId());
                        responseClosure.run(new Status(RaftError.EBUSY, "node has too many tasks."));
                        return;
                    }
                    ThreadHelper.onSpinWait();
                }
            }
        } catch (final Exception e) {
            LoggerUtils.errorIfEnabled(LOGGER, "fail to publish request:{} error", req, e);
            responseClosure.run(new Status(RaftError.EPERM, "publish tx error: %s", e.getMessage()));
        }
    });
}
Also used : Status(com.alipay.sofa.jraft.Status) TransactionState(com.jd.blockchain.ledger.TransactionState) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) BlockProposer(com.jd.blockchain.consensus.raft.consensus.BlockProposer) com.lmax.disruptor(com.lmax.disruptor) Block(com.jd.blockchain.consensus.raft.consensus.Block) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closure(com.alipay.sofa.jraft.Closure) BiConsumer(java.util.function.BiConsumer) RaftError(com.alipay.sofa.jraft.error.RaftError) BlockSerializer(com.jd.blockchain.consensus.raft.consensus.BlockSerializer) com.jd.blockchain.consensus.raft.rpc(com.jd.blockchain.consensus.raft.rpc) Logger(org.slf4j.Logger) PeerId(com.alipay.sofa.jraft.entity.PeerId) Configuration(com.alipay.sofa.jraft.conf.Configuration) com.alipay.sofa.jraft.util(com.alipay.sofa.jraft.util) ProducerType(com.lmax.disruptor.dsl.ProducerType) Status(com.alipay.sofa.jraft.Status) Collectors(java.util.stream.Collectors) JRaftUtils(com.alipay.sofa.jraft.JRaftUtils) LoggerUtils(com.jd.blockchain.consensus.raft.util.LoggerUtils) List(java.util.List) Task(com.alipay.sofa.jraft.entity.Task) Disruptor(com.lmax.disruptor.dsl.Disruptor)

Example 27 with Closure

use of com.alipay.sofa.jraft.Closure in project jdchain-core by blockchain-jd-com.

the class RaftNodeServerServiceImpl method proposalBlock.

private void proposalBlock(List<SubmitTx> submitTxList) {
    if (!proposer.canPropose()) {
        LoggerUtils.debugIfEnabled(LOGGER, "node {} can't propose block", nodeServer.getNode().getNodeId());
        for (SubmitTx submitTx : submitTxList) {
            submitTx.getDone().run(new Status(RaftError.ENEWLEADER, "node {} can't propose block", nodeServer.getNode().getNodeId()));
        }
        return;
    }
    LoggerUtils.debugIfEnabled(LOGGER, "node: {} begin proposal block, proposal tx size: {}", nodeServer.getNode().getNodeId(), submitTxList.size());
    List<byte[]> txList = submitTxList.stream().map(SubmitTx::getValues).collect(Collectors.toList());
    List<Closure> doneList = submitTxList.stream().map(SubmitTx::getDone).collect(Collectors.toList());
    try {
        final Task task = new Task();
        Block block = proposer.proposeBlock(txList);
        task.setData(ByteBuffer.wrap(serializer.serialize(block)));
        task.setDone(new BlockClosure(block, doneList));
        nodeServer.getNode().apply(task);
    } catch (Exception e) {
        LOGGER.error("proposal block error", e);
        // todo retry ?
        for (Closure closure : doneList) {
            runSubmitTxClosure(closure, i -> RpcResponse.fail(RaftError.UNKNOWN.getNumber(), e.getMessage()), 0);
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) TransactionState(com.jd.blockchain.ledger.TransactionState) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) BlockProposer(com.jd.blockchain.consensus.raft.consensus.BlockProposer) com.lmax.disruptor(com.lmax.disruptor) Block(com.jd.blockchain.consensus.raft.consensus.Block) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closure(com.alipay.sofa.jraft.Closure) BiConsumer(java.util.function.BiConsumer) RaftError(com.alipay.sofa.jraft.error.RaftError) BlockSerializer(com.jd.blockchain.consensus.raft.consensus.BlockSerializer) com.jd.blockchain.consensus.raft.rpc(com.jd.blockchain.consensus.raft.rpc) Logger(org.slf4j.Logger) PeerId(com.alipay.sofa.jraft.entity.PeerId) Configuration(com.alipay.sofa.jraft.conf.Configuration) com.alipay.sofa.jraft.util(com.alipay.sofa.jraft.util) ProducerType(com.lmax.disruptor.dsl.ProducerType) Status(com.alipay.sofa.jraft.Status) Collectors(java.util.stream.Collectors) JRaftUtils(com.alipay.sofa.jraft.JRaftUtils) LoggerUtils(com.jd.blockchain.consensus.raft.util.LoggerUtils) List(java.util.List) Task(com.alipay.sofa.jraft.entity.Task) Disruptor(com.lmax.disruptor.dsl.Disruptor) Task(com.alipay.sofa.jraft.entity.Task) Closure(com.alipay.sofa.jraft.Closure) Block(com.jd.blockchain.consensus.raft.consensus.Block)

Example 28 with Closure

use of com.alipay.sofa.jraft.Closure in project jdchain-core by blockchain-jd-com.

the class BlockClosure method run.

@Override
public void run(Status status) {
    int i = 0;
    for (Closure closure : this.getDoneList()) {
        int index = i;
        RpcResponseClosure submitTxDone = (RpcResponseClosure) closure;
        if (submitTxDone != null) {
            Utils.runClosureInThread(s -> {
                if (status.isOk()) {
                    AsyncFuture<byte[]> future = getFuture(index);
                    submitTxDone.setResponse(RpcResponse.success(future.get()));
                } else {
                    submitTxDone.setResponse(RpcResponse.fail(status.getCode(), status.getErrorMsg()));
                }
                submitTxDone.run(s);
            });
        }
        i++;
    }
}
Also used : RpcResponseClosure(com.jd.blockchain.consensus.raft.rpc.RpcResponseClosure) Closure(com.alipay.sofa.jraft.Closure) RpcResponseClosure(com.jd.blockchain.consensus.raft.rpc.RpcResponseClosure)

Aggregations

Closure (com.alipay.sofa.jraft.Closure)28 Status (com.alipay.sofa.jraft.Status)16 PeerId (com.alipay.sofa.jraft.entity.PeerId)11 List (java.util.List)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ArrayList (java.util.ArrayList)7 Configuration (com.alipay.sofa.jraft.conf.Configuration)6 Map (java.util.Map)6 RaftError (com.alipay.sofa.jraft.error.RaftError)5 TimeUnit (java.util.concurrent.TimeUnit)5 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)4 Node (com.alipay.sofa.jraft.Node)3 LeaderChangeContext (com.alipay.sofa.jraft.entity.LeaderChangeContext)3 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 LogId (com.alipay.sofa.jraft.entity.LogId)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)3 KeyValueTool.makeKey (com.alipay.sofa.jraft.rhea.KeyValueTool.makeKey)3