Search in sources :

Example 1 with ReinitializationRequiredException

use of io.pravega.client.stream.ReinitializationRequiredException 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 2 with ReinitializationRequiredException

use of io.pravega.client.stream.ReinitializationRequiredException 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 3 with ReinitializationRequiredException

use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.

the class ReadWriteTest method startNewReader.

private CompletableFuture<Void> startNewReader(final String id, final ClientFactory clientFactory, final String readerGroupName, final ConcurrentLinkedQueue<Long> readResult, final AtomicLong writeCount, final AtomicLong readCount, final AtomicBoolean exitFlag) {
    return CompletableFuture.runAsync(() -> {
        final EventStreamReader<Long> reader = clientFactory.createReader(id, readerGroupName, new JavaSerializer<Long>(), ReaderConfig.builder().build());
        while (!(exitFlag.get() && readCount.get() == writeCount.get())) {
            try {
                final Long longEvent = reader.readNextEvent(SECONDS.toMillis(2)).getEvent();
                log.info("Reading event {}", longEvent);
                if (longEvent != null) {
                    // update if event read is not null.
                    readResult.add(longEvent);
                    readCount.incrementAndGet();
                }
            } catch (ReinitializationRequiredException e) {
                log.warn("Test Exception while reading from the stream", e);
                break;
            }
        }
        log.info("Closing reader {}", reader);
        reader.close();
    });
}
Also used : ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 4 with ReinitializationRequiredException

use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.

the class MultiReadersEndToEndTest method readAllEvents.

private Collection<Integer> readAllEvents(final int numParallelReaders, ClientFactory clientFactory, final String readerGroupName, final int numSegments) {
    ConcurrentLinkedQueue<Integer> read = new ConcurrentLinkedQueue<>();
    final ExecutorService executorService = Executors.newFixedThreadPool(numParallelReaders, new ThreadFactoryBuilder().setNameFormat("testreader-pool-%d").build());
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < numParallelReaders; i++) {
        futures.add(executorService.submit(() -> {
            final String readerId = UUID.randomUUID().toString();
            @Cleanup final EventStreamReader<Integer> reader = clientFactory.createReader(readerId, readerGroupName, new IntegerSerializer(), ReaderConfig.builder().build());
            int emptyCount = 0;
            while (emptyCount <= numSegments) {
                try {
                    final Integer integerEventRead = reader.readNextEvent(100).getEvent();
                    if (integerEventRead != null) {
                        read.add(integerEventRead);
                        emptyCount = 0;
                    } else {
                        emptyCount++;
                    }
                } catch (ReinitializationRequiredException e) {
                    throw new RuntimeException(e);
                }
            }
        }));
    }
    // Wait until all readers are done.
    futures.forEach(f -> Futures.getAndHandleExceptions(f, RuntimeException::new));
    executorService.shutdownNow();
    return read;
}
Also used : ArrayList(java.util.ArrayList) IntegerSerializer(io.pravega.test.integration.utils.IntegerSerializer) EventStreamReader(io.pravega.client.stream.EventStreamReader) ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 5 with ReinitializationRequiredException

use of io.pravega.client.stream.ReinitializationRequiredException in project pravega by pravega.

the class ReadWithAutoScaleTest method startReader.

private CompletableFuture<Void> startReader(final String id, final ClientFactory clientFactory, final String readerGroupName, final ConcurrentLinkedQueue<Long> readResult, final AtomicLong writeCount, final AtomicLong readCount, final AtomicBoolean exitFlag) {
    return CompletableFuture.runAsync(() -> {
        @Cleanup final EventStreamReader<Long> reader = clientFactory.createReader(id, readerGroupName, new JavaSerializer<Long>(), ReaderConfig.builder().build());
        long count;
        while (!(exitFlag.get() && readCount.get() == writeCount.get())) {
            // exit only if exitFlag is true  and read Count equals write count.
            try {
                final Long longEvent = reader.readNextEvent(SECONDS.toMillis(60)).getEvent();
                if (longEvent != null) {
                    // update if event read is not null.
                    readResult.add(longEvent);
                    count = readCount.incrementAndGet();
                    log.debug("Reader {}, read count {}", id, count);
                } else {
                    log.debug("Null event, reader {}, read count {}", id, readCount.get());
                }
            } catch (ReinitializationRequiredException e) {
                log.warn("Test Exception while reading from the stream", e);
                break;
            }
        }
    });
}
Also used : ReinitializationRequiredException(io.pravega.client.stream.ReinitializationRequiredException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Cleanup(lombok.Cleanup)

Aggregations

ReinitializationRequiredException (io.pravega.client.stream.ReinitializationRequiredException)15 ArrayList (java.util.ArrayList)6 Cleanup (lombok.Cleanup)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 ClientConfig (io.pravega.client.ClientConfig)3 EventStreamClientFactory (io.pravega.client.EventStreamClientFactory)3 Segment (io.pravega.client.segment.impl.Segment)3 EventStreamReader (io.pravega.client.stream.EventStreamReader)3 Map (java.util.Map)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)2 ReaderGroup (io.pravega.client.stream.ReaderGroup)2 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)2 AcquireSegment (io.pravega.client.stream.impl.ReaderGroupState.AcquireSegment)2 ReaderGroupStateUpdate (io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateUpdate)2 ReleaseSegment (io.pravega.client.stream.impl.ReaderGroupState.ReleaseSegment)2 UpdateDistanceToTail (io.pravega.client.stream.impl.ReaderGroupState.UpdateDistanceToTail)2 IntegerSerializer (io.pravega.test.integration.utils.IntegerSerializer)2 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2