Search in sources :

Example 41 with StreamManager

use of io.pravega.client.admin.StreamManager in project pravega by pravega.

the class ScopeTest method testListStreams.

@Test(timeout = 30000)
public void testListStreams() throws Exception {
    final String scope = "test";
    final String streamName1 = "test1";
    final String streamName2 = "test2";
    final String streamName3 = "test3";
    final Map<String, Integer> foundCount = new HashMap<>();
    foundCount.put(streamName1, 0);
    foundCount.put(streamName2, 0);
    foundCount.put(streamName3, 0);
    foundCount.put(NameUtils.getMarkStreamForStream(streamName1), 0);
    foundCount.put(NameUtils.getMarkStreamForStream(streamName2), 0);
    foundCount.put(NameUtils.getMarkStreamForStream(streamName3), 0);
    StreamConfiguration config = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.byEventRate(10, 2, 1)).build();
    @Cleanup Controller controller = controllerWrapper.getController();
    ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create("tcp://localhost")).build();
    @Cleanup ConnectionPool cp = new ConnectionPoolImpl(clientConfig, new SocketConnectionFactoryImpl(clientConfig));
    controllerWrapper.getControllerService().createScope(scope, 0L).get();
    controller.createStream(scope, streamName1, config).get();
    controller.createStream(scope, streamName2, config).get();
    controller.createStream(scope, streamName3, config).get();
    @Cleanup StreamManager manager = new StreamManagerImpl(controller, cp);
    Iterator<Stream> iterator = manager.listStreams(scope);
    assertTrue(iterator.hasNext());
    Stream next = iterator.next();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    assertTrue(iterator.hasNext());
    next = iterator.next();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    assertTrue(iterator.hasNext());
    next = iterator.next();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    assertTrue(iterator.hasNext());
    next = iterator.next();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    assertTrue(iterator.hasNext());
    next = iterator.next();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    assertTrue(iterator.hasNext());
    next = iterator.next();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    assertFalse(iterator.hasNext());
    assertTrue(foundCount.entrySet().stream().allMatch(x -> x.getValue() == 1));
    AsyncIterator<Stream> asyncIterator = controller.listStreams(scope);
    next = asyncIterator.getNext().join();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    next = asyncIterator.getNext().join();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    next = asyncIterator.getNext().join();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    next = asyncIterator.getNext().join();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    next = asyncIterator.getNext().join();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    next = asyncIterator.getNext().join();
    foundCount.computeIfPresent(next.getStreamName(), (x, y) -> ++y);
    next = asyncIterator.getNext().join();
    assertNull(next);
    assertTrue(foundCount.entrySet().stream().allMatch(x -> x.getValue() == 2));
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) Arrays(java.util.Arrays) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) AssertExtensions(io.pravega.test.common.AssertExtensions) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) Cleanup(lombok.Cleanup) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) Collections.singletonList(java.util.Collections.singletonList) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Stream(io.pravega.client.stream.Stream) After(org.junit.After) Map(java.util.Map) Status(io.grpc.Status) URI(java.net.URI) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) StreamManagerImpl(io.pravega.client.admin.impl.StreamManagerImpl) KeyValueTableManagerImpl(io.pravega.client.admin.impl.KeyValueTableManagerImpl) Assert.assertFalse(org.junit.Assert.assertFalse) TestUtils(io.pravega.test.common.TestUtils) ControllerWrapper(io.pravega.test.integration.demo.ControllerWrapper) Controller(io.pravega.client.control.impl.Controller) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) StreamManager(io.pravega.client.admin.StreamManager) HashMap(java.util.HashMap) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) KeyValueTableManager(io.pravega.client.admin.KeyValueTableManager) TestingServerStarter(io.pravega.test.common.TestingServerStarter) TestingServer(org.apache.curator.test.TestingServer) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Before(org.junit.Before) NameUtils(io.pravega.shared.NameUtils) Iterator(java.util.Iterator) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) Assert.assertTrue(org.junit.Assert.assertTrue) AsyncIterator(io.pravega.common.util.AsyncIterator) Test(org.junit.Test) StatusRuntimeException(io.grpc.StatusRuntimeException) Assert.assertNull(org.junit.Assert.assertNull) NameUtils.getScopedStreamName(io.pravega.shared.NameUtils.getScopedStreamName) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) HashMap(java.util.HashMap) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) StreamManagerImpl(io.pravega.client.admin.impl.StreamManagerImpl) Controller(io.pravega.client.control.impl.Controller) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Cleanup(lombok.Cleanup) StreamManager(io.pravega.client.admin.StreamManager) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Stream(io.pravega.client.stream.Stream) ClientConfig(io.pravega.client.ClientConfig) Test(org.junit.Test)

Example 42 with StreamManager

use of io.pravega.client.admin.StreamManager in project pravega by pravega.

the class ReaderCheckpointTest method readerCheckpointTest.

@Test
public void readerCheckpointTest() {
    controllerURI = fetchControllerURI();
    final ClientConfig clientConfig = Utils.buildClientConfig(controllerURI);
    StreamManager streamManager = StreamManager.create(clientConfig);
    assertTrue("Creating Scope", streamManager.createScope(SCOPE_1));
    assertTrue("Creating stream", streamManager.createStream(SCOPE_1, STREAM, streamConfig));
    @Cleanup ReaderGroupManager readerGroupManager = ReaderGroupManager.withScope(SCOPE_1, clientConfig);
    readerGroupManager.createReaderGroup(READER_GROUP_NAME, ReaderGroupConfig.builder().stream(io.pravega.client.stream.Stream.of(SCOPE_1, STREAM)).build());
    @Cleanup ReaderGroup readerGroup = readerGroupManager.getReaderGroup(READER_GROUP_NAME);
    int startInclusive = 1;
    int endExclusive = 100;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(SCOPE_1, IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(SCOPE_1, startInclusive, endExclusive);
    // initiate checkpoint100
    Checkpoint checkPoint100 = createCheckPointAndVerify(readerGroup, "batch100");
    // write and read events 100 to 200
    startInclusive = 100;
    endExclusive = 200;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(SCOPE_1, IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(SCOPE_1, startInclusive, endExclusive);
    // reset to check point 100
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(checkPoint100).build());
    readEventsAndVerify(SCOPE_1, 100, endExclusive);
    // initiate checkpoint200
    Checkpoint checkPoint200 = createCheckPointAndVerify(readerGroup, "batch200");
    // write and read events 200 to 300
    startInclusive = 200;
    endExclusive = 300;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(SCOPE_1, IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(SCOPE_1, startInclusive, endExclusive);
    // reset back to checkpoint 200
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(checkPoint200).build());
    readEventsAndVerify(SCOPE_1, 200, endExclusive);
    // reset back to checkpoint 100
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(checkPoint100).build());
    readEventsAndVerify(SCOPE_1, 100, endExclusive);
    // clean up
    readerGroupManager.deleteReaderGroup(READER_GROUP_NAME);
}
Also used : ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Checkpoint(io.pravega.client.stream.Checkpoint) StreamManager(io.pravega.client.admin.StreamManager) ReaderGroup(io.pravega.client.stream.ReaderGroup) ClientConfig(io.pravega.client.ClientConfig) Cleanup(lombok.Cleanup) Checkpoint(io.pravega.client.stream.Checkpoint) Test(org.junit.Test)

Example 43 with StreamManager

use of io.pravega.client.admin.StreamManager in project pravega by pravega.

the class ReaderCheckpointTest method generateStreamCutsTest.

@Test
public void generateStreamCutsTest() {
    controllerURI = fetchControllerURI();
    final ClientConfig clientConfig = Utils.buildClientConfig(controllerURI);
    StreamManager streamManager = StreamManager.create(clientConfig);
    assertTrue("Creating Scope", streamManager.createScope(SCOPE_2));
    assertTrue("Creating stream", streamManager.createStream(SCOPE_2, STREAM, streamConfig));
    @Cleanup ReaderGroupManager readerGroupManager = ReaderGroupManager.withScope(SCOPE_2, clientConfig);
    readerGroupManager.createReaderGroup(READER_GROUP_NAME, ReaderGroupConfig.builder().stream(io.pravega.client.stream.Stream.of(SCOPE_2, STREAM)).groupRefreshTimeMillis(GROUP_REFRESH_TIME_MILLIS).build());
    @Cleanup ReaderGroup readerGroup = readerGroupManager.getReaderGroup(READER_GROUP_NAME);
    int startInclusive = 1;
    int endExclusive = 100;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(SCOPE_2, IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(SCOPE_2, startInclusive, endExclusive);
    // Obtain StreamCuts at 100th event.
    Map<Stream, StreamCut> cutAt100 = generateStreamCuts(readerGroup);
    // Write and read events 100 to 200
    startInclusive = 100;
    endExclusive = 200;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(SCOPE_2, IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(SCOPE_2, startInclusive, endExclusive);
    // Reset to stream cut pointing to 100th event.
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromStreamCuts(cutAt100).build());
    readEventsAndVerify(SCOPE_2, 100, endExclusive);
    // Obtain stream cut at 200th event.
    Map<Stream, StreamCut> cutAt200 = generateStreamCuts(readerGroup);
    // Write and read events 200 to 300.
    startInclusive = 200;
    endExclusive = 300;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(SCOPE_2, IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(SCOPE_2, startInclusive, endExclusive);
    // Reset back to stream cut pointing to 200th event.
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromStreamCuts(cutAt200).build());
    readEventsAndVerify(SCOPE_2, 200, endExclusive);
    // Reset back to stream cut pointing to 100th event.
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromStreamCuts(cutAt100).build());
    readEventsAndVerify(SCOPE_2, 100, endExclusive);
    // clean up
    readerGroupManager.deleteReaderGroup(READER_GROUP_NAME);
}
Also used : ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) StreamCut(io.pravega.client.stream.StreamCut) StreamManager(io.pravega.client.admin.StreamManager) ReaderGroup(io.pravega.client.stream.ReaderGroup) Stream(io.pravega.client.stream.Stream) IntStream(java.util.stream.IntStream) ClientConfig(io.pravega.client.ClientConfig) Cleanup(lombok.Cleanup) Checkpoint(io.pravega.client.stream.Checkpoint) Test(org.junit.Test)

Example 44 with StreamManager

use of io.pravega.client.admin.StreamManager in project pravega by pravega.

the class ByteStreamTest method readWriteTest.

@Test(timeout = 30000)
public void readWriteTest() throws IOException {
    String scope = "ByteStreamTest";
    String stream = "readWriteTest";
    StreamConfiguration config = StreamConfiguration.builder().build();
    @Cleanup StreamManager streamManager = new StreamManagerImpl(PRAVEGA.getLocalController(), Mockito.mock(ConnectionPool.class));
    // create a scope
    Boolean createScopeStatus = streamManager.createScope(scope);
    log.info("Create scope status {}", createScopeStatus);
    // create a stream
    Boolean createStreamStatus = streamManager.createStream(scope, stream, config);
    log.info("Create stream status {}", createStreamStatus);
    @Cleanup ByteStreamClientFactory client = createClientFactory(scope);
    byte[] payload = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    byte[] readBuffer = new byte[10];
    @Cleanup ByteStreamWriter writer = client.createByteStreamWriter(stream);
    @Cleanup ByteStreamReader reader = client.createByteStreamReader(stream);
    AssertExtensions.assertBlocks(() -> reader.read(readBuffer), () -> writer.write(payload));
    assertArrayEquals(payload, readBuffer);
    Arrays.fill(readBuffer, (byte) 0);
    writer.write(payload);
    writer.write(payload);
    writer.write(payload);
    writer.closeAndSeal();
    assertEquals(10, reader.read(readBuffer));
    assertArrayEquals(payload, readBuffer);
    for (int i = 0; i < 10; i++) {
        assertEquals(i, reader.read());
    }
    Arrays.fill(readBuffer, (byte) -1);
    assertEquals(5, reader.read(readBuffer, 0, 5));
    assertEquals(5, reader.read(readBuffer, 5, 5));
    assertArrayEquals(payload, readBuffer);
    assertEquals(-1, reader.read());
    assertEquals(-1, reader.read(readBuffer));
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) StreamManager(io.pravega.client.admin.StreamManager) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ByteStreamReader(io.pravega.client.byteStream.ByteStreamReader) StreamManagerImpl(io.pravega.client.admin.impl.StreamManagerImpl) Cleanup(lombok.Cleanup) ByteStreamClientFactory(io.pravega.client.ByteStreamClientFactory) ByteStreamWriter(io.pravega.client.byteStream.ByteStreamWriter) Test(org.junit.Test)

Example 45 with StreamManager

use of io.pravega.client.admin.StreamManager in project pravega by pravega.

the class ByteStreamTest method readWriteTestTruncate.

@Test(timeout = 30000)
public void readWriteTestTruncate() throws IOException {
    String scope = "ByteStreamTest";
    String stream = "readWriteTestTruncate";
    StreamConfiguration config = StreamConfiguration.builder().build();
    @Cleanup StreamManager streamManager = new StreamManagerImpl(PRAVEGA.getLocalController(), Mockito.mock(ConnectionPool.class));
    // create a scope
    Boolean createScopeStatus = streamManager.createScope(scope);
    log.info("Create scope status {}", createScopeStatus);
    // create a stream
    Boolean createStreamStatus = streamManager.createStream(scope, stream, config);
    log.info("Create stream status {}", createStreamStatus);
    @Cleanup ByteStreamClientFactory client = createClientFactory(scope);
    byte[] payload = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    byte[] readBuffer = new byte[10];
    @Cleanup ByteStreamWriter writer = client.createByteStreamWriter(stream);
    @Cleanup ByteStreamReader reader = client.createByteStreamReader(stream);
    // Verify reads and writes.
    AssertExtensions.assertBlocks(() -> reader.read(readBuffer), () -> writer.write(payload));
    assertArrayEquals(payload, readBuffer);
    // Truncate data before offset 5
    writer.truncateDataBefore(5);
    // seek to an invalid truncated offset and verify if truncation is successful.
    reader.seekToOffset(reader.fetchHeadOffset() - 1);
    assertThrows(SegmentTruncatedException.class, reader::read);
    // seek to the new head and verify if we are able to read the data.
    byte[] data = new byte[] { 5, 6, 7, 8, 9 };
    reader.seekToOffset(reader.fetchHeadOffset());
    byte[] readBuffer1 = new byte[5];
    int bytesRead = reader.read(readBuffer1);
    assertEquals(5, bytesRead);
    assertArrayEquals(readBuffer1, data);
    // create a new byteStream Reader.
    ByteStreamReader reader1 = client.createByteStreamReader(stream);
    // verify it is able to read
    readBuffer1 = new byte[5];
    bytesRead = reader1.read(readBuffer1);
    // verify if all the bytes are read.
    assertEquals(5, bytesRead);
    assertArrayEquals(readBuffer1, data);
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) StreamManagerImpl(io.pravega.client.admin.impl.StreamManagerImpl) Cleanup(lombok.Cleanup) ByteStreamClientFactory(io.pravega.client.ByteStreamClientFactory) ByteStreamWriter(io.pravega.client.byteStream.ByteStreamWriter) StreamManager(io.pravega.client.admin.StreamManager) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ByteStreamReader(io.pravega.client.byteStream.ByteStreamReader) Test(org.junit.Test)

Aggregations

StreamManager (io.pravega.client.admin.StreamManager)59 Cleanup (lombok.Cleanup)54 Test (org.junit.Test)44 ClientConfig (io.pravega.client.ClientConfig)32 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)31 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)24 StreamManagerImpl (io.pravega.client.admin.impl.StreamManagerImpl)22 ConnectionPoolImpl (io.pravega.client.connection.impl.ConnectionPoolImpl)20 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)19 ConnectionPool (io.pravega.client.connection.impl.ConnectionPool)18 Stream (io.pravega.client.stream.Stream)17 EventStreamClientFactory (io.pravega.client.EventStreamClientFactory)15 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)15 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)12 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)12 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)12 URI (java.net.URI)12 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)11 Controller (io.pravega.client.control.impl.Controller)10 StreamImpl (io.pravega.client.stream.impl.StreamImpl)10