use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class StreamManagerImplTest method testStreamInfo.
@Test(timeout = 15000)
public void testStreamInfo() throws Exception {
final String streamName = "stream";
final Stream stream = new StreamImpl(defaultScope, streamName);
// 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);
streamManager.createScope(defaultScope);
streamManager.createStream(defaultScope, streamName, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
// fetch StreamInfo.
StreamInfo info = streamManager.getStreamInfo(defaultScope, streamName);
// validate results.
assertEquals(defaultScope, info.getScope());
assertEquals(streamName, info.getStreamName());
assertNotNull(info.getTailStreamCut());
assertEquals(stream, info.getTailStreamCut().asImpl().getStream());
assertEquals(3, info.getTailStreamCut().asImpl().getPositions().size());
assertNotNull(info.getHeadStreamCut());
assertEquals(stream, info.getHeadStreamCut().asImpl().getStream());
assertEquals(3, info.getHeadStreamCut().asImpl().getPositions().size());
assertFalse(info.isSealed());
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class StreamManagerImplTest method testForceDeleteScopeWithReaderGroups.
@Test
public void testForceDeleteScopeWithReaderGroups() throws ConnectionFailedException, DeleteScopeFailedException {
// 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));
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
WireCommands.DeleteSegment request = (WireCommands.DeleteSegment) invocation.getArgument(0);
connectionFactory.getProcessor(location).process(new WireCommands.SegmentDeleted(request.getRequestId(), request.getSegment()));
return null;
}
}).when(connection).send(Mockito.any(WireCommands.DeleteSegment.class));
connectionFactory.provideConnection(location, connection);
MockController mockController = spy(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 stream1 = "stream1";
String stream2 = "stream2";
String readerGroup1 = "readerGroup1";
String readerGroup2 = "readerGroup2";
ReaderGroupConfig config1 = ReaderGroupConfig.builder().stream(NameUtils.getScopedStreamName(scope, stream1)).build();
ReaderGroupConfig config2 = ReaderGroupConfig.builder().stream(NameUtils.getScopedStreamName(scope, stream2)).build();
streamManager.createScope(scope);
streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
streamManager.createStream(scope, stream2, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
Set<Stream> streams = Sets.newHashSet(streamManager.listStreams(scope));
assertEquals(2, streams.size());
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream1)));
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream2)));
mockController.createReaderGroup(scope, readerGroup1, config1);
mockController.createReaderGroup(scope, readerGroup2, config2);
// mock controller client to throw exceptions when attempting to get config for reader-group.
doAnswer(x -> Futures.failedFuture(new ControllerFailureException("Unable to access reader-group config"))).when(mockController).getReaderGroupConfig(scope, readerGroup1);
doAnswer(x -> new AsyncIterator<Stream>() {
final Iterator<Stream> iterator = new ArrayList<Stream>(Arrays.asList(new StreamImpl(scope, stream1), new StreamImpl(scope, stream2), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup1)), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup2)))).iterator();
@Override
public CompletableFuture<Stream> getNext() {
Stream next;
if (!iterator.hasNext()) {
next = null;
} else {
next = iterator.next();
}
return CompletableFuture.completedFuture(next);
}
}).when(mockController).listStreams(scope);
AssertExtensions.assertThrows("Should have thrown exception", () -> streamManager.deleteScope(scope, true), e -> Exceptions.unwrap(e) instanceof DeleteScopeFailedException);
// reset mock controller
reset(mockController);
streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
streamManager.createStream(scope, stream2, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
streams = Sets.newHashSet(streamManager.listStreams(scope));
assertEquals(2, streams.size());
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream1)));
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream2)));
mockController.createReaderGroup(scope, readerGroup1, config1);
mockController.createReaderGroup(scope, readerGroup2, config2);
// mock controller client to throw exceptions when attempting to delete the reader-group.
doAnswer(x -> Futures.failedFuture(new ControllerFailureException("Unable to delete reader-group"))).when(mockController).deleteReaderGroup(scope, readerGroup1, config1.getReaderGroupId());
doAnswer(x -> new AsyncIterator<Stream>() {
final Iterator<Stream> iterator = new ArrayList<Stream>(Arrays.asList(new StreamImpl(scope, stream1), new StreamImpl(scope, stream2), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup1)), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup2)))).iterator();
@Override
public CompletableFuture<Stream> getNext() {
Stream next;
if (!iterator.hasNext()) {
next = null;
} else {
next = iterator.next();
}
return CompletableFuture.completedFuture(next);
}
}).when(mockController).listStreams(scope);
AssertExtensions.assertThrows("Should have thrown exception", () -> streamManager.deleteScope(scope, true), e -> Exceptions.unwrap(e) instanceof DeleteScopeFailedException);
// reset mock controller
reset(mockController);
streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
streamManager.createStream(scope, stream2, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
streams = Sets.newHashSet(streamManager.listStreams(scope));
assertEquals(2, streams.size());
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream1)));
assertTrue(streams.stream().anyMatch(x -> x.getStreamName().equals(stream2)));
mockController.createReaderGroup(scope, readerGroup1, config1);
mockController.createReaderGroup(scope, readerGroup2, config2);
// mock controller client to throw ReaderGroupNotFoundException when attempting to get the config of reader-group.
doAnswer(x -> Futures.failedFuture(new ReaderGroupNotFoundException("Reader-group does not exist"))).when(mockController).getReaderGroupConfig(scope, readerGroup1);
doAnswer(x -> new AsyncIterator<Stream>() {
final Iterator<Stream> iterator = new ArrayList<Stream>(Arrays.asList(new StreamImpl(scope, stream1), new StreamImpl(scope, stream2), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup1)), new StreamImpl(scope, NameUtils.getStreamForReaderGroup(readerGroup2)))).iterator();
@Override
public CompletableFuture<Stream> getNext() {
Stream next;
if (!iterator.hasNext()) {
next = null;
} else {
next = iterator.next();
}
return CompletableFuture.completedFuture(next);
}
}).when(mockController).listStreams(scope);
assertTrue(streamManager.deleteScope(scope, true));
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class ByteStreamWriterTest 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();
clientFactory = new ByteStreamClientImpl(SCOPE, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
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");
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testFailureWhileRetransmittingInflight.
@Test(timeout = 10000)
public void testFailureWhileRetransmittingInflight() throws ConnectionFailedException {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
// Ensure task submitted to executor is run inline.
implementAsDirectExecutor(executor);
cf.setExecutor(executor);
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
ClientConnection connection = mock(ClientConnection.class);
InOrder inOrder = inOrder(connection);
cf.provideConnection(uri, connection);
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
output.write(PendingEvent.withoutHeader(null, getBuffer("test1"), new CompletableFuture<>()));
output.write(PendingEvent.withoutHeader(null, getBuffer("test2"), new CompletableFuture<>()));
doAnswer(new Answer<Void>() {
boolean failed = false;
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
CompletedCallback callback = (CompletedCallback) invocation.getArgument(1);
if (failed) {
callback.complete(null);
} else {
failed = true;
callback.complete(new ConnectionFailedException("Injected"));
}
return null;
}
}).when(connection).sendAsync(ArgumentMatchers.<List<Append>>any(), Mockito.any(CompletedCallback.class));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
return null;
}
}).doNothing().when(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
cf.getProcessor(uri).connectionDropped();
AssertExtensions.assertBlocks(() -> output.write(PendingEvent.withoutHeader(null, getBuffer("test3"), new CompletableFuture<>())), () -> {
// the write should be blocked until the appendSetup is returned by the Segment stores.
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
});
output.write(PendingEvent.withoutHeader(null, getBuffer("test4"), new CompletableFuture<>()));
Append append1 = new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(getBuffer("test1")), null, output.getRequestId());
Append append2 = new Append(SEGMENT, cid, 2, 1, Unpooled.wrappedBuffer(getBuffer("test2")), null, output.getRequestId());
Append append3 = new Append(SEGMENT, cid, 3, 1, Unpooled.wrappedBuffer(getBuffer("test3")), null, output.getRequestId());
Append append4 = new Append(SEGMENT, cid, 4, 1, Unpooled.wrappedBuffer(getBuffer("test4")), null, output.getRequestId());
inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
inOrder.verify(connection).send(append1);
inOrder.verify(connection).send(append2);
inOrder.verify(connection).close();
inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
inOrder.verify(connection).close();
inOrder.verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
inOrder.verify(connection).sendAsync(eq(ImmutableList.of(append1, append2)), any());
inOrder.verify(connection).send(append3);
inOrder.verify(connection).send(append4);
verifyNoMoreInteractions(connection);
}
use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.
the class SegmentOutputStreamTest method testExceptionSealedCallback.
@Test(timeout = 10000)
public void testExceptionSealedCallback() throws Exception {
UUID cid = UUID.randomUUID();
PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
// Ensure task submitted to executor is run inline.
implementAsDirectExecutor(executor);
cf.setExecutor(executor);
MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf, true);
ClientConnection connection = mock(ClientConnection.class);
cf.provideConnection(uri, connection);
AtomicBoolean shouldThrow = new AtomicBoolean(true);
// call back which throws an exception.
Consumer<Segment> exceptionCallback = s -> {
if (shouldThrow.getAndSet(false)) {
throw new IllegalStateException();
}
};
@SuppressWarnings("resource") SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, true, controller, cf, cid, exceptionCallback, RETRY_SCHEDULE, DelegationTokenProviderFactory.createWithEmptyToken());
output.reconnect();
verify(connection).send(new SetupAppend(output.getRequestId(), cid, SEGMENT, ""));
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
ByteBuffer data = getBuffer("test");
CompletableFuture<Void> ack = new CompletableFuture<>();
output.write(PendingEvent.withoutHeader(null, data, ack));
assertEquals(false, ack.isDone());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cf.getProcessor(uri).appendSetup(new AppendSetup(output.getRequestId(), SEGMENT, cid, 0));
return null;
}
}).when(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
AssertExtensions.assertBlocks(() -> {
AssertExtensions.assertThrows(SegmentSealedException.class, () -> output.flush());
}, () -> {
cf.getProcessor(uri).segmentIsSealed(new WireCommands.SegmentIsSealed(output.getRequestId(), SEGMENT, "SomeException", 1));
output.getUnackedEventsOnSeal();
});
verify(connection).send(new WireCommands.KeepAlive());
verify(connection).send(new Append(SEGMENT, cid, 1, 1, Unpooled.wrappedBuffer(data), null, output.getRequestId()));
assertEquals(false, ack.isDone());
}
Aggregations