@Override public Revision writeConditionally(Revision latestRevision, T value) { boolean wasWritten; long offset = latestRevision.asImpl().getOffsetInSegment(); ByteBuffer serialized = serializer.serialize(value); int size = serialized.remaining(); synchronized (lock) { try { wasWritten = conditional.write(serialized, offset); } catch (SegmentSealedException e) { throw new CorruptedStateException("Unexpected end of segment ", e); } } if (wasWritten) { long newOffset = getNewOffset(offset, size); log.trace("Wrote from {} to {}", offset, newOffset); return new RevisionImpl(segment, newOffset, 0); } else { log.trace("Write failed at offset {}", offset); return null; } }
replies.add(true); replies.add(false); assertTrue(cOut.write(data, 0)); assertFalse(cOut.write(data, 1)); AssertExtensions.assertBlocks(() -> { assertTrue(cOut.write(data, 2)); }, () -> { replies.add(true); }); AssertExtensions.assertBlocks(() -> { assertFalse(cOut.write(data, 3)); }, () -> { replies.add(false); }); AssertExtensions.assertBlocks(() -> { assertTrue(cOut.write(data, 4)); }, () -> { AssertExtensions.assertBlocks(() -> { assertFalse(cOut.write(data, 5)); }, () -> { replies.add(true); }); AssertExtensions.assertBlocks(() -> { assertFalse(cOut.write(data, 6)); }, () -> { AssertExtensions.assertBlocks(() -> { assertTrue(cOut.write(data, 7)); }, () -> {
@Test public void testRetries() throws ConnectionFailedException, SegmentSealedException { MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl(); MockController controller = new MockController("localhost", 0, connectionFactory); ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory); Segment segment = new Segment("scope", "testWrite", 1); ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, "token", EventWriterConfig.builder().build()); ByteBuffer data = ByteBuffer.allocate(10); ClientConnection mock = Mockito.mock(ClientConnection.class); PravegaNodeUri location = new PravegaNodeUri("localhost", 0); connectionFactory.provideConnection(location, mock); setupAppend(connectionFactory, segment, mock, location); final AtomicLong count = new AtomicLong(0); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0); ReplyProcessor processor = connectionFactory.getProcessor(location); if (count.getAndIncrement() < 2) { processor.connectionDropped(); } else { processor.process(new WireCommands.DataAppended(argument.getWriterId(), argument.getEventNumber(), 0)); } return null; } }).when(mock).sendAsync(any(ConditionalAppend.class), any(ClientConnection.CompletedCallback.class)); assertTrue(cOut.write(data, 0)); assertEquals(3, count.get()); }
@Test(timeout = 10000) public void testSegmentSealed() throws ConnectionFailedException, SegmentSealedException { MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl(); MockController controller = new MockController("localhost", 0, connectionFactory); ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory); Segment segment = new Segment("scope", "testWrite", 1); ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, "token", EventWriterConfig.builder().build()); ByteBuffer data = ByteBuffer.allocate(10); String mockClientReplyStackTrace = "SomeException"; ClientConnection mock = Mockito.mock(ClientConnection.class); PravegaNodeUri location = new PravegaNodeUri("localhost", 0); connectionFactory.provideConnection(location, mock); setupAppend(connectionFactory, segment, mock, location); Mockito.doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { ConditionalAppend argument = (ConditionalAppend) invocation.getArgument(0); ReplyProcessor processor = connectionFactory.getProcessor(location); processor.process(new WireCommands.SegmentIsSealed(argument.getEventNumber(), segment.getScopedName(), mockClientReplyStackTrace)); return null; } }).when(mock).sendAsync(any(ConditionalAppend.class), any(ClientConnection.CompletedCallback.class)); AssertExtensions.assertThrows(SegmentSealedException.class, () -> cOut.write(data, 0)); }
@Test public void testClose() throws SegmentSealedException { MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl(); MockController controller = new MockController("localhost", 0, connectionFactory); ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory); Segment segment = new Segment("scope", "testWrite", 1); ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, "token", EventWriterConfig.builder().build()); cOut.close(); AssertExtensions.assertThrows(IllegalStateException.class, () -> cOut.write(ByteBuffer.allocate(0), 0)); }
assertTrue(out.write(ByteBuffer.wrap(testString), 0)); 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);
@Cleanup ConditionalOutputStream out = segmentClient.createConditionalOutputStream(segment, "", EventWriterConfig.builder().build()); assertTrue(out.write(ByteBuffer.wrap(payload), 0)); for (Channel c : clientCF.getActiveChannels()) { c.close(); assertTrue(out.write(ByteBuffer.wrap(payload), payload.length + WireCommands.TYPE_PLUS_LENGTH_SIZE)); SegmentMetadataClient metadataClient = new SegmentMetadataClientFactoryImpl(controller, clientCF).createSegmentMetadataClient(segment, ""); assertEquals((payload.length + WireCommands.TYPE_PLUS_LENGTH_SIZE) * 2,
@Test public void appendThroughConditionalClient() throws Exception { String endpoint = "localhost"; int port = TestUtils.getAvailableListenPort(); String testString = "Hello world\n"; String scope = "scope"; String stream = "stream"; StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService(); TableStore tableStore = serviceBuilder.createTableStoreService(); @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store, tableStore); server.startListening(); @Cleanup ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build()); Controller controller = new MockController(endpoint, port, clientCF); controller.createScope(scope); controller.createStream(scope, stream, StreamConfiguration.builder().build()); ConditionalOutputStreamFactoryImpl segmentClient = new ConditionalOutputStreamFactoryImpl(controller, clientCF); Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next(); @Cleanup ConditionalOutputStream out = segmentClient.createConditionalOutputStream(segment, "", EventWriterConfig.builder().build()); assertTrue(out.write(ByteBuffer.wrap(testString.getBytes()), 0)); }