Search in sources :

Example 46 with ClientFactoryImpl

use of io.pravega.client.stream.impl.ClientFactoryImpl in project pravega by pravega.

the class RevisionedStreamClientTest method testSegmentTruncation.

@Test
public void testSegmentTruncation() {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    createScopeAndStream(scope, stream, controller);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
    Revision r0 = client.fetchLatestRevision();
    client.writeUnconditionally("a");
    Revision ra = client.fetchLatestRevision();
    client.writeUnconditionally("b");
    Revision rb = client.fetchLatestRevision();
    client.writeUnconditionally("c");
    Revision rc = client.fetchLatestRevision();
    assertEquals(r0, client.fetchOldestRevision());
    client.truncateToRevision(r0);
    assertEquals(r0, client.fetchOldestRevision());
    client.truncateToRevision(ra);
    assertEquals(ra, client.fetchOldestRevision());
    client.truncateToRevision(r0);
    assertEquals(ra, client.fetchOldestRevision());
    assertThrows(TruncatedDataException.class, () -> client.readFrom(r0));
    Iterator<Entry<Revision, String>> iterA = client.readFrom(ra);
    assertTrue(iterA.hasNext());
    Iterator<Entry<Revision, String>> iterB = client.readFrom(ra);
    assertTrue(iterB.hasNext());
    assertEquals("b", iterA.next().getValue());
    assertEquals("b", iterB.next().getValue());
    client.truncateToRevision(rb);
    assertTrue(iterA.hasNext());
    assertEquals("c", iterA.next().getValue());
    client.truncateToRevision(rc);
    assertFalse(iterA.hasNext());
    assertTrue(iterB.hasNext());
    assertThrows(TruncatedDataException.class, () -> iterB.next());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 47 with ClientFactoryImpl

use of io.pravega.client.stream.impl.ClientFactoryImpl in project pravega by pravega.

the class RevisionedStreamClientTest method testRetryOnTimeout.

@Test
public void testRetryOnTimeout() throws ConnectionFailedException {
    String scope = "scope";
    String stream = "stream";
    Segment segment = new Segment(scope, stream, 0L);
    // Setup Environment
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    // Setup Mocks
    JavaSerializer<String> serializer = new JavaSerializer<>();
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    // Setup client connection.
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    // Create Scope and Stream.
    createScopeAndStream(scope, stream, controller);
    // Create mock ClientFactory.
    SegmentInputStreamFactory segInputFactory = new SegmentInputStreamFactoryImpl(controller, connectionFactory);
    SegmentOutputStreamFactory segOutputFactory = mock(SegmentOutputStreamFactory.class);
    ConditionalOutputStreamFactory condOutputFactory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    SegmentMetadataClientFactory segMetaFactory = mock(SegmentMetadataClientFactory.class);
    SegmentMetadataClient segMetaClient = mock(SegmentMetadataClient.class);
    when(segMetaFactory.createSegmentMetadataClient(eq(segment), any(DelegationTokenProvider.class))).thenReturn(segMetaClient);
    @Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, segInputFactory, segOutputFactory, condOutputFactory, segMetaFactory);
    RevisionedStreamClientImpl<String> client = spy((RevisionedStreamClientImpl<String>) clientFactory.createRevisionedStreamClient(stream, serializer, SynchronizerConfig.builder().build()));
    // Override the readTimeout value for RevisionedClient to 1 second.
    doReturn(1000L).when(client).getReadTimeout();
    // Setup the SegmentMetadataClient mock.
    doReturn(CompletableFuture.completedFuture(new SegmentInfo(segment, 0L, 30L, false, 1L))).when(segMetaClient).getSegmentInfo();
    // Get the iterator from Revisioned Stream Client.
    Iterator<Entry<Revision, String>> iterator = client.readFrom(new RevisionImpl(segment, 15, 1));
    // since we are trying to read @ offset 15 and the writeOffset is 30L a true is returned for hasNext().
    assertTrue(iterator.hasNext());
    // Setup mock to validate a retry.
    doNothing().doAnswer(i -> {
        WireCommands.ReadSegment request = i.getArgument(0);
        ReplyProcessor rp = connectionFactory.getProcessor(endpoint);
        WireCommands.Event event = new WireCommands.Event(Unpooled.wrappedBuffer(serializer.serialize("A")));
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        event.writeFields(new DataOutputStream(bout));
        ByteBuf eventData = Unpooled.wrappedBuffer(bout.toByteArray());
        // Invoke Reply processor to simulate a successful read.
        rp.process(new WireCommands.SegmentRead(request.getSegment(), 15L, true, true, eventData, request.getRequestId()));
        return null;
    }).when(c).send(any(WireCommands.ReadSegment.class));
    Entry<Revision, String> r = iterator.next();
    assertEquals("A", r.getValue());
    // Verify retries have been performed.
    verify(c, times(3)).send(any(WireCommands.ReadSegment.class));
}
Also used : DataOutputStream(java.io.DataOutputStream) ByteBuf(io.netty.buffer.ByteBuf) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ConditionalOutputStreamFactoryImpl(io.pravega.client.segment.impl.ConditionalOutputStreamFactoryImpl) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) ConditionalOutputStreamFactory(io.pravega.client.segment.impl.ConditionalOutputStreamFactory) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Revision(io.pravega.client.state.Revision) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) MockController(io.pravega.client.stream.mock.MockController) SegmentInfo(io.pravega.client.segment.impl.SegmentInfo) PendingEvent(io.pravega.client.stream.impl.PendingEvent) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 48 with ClientFactoryImpl

use of io.pravega.client.stream.impl.ClientFactoryImpl in project pravega by pravega.

the class RevisionedStreamClientTest method testSegmentSealedFromSegmentOutputStreamError.

@Test
public void testSegmentSealedFromSegmentOutputStreamError() {
    String scope = "scope";
    String stream = "stream";
    // Setup Environment
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    createScopeAndStream(scope, stream, controller);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    // Setup mock
    SegmentOutputStreamFactory outFactory = mock(SegmentOutputStreamFactory.class);
    SegmentOutputStream out = mock(SegmentOutputStream.class);
    when(outFactory.createOutputStreamForSegment(eq(new Segment(scope, stream, 0)), any(), any(), any(DelegationTokenProvider.class))).thenReturn(out);
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, outFactory, streamFactory, streamFactory);
    CompletableFuture<Void> writeFuture = new CompletableFuture<>();
    PendingEvent event1 = PendingEvent.withoutHeader("key", ByteBufferUtils.EMPTY, writeFuture);
    PendingEvent event2 = PendingEvent.withoutHeader("key", ByteBufferUtils.EMPTY, null);
    // Two events are returned when the callback invokes getUnackedEventsOnSeal
    when(out.getUnackedEventsOnSeal()).thenReturn(Arrays.asList(event1, event2));
    @Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), SynchronizerConfig.builder().build());
    // simulate invocation of handleSegmentSealed by Segment writer.
    ((RevisionedStreamClientImpl) client).handleSegmentSealed();
    // Verify SegmentOutputStream#getUnackedEventsOnSeal is invoked.
    verify(out, times(1)).getUnackedEventsOnSeal();
    assertTrue(writeFuture.isCompletedExceptionally());
    assertThrows(SegmentSealedException.class, writeFuture::get);
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) CompletableFuture(java.util.concurrent.CompletableFuture) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) PendingEvent(io.pravega.client.stream.impl.PendingEvent) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) Test(org.junit.Test)

Example 49 with ClientFactoryImpl

use of io.pravega.client.stream.impl.ClientFactoryImpl in project pravega by pravega.

the class RevisionedStreamClientTest method testConditionalWrite.

@Test
public void testConditionalWrite() {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    createScopeAndStream(scope, stream, controller);
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
    client.writeUnconditionally("a");
    Revision revision = client.fetchLatestRevision();
    Revision newRevision = client.writeConditionally(revision, "b");
    assertNotNull(newRevision);
    assertTrue(newRevision.compareTo(revision) > 0);
    assertEquals(newRevision, client.fetchLatestRevision());
    Revision failed = client.writeConditionally(revision, "fail");
    assertNull(failed);
    assertEquals(newRevision, client.fetchLatestRevision());
    Iterator<Entry<Revision, String>> iter = client.readFrom(revision);
    assertTrue(iter.hasNext());
    Entry<Revision, String> entry = iter.next();
    assertEquals(newRevision, entry.getKey());
    assertEquals("b", entry.getValue());
    assertFalse(iter.hasNext());
}
Also used : MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Entry(java.util.Map.Entry) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Revision(io.pravega.client.state.Revision) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 50 with ClientFactoryImpl

use of io.pravega.client.stream.impl.ClientFactoryImpl 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

ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)80 Test (org.junit.Test)63 Cleanup (lombok.Cleanup)59 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)54 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)50 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)44 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)40 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)39 ClientConfig (io.pravega.client.ClientConfig)37 Stream (io.pravega.client.stream.Stream)27 Controller (io.pravega.client.control.impl.Controller)25 StreamImpl (io.pravega.client.stream.impl.StreamImpl)24 HashMap (java.util.HashMap)23 ReaderGroup (io.pravega.client.stream.ReaderGroup)19 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)19 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)19 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)18 URI (java.net.URI)18 CompletableFuture (java.util.concurrent.CompletableFuture)18 ControllerImpl (io.pravega.client.control.impl.ControllerImpl)17