Search in sources :

Example 11 with Task

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

the class NodeImpl method executeApplyingTasks.

private void executeApplyingTasks(final List<LogEntryAndClosure> tasks) {
    this.writeLock.lock();
    try {
        final int size = tasks.size();
        if (this.state != State.STATE_LEADER) {
            final Status st = new Status();
            if (this.state != State.STATE_TRANSFERRING) {
                st.setError(RaftError.EPERM, "Is not leader.");
            } else {
                st.setError(RaftError.EBUSY, "Is transferring leadership.");
            }
            LOG.debug("Node {} can't apply, status={}.", getNodeId(), st);
            final List<Closure> dones = tasks.stream().map(ele -> ele.done).collect(Collectors.toList());
            Utils.runInThread(() -> {
                for (final Closure done : dones) {
                    done.run(st);
                }
            });
            return;
        }
        final List<LogEntry> entries = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            final LogEntryAndClosure task = tasks.get(i);
            if (task.expectedTerm != -1 && task.expectedTerm != this.currTerm) {
                LOG.debug("Node {} can't apply task whose expectedTerm={} doesn't match currTerm={}.", getNodeId(), task.expectedTerm, this.currTerm);
                if (task.done != null) {
                    final Status st = new Status(RaftError.EPERM, "expected_term=%d doesn't match current_term=%d", task.expectedTerm, this.currTerm);
                    Utils.runClosureInThread(task.done, st);
                    task.reset();
                }
                continue;
            }
            if (!this.ballotBox.appendPendingTask(this.conf.getConf(), this.conf.isStable() ? null : this.conf.getOldConf(), task.done)) {
                Utils.runClosureInThread(task.done, new Status(RaftError.EINTERNAL, "Fail to append task."));
                task.reset();
                continue;
            }
            // set task entry info before adding to list.
            task.entry.getId().setTerm(this.currTerm);
            task.entry.setType(EnumOutter.EntryType.ENTRY_TYPE_DATA);
            entries.add(task.entry);
            task.reset();
        }
        this.logManager.appendEntries(entries, new LeaderStableClosure(entries));
        // update conf.first
        checkAndSetConfiguration(true);
    } finally {
        this.writeLock.unlock();
    }
}
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) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) CatchUpClosure(com.alipay.sofa.jraft.closure.CatchUpClosure) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Closure(com.alipay.sofa.jraft.Closure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) LogEntry(com.alipay.sofa.jraft.entity.LogEntry)

Example 12 with Task

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

the class CliServiceTest method sendTestTaskAndWait.

@SuppressWarnings("SameParameterValue")
private void sendTestTaskAndWait(final Node node, final int code) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(10);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new ExpectClosure(code, null, latch));
        node.apply(task);
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer)

Example 13 with Task

use of com.alipay.sofa.jraft.entity.Task in project incubator-hugegraph by apache.

the class RaftNode method submitCommand.

private void submitCommand(StoreCommand command, RaftStoreClosure future) {
    // Wait leader elected
    LeaderInfo leaderInfo = this.waitLeaderElected(RaftSharedContext.NO_TIMEOUT);
    // If myself is not leader, forward to the leader
    if (!leaderInfo.selfIsLeader) {
        this.context.rpcForwarder().forwardToLeader(leaderInfo.leaderId, command, future);
        return;
    }
    // Sleep a while when raft node is busy
    this.waitIfBusy();
    Task task = new Task();
    // Compress data, note compress() will return a BytesBuffer
    ByteBuffer buffer = LZ4Util.compress(command.data(), RaftSharedContext.BLOCK_SIZE).forReadWritten().asByteBuffer();
    LOG.debug("Submit to raft node '{}', the compressed bytes of command " + "{} is {}", this.node, command.action(), buffer.limit());
    task.setData(buffer);
    task.setDone(future);
    this.node.apply(task);
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) ByteBuffer(java.nio.ByteBuffer)

Example 14 with Task

use of com.alipay.sofa.jraft.entity.Task in project mmqtt by MrHKing.

the class JRaftServer method applyOperation.

public void applyOperation(Node node, Message data, FailoverClosure closure) {
    final Task task = new Task();
    task.setDone(new MmqClosure(data, status -> {
        MmqClosure.MmqStatus mmqStatus = (MmqClosure.MmqStatus) status;
        closure.setThrowable(mmqStatus.getThrowable());
        closure.setResponse(mmqStatus.getResponse());
        closure.run(mmqStatus);
    }));
    task.setData(ByteBuffer.wrap(data.toByteArray()));
    node.apply(task);
}
Also used : Response(org.monkey.mmq.core.entity.Response) java.util(java.util) SerializeFactory(org.monkey.mmq.core.consistency.SerializeFactory) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Serializer(org.monkey.mmq.core.consistency.Serializer) CompletableFuture(java.util.concurrent.CompletableFuture) RequestProcessor4CP(org.monkey.mmq.core.consistency.cp.RequestProcessor4CP) ByteBuffer(java.nio.ByteBuffer) JRaftUtils(org.monkey.mmq.core.distributed.raft.utils.JRaftUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) com.alipay.sofa.jraft(com.alipay.sofa.jraft) RestResult(org.monkey.mmq.core.consistency.model.RestResult) BiConsumer(java.util.function.BiConsumer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RaftError(com.alipay.sofa.jraft.error.RaftError) MetricsMonitor(org.monkey.mmq.core.monitor.MetricsMonitor) org.monkey.mmq.core.utils(org.monkey.mmq.core.utils) PeerId(com.alipay.sofa.jraft.entity.PeerId) Executor(java.util.concurrent.Executor) Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) ReadRequest(org.monkey.mmq.core.entity.ReadRequest) org.monkey.mmq.core.distributed.raft.utils(org.monkey.mmq.core.distributed.raft.utils) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) CliServiceImpl(com.alipay.sofa.jraft.core.CliServiceImpl) Task(com.alipay.sofa.jraft.entity.Task) CliOptions(com.alipay.sofa.jraft.option.CliOptions) Paths(java.nio.file.Paths) CollectionUtils(org.springframework.util.CollectionUtils) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) RpcProcessor(com.alipay.sofa.jraft.rpc.RpcProcessor) Message(com.google.protobuf.Message) RequestProcessor(org.monkey.mmq.core.consistency.RequestProcessor) org.monkey.mmq.core.distributed.raft.exception(org.monkey.mmq.core.distributed.raft.exception) EnvUtil(org.monkey.mmq.core.env.EnvUtil) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) Joiner(com.google.common.base.Joiner) Task(com.alipay.sofa.jraft.entity.Task)

Example 15 with Task

use of com.alipay.sofa.jraft.entity.Task in project nacos by alibaba.

the class JRaftServer method applyOperation.

public void applyOperation(Node node, Message data, FailoverClosure closure) {
    final Task task = new Task();
    task.setDone(new NacosClosure(data, status -> {
        NacosClosure.NacosStatus nacosStatus = (NacosClosure.NacosStatus) status;
        closure.setThrowable(nacosStatus.getThrowable());
        closure.setResponse(nacosStatus.getResponse());
        closure.run(nacosStatus);
    }));
    // add request type field at the head of task data.
    byte[] requestTypeFieldBytes = new byte[2];
    requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
    if (data instanceof ReadRequest) {
        requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
    } else {
        requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
    }
    byte[] dataBytes = data.toByteArray();
    task.setData((ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes).put(dataBytes).position(0));
    node.apply(task);
}
Also used : LoggerUtils(com.alibaba.nacos.common.utils.LoggerUtils) FailoverClosure(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) Random(java.util.Random) ThreadUtils(com.alibaba.nacos.common.utils.ThreadUtils) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetricsMonitor(com.alibaba.nacos.core.monitor.MetricsMonitor) Map(java.util.Map) Endpoint(com.alipay.sofa.jraft.util.Endpoint) JRaftUtils(com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils) FailoverClosureImpl(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosureImpl) RestResult(com.alibaba.nacos.common.model.RestResult) SerializeFactory(com.alibaba.nacos.consistency.SerializeFactory) PeerId(com.alipay.sofa.jraft.entity.PeerId) EnvUtil(com.alibaba.nacos.sys.env.EnvUtil) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) ProtoMessageUtil(com.alibaba.nacos.consistency.ProtoMessageUtil) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) Collection(java.util.Collection) InternetAddressUtil(com.alibaba.nacos.common.utils.InternetAddressUtil) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Objects(java.util.Objects) List(java.util.List) JRaftException(com.alibaba.nacos.core.distributed.raft.exception.JRaftException) CliOptions(com.alipay.sofa.jraft.option.CliOptions) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) JRaftConstants(com.alibaba.nacos.core.distributed.raft.utils.JRaftConstants) StringUtils(com.alibaba.nacos.common.utils.StringUtils) CollectionUtils(org.springframework.util.CollectionUtils) RpcProcessor(com.alipay.sofa.jraft.rpc.RpcProcessor) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) Optional(java.util.Optional) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) JustForTest(com.alibaba.nacos.common.JustForTest) Loggers(com.alibaba.nacos.core.utils.Loggers) HashSet(java.util.HashSet) BiConsumer(java.util.function.BiConsumer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) RaftError(com.alipay.sofa.jraft.error.RaftError) ConvertUtils(com.alibaba.nacos.common.utils.ConvertUtils) DuplicateRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException) NoLeaderException(com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException) Serializer(com.alibaba.nacos.consistency.Serializer) Executor(java.util.concurrent.Executor) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor) RaftServiceFactory(com.alipay.sofa.jraft.RaftServiceFactory) Status(com.alipay.sofa.jraft.Status) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) RaftOptionsBuilder(com.alibaba.nacos.core.distributed.raft.utils.RaftOptionsBuilder) Response(com.alibaba.nacos.consistency.entity.Response) TimeUnit(java.util.concurrent.TimeUnit) RaftExecutor(com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor) CliServiceImpl(com.alipay.sofa.jraft.core.CliServiceImpl) Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) Paths(java.nio.file.Paths) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) CliService(com.alipay.sofa.jraft.CliService) Message(com.google.protobuf.Message) Collections(java.util.Collections) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) Task(com.alipay.sofa.jraft.entity.Task) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

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