/** * Creates a new instance of the ClientFactory class. * Note: ConnectionFactory and Controller is closed when {@link ClientFactoryImpl#close()} is invoked. * * @param scope The scope string. * @param controller The reference to Controller. * @param connectionFactory The reference to Connection Factory impl. */ @VisibleForTesting public ClientFactoryImpl(String scope, Controller controller, ConnectionFactory connectionFactory) { this(scope, controller, connectionFactory, new SegmentInputStreamFactoryImpl(controller, connectionFactory), new SegmentOutputStreamFactoryImpl(controller, connectionFactory), new ConditionalOutputStreamFactoryImpl(controller, connectionFactory), new SegmentMetadataClientFactoryImpl(controller, connectionFactory)); }
/** * Creates a new instance of ClientFactory class. * Note: Controller is closed when {@link ClientFactoryImpl#close()} is invoked. * * @param scope The scope string. * @param controller The reference to Controller. */ public ClientFactoryImpl(String scope, Controller controller) { Preconditions.checkNotNull(scope); Preconditions.checkNotNull(controller); this.scope = scope; this.controller = controller; this.connectionFactory = new ConnectionFactoryImpl(ClientConfig.builder().build()); this.inFactory = new SegmentInputStreamFactoryImpl(controller, connectionFactory); this.outFactory = new SegmentOutputStreamFactoryImpl(controller, connectionFactory); this.condFactory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory); this.metaFactory = new SegmentMetadataClientFactoryImpl(controller, connectionFactory); }
@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)); }
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());
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());
controller.createStream(scope, stream, StreamConfiguration.builder().build()); ConditionalOutputStreamFactoryImpl segmentClient = new ConditionalOutputStreamFactoryImpl(controller, clientCF);
@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)); }
controller.createStream(scope, stream, StreamConfiguration.builder().build()); ConditionalOutputStreamFactoryImpl segmentproducerClient = new ConditionalOutputStreamFactoryImpl(controller, clientCF);