Search in sources :

Example 36 with Node

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

the class NodeTest method testSingleNodeWithLearner.

@Test
public void testSingleNodeWithLearner() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    final Endpoint learnerAddr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT + 1);
    final PeerId learnerPeer = new PeerId(learnerAddr, 0);
    NodeManager.getInstance().addAddress(addr);
    NodeManager.getInstance().addAddress(learnerAddr);
    MockStateMachine learnerFsm = null;
    Node learner = null;
    RaftGroupService learnerServer = null;
    {
        // Start learner
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        learnerFsm = new MockStateMachine(learnerAddr);
        nodeOptions.setFsm(learnerFsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log1");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta1");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot1");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(learnerAddr);
        learnerServer = new RaftGroupService("unittest", new PeerId(learnerAddr, 0), nodeOptions, rpcServer);
        learner = learnerServer.start();
    }
    {
        // Start leader
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        final MockStateMachine fsm = new MockStateMachine(addr);
        nodeOptions.setFsm(fsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        final Node node = new NodeImpl("unittest", peer);
        assertTrue(node.init(nodeOptions));
        assertEquals(1, node.listPeers().size());
        assertTrue(node.listPeers().contains(peer));
        while (!node.isLeader()) {
            ;
        }
        sendTestTaskAndWait(node);
        assertEquals(10, fsm.getLogs().size());
        int i = 0;
        for (final ByteBuffer data : fsm.getLogs()) {
            assertEquals("hello" + i++, new String(data.array()));
        }
        // wait for entries to be replicated to learner.
        Thread.sleep(1000);
        node.shutdown();
        node.join();
    }
    {
        // assert learner fsm
        assertEquals(10, learnerFsm.getLogs().size());
        int i = 0;
        for (final ByteBuffer data : learnerFsm.getLogs()) {
            assertEquals("hello" + i++, new String(data.array()));
        }
        learnerServer.shutdown();
        learnerServer.join();
    }
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 37 with Node

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

the class NodeTest method testShuttingDownLeaderTriggerTimeoutNow.

@Test
public void testShuttingDownLeaderTriggerTimeoutNow() 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()));
    }
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    final Node oldLeader = leader;
    LOG.info("Shutdown leader {}", leader);
    leader.shutdown();
    leader.join();
    Thread.sleep(100);
    leader = cluster.getLeader();
    cluster.waitLeader();
    assertNotNull(leader);
    assertNotSame(leader, oldLeader);
    cluster.stopAll();
}
Also used : Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 38 with Node

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

the class NodeImpl method shutdown.

@Override
public void shutdown(Closure done) {
    List<RepeatedTimer> timers = null;
    this.writeLock.lock();
    try {
        LOG.info("Node {} shutdown, currTerm={} state={}.", getNodeId(), this.currTerm, this.state);
        if (this.state.compareTo(State.STATE_SHUTTING) < 0) {
            NodeManager.getInstance().remove(this);
            // If it is follower, call on_stop_following in step_down
            if (this.state.compareTo(State.STATE_FOLLOWER) <= 0) {
                stepDown(this.currTerm, this.state == State.STATE_LEADER, new Status(RaftError.ESHUTDOWN, "Raft node is going to quit."));
            }
            this.state = State.STATE_SHUTTING;
            // Stop all timers
            timers = stopAllTimers();
            if (this.readOnlyService != null) {
                this.readOnlyService.shutdown();
            }
            if (this.logManager != null) {
                this.logManager.shutdown();
            }
            if (this.metaStorage != null) {
                this.metaStorage.shutdown();
            }
            if (this.snapshotExecutor != null) {
                this.snapshotExecutor.shutdown();
            }
            if (this.wakingCandidate != null) {
                Replicator.stop(this.wakingCandidate);
            }
            if (this.fsmCaller != null) {
                this.fsmCaller.shutdown();
            }
            if (this.rpcService != null) {
                this.rpcService.shutdown();
            }
            if (this.applyQueue != null) {
                final CountDownLatch latch = new CountDownLatch(1);
                this.shutdownLatch = latch;
                Utils.runInThread(() -> this.applyQueue.publishEvent((event, sequence) -> event.shutdownLatch = latch));
            } else {
                final int num = GLOBAL_NUM_NODES.decrementAndGet();
                LOG.info("The number of active nodes decrement to {}.", num);
            }
            if (this.timerManager != null) {
                this.timerManager.shutdown();
            }
        }
        if (this.state != State.STATE_SHUTDOWN) {
            if (done != null) {
                this.shutdownContinuations.add(done);
                done = null;
            }
            return;
        }
    } finally {
        this.writeLock.unlock();
        // Destroy all timers out of lock
        if (timers != null) {
            destroyAllTimers(timers);
        }
        // Call join() asynchronously
        final Closure shutdownHook = done;
        Utils.runInThread(() -> {
            try {
                join();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                // a writeLock which is already held by the caller
                if (shutdownHook != null) {
                    shutdownHook.run(Status.OK());
                }
            }
        });
    }
}
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) RepeatedTimer(com.alipay.sofa.jraft.util.RepeatedTimer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 39 with Node

use of com.alipay.sofa.jraft.Node 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 40 with Node

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

the class BaseCliRequestProcessor method processRequest.

@Override
public Message processRequest(T request, RpcRequestClosure done) {
    String groupId = getGroupId(request);
    String peerIdStr = getPeerId(request);
    PeerId peerId = null;
    if (!StringUtils.isBlank(peerIdStr)) {
        peerId = new PeerId();
        if (!peerId.parse(peerIdStr)) {
            return // 
            RpcFactoryHelper.responseFactory().newResponse(defaultResp(), RaftError.EINVAL, "Fail to parse peer: %s", peerIdStr);
        }
    }
    Status st = new Status();
    Node node = getNode(groupId, peerId, st);
    if (!st.isOk()) {
        return // 
        RpcFactoryHelper.responseFactory().newResponse(defaultResp(), st.getCode(), st.getErrorMsg());
    } else {
        return processRequest0(new CliRequestContext(node, groupId, peerId), request, done);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Node (com.alipay.sofa.jraft.Node)90 PeerId (com.alipay.sofa.jraft.entity.PeerId)74 Test (org.junit.Test)64 Endpoint (com.alipay.sofa.jraft.util.Endpoint)41 CountDownLatch (java.util.concurrent.CountDownLatch)32 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 ArrayList (java.util.ArrayList)22 Status (com.alipay.sofa.jraft.Status)21 Task (com.alipay.sofa.jraft.entity.Task)17 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)17 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)14 ByteBuffer (java.nio.ByteBuffer)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)10 RaftException (com.alipay.sofa.jraft.error.RaftException)8 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)7 LinkedHashSet (java.util.LinkedHashSet)7 NodeId (com.alipay.sofa.jraft.entity.NodeId)6 LogIndexOutOfBoundsException (com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException)6 LogNotFoundException (com.alipay.sofa.jraft.error.LogNotFoundException)6