use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class KVStateMachineTest method setup.
@Before
public void setup() throws IOException, InterruptedException {
final Region region = new Region();
region.setId(1);
final StoreEngine storeEngine = new MockStoreEngine();
final KVStoreStateMachine fsm = new KVStoreStateMachine(region, storeEngine);
final NodeOptions nodeOpts = new NodeOptions();
final Configuration conf = new Configuration();
conf.addPeer(PeerId.parsePeer("127.0.0.1:8081"));
nodeOpts.setInitialConf(conf);
nodeOpts.setFsm(fsm);
final String raftDataPath = "raft_st_test";
this.raftDataPath = new File(raftDataPath);
if (this.raftDataPath.exists()) {
FileUtils.forceDelete(this.raftDataPath);
}
FileUtils.forceMkdir(this.raftDataPath);
final Path logUri = Paths.get(raftDataPath, "log");
nodeOpts.setLogUri(logUri.toString());
final Path meteUri = Paths.get(raftDataPath, "meta");
nodeOpts.setRaftMetaUri(meteUri.toString());
final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
nodeOpts.setSnapshotUri(snapshotUri.toString());
final Endpoint serverAddress = new Endpoint("127.0.0.1", 8081);
final PeerId serverId = new PeerId(serverAddress, 0);
this.raftGroupService = new RaftGroupService("st_test", serverId, nodeOpts, null, true);
final Node node = this.raftGroupService.start(false);
for (int i = 0; i < 100; i++) {
if (node.isLeader()) {
break;
}
Thread.sleep(100);
}
final RawKVStore rawKVStore = storeEngine.getRawKVStore();
this.raftRawKVStore = new RaftRawKVStore(node, rawKVStore, null);
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class BaseCliRequestProcessorTest method testSingleNode.
@Test
public void testSingleNode() {
Node node = this.mockNode(false);
this.processor = new MockCliRequestProcessor(null, "test");
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(resp);
assertSame(this.processor.ctx.node, node);
assertNotNull(resp);
assertEquals(0, resp.getErrorCode());
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class BaseCliRequestProcessorTest method mockNode.
private Node mockNode(boolean disableCli) {
Node node = Mockito.mock(Node.class);
Mockito.when(node.getGroupId()).thenReturn("test");
Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", this.peer.copy()));
NodeOptions opts = new NodeOptions();
opts.setDisableCli(disableCli);
Mockito.when(node.getOptions()).thenReturn(opts);
NodeManager.getInstance().addAddress(this.peer.getEndpoint());
NodeManager.getInstance().add(node);
return node;
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeRequestProcessorTest method testOK.
@Test
public void testOK() {
Node node = Mockito.mock(Node.class, withSettings().extraInterfaces(RaftServerService.class));
Mockito.when(node.getGroupId()).thenReturn("test");
PeerId peerId = new PeerId("localhost", 8081);
Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", peerId));
NodeManager.getInstance().addAddress(peerId.getEndpoint());
NodeManager.getInstance().add(node);
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(resp);
assertEquals(0, resp.getErrorCode());
}
use of com.alipay.sofa.jraft.Node 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;
}
Aggregations