Search in sources :

Example 1 with AddReader

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());
}
Also used : AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) AddReader(io.pravega.client.stream.impl.ReaderGroupState.AddReader) Test(org.junit.Test)

Example 2 with AddReader

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());
}
Also used : AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) AddReader(io.pravega.client.stream.impl.ReaderGroupState.AddReader) Segment(io.pravega.client.segment.impl.Segment) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) Test(org.junit.Test)

Example 3 with AddReader

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()));
}
Also used : ReleaseSegment(io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment) ReaderGroupUpdateSerializer(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupUpdateSerializer) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) UpdateDistanceToTail(io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail) CreateCheckpoint(io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint) CheckpointReader(io.pravega.client.stream.impl.ReaderGroupState.CheckpointReader) ClearCheckpointsBefore(io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpointsBefore) RemoveReader(io.pravega.client.stream.impl.ReaderGroupState.RemoveReader) AddReader(io.pravega.client.stream.impl.ReaderGroupState.AddReader) UpdatingConfig(io.pravega.client.stream.impl.ReaderGroupState.UpdatingConfig) SegmentCompleted(io.pravega.client.stream.impl.ReaderGroupState.SegmentCompleted) Test(org.junit.Test)

Example 4 with AddReader

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));
}
Also used : AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) Collections.singletonList(java.util.Collections.singletonList) ImmutableList(com.google.common.collect.ImmutableList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) AddReader(io.pravega.client.stream.impl.ReaderGroupState.AddReader) Segment(io.pravega.client.segment.impl.Segment) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) SegmentCompleted(io.pravega.client.stream.impl.ReaderGroupState.SegmentCompleted) Test(org.junit.Test)

Example 5 with AddReader

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));
}
Also used : AddReader(io.pravega.client.stream.impl.ReaderGroupState.AddReader)

Aggregations

AddReader (io.pravega.client.stream.impl.ReaderGroupState.AddReader)5 AcquireSegment (io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment)4 Test (org.junit.Test)4 Segment (io.pravega.client.segment.impl.Segment)2 SegmentCompleted (io.pravega.client.stream.impl.ReaderGroupState.SegmentCompleted)2 ImmutableList (com.google.common.collect.ImmutableList)1 CheckpointReader (io.pravega.client.stream.impl.ReaderGroupState.CheckpointReader)1 ClearCheckpointsBefore (io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpointsBefore)1 CreateCheckpoint (io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint)1 ReaderGroupUpdateSerializer (io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupUpdateSerializer)1 ReleaseSegment (io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment)1 RemoveReader (io.pravega.client.stream.impl.ReaderGroupState.RemoveReader)1 UpdateDistanceToTail (io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail)1 UpdatingConfig (io.pravega.client.stream.impl.ReaderGroupState.UpdatingConfig)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1