AppendProcessor.TIMEOUT); verify(connection, atLeast(0)).resumeReading(); verify(connection).send(new AppendSetup(1, segment1, clientId1, 0)); verify(connection).send(new DataAppended(clientId1, data.length, 0)); verify(connection).send(new AppendSetup(2, segment2, clientId2, 0)); verify(connection).send(new DataAppended(clientId2, data.length, 0)); verifyNoMoreInteractions(connection);
@Override public boolean write(ByteBuffer data, long expectedOffset) throws SegmentSealedException { synchronized (lock) { //Used to preserver order. long appendSequence = requestIdGenerator.get(); return retrySchedule.retryingOn(ConnectionFailedException.class) .throwingOn(SegmentSealedException.class) .run(() -> { if (client == null || client.isClosed()) { client = new RawClient(controller, connectionFactory, segmentId); long requestId = requestIdGenerator.get(); log.debug("Setting up append on segment: {}", segmentId); SetupAppend setup = new SetupAppend(requestId, writerId, segmentId.getScopedName(), delegationToken); val reply = client.sendRequest(requestId, setup); AppendSetup appendSetup = transformAppendSetup(reply.join()); if (appendSetup.getLastEventNumber() >= appendSequence) { return true; } } val request = new ConditionalAppend(writerId, appendSequence, expectedOffset, new Event(Unpooled.wrappedBuffer(data))); val reply = client.sendRequest(appendSequence, request); return transformDataAppended(reply.join()); }); } }
InOrder inOrder = Mockito.inOrder(connection); inOrder.verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); ByteBuffer data = getBuffer("test"); output.close(); }, () -> { cf.getProcessor(uri).appendSetup(new AppendSetup(2, SEGMENT, cid, 0)); inOrder.verify(connection).send(new WireCommands.KeepAlive()); inOrder.verify(connection).send(new SetupAppend(2, cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); output.write(PendingEvent.withoutHeader(null, getBuffer("test1"), new CompletableFuture<>())); output.write(PendingEvent.withoutHeader(null, getBuffer("test2"), new CompletableFuture<>())); @Override public Void answer(InvocationOnMock invocation) throws Throwable { cf.getProcessor(uri).appendSetup(new AppendSetup(2, SEGMENT, cid, 0)); return null; AssertExtensions.assertBlocks(() -> output.write(PendingEvent.withoutHeader(null, getBuffer("test3"), new CompletableFuture<>())), () -> { cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); }); output.write(PendingEvent.withoutHeader(null, getBuffer("test4"), new CompletableFuture<>()));
InOrder inOrder = Mockito.inOrder(connection); inOrder.verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); ByteBuffer data = getBuffer("test"); output.write(PendingEvent.withoutHeader(null, data, acked2)); }, () -> { cf.getProcessor(uri).appendSetup(new AppendSetup(2, SEGMENT, cid, 0)); }); inOrder.verify(connection).send(append);
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");
output.reconnect(); verify(connection).send(new SetupAppend(1, cid, SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); ByteBuffer data = getBuffer("test");
verify(store).append(streamSegmentName, data.length, data, updateEventNumber(clientId, 2, 1, 1), AppendProcessor.TIMEOUT); verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0)); verify(connection, atLeast(0)).resumeReading(); verify(connection).send(new DataAppended(clientId, 1, 0));
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");
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");
verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 1), AppendProcessor.TIMEOUT); verify(store).append(streamSegmentName, 0L, data, updateEventNumber(clientId, 2, 1, 1), AppendProcessor.TIMEOUT); verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0)); verify(connection, atLeast(0)).resumeReading(); verify(connection).send(new DataAppended(clientId, 1, 0));
latestEventNumbers.putIfAbsent(Pair.of(newSegment, writer), eventNumber); connection.send(new AppendSetup(setupAppend.getRequestId(), newSegment, writer, eventNumber));
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");
cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0)); output.write(PendingEvent.withoutHeader(null, getBuffer("test1"), new CompletableFuture<>())); output.write(PendingEvent.withoutHeader(null, getBuffer("test2"), new CompletableFuture<>())); cf.getProcessor(uri).connectionDropped(); AssertExtensions.assertBlocks(() -> output.write(PendingEvent.withoutHeader(null, getBuffer("test3"), new CompletableFuture<>())), () -> cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0))); output.write(PendingEvent.withoutHeader(null, getBuffer("test4"), new CompletableFuture<>()));
verify(connection).send(new AppendSetup(1, streamSegmentName, clientId, 0)); verify(connection, atLeast(0)).resumeReading(); verify(connection).close();
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");
output.reconnect(); order.verify(connection).send(new SetupAppend(1, cid, TXN_SEGMENT, "")); cf.getProcessor(uri).appendSetup(new AppendSetup(1, TXN_SEGMENT, cid, 0)); ByteBuffer data = getBuffer("test");
@Test(timeout = 10000) public void testNoSuchSegment() 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"); //Write an Event. 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()); //writer is not complete until a response from Segment Store is received. //Simulate a No Such Segment while waiting on flush. AssertExtensions.assertBlocks(() -> { AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush()); }, () -> { cf.getProcessor(uri).noSuchSegment(new WireCommands.NoSuchSegment(1, SEGMENT, "SomeException")); output.getUnackedEventsOnSeal(); }); AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush()); }
@Test(timeout = 10000) public void testSealedAfterFlush() 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()); AssertExtensions.assertBlocks(() -> { AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush()); }, () -> { cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(1, SEGMENT, "SomeException")); output.getUnackedEventsOnSeal(); }); AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush()); }
@Test public void testTransactionAppend() throws Exception { String streamSegmentName = "transactionSegment#transaction.01234567890123456789012345678901"; 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); DynamicLogger mockedDynamicLogger = Mockito.mock(DynamicLogger.class); AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null, mockedDynamicLogger); setupGetAttributes(streamSegmentName, clientId, store); CompletableFuture<Void> result = CompletableFuture.completedFuture(null); 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 DataAppended(clientId, data.length, 0L)); verifyNoMoreInteractions(connection); verifyNoMoreInteractions(store); verify(mockedDynamicLogger).incCounterValue(globalMetricName(SEGMENT_WRITE_BYTES), 8); verify(mockedDynamicLogger).incCounterValue(globalMetricName(SEGMENT_WRITE_EVENTS), 1); verify(mockedDynamicLogger, never()).incCounterValue(nameFromSegment(SEGMENT_WRITE_BYTES, streamSegmentName), 8); verify(mockedDynamicLogger, never()).incCounterValue(nameFromSegment(SEGMENT_WRITE_EVENTS, streamSegmentName), 1); }