public static WireCommand readFrom(DataInput in, int length) throws IOException { long requestId = in.readLong(); UUID uuid = new UUID(in.readLong(), in.readLong()); String segment = in.readUTF(); String delegationToken = in.readUTF(); return new SetupAppend(requestId, uuid, segment, delegationToken); } }
@Test public void testInvalidOffset() { String streamSegmentName = "testAppendSegment"; UUID clientId = UUID.randomUUID(); byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 }; StreamSegmentStore store = mock(StreamSegmentStore.class); ServerConnection connection = mock(ServerConnection.class); AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null); setupGetAttributes(streamSegmentName, clientId, 100, store); processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, "")); try { processor.append(new Append(streamSegmentName, clientId, data.length, 1, Unpooled.wrappedBuffer(data), null)); fail(); } catch (RuntimeException e) { //expected } verify(store).getAttributes(anyString(), eq(Collections.singleton(clientId)), eq(true), eq(AppendProcessor.TIMEOUT)); verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 100)); verify(connection, atLeast(0)).resumeReading(); verifyNoMoreInteractions(connection); verifyNoMoreInteractions(store); }
@Test public void testEventNumbers() { String streamSegmentName = "testAppendSegment"; UUID clientId = UUID.randomUUID(); byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 }; StreamSegmentStore store = mock(StreamSegmentStore.class); ServerConnection connection = mock(ServerConnection.class); AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null); when(store.getAttributes(streamSegmentName, Collections.singleton(clientId), true, AppendProcessor.TIMEOUT)) .thenReturn(CompletableFuture.completedFuture(Collections.emptyMap())); processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, "")); verify(store).getAttributes(streamSegmentName, Collections.singleton(clientId), true, AppendProcessor.TIMEOUT); CompletableFuture<Void> result = CompletableFuture.completedFuture(null); int eventCount = 100; when(store.append(streamSegmentName, data, updateEventNumber(clientId, 100, Attributes.NULL_ATTRIBUTE_VALUE, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result); processor.append(new Append(streamSegmentName, clientId, 100, eventCount, Unpooled.wrappedBuffer(data), null)); verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 100, Attributes.NULL_ATTRIBUTE_VALUE, eventCount), AppendProcessor.TIMEOUT); when(store.append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result); processor.append(new Append(streamSegmentName, clientId, 200, eventCount, Unpooled.wrappedBuffer(data), null)); verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT); verifyNoMoreInteractions(store); }
@Test(timeout = 10000) public void testClose() throws ConnectionFailedException, SegmentSealedException { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); cf.setExecutor(executorService()); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); ByteBuffer data = getBuffer("test"); CompletableFuture<Void> acked = new CompletableFuture<>(); output.write(PendingEvent.withoutHeader(null, data, acked)); verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null)); assertEquals(false, acked.isDone()); AssertExtensions.assertBlocks(() -> output.close(), () -> cf.getProcessor(uri).dataAppended(new WireCommands.DataAppended(cid, 1, 0))); assertEquals(false, acked.isCompletedExceptionally()); assertEquals(true, acked.isDone()); verify(connection, Mockito.atMost(1)).send(new WireCommands.KeepAlive()); verify(connection).close(); verifyNoMoreInteractions(connection); }
@Test public void testUnsupportedOperation() { String streamSegmentName = "testAppendSegment"; UUID clientId = UUID.randomUUID(); byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 }; StreamSegmentStore store = mock(StreamSegmentStore.class); ServerConnection connection = mock(ServerConnection.class); AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null); setupGetAttributes(streamSegmentName, clientId, store); CompletableFuture<Void> result = Futures.failedFuture(new UnsupportedOperationException()); when(store.append(streamSegmentName, data, updateEventNumber(clientId, data.length), AppendProcessor.TIMEOUT)) .thenReturn(result); processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, "")); processor.append(new Append(streamSegmentName, clientId, data.length, 1, Unpooled.wrappedBuffer(data), null)); verify(store).getAttributes(anyString(), eq(Collections.singleton(clientId)), eq(true), eq(AppendProcessor.TIMEOUT)); verify(store).append(streamSegmentName, data, updateEventNumber(clientId, data.length), AppendProcessor.TIMEOUT); verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0)); verify(connection, atLeast(0)).resumeReading(); verify(connection).send(new OperationUnsupported(data.length, "appending data", "")); verifyNoMoreInteractions(connection); verifyNoMoreInteractions(store); }
@Test public void testOverSizedWriteFails() throws ConnectionFailedException, SegmentSealedException { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); cf.setExecutor(executorService()); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); @Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); ByteBuffer data = ByteBuffer.allocate(PendingEvent.MAX_WRITE_SIZE + 1); CompletableFuture<Void> acked = new CompletableFuture<>(); try { output.write(PendingEvent.withoutHeader("routingKey", data, acked)); fail("Did not throw"); } catch (IllegalArgumentException e) { // expected } assertEquals(false, acked.isDone()); verifyNoMoreInteractions(connection); }
@Test(timeout = 10000) public void testMultipleAppends() throws Exception { String segment = "123"; ByteBuf data = Unpooled.wrappedBuffer("Hello world\n".getBytes()); StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService(); @Cleanup EmbeddedChannel channel = createChannel(store); SegmentCreated created = (SegmentCreated) sendRequest(channel, new CreateSegment(1, segment, CreateSegment.NO_SCALE, 0, "")); assertEquals(segment, created.getSegment()); UUID uuid = UUID.randomUUID(); AppendSetup setup = (AppendSetup) sendRequest(channel, new SetupAppend(2, uuid, segment, "")); assertEquals(segment, setup.getSegment()); assertEquals(uuid, setup.getWriterId()); data.retain(); DataAppended ack = (DataAppended) sendRequest(channel, new Append(segment, uuid, 1, new Event(data))); assertEquals(uuid, ack.getWriterId()); assertEquals(1, ack.getEventNumber()); assertEquals(Long.MIN_VALUE, ack.getPreviousEventNumber()); DataAppended ack2 = (DataAppended) sendRequest(channel, new Append(segment, uuid, 2, new Event(data))); assertEquals(uuid, ack2.getWriterId()); assertEquals(2, ack2.getEventNumber()); assertEquals(1, ack2.getPreviousEventNumber()); }
@Test(timeout = 10000) public void testConnectAndFailedSetupAppendDueToTruncation() throws Exception { AtomicBoolean callbackInvoked = new AtomicBoolean(); Consumer<Segment> resendToSuccessorsCallback = segment -> { callbackInvoked.set(true); }; UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); ScheduledExecutorService executor = mock(ScheduledExecutorService.class); implementAsDirectExecutor(executor); // Ensure task submitted to executor is run inline. cf.setExecutor(executor); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); @Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, resendToSuccessorsCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).noSuchSegment(new WireCommands.NoSuchSegment(1, SEGMENT, "SomeException")); CompletableFuture<ClientConnection> connectionFuture = output.getConnection(); assertThrows(NoSuchSegmentException.class, () -> Futures.getThrowingException(connectionFuture)); assertTrue(callbackInvoked.get()); }
@Test(timeout = 10000) public void testConnectWithMultipleFailures() throws Exception { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); ScheduledExecutorService executor = mock(ScheduledExecutorService.class); implementAsDirectExecutor(executor); // Ensure task submitted to executor is run inline. cf.setExecutor(executor); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); @Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); //simulate a processing Failure and ensure SetupAppend is executed. cf.getProcessor(uri).processingFailure(new IOException()); verify(connection).send(new SetupAppend(2, cid, SEGMENT, "")); cf.getProcessor(uri).connectionDropped(); verify(connection).send(new SetupAppend(3, cid, SEGMENT, "")); cf.getProcessor(uri).wrongHost(new WireCommands.WrongHost(3, SEGMENT, "newHost", "SomeException")); verify(connection).send(new SetupAppend(4, cid, SEGMENT, "")); }
@Test(timeout = 10000) public void testSealedBeforeFlush() throws Exception { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); cf.setExecutor(executorService()); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); InOrder order = Mockito.inOrder(connection); SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); order.verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); ByteBuffer data = getBuffer("test"); CompletableFuture<Void> ack = new CompletableFuture<>(); output.write(PendingEvent.withoutHeader(null, data, ack)); order.verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null)); assertEquals(false, ack.isDone()); cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(1, SEGMENT, "SomeException")); output.getUnackedEventsOnSeal(); // this is invoked by the segmentSealedCallback. AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush()); }
@Test(timeout = 10000) public void testConnectAndFailedSetupAppend() throws Exception { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); ScheduledExecutorService executor = mock(ScheduledExecutorService.class); implementAsDirectExecutor(executor); // Ensure task submitted to executor is run inline. cf.setExecutor(executor); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); doThrow(ConnectionFailedException.class).doNothing().when(connection).send(any(SetupAppend.class)); cf.provideConnection(uri, connection); @Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); verify(connection).send(new SetupAppend(2, cid, SEGMENT, "")); }
@Test(timeout = 10000) public void testConnectAndConnectionDrop() throws Exception { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); ScheduledExecutorService executor = mock(ScheduledExecutorService.class); implementAsDirectExecutor(executor); // Ensure task submitted to executor is run inline. cf.setExecutor(executor); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); @Cleanup SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).connectionDropped(); // simulate a connection dropped //Ensure setup Append is invoked on the executor. verify(connection).send(new SetupAppend(2, cid, SEGMENT, "")); }
@Test public void sendReceivingAppend() throws Exception { String segment = "123"; ByteBuf data = Unpooled.wrappedBuffer("Hello world\n".getBytes()); StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService(); @Cleanup EmbeddedChannel channel = createChannel(store); SegmentCreated created = (SegmentCreated) sendRequest(channel, new CreateSegment(1, segment, CreateSegment.NO_SCALE, 0, "")); assertEquals(segment, created.getSegment()); UUID uuid = UUID.randomUUID(); AppendSetup setup = (AppendSetup) sendRequest(channel, new SetupAppend(2, uuid, segment, "")); assertEquals(segment, setup.getSegment()); assertEquals(uuid, setup.getWriterId()); DataAppended ack = (DataAppended) sendRequest(channel, new Append(segment, uuid, data.readableBytes(), new Event(data))); assertEquals(uuid, ack.getWriterId()); assertEquals(data.readableBytes(), ack.getEventNumber()); assertEquals(Long.MIN_VALUE, ack.getPreviousEventNumber()); }
@Test public void testVerySmallBlockSize() throws Exception { @Cleanup("release") ByteBuf fakeNetwork = ByteBufAllocator.DEFAULT.buffer(); byte[] content = new byte[100]; Arrays.fill(content, (byte) 1); Event event = new Event(Unpooled.wrappedBuffer(content)); Append msg = new Append("segment", writerId, 1, event); CommandEncoder commandEncoder = new CommandEncoder(new FixedBatchSizeTracker(3)); SetupAppend setupAppend = new SetupAppend(1, writerId, "segment", ""); commandEncoder.encode(null, setupAppend, fakeNetwork); appendDecoder.processCommand(setupAppend); ArrayList<Object> received = new ArrayList<>(); commandEncoder.encode(null, msg, fakeNetwork); read(fakeNetwork, received); assertEquals(2, received.size()); Append readAppend = (Append) received.get(1); assertEquals(msg.data.readableBytes(), readAppend.data.readableBytes()); assertEquals(content.length + TYPE_PLUS_LENGTH_SIZE, readAppend.data.readableBytes()); }
@Test(timeout = 10000) public void testConditionalSend() throws SegmentSealedException, ConnectionFailedException { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); cf.setExecutor(executorService()); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); sendAndVerifyEvent(cid, connection, output, getBuffer("test"), 1); verifyNoMoreInteractions(connection); }
@Test(timeout = 10000) public void testConnectAndSend() throws SegmentSealedException, ConnectionFailedException { UUID cid = UUID.randomUUID(); PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT); MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl(); cf.setExecutor(executorService()); MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf); ClientConnection connection = mock(ClientConnection.class); cf.provideConnection(uri, connection); SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, ""); output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); sendAndVerifyEvent(cid, connection, output, getBuffer("test"), 1); verifyNoMoreInteractions(connection); }
private ArrayList<Object> setupAppend(String testStream, UUID writerId, ByteBuf fakeNetwork) throws Exception { SetupAppend setupAppend = new SetupAppend(1, writerId, testStream, ""); encoder.encode(null, setupAppend, fakeNetwork); ArrayList<Object> received = new ArrayList<>(); WireCommand command = CommandDecoder.parseCommand(fakeNetwork); assertTrue(appendDecoder.acceptInboundMessage(command)); assertEquals(setupAppend, appendDecoder.processCommand(command)); return received; }
@Test public void testSetupAppend() throws IOException { testCommand(new WireCommands.SetupAppend(l, uuid, testString1, "")); }
@Test public void testSetupOnNonExistentSegment() throws Exception { String segment = "123"; StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService(); @Cleanup EmbeddedChannel channel = createChannel(store); UUID uuid = UUID.randomUUID(); NoSuchSegment setup = (NoSuchSegment) sendRequest(channel, new SetupAppend(1, uuid, segment, "")); assertEquals(segment, setup.getSegment()); }
@Test public void setupAppend() { processor.setupAppend(new WireCommands.SetupAppend(100L, UUID.randomUUID(), "segment", "token")); verify(connection).send(new WireCommands.AuthTokenCheckFailed(100L, "")); } }