use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class ConnectionPoolingTest method testConnectionPooling.
@Test
public void testConnectionPooling() throws Exception {
ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create((this.ssl ? "tls://" : "tcp://") + "localhost")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).maxConnectionsPerSegmentStore(1).build();
@Cleanup SocketConnectionFactoryImpl factory = new SocketConnectionFactoryImpl(clientConfig, 1);
@Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(clientConfig, factory);
ArrayBlockingQueue<WireCommands.SegmentRead> msgRead = new ArrayBlockingQueue<>(10);
FailingReplyProcessor rp = new FailingReplyProcessor() {
@Override
public void connectionDropped() {
}
@Override
public void segmentRead(WireCommands.SegmentRead data) {
msgRead.add(data);
}
@Override
public void processingFailure(Exception error) {
}
@Override
public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
}
};
Flow flow1 = new Flow(1, 0);
@Cleanup ClientConnection connection1 = connectionPool.getClientConnection(flow1, new PravegaNodeUri("localhost", port), rp).join();
connection1.send(readRequestGenerator.apply(flow1.asLong()));
WireCommands.SegmentRead msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
assertEquals(1, connectionPool.getActiveChannels().size());
// create a second connection, since the max number of connections is 1 this should reuse the same connection.
Flow flow2 = new Flow(2, 0);
CompletableFuture<ClientConnection> cf = new CompletableFuture<>();
connectionPool.getClientConnection(flow2, new PravegaNodeUri("localhost", port), rp, cf);
@Cleanup ClientConnection connection2 = cf.join();
// send data over connection2 and verify.
connection2.send(readRequestGenerator.apply(flow2.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
assertEquals(1, connectionPool.getActiveChannels().size());
assertEquals(1, factory.getOpenSocketCount());
// send data over connection1 and verify.
connection1.send(readRequestGenerator.apply(flow1.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
// send data over connection2 and verify.
connection2.send(readRequestGenerator.apply(flow2.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
// close a client connection, this should not close the channel.
connection2.close();
assertThrows(ConnectionFailedException.class, () -> connection2.send(readRequestGenerator.apply(flow2.asLong())));
// verify we are able to send data over connection1.
connection1.send(readRequestGenerator.apply(flow1.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
// close connection1
connection1.close();
assertThrows(ConnectionFailedException.class, () -> connection1.send(readRequestGenerator.apply(flow2.asLong())));
AssertExtensions.assertEventuallyEquals(0, () -> {
connectionPool.pruneUnusedConnections();
return factory.getOpenSocketCount();
}, 10000);
assertEquals(0, connectionPool.getActiveChannels().size());
}
use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class ConnectionPoolingTest method testNonPooling.
@Test
public void testNonPooling() throws Exception {
ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create((this.ssl ? "tls://" : "tcp://") + "localhost")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).maxConnectionsPerSegmentStore(1).build();
@Cleanup SocketConnectionFactoryImpl factory = new SocketConnectionFactoryImpl(clientConfig, 1);
@Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(clientConfig, factory);
ArrayBlockingQueue<WireCommands.SegmentRead> msgRead = new ArrayBlockingQueue<>(10);
FailingReplyProcessor rp = new FailingReplyProcessor() {
@Override
public void connectionDropped() {
}
@Override
public void segmentRead(WireCommands.SegmentRead data) {
msgRead.add(data);
}
@Override
public void processingFailure(Exception error) {
}
@Override
public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
}
};
Flow flow1 = new Flow(1, 0);
@Cleanup ClientConnection connection1 = connectionPool.getClientConnection(flow1, new PravegaNodeUri("localhost", port), rp).join();
connection1.send(readRequestGenerator.apply(flow1.asLong()));
WireCommands.SegmentRead msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
assertEquals(1, connectionPool.getActiveChannels().size());
// create a second connection, since not using a flow.
@Cleanup ClientConnection connection2 = connectionPool.getClientConnection(new PravegaNodeUri("localhost", port), rp).join();
Flow flow2 = new Flow(2, 0);
// send data over connection2 and verify.
connection2.send(readRequestGenerator.apply(flow2.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
assertEquals(1, connectionPool.getActiveChannels().size());
assertEquals(2, factory.getOpenSocketCount());
// send data over connection1 and verify.
connection1.send(readRequestGenerator.apply(flow1.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
// send data over connection2 and verify.
connection2.send(readRequestGenerator.apply(flow2.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow2.asLong()), msg);
// close a client connection, this should not close the channel.
connection2.close();
assertThrows(ConnectionFailedException.class, () -> connection2.send(readRequestGenerator.apply(flow2.asLong())));
// verify we are able to send data over connection1.
connection1.send(readRequestGenerator.apply(flow1.asLong()));
msg = msgRead.take();
assertEquals(readResponseGenerator.apply(flow1.asLong()), msg);
// close connection1
connection1.close();
assertThrows(ConnectionFailedException.class, () -> connection1.send(readRequestGenerator.apply(flow2.asLong())));
AssertExtensions.assertEventuallyEquals(0, () -> {
connectionPool.pruneUnusedConnections();
return factory.getOpenSocketCount();
}, 10000);
assertEquals(0, connectionPool.getActiveChannels().size());
}
use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class ConnectionPoolingTest method testConcurrentRequests.
@Test
public void testConcurrentRequests() throws Exception {
ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create((this.ssl ? "tls://" : "tcp://") + "localhost")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).maxConnectionsPerSegmentStore(1).build();
@Cleanup SocketConnectionFactoryImpl factory = new SocketConnectionFactoryImpl(clientConfig, 1);
@Cleanup ConnectionPoolImpl connectionPool = new ConnectionPoolImpl(clientConfig, factory);
ArrayBlockingQueue<WireCommands.SegmentRead> msgRead = new ArrayBlockingQueue<>(10);
FailingReplyProcessor rp = new FailingReplyProcessor() {
@Override
public void connectionDropped() {
}
@Override
public void segmentRead(WireCommands.SegmentRead data) {
msgRead.add(data);
}
@Override
public void processingFailure(Exception error) {
}
@Override
public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
}
};
Flow flow1 = new Flow(1, 0);
ClientConnection connection1 = connectionPool.getClientConnection(flow1, new PravegaNodeUri("localhost", port), rp).join();
// create a second connection, since the max number of connections is 1 this should reuse the same connection.
Flow flow2 = new Flow(2, 0);
ClientConnection connection2 = connectionPool.getClientConnection(flow2, new PravegaNodeUri("localhost", port), rp).join();
assertEquals(1, factory.getOpenSocketCount());
assertEquals(1, connectionPool.getActiveChannels().size());
connection1.send(readRequestGenerator.apply(flow1.asLong()));
connection2.send(readRequestGenerator.apply(flow2.asLong()));
List<WireCommands.SegmentRead> msgs = new ArrayList<WireCommands.SegmentRead>();
msgs.add(msgRead.take());
msgs.add(msgRead.take());
assertTrue(msgs.contains(readResponseGenerator.apply(flow1.asLong())));
assertTrue(msgs.contains(readResponseGenerator.apply(flow1.asLong())));
assertEquals(1, factory.getOpenSocketCount());
connection1.close();
connection2.close();
AssertExtensions.assertEventuallyEquals(0, () -> {
connectionPool.pruneUnusedConnections();
return factory.getOpenSocketCount();
}, 10000);
assertEquals(0, connectionPool.getActiveChannels().size());
}
use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class ConditionalOutputStreamTest method testRetriesOnTokenExpiry.
@SneakyThrows
@Test(timeout = 10000)
public void testRetriesOnTokenExpiry() {
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
Segment segment = new Segment("scope", "testWrite", 1);
@Cleanup ConditionalOutputStream objectUnderTest = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
ByteBuffer data = ByteBuffer.allocate(10);
ClientConnection clientConnection = Mockito.mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
connectionFactory.provideConnection(location, clientConnection);
setupAppend(connectionFactory, segment, clientConnection, location);
final AtomicLong retryCounter = 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 (retryCounter.getAndIncrement() < 2) {
processor.process(new WireCommands.AuthTokenCheckFailed(argument.getRequestId(), "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED));
} else {
processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
}
return null;
}
}).when(clientConnection).send(any(ConditionalAppend.class));
assertTrue(objectUnderTest.write(data, 0));
assertEquals(3, retryCounter.get());
}
use of io.pravega.shared.protocol.netty.PravegaNodeUri in project pravega by pravega.
the class ConditionalOutputStreamTest method testWrite.
@Test(timeout = 10000)
public void testWrite() throws ConnectionFailedException, SegmentSealedException {
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
Segment segment = new Segment("scope", "testWrite", 1);
@Cleanup ConditionalOutputStream cOut = factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), 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);
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 (argument.getExpectedOffset() == 0 || argument.getExpectedOffset() == 2) {
processor.process(new WireCommands.DataAppended(argument.getRequestId(), argument.getWriterId(), argument.getEventNumber(), 0, -1));
} else {
processor.process(new WireCommands.ConditionalCheckFailed(argument.getWriterId(), argument.getEventNumber(), argument.getRequestId()));
}
return null;
}
}).when(mock).send(any(ConditionalAppend.class));
assertTrue(cOut.write(data, 0));
assertFalse(cOut.write(data, 1));
assertTrue(cOut.write(data, 2));
assertFalse(cOut.write(data, 3));
}
Aggregations