Search in sources :

Example 41 with StreamImpl

use of io.pravega.client.stream.impl.StreamImpl in project pravega by pravega.

the class StreamCutServiceTest method testRetentionService.

@Test(timeout = 10000)
public void testRetentionService() {
    List<StreamCutBucketService> bucketServices = Lists.newArrayList(service.getBuckets());
    assertNotNull(bucketServices);
    assertTrue(bucketServices.size() == 3);
    assertTrue(streamMetadataStore.takeBucketOwnership(0, hostId, executor).join());
    assertTrue(streamMetadataStore.takeBucketOwnership(1, hostId, executor).join());
    assertTrue(streamMetadataStore.takeBucketOwnership(2, hostId, executor).join());
    AssertExtensions.assertThrows("", () -> streamMetadataStore.takeBucketOwnership(3, hostId, executor).join(), e -> e instanceof IllegalArgumentException);
    service.notify(new BucketOwnershipListener.BucketNotification(0, BucketOwnershipListener.BucketNotification.NotificationType.BucketAvailable));
    String scope = "scope";
    String streamName = "stream";
    Stream stream = new StreamImpl(scope, streamName);
    AssertExtensions.assertThrows("Null retention policy check", () -> streamMetadataStore.addUpdateStreamForAutoStreamCut(scope, streamName, null, null, executor).join(), e -> e instanceof NullPointerException);
    streamMetadataStore.addUpdateStreamForAutoStreamCut(scope, streamName, RetentionPolicy.builder().build(), null, executor).join();
    // verify that at least one of the buckets got the notification
    int bucketId = stream.getScopedName().hashCode() % 3;
    StreamCutBucketService bucketService = bucketServices.stream().filter(x -> x.getBucketId() == bucketId).findAny().get();
    AtomicBoolean added = new AtomicBoolean(false);
    RetryHelper.loopWithDelay(() -> !added.get(), () -> CompletableFuture.completedFuture(null).thenAccept(x -> added.set(bucketService.getRetentionFutureMap().size() > 0)), Duration.ofSeconds(1).toMillis(), executor).join();
    assertTrue(bucketService.getRetentionFutureMap().containsKey(stream));
    streamMetadataStore.removeStreamFromAutoStreamCut(scope, streamName, null, executor).join();
    AtomicBoolean removed = new AtomicBoolean(false);
    RetryHelper.loopWithDelay(() -> !removed.get(), () -> CompletableFuture.completedFuture(null).thenAccept(x -> removed.set(bucketService.getRetentionFutureMap().size() == 0)), Duration.ofSeconds(1).toMillis(), executor).join();
    assertTrue(bucketService.getRetentionFutureMap().size() == 0);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamImpl(io.pravega.client.stream.impl.StreamImpl) Stream(io.pravega.client.stream.Stream) Test(org.junit.Test)

Example 42 with StreamImpl

use of io.pravega.client.stream.impl.StreamImpl in project pravega by pravega.

the class ZkStoreRetentionTest method testBucketOwnership.

@Test(timeout = 10000)
public void testBucketOwnership() throws Exception {
    // verify that ownership is not taken up by another host
    assertFalse(streamMetadataStore.takeBucketOwnership(0, "", executor).join());
    // Introduce connection failure error
    zkClient.getZookeeperClient().close();
    // restart
    CuratorFramework zkClient2 = CuratorFrameworkFactory.newClient(zkServer.getConnectString(), 10000, 1000, (r, e, s) -> false);
    zkClient2.start();
    StreamMetadataStore streamMetadataStore2 = StreamStoreFactory.createZKStore(zkClient2, executor);
    String scope = "scope1";
    String streamName = "stream1";
    streamMetadataStore2.addUpdateStreamForAutoStreamCut(scope, streamName, RetentionPolicy.builder().build(), null, executor).join();
    zkClient2.close();
    zkClient.getZookeeperClient().start();
    Stream stream = new StreamImpl(scope, streamName);
    // verify that at least one of the buckets got the notification
    List<StreamCutBucketService> bucketServices = Lists.newArrayList(service.getBuckets());
    int bucketId = stream.getScopedName().hashCode() % 3;
    StreamCutBucketService bucketService = bucketServices.stream().filter(x -> x.getBucketId() == bucketId).findAny().get();
    AtomicBoolean added = new AtomicBoolean(false);
    RetryHelper.loopWithDelay(() -> !added.get(), () -> CompletableFuture.completedFuture(null).thenAccept(x -> added.set(bucketService.getRetentionFutureMap().size() > 0)), Duration.ofSeconds(1).toMillis(), executor).join();
    assertTrue(bucketService.getRetentionFutureMap().containsKey(stream));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) StreamImpl(io.pravega.client.stream.impl.StreamImpl) Stream(io.pravega.client.stream.Stream) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) Test(org.junit.Test)

Example 43 with StreamImpl

use of io.pravega.client.stream.impl.StreamImpl in project pravega by pravega.

the class StreamCutBucketService method performRetention.

private CompletableFuture<Void> performRetention(StreamImpl stream) {
    log.debug("Periodic background processing for retention called for stream {}/{}", stream.getScope(), stream.getStreamName());
    OperationContext context = streamMetadataStore.createContext(stream.getScope(), stream.getStreamName());
    return RetryHelper.withRetriesAsync(() -> streamMetadataStore.getConfiguration(stream.getScope(), stream.getStreamName(), context, executor).thenCompose(config -> streamMetadataTasks.retention(stream.getScope(), stream.getStreamName(), config.getRetentionPolicy(), System.currentTimeMillis(), context, this.streamMetadataTasks.retrieveDelegationToken())).exceptionally(e -> {
        log.warn("Exception thrown while performing auto retention for stream {} ", stream, e);
        throw new CompletionException(e);
    }), RetryHelper.UNCONDITIONAL_PREDICATE, 5, executor).exceptionally(e -> {
        log.warn("Unable to perform retention for stream {}. Ignoring, retention will be attempted in next cycle.", stream, e);
        return null;
    });
}
Also used : OperationContext(io.pravega.controller.store.stream.OperationContext) OperationContext(io.pravega.controller.store.stream.OperationContext) StreamImpl(io.pravega.client.stream.impl.StreamImpl) Exceptions(io.pravega.common.Exceptions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) ConcurrentMap(java.util.concurrent.ConcurrentMap) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) Config(io.pravega.controller.util.Config) Stream(io.pravega.client.stream.Stream) Duration(java.time.Duration) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) AbstractService(com.google.common.util.concurrent.AbstractService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) VisibleForTesting(com.google.common.annotations.VisibleForTesting) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) RetryHelper(io.pravega.controller.util.RetryHelper) CompletionException(java.util.concurrent.CompletionException)

Example 44 with StreamImpl

use of io.pravega.client.stream.impl.StreamImpl in project pravega by pravega.

the class BatchClientImplTest method testSegmentIterator.

@Test(timeout = 5000)
public void testSegmentIterator() throws ConnectionFailedException {
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CreateSegment request = (CreateSegment) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new SegmentCreated(request.getRequestId(), request.getSegment()));
            return null;
        }
    }).when(connection).send(Mockito.any(CreateSegment.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            GetStreamSegmentInfo request = (GetStreamSegmentInfo) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
            return null;
        }
    }).when(connection).send(Mockito.any(GetStreamSegmentInfo.class));
    connectionFactory.provideConnection(location, connection);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory);
    BatchClientImpl client = new BatchClientImpl(mockController, connectionFactory);
    Stream stream = new StreamImpl("scope", "stream");
    mockController.createScope("scope");
    mockController.createStream(StreamConfiguration.builder().scope("scope").streamName("stream").scalingPolicy(ScalingPolicy.fixed(3)).build()).join();
    Iterator<SegmentRange> segments = client.getSegments(stream, null, null).getIterator();
    assertTrue(segments.hasNext());
    assertEquals(0, segments.next().asImpl().getSegment().getSegmentNumber());
    assertTrue(segments.hasNext());
    assertEquals(1, segments.next().asImpl().getSegment().getSegmentNumber());
    assertTrue(segments.hasNext());
    assertEquals(2, segments.next().asImpl().getSegment().getSegmentNumber());
    assertFalse(segments.hasNext());
}
Also used : SegmentRange(io.pravega.client.batch.SegmentRange) GetStreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.GetStreamSegmentInfo) StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) GetStreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.GetStreamSegmentInfo) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamImpl(io.pravega.client.stream.impl.StreamImpl) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) Stream(io.pravega.client.stream.Stream) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) Test(org.junit.Test)

Example 45 with StreamImpl

use of io.pravega.client.stream.impl.StreamImpl in project pravega by pravega.

the class MockController method createStream.

@Override
@Synchronized
public CompletableFuture<Boolean> createStream(StreamConfiguration streamConfig) {
    Stream stream = new StreamImpl(streamConfig.getScope(), streamConfig.getStreamName());
    if (createdStreams.get(stream) != null) {
        return CompletableFuture.completedFuture(false);
    }
    if (createdScopes.get(streamConfig.getScope()) == null) {
        return Futures.failedFuture(new IllegalArgumentException("Scope does not exit."));
    }
    createdStreams.put(stream, streamConfig);
    createdScopes.get(streamConfig.getScope()).add(stream);
    for (Segment segment : getSegmentsForStream(stream)) {
        createSegment(segment.getScopedName(), new PravegaNodeUri(endpoint, port));
    }
    return CompletableFuture.completedFuture(true);
}
Also used : PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) StreamImpl(io.pravega.client.stream.impl.StreamImpl) Stream(io.pravega.client.stream.Stream) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) Segment(io.pravega.client.segment.impl.Segment) DeleteSegment(io.pravega.shared.protocol.netty.WireCommands.DeleteSegment) Synchronized(lombok.Synchronized)

Aggregations

StreamImpl (io.pravega.client.stream.impl.StreamImpl)74 Test (org.junit.Test)50 Stream (io.pravega.client.stream.Stream)47 Cleanup (lombok.Cleanup)36 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)32 HashMap (java.util.HashMap)32 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)22 Map (java.util.Map)22 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)21 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)21 Controller (io.pravega.client.control.impl.Controller)21 ClientConfig (io.pravega.client.ClientConfig)20 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)18 Segment (io.pravega.client.segment.impl.Segment)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)16 Slf4j (lombok.extern.slf4j.Slf4j)14 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)13 CompletableFuture (java.util.concurrent.CompletableFuture)12 Before (org.junit.Before)12