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());
}
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));
}
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);
}
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());
}
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());
}
Aggregations