Search in sources :

Example 1 with AlreadyExistsException

use of org.apache.ratis.protocol.exceptions.AlreadyExistsException 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 2 with AlreadyExistsException

use of org.apache.ratis.protocol.exceptions.AlreadyExistsException 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 3 with AlreadyExistsException

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

the class DataStreamManagement method computeDataStreamIfAbsent.

private CompletableFuture<DataStream> computeDataStreamIfAbsent(RaftClientRequest request) throws IOException {
    final Division division = server.getDivision(request.getRaftGroupId());
    final ClientInvocationId invocationId = ClientInvocationId.valueOf(request);
    final MemoizedSupplier<CompletableFuture<DataStream>> supplier = JavaUtils.memoize(() -> {
        final RequestMetrics metrics = getMetrics().newRequestMetrics(RequestType.STATE_MACHINE_STREAM);
        final RequestContext context = metrics.start();
        return division.getStateMachine().data().stream(request).whenComplete((r, e) -> metrics.stop(context, e == null));
    });
    final CompletableFuture<DataStream> f = division.getDataStreamMap().computeIfAbsent(invocationId, key -> supplier.get());
    if (!supplier.isInitialized()) {
        throw new AlreadyExistsException("A DataStream already exists for " + invocationId);
    }
    return f;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) DataStream(org.apache.ratis.statemachine.StateMachine.DataStream) RequestMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics) Division(org.apache.ratis.server.RaftServer.Division) RequestContext(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId)

Example 4 with AlreadyExistsException

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

the class GroupManagementBaseTest method testGroupAlreadyExists.

@Test
public void testGroupAlreadyExists() throws Exception {
    final MiniRaftCluster cluster = getCluster(1);
    cluster.start();
    final RaftPeer peer = cluster.getPeers().get(0);
    final RaftPeerId peerId = peer.getId();
    final RaftGroup group = RaftGroup.valueOf(cluster.getGroupId(), peer);
    try (final RaftClient client = cluster.createClient()) {
        Assert.assertEquals(group, cluster.getDivision(peerId).getGroup());
        try {
            client.getGroupManagementApi(peer.getId()).add(group);
        } catch (IOException ex) {
            // HadoopRPC throws RemoteException, which makes it hard to check if
            // the exception is instance of AlreadyExistsException
            Assert.assertTrue(ex.toString().contains(AlreadyExistsException.class.getCanonicalName()));
        }
        Assert.assertEquals(group, cluster.getDivision(peerId).getGroup());
        cluster.shutdown();
    }
}
Also used : AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroup(org.apache.ratis.protocol.RaftGroup) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

AlreadyExistsException (org.apache.ratis.protocol.exceptions.AlreadyExistsException)4 IOException (java.io.IOException)3 AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)2 GroupMismatchException (org.apache.ratis.protocol.exceptions.GroupMismatchException)2 TimeDuration (org.apache.ratis.util.TimeDuration)2 Closeable (java.io.Closeable)1 File (java.io.File)1 InterruptedIOException (java.io.InterruptedIOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 java.util.concurrent (java.util.concurrent)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Predicate (java.util.function.Predicate)1