Search in sources :

Example 51 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)

Example 52 with SegmentOutputStream

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

the class EventStreamWriterImpl method resend.

@GuardedBy("writeSealLock")
private void resend(List<PendingEvent> toResend) {
    while (!toResend.isEmpty()) {
        List<PendingEvent> unsent = new ArrayList<>();
        boolean sendFailed = false;
        log.info("Resending {} events", toResend.size());
        for (PendingEvent event : toResend) {
            if (sendFailed) {
                unsent.add(event);
            } else {
                SegmentOutputStream segmentWriter = selector.getSegmentOutputStreamForKey(event.getRoutingKey());
                if (segmentWriter == null) {
                    log.info("No writer for segment during resend.");
                    unsent.addAll(selector.refreshSegmentEventWriters(segmentSealedCallBack));
                    sendFailed = true;
                } else {
                    segmentWriter.write(event);
                }
            }
        }
        toResend = unsent;
    }
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) ArrayList(java.util.ArrayList) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 53 with SegmentOutputStream

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

the class EventStreamWriterImpl method getSegmentWriter.

private SegmentOutputStream getSegmentWriter(String routingKey) {
    SegmentOutputStream segmentWriter = selector.getSegmentOutputStreamForKey(routingKey);
    while (segmentWriter == null) {
        log.info("Don't have a writer for segment: {}", selector.getSegmentForEvent(routingKey));
        handleMissingLog();
        segmentWriter = selector.getSegmentOutputStreamForKey(routingKey);
    }
    return segmentWriter;
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream)

Example 54 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 55 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)

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