Search in sources :

Example 11 with Status

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

the class CliServiceImpl method resetLearners.

@Override
public Status resetLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
    checkLearnersOpParams(groupId, conf, learners);
    final PeerId leaderId = new PeerId();
    final Status st = getLeader(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    if (!this.cliClientService.connect(leaderId.getEndpoint())) {
        return new Status(-1, "Fail to init channel to leader %s", leaderId);
    }
    ResetLearnersRequest req = cliOptions.getRaftMessagesFactory().resetLearnersRequest().groupId(groupId).leaderId(leaderId.toString()).learnersList(learners.stream().map(Object::toString).collect(toList())).build();
    try {
        final Message result = this.cliClientService.resetLearners(leaderId.getEndpoint(), req, null).get();
        return processLearnersOpResponse(groupId, result, "resetting learners: %s", learners);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) ResetLearnersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.ResetLearnersRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 12 with Status

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

the class FSMCallerImpl method doSnapshotLoad.

private void doSnapshotLoad(final LoadSnapshotClosure done) {
    Requires.requireNonNull(done, "LoadSnapshotClosure is null");
    final SnapshotReader reader = done.start();
    if (reader == null) {
        done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
        return;
    }
    final RaftOutter.SnapshotMeta meta = reader.load();
    if (meta == null) {
        done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
        if (reader.getRaftError() == RaftError.EIO) {
            final RaftException err = new RaftException(ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO, "Fail to load snapshot meta");
            setError(err);
        }
        return;
    }
    final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
    final LogId snapshotId = new LogId(meta.lastIncludedIndex(), meta.lastIncludedTerm());
    if (lastAppliedId.compareTo(snapshotId) > 0) {
        done.run(new Status(RaftError.ESTALE, "Loading a stale snapshot last_applied_index=%d last_applied_term=%d snapshot_index=%d snapshot_term=%d", lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
        return;
    }
    if (!this.fsm.onSnapshotLoad(reader)) {
        done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
        final RaftException e = new RaftException(ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
        setError(e);
        return;
    }
    if (meta.oldPeersList() == null) {
        // Joint stage is not supposed to be noticeable by end users.
        final Configuration conf = new Configuration();
        if (meta.peersList() != null) {
            for (String metaPeer : meta.peersList()) {
                final PeerId peer = new PeerId();
                Requires.requireTrue(peer.parse(metaPeer), "Parse peer failed");
                conf.addPeer(peer);
            }
        }
        this.fsm.onConfigurationCommitted(conf);
    }
    this.lastAppliedIndex.set(meta.lastIncludedIndex());
    this.lastAppliedTerm = meta.lastIncludedTerm();
    done.run(Status.OK());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) RaftException(org.apache.ignite.raft.jraft.error.RaftException) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) LogId(org.apache.ignite.raft.jraft.entity.LogId) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 13 with Status

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

the class FSMCallerImpl method doSnapshotSave.

private void doSnapshotSave(final SaveSnapshotClosure done) {
    Requires.requireNonNull(done, "SaveSnapshotClosure is null");
    final long lastAppliedIndex = this.lastAppliedIndex.get();
    final ConfigurationEntry confEntry = this.logManager.getConfiguration(lastAppliedIndex);
    if (confEntry == null || confEntry.isEmpty()) {
        LOG.error("Empty conf entry for lastAppliedIndex={}", lastAppliedIndex);
        Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), done, new Status(RaftError.EINVAL, "Empty conf entry for lastAppliedIndex=%s", lastAppliedIndex));
        return;
    }
    SnapshotMetaBuilder metaBuilder = msgFactory.snapshotMeta().lastIncludedIndex(lastAppliedIndex).lastIncludedTerm(this.lastAppliedTerm).peersList(confEntry.getConf().getPeers().stream().map(Object::toString).collect(toList())).learnersList(confEntry.getConf().getLearners().stream().map(Object::toString).collect(toList()));
    if (confEntry.getOldConf() != null) {
        metaBuilder.oldPeersList(confEntry.getOldConf().getPeers().stream().map(Object::toString).collect(toList())).oldLearnersList(confEntry.getOldConf().getLearners().stream().map(Object::toString).collect(toList()));
    }
    final SnapshotWriter writer = done.start(metaBuilder.build());
    if (writer == null) {
        done.run(new Status(RaftError.EINVAL, "snapshot_storage create SnapshotWriter failed"));
        return;
    }
    this.fsm.onSnapshotSave(writer, done);
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SnapshotWriter(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter) SnapshotMetaBuilder(org.apache.ignite.raft.jraft.entity.SnapshotMetaBuilder) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry)

Example 14 with Status

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

the class ItNodeTest method testChangePeersChaosWithSnapshot.

@Test
public void testChangePeersChaosWithSnapshot() throws Exception {
    // start cluster
    List<PeerId> peers = new ArrayList<>();
    peers.add(new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT));
    cluster = new TestCluster("unittest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
    assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 2));
    // start other peers
    for (int i = 1; i < 10; i++) {
        PeerId peer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + i);
        peers.add(peer);
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    ChangeArg arg = new ChangeArg(cluster, peers, false, false);
    Future<?> future = startChangePeersThread(arg);
    for (int i = 0; i < 5000; ) {
        cluster.waitLeader();
        Node leader = cluster.getLeader();
        if (leader == null)
            continue;
        SynchronizedClosure done = new SynchronizedClosure();
        Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes(UTF_8)), done);
        leader.apply(task);
        Status status = done.await();
        if (status.isOk()) {
            if (++i % 100 == 0)
                System.out.println("Progress:" + i);
        } else
            assertEquals(RaftError.EPERM, status.getRaftError());
    }
    arg.stop = true;
    future.get();
    cluster.waitLeader();
    SynchronizedClosure done = new SynchronizedClosure();
    Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    Status st = done.await();
    assertTrue(st.isOk(), st.getErrorMsg());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) assertTrue(fsm.getLogs().size() >= 5000);
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) Task(org.apache.ignite.raft.jraft.entity.Task) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 15 with Status

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

the class ItNodeTest method assertReadIndex.

@SuppressWarnings({ "unused", "SameParameterValue" })
private boolean assertReadIndex(Node node, int index) throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    byte[] requestContext = TestUtils.getRandomBytes();
    AtomicBoolean success = new AtomicBoolean(false);
    node.readIndex(requestContext, new ReadIndexClosure() {

        @Override
        public void run(Status status, long theIndex, byte[] reqCtx) {
            if (status.isOk()) {
                assertEquals(index, theIndex);
                assertArrayEquals(requestContext, reqCtx);
                success.set(true);
            } else {
                assertTrue(status.getErrorMsg().contains("RPC exception:Check connection["), status.getErrorMsg());
                assertTrue(status.getErrorMsg().contains("] fail and try to create new one"), status.getErrorMsg());
            }
            latch.countDown();
        }
    });
    latch.await();
    return success.get();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Status (org.apache.ignite.raft.jraft.Status)121 Test (org.junit.jupiter.api.Test)49 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)43 CountDownLatch (java.util.concurrent.CountDownLatch)31 Message (org.apache.ignite.raft.jraft.rpc.Message)21 ArrayList (java.util.ArrayList)20 Node (org.apache.ignite.raft.jraft.Node)20 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)20 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)16 LogId (org.apache.ignite.raft.jraft.entity.LogId)14 RaftException (org.apache.ignite.raft.jraft.error.RaftException)14 LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)11 JRaftException (org.apache.ignite.raft.jraft.error.JRaftException)11 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)10 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)10 ByteBuffer (java.nio.ByteBuffer)9 List (java.util.List)9 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)9 Task (org.apache.ignite.raft.jraft.entity.Task)8 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)8