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();
}
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"));
}
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())));
}
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;
}
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);
}
Aggregations