Search in sources :

Example 11 with Configuration

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

the class CliServiceImpl method removePeer.

@Override
public Status removePeer(final String groupId, final Configuration conf, final PeerId peer) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null configuration");
    Requires.requireNonNull(peer, "Null peer");
    Requires.requireTrue(!peer.isEmpty(), "Removing peer is blank");
    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);
    }
    RemovePeerRequest req = cliOptions.getRaftMessagesFactory().removePeerRequest().groupId(groupId).leaderId(leaderId.toString()).peerId(peer.toString()).build();
    try {
        final Message result = this.cliClientService.removePeer(leaderId.getEndpoint(), req, null).get();
        if (result instanceof RemovePeerResponse) {
            final RemovePeerResponse resp = (RemovePeerResponse) result;
            final Configuration oldConf = new Configuration();
            for (final String peerIdStr : resp.oldPeersList()) {
                final PeerId oldPeer = new PeerId();
                oldPeer.parse(peerIdStr);
                oldConf.addPeer(oldPeer);
            }
            final Configuration newConf = new Configuration();
            for (final String peerIdStr : resp.newPeersList()) {
                final PeerId newPeer = new PeerId();
                newPeer.parse(peerIdStr);
                newConf.addPeer(newPeer);
            }
            LOG.info("Configuration of replication group {} changed from {} to {}", groupId, oldConf, newConf);
            return Status.OK();
        } else {
            return statusFromResponse(result);
        }
    } 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) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RemovePeerResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerResponse) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) RemovePeerRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.RemovePeerRequest)

Example 12 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration 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 Configuration

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

the class ItNodeTest method testAutoSnapshot.

@Test
public void testAutoSnapshot() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    NodeOptions nodeOptions = createNodeOptions();
    MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOptions.setSnapshotIntervalSecs(10);
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
    RaftGroupService service = createService("unittest", new PeerId(addr, 0), nodeOptions);
    Node node = service.start();
    // wait node elect self as leader
    Thread.sleep(2000);
    sendTestTaskAndWait(node);
    // wait for auto snapshot
    Thread.sleep(10000);
    // first snapshot will be triggered randomly
    int times = fsm.getSaveSnapshotTimes();
    assertTrue(times >= 1, "snapshotTimes=" + times);
    assertTrue(fsm.getSnapshotIndex() > 0);
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 14 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration 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 Configuration

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

the class ItNodeTest method testSetPeer1.

@Test
public void testSetPeer1() throws Exception {
    cluster = new TestCluster("testSetPeer1", dataPath, new ArrayList<>(), testInfo);
    PeerId bootPeer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    assertTrue(cluster.start(bootPeer.getEndpoint()));
    List<Node> nodes = cluster.getFollowers();
    assertEquals(1, nodes.size());
    List<PeerId> peers = new ArrayList<>();
    peers.add(bootPeer);
    // reset peers from empty
    assertTrue(nodes.get(0).resetPeers(new Configuration(peers)).isOk());
    cluster.waitLeader();
    assertNotNull(cluster.getLeader());
}
Also used : 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)

Aggregations

Configuration (org.apache.ignite.raft.jraft.conf.Configuration)45 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)37 Test (org.junit.jupiter.api.Test)20 Node (org.apache.ignite.raft.jraft.Node)17 Status (org.apache.ignite.raft.jraft.Status)16 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)13 ArrayList (java.util.ArrayList)9 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)9 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)9 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)8 CountDownLatch (java.util.concurrent.CountDownLatch)5 ExecutorService (java.util.concurrent.ExecutorService)5 NetworkAddress (org.apache.ignite.network.NetworkAddress)5 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)5 LogId (org.apache.ignite.raft.jraft.entity.LogId)5 Task (org.apache.ignite.raft.jraft.entity.Task)5 IgniteRpcClient (org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient)5 HashMap (java.util.HashMap)4 ClusterService (org.apache.ignite.network.ClusterService)4 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)4