use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.
the class ReadOnlyServiceTest method setup.
@BeforeEach
public void setup() {
this.readOnlyServiceImpl = new ReadOnlyServiceImpl();
RaftOptions raftOptions = new RaftOptions();
this.msgFactory = raftOptions.getRaftMessagesFactory();
final ReadOnlyServiceOptions opts = new ReadOnlyServiceOptions();
opts.setFsmCaller(this.fsmCaller);
opts.setNode(this.node);
opts.setRaftOptions(raftOptions);
opts.setGroupId("TestSrv");
opts.setReadOnlyServiceDisruptor(disruptor = new StripedDisruptor<>("TestReadOnlyServiceDisruptor", 1024, () -> new ReadOnlyServiceImpl.ReadIndexEvent(), 1));
NodeOptions nodeOptions = new NodeOptions();
ExecutorService executor = JRaftUtils.createExecutor("test-executor", Utils.cpus());
executors.add(executor);
nodeOptions.setCommonExecutor(executor);
ExecutorService clientExecutor = JRaftUtils.createClientExecutor(nodeOptions, "unittest");
executors.add(clientExecutor);
nodeOptions.setClientExecutor(clientExecutor);
Scheduler scheduler = JRaftUtils.createScheduler(nodeOptions);
this.scheduler = scheduler;
nodeOptions.setScheduler(scheduler);
Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
Mockito.when(this.node.getGroupId()).thenReturn("test");
Mockito.when(this.node.getOptions()).thenReturn(nodeOptions);
Mockito.when(this.node.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost:8081", 0)));
Mockito.when(this.node.getServerId()).thenReturn(new PeerId("localhost:8081", 0));
assertTrue(this.readOnlyServiceImpl.init(opts));
}
use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.
the class IteratorImplTest method setup.
@BeforeEach
public void setup() {
this.applyingIndex = new AtomicLong(0);
this.closures = new ArrayList<>();
for (int i = 0; i < 11; i++) {
this.closures.add(new MockClosure());
final LogEntry log = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
log.getId().setIndex(i);
log.getId().setTerm(1);
Mockito.when(this.logManager.getEntry(i)).thenReturn(log);
}
NodeOptions nodeOptions = new NodeOptions();
executor = JRaftUtils.createExecutor("test-executor", Utils.cpus());
nodeOptions.setCommonExecutor(executor);
this.iter = new IteratorImpl(fsm, logManager, closures, 0L, 0L, 10L, applyingIndex, nodeOptions);
}
use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.
the class ItNodeTest method testBootStrapWithoutSnapshot.
@Test
public void testBootStrapWithoutSnapshot() throws Exception {
Endpoint addr = new Endpoint("127.0.0.1", 5006);
MockStateMachine fsm = new MockStateMachine(addr);
BootstrapOptions opts = new BootstrapOptions();
opts.setServiceFactory(new DefaultJRaftServiceFactory());
opts.setLastLogIndex(0);
opts.setRaftMetaUri(dataPath + File.separator + "meta");
opts.setLogUri(dataPath + File.separator + "log");
opts.setSnapshotUri(dataPath + File.separator + "snapshot");
opts.setGroupConf(JRaftUtils.getConfiguration("127.0.0.1:5006"));
opts.setFsm(fsm);
NodeOptions nodeOpts = createNodeOptions();
opts.setNodeOptions(nodeOpts);
assertTrue(JRaftUtils.bootstrap(opts));
nodeOpts.setRaftMetaUri(dataPath + File.separator + "meta");
nodeOpts.setLogUri(dataPath + File.separator + "log");
nodeOpts.setSnapshotUri(dataPath + File.separator + "snapshot");
nodeOpts.setFsm(fsm);
RaftGroupService service = createService("test", new PeerId(addr, 0), nodeOpts);
Node node = service.start();
while (!node.isLeader()) Thread.sleep(20);
sendTestTaskAndWait(node);
assertEquals(10, fsm.getLogs().size());
}
use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.
the class NodeImpl method bootstrap.
public boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
if (opts.getLastLogIndex() > 0 && (opts.getGroupConf().isEmpty() || opts.getFsm() == null)) {
LOG.error("Invalid arguments for bootstrap, groupConf={}, fsm={}, lastLogIndex={}.", opts.getGroupConf(), opts.getFsm(), opts.getLastLogIndex());
return false;
}
if (opts.getGroupConf().isEmpty()) {
LOG.error("Bootstrapping an empty node makes no sense.");
return false;
}
Requires.requireNonNull(opts.getServiceFactory(), "Null jraft service factory");
this.serviceFactory = opts.getServiceFactory();
// Term is not an option since changing it is very dangerous
final long bootstrapLogTerm = opts.getLastLogIndex() > 0 ? 1 : 0;
final LogId bootstrapId = new LogId(opts.getLastLogIndex(), bootstrapLogTerm);
this.options = opts.getNodeOptions() == null ? new NodeOptions() : opts.getNodeOptions();
this.raftOptions = this.options.getRaftOptions();
this.metrics = new NodeMetrics(opts.isEnableMetrics());
this.options.setFsm(opts.getFsm());
this.options.setLogUri(opts.getLogUri());
this.options.setRaftMetaUri(opts.getRaftMetaUri());
this.options.setSnapshotUri(opts.getSnapshotUri());
this.configManager = new ConfigurationManager();
// Create fsmCaller at first as logManager needs it to report error
this.fsmCaller = new FSMCallerImpl();
initPools(opts.getNodeOptions());
if (!initLogStorage()) {
LOG.error("Fail to init log storage.");
return false;
}
if (!initMetaStorage()) {
LOG.error("Fail to init meta storage.");
return false;
}
if (this.currTerm == 0) {
this.currTerm = 1;
if (!this.metaStorage.setTermAndVotedFor(1, new PeerId())) {
LOG.error("Fail to set term.");
return false;
}
}
if (opts.getFsm() != null && !initFSMCaller(bootstrapId)) {
LOG.error("Fail to init fsm caller.");
return false;
}
final LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
entry.getId().setTerm(this.currTerm);
entry.setPeers(opts.getGroupConf().listPeers());
entry.setLearners(opts.getGroupConf().listLearners());
final List<LogEntry> entries = new ArrayList<>();
entries.add(entry);
final BootstrapStableClosure bootstrapDone = new BootstrapStableClosure();
this.logManager.appendEntries(entries, bootstrapDone);
if (!bootstrapDone.await().isOk()) {
LOG.error("Fail to append configuration.");
return false;
}
if (opts.getLastLogIndex() > 0) {
if (!initSnapshotStorage()) {
LOG.error("Fail to init snapshot storage.");
return false;
}
final SynchronizedClosure snapshotDone = new SynchronizedClosure();
this.snapshotExecutor.doSnapshot(snapshotDone);
if (!snapshotDone.await().isOk()) {
LOG.error("Fail to save snapshot, status={}.", snapshotDone.getStatus());
return false;
}
}
if (this.logManager.getFirstLogIndex() != opts.getLastLogIndex() + 1) {
throw new IllegalStateException("First and last log index mismatch");
}
if (opts.getLastLogIndex() > 0) {
if (this.logManager.getLastLogIndex() != opts.getLastLogIndex()) {
throw new IllegalStateException("Last log index mismatch");
}
} else {
if (this.logManager.getLastLogIndex() != opts.getLastLogIndex() + 1) {
throw new IllegalStateException("Last log index mismatch");
}
}
return true;
}
use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.
the class JRaftUtils method bootstrap.
/**
* Bootstrap a non-empty raft node.
*
* @param opts options of bootstrap
* @return true if bootstrap success
*/
public static boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
final NodeImpl node = new NodeImpl("bootstrap", new PeerId("127.0.0.1", 0));
NodeOptions nodeOpts = opts.getNodeOptions();
nodeOpts.setStripes(1);
final boolean ret = node.bootstrap(opts);
node.shutdown();
node.join();
return ret;
}
Aggregations