@Override public void connectionDropped() { closeConnection(new ConnectionFailedException()); }
@Override public void connectionDropped() { closeConnection(new ConnectionFailedException()); }
@Override public void connectionDropped() { failConnection(new ConnectionFailedException()); }
@VisibleForTesting Channel getChannel() throws ConnectionFailedException { Channel ch = channel.get(); if (ch == null) { throw new ConnectionFailedException("Connection to " + connectionName + " is not established."); } return ch; }
@Override public void operationComplete(Future<? super Void> future) throws Exception { Throwable cause = future.cause(); callback.complete(cause == null ? null : new ConnectionFailedException(cause)); } });
@Override public void wrongHost(WrongHost wrongHost) { failConnection(new ConnectionFailedException(wrongHost.toString())); }
@Override public void wrongHost(WireCommands.WrongHost wrongHost) { closeConnection(new ConnectionFailedException(wrongHost.toString())); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { processor.processingFailure(new ConnectionFailedException(cause)); }
@SuppressWarnings("unchecked") @SneakyThrows(ConnectionFailedException.class) private <T extends Reply> T transformReply(Reply reply, Class<T> klass) { if (klass.isAssignableFrom(reply.getClass())) { return (T) reply; } closeConnection(reply); if (reply instanceof WireCommands.NoSuchSegment) { throw new NoSuchSegmentException(reply.toString()); } else if (reply instanceof WrongHost) { throw new ConnectionFailedException(reply.toString()); } else { throw new ConnectionFailedException("Unexpected reply of " + reply + " when expecting a " + klass.getName()); } }
@Override public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { //since ChannelFuture is complete future.channel() is not a blocking call. Channel ch = future.channel(); log.debug("Connect operation completed for channel:{}, local address:{}, remote address:{}", ch.id(), ch.localAddress(), ch.remoteAddress()); channelGroup.add(ch); // Once a channel is closed the channel group implementation removes it. connectionComplete.complete(handler); } else { connectionComplete.completeExceptionally(new ConnectionFailedException(future.cause())); } } });
private RuntimeException handelUnexpectedReply(Reply reply) { closeConnection(reply.toString()); if (reply instanceof WireCommands.NoSuchSegment) { throw new NoSuchSegmentException(reply.toString()); } else if (reply instanceof SegmentIsSealed) { throw Exceptions.sneakyThrow(new SegmentSealedException(reply.toString())); } else if (reply instanceof WrongHost) { throw Exceptions.sneakyThrow(new ConnectionFailedException(reply.toString())); } else { throw Exceptions.sneakyThrow(new ConnectionFailedException("Unexpected reply of " + reply + " when expecting an AppendSetup")); } }
@Override public void sendAsync(WireCommand cmd, CompletedCallback callback) { recentMessage.set(true); try { Channel channel = getChannel(); channel.writeAndFlush(cmd) .addListener((Future<? super Void> f) -> { if (f.isSuccess()) { callback.complete(null); } else { callback.complete(new ConnectionFailedException(f.cause())); } }); } catch (ConnectionFailedException cfe) { callback.complete(cfe); } catch (RuntimeException e) { callback.complete(new ConnectionFailedException(e)); } }
@Override public Void answer(InvocationOnMock invocation) throws Throwable { ClientConnection.CompletedCallback callback = invocation.getArgument(1); callback.complete(new ConnectionFailedException()); return null; } }).when(connection1).sendAsync(Mockito.eq(getSegmentInfo1), Mockito.any(ClientConnection.CompletedCallback.class));
private <ResultT> void sendRequestAsync(final WireCommand request, final ReplyProcessor replyProcessor, final CompletableFuture<ResultT> resultFuture, final ConnectionFactory connectionFactory, final PravegaNodeUri uri) { CompletableFuture<ClientConnection> connectionFuture = connectionFactory.establishConnection(uri, replyProcessor); connectionFuture.whenComplete((connection, e) -> { if (connection == null) { resultFuture.completeExceptionally(new WireCommandFailedException(new ConnectionFailedException(e), request.getType(), WireCommandFailedException.Reason.ConnectionFailed)); } else { connection.sendAsync(request, cfe -> { if (cfe != null) { Throwable cause = Exceptions.unwrap(cfe); if (cause instanceof ConnectionFailedException) { resultFuture.completeExceptionally(new WireCommandFailedException(cause, request.getType(), WireCommandFailedException.Reason.ConnectionFailed)); } else { resultFuture.completeExceptionally(new RuntimeException(cause)); } } }); } }); resultFuture.whenComplete((result, e) -> { connectionFuture.thenAccept(ClientConnection::close); }); }
@Override public CompletableFuture<SegmentRead> read(long offset, int length) { Exceptions.checkNotClosed(closed.get(), this); WireCommands.ReadSegment request = new WireCommands.ReadSegment(segmentId.getScopedName(), offset, length, this.delegationToken); return backoffSchedule.retryWhen(t -> { Throwable ex = Exceptions.unwrap(t); if (closed.get()) { log.debug("Exception while reading from Segment : {}", segmentId, ex); } else { log.warn("Exception while reading from Segment : {}", segmentId, ex); } return ex instanceof Exception && !(ex instanceof ConnectionClosedException) && !(ex instanceof SegmentTruncatedException); }).runAsync(() -> { return getConnection() .whenComplete((connection, ex) -> { if (ex != null) { log.warn("Exception while establishing connection with Pravega " + "node", ex); closeConnection(new ConnectionFailedException(ex)); } }).thenCompose(c -> sendRequestOverConnection(request, c)); }, connectionFactory.getInternalExecutor()); }
@Override public Void answer(InvocationOnMock invocation) throws Throwable { CompletedCallback callback = (CompletedCallback) invocation.getArgument(1); if (failed) { callback.complete(null); } else { failed = true; callback.complete(new ConnectionFailedException("Injected")); } return null; } }).when(connection).sendAsync(Mockito.any(List.class), Mockito.any(CompletedCallback.class));
@Override public Void answer(InvocationOnMock invocation) throws Throwable { cf.getProcessor(uri).connectionDropped(); throw new ConnectionFailedException(); } }).when(connection).send(append);
@Override public Void answer(InvocationOnMock invocation) throws Throwable { cf.getProcessor(uri).connectionDropped(); throw new ConnectionFailedException(); } }).doNothing().when(connection).send(new WireCommands.KeepAlive());
connectionComplete.completeExceptionally(new ConnectionFailedException(future.cause())); connectionComplete.completeExceptionally(new ConnectionFailedException(e));
@Override public void sendAsync(List<Append> appends, CompletedCallback callback) { recentMessage.set(true); Channel ch; try { ch = getChannel(); } catch (ConnectionFailedException e) { callback.complete(new ConnectionFailedException("Connection to " + connectionName + " is not established.")); return; } PromiseCombiner combiner = new PromiseCombiner(); for (Append append : appends) { batchSizeTracker.recordAppend(append.getEventNumber(), append.getData().readableBytes()); combiner.add(ch.write(append)); } ch.flush(); ChannelPromise promise = ch.newPromise(); promise.addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(Future<? super Void> future) throws Exception { Throwable cause = future.cause(); callback.complete(cause == null ? null : new ConnectionFailedException(cause)); } }); combiner.finish(promise); }