Search in sources :

Example 1 with NodeId

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

the class NodeImpl method init.

@Override
public boolean init(final NodeOptions opts) {
    Requires.requireNonNull(opts, "Null node options");
    Requires.requireNonNull(opts.getRaftOptions(), "Null raft options");
    Requires.requireNonNull(opts.getServiceFactory(), "Null jraft service factory");
    this.serviceFactory = opts.getServiceFactory();
    this.options = opts;
    this.raftOptions = opts.getRaftOptions();
    this.metrics = new NodeMetrics(opts.isEnableMetrics());
    this.serverId.setPriority(opts.getElectionPriority());
    this.electionTimeoutCounter = 0;
    if (opts.getReplicationStateListeners() != null)
        this.replicatorStateListeners.addAll(opts.getReplicationStateListeners());
    if (this.serverId.getIp().equals(Utils.IP_ANY)) {
        LOG.error("Node can't start from IP_ANY.");
        return false;
    }
    // Init timers.
    initTimers(opts);
    // Init pools.
    initPools(opts);
    this.configManager = new ConfigurationManager();
    applyDisruptor = opts.getNodeApplyDisruptor();
    applyQueue = applyDisruptor.subscribe(groupId, new LogEntryAndClosureHandler());
    if (this.metrics.getMetricRegistry() != null) {
        this.metrics.getMetricRegistry().register("jraft-node-impl-disruptor", new DisruptorMetricSet(this.applyQueue));
    }
    this.fsmCaller = new FSMCallerImpl();
    if (!initLogStorage()) {
        LOG.error("Node {} initLogStorage failed.", getNodeId());
        return false;
    }
    if (!initMetaStorage()) {
        LOG.error("Node {} initMetaStorage failed.", getNodeId());
        return false;
    }
    if (!initFSMCaller(new LogId(0, 0))) {
        LOG.error("Node {} initFSMCaller failed.", getNodeId());
        return false;
    }
    this.ballotBox = new BallotBox();
    final BallotBoxOptions ballotBoxOpts = new BallotBoxOptions();
    ballotBoxOpts.setWaiter(this.fsmCaller);
    ballotBoxOpts.setClosureQueue(this.closureQueue);
    if (!this.ballotBox.init(ballotBoxOpts)) {
        LOG.error("Node {} init ballotBox failed.", getNodeId());
        return false;
    }
    if (!initSnapshotStorage()) {
        LOG.error("Node {} initSnapshotStorage failed.", getNodeId());
        return false;
    }
    final Status st = this.logManager.checkConsistency();
    if (!st.isOk()) {
        LOG.error("Node {} is initialized with inconsistent log, status={}.", getNodeId(), st);
        return false;
    }
    this.conf = new ConfigurationEntry();
    this.conf.setId(new LogId());
    // if have log using conf in log, else using conf in options
    if (this.logManager.getLastLogIndex() > 0) {
        checkAndSetConfiguration(false);
    } else {
        this.conf.setConf(this.options.getInitialConf());
        // initially set to max(priority of all nodes)
        this.targetPriority = getMaxPriorityOfNodes(this.conf.getConf().getPeers());
    }
    if (!this.conf.isEmpty()) {
        Requires.requireTrue(this.conf.isValid(), "Invalid conf: %s", this.conf);
    } else {
        LOG.info("Init node {} with empty conf.", this.serverId);
    }
    this.replicatorGroup = new ReplicatorGroupImpl();
    this.rpcClientService = new DefaultRaftClientService();
    final ReplicatorGroupOptions rgOpts = new ReplicatorGroupOptions();
    rgOpts.setHeartbeatTimeoutMs(heartbeatTimeout(this.options.getElectionTimeoutMs()));
    rgOpts.setElectionTimeoutMs(this.options.getElectionTimeoutMs());
    rgOpts.setLogManager(this.logManager);
    rgOpts.setBallotBox(this.ballotBox);
    rgOpts.setNode(this);
    rgOpts.setRaftRpcClientService(this.rpcClientService);
    rgOpts.setSnapshotStorage(this.snapshotExecutor != null ? this.snapshotExecutor.getSnapshotStorage() : null);
    rgOpts.setRaftOptions(this.raftOptions);
    rgOpts.setTimerManager(this.options.getScheduler());
    // Adds metric registry to RPC service.
    this.options.setMetricRegistry(this.metrics.getMetricRegistry());
    if (!this.rpcClientService.init(this.options)) {
        LOG.error("Fail to init rpc service.");
        return false;
    }
    this.replicatorGroup.init(new NodeId(this.groupId, this.serverId), rgOpts);
    this.readOnlyService = new ReadOnlyServiceImpl();
    final ReadOnlyServiceOptions rosOpts = new ReadOnlyServiceOptions();
    rosOpts.setGroupId(groupId);
    rosOpts.setFsmCaller(this.fsmCaller);
    rosOpts.setNode(this);
    rosOpts.setRaftOptions(this.raftOptions);
    rosOpts.setReadOnlyServiceDisruptor(opts.getReadOnlyServiceDisruptor());
    if (!this.readOnlyService.init(rosOpts)) {
        LOG.error("Fail to init readOnlyService.");
        return false;
    }
    // set state to follower
    this.state = State.STATE_FOLLOWER;
    if (LOG.isInfoEnabled()) {
        LOG.info("Node {} init, term={}, lastLogId={}, conf={}, oldConf={}.", getNodeId(), this.currTerm, this.logManager.getLastLogId(false), this.conf.getConf(), this.conf.getOldConf());
    }
    if (this.snapshotExecutor != null && this.options.getSnapshotIntervalSecs() > 0) {
        LOG.debug("Node {} start snapshot timer, term={}.", getNodeId(), this.currTerm);
        this.snapshotTimer.start();
    }
    if (!this.conf.isEmpty()) {
        stepDown(this.currTerm, false, new Status());
    }
    // Now the raft node is started , have to acquire the writeLock to avoid race
    // conditions
    this.writeLock.lock();
    if (this.conf.isStable() && this.conf.getConf().size() == 1 && this.conf.getConf().contains(this.serverId)) {
        // The group contains only this server which must be the LEADER, trigger
        // the timer immediately.
        electSelf();
    } else {
        this.writeLock.unlock();
    }
    return true;
}
Also used : Status(org.apache.ignite.raft.jraft.Status) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) ReplicatorGroupOptions(org.apache.ignite.raft.jraft.option.ReplicatorGroupOptions) BallotBoxOptions(org.apache.ignite.raft.jraft.option.BallotBoxOptions) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) DefaultRaftClientService(org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) LogId(org.apache.ignite.raft.jraft.entity.LogId) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry)

Example 2 with NodeId

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

the class BaseNodeRequestProcessorTest method mockNode.

protected PeerId mockNode() {
    Mockito.when(node.getGroupId()).thenReturn(this.groupId);
    final PeerId peerId = new PeerId();
    peerId.parse(this.peerIdStr);
    Mockito.when(node.getNodeId()).thenReturn(new NodeId(groupId, peerId));
    NodeOptions nodeOptions = new NodeOptions();
    executor = JRaftUtils.createCommonExecutor(nodeOptions);
    nodeOptions.setCommonExecutor(executor);
    appendEntriesExecutor = JRaftUtils.createAppendEntriesExecutor(nodeOptions);
    nodeOptions.setStripedExecutor(appendEntriesExecutor);
    Mockito.lenient().when(node.getOptions()).thenReturn(nodeOptions);
    if (asyncContext != null)
        asyncContext.getNodeManager().add(node);
    return peerId;
}
Also used : NodeId(org.apache.ignite.raft.jraft.entity.NodeId) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 3 with NodeId

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

the class BaseCliRequestProcessorTest method testManyNodes.

@Test
public void testManyNodes() {
    Node node1 = Mockito.mock(Node.class);
    Mockito.when(node1.getGroupId()).thenReturn("test");
    Mockito.when(node1.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
    this.asyncContext.getNodeManager().add(node1);
    Node node2 = Mockito.mock(Node.class);
    Mockito.when(node2.getGroupId()).thenReturn("test");
    Mockito.when(node2.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8082)));
    this.asyncContext.getNodeManager().add(node2);
    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.EINVAL.getNumber(), resp.errorCode());
    assertEquals("Peer must be specified since there're 2 nodes in group test", resp.errorMsg());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 4 with NodeId

use of org.apache.ignite.raft.jraft.entity.NodeId 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 5 with NodeId

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

the class NodeManager method add.

/**
 * Adds a node.
 */
public boolean add(final Node node) {
    final NodeId nodeId = node.getNodeId();
    if (this.nodeMap.putIfAbsent(nodeId, node) == null) {
        final String groupId = node.getGroupId();
        List<Node> nodes = this.groupMap.get(groupId);
        if (nodes == null) {
            nodes = new CopyOnWriteArrayList<>();
            List<Node> existsNode = this.groupMap.putIfAbsent(groupId, nodes);
            if (existsNode != null) {
                nodes = existsNode;
            }
        }
        nodes.add(node);
        return true;
    }
    return false;
}
Also used : NodeId(org.apache.ignite.raft.jraft.entity.NodeId)

Aggregations

NodeId (org.apache.ignite.raft.jraft.entity.NodeId)9 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)6 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)4 Node (org.apache.ignite.raft.jraft.Node)3 Test (org.junit.jupiter.api.Test)3 ReadOnlyServiceOptions (org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions)2 ReplicatorGroupOptions (org.apache.ignite.raft.jraft.option.ReplicatorGroupOptions)2 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ExecutorService (java.util.concurrent.ExecutorService)1 Closure (org.apache.ignite.raft.jraft.Closure)1 Status (org.apache.ignite.raft.jraft.Status)1 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)1 ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)1 StripedDisruptor (org.apache.ignite.raft.jraft.disruptor.StripedDisruptor)1 LogId (org.apache.ignite.raft.jraft.entity.LogId)1 BallotBoxOptions (org.apache.ignite.raft.jraft.option.BallotBoxOptions)1 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)1 RaftServerService (org.apache.ignite.raft.jraft.rpc.RaftServerService)1 DefaultRaftClientService (org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService)1