@Override @SuppressWarnings("deprecation") public <T> RevisionedStreamClient<T> createRevisionedStreamClient(String streamName, Serializer<T> serializer, SynchronizerConfig config) { log.info("Creating revisioned stream client for stream: {} with synchronizer configuration: {}", streamName, config); Segment segment = new Segment(scope, streamName, 0); EventSegmentReader in = inFactory.createEventReaderForSegment(segment); // Segment sealed is not expected for Revisioned Stream Client. Consumer<Segment> segmentSealedCallBack = s -> { throw new IllegalStateException("RevisionedClient: Segmentsealed exception observed for segment:" + s); }; String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStreamName()), RuntimeException::new); SegmentOutputStream out = outFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config.getEventWriterConfig(), delegationToken); ConditionalOutputStream cond = condFactory.createConditionalOutputStream(segment, delegationToken, config.getEventWriterConfig()); SegmentMetadataClient meta = metaFactory.createSegmentMetadataClient(segment, delegationToken); return new RevisionedStreamClientImpl<>(segment, in, out, cond, meta, serializer); }
@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)); }
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);
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);