Search in sources :

Example 1 with GroupMismatchException

use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.

the class BlockingImpl method sendRequestWithRetry.

RaftClientReply sendRequestWithRetry(Supplier<RaftClientRequest> supplier) throws IOException {
    RaftClientImpl.PendingClientRequest pending = new RaftClientImpl.PendingClientRequest() {

        @Override
        public RaftClientRequest newRequestImpl() {
            return supplier.get();
        }
    };
    while (true) {
        final RaftClientRequest request = pending.newRequest();
        IOException ioe = null;
        try {
            final RaftClientReply reply = sendRequest(request);
            if (reply != null) {
                return client.handleReply(request, reply);
            }
        } catch (GroupMismatchException | StateMachineException | TransferLeadershipException | LeaderSteppingDownException | AlreadyClosedException | AlreadyExistsException e) {
            throw e;
        } catch (IOException e) {
            ioe = e;
        }
        pending.incrementExceptionCount(ioe);
        ClientRetryEvent event = new ClientRetryEvent(request, ioe, pending);
        final RetryPolicy retryPolicy = client.getRetryPolicy();
        final RetryPolicy.Action action = retryPolicy.handleAttemptFailure(event);
        TimeDuration sleepTime = client.getEffectiveSleepTime(ioe, action.getSleepTime());
        if (!action.shouldRetry()) {
            throw (IOException) client.noMoreRetries(event);
        }
        try {
            sleepTime.sleep();
        } catch (InterruptedException e) {
            throw new InterruptedIOException("retry policy=" + retryPolicy);
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) TimeDuration(org.apache.ratis.util.TimeDuration) RetryPolicy(org.apache.ratis.retry.RetryPolicy)

Example 2 with GroupMismatchException

use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.

the class BlockingImpl method sendRequest.

private RaftClientReply sendRequest(RaftClientRequest request) throws IOException {
    LOG.debug("{}: send {}", client.getId(), request);
    RaftClientReply reply;
    try {
        reply = client.getClientRpc().sendRequest(request);
    } catch (GroupMismatchException gme) {
        throw gme;
    } catch (IOException ioe) {
        client.handleIOException(request, ioe);
        throw ioe;
    }
    LOG.debug("{}: receive {}", client.getId(), reply);
    reply = client.handleLeaderException(request, reply);
    reply = RaftClientImpl.handleRaftException(reply, Function.identity());
    return reply;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 3 with GroupMismatchException

use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.

the class RaftServerProxy method groupAddAsync.

private CompletableFuture<RaftClientReply> groupAddAsync(GroupManagementRequest request, RaftGroup newGroup) {
    if (!request.getRaftGroupId().equals(newGroup.getGroupId())) {
        return JavaUtils.completeExceptionally(new GroupMismatchException(getId() + ": Request group id (" + request.getRaftGroupId() + ") does not match the new group " + newGroup));
    }
    return impls.addNew(newGroup).thenApplyAsync(newImpl -> {
        LOG.debug("{}: newImpl = {}", getId(), newImpl);
        final boolean started = newImpl.start();
        Preconditions.assertTrue(started, () -> getId() + ": failed to start a new impl: " + newImpl);
        return newImpl.newSuccessReply(request);
    }, implExecutor).whenComplete((raftClientReply, throwable) -> {
        if (throwable != null) {
            if (!(throwable.getCause() instanceof AlreadyExistsException)) {
                impls.remove(newGroup.getGroupId());
                LOG.warn(getId() + ": Failed groupAdd* " + request, throwable);
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(getId() + ": Failed groupAdd* " + request, throwable);
                }
            }
        }
    });
}
Also used : Arrays(java.util.Arrays) RequestVoteReplyProto(org.apache.ratis.proto.RaftProtos.RequestVoteReplyProto) RaftRpcRequestProto(org.apache.ratis.proto.RaftProtos.RaftRpcRequestProto) RaftServerRpc(org.apache.ratis.server.RaftServerRpc) ProtoUtils(org.apache.ratis.util.ProtoUtils) Map(java.util.Map) RaftConfigKeys(org.apache.ratis.RaftConfigKeys) JavaUtils(org.apache.ratis.util.JavaUtils) ServerFactory(org.apache.ratis.server.ServerFactory) java.util.concurrent(java.util.concurrent) Predicate(java.util.function.Predicate) Collection(java.util.Collection) AppendEntriesRequestProto(org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto) JvmPauseMonitor(org.apache.ratis.util.JvmPauseMonitor) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) Objects(java.util.Objects) List(java.util.List) InstallSnapshotRequestProto(org.apache.ratis.proto.RaftProtos.InstallSnapshotRequestProto) Stream(java.util.stream.Stream) RaftProperties(org.apache.ratis.conf.RaftProperties) Optional(java.util.Optional) InstallSnapshotReplyProto(org.apache.ratis.proto.RaftProtos.InstallSnapshotReplyProto) TimeDuration(org.apache.ratis.util.TimeDuration) Preconditions(org.apache.ratis.util.Preconditions) org.apache.ratis.protocol(org.apache.ratis.protocol) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) AppendEntriesReplyProto(org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException) SupportedDataStreamType(org.apache.ratis.datastream.SupportedDataStreamType) ConcurrentUtils(org.apache.ratis.util.ConcurrentUtils) IOUtils(org.apache.ratis.util.IOUtils) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) IOException(java.io.IOException) RequestVoteRequestProto(org.apache.ratis.proto.RaftProtos.RequestVoteRequestProto) StartLeaderElectionRequestProto(org.apache.ratis.proto.RaftProtos.StartLeaderElectionRequestProto) File(java.io.File) Parameters(org.apache.ratis.conf.Parameters) DataStreamServerRpc(org.apache.ratis.server.DataStreamServerRpc) Closeable(java.io.Closeable) RpcType(org.apache.ratis.rpc.RpcType) LifeCycle(org.apache.ratis.util.LifeCycle) RaftServer(org.apache.ratis.server.RaftServer) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) StartLeaderElectionReplyProto(org.apache.ratis.proto.RaftProtos.StartLeaderElectionReplyProto) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException)

Example 4 with GroupMismatchException

use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.

the class RaftServerImpl method checkLeaderState.

/**
 * @return null if the server is in leader state.
 */
private CompletableFuture<RaftClientReply> checkLeaderState(RaftClientRequest request, CacheEntry entry, boolean isWrite) {
    try {
        assertGroup(request.getRequestorId(), request.getRaftGroupId());
    } catch (GroupMismatchException e) {
        return RetryCacheImpl.failWithException(e, entry);
    }
    if (!getInfo().isLeader()) {
        NotLeaderException exception = generateNotLeaderException();
        final RaftClientReply reply = newExceptionReply(request, exception);
        return RetryCacheImpl.failWithReply(reply, entry);
    }
    if (!getInfo().isLeaderReady()) {
        final CacheEntry cacheEntry = retryCache.getIfPresent(ClientInvocationId.valueOf(request));
        if (cacheEntry != null && cacheEntry.isCompletedNormally()) {
            return cacheEntry.getReplyFuture();
        }
        final LeaderNotReadyException lnre = new LeaderNotReadyException(getMemberId());
        final RaftClientReply reply = newExceptionReply(request, lnre);
        return RetryCacheImpl.failWithReply(reply, entry);
    }
    if (isWrite && isSteppingDown()) {
        final LeaderSteppingDownException lsde = new LeaderSteppingDownException(getMemberId() + " is stepping down");
        final RaftClientReply reply = newExceptionReply(request, lsde);
        return RetryCacheImpl.failWithReply(reply, entry);
    }
    return null;
}
Also used : NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) CacheEntry(org.apache.ratis.server.impl.RetryCacheImpl.CacheEntry) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)

Example 5 with GroupMismatchException

use of org.apache.ratis.protocol.exceptions.GroupMismatchException in project incubator-ratis by apache.

the class OrderedAsync method sendRequest.

private CompletableFuture<RaftClientReply> sendRequest(PendingOrderedRequest pending) {
    final RetryPolicy retryPolicy = client.getRetryPolicy();
    final CompletableFuture<RaftClientReply> f;
    final RaftClientRequest request;
    if (getSlidingWindow((RaftPeerId) null).isFirst(pending.getSeqNum())) {
        pending.setFirstRequest();
    }
    request = pending.newRequest();
    LOG.debug("{}: send* {}", client.getId(), request);
    f = client.getClientRpc().sendRequestAsync(request);
    return f.thenApply(reply -> {
        LOG.debug("{}: receive* {}", client.getId(), reply);
        getSlidingWindow(request).receiveReply(request.getSlidingWindowEntry().getSeqNum(), reply, this::sendRequestWithRetry);
        return reply;
    }).exceptionally(e -> {
        if (LOG.isTraceEnabled()) {
            LOG.trace(client.getId() + ": Failed* " + request, e);
        } else {
            LOG.debug("{}: Failed* {} with {}", client.getId(), request, e);
        }
        e = JavaUtils.unwrapCompletionException(e);
        if (e instanceof IOException && !(e instanceof GroupMismatchException)) {
            pending.incrementExceptionCount(e);
            final ClientRetryEvent event = new ClientRetryEvent(request, e, pending);
            if (!retryPolicy.handleAttemptFailure(event).shouldRetry()) {
                handleAsyncRetryFailure(event);
            } else {
                if (e instanceof NotLeaderException) {
                    NotLeaderException nle = (NotLeaderException) e;
                    client.handleNotLeaderException(request, nle, this::resetSlidingWindow);
                } else {
                    client.handleIOException(request, (IOException) e, null, this::resetSlidingWindow);
                }
            }
            throw new CompletionException(e);
        }
        failAllAsyncRequests(request, e);
        return null;
    });
}
Also used : Preconditions(org.apache.ratis.util.Preconditions) SlidingWindowEntry(org.apache.ratis.proto.RaftProtos.SlidingWindowEntry) LoggerFactory(org.slf4j.LoggerFactory) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) CallId(org.apache.ratis.rpc.CallId) ConcurrentMap(java.util.concurrent.ConcurrentMap) Message(org.apache.ratis.protocol.Message) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) ProtoUtils(org.apache.ratis.util.ProtoUtils) JavaUtils(org.apache.ratis.util.JavaUtils) IOUtils(org.apache.ratis.util.IOUtils) SlidingWindow(org.apache.ratis.util.SlidingWindow) Logger(org.slf4j.Logger) LongFunction(java.util.function.LongFunction) RetryPolicy(org.apache.ratis.retry.RetryPolicy) Semaphore(java.util.concurrent.Semaphore) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) TypeCase(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto.TypeCase) Objects(java.util.Objects) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) Optional(java.util.Optional) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) RaftClientConfigKeys(org.apache.ratis.client.RaftClientConfigKeys) PendingClientRequest(org.apache.ratis.client.impl.RaftClientImpl.PendingClientRequest) TimeDuration(org.apache.ratis.util.TimeDuration) ClientRetryEvent(org.apache.ratis.client.retry.ClientRetryEvent) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) CompletionException(java.util.concurrent.CompletionException) GroupMismatchException(org.apache.ratis.protocol.exceptions.GroupMismatchException) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) IOException(java.io.IOException) RetryPolicy(org.apache.ratis.retry.RetryPolicy)

Aggregations

GroupMismatchException (org.apache.ratis.protocol.exceptions.GroupMismatchException)8 IOException (java.io.IOException)5 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)4 TimeDuration (org.apache.ratis.util.TimeDuration)4 ClientRetryEvent (org.apache.ratis.client.retry.ClientRetryEvent)3 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)3 InterruptedIOException (java.io.InterruptedIOException)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 CompletionException (java.util.concurrent.CompletionException)2 RaftProperties (org.apache.ratis.conf.RaftProperties)2 AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)2 AlreadyExistsException (org.apache.ratis.protocol.exceptions.AlreadyExistsException)2 LeaderSteppingDownException (org.apache.ratis.protocol.exceptions.LeaderSteppingDownException)2 NotLeaderException (org.apache.ratis.protocol.exceptions.NotLeaderException)2 RetryPolicy (org.apache.ratis.retry.RetryPolicy)2 Closeable (java.io.Closeable)1 File (java.io.File)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1