Search in sources :

Example 26 with Task

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

the class NodeImpl method apply.

@Override
public void apply(final Task task) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(task.getDone(), new Status(RaftError.ENODESHUTDOWN, "Node is shutting down."));
        throw new IllegalStateException("Node is shutting down");
    }
    Requires.requireNonNull(task, "Null task");
    final LogEntry entry = new LogEntry();
    entry.setData(task.getData());
    final EventTranslator<LogEntryAndClosure> translator = (event, sequence) -> {
        event.reset();
        event.done = task.getDone();
        event.entry = entry;
        event.expectedTerm = task.getExpectedTerm();
    };
    switch(this.options.getApplyTaskMode()) {
        case Blocking:
            this.applyQueue.publishEvent(translator);
            break;
        case NonBlocking:
        default:
            if (!this.applyQueue.tryPublishEvent(translator)) {
                String errorMsg = "Node is busy, has too many tasks, queue is full and bufferSize=" + this.applyQueue.getBufferSize();
                Utils.runClosureInThread(task.getDone(), new Status(RaftError.EBUSY, errorMsg));
                LOG.warn("Node {} applyQueue is overload.", getNodeId());
                this.metrics.recordTimes("apply-task-overload-times", 1);
                if (task.getDone() == null) {
                    throw new OverloadException(errorMsg);
                }
            }
            break;
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) SnapshotExecutorImpl(com.alipay.sofa.jraft.storage.snapshot.SnapshotExecutorImpl) Platform(com.alipay.sofa.jraft.util.Platform) StringUtils(org.apache.commons.lang.StringUtils) Ballot(com.alipay.sofa.jraft.entity.Ballot) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) LeaderChangeContext(com.alipay.sofa.jraft.entity.LeaderChangeContext) SystemPropertyUtil(com.alipay.sofa.jraft.util.SystemPropertyUtil) InstallSnapshotResponse(com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotResponse) ReplicatorGroup(com.alipay.sofa.jraft.ReplicatorGroup) RaftMetaStorageOptions(com.alipay.sofa.jraft.option.RaftMetaStorageOptions) RaftMetaStorage(com.alipay.sofa.jraft.storage.RaftMetaStorage) AppendEntriesResponse(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesResponse) RpcFactoryHelper(com.alipay.sofa.jraft.util.RpcFactoryHelper) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ConfigurationManager(com.alipay.sofa.jraft.conf.ConfigurationManager) PeerId(com.alipay.sofa.jraft.entity.PeerId) Set(java.util.Set) RaftServerService(com.alipay.sofa.jraft.rpc.RaftServerService) JRaftServiceFactory(com.alipay.sofa.jraft.JRaftServiceFactory) JRaftUtils(com.alipay.sofa.jraft.JRaftUtils) CountDownLatch(java.util.concurrent.CountDownLatch) ReadOnlyServiceOptions(com.alipay.sofa.jraft.option.ReadOnlyServiceOptions) RequestVoteResponse(com.alipay.sofa.jraft.rpc.RpcRequests.RequestVoteResponse) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LogId(com.alipay.sofa.jraft.entity.LogId) FSMCallerOptions(com.alipay.sofa.jraft.option.FSMCallerOptions) ReadIndexResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexResponse) ReadOnlyOption(com.alipay.sofa.jraft.option.ReadOnlyOption) JRaftSignalHandler(com.alipay.sofa.jraft.util.JRaftSignalHandler) ArrayList(java.util.ArrayList) LogManagerOptions(com.alipay.sofa.jraft.option.LogManagerOptions) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) LogManagerImpl(com.alipay.sofa.jraft.storage.impl.LogManagerImpl) RaftError(com.alipay.sofa.jraft.error.RaftError) EventHandler(com.lmax.disruptor.EventHandler) LinkedHashSet(java.util.LinkedHashSet) SignalHelper(com.alipay.sofa.jraft.util.SignalHelper) SnapshotExecutorOptions(com.alipay.sofa.jraft.option.SnapshotExecutorOptions) RaftOutter(com.alipay.sofa.jraft.entity.RaftOutter) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) DisruptorBuilder(com.alipay.sofa.jraft.util.DisruptorBuilder) ClosureQueue(com.alipay.sofa.jraft.closure.ClosureQueue) TimeoutNowRequest(com.alipay.sofa.jraft.rpc.RpcRequests.TimeoutNowRequest) RaftClientService(com.alipay.sofa.jraft.rpc.RaftClientService) Lock(java.util.concurrent.locks.Lock) FSMCaller(com.alipay.sofa.jraft.FSMCaller) RequestVoteRequest(com.alipay.sofa.jraft.rpc.RpcRequests.RequestVoteRequest) InstallSnapshotRequest(com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotRequest) CatchUpClosure(com.alipay.sofa.jraft.closure.CatchUpClosure) Disruptor(com.lmax.disruptor.dsl.Disruptor) OverloadException(com.alipay.sofa.jraft.error.OverloadException) ScheduledFuture(java.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) JRaftServiceLoader(com.alipay.sofa.jraft.util.JRaftServiceLoader) Describer(com.alipay.sofa.jraft.util.Describer) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogManager(com.alipay.sofa.jraft.storage.LogManager) RaftTimerFactory(com.alipay.sofa.jraft.util.timer.RaftTimerFactory) LogExceptionHandler(com.alipay.sofa.jraft.util.LogExceptionHandler) OnlyForTest(com.alipay.sofa.jraft.util.OnlyForTest) TimeoutNowResponse(com.alipay.sofa.jraft.rpc.RpcRequests.TimeoutNowResponse) Configuration(com.alipay.sofa.jraft.conf.Configuration) Collection(java.util.Collection) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) BootstrapOptions(com.alipay.sofa.jraft.option.BootstrapOptions) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) LogStorage(com.alipay.sofa.jraft.storage.LogStorage) Collectors(java.util.stream.Collectors) List(java.util.List) RaftException(com.alipay.sofa.jraft.error.RaftException) Requires(com.alipay.sofa.jraft.util.Requires) LogIndexOutOfBoundsException(com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException) AppendEntriesRequest(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest) UserLog(com.alipay.sofa.jraft.entity.UserLog) LogNotFoundException(com.alipay.sofa.jraft.error.LogNotFoundException) ReadIndexRequest(com.alipay.sofa.jraft.rpc.RpcRequests.ReadIndexRequest) LongHeldDetectingReadWriteLock(com.alipay.sofa.jraft.util.concurrent.LongHeldDetectingReadWriteLock) Utils(com.alipay.sofa.jraft.util.Utils) EnumOutter(com.alipay.sofa.jraft.entity.EnumOutter) SnapshotExecutor(com.alipay.sofa.jraft.storage.SnapshotExecutor) DefaultRaftClientService(com.alipay.sofa.jraft.rpc.impl.core.DefaultRaftClientService) HashSet(java.util.HashSet) NodeManager(com.alipay.sofa.jraft.NodeManager) Closure(com.alipay.sofa.jraft.Closure) ThreadId(com.alipay.sofa.jraft.util.ThreadId) ClosureQueueImpl(com.alipay.sofa.jraft.closure.ClosureQueueImpl) Logger(org.slf4j.Logger) DisruptorMetricSet(com.alipay.sofa.jraft.util.DisruptorMetricSet) NodeId(com.alipay.sofa.jraft.entity.NodeId) RingBuffer(com.lmax.disruptor.RingBuffer) ProducerType(com.lmax.disruptor.dsl.ProducerType) Status(com.alipay.sofa.jraft.Status) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) TimeUnit(java.util.concurrent.TimeUnit) Task(com.alipay.sofa.jraft.entity.Task) EventTranslator(com.lmax.disruptor.EventTranslator) Node(com.alipay.sofa.jraft.Node) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) ReplicatorGroupOptions(com.alipay.sofa.jraft.option.ReplicatorGroupOptions) Message(com.google.protobuf.Message) ReadOnlyService(com.alipay.sofa.jraft.ReadOnlyService) BallotBoxOptions(com.alipay.sofa.jraft.option.BallotBoxOptions) EventFactory(com.lmax.disruptor.EventFactory) RepeatedTimer(com.alipay.sofa.jraft.util.RepeatedTimer) RpcResponseClosureAdapter(com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter) OverloadException(com.alipay.sofa.jraft.error.OverloadException) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 27 with Task

use of com.alipay.sofa.jraft.entity.Task 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)

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