use of io.pravega.test.integration.ReadWriteUtils.writeEvents in project pravega by pravega.
the class EndToEndTruncationTest method testSegmentTruncationWhileReading.
/**
* This test checks the behavior of a reader (or group of readers) based on whether segment truncation takes place
* while reading (first part of the test) or before starting reading (second part).
*
* @throws InterruptedException If the current thread is interrupted while waiting for the Controller service.
*/
@Test(timeout = 60000)
public void testSegmentTruncationWhileReading() throws InterruptedException {
final int totalEvents = 100;
final String scope = "truncationTests";
final String streamName = "testSegmentTruncationWhileReading";
final String readerGroupName = "RGTestSegmentTruncationWhileReading";
StreamConfiguration config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
LocalController controller = (LocalController) PRAVEGA.getLocalController();
controller.createScope(scope).join();
controller.createStream(scope, streamName, config).join();
@Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(ClientConfig.builder().controllerURI(PRAVEGA.getControllerURI()).build());
@Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory);
// Write half of totalEvents to the Stream.
writeEvents(clientFactory, streamName, totalEvents / 2);
// Seal current segment (0) and split it into two segments (1,2).
Stream stream = new StreamImpl(scope, streamName);
Map<Double, Double> map = new HashMap<>();
map.put(0.0, 0.5);
map.put(0.5, 1.0);
assertTrue(controller.scaleStream(stream, Lists.newArrayList(0L), map, executorService()).getFuture().join());
long one = computeSegmentId(1, 1);
long two = computeSegmentId(2, 1);
// Write rest of events to the new Stream segments.
ReadWriteUtils.writeEvents(clientFactory, streamName, totalEvents, totalEvents / 2);
// Instantiate readers to consume from Stream.
@Cleanup ReaderGroupManager groupManager = new ReaderGroupManagerImpl(scope, controller, clientFactory);
groupManager.createReaderGroup(readerGroupName, ReaderGroupConfig.builder().automaticCheckpointIntervalMillis(100).stream(Stream.of(scope, streamName)).build());
@Cleanup EventStreamReader<String> reader = clientFactory.createReader(String.valueOf(0), readerGroupName, new UTF8StringSerializer(), ReaderConfig.builder().build());
int read = 0;
while (read < 75) {
EventRead<String> event = reader.readNextEvent(1000);
if (event.getEvent() != null) {
read++;
}
}
// Let readers to consume some events and truncate segment while readers are consuming events
Exceptions.handleInterrupted(() -> Thread.sleep(500));
Map<Long, Long> streamCutPositions = new HashMap<>();
streamCutPositions.put(one, 0L);
streamCutPositions.put(two, 0L);
assertTrue(controller.truncateStream(scope, streamName, streamCutPositions).join());
// Wait for readers to complete and assert that they have read all the events (totalEvents).
while (read < totalEvents) {
EventRead<String> event = reader.readNextEvent(1000);
if (event.getEvent() != null) {
read++;
}
}
assertEquals(read, totalEvents);
assertEquals(null, reader.readNextEvent(0).getEvent());
// Assert that from the truncation call onwards, the available segments are the ones after scaling.
List<Long> currentSegments = controller.getCurrentSegments(scope, streamName).join().getSegments().stream().map(Segment::getSegmentId).sorted().collect(toList());
currentSegments.removeAll(Lists.newArrayList(one, two));
assertTrue(currentSegments.isEmpty());
// The new set of readers, should only read the events beyond truncation point (segments 1 and 2).
final String newReaderGroupName = readerGroupName + "new";
groupManager.createReaderGroup(newReaderGroupName, ReaderGroupConfig.builder().stream(Stream.of(scope, streamName)).build());
List<CompletableFuture<Integer>> futures = readEvents(clientFactory, newReaderGroupName, 1);
Futures.allOf(futures).join();
assertEquals((int) futures.stream().map(CompletableFuture::join).reduce((a, b) -> a + b).get(), totalEvents / 2);
}
Aggregations