Search in sources :

Example 1 with InstallSnapshotResponse

use of com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotResponse in project sofa-jraft by sofastack.

the class Replicator method installSnapshot.

void installSnapshot() {
    if (getState() == State.Snapshot) {
        LOG.warn("Replicator {} is installing snapshot, ignore the new request.", this.options.getPeerId());
        unlockId();
        return;
    }
    boolean doUnlock = true;
    if (!this.rpcService.connect(this.options.getPeerId().getEndpoint())) {
        LOG.error("Fail to check install snapshot connection to peer={}, give up to send install snapshot request.", this.options.getPeerId().getEndpoint());
        block(Utils.nowMs(), RaftError.EHOSTDOWN.getNumber());
        return;
    }
    try {
        Requires.requireTrue(this.reader == null, "Replicator %s already has a snapshot reader, current state is %s", this.options.getPeerId(), getState());
        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"));
            unlockId();
            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();
            unlockId();
            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();
            unlockId();
            doUnlock = false;
            node.onError(error);
            return;
        }
        final InstallSnapshotRequest.Builder rb = InstallSnapshotRequest.newBuilder();
        rb.setTerm(this.options.getTerm());
        rb.setGroupId(this.options.getGroupId());
        rb.setServerId(this.options.getServerId().toString());
        rb.setPeerId(this.options.getPeerId().toString());
        rb.setMeta(meta);
        rb.setUri(uri);
        this.statInfo.runningState = RunningState.INSTALLING_SNAPSHOT;
        this.statInfo.lastLogIncluded = meta.getLastIncludedIndex();
        this.statInfo.lastTermIncluded = meta.getLastIncludedTerm();
        final InstallSnapshotRequest request = rb.build();
        setState(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) {
            unlockId();
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Message(com.google.protobuf.Message) RaftOutter(com.alipay.sofa.jraft.entity.RaftOutter) InstallSnapshotRequest(com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotRequest) ByteString(com.google.protobuf.ByteString) InstallSnapshotResponse(com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotResponse)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)1 RaftException (com.alipay.sofa.jraft.error.RaftException)1 InstallSnapshotRequest (com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotRequest)1 InstallSnapshotResponse (com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotResponse)1 ByteString (com.google.protobuf.ByteString)1 Message (com.google.protobuf.Message)1