use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class LargeEventWriterTest method testSegmentSealed.
@Test(timeout = 5000)
public void testSegmentSealed() throws ConnectionFailedException, SegmentSealedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment = new Segment(scope, streamName, 0);
@Cleanup InlineExecutor executor = new InlineExecutor();
EventWriterConfig config = EventWriterConfig.builder().enableLargeEvents(true).build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
MockController controller = spy(new MockController("localhost", 0, connectionFactory, false));
controller.createScope(scope).join();
controller.createStream(scope, streamName, StreamConfiguration.builder().build());
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
ClientConnection connection = Mockito.mock(ClientConnection.class);
connectionFactory.provideConnection(location, connection);
SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
Mockito.when(streamFactory.createOutputStreamForSegment(any(), any(), any(), any())).thenReturn(outputStream);
AtomicBoolean failed = new AtomicBoolean(false);
AtomicBoolean succeeded = new AtomicBoolean(false);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
failed.set(true);
connectionFactory.getProcessor(location).process(new SegmentIsSealed(argument.getRequestId(), segment.getScopedName(), "stacktrace", 0));
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
succeeded.set(true);
connectionFactory.getProcessor(location).process(new SegmentCreated(argument.getRequestId(), "transient-segment"));
return null;
}
}).when(connection).send(any(CreateTransientSegment.class));
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
List<Long> predecessors = Arrays.asList(0L);
return CompletableFuture.completedFuture(new StreamSegmentsWithPredecessors(ImmutableMap.of(new SegmentWithRange(new Segment(scope, streamName, NameUtils.computeSegmentId(1, 1)), 0.0, 0.5), predecessors, new SegmentWithRange(new Segment(scope, streamName, NameUtils.computeSegmentId(2, 1)), 0.5, 1.0), predecessors), ""));
}
}).when(controller).getSuccessors(segment);
answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
answerRequest(connectionFactory, connection, location, ConditionalBlockEnd.class, r -> {
ByteBuf data = r.getData();
return new DataAppended(r.getRequestId(), r.getWriterId(), r.getEventNumber(), r.getEventNumber() - 1, r.getExpectedOffset() + data.readableBytes());
});
answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
});
@Cleanup EventStreamWriter<byte[]> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, new ByteArraySerializer(), config, executor, executor, connectionFactory);
writer.writeEvent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
writer.writeEvent(new byte[Serializer.MAX_EVENT_SIZE * 2]);
assertTrue(failed.get());
assertTrue(succeeded.get());
InOrder order = Mockito.inOrder(connection, outputStream);
order.verify(outputStream).write(any(PendingEvent.class));
order.verify(outputStream).flush();
order.verify(connection, times(2)).send(any(CreateTransientSegment.class));
order.verify(connection).send(any(SetupAppend.class));
order.verify(connection, times(3)).send(any(ConditionalBlockEnd.class));
order.verify(connection).send(any(MergeSegments.class));
order.verify(connection).close();
order.verifyNoMoreInteractions();
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class LargeEventWriterTest method testEventStreamWriter.
@Test(timeout = 5000)
public void testEventStreamWriter() throws ConnectionFailedException, SegmentSealedException {
String scope = "scope";
String streamName = "stream";
StreamImpl stream = new StreamImpl(scope, streamName);
Segment segment = new Segment(scope, streamName, 0);
@Cleanup InlineExecutor executor = new InlineExecutor();
EventWriterConfig config = EventWriterConfig.builder().enableLargeEvents(true).build();
SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
MockController controller = new MockController("localhost", 0, connectionFactory, false);
controller.createScope(scope).join();
controller.createStream(scope, streamName, StreamConfiguration.builder().build());
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
ClientConnection connection = Mockito.mock(ClientConnection.class);
connectionFactory.provideConnection(location, connection);
SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
answerRequest(connectionFactory, connection, location, ConditionalBlockEnd.class, r -> {
ByteBuf data = r.getData();
return new DataAppended(r.getRequestId(), r.getWriterId(), r.getEventNumber(), r.getEventNumber() - 1, r.getExpectedOffset() + data.readableBytes());
});
answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
});
@Cleanup EventStreamWriter<byte[]> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, new ByteArraySerializer(), config, executor, executor, connectionFactory);
writer.writeEvent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
writer.writeEvent(new byte[Serializer.MAX_EVENT_SIZE * 2]);
InOrder order = Mockito.inOrder(connection, outputStream);
order.verify(outputStream).write(any(PendingEvent.class));
order.verify(outputStream).flush();
order.verify(connection).send(any(CreateTransientSegment.class));
order.verify(connection).send(any(SetupAppend.class));
order.verify(connection, times(3)).send(any(ConditionalBlockEnd.class));
order.verify(connection).send(any(MergeSegments.class));
order.verify(connection).close();
order.verifyNoMoreInteractions();
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class MockController method sendRequestOverNewConnection.
private <T> void sendRequestOverNewConnection(WireCommand request, ReplyProcessor replyProcessor, CompletableFuture<T> resultFuture) {
ClientConnection connection = getAndHandleExceptions(connectionPool.getClientConnection(Flow.from(((Request) request).getRequestId()), new PravegaNodeUri(endpoint, port), replyProcessor), RuntimeException::new);
resultFuture.whenComplete((result, e) -> {
connection.close();
});
try {
connection.send(request);
} catch (ConnectionFailedException cfe) {
resultFuture.completeExceptionally(cfe);
}
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class ByteStreanWriterImplTest method setup.
@Before
public void setup() {
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
connectionFactory = new MockConnectionFactoryImpl();
ClientConnection connection = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, connection);
controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
controller.createScope(SCOPE);
controller.createStream(SCOPE, STREAM, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
StreamSegments segments = Futures.getThrowingException(controller.getCurrentSegments(SCOPE, STREAM));
Preconditions.checkState(segments.getNumberOfSegments() > 0, "Stream is sealed");
Preconditions.checkState(segments.getNumberOfSegments() == 1, "Stream is configured with more than one segment");
Segment segment = segments.getSegments().iterator().next();
EventWriterConfig config = EventWriterConfig.builder().retryAttempts(Integer.MAX_VALUE).build();
DelegationTokenProvider tokenProvider = DelegationTokenProviderFactory.create(controller, segment, AccessOperation.WRITE);
mockWriter = new ByteStreamWriterImpl(streamFactory.createOutputStreamForSegment(segment, config, tokenProvider), streamFactory.createSegmentMetadataClient(segment, tokenProvider));
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class StreamManagerImplTest method testListScopes.
@Test(timeout = 10000)
public void testListScopes() throws ConnectionFailedException {
// Setup Mocks
ClientConnection connection = mock(ClientConnection.class);
PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
connectionFactory.provideConnection(location, connection);
MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true);
ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
@Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
String scope = "scope";
String scope1 = "scope1";
String scope2 = "scope2";
String scope3 = "scope3";
String stream1 = "stream1";
streamManager.createScope(scope);
streamManager.createScope(scope1);
streamManager.createScope(scope2);
streamManager.createScope(scope3);
streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
ArrayList<String> result = Lists.newArrayList(streamManager.listScopes());
assertEquals(result.size(), 4);
assertTrue(streamManager.checkScopeExists(scope));
assertFalse(streamManager.checkScopeExists("nonExistent"));
assertTrue(streamManager.checkStreamExists(scope, stream1));
assertFalse(streamManager.checkStreamExists(scope, "nonExistent"));
assertFalse(streamManager.checkStreamExists("nonExistent", "nonExistent"));
}
Aggregations