Search in sources :

Example 1 with SnapshotWriter

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter in project ignite-3 by apache.

the class FSMCallerImpl method doSnapshotSave.

private void doSnapshotSave(final SaveSnapshotClosure done) {
    Requires.requireNonNull(done, "SaveSnapshotClosure is null");
    final long lastAppliedIndex = this.lastAppliedIndex.get();
    final ConfigurationEntry confEntry = this.logManager.getConfiguration(lastAppliedIndex);
    if (confEntry == null || confEntry.isEmpty()) {
        LOG.error("Empty conf entry for lastAppliedIndex={}", lastAppliedIndex);
        Utils.runClosureInThread(this.node.getOptions().getCommonExecutor(), done, new Status(RaftError.EINVAL, "Empty conf entry for lastAppliedIndex=%s", lastAppliedIndex));
        return;
    }
    SnapshotMetaBuilder metaBuilder = msgFactory.snapshotMeta().lastIncludedIndex(lastAppliedIndex).lastIncludedTerm(this.lastAppliedTerm).peersList(confEntry.getConf().getPeers().stream().map(Object::toString).collect(toList())).learnersList(confEntry.getConf().getLearners().stream().map(Object::toString).collect(toList()));
    if (confEntry.getOldConf() != null) {
        metaBuilder.oldPeersList(confEntry.getOldConf().getPeers().stream().map(Object::toString).collect(toList())).oldLearnersList(confEntry.getOldConf().getLearners().stream().map(Object::toString).collect(toList()));
    }
    final SnapshotWriter writer = done.start(metaBuilder.build());
    if (writer == null) {
        done.run(new Status(RaftError.EINVAL, "snapshot_storage create SnapshotWriter failed"));
        return;
    }
    this.fsm.onSnapshotSave(writer, done);
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SnapshotWriter(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter) SnapshotMetaBuilder(org.apache.ignite.raft.jraft.entity.SnapshotMetaBuilder) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry)

Example 2 with SnapshotWriter

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter in project ignite-3 by apache.

the class LocalSnapshotStorageTest method testCreateOpen.

@Test
public void testCreateOpen() throws Exception {
    SnapshotWriter writer = this.snapshotStorage.create();
    assertNotNull(writer);
    RaftOutter.SnapshotMeta wroteMeta = opts.getRaftMessagesFactory().snapshotMeta().lastIncludedIndex(this.lastSnapshotIndex + 1).lastIncludedTerm(1).build();
    ((LocalSnapshotWriter) writer).saveMeta(wroteMeta);
    writer.addFile("data");
    assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex).get());
    writer.close();
    // release old
    assertEquals(0, this.snapshotStorage.getRefs(this.lastSnapshotIndex).get());
    // ref new
    assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
    SnapshotReader reader = this.snapshotStorage.open();
    assertNotNull(reader);
    assertTrue(reader.listFiles().contains("data"));
    RaftOutter.SnapshotMeta readMeta = reader.load();
    assertEquals(wroteMeta, readMeta);
    assertEquals(2, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
    reader.close();
    assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
}
Also used : RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) SnapshotWriter(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 3 with SnapshotWriter

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter in project ignite-3 by apache.

the class FSMCallerTest method testOnSnapshotSave.

@Test
public void testOnSnapshotSave() throws Exception {
    final SnapshotWriter writer = Mockito.mock(SnapshotWriter.class);
    Mockito.when(this.logManager.getConfiguration(10)).thenReturn(TestUtils.getConfEntry("localhost:8081,localhost:8082,localhost:8083", "localhost:8081"));
    final SaveSnapshotClosure done = new SaveSnapshotClosure() {

        @Override
        public void run(final Status status) {
        }

        @Override
        public SnapshotWriter start(final SnapshotMeta meta) {
            assertEquals(10, meta.lastIncludedIndex());
            return writer;
        }
    };
    this.fsmCaller.onSnapshotSave(done);
    this.fsmCaller.flush();
    Mockito.verify(this.fsm).onSnapshotSave(writer, done);
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SnapshotWriter(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter) SaveSnapshotClosure(org.apache.ignite.raft.jraft.closure.SaveSnapshotClosure) SnapshotMeta(org.apache.ignite.raft.jraft.entity.RaftOutter.SnapshotMeta) Test(org.junit.jupiter.api.Test)

Example 4 with SnapshotWriter

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter in project ignite-3 by apache.

the class FSMCallerTest method testOnSnapshotSaveEmptyConf.

@Test
public void testOnSnapshotSaveEmptyConf() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotSave(new SaveSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals("Empty conf entry for lastAppliedIndex=10", status.getErrorMsg());
            latch.countDown();
        }

        @Override
        public SnapshotWriter start(final SnapshotMeta meta) {
            return null;
        }
    });
    latch.await();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SnapshotWriter(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter) SaveSnapshotClosure(org.apache.ignite.raft.jraft.closure.SaveSnapshotClosure) SnapshotMeta(org.apache.ignite.raft.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

SnapshotWriter (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter)4 Status (org.apache.ignite.raft.jraft.Status)3 Test (org.junit.jupiter.api.Test)3 SaveSnapshotClosure (org.apache.ignite.raft.jraft.closure.SaveSnapshotClosure)2 SnapshotMeta (org.apache.ignite.raft.jraft.entity.RaftOutter.SnapshotMeta)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)1 RaftOutter (org.apache.ignite.raft.jraft.entity.RaftOutter)1 SnapshotMetaBuilder (org.apache.ignite.raft.jraft.entity.SnapshotMetaBuilder)1 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)1 SnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader)1