Search in sources :

Example 26 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class ItNodeTest method testChangePeers.

@Test
public void testChangePeers() throws Exception {
    PeerId peer0 = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    cluster = new TestCluster("testChangePeers", dataPath, Collections.singletonList(peer0), testInfo);
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    sendTestTaskAndWait(leader);
    for (int i = 1; i < 10; i++) {
        PeerId peer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + i);
        assertTrue(cluster.start(peer.getEndpoint(), false, 300));
    }
    for (int i = 0; i < 9; i++) {
        cluster.waitLeader();
        leader = cluster.getLeader();
        assertNotNull(leader);
        PeerId peer = new PeerId(TestUtils.getLocalAddress(), peer0.getEndpoint().getPort() + i);
        assertEquals(peer, leader.getNodeId().getPeerId());
        peer = new PeerId(TestUtils.getLocalAddress(), peer0.getEndpoint().getPort() + i + 1);
        SynchronizedClosure done = new SynchronizedClosure();
        leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
        Status status = done.await();
        assertTrue(status.isOk(), status.getRaftError().toString());
    }
    cluster.waitLeader();
    for (MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(10, fsm.getLogs().size());
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 27 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class ItNodeTest method testChangePeersStepsDownInJointConsensus.

@Test
public void testChangePeersStepsDownInJointConsensus() throws Exception {
    List<PeerId> peers = new ArrayList<>();
    PeerId peer0 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5006");
    PeerId peer1 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5007");
    PeerId peer2 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5008");
    PeerId peer3 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5009");
    // start single cluster
    peers.add(peer0);
    cluster = new TestCluster("testChangePeersStepsDownInJointConsensus", dataPath, peers, testInfo);
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    sendTestTaskAndWait(leader);
    // start peer1-3
    assertTrue(cluster.start(peer1.getEndpoint()));
    assertTrue(cluster.start(peer2.getEndpoint()));
    assertTrue(cluster.start(peer3.getEndpoint()));
    // Make sure the topology is ready before adding peers.
    assertTrue(waitForTopology(cluster, leader.getNodeId().getPeerId().getEndpoint(), 4, 3_000));
    Configuration conf = new Configuration();
    conf.addPeer(peer0);
    conf.addPeer(peer1);
    conf.addPeer(peer2);
    conf.addPeer(peer3);
    // change peers
    SynchronizedClosure done = new SynchronizedClosure();
    leader.changePeers(conf, done);
    assertTrue(done.await().isOk());
    // stop peer3
    assertTrue(cluster.stop(peer3.getEndpoint()));
    conf.removePeer(peer0);
    conf.removePeer(peer1);
    // Change peers to [peer2, peer3], which must fail since peer3 is stopped
    done.reset();
    leader.changePeers(conf, done);
    assertEquals(RaftError.EPERM, done.await().getRaftError());
    LOG.info(done.getStatus().toString());
    assertFalse(((NodeImpl) leader).getConf().isStable());
    leader = cluster.getLeader();
    assertNull(leader);
    assertTrue(cluster.start(peer3.getEndpoint()));
    Thread.sleep(1000);
    cluster.waitLeader();
    leader = cluster.getLeader();
    List<PeerId> thePeers = leader.listPeers();
    assertTrue(!thePeers.isEmpty());
    assertEquals(conf.getPeerSet(), new HashSet<>(thePeers));
}
Also used : SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 28 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class ItNodeTest method readCommittedUserLog.

@Test
public void readCommittedUserLog() throws Exception {
    // setup cluster
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    int amount = 10;
    sendTestTaskAndWait(leader, amount);
    assertTrue(waitForCondition(() -> {
        try {
            // index == 1 is a CONFIGURATION log
            UserLog userLog = leader.readCommittedUserLog(1 + amount);
            return userLog != null;
        } catch (Exception ignore) {
            // is updated, so we need to wait.
            return false;
        }
    }, 10_000));
    // index == 1 is a CONFIGURATION log, so real_index will be 2 when returned.
    UserLog userLog = leader.readCommittedUserLog(1);
    assertNotNull(userLog);
    assertEquals(2, userLog.getIndex());
    assertEquals("hello0", stringFromBytes(userLog.getData().array()));
    // index == 5 is a DATA log(a user log)
    userLog = leader.readCommittedUserLog(5);
    assertNotNull(userLog);
    assertEquals(5, userLog.getIndex());
    assertEquals("hello3", stringFromBytes(userLog.getData().array()));
    // index == 15 is greater than last_committed_index
    try {
        assertNull(leader.readCommittedUserLog(15));
        fail();
    } catch (LogIndexOutOfBoundsException e) {
        assertEquals("Request index 15 is greater than lastAppliedIndex: 11", e.getMessage());
    }
    // index == 0 invalid request
    try {
        assertNull(leader.readCommittedUserLog(0));
        fail();
    } catch (LogIndexOutOfBoundsException e) {
        assertEquals("Request index is invalid: 0", e.getMessage());
    }
    LOG.info("Trigger leader snapshot");
    CountDownLatch latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    // remove and add a peer to add two CONFIGURATION logs
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    Node testFollower = followers.get(0);
    latch = new CountDownLatch(1);
    leader.removePeer(testFollower.getNodeId().getPeerId(), new ExpectClosure(latch));
    waitLatch(latch);
    latch = new CountDownLatch(1);
    leader.addPeer(testFollower.getNodeId().getPeerId(), new ExpectClosure(latch));
    waitLatch(latch);
    sendTestTaskAndWait(leader, amount, RaftError.SUCCESS);
    // trigger leader snapshot for the second time, after this the log of index 1~11 will be deleted.
    LOG.info("Trigger leader snapshot");
    latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    Thread.sleep(100);
    // index == 5 log has been deleted in log_storage.
    try {
        leader.readCommittedUserLog(5);
        fail();
    } catch (LogNotFoundException e) {
        assertEquals("User log is deleted at index: 5", e.getMessage());
    }
    // index == 12、index == 13、index=14、index=15 are 4 CONFIGURATION logs(joint consensus), so real_index will be 16 when returned.
    userLog = leader.readCommittedUserLog(12);
    assertNotNull(userLog);
    assertEquals(16, userLog.getIndex());
    assertEquals("hello10", stringFromBytes(userLog.getData().array()));
    // now index == 17 is a user log
    userLog = leader.readCommittedUserLog(17);
    assertNotNull(userLog);
    assertEquals(17, userLog.getIndex());
    assertEquals("hello11", stringFromBytes(userLog.getData().array()));
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
        for (int i = 0; i < 20; i++) assertEquals("hello" + i, stringFromBytes(fsm.getLogs().get(i).array()));
    }
}
Also used : Node(org.apache.ignite.raft.jraft.Node) UserLog(org.apache.ignite.raft.jraft.entity.UserLog) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftException(org.apache.ignite.raft.jraft.error.RaftException) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 29 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class ItNodeTest method testRestoreSnapshotWithDelta.

/**
 * @throws Exception
 */
@Test
public void testRestoreSnapshotWithDelta() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    LOG.info("Leader: " + leader);
    assertNotNull(leader);
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    cluster.ensureSame();
    triggerLeaderSnapshot(cluster, leader);
    // stop leader
    Endpoint leaderAddr = leader.getNodeId().getPeerId().getEndpoint().copy();
    assertTrue(cluster.stop(leaderAddr));
    // restart leader
    cluster.waitLeader();
    sendTestTaskAndWait(cluster.getLeader(), 10, RaftError.SUCCESS);
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
    assertTrue(cluster.start(leaderAddr));
    Node oldLeader = cluster.getNode(leaderAddr);
    cluster.ensureSame();
    assertEquals(0, cluster.getLeaderFsm().getLoadSnapshotTimes());
    MockStateMachine fsm = (MockStateMachine) oldLeader.getOptions().getFsm();
    assertEquals(1, fsm.getLoadSnapshotTimes());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 30 with Node

use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.

the class NodeImpl method shutdown.

@Override
public void shutdown(final Closure done) {
    this.writeLock.lock();
    try {
        LOG.info("Node {} shutdown, currTerm={} state={}.", getNodeId(), this.currTerm, this.state);
        if (this.state.compareTo(State.STATE_SHUTTING) < 0) {
            // 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 pending timer callbacks.
            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.rpcClientService != null) {
                this.rpcClientService.shutdown();
            }
            if (this.applyQueue != null) {
                final CountDownLatch latch = new CountDownLatch(1);
                this.shutdownLatch = latch;
                Utils.runInThread(this.getOptions().getCommonExecutor(), () -> this.applyQueue.publishEvent((event, sequence) -> {
                    event.groupId = groupId;
                    event.shutdownLatch = latch;
                }));
            }
        }
        if (this.state != State.STATE_SHUTDOWN) {
            if (done != null) {
                this.shutdownContinuations.add(done);
            }
            return;
        }
        // a writeLock which is already held by the caller
        if (done != null) {
            Utils.runClosureInThread(this.getOptions().getCommonExecutor(), done);
        }
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) SnapshotExecutorImpl(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotExecutorImpl) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Set(java.util.Set) AppendEntriesResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.AppendEntriesResponse) ExecutorServiceHelper(org.apache.ignite.raft.jraft.util.ExecutorServiceHelper) AppendEntriesRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.AppendEntriesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) SnapshotExecutorOptions(org.apache.ignite.raft.jraft.option.SnapshotExecutorOptions) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RaftError(org.apache.ignite.raft.jraft.error.RaftError) LogManagerImpl(org.apache.ignite.raft.jraft.storage.impl.LogManagerImpl) StringUtils(org.apache.ignite.raft.jraft.util.StringUtils) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) RaftException(org.apache.ignite.raft.jraft.error.RaftException) RaftMetaStorageOptions(org.apache.ignite.raft.jraft.option.RaftMetaStorageOptions) ArrayList(java.util.ArrayList) Task(org.apache.ignite.raft.jraft.entity.Task) TimeoutStrategy(org.apache.ignite.raft.jraft.util.TimeoutStrategy) SnapshotExecutor(org.apache.ignite.raft.jraft.storage.SnapshotExecutor) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CatchUpClosure(org.apache.ignite.raft.jraft.closure.CatchUpClosure) BallotBoxOptions(org.apache.ignite.raft.jraft.option.BallotBoxOptions) EventHandler(com.lmax.disruptor.EventHandler) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) ReadIndexResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexResponse) LinkedHashSet(java.util.LinkedHashSet) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) SystemPropertyUtil(org.apache.ignite.raft.jraft.util.SystemPropertyUtil) TimeoutNowResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.TimeoutNowResponse) Closure(org.apache.ignite.raft.jraft.Closure) ClosureQueue(org.apache.ignite.raft.jraft.closure.ClosureQueue) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) DefaultRaftClientService(org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService) RaftRpcFactory(org.apache.ignite.raft.jraft.rpc.RaftRpcFactory) Lock(java.util.concurrent.locks.Lock) LogManagerOptions(org.apache.ignite.raft.jraft.option.LogManagerOptions) FSMCaller(org.apache.ignite.raft.jraft.FSMCaller) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) TimeoutNowRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.TimeoutNowRequest) LeaderChangeContext(org.apache.ignite.raft.jraft.entity.LeaderChangeContext) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RaftServerService(org.apache.ignite.raft.jraft.rpc.RaftServerService) LogManager(org.apache.ignite.raft.jraft.storage.LogManager) ScheduledFuture(java.util.concurrent.ScheduledFuture) EnumOutter(org.apache.ignite.raft.jraft.entity.EnumOutter) Requires(org.apache.ignite.raft.jraft.util.Requires) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ByteBuffer(java.nio.ByteBuffer) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) ReadIndexResponseBuilder(org.apache.ignite.raft.jraft.rpc.ReadIndexResponseBuilder) ReplicatorGroupOptions(org.apache.ignite.raft.jraft.option.ReplicatorGroupOptions) ReadOnlyOption(org.apache.ignite.raft.jraft.option.ReadOnlyOption) AppendEntriesResponseBuilder(org.apache.ignite.raft.jraft.rpc.AppendEntriesResponseBuilder) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) RpcResponseClosureAdapter(org.apache.ignite.raft.jraft.rpc.RpcResponseClosureAdapter) Node(org.apache.ignite.raft.jraft.Node) LogId(org.apache.ignite.raft.jraft.entity.LogId) ReadOnlyService(org.apache.ignite.raft.jraft.ReadOnlyService) Collection(java.util.Collection) Status(org.apache.ignite.raft.jraft.Status) GroupAware(org.apache.ignite.raft.jraft.disruptor.GroupAware) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) BootstrapOptions(org.apache.ignite.raft.jraft.option.BootstrapOptions) Collectors(java.util.stream.Collectors) OnlyForTest(org.apache.ignite.raft.jraft.util.OnlyForTest) Utils(org.apache.ignite.raft.jraft.util.Utils) List(java.util.List) LogStorage(org.apache.ignite.raft.jraft.storage.LogStorage) LongHeldDetectingReadWriteLock(org.apache.ignite.raft.jraft.util.concurrent.LongHeldDetectingReadWriteLock) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) RequestVoteRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.RequestVoteRequest) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) HashSet(java.util.HashSet) RepeatedTimer(org.apache.ignite.raft.jraft.util.RepeatedTimer) RequestVoteResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.RequestVoteResponse) JRaftServiceFactory(org.apache.ignite.raft.jraft.JRaftServiceFactory) Describer(org.apache.ignite.raft.jraft.util.Describer) RaftClientService(org.apache.ignite.raft.jraft.rpc.RaftClientService) ClosureQueueImpl(org.apache.ignite.raft.jraft.closure.ClosureQueueImpl) JRaftUtils(org.apache.ignite.raft.jraft.JRaftUtils) RingBuffer(com.lmax.disruptor.RingBuffer) FSMCallerOptions(org.apache.ignite.raft.jraft.option.FSMCallerOptions) RaftMetaStorage(org.apache.ignite.raft.jraft.storage.RaftMetaStorage) ThreadHelper(org.apache.ignite.raft.jraft.util.ThreadHelper) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Peer(org.apache.ignite.raft.client.Peer) Ballot(org.apache.ignite.raft.jraft.entity.Ballot) InstallSnapshotRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotRequest) EventTranslator(com.lmax.disruptor.EventTranslator) UserLog(org.apache.ignite.raft.jraft.entity.UserLog) Message(org.apache.ignite.raft.jraft.rpc.Message) ThreadId(org.apache.ignite.raft.jraft.util.ThreadId) ReplicatorGroup(org.apache.ignite.raft.jraft.ReplicatorGroup) ByteString(org.apache.ignite.raft.jraft.util.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Node (org.apache.ignite.raft.jraft.Node)84 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)75 Test (org.junit.jupiter.api.Test)66 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)38 CountDownLatch (java.util.concurrent.CountDownLatch)30 ArrayList (java.util.ArrayList)26 Status (org.apache.ignite.raft.jraft.Status)23 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)23 Task (org.apache.ignite.raft.jraft.entity.Task)18 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)17 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)16 ByteBuffer (java.nio.ByteBuffer)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)12 RaftException (org.apache.ignite.raft.jraft.error.RaftException)11 LinkedHashSet (java.util.LinkedHashSet)10 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)10 List (java.util.List)9 LogIndexOutOfBoundsException (org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException)9 LogNotFoundException (org.apache.ignite.raft.jraft.error.LogNotFoundException)9