public ByteStreamClientImpl(String scope, Controller controller, ConnectionFactory connectionFactory) { this.scope = Preconditions.checkNotNull(scope); this.controller = Preconditions.checkNotNull(controller); this.connectionFactory = Preconditions.checkNotNull(connectionFactory); this.inputStreamFactory = new SegmentInputStreamFactoryImpl(controller, connectionFactory); this.outputStreamFactory = new SegmentOutputStreamFactoryImpl(controller, connectionFactory); this.metaStreamFactory = new SegmentMetadataClientFactoryImpl(controller, connectionFactory); }
@Override public EventSegmentReader createEventReaderForSegment(Segment segment) { return createEventReaderForSegment(segment, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); }
@Override public EventSegmentReader createEventReaderForSegment(Segment segment, long endOffset) { return getEventSegmentReader(segment, endOffset, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); }
SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, clientCF); EventSegmentReader in = segmentConsumerClient.createEventReaderForSegment(segment); ByteBuffer result = in.read(); assertEquals(ByteBuffer.wrap(testString.getBytes()), result);
SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, clientCF); EventSegmentReader in = segmentConsumerClient.createEventReaderForSegment(segment); ByteBuffer result = in.read(); assertEquals(ByteBuffer.wrap(testString), result);
@Override public EventSegmentReader createEventReaderForSegment(Segment segment, int bufferSize) { return getEventSegmentReader(segment, Long.MAX_VALUE, bufferSize); }
public BatchClientFactoryImpl(Controller controller, ConnectionFactory connectionFactory) { this.controller = controller; this.connectionFactory = connectionFactory; this.inputStreamFactory = new SegmentInputStreamFactoryImpl(controller, connectionFactory); this.segmentMetadataClientFactory = new SegmentMetadataClientFactoryImpl(controller, connectionFactory); this.streamCutHelper = new StreamCutHelper(controller, connectionFactory); this.latestDelegationToken = new AtomicReference<>(); }
@Test(expected = IllegalArgumentException.class) public void testReadWithInvalidEndOffset() { AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); //Set the end offset which is less than startOffset @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 10, 9, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); }
/** * Creates a new instance of the ClientFactory class. * Note: ConnectionFactory and Controller is closed when {@link ClientFactoryImpl#close()} is invoked. * * @param scope The scope string. * @param controller The reference to Controller. * @param connectionFactory The reference to Connection Factory impl. */ @VisibleForTesting public ClientFactoryImpl(String scope, Controller controller, ConnectionFactory connectionFactory) { this(scope, controller, connectionFactory, new SegmentInputStreamFactoryImpl(controller, connectionFactory), new SegmentOutputStreamFactoryImpl(controller, connectionFactory), new ConditionalOutputStreamFactoryImpl(controller, connectionFactory), new SegmentMetadataClientFactoryImpl(controller, connectionFactory)); }
@Test(expected = IllegalArgumentException.class) public void testReadWithNegativeEndOffset() { AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, -2, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); }
/** * Creates a new instance of ClientFactory class. * Note: Controller is closed when {@link ClientFactoryImpl#close()} is invoked. * * @param scope The scope string. * @param controller The reference to Controller. */ public ClientFactoryImpl(String scope, Controller controller) { Preconditions.checkNotNull(scope); Preconditions.checkNotNull(controller); this.scope = scope; this.controller = controller; this.connectionFactory = new ConnectionFactoryImpl(ClientConfig.builder().build()); this.inFactory = new SegmentInputStreamFactoryImpl(controller, connectionFactory); this.outFactory = new SegmentOutputStreamFactoryImpl(controller, connectionFactory); this.condFactory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory); this.metaFactory = new SegmentMetadataClientFactoryImpl(controller, connectionFactory); }
private EventSegmentReader getEventSegmentReader(Segment segment, long endOffset, int bufferSize) { String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStream() .getStreamName()), RuntimeException::new); AsyncSegmentInputStreamImpl async = new AsyncSegmentInputStreamImpl(controller, cf, segment, delegationToken); async.getConnection(); return getEventSegmentReader(async, 0, endOffset, bufferSize); }
@Test(expected = EndOfSegmentException.class) public void testReadWithEndOffset() throws Exception { byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); int wireDataSize = wireData.remaining(); //size of the data with header size. AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); when(mockAsyncInputStream.read(0, wireDataSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, wireDataSize, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data), read); //verify we are reading the data. verify(mockAsyncInputStream, times(1)).read(0L, wireDataSize); //ensure there is one invocation. stream.read(); // this should throw EndOfSegmentExceptiono as we have reached the endOffset }
@Test public void testReadWithEndOffsetWithDataGreaterThanBuffer() throws Exception { byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); int wireDataSize = wireData.remaining(); //size of the data with header size. int bufferSize = wireDataSize / 2; //buffer is half the data length AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); when(mockAsyncInputStream.read(0, bufferSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, bufferSize)))); when(mockAsyncInputStream.read(bufferSize, wireDataSize - bufferSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), bufferSize, false, false, ByteBufferUtils.slice(wireData, bufferSize, wireDataSize - bufferSize)))); //Create a SegmentInputStream where the Buffer can hold only part of the data. @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, wireDataSize, bufferSize); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data), read); //verify we are reading the data. verify(mockAsyncInputStream, times(1)).read(0L, bufferSize); verify(mockAsyncInputStream, times(1)).read(bufferSize, wireDataSize - bufferSize); }
@Test public void testReadWithEndOffsetWithSmallerReads() throws Exception { byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); int wireDataSize = wireData.remaining(); //size of the data with header size. AsyncSegmentInputStream mockAsyncInputStream = mock(AsyncSegmentInputStream.class); when(mockAsyncInputStream.read(0, wireDataSize)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, 2)))); when(mockAsyncInputStream.read(2, 16)) .thenReturn(CompletableFuture.completedFuture(new WireCommands.SegmentRead(segment.getScopedName(), 2, false, false, ByteBufferUtils.slice(wireData, 2, wireDataSize - 2)))); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, wireDataSize, SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data), read); //verify we are reading the data. verify(mockAsyncInputStream, times(1)).read(0L, wireDataSize); verify(mockAsyncInputStream, times(1)).read(2L, wireDataSize - 2); }
EventSegmentReaderImpl stream1 = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0); EventSegmentReaderImpl stream2 = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, wireDataSize, bufferSize); completedFuture(new SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); @Cleanup EventSegmentReaderImpl stream3 = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, Long.MAX_VALUE, 100);
@Test public void testStreamTruncatedWithPartialEvent() throws EndOfSegmentException { ByteBuffer trailingData = ByteBuffer.wrap(new byte[] {0, 1}); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 1); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, true, trailingData.slice())); AssertExtensions.assertThrows(EndOfSegmentException.class, () -> stream.read()); }
@Test public void testLongerThanRequestedRead() throws EndOfSegmentException, SegmentTruncatedException { byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int numEntries = SegmentInputStreamImpl.DEFAULT_BUFFER_SIZE / data.length; ByteBuffer wireData = ByteBuffer.allocate((data.length + WireCommands.TYPE_PLUS_LENGTH_SIZE) * numEntries); for (int i = 0; i < numEntries; i++) { wireData.putInt(WireCommandType.EVENT.getCode()); wireData.putInt(data.length); wireData.put(data); } wireData.flip(); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 3); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice())); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); for (int i = 0; i < numEntries; i++) { assertEquals(ByteBuffer.wrap(data), stream.read()); } ByteBuffer read = assertBlocks(() -> stream.read(), () -> { fakeNetwork.complete(1, new WireCommands.SegmentRead(segment.getScopedName(), wireData.capacity(), false, false, createEventFromData(data))); }); assertEquals(ByteBuffer.wrap(data), read); }
@Test public void testRead() { byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 3); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); ByteBuffer read = assertBlocks(() -> stream.read(), () -> fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); assertEquals(ByteBuffer.wrap(data), read); read = assertBlocks(() -> stream .read(), () -> fakeNetwork.complete(1, new WireCommands.SegmentRead(segment.getScopedName(), wireData.capacity(), false, false, wireData.slice()))); assertEquals(ByteBuffer.wrap(data), read); }
@Test(timeout = 5000) public void testClose() { byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 2); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, wireData.remaining()))); EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); stream.close(); AssertExtensions.assertThrows(ObjectClosedException.class, () -> stream.read()); }