Java Code Examples for com.alipay.sofa.jraft.storage.snapshot.SnapshotReader#load()

The following examples show how to use com.alipay.sofa.jraft.storage.snapshot.SnapshotReader#load() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalSnapshotStorageTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 2
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
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());
}