use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RouteTable method getConfiguration.
/**
* Get the configuration by groupId, returns null when not found.
*
* @param groupId raft group id
* @return configuration of the group id
*/
public Configuration getConfiguration(final String groupId) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
final GroupConf gc = this.groupConfTable.get(groupId);
if (gc == null) {
return null;
}
final StampedLock stampedLock = gc.stampedLock;
long stamp = stampedLock.tryOptimisticRead();
Configuration conf = gc.conf;
if (!stampedLock.validate(stamp)) {
stamp = stampedLock.readLock();
try {
conf = gc.conf;
} finally {
stampedLock.unlockRead(stamp);
}
}
return conf;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class StoreEngine method inConfiguration.
private boolean inConfiguration(final String curr, final String all) {
final PeerId currPeer = new PeerId();
if (!currPeer.parse(curr)) {
return false;
}
final Configuration allConf = new Configuration();
if (!allConf.parse(all)) {
return false;
}
return allConf.contains(currPeer) || allConf.getLearners().contains(currPeer);
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class RegionEngine method init.
@Override
public synchronized boolean init(final RegionEngineOptions opts) {
if (this.started) {
LOG.info("[RegionEngine: {}] already started.", this.region);
return true;
}
this.regionOpts = Requires.requireNonNull(opts, "opts");
this.fsm = new KVStoreStateMachine(this.region, this.storeEngine);
// node options
NodeOptions nodeOpts = opts.getNodeOptions();
if (nodeOpts == null) {
nodeOpts = new NodeOptions();
}
final long metricsReportPeriod = opts.getMetricsReportPeriod();
if (metricsReportPeriod > 0) {
// metricsReportPeriod > 0 means enable metrics
nodeOpts.setEnableMetrics(true);
}
final Configuration initialConf = new Configuration();
if (!initialConf.parse(opts.getInitialServerList())) {
LOG.error("Fail to parse initial configuration {}.", opts.getInitialServerList());
return false;
}
nodeOpts.setInitialConf(initialConf);
nodeOpts.setFsm(this.fsm);
final String raftDataPath = opts.getRaftDataPath();
try {
FileUtils.forceMkdir(new File(raftDataPath));
} catch (final Throwable t) {
LOG.error("Fail to make dir for raftDataPath {}.", raftDataPath);
return false;
}
if (Strings.isBlank(nodeOpts.getLogUri())) {
final Path logUri = Paths.get(raftDataPath, "log");
nodeOpts.setLogUri(logUri.toString());
}
if (Strings.isBlank(nodeOpts.getRaftMetaUri())) {
final Path meteUri = Paths.get(raftDataPath, "meta");
nodeOpts.setRaftMetaUri(meteUri.toString());
}
if (Strings.isBlank(nodeOpts.getSnapshotUri())) {
final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
nodeOpts.setSnapshotUri(snapshotUri.toString());
}
LOG.info("[RegionEngine: {}], log uri: {}, raft meta uri: {}, snapshot uri: {}.", this.region, nodeOpts.getLogUri(), nodeOpts.getRaftMetaUri(), nodeOpts.getSnapshotUri());
final Endpoint serverAddress = opts.getServerAddress();
final PeerId serverId = new PeerId(serverAddress, 0);
final RpcServer rpcServer = this.storeEngine.getRpcServer();
this.raftGroupService = new RaftGroupService(opts.getRaftGroupId(), serverId, nodeOpts, rpcServer, true);
this.node = this.raftGroupService.start(false);
RouteTable.getInstance().updateConfiguration(this.raftGroupService.getGroupId(), nodeOpts.getInitialConf());
if (this.node != null) {
final RawKVStore rawKVStore = this.storeEngine.getRawKVStore();
final Executor readIndexExecutor = this.storeEngine.getReadIndexExecutor();
this.raftRawKVStore = new RaftRawKVStore(this.node, rawKVStore, readIndexExecutor);
this.metricsRawKVStore = new MetricsRawKVStore(this.region.getId(), this.raftRawKVStore);
// metrics config
if (this.regionMetricsReporter == null && metricsReportPeriod > 0) {
final MetricRegistry metricRegistry = this.node.getNodeMetrics().getMetricRegistry();
if (metricRegistry != null) {
final ScheduledExecutorService scheduler = this.storeEngine.getMetricsScheduler();
// start raft node metrics reporter
this.regionMetricsReporter = //
Slf4jReporter.forRegistry(metricRegistry).prefixedWith(//
"region_" + this.region.getId()).withLoggingLevel(//
Slf4jReporter.LoggingLevel.INFO).outputTo(//
LOG).scheduleOn(//
scheduler).shutdownExecutorOnStop(//
scheduler != null).build();
this.regionMetricsReporter.start(metricsReportPeriod, TimeUnit.SECONDS);
}
}
this.started = true;
LOG.info("[RegionEngine] start successfully: {}.", this);
}
return this.started;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class TestCluster method start.
public boolean start(final Endpoint listenAddr, final boolean emptyPeers, final int snapshotIntervalSecs, final boolean enableMetrics, final SnapshotThrottle snapshotThrottle, final RaftOptions raftOptions) throws IOException {
if (this.serverMap.get(listenAddr.toString()) != null) {
return true;
}
final NodeOptions nodeOptions = new NodeOptions();
nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
nodeOptions.setEnableMetrics(enableMetrics);
nodeOptions.setSnapshotThrottle(snapshotThrottle);
nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
if (raftOptions != null) {
nodeOptions.setRaftOptions(raftOptions);
}
final String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
FileUtils.forceMkdir(new File(serverDataPath));
nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
final MockStateMachine fsm = new MockStateMachine(listenAddr);
nodeOptions.setFsm(fsm);
if (!emptyPeers) {
nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
}
final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(listenAddr);
final RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0), nodeOptions, rpcServer);
this.lock.lock();
try {
if (this.serverMap.put(listenAddr.toString(), server) == null) {
final Node node = server.start();
this.fsms.put(new PeerId(listenAddr, 0), fsm);
this.nodes.add((NodeImpl) node);
return true;
}
} finally {
this.lock.unlock();
}
return false;
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testNodeTaskOverload.
@Test
public void testNodeTaskOverload() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
final PeerId peer = new PeerId(addr, 0);
NodeManager.getInstance().addAddress(addr);
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
final RaftOptions raftOptions = new RaftOptions();
raftOptions.setDisruptorBufferSize(2);
nodeOptions.setRaftOptions(raftOptions);
final MockStateMachine fsm = new MockStateMachine(addr);
nodeOptions.setFsm(fsm);
nodeOptions.setLogUri(this.dataPath + File.separator + "log");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
final Node node = new NodeImpl("unittest", peer);
assertTrue(node.init(nodeOptions));
assertEquals(1, node.listPeers().size());
assertTrue(node.listPeers().contains(peer));
while (!node.isLeader()) {
;
}
final List<Task> tasks = new ArrayList<>();
final AtomicInteger c = new AtomicInteger(0);
for (int i = 0; i < 10; i++) {
final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
final Task task = new Task(data, new JoinableClosure(status -> {
System.out.println(status);
if (!status.isOk()) {
assertTrue(status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
}
c.incrementAndGet();
}));
node.apply(task);
tasks.add(task);
}
try {
Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
assertEquals(10, c.get());
} finally {
node.shutdown();
node.join();
}
}
Aggregations