Search in sources :

Example 16 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ReaderGroupImpl method resetReaderGroup.

@Override
public void resetReaderGroup(ReaderGroupConfig config) {
    @Cleanup StateSynchronizer<ReaderGroupState> synchronizer = createSynchronizer();
    Map<Segment, Long> segments = getSegmentsForStreams(config);
    synchronizer.updateStateUnconditionally(new ReaderGroupStateInit(config, segments));
}
Also used : ReaderGroupStateInit(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateInit) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment)

Example 17 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ReaderGroupStateManager method acquireSegment.

private Map<Segment, Long> acquireSegment(long timeLag) throws ReinitializationRequiredException {
    AtomicReference<Map<Segment, Long>> result = new AtomicReference<>();
    AtomicBoolean reinitRequired = new AtomicBoolean(false);
    sync.updateState(state -> {
        result.set(Collections.emptyMap());
        if (!state.isReaderOnline(readerId)) {
            reinitRequired.set(true);
            return null;
        }
        if (state.getCheckpointForReader(readerId) != null) {
            return null;
        }
        int toAcquire = calculateNumSegmentsToAcquire(state);
        if (toAcquire == 0) {
            return null;
        }
        Map<Segment, Long> unassignedSegments = state.getUnassignedSegments();
        Map<Segment, Long> acquired = new HashMap<>(toAcquire);
        List<ReaderGroupStateUpdate> updates = new ArrayList<>(toAcquire);
        Iterator<Entry<Segment, Long>> iter = unassignedSegments.entrySet().iterator();
        for (int i = 0; i < toAcquire; i++) {
            assert iter.hasNext();
            Entry<Segment, Long> segment = iter.next();
            acquired.put(segment.getKey(), segment.getValue());
            updates.add(new AcquireSegment(readerId, segment.getKey()));
        }
        updates.add(new UpdateDistanceToTail(readerId, timeLag));
        result.set(acquired);
        return updates;
    });
    if (reinitRequired.get()) {
        throw new ReinitializationRequiredException();
    }
    releaseTimer.reset(calculateReleaseTime(readerId, sync.getState()));
    acquireTimer.reset(calculateAcquireTime(readerId, sync.getState()));
    return result.get();
}
Also used : HashMap(java.util.HashMap) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CreateCheckpoint(io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint) ReleaseSegment(io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment) Segment(io.pravega.client.segment.impl.Segment) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment) ReaderGroupStateUpdate(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Entry(java.util.Map.Entry) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) UpdateDistanceToTail(io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ReaderGroupStateManager method releaseSegment.

/**
 * Releases a segment to another reader. This reader should no longer read from the segment.
 *
 * @param segment The segment to be released
 * @param lastOffset The offset from which the new owner should start reading from.
 * @param timeLag How far the reader is from the tail of the stream in time.
 * @return a boolean indicating if the segment was successfully released.
 * @throws ReinitializationRequiredException If the reader has been declared offline.
 */
boolean releaseSegment(Segment segment, long lastOffset, long timeLag) throws ReinitializationRequiredException {
    sync.updateState(state -> {
        Set<Segment> segments = state.getSegments(readerId);
        if (segments == null || !segments.contains(segment) || state.getCheckpointForReader(readerId) != null || !doesReaderOwnTooManySegments(state)) {
            return null;
        }
        List<ReaderGroupStateUpdate> result = new ArrayList<>(2);
        result.add(new ReleaseSegment(readerId, segment, lastOffset));
        result.add(new UpdateDistanceToTail(readerId, timeLag));
        return result;
    });
    ReaderGroupState state = sync.getState();
    releaseTimer.reset(calculateReleaseTime(readerId, state));
    acquireTimer.reset(calculateAcquireTime(readerId, state));
    if (!state.isReaderOnline(readerId)) {
        throw new ReinitializationRequiredException();
    }
    return !state.getSegments(readerId).contains(segment);
}
Also used : ReaderGroupStateUpdate(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate) ReleaseSegment(io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) UpdateDistanceToTail(io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail) ArrayList(java.util.ArrayList) ReleaseSegment(io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment) Segment(io.pravega.client.segment.impl.Segment) AcquireSegment(io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment)

Example 19 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class SegmentRangeImplTest method testValid.

@Test
public void testValid() {
    SegmentRangeImpl segmentRange = SegmentRangeImpl.builder().endOffset(20L).segment(new Segment("scope", "stream", 0)).build();
    assertEquals(0L, segmentRange.getStartOffset());
    assertEquals(20L, segmentRange.getEndOffset());
    assertEquals(new Segment("scope", "stream", 0), segmentRange.getSegment());
}
Also used : Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 20 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class StreamSegmentsInfoTest method getStreamCut.

private StreamCut getStreamCut(long offset, int... segmentNumbers) {
    ImmutableMap.Builder<Segment, Long> segmentMap = ImmutableMap.builder();
    Arrays.stream(segmentNumbers).forEach(seg -> segmentMap.put(new Segment(SCOPE, STREAM_NAME, seg), offset));
    return new StreamCutImpl(new StreamImpl(SCOPE, STREAM_NAME), segmentMap.build());
}
Also used : StreamCutImpl(io.pravega.client.stream.impl.StreamCutImpl) StreamImpl(io.pravega.client.stream.impl.StreamImpl) ImmutableMap(com.google.common.collect.ImmutableMap) Segment(io.pravega.client.segment.impl.Segment)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)97 Test (org.junit.Test)63 Cleanup (lombok.Cleanup)35 HashMap (java.util.HashMap)27 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)22 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)19 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)17 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)16 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)16 MockController (io.pravega.client.stream.mock.MockController)15 ClientFactory (io.pravega.client.ClientFactory)13 Stream (io.pravega.client.stream.Stream)13 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)13 SynchronizerConfig (io.pravega.client.state.SynchronizerConfig)12 InlineExecutor (io.pravega.test.common.InlineExecutor)12 TreeMap (java.util.TreeMap)10 lombok.val (lombok.val)10 StreamCut (io.pravega.client.stream.StreamCut)8 ByteBuffer (java.nio.ByteBuffer)8