Search in sources :

Example 1 with Position

use of io.pravega.client.stream.Position in project pravega by pravega.

the class CommitEventProcessor method process.

@Override
protected void process(CommitEvent event, Position position) {
    String scope = event.getScope();
    String stream = event.getStream();
    int epoch = event.getEpoch();
    UUID txnId = event.getTxid();
    OperationContext context = streamMetadataStore.createContext(scope, stream);
    log.debug("Committing transaction {} on stream {}/{}", event.getTxid(), event.getScope(), event.getStream());
    streamMetadataStore.getActiveEpoch(scope, stream, context, false, executor).thenComposeAsync(pair -> {
        // complete before transitioning the stream to new epoch.
        if (epoch < pair.getKey()) {
            return CompletableFuture.completedFuture(null);
        } else if (epoch == pair.getKey()) {
            // If the transaction's epoch is same as the stream's current epoch, commit it.
            return completeCommit(scope, stream, epoch, txnId, context, this.streamMetadataTasks.retrieveDelegationToken());
        } else {
            // Otherwise, postpone commit operation until the stream transitions to next epoch.
            return postponeCommitEvent(event);
        }
    }).whenCompleteAsync((result, error) -> {
        if (error != null) {
            log.error("Failed committing transaction {} on stream {}/{}", txnId, scope, stream);
        } else {
            log.debug("Successfully committed transaction {} on stream {}/{}", txnId, scope, stream);
            if (processedEvents != null) {
                processedEvents.offer(event);
            }
        }
    }, executor).join();
}
Also used : OperationContext(io.pravega.controller.store.stream.OperationContext) CommitEvent(io.pravega.shared.controller.event.CommitEvent) OperationContext(io.pravega.controller.store.stream.OperationContext) Retry(io.pravega.common.util.Retry) SegmentHelper(io.pravega.controller.server.SegmentHelper) EventProcessor(io.pravega.controller.eventProcessor.impl.EventProcessor) Exceptions(io.pravega.common.Exceptions) BlockingQueue(java.util.concurrent.BlockingQueue) CompletableFuture(java.util.concurrent.CompletableFuture) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) WriteFailedException(io.pravega.controller.task.Stream.WriteFailedException) Position(io.pravega.client.stream.Position) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) Futures(io.pravega.common.concurrent.Futures) UUID(java.util.UUID)

Example 2 with Position

use of io.pravega.client.stream.Position in project pravega by pravega.

the class AbstractControllerMetadataCommandsTest method testControllerMetadataViewReaderInfoCommand.

@Test
public void testControllerMetadataViewReaderInfoCommand() throws Exception {
    final String process = UUID.randomUUID().toString();
    final String readerGroup = UUID.randomUUID().toString();
    final String reader = UUID.randomUUID().toString();
    ZKHelper zkHelper = ZKHelper.create(SETUP_UTILS.getZkTestServer().getConnectString(), "pravega-cluster");
    CheckpointStore checkpointStore = zkHelper.getCheckPointStore();
    checkpointStore.addReaderGroup(process, readerGroup);
    checkpointStore.addReader(process, readerGroup, reader);
    Position position = new PositionImpl(ImmutableMap.of(new SegmentWithRange(Segment.fromScopedName("testScope/testStream/0"), 0, 0.5), 9999999L, new SegmentWithRange(Segment.fromScopedName("testScope/testStream/1"), 0.5, 1.0), -1L));
    checkpointStore.setPosition(process, readerGroup, reader, position);
    String commandResult = TestUtils.executeCommand("controller-metadata get-reader " + process + " " + readerGroup + " " + reader, STATE.get());
    Assert.assertTrue(commandResult.contains("testScope/testStream"));
}
Also used : ZKHelper(io.pravega.cli.admin.utils.ZKHelper) Position(io.pravega.client.stream.Position) PositionImpl(io.pravega.client.stream.impl.PositionImpl) SegmentWithRange(io.pravega.client.stream.impl.SegmentWithRange) CheckpointStore(io.pravega.controller.store.checkpoint.CheckpointStore) Test(org.junit.Test)

Example 3 with Position

use of io.pravega.client.stream.Position in project pravega by pravega.

the class ReaderGroupStateManagerTest method testReleaseCompletedSegment.

@Test(timeout = 10000)
public void testReleaseCompletedSegment() throws ReaderNotInReaderGroupException {
    String scope = "scope";
    String stream = "stream";
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    controller.createScope(scope);
    controller.createStream(scope, stream, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(2)).build());
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    @Cleanup SynchronizerClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory, streamFactory);
    SynchronizerConfig config = SynchronizerConfig.builder().build();
    @Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
    AtomicLong clock = new AtomicLong();
    Map<SegmentWithRange, Long> segments = new HashMap<>();
    segments.put(new SegmentWithRange(new Segment(scope, stream, 0), 0.0, 0.5), 123L);
    segments.put(new SegmentWithRange(new Segment(scope, stream, 1), 0.5, 1.0), 456L);
    stateSynchronizer.initialize(new ReaderGroupState.ReaderGroupStateInit(ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments, Collections.emptyMap(), false));
    ReaderGroupStateManager readerState1 = new ReaderGroupStateManager(scope, stream, "testReader", stateSynchronizer, controller, clock::get);
    readerState1.initializeReader(0);
    Segment toRelease = readerState1.findSegmentToReleaseIfRequired();
    assertNull(toRelease);
    Map<SegmentWithRange, Long> newSegments = readerState1.acquireNewSegmentsIfNeeded(0, new PositionImpl(Collections.emptyMap()));
    assertFalse(newSegments.isEmpty());
    assertEquals(2, newSegments.size());
    ReaderGroupStateManager readerState2 = new ReaderGroupStateManager(scope, stream, "testReader2", stateSynchronizer, controller, clock::get);
    readerState2.initializeReader(0);
    clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
    Position pos = new PositionImpl(ImmutableMap.of(new SegmentWithRange(new Segment(scope, stream, 0), 0.0, 0.5), -1L, new SegmentWithRange(new Segment(scope, stream, 1), 0.5, 1.0), 789L));
    ReaderGroupStateManager.readerShutdown("testReader", pos, stateSynchronizer);
    newSegments = readerState2.acquireNewSegmentsIfNeeded(0, new PositionImpl(Collections.emptyMap()));
    assertEquals(2, newSegments.size());
    assertEquals(Long.valueOf(789L), newSegments.get(new SegmentWithRange(new Segment(scope, stream, 1), 0.5, 1.0)));
    assertEquals(0, stateSynchronizer.getState().getNumberOfUnassignedSegments());
    AssertExtensions.assertThrows(ReaderNotInReaderGroupException.class, () -> readerState1.acquireNewSegmentsIfNeeded(0L, new PositionImpl(Collections.emptyMap())));
}
Also used : HashMap(java.util.HashMap) Position(io.pravega.client.stream.Position) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockController(io.pravega.client.stream.mock.MockController) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Test(org.junit.Test)

Example 4 with Position

use of io.pravega.client.stream.Position in project pravega by pravega.

the class ZKCheckpointStore method getPositions.

@Override
public Map<String, Position> getPositions(String process, String readerGroup) throws CheckpointStoreException {
    Map<String, Position> map = new HashMap<>();
    String path = getReaderGroupPath(process, readerGroup);
    ReaderGroupData rgData = groupDataSerializer.deserialize(ByteBuffer.wrap(getData(path)));
    rgData.getReaderIds().forEach(x -> map.put(x, null));
    for (String child : getChildren(path)) {
        Position position = null;
        byte[] data = getData(path + "/" + child);
        if (data != null && data.length > 0) {
            position = positionSerializer.deserialize(ByteBuffer.wrap(data));
        }
        map.put(child, position);
    }
    return map;
}
Also used : Position(io.pravega.client.stream.Position) HashMap(java.util.HashMap)

Example 5 with Position

use of io.pravega.client.stream.Position in project pravega by pravega.

the class ZkCheckpointStoreConnectivityTest method connectivityFailureTests.

@Test
public void connectivityFailureTests() throws IOException {
    final String process1 = UUID.randomUUID().toString();
    final String readerGroup1 = UUID.randomUUID().toString();
    final String reader1 = UUID.randomUUID().toString();
    Predicate<Throwable> predicate = e -> e instanceof CheckpointStoreException && ((CheckpointStoreException) e).getType().equals(CheckpointStoreException.Type.Connectivity);
    AssertExtensions.assertThrows("failed getProcesses", () -> checkpointStore.getProcesses(), predicate);
    AssertExtensions.assertThrows("failed addReaderGroup", () -> checkpointStore.addReaderGroup(process1, readerGroup1), predicate);
    AssertExtensions.assertThrows("failed addReader", () -> checkpointStore.addReader(process1, readerGroup1, reader1), predicate);
    AssertExtensions.assertThrows("failed sealReaderGroup", () -> checkpointStore.sealReaderGroup(process1, readerGroup1), predicate);
    AssertExtensions.assertThrows("failed removeReader", () -> checkpointStore.removeReader(process1, readerGroup1, reader1), predicate);
    AssertExtensions.assertThrows("failed getPositions", () -> checkpointStore.getPositions(process1, readerGroup1), predicate);
    Position position = new PositionImpl(Collections.emptyMap());
    AssertExtensions.assertThrows("failed setPosition", () -> checkpointStore.setPosition(process1, readerGroup1, reader1, position), predicate);
    AssertExtensions.assertThrows("failed removeReader", () -> checkpointStore.removeReader(process1, readerGroup1, reader1), predicate);
    AssertExtensions.assertThrows("failed removeReaderGroup", () -> checkpointStore.removeReaderGroup(process1, readerGroup1), predicate);
}
Also used : CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) AssertExtensions(io.pravega.test.common.AssertExtensions) Predicate(java.util.function.Predicate) CheckpointStoreFactory(io.pravega.controller.store.checkpoint.CheckpointStoreFactory) Test(org.junit.Test) IOException(java.io.IOException) UUID(java.util.UUID) CheckpointStore(io.pravega.controller.store.checkpoint.CheckpointStore) CheckpointStoreException(io.pravega.controller.store.checkpoint.CheckpointStoreException) TimeUnit(java.util.concurrent.TimeUnit) CuratorFramework(org.apache.curator.framework.CuratorFramework) Rule(org.junit.Rule) PositionImpl(io.pravega.client.stream.impl.PositionImpl) Position(io.pravega.client.stream.Position) After(org.junit.After) Timeout(org.junit.rules.Timeout) Collections(java.util.Collections) Before(org.junit.Before) Position(io.pravega.client.stream.Position) PositionImpl(io.pravega.client.stream.impl.PositionImpl) CheckpointStoreException(io.pravega.controller.store.checkpoint.CheckpointStoreException) Test(org.junit.Test)

Aggregations

Position (io.pravega.client.stream.Position)15 Test (org.junit.Test)11 PositionImpl (io.pravega.client.stream.impl.PositionImpl)4 CheckpointStoreException (io.pravega.controller.store.checkpoint.CheckpointStoreException)3 UUID (java.util.UUID)3 Cleanup (lombok.Cleanup)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 SynchronizerClientFactory (io.pravega.client.SynchronizerClientFactory)2 Segment (io.pravega.client.segment.impl.Segment)2 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)2 ReaderGroup (io.pravega.client.stream.ReaderGroup)2 Exceptions (io.pravega.common.Exceptions)2 CheckpointStore (io.pravega.controller.store.checkpoint.CheckpointStore)2 CheckpointStoreFactory (io.pravega.controller.store.checkpoint.CheckpointStoreFactory)2 AssertExtensions (io.pravega.test.common.AssertExtensions)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Predicate (java.util.function.Predicate)2