Search in sources :

Example 11 with SnapshotReader

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotReader 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 12 with SnapshotReader

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

the class FSMCallerTest method testOnSnapshotLoadFSMError.

@Test
public void testOnSnapshotLoadFSMError() throws Exception {
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(12).setLastIncludedTerm(1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    Mockito.when(this.fsm.onSnapshotLoad(reader)).thenReturn(false);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals(-1, status.getCode());
            assertEquals("StateMachine onSnapshotLoad failed", status.getErrorMsg());
            latch.countDown();
        }

        @Override
        public SnapshotReader start() {
            return reader;
        }
    });
    latch.await();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 10);
}
Also used : LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Status(com.alipay.sofa.jraft.Status) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with SnapshotReader

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

the class LocalSnapshotCopierTest method testInterrupt.

@Test
public void testInterrupt() throws Exception {
    final FutureImpl<Message> future = new FutureImpl<>();
    final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
    // mock get metadata
    final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    this.copier.start();
    Thread.sleep(10);
    Utils.runInThread(new Runnable() {

        @Override
        public void run() {
            LocalSnapshotCopierTest.this.copier.cancel();
        }
    });
    this.copier.join();
    // start timer
    final SnapshotReader reader = this.copier.getReader();
    assertNull(reader);
    Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    Assert.assertEquals("Cancel the copier manually.", this.copier.getErrorMsg());
}
Also used : Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 14 with SnapshotReader

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

the class LocalSnapshotStorage method copyFrom.

@Override
public SnapshotReader copyFrom(final String uri, final SnapshotCopierOptions opts) {
    final SnapshotCopier copier = startToCopyFrom(uri, opts);
    if (copier == null) {
        return null;
    }
    try {
        copier.join();
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Join on snapshot copier was interrupted.");
        return null;
    }
    final SnapshotReader reader = copier.getReader();
    Utils.closeQuietly(copier);
    return reader;
}
Also used : SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) SnapshotCopier(com.alipay.sofa.jraft.storage.snapshot.SnapshotCopier)

Example 15 with SnapshotReader

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

the class FSMCallerImpl method doSnapshotLoad.

private void doSnapshotLoad(final LoadSnapshotClosure done) {
    Requires.requireNonNull(done, "LoadSnapshotClosure is null");
    final SnapshotReader reader = done.start();
    if (reader == null) {
        done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
        return;
    }
    final RaftOutter.SnapshotMeta meta = reader.load();
    if (meta == null) {
        done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
        if (reader.getRaftError() == RaftError.EIO) {
            final RaftException err = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO, "Fail to load snapshot meta");
            setError(err);
        }
        return;
    }
    final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
    final LogId snapshotId = new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm());
    if (lastAppliedId.compareTo(snapshotId) > 0) {
        done.run(new Status(RaftError.ESTALE, "Loading a stale snapshot last_applied_index=%d last_applied_term=%d snapshot_index=%d snapshot_term=%d", lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
        return;
    }
    if (!this.fsm.onSnapshotLoad(reader)) {
        done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
        final RaftException e = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
        setError(e);
        return;
    }
    if (meta.getOldPeersCount() == 0) {
        // Joint stage is not supposed to be noticeable by end users.
        final Configuration conf = new Configuration();
        for (int i = 0, size = meta.getPeersCount(); i < size; i++) {
            final PeerId peer = new PeerId();
            Requires.requireTrue(peer.parse(meta.getPeers(i)), "Parse peer failed");
            conf.addPeer(peer);
        }
        this.fsm.onConfigurationCommitted(conf);
    }
    this.lastAppliedIndex.set(meta.getLastIncludedIndex());
    this.lastAppliedTerm = meta.getLastIncludedTerm();
    done.run(Status.OK());
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftOutter(com.alipay.sofa.jraft.entity.RaftOutter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) LogId(com.alipay.sofa.jraft.entity.LogId) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)16 Test (org.junit.Test)10 Status (com.alipay.sofa.jraft.Status)6 Message (com.google.protobuf.Message)6 LoadSnapshotClosure (com.alipay.sofa.jraft.closure.LoadSnapshotClosure)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)5 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)5 Endpoint (com.alipay.sofa.jraft.util.Endpoint)5 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)3 SnapshotMeta (com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)3 ByteBuffer (java.nio.ByteBuffer)3 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)2 RpcRequestClosure (com.alipay.sofa.jraft.rpc.RpcRequestClosure)2 LocalSnapshotReader (com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader)2 BiConsumer (java.util.function.BiConsumer)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1