@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));
@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());
@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); }
@Test(timeout = 10000) public void testTimeout() throws EndOfSegmentException, SegmentTruncatedException { byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 7); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertBlocks(() -> stream.read(), () -> fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); ByteBuffer read = stream.read(10); assertNull(read); fakeNetwork.completeExceptionally(1, new ConnectionFailedException()); AssertExtensions.assertThrows(ConnectionFailedException.class, () -> stream.read()); stream.read(10); assertNull(read); read = stream.read(10); assertNull(read); }
@Test public void testExceptionRecovery() throws EndOfSegmentException, SegmentTruncatedException { byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 7); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, 2))); fakeNetwork.completeExceptionally(1, new ConnectionFailedException()); fakeNetwork.complete(2, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, 2))); fakeNetwork.complete(3, new WireCommands.SegmentRead(segment.getScopedName(), 2, false, false, ByteBufferUtils.slice(wireData, 2, 7))); fakeNetwork.complete(4, new WireCommands.SegmentRead(segment.getScopedName(), 9, false, false, ByteBufferUtils.slice(wireData, 9, 2))); fakeNetwork.complete(5, new WireCommands.SegmentRead(segment.getScopedName(), 11, false, false, ByteBufferUtils.slice(wireData, 11, wireData.capacity() - 11))); AssertExtensions.assertThrows(ConnectionFailedException.class, () -> stream.read()); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data), read); }