Search in sources :

Example 61 with PravegaNodeUri

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

the class ConnectionPoolingTest method testConnectionPooling.

@Test
public void testConnectionPooling() throws Exception {
    ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create((this.ssl ? "tls://" : "tcp://") + "localhost")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).maxConnectionsPerSegmentStore(1).build();
    @Cleanup SocketConnectionFactoryImpl factory = new SocketConnectionFactoryImpl(clientConfig, 1);
    @Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(clientConfig, factory);
    ArrayBlockingQueue<WireCommands.SegmentRead> msgRead = new ArrayBlockingQueue<>(10);
    FailingReplyProcessor rp = new FailingReplyProcessor() {

        @Override
        public void connectionDropped() {
        }

        @Override
        public void segmentRead(WireCommands.SegmentRead data) {
            msgRead.add(data);
        }

        @Override
        public void processingFailure(Exception error) {
        }

        @Override
        public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
        }
    };
    Flow flow1 = new Flow(1, 0);
    @Cleanup ClientConnection connection1 = connectionPool.getClientConnection(flow1, new PravegaNodeUri("localhost", port), rp).join();
    connection1.send(readRequestGenerator.apply(flow1.asLong()));
    WireCommands.SegmentRead msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
    assertEquals(1, connectionPool.getActiveChannels().size());
    // create a second connection, since the max number of connections is 1 this should reuse the same connection.
    Flow flow2 = new Flow(2, 0);
    CompletableFuture<ClientConnection> cf = new CompletableFuture<>();
    connectionPool.getClientConnection(flow2, new PravegaNodeUri("localhost", port), rp, cf);
    @Cleanup ClientConnection connection2 = cf.join();
    // send data over connection2 and verify.
    connection2.send(readRequestGenerator.apply(flow2.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
    assertEquals(1, connectionPool.getActiveChannels().size());
    assertEquals(1, factory.getOpenSocketCount());
    // send data over connection1 and verify.
    connection1.send(readRequestGenerator.apply(flow1.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
    // send data over connection2 and verify.
    connection2.send(readRequestGenerator.apply(flow2.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
    // close a client connection, this should not close the channel.
    connection2.close();
    assertThrows(ConnectionFailedException.class, () -> connection2.send(readRequestGenerator.apply(flow2.asLong())));
    // verify we are able to send data over connection1.
    connection1.send(readRequestGenerator.apply(flow1.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
    // close connection1
    connection1.close();
    assertThrows(ConnectionFailedException.class, () -> connection1.send(readRequestGenerator.apply(flow2.asLong())));
    AssertExtensions.assertEventuallyEquals(0, () -> {
        connectionPool.pruneUnusedConnections();
        return factory.getOpenSocketCount();
    }, 10000);
    assertEquals(0, connectionPool.getActiveChannels().size());
}
Also used : FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) Cleanup(lombok.Cleanup) SSLException(javax.net.ssl.SSLException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ClientConfig(io.pravega.client.ClientConfig) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 62 with PravegaNodeUri

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

the class ConnectionPoolingTest method testNonPooling.

@Test
public void testNonPooling() throws Exception {
    ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create((this.ssl ? "tls://" : "tcp://") + "localhost")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).maxConnectionsPerSegmentStore(1).build();
    @Cleanup SocketConnectionFactoryImpl factory = new SocketConnectionFactoryImpl(clientConfig, 1);
    @Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(clientConfig, factory);
    ArrayBlockingQueue<WireCommands.SegmentRead> msgRead = new ArrayBlockingQueue<>(10);
    FailingReplyProcessor rp = new FailingReplyProcessor() {

        @Override
        public void connectionDropped() {
        }

        @Override
        public void segmentRead(WireCommands.SegmentRead data) {
            msgRead.add(data);
        }

        @Override
        public void processingFailure(Exception error) {
        }

        @Override
        public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
        }
    };
    Flow flow1 = new Flow(1, 0);
    @Cleanup ClientConnection connection1 = connectionPool.getClientConnection(flow1, new PravegaNodeUri("localhost", port), rp).join();
    connection1.send(readRequestGenerator.apply(flow1.asLong()));
    WireCommands.SegmentRead msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
    assertEquals(1, connectionPool.getActiveChannels().size());
    // create a second connection, since not using a flow.
    @Cleanup ClientConnection connection2 = connectionPool.getClientConnection(new PravegaNodeUri("localhost", port), rp).join();
    Flow flow2 = new Flow(2, 0);
    // send data over connection2 and verify.
    connection2.send(readRequestGenerator.apply(flow2.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
    assertEquals(1, connectionPool.getActiveChannels().size());
    assertEquals(2, factory.getOpenSocketCount());
    // send data over connection1 and verify.
    connection1.send(readRequestGenerator.apply(flow1.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
    // send data over connection2 and verify.
    connection2.send(readRequestGenerator.apply(flow2.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
    // close a client connection, this should not close the channel.
    connection2.close();
    assertThrows(ConnectionFailedException.class, () -> connection2.send(readRequestGenerator.apply(flow2.asLong())));
    // verify we are able to send data over connection1.
    connection1.send(readRequestGenerator.apply(flow1.asLong()));
    msg = msgRead.take();
    assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
    // close connection1
    connection1.close();
    assertThrows(ConnectionFailedException.class, () -> connection1.send(readRequestGenerator.apply(flow2.asLong())));
    AssertExtensions.assertEventuallyEquals(0, () -> {
        connectionPool.pruneUnusedConnections();
        return factory.getOpenSocketCount();
    }, 10000);
    assertEquals(0, connectionPool.getActiveChannels().size());
}
Also used : FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) Cleanup(lombok.Cleanup) SSLException(javax.net.ssl.SSLException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ClientConfig(io.pravega.client.ClientConfig) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 63 with PravegaNodeUri

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

the class ConnectionPoolingTest method testConcurrentRequests.

@Test
public void testConcurrentRequests() throws Exception {
    ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create((this.ssl ? "tls://" : "tcp://") + "localhost")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).maxConnectionsPerSegmentStore(1).build();
    @Cleanup SocketConnectionFactoryImpl factory = new SocketConnectionFactoryImpl(clientConfig, 1);
    @Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(clientConfig, factory);
    ArrayBlockingQueue<WireCommands.SegmentRead> msgRead = new ArrayBlockingQueue<>(10);
    FailingReplyProcessor rp = new FailingReplyProcessor() {

        @Override
        public void connectionDropped() {
        }

        @Override
        public void segmentRead(WireCommands.SegmentRead data) {
            msgRead.add(data);
        }

        @Override
        public void processingFailure(Exception error) {
        }

        @Override
        public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
        }
    };
    Flow flow1 = new Flow(1, 0);
    ClientConnection connection1 = connectionPool.getClientConnection(flow1, new PravegaNodeUri("localhost", port), rp).join();
    // create a second connection, since the max number of connections is 1 this should reuse the same connection.
    Flow flow2 = new Flow(2, 0);
    ClientConnection connection2 = connectionPool.getClientConnection(flow2, new PravegaNodeUri("localhost", port), rp).join();
    assertEquals(1, factory.getOpenSocketCount());
    assertEquals(1, connectionPool.getActiveChannels().size());
    connection1.send(readRequestGenerator.apply(flow1.asLong()));
    connection2.send(readRequestGenerator.apply(flow2.asLong()));
    List<WireCommands.SegmentRead> msgs = new ArrayList<WireCommands.SegmentRead>();
    msgs.add(msgRead.take());
    msgs.add(msgRead.take());
    assertTrue(msgs.contains(readResponseGenerator.apply(flow1.asLong())));
    assertTrue(msgs.contains(readResponseGenerator.apply(flow1.asLong())));
    assertEquals(1, factory.getOpenSocketCount());
    connection1.close();
    connection2.close();
    AssertExtensions.assertEventuallyEquals(0, () -> {
        connectionPool.pruneUnusedConnections();
        return factory.getOpenSocketCount();
    }, 10000);
    assertEquals(0, connectionPool.getActiveChannels().size());
}
Also used : FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) SSLException(javax.net.ssl.SSLException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ClientConfig(io.pravega.client.ClientConfig) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 64 with PravegaNodeUri

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

the class ConditionalOutputStreamTest method testRetriesOnTokenExpiry.

@SneakyThrows
@Test(timeout = 10000)
public void testRetriesOnTokenExpiry() {
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
    ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    Segment segment = new Segment("scope", "testWrite", 1);
    @Cleanup ConditionalOutputStream objectUnderTest = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
    ByteBuffer data = ByteBuffer.allocate(10);
    ClientConnection clientConnection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, clientConnection);
    setupAppend(connectionFactory, segment, clientConnection, location);
    final AtomicLong retryCounter = new AtomicLong(0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0);
            ReplyProcessor processor = connectionFactory.getProcessor(location);
            if (retryCounter.getAndIncrement() < 2) {
                processor.process(new WireCommands.AuthTokenCheckFailed(argument.getRequestId(), "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED));
            } else {
                processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
            }
            return null;
        }
    }).when(clientConnection).send(any(ConditionalAppend.class));
    assertTrue(objectUnderTest.write(data, 0));
    assertEquals(3, retryCounter.get());
}
Also used : ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) AtomicLong(java.util.concurrent.atomic.AtomicLong) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test) SneakyThrows(lombok.SneakyThrows)

Example 65 with PravegaNodeUri

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

the class ConditionalOutputStreamTest method testWrite.

@Test(timeout = 10000)
public void testWrite() throws ConnectionFailedException, SegmentSealedException {
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
    ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    Segment segment = new Segment("scope", "testWrite", 1);
    @Cleanup ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
    ByteBuffer data = ByteBuffer.allocate(10);
    ClientConnection mock = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    connectionFactory.provideConnection(location, mock);
    setupAppend(connectionFactory, segment, mock, location);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0);
            ReplyProcessor processor = connectionFactory.getProcessor(location);
            if (argument.getExpectedOffset() == 0 || argument.getExpectedOffset() == 2) {
                processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
            } else {
                processor.process(new WireCommands.ConditionalCheckFailed(argument.getWriterId(), argument.getEventNumber(), argument.getRequestId()));
            }
            return null;
        }
    }).when(mock).send(any(ConditionalAppend.class));
    assertTrue(cOut.write(data, 0));
    assertFalse(cOut.write(data, 1));
    assertTrue(cOut.write(data, 2));
    assertFalse(cOut.write(data, 3));
}
Also used : ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) Cleanup(lombok.Cleanup) ByteBuffer(java.nio.ByteBuffer) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Aggregations

PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)185 Test (org.junit.Test)154 Cleanup (lombok.Cleanup)124 MockController (io.pravega.client.stream.mock.MockController)122 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)119 ClientConnection (io.pravega.client.connection.impl.ClientConnection)81 WireCommands (io.pravega.shared.protocol.netty.WireCommands)70 UUID (java.util.UUID)57 Segment (io.pravega.client.segment.impl.Segment)49 CompletableFuture (java.util.concurrent.CompletableFuture)44 InvocationOnMock (org.mockito.invocation.InvocationOnMock)44 ReplyProcessor (io.pravega.shared.protocol.netty.ReplyProcessor)40 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)35 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)35 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)35 ByteBuffer (java.nio.ByteBuffer)34 Append (io.pravega.shared.protocol.netty.Append)33 AtomicLong (java.util.concurrent.atomic.AtomicLong)31 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)30 InOrder (org.mockito.InOrder)28