Search in sources :

Example 11 with Segment

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

the class EventStreamWriterImpl method beginTxn.

@Override
public Transaction<Type> beginTxn() {
    TxnSegments txnSegments = getAndHandleExceptions(controller.createTransaction(stream, config.getTransactionTimeoutTime(), config.getTransactionTimeoutScaleGracePeriod()), RuntimeException::new);
    UUID txnId = txnSegments.getTxnId();
    Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>();
    for (Segment s : txnSegments.getSteamSegments().getSegments()) {
        SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txnId, segmentSealedCallBack, config, txnSegments.getSteamSegments().getDelegationToken());
        SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txnId, out, serializer);
        transactions.put(s, impl);
    }
    pinger.startPing(txnId);
    return new TransactionImpl<Type>(txnId, transactions, txnSegments.getSteamSegments(), controller, stream, pinger);
}
Also used : HashMap(java.util.HashMap) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) UUID(java.util.UUID) Segment(io.pravega.client.segment.impl.Segment)

Example 12 with Segment

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

the class EventStreamWriterImpl method getTxn.

@Override
public Transaction<Type> getTxn(UUID txId) {
    StreamSegments segments = getAndHandleExceptions(controller.getCurrentSegments(stream.getScope(), stream.getStreamName()), RuntimeException::new);
    Status status = getAndHandleExceptions(controller.checkTransactionStatus(stream, txId), RuntimeException::new);
    if (status != Status.OPEN) {
        return new TransactionImpl<>(txId, controller, stream);
    }
    Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>();
    for (Segment s : segments.getSegments()) {
        SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txId, segmentSealedCallBack, config, segments.getDelegationToken());
        SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txId, out, serializer);
        transactions.put(s, impl);
    }
    return new TransactionImpl<Type>(txId, transactions, segments, controller, stream, pinger);
}
Also used : Status(io.pravega.client.stream.Transaction.Status) HashMap(java.util.HashMap) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment)

Example 13 with Segment

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

the class ReaderGroupImpl method getRemainingBytes.

private long getRemainingBytes(SegmentMetadataClientFactory metaFactory, StreamCut position) {
    long totalLength = 0;
    CompletableFuture<StreamSegmentSuccessors> unread = controller.getSuccessors(position);
    StreamSegmentSuccessors unreadVal = Futures.getAndHandleExceptions(unread, RuntimeException::new);
    for (Segment s : unreadVal.getSegments()) {
        @Cleanup SegmentMetadataClient metadataClient = metaFactory.createSegmentMetadataClient(s, unreadVal.getDelegationToken());
        totalLength += metadataClient.fetchCurrentSegmentLength();
    }
    for (long bytesRead : position.asImpl().getPositions().values()) {
        totalLength -= bytesRead;
    }
    log.debug("Remaining bytes after position: {} is {}", position, totalLength);
    return totalLength;
}
Also used : Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient)

Example 14 with Segment

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

the class ReaderGroupImpl method unreadBytes.

@Override
public long unreadBytes() {
    @Cleanup StateSynchronizer<ReaderGroupState> synchronizer = createSynchronizer();
    synchronizer.fetchUpdates();
    Optional<Map<Stream, Map<Segment, Long>>> checkPointedPositions = synchronizer.getState().getPositionsForLastCompletedCheckpoint();
    SegmentMetadataClientFactory metaFactory = new SegmentMetadataClientFactoryImpl(controller, connectionFactory);
    if (checkPointedPositions.isPresent()) {
        log.debug("Computing unread bytes based on the last checkPoint position");
        return getUnreadBytes(checkPointedPositions.get(), metaFactory);
    } else {
        log.info("No checkpoints found, using the last known offset to compute unread bytes");
        return getUnreadBytes(synchronizer.getState().getPositions(), metaFactory);
    }
}
Also used : SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) Cleanup(lombok.Cleanup) Map(java.util.Map) HashMap(java.util.HashMap) Segment(io.pravega.client.segment.impl.Segment)

Example 15 with Segment

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

the class ReaderGroupImpl method completeCheckpoint.

@SneakyThrows(CheckpointFailedException.class)
private Checkpoint completeCheckpoint(String checkpointName, StateSynchronizer<ReaderGroupState> synchronizer) {
    ReaderGroupState state = synchronizer.getState();
    Map<Segment, Long> map = state.getPositionsForCompletedCheckpoint(checkpointName);
    synchronizer.updateStateUnconditionally(new ClearCheckpoints(checkpointName));
    if (map == null) {
        throw new CheckpointFailedException("Checkpoint was cleared before results could be read.");
    }
    return new CheckpointImpl(checkpointName, map);
}
Also used : ClearCheckpoints(io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpoints) Segment(io.pravega.client.segment.impl.Segment) SneakyThrows(lombok.SneakyThrows)

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