use of io.pravega.client.stream.ReaderGroupNotFoundException 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.stream.ReaderGroupNotFoundException in project pravega by pravega.
the class ReaderGroupManagerImpl method deleteReaderGroup.
@Override
public void deleteReaderGroup(String groupName) {
UUID readerGroupId = null;
ReaderGroupConfig syncConfig = null;
try {
@Cleanup StateSynchronizer<ReaderGroupState> synchronizer = clientFactory.createStateSynchronizer(NameUtils.getStreamForReaderGroup(groupName), new ReaderGroupStateUpdatesSerializer(), new ReaderGroupStateInitSerializer(), SynchronizerConfig.builder().build());
synchronizer.fetchUpdates();
syncConfig = synchronizer.getState().getConfig();
readerGroupId = syncConfig.getReaderGroupId();
if (ReaderGroupConfig.DEFAULT_UUID.equals(syncConfig.getReaderGroupId()) && ReaderGroupConfig.DEFAULT_GENERATION == syncConfig.getGeneration()) {
// migrate RG case
try {
controller.getReaderGroupConfig(scope, groupName).thenCompose(conf -> controller.deleteReaderGroup(scope, groupName, conf.getReaderGroupId())).join();
} catch (ReaderGroupNotFoundException ex) {
controller.sealStream(scope, getStreamForReaderGroup(groupName)).thenCompose(b -> controller.deleteStream(scope, getStreamForReaderGroup(groupName))).exceptionally(e -> {
log.warn("Failed to delete ReaderGroup Stream {}", getStreamForReaderGroup(groupName), e);
throw Exceptions.sneakyThrow(e);
}).join();
}
return;
}
} catch (InvalidStreamException ex) {
log.warn("State-Synchronizer Stream for ReaderGroup {} was not found.", NameUtils.getScopedReaderGroupName(scope, groupName));
// if the State Synchronizer Stream was deleted, but the RG still exists, get Id from Controller
readerGroupId = getAndHandleExceptions(controller.getReaderGroupConfig(scope, groupName), RuntimeException::new).getReaderGroupId();
}
// normal delete
getAndHandleExceptions(controller.deleteReaderGroup(scope, groupName, readerGroupId), RuntimeException::new);
}
use of io.pravega.client.stream.ReaderGroupNotFoundException in project pravega by pravega.
the class StreamMetadataResourceImpl method getReaderGroup.
@Override
public void getReaderGroup(final String scopeName, final String readerGroupName, final SecurityContext securityContext, final AsyncResponse asyncResponse) {
long traceId = LoggerHelpers.traceEnter(log, "getReaderGroup");
long requestId = requestIdGenerator.nextLong();
try {
restAuthHelper.authenticateAuthorize(getAuthorizationHeader(), authorizationResource.ofReaderGroupInScope(scopeName, readerGroupName), READ);
} catch (AuthException e) {
log.warn(requestId, "Get reader group for {} failed due to authentication failure.", scopeName + "/" + readerGroupName);
asyncResponse.resume(Response.status(Status.fromStatusCode(e.getResponseCode())).build());
LoggerHelpers.traceLeave(log, "getReaderGroup", traceId);
return;
}
ClientFactoryImpl clientFactory = new ClientFactoryImpl(scopeName, this.localController, this.clientConfig);
ReaderGroupManager readerGroupManager = new ReaderGroupManagerImpl(scopeName, this.localController, clientFactory);
ReaderGroupProperty readerGroupProperty = new ReaderGroupProperty();
readerGroupProperty.setScopeName(scopeName);
readerGroupProperty.setReaderGroupName(readerGroupName);
CompletableFuture.supplyAsync(() -> {
ReaderGroup readerGroup = readerGroupManager.getReaderGroup(readerGroupName);
readerGroupProperty.setOnlineReaderIds(new ArrayList<>(readerGroup.getOnlineReaders()));
readerGroupProperty.setStreamList(new ArrayList<>(readerGroup.getStreamNames()));
return Response.status(Status.OK).entity(readerGroupProperty).build();
}, controllerService.getExecutor()).exceptionally(exception -> {
log.warn(requestId, "getReaderGroup for {} failed with exception: ", readerGroupName, exception);
if (exception.getCause() instanceof ReaderGroupNotFoundException) {
return Response.status(Status.NOT_FOUND).build();
} else {
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
}).thenAccept(response -> {
asyncResponse.resume(response);
readerGroupManager.close();
clientFactory.close();
LoggerHelpers.traceLeave(log, "getReaderGroup", traceId);
});
}
Aggregations