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);
}
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));
});
}
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;
}
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));
}
}
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);
}
}
Aggregations