use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class NodeTest method testSingleNodeWithLearner.
@Test
public void testSingleNodeWithLearner() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
final PeerId peer = new PeerId(addr, 0);
final Endpoint learnerAddr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT + 1);
final PeerId learnerPeer = new PeerId(learnerAddr, 0);
NodeManager.getInstance().addAddress(addr);
NodeManager.getInstance().addAddress(learnerAddr);
MockStateMachine learnerFsm = null;
Node learner = null;
RaftGroupService learnerServer = null;
{
// Start learner
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
learnerFsm = new MockStateMachine(learnerAddr);
nodeOptions.setFsm(learnerFsm);
nodeOptions.setLogUri(this.dataPath + File.separator + "log1");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta1");
nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot1");
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(learnerAddr);
learnerServer = new RaftGroupService("unittest", new PeerId(learnerAddr, 0), nodeOptions, rpcServer);
learner = learnerServer.start();
}
{
// Start leader
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
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), Collections.singletonList(learnerPeer)));
final Node node = new NodeImpl("unittest", peer);
assertTrue(node.init(nodeOptions));
assertEquals(1, node.listPeers().size());
assertTrue(node.listPeers().contains(peer));
while (!node.isLeader()) {
;
}
sendTestTaskAndWait(node);
assertEquals(10, fsm.getLogs().size());
int i = 0;
for (final ByteBuffer data : fsm.getLogs()) {
assertEquals("hello" + i++, new String(data.array()));
}
// wait for entries to be replicated to learner.
Thread.sleep(1000);
node.shutdown();
node.join();
}
{
// assert learner fsm
assertEquals(10, learnerFsm.getLogs().size());
int i = 0;
for (final ByteBuffer data : learnerFsm.getLogs()) {
assertEquals("hello" + i++, new String(data.array()));
}
learnerServer.shutdown();
learnerServer.join();
}
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class AtomicRangeGroup method start.
public static AtomicRangeGroup start(StartupConf conf, RpcServer rpcServer) throws IOException {
final NodeOptions nodeOptions = new NodeOptions();
// Set election timeout to 1 second
nodeOptions.setElectionTimeoutMs(1000);
// Close cli service
nodeOptions.setDisableCli(false);
// A snapshot saving would be triggered every 30 seconds
// nodeOptions.setSnapshotIntervalSecs(30);
// Parsing Options
final PeerId serverId = new PeerId();
if (!serverId.parse(conf.getServerAddress())) {
throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
}
final Configuration initConf = new Configuration();
if (!initConf.parse(conf.getConf())) {
throw new IllegalArgumentException("Fail to parse initConf:" + conf.getConf());
}
// Set the initial cluster configuration
nodeOptions.setInitialConf(initConf);
// Startup node
final AtomicRangeGroup node = new AtomicRangeGroup(conf.getDataPath(), conf.getGroupId(), serverId, conf.getMinSlot(), conf.getMaxSlot(), nodeOptions, rpcServer);
LOG.info("Started range node[{}-{}] at port:{}", conf.getMinSlot(), conf.getMaxSlot(), node.getNode().getNodeId().getPeerId().getPort());
return node;
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
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;
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class PriorityElectionNode method init.
@Override
public boolean init(final PriorityElectionNodeOptions opts) {
if (this.started) {
LOG.info("[PriorityElectionNode: {}] already started.", opts.getServerAddress());
return true;
}
// node options
NodeOptions nodeOpts = opts.getNodeOptions();
if (nodeOpts == null) {
nodeOpts = new NodeOptions();
}
this.fsm = new PriorityElectionOnlyStateMachine(this.listeners);
// Set the initial PriorityElectionOnlyStateMachine
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());
}
/**
* Set priority value, required for priority-based election, it must be a positive value when
* enable the feature, some special value meaning:
* <ul>
* <li>-1 : disable priority-based election.</li>
* <li>0: will never participate in election.</li>
* <li>1: minimum value</li>
* </ul>
* value.
*/
nodeOpts.setElectionPriority(serverId.getPriority());
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;
}
use of com.alipay.sofa.jraft.option.NodeOptions in project jdchain-core by blockchain-jd-com.
the class RaftNodeServer method initNodeOptions.
private NodeOptions initNodeOptions(RaftServerSettings config) {
NodeOptions options = new NodeOptions();
options.setElectionTimeoutMs(config.getElectionTimeoutMs());
options.setSnapshotIntervalSecs(config.getSnapshotIntervalSec());
mkdirRaftDirs(serverSettings.getExtraProperties().getProperty(RAFT_PATH_KEY), options);
options.setSharedElectionTimer(true);
options.setSharedVoteTimer(true);
options.setSharedStepDownTimer(true);
options.setSharedSnapshotTimer(true);
options.setRaftOptions(RaftConfig.buildRaftOptions(config.getRaftSettings()));
return options;
}
Aggregations