completedFuture(new SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); completedFuture(new SegmentRead(segment.getScopedName(), wireDataSize, false, false, wireData.slice()))); completedFuture(new SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); completedFuture(new SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); @Cleanup EventSegmentReaderImpl stream3 = SegmentInputStreamFactoryImpl.getEventSegmentReader(mockAsyncInputStream, 0, Long.MAX_VALUE, 100);
SegmentRead result = (SegmentRead) AppendTest.sendRequest(channel, new ReadSegment(segmentName, actual.position(), 10000, "")); assertEquals(segmentName, result.getSegment()); assertEquals(result.getOffset(), actual.position()); assertTrue(result.isAtTail()); assertFalse(result.isEndOfSegment()); actual.put(result.getData()); if (actual.position() < actual.capacity()) {
log.info(requestId, "Closing connection {} while reading segment {} due to CancellationException.", connection, segment); invokeSafely(connection::send, new SegmentRead(segment, requestId, true, false, EMPTY_BYTE_BUFFER), failureHandler); } else if (u instanceof CancellationException) { log.info(requestId, "Closing connection {} while performing {} due to {}.",
EventSegmentReaderImpl stream1 = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertFalse(stream1.isSegmentReady()); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, true, ByteBufferUtils.slice(wireData, 0, 0))); assertTrue(stream1.isSegmentReady()); AssertExtensions.assertThrows(EndOfSegmentException.class, () -> stream1.read()); EventSegmentReaderImpl stream2 = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertFalse(stream2.isSegmentReady()); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, true, wireData.slice())); assertTrue(stream2.isSegmentReady()); assertEquals(ByteBuffer.wrap(data), stream2.read()); EventSegmentReaderImpl stream3 = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertFalse(stream3.isSegmentReady()); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice())); fakeNetwork.complete(1, new WireCommands.SegmentRead(segment.getScopedName(), wireData.remaining(), false, true, ByteBufferUtils.slice(wireData, 0, 0))); assertTrue(stream3.isSegmentReady()); assertEquals(ByteBuffer.wrap(data), stream3.read()); EventSegmentReaderImpl stream4 = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertFalse(stream4.isSegmentReady()); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, 0))); fakeNetwork.complete(1, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, true, wireData.slice())); assertEquals(ByteBuffer.wrap(data), stream4.read()); assertTrue(stream4.isSegmentReady()); EventSegmentReaderImpl stream5 = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertFalse(stream5.isSegmentReady()); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData, 0, 2)));
SegmentRead reply = new SegmentRead(segment, request.getOffset(), atTail, endOfSegment, data); connection.send(reply); dynamicLogger.incCounterValue(globalMetricName(SEGMENT_READ_BYTES), reply.getData().array().length); dynamicLogger.incCounterValue(nameFromSegment(SEGMENT_READ_BYTES, segment), reply.getData().array().length); } else if (truncated) { .thenAccept(contents -> { ByteBuffer data = copyData(Collections.singletonList(contents)); SegmentRead reply = new SegmentRead(segment, nonCachedEntry.getStreamSegmentOffset(), false, endOfSegment, data); connection.send(reply); dynamicLogger.incCounterValue(globalMetricName(SEGMENT_READ_BYTES), reply.getData().array().length); dynamicLogger.incCounterValue(nameFromSegment(SEGMENT_READ_BYTES, segment), reply.getData().array().length); }) .exceptionally(e -> {
EventSegmentReaderImpl stream5 = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork5, 0); fakeNetwork2.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice())); fakeNetwork3.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, true, wireData.slice())); fakeNetwork4.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, true, ByteBuffer.allocate(0))); fakeNetwork5.completeExceptionally(0, new SegmentTruncatedException());
connectionFactory.provideConnection(endpoint, c); WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, ByteBuffer.allocate(0)); Mockito.doAnswer(new Answer<Void>() { @Override
WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 5656, false, false, ByteBuffer.allocate(0)); CompletableFuture<SegmentRead> readFuture2 = in.read(5656, 5678); AssertExtensions.assertBlocks(() -> readFuture2.get(), () -> {
@Test(timeout = 20000) public void testReadSegment() { // Set up PravegaRequestProcessor instance to execute read segment request against String streamSegmentName = "testReadSegment"; byte[] data = new byte[]{1, 2, 3, 4, 6, 7, 8, 9}; int readLength = 1000; StreamSegmentStore store = mock(StreamSegmentStore.class); ServerConnection connection = mock(ServerConnection.class); PravegaRequestProcessor processor = new PravegaRequestProcessor(store, mock(TableStore.class), connection); TestReadResultEntry entry1 = new TestReadResultEntry(ReadResultEntryType.Cache, 0, readLength); entry1.complete(new ReadResultEntryContents(new ByteArrayInputStream(data), data.length)); TestReadResultEntry entry2 = new TestReadResultEntry(ReadResultEntryType.Future, data.length, readLength); List<ReadResultEntry> results = new ArrayList<>(); results.add(entry1); results.add(entry2); CompletableFuture<ReadResult> readResult = new CompletableFuture<>(); readResult.complete(new TestReadResult(0, readLength, results)); when(store.read(streamSegmentName, 0, readLength, PravegaRequestProcessor.TIMEOUT)).thenReturn(readResult); // Execute and Verify readSegment calling stack in connection and store is executed as design. processor.readSegment(new WireCommands.ReadSegment(streamSegmentName, 0, readLength, "")); verify(store).read(streamSegmentName, 0, readLength, PravegaRequestProcessor.TIMEOUT); verify(connection).send(new WireCommands.SegmentRead(streamSegmentName, 0, true, false, ByteBuffer.wrap(data))); verifyNoMoreInteractions(connection); verifyNoMoreInteractions(store); entry2.complete(new ReadResultEntryContents(new ByteArrayInputStream(data), data.length)); verifyNoMoreInteractions(connection); verifyNoMoreInteractions(store); }
@Test public void testIsSegmentReady() 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); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertFalse(stream.isSegmentReady()); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, true, false, wireData.slice())); for (int i = 0; i < numEntries; i++) { assertTrue(stream.isSegmentReady()); assertEquals(ByteBuffer.wrap(data), stream.read()); } assertFalse(stream.isSegmentReady()); assertBlocks(() -> stream.read(), () -> { fakeNetwork.complete(1, new WireCommands.SegmentRead(segment.getScopedName(), wireData.capacity(), false, false, createEventFromData(data))); }); assertFalse(stream.isSegmentReady()); }
@Test(timeout = 10000) public void testRead() throws ConnectionFailedException { Segment segment = new Segment("scope", "testRead", 1); PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT); MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl(); MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory); @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, ""); ClientConnection c = mock(ClientConnection.class); connectionFactory.provideConnection(endpoint, c); WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, ByteBuffer.allocate(0)); CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678); AssertExtensions.assertBlocks(() -> readFuture.get(), () -> { ReplyProcessor processor = connectionFactory.getProcessor(endpoint); processor.segmentRead(segmentRead); }); verify(c).sendAsync(Mockito.eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "")), Mockito.any(ClientConnection.CompletedCallback.class)); assertTrue(Futures.isSuccessful(readFuture)); assertEquals(segmentRead, readFuture.join()); verifyNoMoreInteractions(c); }
@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); }
private void handleRequest() throws SegmentTruncatedException { SegmentRead segmentRead; try { segmentRead = outstandingRequest.join(); } catch (Exception e) { outstandingRequest = null; if (Exceptions.unwrap(e) instanceof SegmentTruncatedException) { receivedTruncated = true; throw new SegmentTruncatedException(e); } throw e; } verifyIsAtCorrectOffset(segmentRead); if (segmentRead.getData().hasRemaining()) { buffer.fill(segmentRead.getData()); } if (segmentRead.isEndOfSegment()) { receivedEndOfSegment = true; } if (!segmentRead.getData().hasRemaining()) { outstandingRequest = null; issueRequestIfNeeded(); } }
@Test(timeout = 20000) public void testReadSegmentEmptySealed() { // Set up PravegaRequestProcessor instance to execute read segment request against String streamSegmentName = "testReadSegment"; int readLength = 1000; StreamSegmentStore store = mock(StreamSegmentStore.class); ServerConnection connection = mock(ServerConnection.class); PravegaRequestProcessor processor = new PravegaRequestProcessor(store, mock(TableStore.class), connection); TestReadResultEntry entry1 = new TestReadResultEntry(ReadResultEntryType.EndOfStreamSegment, 0, readLength); List<ReadResultEntry> results = new ArrayList<>(); results.add(entry1); CompletableFuture<ReadResult> readResult = new CompletableFuture<>(); readResult.complete(new TestReadResult(0, readLength, results)); when(store.read(streamSegmentName, 0, readLength, PravegaRequestProcessor.TIMEOUT)).thenReturn(readResult); // Execute and Verify readSegment calling stack in connection and store is executed as design. processor.readSegment(new WireCommands.ReadSegment(streamSegmentName, 0, readLength, "")); verify(store).read(streamSegmentName, 0, readLength, PravegaRequestProcessor.TIMEOUT); verify(connection).send(new WireCommands.SegmentRead(streamSegmentName, 0, false, true, ByteBuffer.wrap(new byte[0]))); verifyNoMoreInteractions(connection); verifyNoMoreInteractions(store); }
@Test(timeout = 10000) public void testWrongOffsetReturned() throws ConnectionFailedException { Segment segment = new Segment("scope", "testWrongOffsetReturned", 0); byte[] good = new byte[] { 0, 1, 2, 3, 4 }; byte[] bad = new byte[] { 9, 8, 7, 6 }; PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT); MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl(); MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory); @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, ""); ClientConnection c = mock(ClientConnection.class); connectionFactory.provideConnection(endpoint, c); CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678); AssertExtensions.assertBlocks(() -> readFuture.get(), () -> { ReplyProcessor processor = connectionFactory.getProcessor(endpoint); processor.segmentRead(new WireCommands.SegmentRead(segment.getScopedName(), 1235, false, false, ByteBuffer.wrap(bad))); processor.segmentRead(new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, ByteBuffer.wrap(good))); }); verify(c).sendAsync(Mockito.eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "")), Mockito.any(ClientConnection.CompletedCallback.class)); assertTrue(Futures.isSuccessful(readFuture)); assertEquals(ByteBuffer.wrap(good), readFuture.join().getData()); verifyNoMoreInteractions(c); }
@Test public void testSetOffset() throws EndOfSegmentException, SegmentTruncatedException { byte[] data1 = new byte[]{0, 1, 2, 3, 4, 5}; byte[] data2 = new byte[]{6, 7, 8, 9}; ByteBuffer wireData1 = createEventFromData(data1); ByteBuffer wireData2 = createEventFromData(data2); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 5); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData1, 0, wireData1.remaining()))); ByteBuffer read = stream.read(); assertEquals(ByteBuffer.wrap(data1), read); fakeNetwork.complete(2, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, ByteBufferUtils.slice(wireData1, 0, wireData1.remaining()))); fakeNetwork.complete(3, new WireCommands.SegmentRead(segment.getScopedName(), wireData1.remaining(), false, false, ByteBufferUtils.slice(wireData2, 0, wireData2.remaining()))); stream.setOffset(0); read = stream.read(); assertEquals(ByteBuffer.wrap(data1), read); read = stream.read(); assertEquals(ByteBuffer.wrap(data2), read); }
@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); }
@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(timeout = 10000) public void testTimeout() throws EndOfSegmentException, SegmentTruncatedException { byte[] data = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; ByteBuffer wireData = createEventFromData(data); TestAsyncSegmentInputStream fakeNetwork = new TestAsyncSegmentInputStream(segment, 7); @Cleanup EventSegmentReaderImpl stream = SegmentInputStreamFactoryImpl.getEventSegmentReader(fakeNetwork, 0); assertBlocks(() -> stream.read(), () -> fakeNetwork.complete(0, new WireCommands.SegmentRead(segment.getScopedName(), 0, false, false, wireData.slice()))); ByteBuffer read = stream.read(10); assertNull(read); fakeNetwork.completeExceptionally(1, new ConnectionFailedException()); AssertExtensions.assertThrows(ConnectionFailedException.class, () -> stream.read()); stream.read(10); assertNull(read); read = stream.read(10); assertNull(read); }
@Test(timeout = 20000) public void testReadSegmentWithCancellationException() { // Set up PravegaRequestProcessor instance to execute read segment request against String streamSegmentName = "testReadSegment"; int readLength = 1000; StreamSegmentStore store = mock(StreamSegmentStore.class); ServerConnection connection = mock(ServerConnection.class); PravegaRequestProcessor processor = new PravegaRequestProcessor(store, mock(TableStore.class), connection); CompletableFuture<ReadResult> readResult = new CompletableFuture<>(); readResult.completeExceptionally(new CancellationException("cancel read")); // Simulate a CancellationException for a Read Segment. when(store.read(streamSegmentName, 0, readLength, PravegaRequestProcessor.TIMEOUT)).thenReturn(readResult); // Execute and Verify readSegment is calling stack in connection and store is executed as design. processor.readSegment(new WireCommands.ReadSegment(streamSegmentName, 0, readLength, "")); verify(store).read(streamSegmentName, 0, readLength, PravegaRequestProcessor.TIMEOUT); // Since the underlying store cancels the read request verify if an empty SegmentRead Wirecommand is sent as a response. verify(connection).send(new WireCommands.SegmentRead(streamSegmentName, 0, true, false, ByteBuffer.wrap(new byte[0]))); verifyNoMoreInteractions(connection); verifyNoMoreInteractions(store); }