Search in sources :

Example 11 with ServiceBuilder

use of io.pravega.segmentstore.server.store.ServiceBuilder in project pravega by pravega.

the class AutoCheckpointTest method testOnlyOneOutstanding.

@Test(timeout = 30000)
public void testOnlyOneOutstanding() throws ReinitializationRequiredException, DurableDataLogException {
    String endpoint = "localhost";
    String streamName = "abc";
    String readerGroup = "group";
    int port = TestUtils.getAvailableListenPort();
    String testString = "Hello world: ";
    String scope = "Scope1";
    @Cleanup ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
    serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    @Cleanup MockStreamManager streamManager = new MockStreamManager(scope, endpoint, port);
    MockClientFactory clientFactory = streamManager.getClientFactory();
    ReaderGroupConfig groupConfig = ReaderGroupConfig.builder().automaticCheckpointIntervalMillis(1000).stream(Stream.of(scope, streamName)).build();
    streamManager.createScope(scope);
    streamManager.createStream(scope, streamName, null);
    streamManager.createReaderGroup(readerGroup, groupConfig);
    JavaSerializer<String> serializer = new JavaSerializer<>();
    populateEvents(streamName, testString, clientFactory, serializer);
    AtomicLong fakeClock = new AtomicLong(0);
    @Cleanup EventStreamReader<String> reader1 = clientFactory.createReader("reader1", readerGroup, serializer, ReaderConfig.builder().build(), () -> fakeClock.get(), () -> fakeClock.get() / NANOS_PER_SECOND);
    @Cleanup EventStreamReader<String> reader2 = clientFactory.createReader("reader2", readerGroup, serializer, ReaderConfig.builder().build(), () -> fakeClock.get(), () -> fakeClock.get() / NANOS_PER_SECOND);
    int numRead = 0;
    int checkpointCount = 0;
    while (numRead < 100) {
        fakeClock.addAndGet(NANOS_PER_SECOND);
        EventRead<String> event = reader1.readNextEvent(1000);
        if (event.isCheckpoint()) {
            checkpointCount++;
        } else {
            String message = event.getEvent();
            assertEquals(testString + numRead, message);
            numRead++;
        }
    }
    assertEquals("As there is a second reader that does not pass the checkpoint, only one should occur", 1, checkpointCount);
}
Also used : ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockStreamManager(io.pravega.client.stream.mock.MockStreamManager) Test(org.junit.Test)

Example 12 with ServiceBuilder

use of io.pravega.segmentstore.server.store.ServiceBuilder in project pravega by pravega.

the class ControllerStreamMetadataTest method setUp.

@Before
public void setUp() throws Exception {
    final int controllerPort = TestUtils.getAvailableListenPort();
    final String serviceHost = "localhost";
    final int servicePort = TestUtils.getAvailableListenPort();
    final int containerCount = 4;
    try {
        // 1. Start ZK
        this.zkTestServer = new TestingServerStarter().start();
        // 2. Start Pravega service.
        ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
        serviceBuilder.initialize();
        StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
        this.server = new PravegaConnectionListener(false, servicePort, store);
        this.server.startListening();
        // 3. Start controller
        this.controllerWrapper = new ControllerWrapper(zkTestServer.getConnectString(), false, controllerPort, serviceHost, servicePort, containerCount);
        this.controllerWrapper.awaitRunning();
        this.controller = controllerWrapper.getController();
        this.streamConfiguration = StreamConfiguration.builder().scope(SCOPE).streamName(STREAM).scalingPolicy(ScalingPolicy.fixed(1)).build();
    } catch (Exception e) {
        log.error("Error during setup", e);
        throw e;
    }
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) TestingServerStarter(io.pravega.test.common.TestingServerStarter) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ControllerWrapper(io.pravega.test.integration.demo.ControllerWrapper) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) Before(org.junit.Before)

Example 13 with ServiceBuilder

use of io.pravega.segmentstore.server.store.ServiceBuilder in project pravega by pravega.

the class PravegaRequestProcessorTest method testTransaction.

@Test(timeout = 20000)
public void testTransaction() throws Exception {
    String streamSegmentName = "testTxn";
    UUID txnid = UUID.randomUUID();
    @Cleanup ServiceBuilder serviceBuilder = newInlineExecutionInMemoryBuilder(getBuilderConfig());
    serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    ServerConnection connection = mock(ServerConnection.class);
    InOrder order = inOrder(connection);
    PravegaRequestProcessor processor = new PravegaRequestProcessor(store, connection);
    processor.createSegment(new WireCommands.CreateSegment(0, streamSegmentName, WireCommands.CreateSegment.NO_SCALE, 0, ""));
    order.verify(connection).send(new WireCommands.SegmentCreated(0, streamSegmentName));
    processor.createTransaction(new WireCommands.CreateTransaction(1, streamSegmentName, txnid, ""));
    assertTrue(append(StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid), 1, store));
    processor.getTransactionInfo(new WireCommands.GetTransactionInfo(2, streamSegmentName, txnid, ""));
    assertTrue(append(StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid), 2, store));
    order.verify(connection).send(new WireCommands.TransactionCreated(1, streamSegmentName, txnid));
    order.verify(connection).send(Mockito.argThat(t -> {
        return t instanceof TransactionInfo && ((TransactionInfo) t).exists();
    }));
    processor.commitTransaction(new WireCommands.CommitTransaction(3, streamSegmentName, txnid, ""));
    order.verify(connection).send(new WireCommands.TransactionCommitted(3, streamSegmentName, txnid));
    processor.getTransactionInfo(new WireCommands.GetTransactionInfo(4, streamSegmentName, txnid, ""));
    order.verify(connection).send(new WireCommands.NoSuchSegment(4, StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid)));
    txnid = UUID.randomUUID();
    processor.createTransaction(new WireCommands.CreateTransaction(1, streamSegmentName, txnid, ""));
    assertTrue(append(StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid), 1, store));
    order.verify(connection).send(new WireCommands.TransactionCreated(1, streamSegmentName, txnid));
    processor.getTransactionInfo(new WireCommands.GetTransactionInfo(2, streamSegmentName, txnid, ""));
    order.verify(connection).send(Mockito.argThat(t -> {
        return t instanceof TransactionInfo && ((TransactionInfo) t).exists();
    }));
    processor.abortTransaction(new WireCommands.AbortTransaction(3, streamSegmentName, txnid, ""));
    order.verify(connection).send(new WireCommands.TransactionAborted(3, streamSegmentName, txnid));
    processor.getTransactionInfo(new WireCommands.GetTransactionInfo(4, streamSegmentName, txnid, ""));
    order.verify(connection).send(new WireCommands.NoSuchSegment(4, StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid)));
    // Verify the case when the transaction segment is already sealed. This simulates the case when the process
    // crashed after sealing, but before issuing the merge.
    txnid = UUID.randomUUID();
    processor.createTransaction(new WireCommands.CreateTransaction(1, streamSegmentName, txnid, ""));
    assertTrue(append(StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid), 1, store));
    processor.getTransactionInfo(new WireCommands.GetTransactionInfo(2, streamSegmentName, txnid, ""));
    assertTrue(append(StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid), 2, store));
    // Seal the transaction in the SegmentStore.
    String txnName = StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid);
    store.sealStreamSegment(txnName, Duration.ZERO).join();
    processor.commitTransaction(new WireCommands.CommitTransaction(3, streamSegmentName, txnid, ""));
    order.verify(connection).send(new WireCommands.TransactionCommitted(3, streamSegmentName, txnid));
    processor.getTransactionInfo(new WireCommands.GetTransactionInfo(4, streamSegmentName, txnid, ""));
    order.verify(connection).send(new WireCommands.NoSuchSegment(4, StreamSegmentNameUtils.getTransactionNameFromId(streamSegmentName, txnid)));
    order.verifyNoMoreInteractions();
}
Also used : ReadResultEntryBase(io.pravega.segmentstore.server.reading.ReadResultEntryBase) StreamSegmentInformation(io.pravega.segmentstore.contracts.StreamSegmentInformation) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) AssertExtensions(io.pravega.test.common.AssertExtensions) Cleanup(lombok.Cleanup) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) ByteBuffer(java.nio.ByteBuffer) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ReadResultEntryContents(io.pravega.segmentstore.contracts.ReadResultEntryContents) Duration(java.time.Duration) Mockito.doReturn(org.mockito.Mockito.doReturn) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ReadResultEntryType(io.pravega.segmentstore.contracts.ReadResultEntryType) UUID(java.util.UUID) OpStatsData(io.pravega.shared.metrics.OpStatsData) StreamSegmentNameUtils(io.pravega.shared.segment.StreamSegmentNameUtils) MetricsProvider(io.pravega.shared.metrics.MetricsProvider) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.inOrder(org.mockito.Mockito.inOrder) TestUtils(io.pravega.test.common.TestUtils) Futures(io.pravega.common.concurrent.Futures) ReadResult(io.pravega.segmentstore.contracts.ReadResult) StreamSegmentService(io.pravega.segmentstore.server.store.StreamSegmentService) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ServiceConfig(io.pravega.segmentstore.server.store.ServiceConfig) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) ReadResultEntry(io.pravega.segmentstore.contracts.ReadResultEntry) Properties(java.util.Properties) InOrder(org.mockito.InOrder) lombok.val(lombok.val) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StreamSegmentMergedException(io.pravega.segmentstore.contracts.StreamSegmentMergedException) Mockito.when(org.mockito.Mockito.when) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) SynchronousStreamSegmentStore(io.pravega.segmentstore.server.mocks.SynchronousStreamSegmentStore) InlineExecutor(io.pravega.test.common.InlineExecutor) Data(lombok.Data) MetricsConfig(io.pravega.shared.metrics.MetricsConfig) Preconditions(com.google.common.base.Preconditions) TransactionInfo(io.pravega.shared.protocol.netty.WireCommands.TransactionInfo) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InOrder(org.mockito.InOrder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) SynchronousStreamSegmentStore(io.pravega.segmentstore.server.mocks.SynchronousStreamSegmentStore) TransactionInfo(io.pravega.shared.protocol.netty.WireCommands.TransactionInfo) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 14 with ServiceBuilder

use of io.pravega.segmentstore.server.store.ServiceBuilder in project pravega by pravega.

the class PravegaRequestProcessorTest method testCreateSegment.

@Test(timeout = 20000)
public void testCreateSegment() throws Exception {
    // Set up PravegaRequestProcessor instance to execute requests against
    String streamSegmentName = "testCreateSegment";
    @Cleanup ServiceBuilder serviceBuilder = newInlineExecutionInMemoryBuilder(getBuilderConfig());
    serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    ServerConnection connection = mock(ServerConnection.class);
    InOrder order = inOrder(connection);
    PravegaRequestProcessor processor = new PravegaRequestProcessor(store, connection);
    // Execute and Verify createSegment/getStreamSegmentInfo calling stack is executed as design.
    processor.createSegment(new WireCommands.CreateSegment(1, streamSegmentName, WireCommands.CreateSegment.NO_SCALE, 0, ""));
    assertTrue(append(streamSegmentName, 1, store));
    processor.getStreamSegmentInfo(new WireCommands.GetStreamSegmentInfo(1, streamSegmentName, ""));
    assertTrue(append(streamSegmentName, 2, store));
    order.verify(connection).send(new WireCommands.SegmentCreated(1, streamSegmentName));
    order.verify(connection).send(Mockito.any(WireCommands.StreamSegmentInfo.class));
    // TestCreateSealDelete may executed before this test case,
    // so createSegmentStats may record 1 or 2 createSegment operation here.
    OpStatsData createSegmentStats = processor.getCreateStreamSegment().toOpStatsData();
    assertNotEquals(0, createSegmentStats.getNumSuccessfulEvents());
    assertEquals(0, createSegmentStats.getNumFailedEvents());
}
Also used : InOrder(org.mockito.InOrder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) SynchronousStreamSegmentStore(io.pravega.segmentstore.server.mocks.SynchronousStreamSegmentStore) OpStatsData(io.pravega.shared.metrics.OpStatsData) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 15 with ServiceBuilder

use of io.pravega.segmentstore.server.store.ServiceBuilder in project pravega by pravega.

the class EndToEndAutoScaleUpTest method main.

public static void main(String[] args) throws Exception {
    try {
        @Cleanup TestingServer zkTestServer = new TestingServerStarter().start();
        int port = Config.SERVICE_PORT;
        @Cleanup ControllerWrapper controllerWrapper = new ControllerWrapper(zkTestServer.getConnectString(), port, false);
        Controller controller = controllerWrapper.getController();
        ClientFactory internalCF = new ClientFactoryImpl(NameUtils.INTERNAL_SCOPE_NAME, controller, new ConnectionFactoryImpl(ClientConfig.builder().build()));
        ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
        serviceBuilder.initialize();
        StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
        @Cleanup SegmentStatsFactory segmentStatsFactory = new SegmentStatsFactory();
        SegmentStatsRecorder statsRecorder = segmentStatsFactory.createSegmentStatsRecorder(store, internalCF, AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).build());
        @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, "localhost", 12345, store, statsRecorder, null, null, null);
        server.startListening();
        controllerWrapper.awaitRunning();
        controllerWrapper.getControllerService().createScope("test").get();
        controller.createStream(CONFIG).get();
        @Cleanup MockClientFactory clientFactory = new MockClientFactory("test", controller);
        // Mocking pravega service by putting scale up and scale down requests for the stream
        EventStreamWriter<String> test = clientFactory.createEventWriter("test", new JavaSerializer<>(), EventWriterConfig.builder().build());
        // keep writing. Scale should happen
        long start = System.currentTimeMillis();
        char[] chars = new char[1];
        Arrays.fill(chars, 'a');
        String str = new String(chars);
        CompletableFuture.runAsync(() -> {
            while (System.currentTimeMillis() - start < Duration.ofMinutes(3).toMillis()) {
                try {
                    test.writeEvent("0", str).get();
                } catch (Throwable e) {
                    System.err.println("test exception writing events " + e.getMessage());
                    break;
                }
            }
        });
        Retry.withExpBackoff(10, 10, 100, 10000).retryingOn(NotDoneException.class).throwingOn(RuntimeException.class).runAsync(() -> controller.getCurrentSegments("test", "test").thenAccept(streamSegments -> {
            if (streamSegments.getSegments().size() > 3) {
                System.err.println("Success");
                log.info("Success");
                System.exit(0);
            } else {
                throw new NotDoneException();
            }
        }), Executors.newSingleThreadScheduledExecutor()).exceptionally(e -> {
            System.err.println("Failure");
            log.error("Failure");
            System.exit(1);
            return null;
        }).get();
    } catch (Throwable e) {
        System.err.print("Test failed with exception: " + e.getMessage());
        System.exit(-1);
    }
    System.exit(0);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) SegmentStatsFactory(io.pravega.segmentstore.server.host.stat.SegmentStatsFactory) Arrays(java.util.Arrays) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) Retry(io.pravega.common.util.Retry) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) TestingServerStarter(io.pravega.test.common.TestingServerStarter) Duration(java.time.Duration) SegmentStatsRecorder(io.pravega.segmentstore.server.host.stat.SegmentStatsRecorder) TestingServer(org.apache.curator.test.TestingServer) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) AutoScalerConfig(io.pravega.segmentstore.server.host.stat.AutoScalerConfig) NameUtils(io.pravega.shared.NameUtils) Executors(java.util.concurrent.Executors) Slf4j(lombok.extern.slf4j.Slf4j) Config(io.pravega.controller.util.Config) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) ClientFactory(io.pravega.client.ClientFactory) Controller(io.pravega.client.stream.impl.Controller) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) ClientConfig(io.pravega.client.ClientConfig) SegmentStatsRecorder(io.pravega.segmentstore.server.host.stat.SegmentStatsRecorder) TestingServerStarter(io.pravega.test.common.TestingServerStarter) ClientFactory(io.pravega.client.ClientFactory) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) SegmentStatsFactory(io.pravega.segmentstore.server.host.stat.SegmentStatsFactory) Controller(io.pravega.client.stream.impl.Controller) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl)

Aggregations

ServiceBuilder (io.pravega.segmentstore.server.store.ServiceBuilder)26 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)25 Cleanup (lombok.Cleanup)20 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)19 Test (org.junit.Test)15 TestingServerStarter (io.pravega.test.common.TestingServerStarter)11 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)9 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)8 Controller (io.pravega.client.stream.impl.Controller)8 MockStreamManager (io.pravega.client.stream.mock.MockStreamManager)7 TestingServer (org.apache.curator.test.TestingServer)7 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)6 SynchronousStreamSegmentStore (io.pravega.segmentstore.server.mocks.SynchronousStreamSegmentStore)6 WireCommands (io.pravega.shared.protocol.netty.WireCommands)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 InOrder (org.mockito.InOrder)6 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)5 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)5 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)5 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)5