Search in sources :

Example 86 with Node

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

the class CliServiceTest method testChangePeers.

@Test
public void testChangePeers() throws Exception {
    final List<PeerId> newPeers = TestUtils.generatePeers(10);
    newPeers.removeAll(this.conf.getPeerSet());
    for (final PeerId peer : newPeers) {
        assertTrue(this.cluster.start(peer.getEndpoint()));
    }
    this.cluster.waitLeader();
    final Node oldLeaderNode = this.cluster.getLeader();
    assertNotNull(oldLeaderNode);
    final PeerId oldLeader = oldLeaderNode.getNodeId().getPeerId();
    assertNotNull(oldLeader);
    assertTrue(this.cliService.changePeers(this.groupId, this.conf, new Configuration(newPeers)).isOk());
    this.cluster.waitLeader();
    final PeerId newLeader = this.cluster.getLeader().getNodeId().getPeerId();
    assertNotEquals(oldLeader, newLeader);
    assertTrue(newPeers.contains(newLeader));
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 87 with Node

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

the class TestCluster method stop.

public boolean stop(final Endpoint listenAddr) throws InterruptedException {
    final Node node = removeNode(listenAddr);
    final CountDownLatch latch = new CountDownLatch(1);
    if (node != null) {
        node.shutdown(new ExpectClosure(latch));
        node.join();
        latch.await();
    }
    final RaftGroupService raftGroupService = this.serverMap.remove(listenAddr.toString());
    raftGroupService.shutdown();
    raftGroupService.join();
    return node != null;
}
Also used : Node(com.alipay.sofa.jraft.Node) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 88 with Node

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

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

the class Replicator method notifyReplicatorStatusListener.

/**
 * Notify replicator event(such as created, error, destroyed) to replicatorStateListener which is implemented by users.
 *
 * @param replicator replicator object
 * @param event      replicator's state listener event type
 * @param status     replicator's error detailed status
 */
private static void notifyReplicatorStatusListener(final Replicator replicator, final ReplicatorEvent event, final Status status, final ReplicatorState newState) {
    final ReplicatorOptions replicatorOpts = Requires.requireNonNull(replicator.getOpts(), "replicatorOptions");
    final Node node = Requires.requireNonNull(replicatorOpts.getNode(), "node");
    final PeerId peer = Requires.requireNonNull(replicatorOpts.getPeerId(), "peer");
    final List<ReplicatorStateListener> listenerList = node.getReplicatorStatueListeners();
    for (int i = 0; i < listenerList.size(); i++) {
        final ReplicatorStateListener listener = listenerList.get(i);
        if (listener != null) {
            try {
                switch(event) {
                    case CREATED:
                        RpcUtils.runInThread(() -> listener.onCreated(peer));
                        break;
                    case ERROR:
                        RpcUtils.runInThread(() -> listener.onError(peer, status));
                        break;
                    case DESTROYED:
                        RpcUtils.runInThread(() -> listener.onDestroyed(peer));
                        break;
                    case STATE_CHANGED:
                        RpcUtils.runInThread(() -> listener.stateChanged(peer, newState));
                    default:
                        break;
                }
            } catch (final Exception e) {
                LOG.error("Fail to notify ReplicatorStatusListener, listener={}, event={}.", listener, event);
            }
        }
    }
}
Also used : Node(com.alipay.sofa.jraft.Node) ReplicatorOptions(com.alipay.sofa.jraft.option.ReplicatorOptions) RaftException(com.alipay.sofa.jraft.error.RaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 90 with Node

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

the class RaftGroupManagerImpl method setLeader.

@Override
public String setLeader(String endpoint) {
    PeerId newLeaderId = PeerId.parsePeer(endpoint);
    Node node = this.raftNode.node();
    // If expected endpoint has already been raft leader
    if (node.getLeaderId().equals(newLeaderId)) {
        return newLeaderId.toString();
    }
    if (this.raftNode.selfIsLeader()) {
        // If current node is the leader, transfer directly
        this.transferLeaderTo(endpoint);
    } else {
        // If current node is not leader, forward request to leader
        SetLeaderRequest request = SetLeaderRequest.newBuilder().setEndpoint(endpoint).build();
        try {
            RaftClosure<SetLeaderResponse> future;
            future = this.forwardToLeader(request);
            future.waitFinished();
        } catch (Throwable e) {
            throw new BackendException("Failed to set leader to '%s'", e, endpoint);
        }
    }
    return newLeaderId.toString();
}
Also used : SetLeaderResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderResponse) Node(com.alipay.sofa.jraft.Node) SetLeaderRequest(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderRequest) PeerId(com.alipay.sofa.jraft.entity.PeerId) BackendException(com.baidu.hugegraph.backend.BackendException)

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