Search in sources :

Example 11 with SegmentOutputStream

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

the class ReadTest method readThroughSegmentClient.

@Test
public void readThroughSegmentClient() throws SegmentSealedException, EndOfSegmentException, SegmentTruncatedException {
    String endpoint = "localhost";
    String scope = "scope";
    String stream = "stream";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world\n";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build());
    Controller controller = new MockController(endpoint, port, clientCF);
    controller.createScope(scope);
    controller.createStream(StreamConfiguration.builder().scope(scope).streamName(stream).build());
    SegmentOutputStreamFactoryImpl segmentproducerClient = new SegmentOutputStreamFactoryImpl(controller, clientCF);
    SegmentInputStreamFactoryImpl segmentConsumerClient = new SegmentInputStreamFactoryImpl(controller, clientCF);
    Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
    @Cleanup("close") SegmentOutputStream out = segmentproducerClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), "");
    out.write(new PendingEvent(null, ByteBuffer.wrap(testString.getBytes()), new CompletableFuture<>()));
    out.flush();
    @Cleanup("close") SegmentInputStream in = segmentConsumerClient.createInputStreamForSegment(segment);
    ByteBuffer result = in.read();
    assertEquals(ByteBuffer.wrap(testString.getBytes()), result);
    // Test large write followed by read
    out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
    out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[15]), new CompletableFuture<>()));
    out.write(new PendingEvent(null, ByteBuffer.wrap(new byte[150000]), new CompletableFuture<>()));
    assertEquals(in.read().capacity(), 15);
    assertEquals(in.read().capacity(), 15);
    assertEquals(in.read().capacity(), 150000);
}
Also used : MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.stream.impl.Controller) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ByteBuffer(java.nio.ByteBuffer) Segment(io.pravega.client.segment.impl.Segment) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PendingEvent(io.pravega.client.stream.impl.PendingEvent) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockController(io.pravega.client.stream.mock.MockController) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) Test(org.junit.Test)

Example 12 with SegmentOutputStream

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

the class AppendTest method appendThroughSegmentClient.

@Test
public void appendThroughSegmentClient() throws Exception {
    String endpoint = "localhost";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world\n";
    String scope = "scope";
    String stream = "stream";
    StreamSegmentStore store = this.serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup ConnectionFactory clientCF = new ConnectionFactoryImpl(ClientConfig.builder().build());
    Controller controller = new MockController(endpoint, port, clientCF);
    controller.createScope(scope);
    controller.createStream(StreamConfiguration.builder().scope(scope).streamName(stream).build());
    SegmentOutputStreamFactoryImpl segmentClient = new SegmentOutputStreamFactoryImpl(controller, clientCF);
    Segment segment = Futures.getAndHandleExceptions(controller.getCurrentSegments(scope, stream), RuntimeException::new).getSegments().iterator().next();
    @Cleanup SegmentOutputStream out = segmentClient.createOutputStreamForSegment(segment, segmentSealedCallback, EventWriterConfig.builder().build(), "");
    CompletableFuture<Boolean> ack = new CompletableFuture<>();
    out.write(new PendingEvent(null, ByteBuffer.wrap(testString.getBytes()), ack));
    assertTrue(ack.get(5, TimeUnit.SECONDS));
}
Also used : MockController(io.pravega.client.stream.mock.MockController) Controller(io.pravega.client.stream.impl.Controller) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PendingEvent(io.pravega.client.stream.impl.PendingEvent) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockController(io.pravega.client.stream.mock.MockController) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) Test(org.junit.Test)

Example 13 with SegmentOutputStream

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

the class EventStreamWriterImpl method flushInternal.

private void flushInternal() {
    writeFlushLock.readLock().lock();
    boolean success = false;
    try {
        while (!success) {
            success = true;
            for (SegmentOutputStream writer : selector.getWriters()) {
                try {
                    writer.flush();
                } catch (SegmentSealedException e) {
                    // Segment sealed exception observed during a flush. Re-run flush on all the available writers.
                    success = false;
                    log.warn("Flush failed due to {}, it will be retried.", e.getMessage());
                }
            }
        }
    } finally {
        writeFlushLock.readLock().unlock();
    }
}
Also used : SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) SegmentSealedException(io.pravega.client.segment.impl.SegmentSealedException)

Example 14 with SegmentOutputStream

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

the class EventStreamWriterImpl method resend.

@GuardedBy("writeFlushLock")
private void resend(List<PendingEvent> toResend) {
    while (!toResend.isEmpty()) {
        List<PendingEvent> unsent = new ArrayList<>();
        boolean sendFailed = false;
        for (PendingEvent event : toResend) {
            if (sendFailed) {
                unsent.add(event);
            } else {
                SegmentOutputStream segmentWriter = selector.getSegmentOutputStreamForKey(event.getRoutingKey());
                if (segmentWriter == null) {
                    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 15 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<Boolean> ackFuture = new CompletableFuture<Boolean>();
    writeFlushLock.writeLock().lock();
    try {
        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);
        }
        segmentWriter.write(new PendingEvent(routingKey, data, ackFuture));
    } finally {
        writeFlushLock.writeLock().unlock();
    }
    CompletableFuture<Void> result = new CompletableFuture<>();
    ackFuture.whenComplete((bool, exception) -> {
        if (exception != null) {
            result.completeExceptionally(exception);
        } else {
            if (bool) {
                result.complete(null);
            } else {
                result.completeExceptionally(new IllegalStateException("Condition failed for non-conditional " + "write!?"));
            }
        }
    });
    return result;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)25 Segment (io.pravega.client.segment.impl.Segment)19 Test (org.junit.Test)16 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)11 ByteBuffer (java.nio.ByteBuffer)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 SegmentMetadataClient (io.pravega.client.segment.impl.SegmentMetadataClient)5 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)5 Cleanup (lombok.Cleanup)5 SegmentInputStream (io.pravega.client.segment.impl.SegmentInputStream)4 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)3 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)3 SegmentOutputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl)3 SegmentSealedException (io.pravega.client.segment.impl.SegmentSealedException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 SegmentInputStreamFactoryImpl (io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl)2 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)2 Controller (io.pravega.client.stream.impl.Controller)2 PendingEvent (io.pravega.client.stream.impl.PendingEvent)2 MockController (io.pravega.client.stream.mock.MockController)2