Search in sources :

Example 31 with SegmentOutputStream

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

the class SegmentSelector method createMissingWriters.

private void createMissingWriters(Consumer<Segment> segmentSealedCallBack, String delegationToken) {
    for (Segment segment : currentSegments.getSegments()) {
        if (!writers.containsKey(segment)) {
            SegmentOutputStream out = outputStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config, delegationToken);
            writers.put(segment, out);
        }
    }
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment)

Example 32 with SegmentOutputStream

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

the class TransactionalEventStreamWriterImpl method getTxn.

@Override
public Transaction<Type> getTxn(UUID txId) {
    // check if the transaction is open.
    Status status = Futures.getThrowingException(controller.checkTransactionStatus(stream, txId));
    if (status != Status.OPEN) {
        return new TransactionImpl<>(writerId, txId, controller, stream);
    }
    // get the segments corresponding to the transaction.
    StreamSegments segments = Futures.getThrowingException(controller.getEpochSegments(stream.getScope(), stream.getStreamName(), getEpoch(txId)));
    assert segments != null : "Epoch segments returned is null";
    Preconditions.checkState(segments.getSegments().size() > 0, "There should be at least 1 epoch segment");
    // Create OutputStream for every segment.
    Map<Segment, SegmentTransaction<Type>> transactions = new HashMap<>();
    DelegationTokenProvider tokenProvider = null;
    for (Segment s : segments.getSegments()) {
        if (tokenProvider == null) {
            tokenProvider = DelegationTokenProviderFactory.create(controller, s, AccessOperation.WRITE);
        }
        SegmentOutputStream out = outputStreamFactory.createOutputStreamForTransaction(s, txId, config, tokenProvider);
        SegmentTransactionImpl<Type> impl = new SegmentTransactionImpl<>(txId, out, serializer);
        transactions.put(s, impl);
    }
    pinger.startPing(txId);
    return new TransactionImpl<Type>(writerId, 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) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider)

Example 33 with SegmentOutputStream

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

the class SegmentSelector method createMissingWriters.

private void createMissingWriters(Consumer<Segment> segmentSealedCallBack) {
    for (Segment segment : currentSegments.getSegments()) {
        if (!writers.containsKey(segment)) {
            log.debug("Creating writer for segment {}", segment);
            SegmentOutputStream out = outputStreamFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config, tokenProvider);
            writers.put(segment, out);
        }
    }
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment)

Example 34 with SegmentOutputStream

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

the class SegmentSelector method updateSegments.

@Synchronized
private List<PendingEvent> updateSegments(StreamSegments newStreamSegments, Consumer<Segment> segmentSealedCallBack) {
    Preconditions.checkState(newStreamSegments.getNumberOfSegments() > 0, "Writers cannot proceed writing since the stream %s is sealed", stream);
    Preconditions.checkState(!isStreamSealed(), "Writers cannot proceed writing since the stream %s is sealed", stream);
    currentSegments = newStreamSegments;
    createMissingWriters(segmentSealedCallBack);
    List<PendingEvent> toResend = new ArrayList<>();
    Iterator<Entry<Segment, SegmentOutputStream>> iter = writers.entrySet().iterator();
    while (iter.hasNext()) {
        Entry<Segment, SegmentOutputStream> entry = iter.next();
        if (!currentSegments.getSegments().contains(entry.getKey())) {
            SegmentOutputStream writer = entry.getValue();
            log.info("Closing writer {} on segment {} during segment refresh", writer, entry.getKey());
            iter.remove();
            try {
                writer.close();
            } catch (SegmentSealedException e) {
                log.info("Caught segment sealed while refreshing on segment {}", entry.getKey());
            }
            toResend.addAll(writer.getUnackedEventsOnSeal());
        }
    }
    return toResend;
}
Also used : Entry(java.util.Map.Entry) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) ArrayList(java.util.ArrayList) SegmentSealedException(io.pravega.client.segment.impl.SegmentSealedException) Segment(io.pravega.client.segment.impl.Segment) Synchronized(lombok.Synchronized)

Example 35 with SegmentOutputStream

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

the class EventStreamWriterImpl method writeEventInternal.

private CompletableFuture<Void> writeEventInternal(String routingKey, Type event) {
    Preconditions.checkNotNull(event);
    Exceptions.checkNotClosed(closed.get(), this);
    ByteBuffer data = serializer.serialize(event);
    CompletableFuture<Void> ackFuture = new CompletableFuture<Void>();
    synchronized (writeFlushLock) {
        if (config.isEnableLargeEvents() && data.remaining() > Serializer.MAX_EVENT_SIZE) {
            writeLargeEvent(routingKey, Collections.singletonList(data), ackFuture);
        } else {
            synchronized (writeSealLock) {
                SegmentOutputStream segmentWriter = getSegmentWriter(routingKey);
                segmentWriter.write(PendingEvent.withHeader(routingKey, data, ackFuture));
            }
        }
    }
    return ackFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)57 Segment (io.pravega.client.segment.impl.Segment)49 Test (org.junit.Test)41 Cleanup (lombok.Cleanup)35 Controller (io.pravega.client.control.impl.Controller)31 MockController (io.pravega.client.stream.mock.MockController)26 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)24 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)18 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)16 ByteBuffer (java.nio.ByteBuffer)15 CompletableFuture (java.util.concurrent.CompletableFuture)11 DelegationTokenProvider (io.pravega.client.security.auth.DelegationTokenProvider)10 InOrder (org.mockito.InOrder)9 EventSegmentReader (io.pravega.client.segment.impl.EventSegmentReader)8 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)7 SegmentSealedException (io.pravega.client.segment.impl.SegmentSealedException)6 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)6 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)6 UUID (java.util.UUID)6