Search in sources :

Example 1 with InstallSnapshotRequest

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotRequest in project ignite-3 by apache.

the class Replicator method installSnapshot.

void installSnapshot() {
    if (this.state == State.Snapshot) {
        LOG.warn("Replicator {} is installing snapshot, ignore the new request.", this.options.getPeerId());
        this.id.unlock();
        return;
    }
    boolean doUnlock = true;
    try {
        Requires.requireTrue(this.reader == null, "Replicator %s already has a snapshot reader, current state is %s", this.options.getPeerId(), this.state);
        this.reader = this.options.getSnapshotStorage().open();
        if (this.reader == null) {
            final NodeImpl node = this.options.getNode();
            final RaftException error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT);
            error.setStatus(new Status(RaftError.EIO, "Fail to open snapshot"));
            this.id.unlock();
            doUnlock = false;
            node.onError(error);
            return;
        }
        final String uri = this.reader.generateURIForCopy();
        if (uri == null) {
            final NodeImpl node = this.options.getNode();
            final RaftException error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT);
            error.setStatus(new Status(RaftError.EIO, "Fail to generate uri for snapshot reader"));
            releaseReader();
            this.id.unlock();
            doUnlock = false;
            node.onError(error);
            return;
        }
        final RaftOutter.SnapshotMeta meta = this.reader.load();
        if (meta == null) {
            final String snapshotPath = this.reader.getPath();
            final NodeImpl node = this.options.getNode();
            final RaftException error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT);
            error.setStatus(new Status(RaftError.EIO, "Fail to load meta from %s", snapshotPath));
            releaseReader();
            this.id.unlock();
            doUnlock = false;
            node.onError(error);
            return;
        }
        final InstallSnapshotRequest request = raftOptions.getRaftMessagesFactory().installSnapshotRequest().term(options.getTerm()).groupId(options.getGroupId()).serverId(options.getServerId().toString()).peerId(options.getPeerId().toString()).meta(meta).uri(uri).build();
        this.statInfo.runningState = RunningState.INSTALLING_SNAPSHOT;
        this.statInfo.lastLogIncluded = meta.lastIncludedIndex();
        this.statInfo.lastTermIncluded = meta.lastIncludedTerm();
        this.state = State.Snapshot;
        // noinspection NonAtomicOperationOnVolatileField
        this.installSnapshotCounter++;
        final long monotonicSendTimeMs = Utils.monotonicMs();
        final int stateVersion = this.version;
        final int seq = getAndIncrementReqSeq();
        final Future<Message> rpcFuture = this.rpcService.installSnapshot(this.options.getPeerId().getEndpoint(), request, new RpcResponseClosureAdapter<InstallSnapshotResponse>() {

            @Override
            public void run(final Status status) {
                onRpcReturned(Replicator.this.id, RequestType.Snapshot, status, request, getResponse(), seq, stateVersion, monotonicSendTimeMs);
            }
        });
        addInflight(RequestType.Snapshot, this.nextIndex, 0, 0, seq, rpcFuture);
    } finally {
        if (doUnlock) {
            this.id.unlock();
        }
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) RaftException(org.apache.ignite.raft.jraft.error.RaftException) Message(org.apache.ignite.raft.jraft.rpc.Message) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) InstallSnapshotRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotRequest) ByteString(org.apache.ignite.raft.jraft.util.ByteString) InstallSnapshotResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotResponse)

Aggregations

Status (org.apache.ignite.raft.jraft.Status)1 RaftOutter (org.apache.ignite.raft.jraft.entity.RaftOutter)1 RaftException (org.apache.ignite.raft.jraft.error.RaftException)1 Message (org.apache.ignite.raft.jraft.rpc.Message)1 InstallSnapshotRequest (org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotRequest)1 InstallSnapshotResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotResponse)1 ByteString (org.apache.ignite.raft.jraft.util.ByteString)1