Search in sources :

Example 1 with Transaction

use of io.pravega.client.stream.Transaction in project pravega by pravega.

the class EndToEndAutoScaleUpWithTxnTest 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);
        Controller controller = controllerWrapper.getController();
        controllerWrapper.getControllerService().createScope(NameUtils.INTERNAL_SCOPE_NAME).get();
        @Cleanup ConnectionFactory connectionFactory = new ConnectionFactoryImpl(ClientConfig.builder().build());
        @Cleanup ClientFactory internalCF = new ClientFactoryImpl(NameUtils.INTERNAL_SCOPE_NAME, controller, connectionFactory);
        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
        EventWriterConfig writerConfig = EventWriterConfig.builder().transactionTimeoutTime(30000).transactionTimeoutScaleGracePeriod(30000).build();
        EventStreamWriter<String> test = clientFactory.createEventWriter("test", new JavaSerializer<>(), writerConfig);
        // region Successful commit tests
        Transaction<String> txn1 = test.beginTxn();
        txn1.writeEvent("1");
        txn1.flush();
        Map<Double, Double> map = new HashMap<>();
        map.put(0.0, 1.0 / 3.0);
        map.put(1.0 / 3.0, 2.0 / 3.0);
        map.put(2.0 / 3.0, 1.0);
        Stream stream = new StreamImpl("test", "test");
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        controller.scaleStream(stream, Collections.singletonList(0), map, executor).getFuture().get();
        Transaction<String> txn2 = test.beginTxn();
        txn2.writeEvent("2");
        txn2.flush();
        txn2.commit();
        txn1.commit();
        Thread.sleep(1000);
        @Cleanup ReaderGroupManager readerGroupManager = new ReaderGroupManagerImpl("test", controller, clientFactory, connectionFactory);
        readerGroupManager.createReaderGroup("readergrp", ReaderGroupConfig.builder().stream("test").build());
        final EventStreamReader<String> reader = clientFactory.createReader("1", "readergrp", new JavaSerializer<>(), ReaderConfig.builder().build());
        String event1 = reader.readNextEvent(SECONDS.toMillis(60)).getEvent();
        String event2 = reader.readNextEvent(SECONDS.toMillis(60)).getEvent();
        assert event1.equals("1");
        assert event2.equals("2");
        final AtomicBoolean done = new AtomicBoolean(false);
        startWriter(test, done);
        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());
        log.error("Test failed with exception: {}", e);
        System.exit(-1);
    }
    System.exit(0);
}
Also used : SegmentStatsFactory(io.pravega.segmentstore.server.host.stat.SegmentStatsFactory) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) StreamImpl(io.pravega.client.stream.impl.StreamImpl) Retry(io.pravega.common.util.Retry) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cleanup(lombok.Cleanup) HashMap(java.util.HashMap) 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) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) TestingServerStarter(io.pravega.test.common.TestingServerStarter) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) SegmentStatsRecorder(io.pravega.segmentstore.server.host.stat.SegmentStatsRecorder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TestingServer(org.apache.curator.test.TestingServer) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) Transaction(io.pravega.client.stream.Transaction) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) 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) EventStreamReader(io.pravega.client.stream.EventStreamReader) 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) ReaderConfig(io.pravega.client.stream.ReaderConfig) Controller(io.pravega.client.stream.impl.Controller) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ClientConfig(io.pravega.client.ClientConfig) SegmentStatsRecorder(io.pravega.segmentstore.server.host.stat.SegmentStatsRecorder) HashMap(java.util.HashMap) ClientFactory(io.pravega.client.ClientFactory) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) SegmentStatsFactory(io.pravega.segmentstore.server.host.stat.SegmentStatsFactory) 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) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Stream(io.pravega.client.stream.Stream) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) TestingServer(org.apache.curator.test.TestingServer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) TestingServerStarter(io.pravega.test.common.TestingServerStarter) Controller(io.pravega.client.stream.impl.Controller) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) StreamImpl(io.pravega.client.stream.impl.StreamImpl) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl)

Example 2 with Transaction

use of io.pravega.client.stream.Transaction in project pravega by pravega.

the class EndToEndTransactionTest method main.

@Test
public static void main(String[] args) throws Exception {
    @Cleanup TestingServer zkTestServer = new TestingServerStarter().start();
    ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
    serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    int port = Config.SERVICE_PORT;
    @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, port, store);
    server.startListening();
    Thread.sleep(1000);
    @Cleanup ControllerWrapper controllerWrapper = new ControllerWrapper(zkTestServer.getConnectString(), port);
    Controller controller = controllerWrapper.getController();
    controllerWrapper.awaitRunning();
    final String testScope = "testScope";
    final String testStream = "testStream";
    if (!controller.createScope(testScope).get()) {
        log.error("FAILURE: Error creating test scope");
        return;
    }
    ScalingPolicy policy = ScalingPolicy.fixed(5);
    StreamConfiguration streamConfig = StreamConfiguration.builder().scope(testScope).streamName(testStream).scalingPolicy(policy).build();
    if (!controller.createStream(streamConfig).get()) {
        log.error("FAILURE: Error creating test stream");
        return;
    }
    final long txnTimeout = 4000;
    @Cleanup MockClientFactory clientFactory = new MockClientFactory(testScope, controller);
    @Cleanup EventStreamWriter<String> producer = clientFactory.createEventWriter(testStream, new JavaSerializer<>(), EventWriterConfig.builder().transactionTimeoutTime(txnTimeout).transactionTimeoutScaleGracePeriod(txnTimeout).build());
    // region Successful commit tests
    Transaction<String> transaction = producer.beginTxn();
    for (int i = 0; i < 1; i++) {
        String event = "\n Transactional Publish \n";
        log.info("Producing event: " + event);
        transaction.writeEvent("", event);
        transaction.flush();
        Thread.sleep(500);
    }
    CompletableFuture<Object> commit = CompletableFuture.supplyAsync(() -> {
        try {
            transaction.commit();
        } catch (Exception e) {
            log.warn("Error committing transaction", e);
        }
        return null;
    });
    commit.join();
    Transaction.Status txnStatus = transaction.checkStatus();
    assertTrue(txnStatus == Transaction.Status.COMMITTING || txnStatus == Transaction.Status.COMMITTED);
    log.info("SUCCESS: successful in committing transaction. Transaction status=" + txnStatus);
    Thread.sleep(2000);
    txnStatus = transaction.checkStatus();
    assertTrue(txnStatus == Transaction.Status.COMMITTED);
    log.info("SUCCESS: successfully committed transaction. Transaction status=" + txnStatus);
    // endregion
    // region Successful abort tests
    Transaction<String> transaction2 = producer.beginTxn();
    for (int i = 0; i < 1; i++) {
        String event = "\n Transactional Publish \n";
        log.info("Producing event: " + event);
        transaction2.writeEvent("", event);
        transaction2.flush();
        Thread.sleep(500);
    }
    CompletableFuture<Object> drop = CompletableFuture.supplyAsync(() -> {
        try {
            transaction2.abort();
        } catch (Exception e) {
            log.warn("Error aborting transaction", e);
        }
        return null;
    });
    drop.join();
    Transaction.Status txn2Status = transaction2.checkStatus();
    assertTrue(txn2Status == Transaction.Status.ABORTING || txn2Status == Transaction.Status.ABORTED);
    log.info("SUCCESS: successful in dropping transaction. Transaction status=" + txn2Status);
    Thread.sleep(2000);
    txn2Status = transaction2.checkStatus();
    assertTrue(txn2Status == Transaction.Status.ABORTED);
    log.info("SUCCESS: successfully aborted transaction. Transaction status=" + txn2Status);
    // endregion
    // region Successful timeout tests
    Transaction<String> tx1 = producer.beginTxn();
    Thread.sleep((long) (1.3 * txnTimeout));
    Transaction.Status txStatus = tx1.checkStatus();
    Assert.assertTrue(Transaction.Status.ABORTING == txStatus || Transaction.Status.ABORTED == txStatus);
    log.info("SUCCESS: successfully aborted transaction after timeout. Transaction status=" + txStatus);
    // endregion
    // region Ping failure due to controller going into disconnection state
    // Fill in these tests once we have controller.stop() implemented.
    System.exit(0);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) TestingServerStarter(io.pravega.test.common.TestingServerStarter) 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) Transaction(io.pravega.client.stream.Transaction) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Test(org.junit.Test)

Example 3 with Transaction

use of io.pravega.client.stream.Transaction in project pravega by pravega.

the class ControllerFailoverTest method failoverTest.

@Test(timeout = 180000)
public void failoverTest() throws InterruptedException, ExecutionException {
    String scope = "testFailoverScope" + RandomStringUtils.randomAlphabetic(5);
    String stream = "testFailoverStream" + RandomStringUtils.randomAlphabetic(5);
    int initialSegments = 2;
    List<Integer> segmentsToSeal = Collections.singletonList(0);
    Map<Double, Double> newRangesToCreate = new HashMap<>();
    newRangesToCreate.put(0.0, 0.25);
    newRangesToCreate.put(0.25, 0.5);
    long lease = 29000;
    long maxExecutionTime = 60000;
    long scaleGracePeriod = 30000;
    // Connect with first controller instance.
    final Controller controller1 = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(controllerURIDirect).build()).build(), EXECUTOR_SERVICE);
    // Create scope, stream, and a transaction with high timeout value.
    controller1.createScope(scope).join();
    log.info("Scope {} created successfully", scope);
    createStream(controller1, scope, stream, ScalingPolicy.fixed(initialSegments));
    log.info("Stream {}/{} created successfully", scope, stream);
    long txnCreationTimestamp = System.nanoTime();
    StreamImpl stream1 = new StreamImpl(scope, stream);
    TxnSegments txnSegments = controller1.createTransaction(stream1, lease, scaleGracePeriod).join();
    log.info("Transaction {} created successfully, beginTime={}", txnSegments.getTxnId(), txnCreationTimestamp);
    // Initiate scale operation. It will block until ongoing transaction is complete.
    controller1.startScale(stream1, segmentsToSeal, newRangesToCreate).join();
    // Ensure that scale is not yet done.
    boolean scaleStatus = controller1.checkScaleStatus(stream1, 0).join();
    log.info("Status of scale operation isDone={}", scaleStatus);
    Assert.assertTrue(!scaleStatus);
    // Now stop the controller instance executing scale operation.
    Futures.getAndHandleExceptions(controllerService1.scaleService(1), ExecutionException::new);
    log.info("Successfully stopped one instance of controller service");
    List<URI> conUris = controllerService1.getServiceDetails();
    // Fetch all the RPC endpoints and construct the client URIs.
    final List<String> uris = conUris.stream().filter(uri -> Utils.DOCKER_BASED ? uri.getPort() == Utils.DOCKER_CONTROLLER_PORT : uri.getPort() == Utils.MARATHON_CONTROLLER_PORT).map(URI::getAuthority).collect(Collectors.toList());
    controllerURIDirect = URI.create("tcp://" + String.join(",", uris));
    log.info("Controller Service direct URI: {}", controllerURIDirect);
    // Connect to another controller instance.
    final Controller controller2 = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(controllerURIDirect).build()).build(), EXECUTOR_SERVICE);
    // Fetch status of transaction.
    log.info("Fetching status of transaction {}, time elapsed since its creation={}", txnSegments.getTxnId(), System.nanoTime() - txnCreationTimestamp);
    Transaction.Status status = controller2.checkTransactionStatus(stream1, txnSegments.getTxnId()).join();
    log.info("Transaction {} status={}", txnSegments.getTxnId(), status);
    if (status == Transaction.Status.OPEN) {
        // Abort the ongoing transaction.
        log.info("Trying to abort transaction {}, by sending request to controller at {}", txnSegments.getTxnId(), controllerURIDirect);
        controller2.abortTransaction(stream1, txnSegments.getTxnId()).join();
    }
    // Note: if scale does not complete within desired time, test will timeout.
    while (!scaleStatus) {
        scaleStatus = controller2.checkScaleStatus(stream1, 0).join();
        Thread.sleep(30000);
    }
    // Ensure that the stream has 3 segments now.
    log.info("Checking whether scale operation succeeded by fetching current segments");
    StreamSegments streamSegments = controller2.getCurrentSegments(scope, stream).join();
    log.info("Current segment count=", streamSegments.getSegments().size());
    Assert.assertEquals(initialSegments - segmentsToSeal.size() + newRangesToCreate.size(), streamSegments.getSegments().size());
}
Also used : TxnSegments(io.pravega.client.stream.impl.TxnSegments) HashMap(java.util.HashMap) ControllerImpl(io.pravega.client.stream.impl.ControllerImpl) Controller(io.pravega.client.stream.impl.Controller) URI(java.net.URI) Transaction(io.pravega.client.stream.Transaction) StreamImpl(io.pravega.client.stream.impl.StreamImpl) ExecutionException(java.util.concurrent.ExecutionException) StreamSegments(io.pravega.client.stream.impl.StreamSegments) Test(org.junit.Test)

Aggregations

Transaction (io.pravega.client.stream.Transaction)3 Controller (io.pravega.client.stream.impl.Controller)3 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)2 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)2 StreamImpl (io.pravega.client.stream.impl.StreamImpl)2 MockClientFactory (io.pravega.client.stream.mock.MockClientFactory)2 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)2 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)2 ServiceBuilder (io.pravega.segmentstore.server.store.ServiceBuilder)2 TestingServerStarter (io.pravega.test.common.TestingServerStarter)2 HashMap (java.util.HashMap)2 Cleanup (lombok.Cleanup)2 Test (org.junit.Test)2 ClientConfig (io.pravega.client.ClientConfig)1 ClientFactory (io.pravega.client.ClientFactory)1 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)1 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)1 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)1 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)1 EventStreamReader (io.pravega.client.stream.EventStreamReader)1