Search in sources :

Example 1 with SnapshotReader

use of io.dingodb.raft.storage.snapshot.SnapshotReader in project dingo by dingodb.

the class LocalSnapshotStorage method open.

@Override
public SnapshotReader open() {
    long lsIndex = 0;
    this.lock.lock();
    try {
        if (this.lastSnapshotIndex != 0) {
            lsIndex = this.lastSnapshotIndex;
            ref(lsIndex);
        }
    } finally {
        this.lock.unlock();
    }
    if (lsIndex == 0) {
        LOG.warn("No data for snapshot reader {}.", this.path);
        return null;
    }
    final String snapshotPath = getSnapshotPath(lsIndex);
    final SnapshotReader reader = new LocalSnapshotReader(this, this.snapshotThrottle, this.addr, this.raftOptions, snapshotPath);
    if (!reader.init(null)) {
        LOG.error("Fail to init reader for path {}.", snapshotPath);
        unref(lsIndex);
        return null;
    }
    return reader;
}
Also used : SnapshotReader(io.dingodb.raft.storage.snapshot.SnapshotReader)

Example 2 with SnapshotReader

use of io.dingodb.raft.storage.snapshot.SnapshotReader in project dingo by dingodb.

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(io.dingodb.raft.storage.snapshot.SnapshotReader) SnapshotCopier(io.dingodb.raft.storage.snapshot.SnapshotCopier)

Example 3 with SnapshotReader

use of io.dingodb.raft.storage.snapshot.SnapshotReader in project dingo by dingodb.

the class LocalSnapshotCopier method filter.

private void filter() throws IOException {
    this.writer = (LocalSnapshotWriter) this.storage.create(!this.filterBeforeCopyRemote);
    if (this.writer == null) {
        setError(RaftError.EIO, "Fail to create snapshot writer");
        return;
    }
    if (this.filterBeforeCopyRemote) {
        final SnapshotReader reader = this.storage.open();
        if (!filterBeforeCopy(this.writer, reader)) {
            LOG.warn("Fail to filter writer before copying, destroy and create a new writer.");
            this.writer.setError(-1, "Fail to filter");
            Utils.closeQuietly(this.writer);
            this.writer = (LocalSnapshotWriter) this.storage.create(true);
        }
        if (reader != null) {
            Utils.closeQuietly(reader);
        }
        if (this.writer == null) {
            setError(RaftError.EIO, "Fail to create snapshot writer");
            return;
        }
    }
    this.writer.saveMeta(this.remoteSnapshot.getMetaTable().getMeta());
    if (!this.writer.sync()) {
        LOG.error("Fail to sync snapshot writer path={}", this.writer.getPath());
        setError(RaftError.EIO, "Fail to sync snapshot writer");
    }
}
Also used : SnapshotReader(io.dingodb.raft.storage.snapshot.SnapshotReader)

Example 4 with SnapshotReader

use of io.dingodb.raft.storage.snapshot.SnapshotReader in project dingo by dingodb.

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(io.dingodb.raft.Status) RaftException(io.dingodb.raft.error.RaftException) Configuration(io.dingodb.raft.conf.Configuration) RaftOutter(io.dingodb.raft.entity.RaftOutter) SnapshotReader(io.dingodb.raft.storage.snapshot.SnapshotReader) LogId(io.dingodb.raft.entity.LogId) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

SnapshotReader (io.dingodb.raft.storage.snapshot.SnapshotReader)4 Status (io.dingodb.raft.Status)1 Configuration (io.dingodb.raft.conf.Configuration)1 LogId (io.dingodb.raft.entity.LogId)1 PeerId (io.dingodb.raft.entity.PeerId)1 RaftOutter (io.dingodb.raft.entity.RaftOutter)1 RaftException (io.dingodb.raft.error.RaftException)1 SnapshotCopier (io.dingodb.raft.storage.snapshot.SnapshotCopier)1