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());
}
}
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());
}
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);
}
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);
}
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());
}
Aggregations