Search in sources :

Example 11 with SegmentRange

use of io.pravega.client.batch.SegmentRange in project pravega by pravega.

the class BatchClientSimpleTest method batchClientSimpleTest.

/**
 * This test verifies the basic functionality of {@link BatchClientFactory}, including stream metadata checks, segment
 * counts, parallel segment reads and reads with offsets using stream cuts.
 */
@Test
@SuppressWarnings("deprecation")
public void batchClientSimpleTest() {
    final int totalEvents = RG_PARALLELISM * 100;
    final int offsetEvents = RG_PARALLELISM * 20;
    final int batchIterations = 4;
    final Stream stream = Stream.of(SCOPE, STREAM);
    final ClientConfig clientConfig = Utils.buildClientConfig(controllerURI);
    @Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(clientConfig);
    ControllerImpl controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(clientConfig).build(), connectionFactory.getInternalExecutor());
    @Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl(SCOPE, controller, connectionFactory);
    @Cleanup BatchClientFactory batchClient = BatchClientFactory.withScope(SCOPE, clientConfig);
    log.info("Invoking batchClientSimpleTest test with Controller URI: {}", controllerURI);
    @Cleanup ReaderGroupManager groupManager = ReaderGroupManager.withScope(SCOPE, clientConfig);
    groupManager.createReaderGroup(READER_GROUP, ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(SCOPE + "/" + STREAM).build());
    ReaderGroup readerGroup = groupManager.getReaderGroup(READER_GROUP);
    log.info("Writing events to stream");
    // Write events to the Stream.
    writeEvents(clientFactory, STREAM, totalEvents);
    // Instantiate readers to consume from Stream up to truncatedEvents.
    List<CompletableFuture<Integer>> futures = readEventFutures(clientFactory, READER_GROUP, RG_PARALLELISM, offsetEvents);
    Futures.allOf(futures).join();
    // Create a stream cut on the specified offset position.
    Checkpoint cp = readerGroup.initiateCheckpoint("batchClientCheckpoint", executor).join();
    StreamCut streamCut = cp.asImpl().getPositions().values().iterator().next();
    // Instantiate the batch client and assert it provides correct stream info.
    log.debug("Creating batch client.");
    StreamInfo streamInfo = streamManager.getStreamInfo(SCOPE, stream.getStreamName());
    log.debug("Validating stream metadata fields.");
    assertEquals("Expected Stream name: ", STREAM, streamInfo.getStreamName());
    assertEquals("Expected Scope name: ", SCOPE, streamInfo.getScope());
    // Test that we can read events from parallel segments from an offset onwards.
    log.debug("Reading events from stream cut onwards in parallel.");
    List<SegmentRange> ranges = Lists.newArrayList(batchClient.getSegments(stream, streamCut, StreamCut.UNBOUNDED).getIterator());
    assertEquals("Expected events read: ", totalEvents - offsetEvents, readFromRanges(ranges, batchClient));
    // Emulate the behavior of Hadoop client: i) Get tail of Stream, ii) Read from current point until tail, iii) repeat.
    log.debug("Reading in batch iterations.");
    StreamCut currentTailStreamCut = streamManager.getStreamInfo(SCOPE, stream.getStreamName()).getTailStreamCut();
    int readEvents = 0;
    for (int i = 0; i < batchIterations; i++) {
        writeEvents(clientFactory, STREAM, totalEvents);
        // Read all the existing events in parallel segments from the previous tail to the current one.
        ranges = Lists.newArrayList(batchClient.getSegments(stream, currentTailStreamCut, StreamCut.UNBOUNDED).getIterator());
        assertEquals("Expected number of segments: ", RG_PARALLELISM, ranges.size());
        readEvents += readFromRanges(ranges, batchClient);
        log.debug("Events read in parallel so far: {}.", readEvents);
        currentTailStreamCut = streamManager.getStreamInfo(SCOPE, stream.getStreamName()).getTailStreamCut();
    }
    assertEquals("Expected events read: .", totalEvents * batchIterations, readEvents);
    // Truncate the stream in first place.
    log.debug("Truncating stream at event {}.", offsetEvents);
    assertTrue(controller.truncateStream(SCOPE, STREAM, streamCut).join());
    // Test the batch client when we select to start reading a Stream from a truncation point.
    StreamCut initialPosition = streamManager.getStreamInfo(SCOPE, stream.getStreamName()).getHeadStreamCut();
    List<SegmentRange> newRanges = Lists.newArrayList(batchClient.getSegments(stream, initialPosition, StreamCut.UNBOUNDED).getIterator());
    assertEquals("Expected events read: ", (totalEvents - offsetEvents) + totalEvents * batchIterations, readFromRanges(newRanges, batchClient));
    log.debug("Events correctly read from Stream: simple batch client test passed.");
}
Also used : SegmentRange(io.pravega.client.batch.SegmentRange) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) StreamCut(io.pravega.client.stream.StreamCut) ReaderGroup(io.pravega.client.stream.ReaderGroup) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Cleanup(lombok.Cleanup) Checkpoint(io.pravega.client.stream.Checkpoint) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Checkpoint(io.pravega.client.stream.Checkpoint) BatchClientFactory(io.pravega.client.BatchClientFactory) StreamInfo(io.pravega.client.admin.StreamInfo) Stream(io.pravega.client.stream.Stream) ClientConfig(io.pravega.client.ClientConfig) Test(org.junit.Test)

Example 12 with SegmentRange

use of io.pravega.client.batch.SegmentRange in project pravega by pravega.

the class BatchClientSimpleTest method readFromRanges.

// Start utils region
private int readFromRanges(List<SegmentRange> ranges, BatchClientFactory batchClient) {
    List<CompletableFuture<Integer>> eventCounts = ranges.parallelStream().map(range -> CompletableFuture.supplyAsync(() -> batchClient.readSegment(range, new JavaSerializer<>())).thenApplyAsync(segmentIterator -> {
        log.debug("Thread " + Thread.currentThread().getId() + " reading events.");
        int numEvents = Lists.newArrayList(segmentIterator).size();
        segmentIterator.close();
        return numEvents;
    })).collect(Collectors.toList());
    return eventCounts.stream().map(CompletableFuture::join).mapToInt(Integer::intValue).sum();
}
Also used : StreamCut(io.pravega.client.stream.StreamCut) MarathonException(mesosphere.marathon.client.MarathonException) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) StreamManager(io.pravega.client.admin.StreamManager) RunWith(org.junit.runner.RunWith) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) ReaderGroup(io.pravega.client.stream.ReaderGroup) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Service(io.pravega.test.system.framework.services.Service) Lists(com.google.common.collect.Lists) SegmentRange(io.pravega.client.batch.SegmentRange) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Stream(io.pravega.client.stream.Stream) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) Checkpoint(io.pravega.client.stream.Checkpoint) URI(java.net.URI) Utils(io.pravega.test.system.framework.Utils) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) BatchClientFactory(io.pravega.client.BatchClientFactory) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Before(org.junit.Before) Environment(io.pravega.test.system.framework.Environment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StreamInfo(io.pravega.client.admin.StreamInfo) Collectors(java.util.stream.Collectors) ControllerImplConfig(io.pravega.client.control.impl.ControllerImplConfig) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) RandomFactory(io.pravega.common.hash.RandomFactory) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Futures(io.pravega.common.concurrent.Futures) SystemTestRunner(io.pravega.test.system.framework.SystemTestRunner) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) CompletableFuture(java.util.concurrent.CompletableFuture) Checkpoint(io.pravega.client.stream.Checkpoint)

Example 13 with SegmentRange

use of io.pravega.client.batch.SegmentRange in project pravega by pravega.

the class BatchClientImplTest method testGetSegmentsWithStreamCut.

@Test(timeout = 5000)
public void testGetSegmentsWithStreamCut() throws Exception {
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    MockConnectionFactoryImpl connectionFactory = getMockConnectionFactory(location);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, false);
    Stream stream = createStream(SCOPE, STREAM, 3, mockController);
    @Cleanup BatchClientFactoryImpl client = new BatchClientFactoryImpl(mockController, ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    Iterator<SegmentRange> boundedSegments = client.getSegments(stream, getStreamCut(5L, 0, 1, 2), getStreamCut(15L, 0, 1, 2)).getIterator();
    assertTrue(boundedSegments.hasNext());
    assertEquals(0L, boundedSegments.next().asImpl().getSegment().getSegmentId());
    assertTrue(boundedSegments.hasNext());
    assertEquals(1L, boundedSegments.next().asImpl().getSegment().getSegmentId());
    assertTrue(boundedSegments.hasNext());
    assertEquals(2L, boundedSegments.next().asImpl().getSegment().getSegmentId());
    assertFalse(boundedSegments.hasNext());
}
Also used : SegmentRange(io.pravega.client.batch.SegmentRange) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Stream(io.pravega.client.stream.Stream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 14 with SegmentRange

use of io.pravega.client.batch.SegmentRange in project pravega by pravega.

the class BatchClientImplTest method testGetSegmentsWithMultipleSegments.

@Test(timeout = 5000)
public void testGetSegmentsWithMultipleSegments() throws Exception {
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    @Cleanup MockConnectionFactoryImpl connectionFactory = getMockConnectionFactory(location);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, false);
    MockController stubbedController = spy(mockController);
    Stream stream = createStream(SCOPE, STREAM, 2, stubbedController);
    Set<Segment> segments = ImmutableSet.<Segment>builder().add(new Segment(SCOPE, STREAM, 0L), new Segment(SCOPE, STREAM, 1L), new Segment(SCOPE, STREAM, 2L)).build();
    // Setup mock.
    doReturn(CompletableFuture.completedFuture(new StreamSegmentSuccessors(segments, ""))).when(stubbedController).getSegments(any(StreamCut.class), any(StreamCut.class));
    @Cleanup BatchClientFactoryImpl client = new BatchClientFactoryImpl(stubbedController, ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    Iterator<SegmentRange> segmentIterator = client.getSegments(stream, null, null).getIterator();
    assertTrue(segmentIterator.hasNext());
    assertEquals(0L, segmentIterator.next().asImpl().getSegment().getSegmentId());
    assertTrue(segmentIterator.hasNext());
    assertEquals(1L, segmentIterator.next().asImpl().getSegment().getSegmentId());
    assertTrue(segmentIterator.hasNext());
    assertEquals(2L, segmentIterator.next().asImpl().getSegment().getSegmentId());
    assertFalse(segmentIterator.hasNext());
}
Also used : SegmentRange(io.pravega.client.batch.SegmentRange) StreamCut(io.pravega.client.stream.StreamCut) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) StreamSegmentSuccessors(io.pravega.client.stream.impl.StreamSegmentSuccessors) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Stream(io.pravega.client.stream.Stream) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 15 with SegmentRange

use of io.pravega.client.batch.SegmentRange in project pravega by pravega.

the class BatchClientImplTest method testGetSegmentsWithNullStreamCut.

@Test(timeout = 5000)
public void testGetSegmentsWithNullStreamCut() throws Exception {
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    @Cleanup MockConnectionFactoryImpl connectionFactory = getMockConnectionFactory(location);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, false);
    Stream stream = createStream(SCOPE, STREAM, 3, mockController);
    @Cleanup BatchClientFactoryImpl client = new BatchClientFactoryImpl(mockController, ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    Iterator<SegmentRange> segments = client.getSegments(stream, null, null).getIterator();
    assertTrue(segments.hasNext());
    assertEquals(0L, segments.next().asImpl().getSegment().getSegmentId());
    assertTrue(segments.hasNext());
    assertEquals(1L, segments.next().asImpl().getSegment().getSegmentId());
    assertTrue(segments.hasNext());
    assertEquals(2L, segments.next().asImpl().getSegment().getSegmentId());
    assertFalse(segments.hasNext());
}
Also used : SegmentRange(io.pravega.client.batch.SegmentRange) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Stream(io.pravega.client.stream.Stream) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

SegmentRange (io.pravega.client.batch.SegmentRange)15 Test (org.junit.Test)12 Cleanup (lombok.Cleanup)11 Stream (io.pravega.client.stream.Stream)9 Segment (io.pravega.client.segment.impl.Segment)8 BatchClientFactory (io.pravega.client.BatchClientFactory)7 StreamCut (io.pravega.client.stream.StreamCut)6 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)5 MockController (io.pravega.client.stream.mock.MockController)5 ClientConfig (io.pravega.client.ClientConfig)4 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)4 EventStreamClientFactory (io.pravega.client.EventStreamClientFactory)3 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)3 StreamInfo (io.pravega.client.admin.StreamInfo)3 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)3 StreamSegmentSuccessors (io.pravega.client.stream.impl.StreamSegmentSuccessors)3 ArrayList (java.util.ArrayList)3 StreamManager (io.pravega.client.admin.StreamManager)2 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)2 ControllerImpl (io.pravega.client.control.impl.ControllerImpl)2