use of io.pravega.client.admin.impl.ReaderGroupManagerImpl in project pravega by pravega.
the class EndToEndTruncationTest method testWriteDuringScaleAndTruncation.
@Test(timeout = 50000)
public void testWriteDuringScaleAndTruncation() throws Exception {
String streamName = "testWriteDuringScaleAndTruncation";
Stream stream = new StreamImpl("test", streamName);
StreamConfiguration config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 2)).build();
LocalController controller = (LocalController) PRAVEGA.getLocalController();
controller.createScope("test").get();
controller.createStream("test", streamName, config).get();
config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
controller.updateStream("test", streamName, config).get();
@Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(ClientConfig.builder().controllerURI(PRAVEGA.getControllerURI()).build());
@Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl("test", controller, connectionFactory);
@Cleanup EventStreamWriter<String> writer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().build());
// routing key "0" translates to key 0.8. This write happens to segment 1.
writer.writeEvent("0", "truncationTest1").get();
// Peform scaling operations on the stream.
ImmutableMap<Double, Double> singleSegmentKeyRange = ImmutableMap.of(0.0, 1.0);
ImmutableMap<Double, Double> twoSegmentKeyRange = ImmutableMap.of(0.0, 0.5, 0.5, 1.0);
// scale down to 1 segment.
assertTrue("Stream Scale down", controller.scaleStream(stream, Lists.newArrayList(0L, 1L), singleSegmentKeyRange, executorService()).getFuture().get());
// scale up to 2 segments.
assertTrue("Stream Scale up", controller.scaleStream(stream, Lists.newArrayList(computeSegmentId(2, 1)), twoSegmentKeyRange, executorService()).getFuture().get());
// scale down to 1 segment.
assertTrue("Stream Scale down", controller.scaleStream(stream, Lists.newArrayList(computeSegmentId(3, 2), computeSegmentId(4, 2)), singleSegmentKeyRange, executorService()).getFuture().get());
// scale up to 2 segments.
assertTrue("Stream Scale up", controller.scaleStream(stream, Lists.newArrayList(computeSegmentId(5, 3)), twoSegmentKeyRange, executorService()).getFuture().get());
// truncateStream.
Map<Long, Long> streamCutPositions = new HashMap<>();
streamCutPositions.put(computeSegmentId(3, 2), 0L);
streamCutPositions.put(computeSegmentId(4, 2), 0L);
assertTrue("Truncate stream", controller.truncateStream("test", streamName, streamCutPositions).get());
// write an event.
writer.writeEvent("0", "truncationTest3");
writer.flush();
// Read the event back.
String group = "testWriteDuringScaleAndTruncation-group";
@Cleanup ReaderGroupManager groupManager = new ReaderGroupManagerImpl("test", controller, clientFactory);
groupManager.createReaderGroup(group, ReaderGroupConfig.builder().disableAutomaticCheckpoints().groupRefreshTimeMillis(0).stream("test/" + streamName).build());
@Cleanup EventStreamReader<String> reader = clientFactory.createReader("readerId", group, new JavaSerializer<>(), ReaderConfig.builder().build());
EventRead<String> event = reader.readNextEvent(200);
assertNull(event.getEvent());
groupManager.getReaderGroup(group).initiateCheckpoint("cp1", executorService());
event = reader.readNextEvent(2000);
assertEquals("cp1", event.getCheckpointName());
event = reader.readNextEvent(200);
assertNull(event.getEvent());
groupManager.getReaderGroup(group).initiateCheckpoint("cp2", executorService());
event = reader.readNextEvent(2000);
assertEquals("cp2", event.getCheckpointName());
event = reader.readNextEvent(10000);
assertEquals("truncationTest3", event.getEvent());
}
use of io.pravega.client.admin.impl.ReaderGroupManagerImpl 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