use of io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl in project pravega by pravega.
the class ReadTest method readThroughSegmentClient.
@Test(timeout = 10000)
public void readThroughSegmentClient() throws SegmentSealedException, EndOfSegmentException, SegmentTruncatedException {
String endpoint = "localhost";
String scope = "scope";
String stream = "readThroughSegmentClient";
int port = TestUtils.getAvailableListenPort();
String testString = "Hello world\n";
StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, SERVICE_BUILDER.getLowPriorityExecutor());
server.startListening();
@Cleanup SocketConnectionFactoryImpl clientCF = new SocketConnectionFactoryImpl(ClientConfig.builder().build());
@Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(ClientConfig.builder().build(), clientCF);
@Cleanup Controller controller = new MockController(endpoint, port, connectionPool, true);
controller.createScope(scope);
controller.createStream(scope, stream, StreamConfiguration.builder().build());
SegmentOutputStreamFactoryImpl segmentproducerClient = new SegmentOutputStreamFactoryImpl(controller, connectionPool);
SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, connectionPool);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup SegmentOutputStream out = segmentproducerClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), DelegationTokenProviderFactory.createWithEmptyToken());
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(testString.getBytes()), new CompletableFuture<>()));
out.flush();
@Cleanup EventSegmentReader in = segmentConsumerClient.createEventReaderForSegment(segment);
ByteBuffer result = in.read();
assertEquals(ByteBuffer.wrap(testString.getBytes()), result);
// Test large write followed by read
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
out.write(PendingEvent.withHeader(null, ByteBuffer.wrap(new byte[150000]), new CompletableFuture<>()));
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 15);
assertEquals(in.read().capacity(), 150000);
}
use of io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl in project pravega by pravega.
the class ReadTest method readConditionalData.
@Test(timeout = 10000)
public void readConditionalData() throws SegmentSealedException, EndOfSegmentException, SegmentTruncatedException {
String endpoint = "localhost";
String scope = "scope";
String stream = "readConditionalData";
int port = TestUtils.getAvailableListenPort();
byte[] testString = "Hello world\n".getBytes();
StreamSegmentStore store = SERVICE_BUILDER.createStreamSegmentService();
TableStore tableStore = SERVICE_BUILDER.createTableStoreService();
@Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore, SERVICE_BUILDER.getLowPriorityExecutor());
server.startListening();
@Cleanup SocketConnectionFactoryImpl clientCF = new SocketConnectionFactoryImpl(ClientConfig.builder().build());
@Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(ClientConfig.builder().build(), clientCF);
@Cleanup Controller controller = new MockController(endpoint, port, connectionPool, true);
controller.createScope(scope);
controller.createStream(scope, stream, StreamConfiguration.builder().build());
ConditionalOutputStreamFactoryImpl segmentproducerClient = new ConditionalOutputStreamFactoryImpl(controller, connectionPool);
SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, connectionPool);
Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
@Cleanup ConditionalOutputStream out = segmentproducerClient.createConditionalOutputStream(segment, DelegationTokenProviderFactory.createWithEmptyToken(), EventWriterConfig.builder().build());
assertTrue(out.write(ByteBuffer.wrap(testString), 0));
@Cleanup EventSegmentReader in = segmentConsumerClient.createEventReaderForSegment(segment);
ByteBuffer result = in.read();
assertEquals(ByteBuffer.wrap(testString), result);
assertNull(in.read(100));
assertFalse(out.write(ByteBuffer.wrap(testString), 0));
assertTrue(out.write(ByteBuffer.wrap(testString), testString.length + WireCommands.TYPE_PLUS_LENGTH_SIZE));
result = in.read();
assertEquals(ByteBuffer.wrap(testString), result);
assertNull(in.read(100));
}
use of io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl 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.segment.impl.SegmentInputStreamFactoryImpl in project pravega by pravega.
the class ByteClientTest method createClientFactory.
ByteStreamClientFactory createClientFactory(String scope) {
ClientConfig config = ClientConfig.builder().build();
ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(config);
ControllerImpl controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(Utils.buildClientConfig(controllerURI)).build(), connectionFactory.getInternalExecutor());
ConnectionPool pool = new ConnectionPoolImpl(config, connectionFactory);
val inputStreamFactory = new SegmentInputStreamFactoryImpl(controller, pool);
val outputStreamFactory = new SegmentOutputStreamFactoryImpl(controller, pool);
val metaStreamFactory = new SegmentMetadataClientFactoryImpl(controller, pool);
return new ByteStreamClientImpl(scope, controller, pool, inputStreamFactory, outputStreamFactory, metaStreamFactory);
}
use of io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl in project pravega by pravega.
the class ByteStreamClientFactory method withScope.
/**
* Creates a new instance of ByteStreamClientFactory.
*
* @param scope The scope string.
* @param config Configuration for the client.
* @return Instance of ByteStreamClientFactory implementation.
*/
static ByteStreamClientFactory withScope(String scope, ClientConfig config) {
val connectionFactory = new SocketConnectionFactoryImpl(config);
ControllerImpl controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(config).build(), connectionFactory.getInternalExecutor());
val connectionPool = new ConnectionPoolImpl(config, Preconditions.checkNotNull(connectionFactory));
val inputStreamFactory = new SegmentInputStreamFactoryImpl(controller, connectionPool);
val outputStreamFactory = new SegmentOutputStreamFactoryImpl(controller, connectionPool);
val metaStreamFactory = new SegmentMetadataClientFactoryImpl(controller, connectionPool);
return new ByteStreamClientImpl(scope, controller, connectionPool, inputStreamFactory, outputStreamFactory, metaStreamFactory);
}
Aggregations