Search in sources :

Example 1 with SnapshotWriter

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.

the class LocalSnapshotStorageTest method testCreateOpen.

@Test
public void testCreateOpen() throws Exception {
    SnapshotWriter writer = this.snapshotStorage.create();
    assertNotNull(writer);
    RaftOutter.SnapshotMeta wroteMeta = RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(this.lastSnapshotIndex + 1).setLastIncludedTerm(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(com.alipay.sofa.jraft.entity.RaftOutter) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 2 with SnapshotWriter

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.

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) {
            // TODO Auto-generated method stub
            return null;
        }
    });
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with SnapshotWriter

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project nacos by alibaba.

the class NacosStateMachine method adapterToJRaftSnapshot.

private void adapterToJRaftSnapshot(Collection<SnapshotOperation> userOperates) {
    List<JSnapshotOperation> tmp = new ArrayList<>();
    for (SnapshotOperation item : userOperates) {
        if (item == null) {
            Loggers.RAFT.error("Existing SnapshotOperation for null");
            continue;
        }
        tmp.add(new JSnapshotOperation() {

            @Override
            public void onSnapshotSave(SnapshotWriter writer, Closure done) {
                final Writer wCtx = new Writer(writer.getPath());
                // Do a layer of proxy operation to shield different Raft
                // components from implementing snapshots
                final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
                    boolean[] results = new boolean[wCtx.listFiles().size()];
                    int[] index = new int[] { 0 };
                    wCtx.listFiles().forEach((file, meta) -> {
                        try {
                            results[index[0]++] = writer.addFile(file, buildMetadata(meta));
                        } catch (Exception e) {
                            throw new ConsistencyException(e);
                        }
                    });
                    final Status status = result && !Arrays.asList(results).stream().anyMatch(Boolean.FALSE::equals) ? Status.OK() : new Status(RaftError.EIO, "Fail to compress snapshot at %s, error is %s", writer.getPath(), t == null ? "" : t.getMessage());
                    done.run(status);
                };
                item.onSnapshotSave(wCtx, callFinally);
            }

            @Override
            public boolean onSnapshotLoad(SnapshotReader reader) {
                final Map<String, LocalFileMeta> metaMap = new HashMap<>(reader.listFiles().size());
                for (String fileName : reader.listFiles()) {
                    final LocalFileMetaOutter.LocalFileMeta meta = (LocalFileMetaOutter.LocalFileMeta) reader.getFileMeta(fileName);
                    byte[] bytes = meta.getUserMeta().toByteArray();
                    final LocalFileMeta fileMeta;
                    if (bytes == null || bytes.length == 0) {
                        fileMeta = new LocalFileMeta();
                    } else {
                        fileMeta = JacksonUtils.toObj(bytes, LocalFileMeta.class);
                    }
                    metaMap.put(fileName, fileMeta);
                }
                final Reader rCtx = new Reader(reader.getPath(), metaMap);
                return item.onSnapshotLoad(rCtx);
            }

            @Override
            public String info() {
                return item.toString();
            }
        });
    }
    this.operations = Collections.unmodifiableList(tmp);
}
Also used : Status(com.alipay.sofa.jraft.Status) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) Closure(com.alipay.sofa.jraft.Closure) ArrayList(java.util.ArrayList) Reader(com.alibaba.nacos.consistency.snapshot.Reader) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) RaftException(com.alipay.sofa.jraft.error.RaftException) SnapshotOperation(com.alibaba.nacos.consistency.snapshot.SnapshotOperation) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) HashMap(java.util.HashMap) Map(java.util.Map) Writer(com.alibaba.nacos.consistency.snapshot.Writer) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) BiConsumer(java.util.function.BiConsumer)

Example 4 with SnapshotWriter

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.

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.getLastIncludedIndex());
            return writer;
        }
    };
    this.fsmCaller.onSnapshotSave(done);
    this.fsmCaller.flush();
    Mockito.verify(this.fsm).onSnapshotSave(writer, done);
}
Also used : Status(com.alipay.sofa.jraft.Status) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) Test(org.junit.Test)

Example 5 with SnapshotWriter

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.

the class FSMCallerImpl method doSnapshotSave.

private void doSnapshotSave(final SaveSnapshotClosure done) {
    Requires.requireNonNull(done, "SaveSnapshotClosure is null");
    final long lastAppliedIndex = this.lastAppliedIndex.get();
    final RaftOutter.SnapshotMeta.Builder metaBuilder = // 
    RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(// 
    lastAppliedIndex).setLastIncludedTerm(this.lastAppliedTerm);
    final ConfigurationEntry confEntry = this.logManager.getConfiguration(lastAppliedIndex);
    if (confEntry == null || confEntry.isEmpty()) {
        LOG.error("Empty conf entry for lastAppliedIndex={}", lastAppliedIndex);
        Utils.runClosureInThread(done, new Status(RaftError.EINVAL, "Empty conf entry for lastAppliedIndex=%s", lastAppliedIndex));
        return;
    }
    for (final PeerId peer : confEntry.getConf()) {
        metaBuilder.addPeers(peer.toString());
    }
    for (final PeerId peer : confEntry.getConf().getLearners()) {
        metaBuilder.addLearners(peer.toString());
    }
    if (confEntry.getOldConf() != null) {
        for (final PeerId peer : confEntry.getOldConf()) {
            metaBuilder.addOldPeers(peer.toString());
        }
        for (final PeerId peer : confEntry.getOldConf().getLearners()) {
            metaBuilder.addOldLearners(peer.toString());
        }
    }
    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(com.alipay.sofa.jraft.Status) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)6 Status (com.alipay.sofa.jraft.Status)4 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)3 Test (org.junit.Test)3 SaveSnapshotClosure (com.alipay.sofa.jraft.closure.SaveSnapshotClosure)2 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)2 SnapshotMeta (com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta)2 RaftException (com.alipay.sofa.jraft.error.RaftException)2 BiConsumer (java.util.function.BiConsumer)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 LocalFileMeta (com.alibaba.nacos.consistency.snapshot.LocalFileMeta)1 Reader (com.alibaba.nacos.consistency.snapshot.Reader)1 SnapshotOperation (com.alibaba.nacos.consistency.snapshot.SnapshotOperation)1 Writer (com.alibaba.nacos.consistency.snapshot.Writer)1 Closure (com.alipay.sofa.jraft.Closure)1 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)1 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)1 ArrayList (java.util.ArrayList)1