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);
}
}
}
});
}
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);
}
}
}
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;
}
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();
}
}
Aggregations