use of io.pravega.client.stream.impl.ReaderGroupState.AddReader in project pravega by pravega.
the class ReaderGroupStateTest method getRanking.
@Test
public void getRanking() throws Exception {
assertTrue(readerState.getOnlineReaders().isEmpty());
AddReader addR1 = new ReaderGroupState.AddReader("r1");
addR1.applyTo(readerState, revision);
assertEquals(1, readerState.getOnlineReaders().size());
AddReader addR2 = new ReaderGroupState.AddReader("r2");
addR2.applyTo(readerState, revision);
assertEquals(2, readerState.getOnlineReaders().size());
new ReaderGroupState.AcquireSegment("r1", getSegment("S1")).applyTo(readerState, revision);
SegmentWithRange s1r = new SegmentWithRange(getSegment("S1"), 0, 1);
ImmutableMap<SegmentWithRange, Long> positions = ImmutableMap.of(s1r, 123L);
new ReaderGroupState.UpdateDistanceToTail("r1", 1, positions).applyTo(readerState, revision);
assertEquals(Collections.singleton(getSegment("S1")), readerState.getSegments("r1"));
assertEquals(0, readerState.getRanking("r1"));
assertEquals(1, readerState.getRanking("r2"));
assertEquals(123L, readerState.getLastReadPositions(getStream("S1")).get(s1r).longValue());
new ReaderGroupState.AcquireSegment("r1", getSegment("S2")).applyTo(readerState, revision);
assertEquals(2, readerState.getSegments("r1").size());
assertEquals(0, readerState.getRanking("r1"));
assertEquals(1, readerState.getRanking("r2"));
new ReaderGroupState.ReleaseSegment("r1", getSegment("S1"), 1).applyTo(readerState, revision);
new ReaderGroupState.ReleaseSegment("r1", getSegment("S2"), 1).applyTo(readerState, revision);
new ReaderGroupState.AcquireSegment("r2", getSegment("S1")).applyTo(readerState, revision);
new ReaderGroupState.AcquireSegment("r2", getSegment("S2")).applyTo(readerState, revision);
SegmentWithRange s2r = new SegmentWithRange(getSegment("S2"), 0, 1);
positions = ImmutableMap.of(s2r, 123L);
new ReaderGroupState.UpdateDistanceToTail("r2", 1, positions).applyTo(readerState, revision);
assertEquals(0, readerState.getSegments("r1").size());
assertEquals(1, readerState.getRanking("r1"));
assertEquals(0, readerState.getRanking("r2"));
assertEquals(1L, readerState.getLastReadPositions(getStream("S1")).get(s1r).longValue());
assertEquals(123L, readerState.getLastReadPositions(getStream("S2")).get(s2r).longValue());
}
use of io.pravega.client.stream.impl.ReaderGroupState.AddReader in project pravega by pravega.
the class ReaderGroupStateTest method getStreamNames.
@Test
public void getStreamNames() {
// configured Streams.
Set<String> configuredStreams = ImmutableSet.of(getStream("S1").getScopedName(), getStream("S2").getScopedName());
// validate stream names
assertEquals(configuredStreams, readerState.getStreamNames());
// Simulate addition of a reader and assigning of segments to the reader.
new AddReader("reader1").applyTo(readerState, revision);
new ReaderGroupState.AcquireSegment("reader1", new Segment(SCOPE, "S1", 0)).applyTo(readerState, revision);
// validate stream names
assertEquals(configuredStreams, readerState.getStreamNames());
}
use of io.pravega.client.stream.impl.ReaderGroupState.AddReader in project pravega by pravega.
the class SerializationTest method testReaderGroupUpdates.
@Test
public void testReaderGroupUpdates() throws Exception {
ReaderGroupUpdateSerializer serializer = new ReaderGroupUpdateSerializer();
verify(serializer, new AddReader(createString()));
verify(serializer, new RemoveReader(createString(), createSegmentToLongMap()));
verify(serializer, new ReleaseSegment(createString(), createSegment(), r.nextLong()));
verify(serializer, new AcquireSegment(createString(), createSegment()));
verify(serializer, new UpdateDistanceToTail(createString(), r.nextLong(), createSegmentRangeMap()));
verify(serializer, new SegmentCompleted(createString(), createSegmentWithRange(), createMap(this::createSegmentWithRange, this::createLongList)));
verify(serializer, new CheckpointReader(createString(), createString(), createSegmentToLongMap()));
verify(serializer, new CreateCheckpoint(createString()));
verify(serializer, new ClearCheckpointsBefore(createString()));
verify(serializer, new UpdatingConfig(r.nextBoolean()));
}
use of io.pravega.client.stream.impl.ReaderGroupState.AddReader in project pravega by pravega.
the class ReaderGroupStateTest method testLastReadPositions.
@Test
public void testLastReadPositions() {
Map<SegmentWithRange, Long> p1 = readerState.getLastReadPositions(Stream.of(SCOPE, "S1"));
assertEquals(1, p1.size());
Segment s1 = new Segment(SCOPE, "S1", 0);
SegmentWithRange sr1 = new SegmentWithRange(s1, 0, 1);
assertEquals(Long.valueOf(1), p1.get(sr1));
Map<SegmentWithRange, Long> p2 = readerState.getLastReadPositions(Stream.of(SCOPE, "S2"));
assertEquals(1, p2.size());
Segment s2 = new Segment(SCOPE, "S2", 0);
SegmentWithRange sr2 = new SegmentWithRange(s2, 0, 1);
assertEquals(Long.valueOf(1), p2.get(sr2));
AddReader addR1 = new ReaderGroupState.AddReader("r1");
addR1.applyTo(readerState, revision);
AcquireSegment aquire = new ReaderGroupState.AcquireSegment("r1", getSegment("S1"));
aquire.applyTo(readerState, revision);
SegmentWithRange sr3 = new SegmentWithRange(new Segment(SCOPE, "S1", 1), 0, 1);
ImmutableMap<SegmentWithRange, List<Long>> successors = ImmutableMap.of(sr3, ImmutableList.of(0L));
SegmentCompleted completed = new ReaderGroupState.SegmentCompleted("r1", sr1, successors);
completed.applyTo(readerState, revision);
p1 = readerState.getLastReadPositions(Stream.of(SCOPE, "S1"));
assertEquals(1, p1.size());
assertEquals(Long.valueOf(0), p1.get(sr3));
}
use of io.pravega.client.stream.impl.ReaderGroupState.AddReader in project pravega by pravega.
the class ReaderGroupStateManager method initializeReader.
/**
* Add this reader to the reader group so that it is able to acquire segments
*/
void initializeReader(long initialAllocationDelay) {
boolean alreadyAdded = sync.updateState((state, updates) -> {
if (state.getSegments(readerId) == null) {
log.debug("Adding reader {} to reader group. CurrentState is: {}", readerId, state);
updates.add(new AddReader(readerId));
return false;
} else {
return true;
}
});
if (alreadyAdded) {
throw new IllegalStateException("The requested reader: " + readerId + " cannot be added to the group because it is already in the group. Perhaps close() was not called?");
}
long randomDelay = (long) (Math.random() * Math.min(initialAllocationDelay, sync.getState().getConfig().getGroupRefreshTimeMillis()));
acquireTimer.reset(Duration.ofMillis(initialAllocationDelay + randomDelay));
}
Aggregations