Search in sources :

Example 1 with NodeOptions

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);
}
Also used : Path(java.nio.file.Path) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Region(com.alipay.sofa.jraft.rhea.metadata.Region) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId) Before(org.junit.Before)

Example 2 with NodeOptions

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));
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) LocalSnapshotMetaTable(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotMetaTable) LocalSnapshotReader(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader) SnapshotExecutorImpl(com.alipay.sofa.jraft.storage.snapshot.SnapshotExecutorImpl) CopyOptions(com.alipay.sofa.jraft.option.CopyOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) TimerManager(com.alipay.sofa.jraft.core.TimerManager) LocalSnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotWriter) SnapshotExecutorOptions(com.alipay.sofa.jraft.option.SnapshotExecutorOptions) Before(org.junit.Before)

Example 3 with NodeOptions

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());
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Test(org.junit.Test)

Example 4 with NodeOptions

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);
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) SnapshotCopierOptions(com.alipay.sofa.jraft.option.SnapshotCopierOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CopyOptions(com.alipay.sofa.jraft.option.CopyOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) TimerManager(com.alipay.sofa.jraft.core.TimerManager) Before(org.junit.Before)

Example 5 with NodeOptions

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());
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) SnapshotCopierOptions(com.alipay.sofa.jraft.option.SnapshotCopierOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Test(org.junit.Test)

Aggregations

NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)36 PeerId (com.alipay.sofa.jraft.entity.PeerId)22 Endpoint (com.alipay.sofa.jraft.util.Endpoint)22 Configuration (com.alipay.sofa.jraft.conf.Configuration)17 Test (org.junit.Test)16 Node (com.alipay.sofa.jraft.Node)13 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 File (java.io.File)7 Before (org.junit.Before)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)3 BootstrapOptions (com.alipay.sofa.jraft.option.BootstrapOptions)3 SnapshotCopierOptions (com.alipay.sofa.jraft.option.SnapshotCopierOptions)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Iterator (com.alipay.sofa.jraft.Iterator)2 StateMachine (com.alipay.sofa.jraft.StateMachine)2