Search in sources :

Example 46 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project jdchain-core by blockchain-jd-com.

the class RaftMessageService method queryPeersManagerInfo.

private AsyncFuture<byte[]> queryPeersManagerInfo() {
    Configuration configuration = RouteTable.getInstance().getConfiguration(this.groupId);
    List<PeerId> peerIds = configuration.listPeers();
    return CompletableAsyncFuture.callAsync((Callable<byte[]>) () -> {
        try {
            List<MonitorNodeNetwork> monitorNodeNetworkList = queryPeersManagerInfo(peerIds);
            MonitorNodeNetwork[] monitorNodeNetworks = monitorNodeNetworkList.toArray(new MonitorNodeNetwork[] {});
            MonitorNodeNetworkAddresses addresses = new MonitorNodeNetworkAddresses(monitorNodeNetworks);
            return BinaryProtocol.encode(addresses, NodeNetworkAddresses.class);
        } catch (Exception e) {
            LOGGER.error("refreshAndQueryPeersManagerInfo error", e);
        }
        return null;
    }, monitorExecutor);
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) ProtocolStringList(com.google.protobuf.ProtocolStringList) ArrayList(java.util.ArrayList) List(java.util.List) RemotingException(com.alipay.sofa.jraft.error.RemotingException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 47 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project jdchain-core by blockchain-jd-com.

the class RaftNodeServerServiceImpl method transferParticipantNode.

@Override
public void transferParticipantNode(ParticipantNodeTransferRequest request, Closure done) {
    applyRequest(request, (RpcResponseClosure) done, (req, closure) -> {
        PeerId removePeer = PeerId.parsePeer(String.format(PEER_FORMAT, request.getPreHost(), request.getPrePort()));
        PeerId addPeer = PeerId.parsePeer(String.format(PEER_FORMAT, request.getNewHost(), request.getNewPort()));
        List<PeerId> peerIds = nodeServer.getNode().listPeers();
        peerIds.remove(removePeer);
        peerIds.add(addPeer);
        nodeServer.getNode().changePeers(new Configuration(peerIds), new ParticipantResponseClosure(closure));
    });
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 48 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project incubator-hugegraph by apache.

the class RaftSharedContext method nodeOptions.

public NodeOptions nodeOptions() throws IOException {
    HugeConfig config = this.config();
    PeerId selfId = new PeerId();
    selfId.parse(config.get(CoreOptions.RAFT_ENDPOINT));
    NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setEnableMetrics(false);
    nodeOptions.setRpcProcessorThreadPoolSize(config.get(CoreOptions.RAFT_RPC_THREADS));
    nodeOptions.setRpcConnectTimeoutMs(config.get(CoreOptions.RAFT_RPC_CONNECT_TIMEOUT));
    nodeOptions.setRpcDefaultTimeout(config.get(CoreOptions.RAFT_RPC_TIMEOUT));
    int electionTimeout = config.get(CoreOptions.RAFT_ELECTION_TIMEOUT);
    nodeOptions.setElectionTimeoutMs(electionTimeout);
    nodeOptions.setDisableCli(false);
    int snapshotInterval = config.get(CoreOptions.RAFT_SNAPSHOT_INTERVAL);
    nodeOptions.setSnapshotIntervalSecs(snapshotInterval);
    Configuration groupPeers = new Configuration();
    String groupPeersStr = config.get(CoreOptions.RAFT_GROUP_PEERS);
    if (!groupPeers.parse(groupPeersStr)) {
        throw new HugeException("Failed to parse group peers %s", groupPeersStr);
    }
    nodeOptions.setInitialConf(groupPeers);
    String raftPath = config.get(CoreOptions.RAFT_PATH);
    String logUri = Paths.get(raftPath, "log").toString();
    FileUtils.forceMkdir(new File(logUri));
    nodeOptions.setLogUri(logUri);
    String metaUri = Paths.get(raftPath, "meta").toString();
    FileUtils.forceMkdir(new File(metaUri));
    nodeOptions.setRaftMetaUri(metaUri);
    if (config.get(CoreOptions.RAFT_USE_SNAPSHOT)) {
        String snapshotUri = Paths.get(raftPath, "snapshot").toString();
        FileUtils.forceMkdir(new File(snapshotUri));
        nodeOptions.setSnapshotUri(snapshotUri);
    }
    RaftOptions raftOptions = nodeOptions.getRaftOptions();
    /*
         * NOTE: if buffer size is too small(<=1024), will throw exception
         * "LogManager is busy, disk queue overload"
         */
    raftOptions.setApplyBatch(config.get(CoreOptions.RAFT_APPLY_BATCH));
    raftOptions.setDisruptorBufferSize(config.get(CoreOptions.RAFT_QUEUE_SIZE));
    raftOptions.setDisruptorPublishEventWaitTimeoutSecs(config.get(CoreOptions.RAFT_QUEUE_PUBLISH_TIMEOUT));
    raftOptions.setReplicatorPipeline(config.get(CoreOptions.RAFT_REPLICATOR_PIPELINE));
    raftOptions.setOpenStatistics(false);
    raftOptions.setReadOnlyOptions(ReadOnlyOption.valueOf(config.get(CoreOptions.RAFT_READ_STRATEGY)));
    return nodeOptions;
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Configuration(com.alipay.sofa.jraft.conf.Configuration) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) HugeConfig(com.baidu.hugegraph.config.HugeConfig) HugeException(com.baidu.hugegraph.HugeException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 49 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project mmqtt by MrHKing.

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.getMmqHome(), "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
        MmqStateMachine machine = new MmqStateMachine(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) RequestProcessor4CP(org.monkey.mmq.core.consistency.cp.RequestProcessor4CP) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 50 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project mmqtt by MrHKing.

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();
        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 : Configuration(com.alipay.sofa.jraft.conf.Configuration)

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