Search in sources :

Example 1 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions 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 2 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions 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 3 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project sofa-jraft by sofastack.

the class LocalSnapshotMetaTableTest method testSaveLoadFile.

@Test
public void testSaveLoadFile() throws IOException {
    LocalFileMetaOutter.LocalFileMeta meta1 = LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("data1").setSource(LocalFileMetaOutter.FileSource.FILE_SOURCE_LOCAL).build();
    assertTrue(this.table.addFile("data1", meta1));
    LocalFileMetaOutter.LocalFileMeta meta2 = LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("data2").setSource(LocalFileMetaOutter.FileSource.FILE_SOURCE_LOCAL).build();
    assertTrue(this.table.addFile("data2", meta2));
    assertTrue(table.listFiles().contains("data1"));
    assertTrue(table.listFiles().contains("data2"));
    String path = TestUtils.mkTempDir();
    FileUtils.forceMkdir(new File(path));
    try {
        String filePath = path + File.separator + "table";
        table.saveToFile(filePath);
        LocalSnapshotMetaTable newTable = new LocalSnapshotMetaTable(new RaftOptions());
        assertNull(newTable.getFileMeta("data1"));
        assertNull(newTable.getFileMeta("data2"));
        assertTrue(newTable.loadFromFile(filePath));
        Assert.assertEquals(meta1, newTable.getFileMeta("data1"));
        Assert.assertEquals(meta2, newTable.getFileMeta("data2"));
    } finally {
        FileUtils.deleteDirectory(new File(path));
    }
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) File(java.io.File) Test(org.junit.Test)

Example 4 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project sofa-jraft by sofastack.

the class LocalSnapshotMetaTableTest method testSaveLoadIoBuffer.

@Test
public void testSaveLoadIoBuffer() throws Exception {
    LocalFileMetaOutter.LocalFileMeta meta1 = LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("data1").setSource(LocalFileMetaOutter.FileSource.FILE_SOURCE_LOCAL).build();
    assertTrue(this.table.addFile("data1", meta1));
    LocalFileMetaOutter.LocalFileMeta meta2 = LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("data2").setSource(LocalFileMetaOutter.FileSource.FILE_SOURCE_LOCAL).build();
    assertTrue(this.table.addFile("data2", meta2));
    ByteBuffer buf = this.table.saveToByteBufferAsRemote();
    assertNotNull(buf);
    assertTrue(buf.hasRemaining());
    LocalSnapshotMetaTable newTable = new LocalSnapshotMetaTable(new RaftOptions());
    assertNull(newTable.getFileMeta("data1"));
    assertNull(newTable.getFileMeta("data2"));
    assertTrue(newTable.loadFromIoBufferAsRemote(buf));
    Assert.assertEquals(meta1, newTable.getFileMeta("data1"));
    Assert.assertEquals(meta2, newTable.getFileMeta("data2"));
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with RaftOptions

use of com.alipay.sofa.jraft.option.RaftOptions in project sofa-jraft by sofastack.

the class LocalSnapshotStorageTest method setup.

@Override
@Before
public void setup() throws Exception {
    super.setup();
    String snapshotPath = this.path + File.separator + Snapshot.JRAFT_SNAPSHOT_PREFIX + lastSnapshotIndex;
    FileUtils.forceMkdir(new File(snapshotPath));
    this.table = new LocalSnapshotMetaTable(new RaftOptions());
    this.table.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(this.lastSnapshotIndex).setLastIncludedTerm(1).build());
    this.table.saveToFile(snapshotPath + File.separator + Snapshot.JRAFT_SNAPSHOT_META_FILE);
    this.snapshotStorage = new LocalSnapshotStorage(path, new RaftOptions());
    assertTrue(this.snapshotStorage.init(null));
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) File(java.io.File) Before(org.junit.Before)

Aggregations

RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)23 Test (org.junit.Test)11 Endpoint (com.alipay.sofa.jraft.util.Endpoint)9 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)8 Before (org.junit.Before)8 File (java.io.File)5 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)4 PeerId (com.alipay.sofa.jraft.entity.PeerId)4 TimerManager (com.alipay.sofa.jraft.core.TimerManager)3 CopyOptions (com.alipay.sofa.jraft.option.CopyOptions)3 LogStorageOptions (com.alipay.sofa.jraft.option.LogStorageOptions)3 SnapshotCopierOptions (com.alipay.sofa.jraft.option.SnapshotCopierOptions)3 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)3 RocksDBSegmentLogStorage (com.alipay.sofa.jraft.storage.log.RocksDBSegmentLogStorage)3 ByteBuffer (java.nio.ByteBuffer)3 Node (com.alipay.sofa.jraft.Node)2 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)2 CliServiceImpl (com.alipay.sofa.jraft.core.CliServiceImpl)2 CliOptions (com.alipay.sofa.jraft.option.CliOptions)2 Iterator (com.alipay.sofa.jraft.Iterator)1