Search in sources :

Example 41 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class LargeEventWriterTest method testSegmentSealed.

@Test(timeout = 5000)
public void testSegmentSealed() throws ConnectionFailedException, SegmentSealedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    @Cleanup InlineExecutor executor = new InlineExecutor();
    EventWriterConfig config = EventWriterConfig.builder().enableLargeEvents(true).build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = spy(new MockController("localhost", 0, connectionFactory, false));
    controller.createScope(scope).join();
    controller.createStream(scope, streamName, StreamConfiguration.builder().build());
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    connectionFactory.provideConnection(location, connection);
    SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
    Mockito.when(streamFactory.createOutputStreamForSegment(any(), any(), any(), any())).thenReturn(outputStream);
    AtomicBoolean failed = new AtomicBoolean(false);
    AtomicBoolean succeeded = new AtomicBoolean(false);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
            failed.set(true);
            connectionFactory.getProcessor(location).process(new SegmentIsSealed(argument.getRequestId(), segment.getScopedName(), "stacktrace", 0));
            return null;
        }
    }).doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            CreateTransientSegment argument = (CreateTransientSegment) invocation.getArgument(0);
            succeeded.set(true);
            connectionFactory.getProcessor(location).process(new SegmentCreated(argument.getRequestId(), "transient-segment"));
            return null;
        }
    }).when(connection).send(any(CreateTransientSegment.class));
    Mockito.doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            List<Long> predecessors = Arrays.asList(0L);
            return CompletableFuture.completedFuture(new StreamSegmentsWithPredecessors(ImmutableMap.of(new SegmentWithRange(new Segment(scope, streamName, NameUtils.computeSegmentId(1, 1)), 0.0, 0.5), predecessors, new SegmentWithRange(new Segment(scope, streamName, NameUtils.computeSegmentId(2, 1)), 0.5, 1.0), predecessors), ""));
        }
    }).when(controller).getSuccessors(segment);
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    answerRequest(connectionFactory, connection, location, ConditionalBlockEnd.class, r -> {
        ByteBuf data = r.getData();
        return new DataAppended(r.getRequestId(), r.getWriterId(), r.getEventNumber(), r.getEventNumber() - 1, r.getExpectedOffset() + data.readableBytes());
    });
    answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
        return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
    });
    @Cleanup EventStreamWriter<byte[]> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, new ByteArraySerializer(), config, executor, executor, connectionFactory);
    writer.writeEvent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
    writer.writeEvent(new byte[Serializer.MAX_EVENT_SIZE * 2]);
    assertTrue(failed.get());
    assertTrue(succeeded.get());
    InOrder order = Mockito.inOrder(connection, outputStream);
    order.verify(outputStream).write(any(PendingEvent.class));
    order.verify(outputStream).flush();
    order.verify(connection, times(2)).send(any(CreateTransientSegment.class));
    order.verify(connection).send(any(SetupAppend.class));
    order.verify(connection, times(3)).send(any(ConditionalBlockEnd.class));
    order.verify(connection).send(any(MergeSegments.class));
    order.verify(connection).close();
    order.verifyNoMoreInteractions();
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) ConditionalBlockEnd(io.pravega.shared.protocol.netty.WireCommands.ConditionalBlockEnd) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InlineExecutor(io.pravega.test.common.InlineExecutor) SegmentIsSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentIsSealed) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ClientConnection(io.pravega.client.connection.impl.ClientConnection) List(java.util.List) ArrayList(java.util.ArrayList) InOrder(org.mockito.InOrder) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Answer(org.mockito.stubbing.Answer) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) MergeSegments(io.pravega.shared.protocol.netty.WireCommands.MergeSegments) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) MockController(io.pravega.client.stream.mock.MockController) SegmentsMerged(io.pravega.shared.protocol.netty.WireCommands.SegmentsMerged) Test(org.junit.Test)

Example 42 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class LargeEventWriterTest method testEventStreamWriter.

@Test(timeout = 5000)
public void testEventStreamWriter() throws ConnectionFailedException, SegmentSealedException {
    String scope = "scope";
    String streamName = "stream";
    StreamImpl stream = new StreamImpl(scope, streamName);
    Segment segment = new Segment(scope, streamName, 0);
    @Cleanup InlineExecutor executor = new InlineExecutor();
    EventWriterConfig config = EventWriterConfig.builder().enableLargeEvents(true).build();
    SegmentOutputStreamFactory streamFactory = Mockito.mock(SegmentOutputStreamFactory.class);
    MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    MockController controller = new MockController("localhost", 0, connectionFactory, false);
    controller.createScope(scope).join();
    controller.createStream(scope, streamName, StreamConfiguration.builder().build());
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    connectionFactory.provideConnection(location, connection);
    SegmentOutputStream outputStream = Mockito.mock(SegmentOutputStream.class);
    Mockito.when(streamFactory.createOutputStreamForSegment(eq(segment), any(), any(), any())).thenReturn(outputStream);
    answerRequest(connectionFactory, connection, location, CreateTransientSegment.class, r -> new SegmentCreated(r.getRequestId(), "transient-segment"));
    answerRequest(connectionFactory, connection, location, SetupAppend.class, r -> new AppendSetup(r.getRequestId(), segment.getScopedName(), r.getWriterId(), WireCommands.NULL_ATTRIBUTE_VALUE));
    answerRequest(connectionFactory, connection, location, ConditionalBlockEnd.class, r -> {
        ByteBuf data = r.getData();
        return new DataAppended(r.getRequestId(), r.getWriterId(), r.getEventNumber(), r.getEventNumber() - 1, r.getExpectedOffset() + data.readableBytes());
    });
    answerRequest(connectionFactory, connection, location, MergeSegments.class, r -> {
        return new SegmentsMerged(r.getRequestId(), r.getSource(), r.getTarget(), -1);
    });
    @Cleanup EventStreamWriter<byte[]> writer = new EventStreamWriterImpl<>(stream, "id", controller, streamFactory, new ByteArraySerializer(), config, executor, executor, connectionFactory);
    writer.writeEvent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
    writer.writeEvent(new byte[Serializer.MAX_EVENT_SIZE * 2]);
    InOrder order = Mockito.inOrder(connection, outputStream);
    order.verify(outputStream).write(any(PendingEvent.class));
    order.verify(outputStream).flush();
    order.verify(connection).send(any(CreateTransientSegment.class));
    order.verify(connection).send(any(SetupAppend.class));
    order.verify(connection, times(3)).send(any(ConditionalBlockEnd.class));
    order.verify(connection).send(any(MergeSegments.class));
    order.verify(connection).close();
    order.verifyNoMoreInteractions();
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) ConditionalBlockEnd(io.pravega.shared.protocol.netty.WireCommands.ConditionalBlockEnd) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InlineExecutor(io.pravega.test.common.InlineExecutor) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ClientConnection(io.pravega.client.connection.impl.ClientConnection) InOrder(org.mockito.InOrder) CreateTransientSegment(io.pravega.shared.protocol.netty.WireCommands.CreateTransientSegment) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) SegmentCreated(io.pravega.shared.protocol.netty.WireCommands.SegmentCreated) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) MergeSegments(io.pravega.shared.protocol.netty.WireCommands.MergeSegments) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) MockController(io.pravega.client.stream.mock.MockController) SegmentsMerged(io.pravega.shared.protocol.netty.WireCommands.SegmentsMerged) Test(org.junit.Test)

Example 43 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class MockController method sendRequestOverNewConnection.

private <T> void sendRequestOverNewConnection(WireCommand request, ReplyProcessor replyProcessor, CompletableFuture<T> resultFuture) {
    ClientConnection connection = getAndHandleExceptions(connectionPool.getClientConnection(Flow.from(((Request) request).getRequestId()), new PravegaNodeUri(endpoint, port), replyProcessor), RuntimeException::new);
    resultFuture.whenComplete((result, e) -> {
        connection.close();
    });
    try {
        connection.send(request);
    } catch (ConnectionFailedException cfe) {
        resultFuture.completeExceptionally(cfe);
    }
}
Also used : PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) ClientConnection(io.pravega.client.connection.impl.ClientConnection) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException)

Example 44 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class ByteStreanWriterImplTest method setup.

@Before
public void setup() {
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
    connectionFactory = new MockConnectionFactoryImpl();
    ClientConnection connection = mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, connection);
    controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
    controller.createScope(SCOPE);
    controller.createStream(SCOPE, STREAM, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
    MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
    StreamSegments segments = Futures.getThrowingException(controller.getCurrentSegments(SCOPE, STREAM));
    Preconditions.checkState(segments.getNumberOfSegments() > 0, "Stream is sealed");
    Preconditions.checkState(segments.getNumberOfSegments() == 1, "Stream is configured with more than one segment");
    Segment segment = segments.getSegments().iterator().next();
    EventWriterConfig config = EventWriterConfig.builder().retryAttempts(Integer.MAX_VALUE).build();
    DelegationTokenProvider tokenProvider = DelegationTokenProviderFactory.create(controller, segment, AccessOperation.WRITE);
    mockWriter = new ByteStreamWriterImpl(streamFactory.createOutputStreamForSegment(segment, config, tokenProvider), streamFactory.createSegmentMetadataClient(segment, tokenProvider));
}
Also used : PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) MockSegmentStreamFactory(io.pravega.client.stream.mock.MockSegmentStreamFactory) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) StreamSegments(io.pravega.client.stream.impl.StreamSegments) Segment(io.pravega.client.segment.impl.Segment) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) Before(org.junit.Before)

Example 45 with ClientConnection

use of io.pravega.client.connection.impl.ClientConnection in project pravega by pravega.

the class StreamManagerImplTest method testListScopes.

@Test(timeout = 10000)
public void testListScopes() throws ConnectionFailedException {
    // Setup Mocks
    ClientConnection connection = mock(ClientConnection.class);
    PravegaNodeUri location = new PravegaNodeUri("localhost", 0);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.CreateSegment request = (WireCommands.CreateSegment) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.SegmentCreated(request.getRequestId(), request.getSegment()));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.CreateSegment.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            WireCommands.GetStreamSegmentInfo request = (WireCommands.GetStreamSegmentInfo) invocation.getArgument(0);
            connectionFactory.getProcessor(location).process(new WireCommands.StreamSegmentInfo(request.getRequestId(), request.getSegmentName(), true, false, false, 0, 0, 0));
            return null;
        }
    }).when(connection).send(Mockito.any(WireCommands.GetStreamSegmentInfo.class));
    connectionFactory.provideConnection(location, connection);
    MockController mockController = new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true);
    ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    @Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
    String scope = "scope";
    String scope1 = "scope1";
    String scope2 = "scope2";
    String scope3 = "scope3";
    String stream1 = "stream1";
    streamManager.createScope(scope);
    streamManager.createScope(scope1);
    streamManager.createScope(scope2);
    streamManager.createScope(scope3);
    streamManager.createStream(scope, stream1, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    ArrayList<String> result = Lists.newArrayList(streamManager.listScopes());
    assertEquals(result.size(), 4);
    assertTrue(streamManager.checkScopeExists(scope));
    assertFalse(streamManager.checkScopeExists("nonExistent"));
    assertTrue(streamManager.checkStreamExists(scope, stream1));
    assertFalse(streamManager.checkStreamExists(scope, "nonExistent"));
    assertFalse(streamManager.checkStreamExists("nonExistent", "nonExistent"));
}
Also used : ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) Cleanup(lombok.Cleanup) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamManager(io.pravega.client.admin.StreamManager) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Aggregations

ClientConnection (io.pravega.client.connection.impl.ClientConnection)87 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)81 MockController (io.pravega.client.stream.mock.MockController)79 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)78 Test (org.junit.Test)77 Cleanup (lombok.Cleanup)51 InvocationOnMock (org.mockito.invocation.InvocationOnMock)43 WireCommands (io.pravega.shared.protocol.netty.WireCommands)40 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)35 AppendSetup (io.pravega.shared.protocol.netty.WireCommands.AppendSetup)34 UUID (java.util.UUID)34 ReplyProcessor (io.pravega.shared.protocol.netty.ReplyProcessor)33 ByteBuffer (java.nio.ByteBuffer)31 CompletableFuture (java.util.concurrent.CompletableFuture)30 InOrder (org.mockito.InOrder)27 Append (io.pravega.shared.protocol.netty.Append)23 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)23 Answer (org.mockito.stubbing.Answer)17 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)16 ArrayList (java.util.ArrayList)14