Search in sources :

Example 1 with Append

use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.

the class ClientConnectionInboundHandler method sendAsync.

@Override
public void sendAsync(List<Append> appends, CompletedCallback callback) {
    recentMessage.set(true);
    Channel ch;
    try {
        ch = getChannel();
    } catch (ConnectionFailedException e) {
        callback.complete(new ConnectionFailedException("Connection to " + connectionName + " is not established."));
        return;
    }
    PromiseCombiner combiner = new PromiseCombiner();
    for (Append append : appends) {
        batchSizeTracker.recordAppend(append.getEventNumber(), append.getData().readableBytes());
        combiner.add(ch.write(append));
    }
    ch.flush();
    ChannelPromise promise = ch.newPromise();
    promise.addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            Throwable cause = future.cause();
            callback.complete(cause == null ? null : new ConnectionFailedException(cause));
        }
    });
    combiner.finish(promise);
}
Also used : PromiseCombiner(io.netty.util.concurrent.PromiseCombiner) Append(io.pravega.shared.protocol.netty.Append) Channel(io.netty.channel.Channel) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future) ChannelPromise(io.netty.channel.ChannelPromise) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException)

Example 2 with Append

use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.

the class AppendProcessor method getNextAppend.

private Append getNextAppend() {
    synchronized (lock) {
        if (outstandingAppend != null || waitingAppends.isEmpty()) {
            return null;
        }
        UUID writer = waitingAppends.keys().iterator().next();
        List<Append> appends = waitingAppends.get(writer);
        if (appends.get(0).isConditional()) {
            outstandingAppend = appends.remove(0);
        } else {
            ByteBuf[] toAppend = new ByteBuf[appends.size()];
            Append last = appends.get(0);
            int eventCount = 0;
            int i = -1;
            for (Iterator<Append> iterator = appends.iterator(); iterator.hasNext(); ) {
                Append a = iterator.next();
                if (a.isConditional()) {
                    break;
                }
                i++;
                toAppend[i] = a.getData();
                last = a;
                eventCount += a.getEventCount();
                iterator.remove();
            }
            ByteBuf data = Unpooled.wrappedBuffer(toAppend);
            String segment = last.getSegment();
            long eventNumber = last.getEventNumber();
            outstandingAppend = new Append(segment, writer, eventNumber, eventCount, data, null);
        }
        return outstandingAppend;
    }
}
Also used : Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) UUID(java.util.UUID) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with Append

use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.

the class AppendProcessor method performNextWrite.

/**
 * If there isn't already an append outstanding against the store, write a new one.
 * Appends are opportunistically batched here. i.e. If many are waiting they are combined into a single append and
 * that is written.
 */
private void performNextWrite() {
    Append append = getNextAppend();
    if (append == null) {
        return;
    }
    long traceId = LoggerHelpers.traceEnter(log, "storeAppend", append);
    Timer timer = new Timer();
    storeAppend(append).whenComplete((v, e) -> {
        handleAppendResult(append, e);
        LoggerHelpers.traceLeave(log, "storeAppend", traceId, v, e);
        if (e == null) {
            WRITE_STREAM_SEGMENT.reportSuccessEvent(timer.getElapsed());
        } else {
            WRITE_STREAM_SEGMENT.reportFailEvent(timer.getElapsed());
        }
    }).whenComplete((v, e) -> append.getData().release());
}
Also used : AuthenticationException(io.pravega.common.auth.AuthenticationException) Arrays(java.util.Arrays) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) AuthHandler(io.pravega.auth.AuthHandler) MetricsNames.nameFromSegment(io.pravega.shared.MetricsNames.nameFromSegment) Unpooled(io.netty.buffer.Unpooled) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) Pair(org.apache.commons.lang3.tuple.Pair) Duration(java.time.Duration) SegmentStatsRecorder(io.pravega.segmentstore.server.host.stat.SegmentStatsRecorder) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) ContainerNotFoundException(io.pravega.segmentstore.contracts.ContainerNotFoundException) UUID(java.util.UUID) GuardedBy(javax.annotation.concurrent.GuardedBy) MetricsProvider(io.pravega.shared.metrics.MetricsProvider) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) DelegationTokenVerifier(io.pravega.segmentstore.server.host.delegationtoken.DelegationTokenVerifier) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SegmentIsSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentIsSealed) Getter(lombok.Getter) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) Exceptions(io.pravega.common.Exceptions) ConditionalCheckFailed(io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) BadAttributeUpdateException(io.pravega.segmentstore.contracts.BadAttributeUpdateException) Append(io.pravega.shared.protocol.netty.Append) DelegatingRequestProcessor(io.pravega.shared.protocol.netty.DelegatingRequestProcessor) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ByteBuf(io.netty.buffer.ByteBuf) DynamicLogger(io.pravega.shared.metrics.DynamicLogger) SEGMENT_WRITE_BYTES(io.pravega.shared.MetricsNames.SEGMENT_WRITE_BYTES) SEGMENT_WRITE_LATENCY(io.pravega.shared.MetricsNames.SEGMENT_WRITE_LATENCY) Hello(io.pravega.shared.protocol.netty.WireCommands.Hello) SegmentAlreadyExists(io.pravega.shared.protocol.netty.WireCommands.SegmentAlreadyExists) LoggerHelpers(io.pravega.common.LoggerHelpers) StatsLogger(io.pravega.shared.metrics.StatsLogger) Iterator(java.util.Iterator) EVENT_COUNT(io.pravega.segmentstore.contracts.Attributes.EVENT_COUNT) OpStatsLogger(io.pravega.shared.metrics.OpStatsLogger) SEGMENT_WRITE_EVENTS(io.pravega.shared.MetricsNames.SEGMENT_WRITE_EVENTS) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Timer(io.pravega.common.Timer) WrongHost(io.pravega.shared.protocol.netty.WireCommands.WrongHost) InvalidEventNumber(io.pravega.shared.protocol.netty.WireCommands.InvalidEventNumber) RequestProcessor(io.pravega.shared.protocol.netty.RequestProcessor) OperationUnsupported(io.pravega.shared.protocol.netty.WireCommands.OperationUnsupported) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) TooManyAttributesException(io.pravega.segmentstore.contracts.TooManyAttributesException) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) Timer(io.pravega.common.Timer)

Example 4 with Append

use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.

the class ClientConnectionInboundHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    when(buffer.readableBytes()).thenReturn(10);
    appendCmd = new Append("segment0", UUID.randomUUID(), 2, buffer, 10L);
    doNothing().when(tracker).recordAppend(anyLong(), anyInt());
    when(ctx.channel()).thenReturn(ch);
    when(ch.eventLoop()).thenReturn(loop);
    when(ch.writeAndFlush(any(Object.class))).thenReturn(completedFuture);
    handler = new ClientConnectionInboundHandler("testConnection", processor, tracker);
}
Also used : Append(io.pravega.shared.protocol.netty.Append) Before(org.junit.Before)

Example 5 with Append

use of io.pravega.shared.protocol.netty.Append in project pravega by pravega.

the class SegmentOutputStreamTest method testFailDurringFlush.

@Test(timeout = 10000)
public void testFailDurringFlush() throws ConnectionFailedException {
    UUID cid = UUID.randomUUID();
    PravegaNodeUri uri = new PravegaNodeUri("endpoint", SERVICE_PORT);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
    // Ensure task submitted to executor is run inline.
    implementAsDirectExecutor(executor);
    cf.setExecutor(executor);
    MockController controller = new MockController(uri.getEndpoint(), uri.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(uri, connection);
    InOrder order = Mockito.inOrder(connection);
    SegmentOutputStreamImpl output = new SegmentOutputStreamImpl(SEGMENT, controller, cf, cid, segmentSealedCallback, RETRY_SCHEDULE, "");
    output.reconnect();
    order.verify(connection).send(new SetupAppend(1, cid, SEGMENT, ""));
    cf.getProcessor(uri).appendSetup(new AppendSetup(1, SEGMENT, cid, 0));
    ByteBuffer data = getBuffer("test");
    CompletableFuture<Boolean> ack = new CompletableFuture<>();
    output.write(new PendingEvent(null, data, ack));
    order.verify(connection).send(new Append(SEGMENT, cid, 1, Unpooled.wrappedBuffer(data), null));
    assertEquals(false, ack.isDone());
    Mockito.doThrow(new ConnectionFailedException()).when(connection).send(new WireCommands.KeepAlive());
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            cf.getProcessor(uri).appendSetup(new AppendSetup(3, SEGMENT, cid, 1));
            return null;
        }
    }).when(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
    Async.testBlocking(() -> {
        output.flush();
    }, () -> {
        cf.getProcessor(uri).connectionDropped();
    });
    order.verify(connection).send(new SetupAppend(3, cid, SEGMENT, ""));
    assertEquals(true, ack.isDone());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InOrder(org.mockito.InOrder) ByteBuffer(java.nio.ByteBuffer) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) PendingEvent(io.pravega.client.stream.impl.PendingEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Aggregations

Append (io.pravega.shared.protocol.netty.Append)74 UUID (java.util.UUID)67 Test (org.junit.Test)64 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)52 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)44 WireCommands (io.pravega.shared.protocol.netty.WireCommands)37 Cleanup (lombok.Cleanup)37 CompletableFuture (java.util.concurrent.CompletableFuture)33 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)31 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)27 InOrder (org.mockito.InOrder)22 ClientConnection (io.pravega.client.connection.impl.ClientConnection)21 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)21 MockController (io.pravega.client.stream.mock.MockController)21 Event (io.pravega.shared.protocol.netty.WireCommands.Event)21 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)21 lombok.val (lombok.val)20 DataAppended (io.pravega.shared.protocol.netty.WireCommands.DataAppended)17 ByteBuffer (java.nio.ByteBuffer)17 ByteBuf (io.netty.buffer.ByteBuf)16