use of com.alipay.sofa.jraft.option.NodeOptions 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.option.NodeOptions 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();
}
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class NodeTest method testSingleNode.
@Test
public void testSingleNode() 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 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()) {
;
}
sendTestTaskAndWait(node);
assertEquals(10, fsm.getLogs().size());
int i = 0;
for (final ByteBuffer data : fsm.getLogs()) {
assertEquals("hello" + i++, new String(data.array()));
}
node.shutdown();
node.join();
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class ReadOnlyServiceTest method setup.
@Before
public void setup() {
this.readOnlyServiceImpl = new ReadOnlyServiceImpl();
final ReadOnlyServiceOptions opts = new ReadOnlyServiceOptions();
opts.setFsmCaller(this.fsmCaller);
opts.setNode(this.node);
opts.setRaftOptions(new RaftOptions());
Mockito.when(this.node.getNodeMetrics()).thenReturn(new NodeMetrics(false));
Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
Mockito.when(this.node.getGroupId()).thenReturn("test");
Mockito.when(this.node.getServerId()).thenReturn(new PeerId("localhost:8081", 0));
assertTrue(this.readOnlyServiceImpl.init(opts));
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class NodeTest method testNoSnapshot.
@Test
public void testNoSnapshot() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
NodeManager.getInstance().addAddress(addr);
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.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
assertTrue(node.init(nodeOptions));
// wait node elect self as leader
Thread.sleep(2000);
this.sendTestTaskAndWait(node);
assertEquals(0, fsm.getSaveSnapshotTimes());
// do snapshot but returns error
CountDownLatch latch = new CountDownLatch(1);
node.snapshot(new ExpectClosure(RaftError.EINVAL, "Snapshot is not supported", latch));
waitLatch(latch);
assertEquals(0, fsm.getSaveSnapshotTimes());
latch = new CountDownLatch(1);
node.shutdown(new ExpectClosure(latch));
node.join();
waitLatch(latch);
}
Aggregations