Search in sources :

Example 21 with ConnectionPoolImpl

use of io.pravega.client.connection.impl.ConnectionPoolImpl in project pravega by pravega.

the class StreamManagerImplTest method testListScopes.

@Test(timeout = 10000)
public void testListScopes() throws ConnectionFailedException {
    // Setup Mocks
    ClientConnection connection = mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
    connectionFactory.provideConnection(location, connection);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true);
    ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    @Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
    String scope = "scope";
    String scope1 = "scope1";
    String scope2 = "scope2";
    String scope3 = "scope3";
    String stream1 = "stream1";
    streamManager.createScope(scope);
    streamManager.createScope(scope1);
    streamManager.createScope(scope2);
    streamManager.createScope(scope3);
    streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    ArrayList<String> result = Lists.newArrayList(streamManager.listScopes());
    assertEquals(result.size(), 4);
    assertTrue(streamManager.checkScopeExists(scope));
    assertFalse(streamManager.checkScopeExists("nonExistent"));
    assertTrue(streamManager.checkStreamExists(scope, stream1));
    assertFalse(streamManager.checkStreamExists(scope, "nonExistent"));
    assertFalse(streamManager.checkStreamExists("nonExistent", "nonExistent"));
}
Also used : ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) Cleanup(lombok.Cleanup) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamManager(io.pravega.client.admin.StreamManager) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 22 with ConnectionPoolImpl

use of io.pravega.client.connection.impl.ConnectionPoolImpl in project pravega by pravega.

the class StreamManagerImplTest method testStreamInfo.

@Test(timeout = 15000)
public void testStreamInfo() throws Exception {
    final String streamName = "stream";
    final Stream stream = new StreamImpl(defaultScope, streamName);
    // Setup Mocks
    ClientConnection connection = mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
    connectionFactory.provideConnection(location, connection);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true);
    ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    @Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
    streamManager.createScope(defaultScope);
    streamManager.createStream(defaultScope, streamName, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    // fetch StreamInfo.
    StreamInfo info = streamManager.getStreamInfo(defaultScope, streamName);
    // validate results.
    assertEquals(defaultScope, info.getScope());
    assertEquals(streamName, info.getStreamName());
    assertNotNull(info.getTailStreamCut());
    assertEquals(stream, info.getTailStreamCut().asImpl().getStream());
    assertEquals(3, info.getTailStreamCut().asImpl().getPositions().size());
    assertNotNull(info.getHeadStreamCut());
    assertEquals(stream, info.getHeadStreamCut().asImpl().getStream());
    assertEquals(3, info.getHeadStreamCut().asImpl().getPositions().size());
    assertFalse(info.isSealed());
}
Also used : ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) Cleanup(lombok.Cleanup) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) StreamImpl(io.pravega.client.stream.impl.StreamImpl) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamManager(io.pravega.client.admin.StreamManager) StreamInfo(io.pravega.client.admin.StreamInfo) MockController(io.pravega.client.stream.mock.MockController) Stream(io.pravega.client.stream.Stream) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 23 with ConnectionPoolImpl

use of io.pravega.client.connection.impl.ConnectionPoolImpl in project pravega by pravega.

the class StreamManagerImplTest method testConnectionPoolConfig.

@Test
public void testConnectionPoolConfig() {
    ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create("tcp://localhost:" + TestUtils.getAvailableListenPort())).build();
    @Cleanup StreamManagerImpl streamManager = new StreamManagerImpl(clientConfig);
    ConnectionPoolImpl connectionPool = (ConnectionPoolImpl) streamManager.getConnectionPool();
    Assert.assertEquals(clientConfig, connectionPool.getClientConfig());
}
Also used : ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) ClientConfig(io.pravega.client.ClientConfig) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 24 with ConnectionPoolImpl

use of io.pravega.client.connection.impl.ConnectionPoolImpl in project pravega by pravega.

the class StreamManagerImplTest method testForceDeleteScopeWithReaderGroups.

@Test
public void testForceDeleteScopeWithReaderGroups() throws ConnectionFailedException, DeleteScopeFailedException {
    // Setup Mocks
    ClientConnection connection = mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.DeleteSegment request = (WireCommands.DeleteSegment) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.SegmentDeleted(request.getRequestId(), request.getSegment()));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.DeleteSegment.class));
    connectionFactory.provideConnection(location, connection);
    MockController mockController = spy(new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true));
    ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    @Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
    String scope = "scope";
    String stream1 = "stream1";
    String stream2 = "stream2";
    String readerGroup1 = "readerGroup1";
    String readerGroup2 = "readerGroup2";
    ReaderGroupConfig config1 = ReaderGroupConfig.builder().stream(NameUtils.getScopedStreamName(scope, stream1)).build();
    ReaderGroupConfig config2 = ReaderGroupConfig.builder().stream(NameUtils.getScopedStreamName(scope, stream2)).build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    streamManager.createStream(scope, stream2, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    Set<Stream> streams = Sets.newHashSet(streamManager.listStreams(scope));
    assertEquals(2, streams.size());
    assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream1)));
    assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream2)));
    mockController.createReaderGroup(scope, readerGroup1, config1);
    mockController.createReaderGroup(scope, readerGroup2, config2);
    // mock controller client to throw exceptions when attempting to get config for reader-group.
    doAnswer(x -> Futures.failedFuture(new ControllerFailureException("Unable to access reader-group config"))).when(mockController).getReaderGroupConfig(scope, readerGroup1);
    doAnswer(x -> new AsyncIterator<Stream>() {

        final Iterator<Stream> iterator = new ArrayList<Stream>(Arrays.asList(new StreamImpl(scope, stream1), new StreamImpl(scope, stream2), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup1)), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup2)))).iterator();

        @Override
        public CompletableFuture<Stream> getNext() {
            Stream next;
            if (!iterator.hasNext()) {
                next = null;
            } else {
                next = iterator.next();
            }
            return CompletableFuture.completedFuture(next);
        }
    }).when(mockController).listStreams(scope);
    AssertExtensions.assertThrows("Should have thrown exception", () -> streamManager.deleteScope(scope, true), e -> Exceptions.unwrap(e) instanceof DeleteScopeFailedException);
    // reset mock controller
    reset(mockController);
    streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    streamManager.createStream(scope, stream2, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    streams = Sets.newHashSet(streamManager.listStreams(scope));
    assertEquals(2, streams.size());
    assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream1)));
    assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream2)));
    mockController.createReaderGroup(scope, readerGroup1, config1);
    mockController.createReaderGroup(scope, readerGroup2, config2);
    // mock controller client to throw exceptions when attempting to delete the reader-group.
    doAnswer(x -> Futures.failedFuture(new ControllerFailureException("Unable to delete reader-group"))).when(mockController).deleteReaderGroup(scope, readerGroup1, config1.getReaderGroupId());
    doAnswer(x -> new AsyncIterator<Stream>() {

        final Iterator<Stream> iterator = new ArrayList<Stream>(Arrays.asList(new StreamImpl(scope, stream1), new StreamImpl(scope, stream2), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup1)), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup2)))).iterator();

        @Override
        public CompletableFuture<Stream> getNext() {
            Stream next;
            if (!iterator.hasNext()) {
                next = null;
            } else {
                next = iterator.next();
            }
            return CompletableFuture.completedFuture(next);
        }
    }).when(mockController).listStreams(scope);
    AssertExtensions.assertThrows("Should have thrown exception", () -> streamManager.deleteScope(scope, true), e -> Exceptions.unwrap(e) instanceof DeleteScopeFailedException);
    // reset mock controller
    reset(mockController);
    streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    streamManager.createStream(scope, stream2, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    streams = Sets.newHashSet(streamManager.listStreams(scope));
    assertEquals(2, streams.size());
    assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream1)));
    assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream2)));
    mockController.createReaderGroup(scope, readerGroup1, config1);
    mockController.createReaderGroup(scope, readerGroup2, config2);
    // mock controller client to throw ReaderGroupNotFoundException when attempting to get the config of reader-group.
    doAnswer(x -> Futures.failedFuture(new ReaderGroupNotFoundException("Reader-group does not exist"))).when(mockController).getReaderGroupConfig(scope, readerGroup1);
    doAnswer(x -> new AsyncIterator<Stream>() {

        final Iterator<Stream> iterator = new ArrayList<Stream>(Arrays.asList(new StreamImpl(scope, stream1), new StreamImpl(scope, stream2), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup1)), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup2)))).iterator();

        @Override
        public CompletableFuture<Stream> getNext() {
            Stream next;
            if (!iterator.hasNext()) {
                next = null;
            } else {
                next = iterator.next();
            }
            return CompletableFuture.completedFuture(next);
        }
    }).when(mockController).listStreams(scope);
    assertTrue(streamManager.deleteScope(scope, true));
}
Also used : Arrays(java.util.Arrays) AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) DeleteScopeFailedException(io.pravega.client.stream.DeleteScopeFailedException) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) ReaderGroupNotFoundException(io.pravega.client.stream.ReaderGroupNotFoundException) StreamSegments(io.pravega.client.stream.impl.StreamSegments) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) KeyValueTableInfo(io.pravega.client.admin.KeyValueTableInfo) Stream(io.pravega.client.stream.Stream) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) URI(java.net.URI) Mockito.doReturn(org.mockito.Mockito.doReturn) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Set(java.util.Set) StreamInfo(io.pravega.client.admin.StreamInfo) Sets(com.google.common.collect.Sets) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Assert.assertFalse(org.junit.Assert.assertFalse) TestUtils(io.pravega.test.common.TestUtils) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) Mockito.mock(org.mockito.Mockito.mock) StreamImpl(io.pravega.client.stream.impl.StreamImpl) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) StreamManager(io.pravega.client.admin.StreamManager) Exceptions(io.pravega.common.Exceptions) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) KeyValueTableManager(io.pravega.client.admin.KeyValueTableManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Lists(com.google.common.collect.Lists) ControllerFailureException(io.pravega.client.control.impl.ControllerFailureException) Before(org.junit.Before) NameUtils(io.pravega.shared.NameUtils) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) Assert.assertTrue(org.junit.Assert.assertTrue) AsyncIterator(io.pravega.common.util.AsyncIterator) Test(org.junit.Test) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Mockito(org.mockito.Mockito) TreeMap(java.util.TreeMap) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) Assert(org.junit.Assert) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) Cleanup(lombok.Cleanup) ReaderGroupNotFoundException(io.pravega.client.stream.ReaderGroupNotFoundException) CompletableFuture(java.util.concurrent.CompletableFuture) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ClientConnection(io.pravega.client.connection.impl.ClientConnection) Stream(io.pravega.client.stream.Stream) DeleteScopeFailedException(io.pravega.client.stream.DeleteScopeFailedException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamManager(io.pravega.client.admin.StreamManager) StreamImpl(io.pravega.client.stream.impl.StreamImpl) ControllerFailureException(io.pravega.client.control.impl.ControllerFailureException) MockController(io.pravega.client.stream.mock.MockController) Test(org.junit.Test)

Example 25 with ConnectionPoolImpl

use of io.pravega.client.connection.impl.ConnectionPoolImpl in project pravega by pravega.

the class SynchronizerTest method testSynchronizerClientFactory.

@Test(timeout = 5000)
public void testSynchronizerClientFactory() {
    ClientConfig config = ClientConfig.builder().controllerURI(URI.create("tls://localhost:9090")).build();
    @Cleanup ClientFactoryImpl factory = (ClientFactoryImpl) SynchronizerClientFactory.withScope("scope", config);
    ConnectionPoolImpl cp = (ConnectionPoolImpl) factory.getConnectionPool();
    assertEquals(1, cp.getClientConfig().getMaxConnectionsPerSegmentStore());
    assertEquals(config.isEnableTls(), cp.getClientConfig().isEnableTls());
}
Also used : ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) ClientConfig(io.pravega.client.ClientConfig) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

ConnectionPoolImpl (io.pravega.client.connection.impl.ConnectionPoolImpl)45 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)34 Test (org.junit.Test)34 Cleanup (lombok.Cleanup)33 ClientConfig (io.pravega.client.ClientConfig)31 StreamManager (io.pravega.client.admin.StreamManager)20 Controller (io.pravega.client.control.impl.Controller)20 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)19 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)17 StreamManagerImpl (io.pravega.client.admin.impl.StreamManagerImpl)14 MockController (io.pravega.client.stream.mock.MockController)14 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)12 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)12 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)11 Stream (io.pravega.client.stream.Stream)11 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)10 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)9 StreamImpl (io.pravega.client.stream.impl.StreamImpl)9 Segment (io.pravega.client.segment.impl.Segment)8 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)8