use of io.pravega.client.state.SynchronizerConfig in project pravega by pravega.
the class RevisionedStreamClientTest method testSegmentTruncation.
@Test
public void testSegmentTruncation() {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
Revision r0 = client.fetchLatestRevision();
client.writeUnconditionally("a");
Revision ra = client.fetchLatestRevision();
client.writeUnconditionally("b");
Revision rb = client.fetchLatestRevision();
client.writeUnconditionally("c");
Revision rc = client.fetchLatestRevision();
assertEquals(r0, client.fetchOldestRevision());
client.truncateToRevision(r0);
assertEquals(r0, client.fetchOldestRevision());
client.truncateToRevision(ra);
assertEquals(ra, client.fetchOldestRevision());
client.truncateToRevision(r0);
assertEquals(ra, client.fetchOldestRevision());
AssertExtensions.assertThrows(TruncatedDataException.class, () -> client.readFrom(r0));
Iterator<Entry<Revision, String>> iterA = client.readFrom(ra);
assertTrue(iterA.hasNext());
Iterator<Entry<Revision, String>> iterB = client.readFrom(ra);
assertTrue(iterB.hasNext());
assertEquals("b", iterA.next().getValue());
assertEquals("b", iterB.next().getValue());
client.truncateToRevision(rb);
assertTrue(iterA.hasNext());
assertEquals("c", iterA.next().getValue());
client.truncateToRevision(rc);
assertFalse(iterA.hasNext());
assertTrue(iterB.hasNext());
AssertExtensions.assertThrows(TruncatedDataException.class, () -> iterB.next());
}
use of io.pravega.client.state.SynchronizerConfig in project pravega by pravega.
the class ReaderGroupStateManagerTest method testReleaseAndAcquireTimes.
@Test(timeout = 5000)
public void testReleaseAndAcquireTimes() throws ReinitializationRequiredException {
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);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup StateSynchronizer<ReaderGroupState> state = clientFactory.createStateSynchronizer(stream, new JavaSerializer<>(), new JavaSerializer<>(), config);
AtomicLong clock = new AtomicLong();
Map<Segment, Long> segments = new HashMap<>();
segments.put(new Segment(scope, stream, 0), 0L);
segments.put(new Segment(scope, stream, 1), 1L);
segments.put(new Segment(scope, stream, 2), 2L);
segments.put(new Segment(scope, stream, 3), 3L);
ReaderGroupStateManager.initializeReaderGroup(state, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
ReaderGroupStateManager reader1 = new ReaderGroupStateManager("reader1", state, controller, clock::get);
reader1.initializeReader(0);
ReaderGroupStateManager reader2 = new ReaderGroupStateManager("reader2", state, controller, clock::get);
reader2.initializeReader(0);
clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
Map<Segment, Long> newSegments = reader1.acquireNewSegmentsIfNeeded(123);
assertEquals(2, newSegments.size());
Duration r1aqt = ReaderGroupStateManager.calculateAcquireTime("reader1", state.getState());
Duration r2aqt = ReaderGroupStateManager.calculateAcquireTime("reader2", state.getState());
assertTrue(r1aqt.toMillis() > r2aqt.toMillis());
Duration r1rlt = ReaderGroupStateManager.calculateReleaseTime("reader1", state.getState());
Duration r2rlt = ReaderGroupStateManager.calculateReleaseTime("reader2", state.getState());
assertTrue(r1rlt.toMillis() < r2rlt.toMillis());
reader1.releaseSegment(newSegments.keySet().iterator().next(), 0, 123);
newSegments = reader2.acquireNewSegmentsIfNeeded(123);
assertEquals(2, newSegments.size());
r1aqt = ReaderGroupStateManager.calculateAcquireTime("reader1", state.getState());
r2aqt = ReaderGroupStateManager.calculateAcquireTime("reader2", state.getState());
assertTrue(r1aqt.toMillis() < r2aqt.toMillis());
r1rlt = ReaderGroupStateManager.calculateReleaseTime("reader1", state.getState());
r2rlt = ReaderGroupStateManager.calculateReleaseTime("reader2", state.getState());
assertTrue(r1rlt.toMillis() > r2rlt.toMillis());
}
use of io.pravega.client.state.SynchronizerConfig in project pravega by pravega.
the class ReaderGroupStateManagerTest method testCheckpointContainsAllShards.
@Test(timeout = 10000)
public void testCheckpointContainsAllShards() throws ReinitializationRequiredException {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
Segment segment0 = new Segment(scope, stream, 0);
Segment segment1 = new Segment(scope, stream, 1);
Segment segment2 = new Segment(scope, stream, 2);
MockController controller = new MockControllerWithSuccessors(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, new StreamSegmentsWithPredecessors(ImmutableMap.of(), ""));
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
Map<Segment, Long> segments = ImmutableMap.of(segment0, 0L, segment1, 1L, segment2, 2L);
ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
val readerState1 = new ReaderGroupStateManager("reader1", stateSynchronizer, controller, null);
readerState1.initializeReader(0);
val readerState2 = new ReaderGroupStateManager("reader2", stateSynchronizer, controller, null);
readerState2.initializeReader(0);
assertEquals(segments, stateSynchronizer.getState().getUnassignedSegments());
stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP1"));
stateSynchronizer.fetchUpdates();
assertEquals("CP1", readerState1.getCheckpoint());
assertEquals(Collections.emptyMap(), readerState1.acquireNewSegmentsIfNeeded(1));
assertEquals(Collections.emptyMap(), readerState2.acquireNewSegmentsIfNeeded(2));
assertEquals("CP1", readerState2.getCheckpoint());
readerState1.checkpoint("CP1", new PositionImpl(Collections.emptyMap()));
readerState2.checkpoint("CP1", new PositionImpl(Collections.emptyMap()));
assertTrue(stateSynchronizer.getState().isCheckpointComplete("CP1"));
assertEquals(segments, stateSynchronizer.getState().getPositionsForCompletedCheckpoint("CP1"));
}
use of io.pravega.client.state.SynchronizerConfig in project pravega by pravega.
the class ReaderGroupStateManagerTest method testAddReader.
@Test(timeout = 10000)
public void testAddReader() throws ReinitializationRequiredException {
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);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
Map<Segment, Long> segments = new HashMap<>();
segments.put(new Segment(scope, stream, 0), 1L);
ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
ReaderGroupStateManager readerState = new ReaderGroupStateManager("testReader", stateSynchronizer, controller, null);
readerState.initializeReader(0);
Segment toRelease = readerState.findSegmentToReleaseIfRequired();
assertNull(toRelease);
Map<Segment, Long> newSegments = readerState.acquireNewSegmentsIfNeeded(0);
assertFalse(newSegments.isEmpty());
assertEquals(1, newSegments.size());
assertTrue(newSegments.containsKey(new Segment(scope, stream, 0)));
assertEquals(1, newSegments.get(new Segment(scope, stream, 0)).longValue());
}
use of io.pravega.client.state.SynchronizerConfig in project pravega by pravega.
the class ReaderGroupStateManagerTest method testCheckpoint.
@Test(timeout = 10000)
public void testCheckpoint() throws ReinitializationRequiredException {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
Segment initialSegment = new Segment(scope, stream, 0);
Segment successorA = new Segment(scope, stream, 1);
Segment successorB = new Segment(scope, stream, 2);
MockController controller = new MockControllerWithSuccessors(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, new StreamSegmentsWithPredecessors(ImmutableMap.of(new SegmentWithRange(successorA, 0.0, 0.5), singletonList(0), new SegmentWithRange(successorB, 0.5, 1.0), singletonList(0)), ""));
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
Map<Segment, Long> segments = new HashMap<>();
segments.put(initialSegment, 1L);
ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
val readerState = new ReaderGroupStateManager("testReader", stateSynchronizer, controller, null);
readerState.initializeReader(0);
assertNull(readerState.getCheckpoint());
stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP1"));
stateSynchronizer.fetchUpdates();
assertEquals("CP1", readerState.getCheckpoint());
assertEquals("CP1", readerState.getCheckpoint());
readerState.checkpoint("CP1", new PositionImpl(Collections.emptyMap()));
assertNull(readerState.getCheckpoint());
stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP2"));
stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP3"));
stateSynchronizer.fetchUpdates();
assertEquals("CP2", readerState.getCheckpoint());
readerState.checkpoint("CP2", new PositionImpl(Collections.emptyMap()));
assertEquals("CP3", readerState.getCheckpoint());
readerState.checkpoint("CP3", new PositionImpl(Collections.emptyMap()));
assertNull(readerState.getCheckpoint());
}
Aggregations