use of com.alipay.sofa.jraft.option.NodeOptions 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.option.NodeOptions in project sofa-jraft by sofastack.
the class SnapshotExecutorTest method setup.
@Override
@Before
public void setup() throws Exception {
super.setup();
this.timerManager = new TimerManager(5);
this.raftOptions = new RaftOptions();
this.writer = new LocalSnapshotWriter(this.path, this.snapshotStorage, this.raftOptions);
this.reader = new LocalSnapshotReader(this.snapshotStorage, null, new Endpoint("localhost", 8081), this.raftOptions, this.path);
Mockito.when(this.snapshotStorage.open()).thenReturn(this.reader);
Mockito.when(this.snapshotStorage.create(true)).thenReturn(this.writer);
this.table = new LocalSnapshotMetaTable(this.raftOptions);
this.table.addFile("testFile", LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("test").build());
this.table.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(1).build());
this.uri = "remote://" + this.hostPort + "/" + this.readerId;
this.copyOpts = new CopyOptions();
Mockito.when(this.node.getRaftOptions()).thenReturn(new RaftOptions());
Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
Mockito.when(this.node.getRpcService()).thenReturn(this.raftClientService);
Mockito.when(this.node.getTimerManager()).thenReturn(this.timerManager);
Mockito.when(this.node.getServiceFactory()).thenReturn(DefaultJRaftServiceFactory.newInstance());
this.executor = new SnapshotExecutorImpl();
final SnapshotExecutorOptions opts = new SnapshotExecutorOptions();
opts.setFsmCaller(this.fSMCaller);
opts.setInitTerm(0);
opts.setNode(this.node);
opts.setLogManager(this.logManager);
opts.setUri(this.path);
this.addr = new Endpoint("localhost", 8081);
opts.setAddr(this.addr);
assertTrue(this.executor.init(opts));
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class SnapshotExecutorTest method testDoSnapshotWithIntervalDist.
@Test
public void testDoSnapshotWithIntervalDist() throws Exception {
final NodeOptions nodeOptions = new NodeOptions();
nodeOptions.setSnapshotLogIndexMargin(5);
Mockito.when(this.node.getOptions()).thenReturn(nodeOptions);
Mockito.when(this.fSMCaller.getLastAppliedIndex()).thenReturn(6L);
final ArgumentCaptor<SaveSnapshotClosure> saveSnapshotClosureArg = ArgumentCaptor.forClass(SaveSnapshotClosure.class);
Mockito.when(this.fSMCaller.onSnapshotSave(saveSnapshotClosureArg.capture())).thenReturn(true);
final SynchronizedClosure done = new SynchronizedClosure();
this.executor.doSnapshot(done);
final SaveSnapshotClosure closure = saveSnapshotClosureArg.getValue();
assertNotNull(closure);
closure.start(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(6).setLastIncludedTerm(1).build());
closure.run(Status.OK());
done.await();
this.executor.join();
assertEquals(1, this.executor.getLastSnapshotTerm());
assertEquals(6, this.executor.getLastSnapshotIndex());
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class LocalSnapshotCopierTest method setup.
@Override
@Before
public void setup() throws Exception {
super.setup();
this.timerManager = new TimerManager(5);
this.raftOptions = new RaftOptions();
this.writer = new LocalSnapshotWriter(this.path, this.snapshotStorage, this.raftOptions);
this.reader = new LocalSnapshotReader(this.snapshotStorage, null, new Endpoint("localhost", 8081), this.raftOptions, this.path);
Mockito.when(this.snapshotStorage.open()).thenReturn(this.reader);
Mockito.when(this.snapshotStorage.create(true)).thenReturn(this.writer);
this.table = new LocalSnapshotMetaTable(this.raftOptions);
this.table.addFile("testFile", LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("test").build());
this.table.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(1).build());
this.uri = "remote://" + this.hostPort + "/" + this.readerId;
this.copier = new LocalSnapshotCopier();
this.copyOpts = new CopyOptions();
Mockito.when(this.raftClientService.connect(new Endpoint("localhost", 8081))).thenReturn(true);
assertTrue(this.copier.init(this.uri, new SnapshotCopierOptions(this.raftClientService, this.timerManager, this.raftOptions, new NodeOptions())));
this.copier.setStorage(this.snapshotStorage);
}
use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.
the class RemoteFileCopierTest method testInit.
@Test
public void testInit() {
Mockito.when(rpcService.connect(new Endpoint("localhost", 8081))).thenReturn(true);
assertTrue(copier.init("remote://localhost:8081/999", null, new SnapshotCopierOptions(rpcService, timerManager, new RaftOptions(), new NodeOptions())));
assertEquals(999, copier.getReaderId());
Assert.assertEquals("localhost", copier.getEndpoint().getIp());
Assert.assertEquals(8081, copier.getEndpoint().getPort());
}
Aggregations