Search in sources :

Example 11 with StreamSegments

use of io.pravega.client.stream.impl.StreamSegments in project pravega by pravega.

the class StreamManagerImplTest method testSealedStream.

@Test(timeout = 10000)
public void testSealedStream() throws ConnectionFailedException {
    final String streamName = "stream";
    final Stream stream = new StreamImpl(defaultScope, streamName);
    // 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 = spy(new MockController(location.getEndpoint(), location.getPort(), connectionFactory, true));
    doReturn(CompletableFuture.completedFuture(true)).when(mockController).sealStream(defaultScope, streamName);
    StreamSegments empty = new StreamSegments(new TreeMap<>());
    doReturn(CompletableFuture.completedFuture(empty)).when(mockController).getCurrentSegments(defaultScope, streamName);
    ConnectionPoolImpl pool = new ConnectionPoolImpl(ClientConfig.builder().maxConnectionsPerSegmentStore(1).build(), connectionFactory);
    // Create a StreamManager
    @Cleanup final StreamManager streamManager = new StreamManagerImpl(mockController, pool);
    // Create a scope and stream and seal it.
    streamManager.createScope(defaultScope);
    streamManager.createStream(defaultScope, streamName, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(3)).build());
    streamManager.sealStream(defaultScope, streamName);
    // Fetch StreamInfo
    StreamInfo info = streamManager.getStreamInfo(defaultScope, streamName);
    // validate results.
    assertEquals(defaultScope, info.getScope());
    assertEquals(streamName, info.getStreamName());
    assertNotNull(info.getTailStreamCut());
    assertEquals(stream, info.getTailStreamCut().asImpl().getStream());
    assertEquals(0, info.getTailStreamCut().asImpl().getPositions().size());
    assertNotNull(info.getHeadStreamCut());
    assertEquals(stream, info.getHeadStreamCut().asImpl().getStream());
    assertEquals(3, info.getHeadStreamCut().asImpl().getPositions().size());
    assertTrue(info.isSealed());
}
Also used : ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) Cleanup(lombok.Cleanup) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) StreamImpl(io.pravega.client.stream.impl.StreamImpl) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamManager(io.pravega.client.admin.StreamManager) StreamInfo(io.pravega.client.admin.StreamInfo) MockController(io.pravega.client.stream.mock.MockController) Stream(io.pravega.client.stream.Stream) ClientConnection(io.pravega.client.connection.impl.ClientConnection) WireCommands(io.pravega.shared.protocol.netty.WireCommands) StreamSegments(io.pravega.client.stream.impl.StreamSegments) Test(org.junit.Test)

Example 12 with StreamSegments

use of io.pravega.client.stream.impl.StreamSegments in project pravega by pravega.

the class MockController method createTransaction.

@Override
public CompletableFuture<TxnSegments> createTransaction(final Stream stream, final long lease) {
    UUID txId = UUID.randomUUID();
    List<CompletableFuture<Void>> futures = new ArrayList<>();
    StreamSegments currentSegments = getCurrentSegments(stream);
    for (Segment segment : currentSegments.getSegments()) {
        futures.add(createSegmentTx(txId, segment));
    }
    return Futures.allOf(futures).thenApply(v -> new TxnSegments(currentSegments, txId));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) TxnSegments(io.pravega.client.stream.impl.TxnSegments) ArrayList(java.util.ArrayList) UUID(java.util.UUID) StreamSegments(io.pravega.client.stream.impl.StreamSegments) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) Segment(io.pravega.client.segment.impl.Segment) DeleteSegment(io.pravega.shared.protocol.netty.WireCommands.DeleteSegment)

Example 13 with StreamSegments

use of io.pravega.client.stream.impl.StreamSegments in project pravega by pravega.

the class MockController method getCurrentSegments.

private StreamSegments getCurrentSegments(Stream stream) {
    if (isStreamSealed(stream)) {
        return new StreamSegments(new TreeMap<>());
    } else {
        List<Segment> segmentsInStream = getSegmentsForStream(stream);
        TreeMap<Double, SegmentWithRange> segments = new TreeMap<>();
        for (int i = 0; i < segmentsInStream.size(); i++) {
            SegmentWithRange s = createRange(stream.getScope(), stream.getStreamName(), segmentsInStream.size(), i);
            segments.put(s.getRange().getHigh(), s);
        }
        return new StreamSegments(segments);
    }
}
Also used : SegmentWithRange(io.pravega.client.stream.impl.SegmentWithRange) TreeMap(java.util.TreeMap) StreamSegments(io.pravega.client.stream.impl.StreamSegments) CreateSegment(io.pravega.shared.protocol.netty.WireCommands.CreateSegment) Segment(io.pravega.client.segment.impl.Segment) DeleteSegment(io.pravega.shared.protocol.netty.WireCommands.DeleteSegment)

Example 14 with StreamSegments

use of io.pravega.client.stream.impl.StreamSegments in project pravega by pravega.

the class LocalControllerTest method testGetCurrentSegments.

@Test(timeout = 10000)
public void testGetCurrentSegments() {
    Controller.StreamInfo info = Controller.StreamInfo.newBuilder().setScope("scope").setStream("stream").build();
    Controller.SegmentId segment1 = Controller.SegmentId.newBuilder().setSegmentId(1).setStreamInfo(info).build();
    Controller.SegmentId segment2 = Controller.SegmentId.newBuilder().setSegmentId(2).setStreamInfo(info).build();
    Controller.SegmentId segment3 = Controller.SegmentId.newBuilder().setSegmentId(3).setStreamInfo(info).build();
    Controller.SegmentRange segmentRange1 = Controller.SegmentRange.newBuilder().setSegmentId(segment1).setMinKey(0.1).setMaxKey(0.3).build();
    Controller.SegmentRange segmentRange2 = Controller.SegmentRange.newBuilder().setSegmentId(segment2).setMinKey(0.4).setMaxKey(0.6).build();
    Controller.SegmentRange segmentRange3 = Controller.SegmentRange.newBuilder().setSegmentId(segment3).setMinKey(0.7).setMaxKey(1.0).build();
    List<Controller.SegmentRange> segmentsList = new ArrayList<Controller.SegmentRange>(3);
    segmentsList.add(segmentRange1);
    segmentsList.add(segmentRange2);
    segmentsList.add(segmentRange3);
    when(this.mockControllerService.getCurrentSegments(eq("scope"), eq("stream"), anyLong())).thenReturn(CompletableFuture.completedFuture(segmentsList));
    StreamSegments currentSegments = this.testController.getCurrentSegments("scope", "stream").join();
    assertEquals(3, currentSegments.getSegments().size());
    assertEquals(new Segment("scope", "stream", 1), currentSegments.getSegmentForKey(0.2));
    assertEquals(new Segment("scope", "stream", 3), currentSegments.getSegmentForKey(0.9));
}
Also used : ArrayList(java.util.ArrayList) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) StreamSegments(io.pravega.client.stream.impl.StreamSegments) Segment(io.pravega.client.segment.impl.Segment) Test(org.junit.Test)

Example 15 with StreamSegments

use of io.pravega.client.stream.impl.StreamSegments 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)

Aggregations

StreamSegments (io.pravega.client.stream.impl.StreamSegments)24 Segment (io.pravega.client.segment.impl.Segment)13 Test (org.junit.Test)11 StreamImpl (io.pravega.client.stream.impl.StreamImpl)7 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)6 TreeMap (java.util.TreeMap)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Cleanup (lombok.Cleanup)6 Stream (io.pravega.client.stream.Stream)5 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)5 SegmentWithRange (io.pravega.client.stream.impl.SegmentWithRange)5 ExecutionException (java.util.concurrent.ExecutionException)5 TxnSegments (io.pravega.client.stream.impl.TxnSegments)4 MockController (io.pravega.client.stream.mock.MockController)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 StatusRuntimeException (io.grpc.StatusRuntimeException)3 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)3