org.apache.ratis.protocol.AlreadyClosedException Java Examples

The following examples show how to use org.apache.ratis.protocol.AlreadyClosedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SlidingWindow.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
void endOfRequests(long nextToProcess, REQUEST end, Consumer<REQUEST> replyMethod) {
  final REQUEST nextToProcessRequest = requests.get(nextToProcess);
  Preconditions.assertNull(nextToProcessRequest,
      () -> "nextToProcessRequest = " + nextToProcessRequest + " != null, nextToProcess = " + nextToProcess);

  final SortedMap<Long, REQUEST> tail = requests.tailMap(nextToProcess);
  for (REQUEST r : tail.values()) {
    final AlreadyClosedException e = new AlreadyClosedException(
        getName() + " is closing: seq = " + r.getSeqNum() + " > nextToProcess = " + nextToProcess
            + " will NEVER be processed; request = " + r);
    r.fail(e);
    replyMethod.accept(r);
  }
  tail.clear();

  putNewRequest(end);
}
 
Example #2
Source File: GrpcClientProtocolClient.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
CompletableFuture<RaftClientReply> onNext(RaftClientRequest request) {
  final long callId = request.getCallId();
  final CompletableFuture<RaftClientReply> f = replies.putNew(callId);
  if (f == null) {
    return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + " is closed."));
  }
  try {
    if (!requestStreamer.onNext(ClientProtoUtils.toRaftClientRequestProto(request))) {
      return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + ": the stream is closed."));
    }
  } catch(Throwable t) {
    handleReplyFuture(request.getCallId(), future -> future.completeExceptionally(t));
    return f;
  }

  if (RaftClientRequestProto.TypeCase.WATCH.equals(request.getType().getTypeCase())) {
    scheduler.onTimeout(watchRequestTimeoutDuration, () ->
            timeoutCheck(callId, watchRequestTimeoutDuration), LOG,
        () -> "Timeout check failed for client request #" + callId);
  } else {
    scheduler.onTimeout(requestTimeoutDuration,
        () -> timeoutCheck(callId, requestTimeoutDuration), LOG,
        () -> "Timeout check failed for client request #" + callId);
  }
  return f;
}
 
Example #3
Source File: WatchRequestTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static void runTestWatchRequestClientTimeout(TestParameters p) throws Exception {
  final Logger LOG = p.log;

  CompletableFuture<RaftClientReply> watchReply;
  // watch 1000 which will never be committed
  // so client can not receive reply, and connection closed, throw TimeoutException.
  // We should not retry, because if retry, RaftClientImpl::handleIOException will random select a leader,
  // then sometimes throw NotLeaderException.
  watchReply = p.sendWatchRequest(1000, RetryPolicies.noRetry());

  try {
    watchReply.get();
    fail("runTestWatchRequestClientTimeout failed");
  } catch (Exception ex) {
    LOG.error("error occurred", ex);
    Assert.assertTrue(ex.getCause().getClass() == AlreadyClosedException.class ||
        ex.getCause().getClass() == RaftRetryFailureException.class);
    if (ex.getCause() != null) {
      if (ex.getCause().getCause() != null) {
        Assert.assertEquals(TimeoutIOException.class,
            ex.getCause().getCause().getClass());
      }
    }
  }
}
 
Example #4
Source File: GrpcClientProtocolClient.java    From ratis with Apache License 2.0 6 votes vote down vote up
CompletableFuture<RaftClientReply> onNext(RaftClientRequest request) {
  final Map<Long, CompletableFuture<RaftClientReply>> map = replies.get();
  if (map == null) {
    return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + " is closed."));
  }
  final CompletableFuture<RaftClientReply> f = new CompletableFuture<>();
  CollectionUtils.putNew(request.getCallId(), f, map,
      () -> getName() + ":" + getClass().getSimpleName());
  try {
    requestStreamObserver.onNext(ClientProtoUtils.toRaftClientRequestProto(request));
    scheduler.onTimeout(requestTimeoutDuration, () -> timeoutCheck(request), LOG,
        () -> "Timeout check failed for client request: " + request);
  } catch(Throwable t) {
    handleReplyFuture(request.getCallId(), future -> future.completeExceptionally(t));
  }
  return f;
}
 
Example #5
Source File: PeerProxyMap.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
PROXY getProxy() throws IOException {
  if (proxy == null) {
    synchronized (this) {
      if (proxy == null) {
        final LifeCycle.State current = lifeCycle.getCurrentState();
        if (current.isClosingOrClosed()) {
          throw new AlreadyClosedException(name + " is already " + current);
        }
        lifeCycle.startAndTransition(
            () -> proxy = createProxy.apply(peer), IOException.class);
      }
    }
  }
  return proxy;
}
 
Example #6
Source File: GrpcClientProtocolClient.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void completeReplyExceptionally(Throwable t, String event) {
  final Map<Long, CompletableFuture<RaftClientReply>> map = replies.getAndSetNull();
  if (map == null) {
    return;
  }
  for (Map.Entry<Long, CompletableFuture<RaftClientReply>> entry : map.entrySet()) {
    final CompletableFuture<RaftClientReply> f = entry.getValue();
    if (!f.isDone()) {
      f.completeExceptionally(t != null? t
          : new AlreadyClosedException(getName() + ": Stream " + event
              + ": no reply for async request cid=" + entry.getKey()));
    }
  }
}
 
Example #7
Source File: RaftAsyncExceptionTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void runTestGroupMismatchException(CLUSTER cluster) throws Exception {
  // send a message to make sure the cluster is working
  try(RaftClient client = cluster.createClient()) {
    final RaftClientReply reply = client.sendAsync(new SimpleMessage("first")).get();
    Assert.assertTrue(reply.isSuccess());
  }

  // create another group
  final RaftGroup clusterGroup = cluster.getGroup();
  final RaftGroup anotherGroup = RaftGroup.valueOf(RaftGroupId.randomId(), clusterGroup.getPeers());
  Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId());

  // create another client using another group
  final SimpleMessage[] messages = SimpleMessage.create(5);
  try(RaftClient client = cluster.createClient(anotherGroup)) {
    // send a few messages
    final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
    for(SimpleMessage m : messages) {
      futures.add(client.sendAsync(m));
    }
    Assert.assertEquals(messages.length, futures.size());

    // check replies
    final Iterator<CompletableFuture<RaftClientReply>> i = futures.iterator();
    testFailureCase("First reply is GroupMismatchException",
        () -> i.next().get(),
        ExecutionException.class, GroupMismatchException.class);
    for(; i.hasNext(); ) {
      testFailureCase("Following replies are AlreadyClosedException caused by GroupMismatchException",
          () -> i.next().get(),
          ExecutionException.class, AlreadyClosedException.class, GroupMismatchException.class);
    }
  }
}
 
Example #8
Source File: TestCommitWatcher.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@Test
public void testReleaseBuffersOnException() throws Exception {
  int capacity = 2;
  BufferPool bufferPool = new BufferPool(chunkSize, capacity);
  XceiverClientManager clientManager = new XceiverClientManager(conf);
  ContainerWithPipeline container = storageContainerLocationClient
      .allocateContainer(HddsProtos.ReplicationType.RATIS,
          HddsProtos.ReplicationFactor.THREE, OzoneConsts.OZONE);
  Pipeline pipeline = container.getPipeline();
  long containerId = container.getContainerInfo().getContainerID();
  XceiverClientSpi xceiverClient = clientManager.acquireClient(pipeline);
  Assert.assertEquals(1, xceiverClient.getRefcount());
  Assert.assertTrue(xceiverClient instanceof XceiverClientRatis);
  XceiverClientRatis ratisClient = (XceiverClientRatis) xceiverClient;
  CommitWatcher watcher = new CommitWatcher(bufferPool, ratisClient);
  BlockID blockID = ContainerTestHelper.getTestBlockID(containerId);
  List<XceiverClientReply> replies = new ArrayList<>();
  long length = 0;
  List<CompletableFuture<ContainerProtos.ContainerCommandResponseProto>>
      futures = new ArrayList<>();
  for (int i = 0; i < capacity; i++) {
    ContainerProtos.ContainerCommandRequestProto writeChunkRequest =
        ContainerTestHelper
            .getWriteChunkRequest(pipeline, blockID, chunkSize, null);
    // add the data to the buffer pool
    final ChunkBuffer byteBuffer = bufferPool.allocateBufferIfNeeded(0);
    byteBuffer.put(writeChunkRequest.getWriteChunk().getData());
    ratisClient.sendCommandAsync(writeChunkRequest);
    ContainerProtos.ContainerCommandRequestProto putBlockRequest =
        ContainerTestHelper
            .getPutBlockRequest(pipeline, writeChunkRequest.getWriteChunk());
    XceiverClientReply reply = ratisClient.sendCommandAsync(putBlockRequest);
    final List<ChunkBuffer> bufferList = singletonList(byteBuffer);
    length += byteBuffer.position();
    CompletableFuture<ContainerProtos.ContainerCommandResponseProto> future =
        reply.getResponse().thenApply(v -> {
          watcher.updateCommitInfoMap(reply.getLogIndex(), bufferList);
          return v;
        });
    futures.add(future);
    watcher.getFutureMap().put(length, future);
    replies.add(reply);
  }

  Assert.assertTrue(replies.size() == 2);
  // wait on the 1st putBlock to complete
  CompletableFuture<ContainerProtos.ContainerCommandResponseProto> future1 =
      futures.get(0);
  CompletableFuture<ContainerProtos.ContainerCommandResponseProto> future2 =
      futures.get(1);
  future1.get();
  Assert.assertNotNull(watcher.getFutureMap().get(new Long(chunkSize)));
  Assert.assertTrue(
      watcher.getFutureMap().get(new Long(chunkSize)).equals(future1));
  // wait on 2nd putBlock to complete
  future2.get();
  Assert.assertNotNull(watcher.getFutureMap().get(new Long(2 * chunkSize)));
  Assert.assertTrue(
      watcher.getFutureMap().get(new Long(2 * chunkSize)).equals(future2));
  Assert.assertTrue(watcher.getCommitIndex2flushedDataMap().size() == 2);
  watcher.watchOnFirstIndex();
  Assert.assertFalse(watcher.getCommitIndex2flushedDataMap()
      .containsKey(replies.get(0).getLogIndex()));
  Assert.assertFalse(watcher.getFutureMap().containsKey(chunkSize));
  Assert.assertTrue(watcher.getTotalAckDataLength() >= chunkSize);
  cluster.shutdownHddsDatanode(pipeline.getNodes().get(0));
  cluster.shutdownHddsDatanode(pipeline.getNodes().get(1));
  try {
    // just watch for a higher index so as to ensure, it does an actual
    // call to Ratis. Otherwise, it may just return in case the commitInfoMap
    // is updated to the latest index in putBlock response.
    watcher.watchForCommit(replies.get(1).getLogIndex() + 100);
    Assert.fail("Expected exception not thrown");
  } catch(IOException ioe) {
    // with retry count set to lower limit and a lower watch request
    // timeout, watch request will eventually
    // fail with RaftRetryFailure exception from ratis client or the client
    // can itself get AlreadyClosedException from the Ratis Server
    Throwable t = HddsClientUtils.checkForException(ioe);
    Assert.assertTrue(t instanceof RaftRetryFailureException ||
            t instanceof AlreadyClosedException ||
            t instanceof NotReplicatedException);
  }
  if (ratisClient.getReplicatedMinCommitIndex() < replies.get(1)
      .getLogIndex()) {
    Assert.assertTrue(watcher.getTotalAckDataLength() == chunkSize);
    Assert.assertTrue(watcher.getCommitIndex2flushedDataMap().size() == 1);
    Assert.assertTrue(watcher.getFutureMap().size() == 1);
  } else {
    Assert.assertTrue(watcher.getTotalAckDataLength() == 2 * chunkSize);
    Assert.assertTrue(watcher.getFutureMap().isEmpty());
    Assert.assertTrue(watcher.getCommitIndex2flushedDataMap().isEmpty());
  }
}
 
Example #9
Source File: SlidingWindow.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private void alreadyClosed(REQUEST request, Throwable e) {
  request.fail(new AlreadyClosedException(requests.getName() + " is closed.", e));
}
 
Example #10
Source File: IOUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static boolean shouldReconnect(Throwable e) {
  return ReflectionUtils.isInstance(e,
      SocketException.class, SocketTimeoutException.class, ClosedChannelException.class, EOFException.class,
      AlreadyClosedException.class);
}
 
Example #11
Source File: SlidingWindow.java    From ratis with Apache License 2.0 4 votes vote down vote up
private void alreadyClosed(REQUEST request, Exception e) {
  request.fail(new AlreadyClosedException(SlidingWindow.class.getSimpleName() + "$" + getClass().getSimpleName()
      + " " + requests.getName() + " is closed.", e));
}
 
Example #12
Source File: KeyOutputStream.java    From hadoop-ozone with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if the provided exception signifies retry failure in ratis client.
 * In case of retry failure, ratis client throws RaftRetryFailureException
 * and all succeeding operations are failed with AlreadyClosedException.
 */
private boolean checkForRetryFailure(Throwable t) {
  return t instanceof RaftRetryFailureException
      || t instanceof AlreadyClosedException;
}