Search in sources :

Example 1 with ReplyProcessor

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

the class SegmentMetadataClientTest method testCurrentStreamLength.

@Test(timeout = 10000)
public void testCurrentStreamLength() throws Exception {
    Segment segment = new Segment("scope", "testRetry", 4);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
    @Cleanup MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf);
    @Cleanup ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(endpoint, connection);
    SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
    client.getConnection();
    ReplyProcessor processor = cf.getProcessor(endpoint);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            processor.process(new StreamSegmentInfo(1, segment.getScopedName(), true, false, false, 0, 123, 121));
            return null;
        }
    }).when(connection).send(new WireCommands.GetStreamSegmentInfo(1, segment.getScopedName(), ""));
    long length = client.fetchCurrentSegmentLength();
    assertEquals(123, length);
}
Also used : StreamSegmentInfo(io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo) Cleanup(lombok.Cleanup) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 2 with ReplyProcessor

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

the class SegmentMetadataClientTest method compareAndSetAttribute.

@Test(timeout = 10000)
public void compareAndSetAttribute() throws Exception {
    UUID attributeId = SegmentAttribute.RevisionStreamClientMark.getValue();
    Segment segment = new Segment("scope", "testRetry", 4);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
    MockConnectionFactoryImpl cf = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), cf);
    ClientConnection connection = mock(ClientConnection.class);
    cf.provideConnection(endpoint, connection);
    SegmentMetadataClientImpl client = new SegmentMetadataClientImpl(segment, controller, cf, "");
    client.getConnection();
    ReplyProcessor processor = cf.getProcessor(endpoint);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            processor.process(new SegmentAttributeUpdated(1, true));
            return null;
        }
    }).when(connection).send(new WireCommands.UpdateSegmentAttribute(1, segment.getScopedName(), attributeId, 1234, -1234, ""));
    assertTrue(client.compareAndSetAttribute(SegmentAttribute.RevisionStreamClientMark, -1234, 1234));
}
Also used : PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SegmentAttributeUpdated(io.pravega.shared.protocol.netty.WireCommands.SegmentAttributeUpdated) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 3 with ReplyProcessor

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

the class SegmentHelper method sendRequestAsync.

private <ResultT> void sendRequestAsync(final WireCommand request, final ReplyProcessor replyProcessor, final CompletableFuture<ResultT> resultFuture, final ConnectionFactory connectionFactory, final PravegaNodeUri uri) {
    CompletableFuture<ClientConnection> connectionFuture = connectionFactory.establishConnection(uri, replyProcessor);
    connectionFuture.whenComplete((connection, e) -> {
        if (connection == null) {
            resultFuture.completeExceptionally(new WireCommandFailedException(new ConnectionFailedException(e), request.getType(), WireCommandFailedException.Reason.ConnectionFailed));
        } else {
            try {
                connection.send(request);
            } catch (ConnectionFailedException cfe) {
                throw new WireCommandFailedException(cfe, request.getType(), WireCommandFailedException.Reason.ConnectionFailed);
            } catch (Exception e2) {
                throw new RuntimeException(e2);
            }
        }
    }).exceptionally(e -> {
        Throwable cause = Exceptions.unwrap(e);
        if (cause instanceof WireCommandFailedException) {
            resultFuture.completeExceptionally(cause);
        } else if (cause instanceof ConnectionFailedException) {
            resultFuture.completeExceptionally(new WireCommandFailedException(cause, request.getType(), WireCommandFailedException.Reason.ConnectionFailed));
        } else {
            resultFuture.completeExceptionally(new RuntimeException(cause));
        }
        return null;
    });
    resultFuture.whenComplete((result, e) -> {
        connectionFuture.thenAccept(ClientConnection::close);
    });
}
Also used : ClientConnection(io.pravega.client.netty.impl.ClientConnection) AuthenticationException(io.pravega.common.auth.AuthenticationException) Segment(io.pravega.client.segment.impl.Segment) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Exceptions(io.pravega.common.Exceptions) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) Pair(org.apache.commons.lang3.tuple.Pair) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) WireCommandType(io.pravega.shared.protocol.netty.WireCommandType) ModelHelper(io.pravega.client.stream.impl.ModelHelper) Host(io.pravega.common.cluster.Host) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) WireCommand(io.pravega.shared.protocol.netty.WireCommand) AtomicLong(java.util.concurrent.atomic.AtomicLong) Slf4j(lombok.extern.slf4j.Slf4j) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) TxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.TxnStatus) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) ClientConnection(io.pravega.client.netty.impl.ClientConnection) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) AuthenticationException(io.pravega.common.auth.AuthenticationException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException)

Example 4 with ReplyProcessor

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

the class RawClientTest method testRequestReply.

@Test
public void testRequestReply() throws ConnectionFailedException, InterruptedException, ExecutionException {
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", -1);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, connection);
    @Cleanup RawClient rawClient = new RawClient(controller, connectionFactory, new Segment("scope", "testHello", 0));
    UUID id = UUID.randomUUID();
    ConditionalAppend request = new ConditionalAppend(id, 1, 0, Unpooled.EMPTY_BUFFER);
    CompletableFuture<Reply> future = rawClient.sendRequest(1, request);
    Mockito.verify(connection).send(request);
    assertFalse(future.isDone());
    ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
    DataAppended reply = new DataAppended(id, 1, 0);
    processor.process(reply);
    assertTrue(future.isDone());
    assertEquals(reply, future.get());
}
Also used : ConditionalAppend(io.pravega.shared.protocol.netty.WireCommands.ConditionalAppend) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Reply(io.pravega.shared.protocol.netty.Reply) UUID(java.util.UUID) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 5 with ReplyProcessor

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

the class AsyncSegmentInputStreamTest method testRead.

@Test(timeout = 10000)
public void testRead() throws ConnectionFailedException {
    Segment segment = new Segment("scope", "testRead", 1);
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
    @Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, "");
    ClientConnection c = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, c);
    WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, ByteBuffer.allocate(0));
    CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
    Async.testBlocking(() -> readFuture.get(), () -> {
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        processor.segmentRead(segmentRead);
    });
    verify(c).sendAsync(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, ""));
    assertTrue(Futures.isSuccessful(readFuture));
    assertEquals(segmentRead, readFuture.join());
    verifyNoMoreInteractions(c);
}
Also used : Cleanup(lombok.Cleanup) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) SegmentRead(io.pravega.shared.protocol.netty.WireCommands.SegmentRead) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) ReadSegment(io.pravega.shared.protocol.netty.WireCommands.ReadSegment) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.netty.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Aggregations

PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)10 ReplyProcessor (io.pravega.shared.protocol.netty.ReplyProcessor)10 ClientConnection (io.pravega.client.netty.impl.ClientConnection)9 MockController (io.pravega.client.stream.mock.MockController)9 WireCommands (io.pravega.shared.protocol.netty.WireCommands)9 Test (org.junit.Test)9 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)8 Cleanup (lombok.Cleanup)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 UUID (java.util.UUID)4 StreamSegmentInfo (io.pravega.shared.protocol.netty.WireCommands.StreamSegmentInfo)3 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)2 Segment (io.pravega.client.segment.impl.Segment)2 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)2 ReadSegment (io.pravega.shared.protocol.netty.WireCommands.ReadSegment)2 SegmentRead (io.pravega.shared.protocol.netty.WireCommands.SegmentRead)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 InOrder (org.mockito.InOrder)2 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)1 ModelHelper (io.pravega.client.stream.impl.ModelHelper)1