Search in sources :

Example 6 with ConnectionFailedException

use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.

the class SegmentOutputStreamTest method testFailDurringFlush.

@Test(timeout = 10000)
public void testFailDurringFlush() throws ConnectionFailedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    InOrder order = Mockito.inOrder(connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    order.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Boolean> ack = new CompletableFuture<>();
    output.write(new PendingEvent(null, data, ack));
    order.verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
    assertEquals(false, ack.isDone());
    Mockito.doThrow(new ConnectionFailedException()).when(connection).send(new WireCommands.KeepAlive());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).appendSetup(new AppendSetup(3, SEGMENT, cid, 1));
            return null;
        }
    }).when(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
    Async.testBlocking(() -> {
        output.flush();
    }, () -> {
        cf.getProcessor(uri).connectionDropped();
    });
    order.verify(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
    assertEquals(true, ack.isDone());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InOrder(org.mockito.InOrder) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Example 7 with ConnectionFailedException

use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.

the class SegmentHelper method sendRequestAsync.

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 {
            try {
                connection.send(request);
            } catch (ConnectionFailedException cfe) {
                throw new WireCommandFailedException(cfe, request.getType(), WireCommandFailedException.Reason.ConnectionFailed);
            } catch (Exception e2) {
                throw new RuntimeException(e2);
            }
        }
    }).exceptionally(e -> {
        Throwable cause = Exceptions.unwrap(e);
        if (cause instanceof WireCommandFailedException) {
            resultFuture.completeExceptionally(cause);
        } else if (cause instanceof ConnectionFailedException) {
            resultFuture.completeExceptionally(new WireCommandFailedException(cause, request.getType(), WireCommandFailedException.Reason.ConnectionFailed));
        } else {
            resultFuture.completeExceptionally(new RuntimeException(cause));
        }
        return null;
    });
    resultFuture.whenComplete((result, e) -> {
        connectionFuture.thenAccept(ClientConnection::close);
    });
}
Also used : ClientConnection(io.pravega.client.netty.impl.ClientConnection) AuthenticationException(io.pravega.common.auth.AuthenticationException) Segment(io.pravega.client.segment.impl.Segment) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Exceptions(io.pravega.common.Exceptions) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) Pair(org.apache.commons.lang3.tuple.Pair) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) WireCommandType(io.pravega.shared.protocol.netty.WireCommandType) ModelHelper(io.pravega.client.stream.impl.ModelHelper) Host(io.pravega.common.cluster.Host) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) WireCommand(io.pravega.shared.protocol.netty.WireCommand) AtomicLong(java.util.concurrent.atomic.AtomicLong) Slf4j(lombok.extern.slf4j.Slf4j) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) TxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.TxnStatus) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) ClientConnection(io.pravega.client.netty.impl.ClientConnection) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) AuthenticationException(io.pravega.common.auth.AuthenticationException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException)

Example 8 with ConnectionFailedException

use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.

the class ClientConnectionInboundHandler method sendAsync.

@Override
public void sendAsync(WireCommand cmd) throws ConnectionFailedException {
    recentMessage.set(true);
    Channel channel = getChannel();
    try {
        channel.writeAndFlush(cmd, channel.voidPromise());
    } catch (RuntimeException e) {
        throw new ConnectionFailedException(e);
    }
}
Also used : Channel(io.netty.channel.Channel) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException)

Example 9 with ConnectionFailedException

use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.

the class SegmentOutputStreamImpl method write.

/**
 * @see SegmentOutputStream#write(PendingEvent)
 */
@Override
public void write(PendingEvent event) {
    checkState(!state.isAlreadySealed(), "Segment: %s is already sealed", segmentName);
    synchronized (writeOrderLock) {
        ClientConnection connection;
        try {
            // if connection is null getConnection() establishes a connection and retransmits all events in inflight
            // list.
            connection = Futures.getThrowingException(getConnection());
        } catch (SegmentSealedException e) {
            // Add the event to inflight and indicate to the caller that the segment is sealed.
            state.addToInflight(event);
            return;
        }
        long eventNumber = state.addToInflight(event);
        try {
            Append append = new Append(segmentName, writerId, eventNumber, Unpooled.wrappedBuffer(event.getData()), event.getExpectedOffset());
            log.trace("Sending append request: {}", append);
            connection.send(append);
        } catch (ConnectionFailedException e) {
            log.warn("Connection " + writerId + " failed due to: ", e);
            // As the message is inflight, this will perform the retransmission.
            reconnect();
        }
    }
}
Also used : Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ClientConnection(io.pravega.client.netty.impl.ClientConnection) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException)

Example 10 with ConnectionFailedException

use of io.pravega.shared.protocol.netty.ConnectionFailedException in project pravega by pravega.

the class SegmentInputStreamTest method testTimeout.

@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 SegmentInputStreamImpl stream = new SegmentInputStreamImpl(fakeNetwork, 0);
    testBlocking(() -> 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);
}
Also used : SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) ByteBuffer(java.nio.ByteBuffer) Cleanup(lombok.Cleanup) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Aggregations

ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)14 CompletableFuture (java.util.concurrent.CompletableFuture)10 ClientConnection (io.pravega.client.netty.impl.ClientConnection)9 Append (io.pravega.shared.protocol.netty.Append)8 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)8 Test (org.junit.Test)8 WireCommands (io.pravega.shared.protocol.netty.WireCommands)7 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)7 UUID (java.util.UUID)7 PendingEvent (io.pravega.client.stream.impl.PendingEvent)6 MockController (io.pravega.client.stream.mock.MockController)6 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)6 ByteBuffer (java.nio.ByteBuffer)6 Cleanup (lombok.Cleanup)6 InOrder (org.mockito.InOrder)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 CompletedCallback (io.pravega.client.netty.impl.ClientConnection.CompletedCallback)4 Channel (io.netty.channel.Channel)3