Search in sources :

Example 51 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project RICE by gaojiayi.

the class ElectionNode method init.

@Override
public boolean init(final ElectionNodeOptions opts) {
    if (this.started) {
        LOG.info("[ElectionNode: {}] already started.", opts.getServerAddress());
        return true;
    }
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    this.fsm = new ElectionOnlyStateMachine(this.listeners);
    nodeOpts.setFsm(this.fsm);
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerAddressList())) {
        throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
    }
    // Set the initial cluster configuration
    nodeOpts.setInitialConf(initialConf);
    final String dataPath = opts.getDataPath();
    try {
        FileUtils.forceMkdir(new File(dataPath));
    } catch (final IOException e) {
        LOG.error("Fail to make dir for dataPath {}.", dataPath);
        return false;
    }
    // Set the data path
    // Log, required
    nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
    // Metadata, required
    nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
    // nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
    final String groupId = opts.getGroupId();
    final PeerId serverId = new PeerId();
    if (!serverId.parse(opts.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
    }
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
    this.node = this.raftGroupService.start();
    if (this.node != null) {
        this.started = true;
    }
    return this.started;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 52 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project nacos by alibaba.

the class JRaftServerTest method initPeersAndConfiguration.

private void initPeersAndConfiguration() {
    peerId1 = new PeerId("11.11.11.11", 7848);
    peerId2 = new PeerId("22.22.22.22", 7848);
    peerId3 = new PeerId("33.33.33.33", 7848);
    this.conf = new Configuration();
    conf.addPeer(peerId1);
    conf.addPeer(peerId2);
    conf.addPeer(peerId3);
    RouteTable.getInstance().updateConfiguration(groupId, conf);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 53 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project nacos by alibaba.

the class JRaftServer method refreshRouteTable.

void refreshRouteTable(String group) {
    if (isShutdown) {
        return;
    }
    final String groupName = group;
    Status status = null;
    try {
        RouteTable instance = RouteTable.getInstance();
        Configuration oldConf = instance.getConfiguration(groupName);
        String oldLeader = Optional.ofNullable(instance.selectLeader(groupName)).orElse(PeerId.emptyPeer()).getEndpoint().toString();
        // fix issue #3661  https://github.com/alibaba/nacos/issues/3661
        status = instance.refreshLeader(this.cliClientService, groupName, rpcRequestTimeoutMs);
        if (!status.isOk()) {
            Loggers.RAFT.error("Fail to refresh leader for group : {}, status is : {}", groupName, status);
        }
        status = instance.refreshConfiguration(this.cliClientService, groupName, rpcRequestTimeoutMs);
        if (!status.isOk()) {
            Loggers.RAFT.error("Fail to refresh route configuration for group : {}, status is : {}", groupName, status);
        }
    } catch (Exception e) {
        Loggers.RAFT.error("Fail to refresh raft metadata info for group : {}, error is : {}", groupName, e);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) JRaftException(com.alibaba.nacos.core.distributed.raft.exception.JRaftException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DuplicateRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException) NoLeaderException(com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)

Example 54 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project nacos by alibaba.

the class JRaftServer method createMultiRaftGroup.

synchronized void createMultiRaftGroup(Collection<RequestProcessor4CP> processors) {
    // There is no reason why the LogProcessor cannot be processed because of the synchronization
    if (!this.isStarted) {
        this.processors.addAll(processors);
        return;
    }
    final String parentPath = Paths.get(EnvUtil.getNacosHome(), "data/protocol/raft").toString();
    for (RequestProcessor4CP processor : processors) {
        final String groupName = processor.group();
        if (multiRaftGroup.containsKey(groupName)) {
            throw new DuplicateRaftGroupException(groupName);
        }
        // Ensure that each Raft Group has its own configuration and NodeOptions
        Configuration configuration = conf.copy();
        NodeOptions copy = nodeOptions.copy();
        JRaftUtils.initDirectory(parentPath, groupName, copy);
        // Here, the LogProcessor is passed into StateMachine, and when the StateMachine
        // triggers onApply, the onApply of the LogProcessor is actually called
        NacosStateMachine machine = new NacosStateMachine(this, processor);
        copy.setFsm(machine);
        copy.setInitialConf(configuration);
        // Set snapshot interval, default 1800 seconds
        int doSnapshotInterval = ConvertUtils.toInt(raftConfig.getVal(RaftSysConstants.RAFT_SNAPSHOT_INTERVAL_SECS), RaftSysConstants.DEFAULT_RAFT_SNAPSHOT_INTERVAL_SECS);
        // If the business module does not implement a snapshot processor, cancel the snapshot
        doSnapshotInterval = CollectionUtils.isEmpty(processor.loadSnapshotOperate()) ? 0 : doSnapshotInterval;
        copy.setSnapshotIntervalSecs(doSnapshotInterval);
        Loggers.RAFT.info("create raft group : {}", groupName);
        RaftGroupService raftGroupService = new RaftGroupService(groupName, localPeerId, copy, rpcServer, true);
        // Because BaseRpcServer has been started before, it is not allowed to start again here
        Node node = raftGroupService.start(false);
        machine.setNode(node);
        RouteTable.getInstance().updateConfiguration(groupName, configuration);
        RaftExecutor.executeByCommon(() -> registerSelfToCluster(groupName, localPeerId, configuration));
        // Turn on the leader auto refresh for this group
        Random random = new Random();
        long period = nodeOptions.getElectionTimeoutMs() + random.nextInt(5 * 1000);
        RaftExecutor.scheduleRaftMemberRefreshJob(() -> refreshRouteTable(groupName), nodeOptions.getElectionTimeoutMs(), period, TimeUnit.MILLISECONDS);
        multiRaftGroup.put(groupName, new RaftGroupTuple(node, processor, raftGroupService, machine));
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) Random(java.util.Random) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) DuplicateRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 55 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class AbstractRheaKVStoreTest method rangeSplitTest.

@Test
public void rangeSplitTest() {
    final RheaKVStore store = getRandomLeaderStore();
    final long regionId = 1;
    for (int i = 0; i < 20; i++) {
        store.bPut("a" + i, BytesUtil.writeUtf8("split"));
    }
    final CliOptions opts = new CliOptions();
    opts.setTimeoutMs(30000);
    final RheaKVCliService cliService = RheaKVServiceFactory.createAndInitRheaKVCliService(opts);
    final long newRegionId = 101;
    final String groupId = JRaftHelper.getJRaftGroupId("rhea_test", regionId);
    final Configuration conf = JRaftUtils.getConfiguration("127.0.0.1:18181,127.0.0.1:18182,127.0.0.1:18183");
    final Status st = cliService.rangeSplit(regionId, newRegionId, groupId, conf);
    System.err.println("Status:" + st);
    assertTrue(st.isOk());
    final RheaKVStore newStore = getLeaderStore(101);
    newStore.bPut("f_first_key", BytesUtil.writeUtf8("split_ok"));
    assertArrayEquals(BytesUtil.writeUtf8("split_ok"), newStore.bGet("f_first_key"));
}
Also used : Status(com.alipay.sofa.jraft.Status) RheaKVStore(com.alipay.sofa.jraft.rhea.client.RheaKVStore) RheaKVCliService(com.alipay.sofa.jraft.rhea.client.RheaKVCliService) Configuration(com.alipay.sofa.jraft.conf.Configuration) CliOptions(com.alipay.sofa.jraft.option.CliOptions) Test(org.junit.Test)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3