Search in sources :

Example 11 with NodeOptions

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));
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) ExecutorService(java.util.concurrent.ExecutorService) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with NodeOptions

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);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 13 with 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());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) BootstrapOptions(org.apache.ignite.raft.jraft.option.BootstrapOptions) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 14 with NodeOptions

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;
}
Also used : SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) LogId(org.apache.ignite.raft.jraft.entity.LogId) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 15 with NodeOptions

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;
}
Also used : NodeImpl(org.apache.ignite.raft.jraft.core.NodeImpl) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)34 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)17 Test (org.junit.jupiter.api.Test)14 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)13 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 Node (org.apache.ignite.raft.jraft.Node)10 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)8 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)8 ExecutorService (java.util.concurrent.ExecutorService)5 NodeId (org.apache.ignite.raft.jraft.entity.NodeId)4 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClusterService (org.apache.ignite.network.ClusterService)3 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)3 BootstrapOptions (org.apache.ignite.raft.jraft.option.BootstrapOptions)3 File (java.io.File)2 Path (java.nio.file.Path)2