Search in sources :

Example 1 with ConnectionFactory

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

the class KeyValueTableFactory method withScope.

/**
 * Creates a new instance of {@link KeyValueTableFactory}.
 *
 * @param scope  The Key-Value Table scope.
 * @param config Configuration for the client.
 * @return Instance of {@link KeyValueTableFactory} implementation.
 */
static KeyValueTableFactory withScope(String scope, ClientConfig config) {
    ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(config);
    ConnectionPool connectionPool = new ConnectionPoolImpl(config, connectionFactory);
    Controller controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(config).build(), connectionFactory.getInternalExecutor());
    return new KeyValueTableFactoryImpl(scope, controller, connectionPool);
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) KeyValueTableFactoryImpl(io.pravega.client.tables.impl.KeyValueTableFactoryImpl) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Controller(io.pravega.client.control.impl.Controller)

Example 2 with ConnectionFactory

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

the class ControllerServiceStarter method startUp.

@Override
protected void startUp() {
    long traceId = LoggerHelpers.traceEnterWithContext(log, this.objectId, "startUp");
    log.info("Initiating controller service startUp");
    log.info("Controller serviceConfig = {}", serviceConfig.toString());
    log.info("Event processors enabled = {}", serviceConfig.getEventProcessorConfig().isPresent());
    log.info("Cluster listener enabled = {}", serviceConfig.isControllerClusterListenerEnabled());
    log.info("    Host monitor enabled = {}", serviceConfig.getHostMonitorConfig().isHostMonitorEnabled());
    log.info("     gRPC server enabled = {}", serviceConfig.getGRPCServerConfig().isPresent());
    log.info("     REST server enabled = {}", serviceConfig.getRestServerConfig().isPresent());
    final BucketStore bucketStore;
    final TaskMetadataStore taskMetadataStore;
    final HostControllerStore hostStore;
    final CheckpointStore checkpointStore;
    try {
        // Initialize the executor service.
        controllerExecutor = ExecutorServiceHelpers.newScheduledThreadPool(serviceConfig.getThreadPoolSize(), "controllerpool");
        eventExecutor = ExecutorServiceHelpers.newScheduledThreadPool(serviceConfig.getThreadPoolSize(), "eventprocessor");
        retentionExecutor = ExecutorServiceHelpers.newScheduledThreadPool(Config.RETENTION_THREAD_POOL_SIZE, "retentionpool");
        watermarkingExecutor = ExecutorServiceHelpers.newScheduledThreadPool(Config.WATERMARKING_THREAD_POOL_SIZE, "watermarkingpool");
        bucketStore = StreamStoreFactory.createBucketStore(storeClient, controllerExecutor);
        log.info("Created the bucket store.");
        taskMetadataStore = TaskStoreFactory.createStore(storeClient, controllerExecutor);
        log.info("Created the task store.");
        hostStore = HostStoreFactory.createStore(serviceConfig.getHostMonitorConfig(), storeClient);
        log.info("Created the host store.");
        checkpointStore = CheckpointStoreFactory.create(storeClient);
        log.info("Created the checkpoint store.");
        // Initialize Stream and Transaction metrics.
        StreamMetrics.initialize();
        TransactionMetrics.initialize();
        // On each controller process restart, we use a fresh hostId,
        // which is a combination of hostname and random GUID.
        String hostName = getHostName();
        Host host = new Host(hostName, getPort(), UUID.randomUUID().toString());
        // Create a RequestTracker instance to trace client requests end-to-end.
        GRPCServerConfig grpcServerConfig = serviceConfig.getGRPCServerConfig().get();
        RequestTracker requestTracker = new RequestTracker(grpcServerConfig.isRequestTracingEnabled());
        // Create a Health Service Manager instance.
        healthServiceManager = new HealthServiceManager(serviceConfig.getHealthCheckFrequency());
        if (serviceConfig.getHostMonitorConfig().isHostMonitorEnabled()) {
            // Start the Segment Container Monitor.
            monitor = new SegmentContainerMonitor(hostStore, (CuratorFramework) storeClient.getClient(), new UniformContainerBalancer(), serviceConfig.getHostMonitorConfig().getHostMonitorMinRebalanceInterval());
            monitor.startAsync();
            log.info("Started Segment Container Monitor service.");
            SegmentContainerMonitorHealthContributor segmentContainerMonitorHC = new SegmentContainerMonitorHealthContributor("segmentContainerMonitor", monitor);
            healthServiceManager.register(segmentContainerMonitorHC);
        }
        // This client config is used by the segment store helper (SegmentHelper) to connect to the segment store.
        ClientConfig.ClientConfigBuilder clientConfigBuilder = ClientConfig.builder().controllerURI(URI.create((grpcServerConfig.isTlsEnabled() ? "tls://" : "tcp://") + "localhost:" + grpcServerConfig.getPort())).trustStore(grpcServerConfig.getTlsTrustStore()).validateHostName(false);
        Optional<Boolean> tlsEnabledForSegmentStore = BooleanUtils.extract(serviceConfig.getTlsEnabledForSegmentStore());
        if (tlsEnabledForSegmentStore.isPresent()) {
            clientConfigBuilder.enableTlsToSegmentStore(tlsEnabledForSegmentStore.get());
        }
        // Use one connection per Segment Store to save up resources.
        ClientConfig clientConfig = clientConfigBuilder.maxConnectionsPerSegmentStore(1).build();
        connectionFactory = connectionFactoryRef.orElseGet(() -> new SocketConnectionFactoryImpl(clientConfig));
        connectionPool = new ConnectionPoolImpl(clientConfig, connectionFactory);
        segmentHelper = segmentHelperRef.orElseGet(() -> new SegmentHelper(connectionPool, hostStore, controllerExecutor));
        GrpcAuthHelper authHelper = new GrpcAuthHelper(serviceConfig.getGRPCServerConfig().get().isAuthorizationEnabled(), grpcServerConfig.getTokenSigningKey(), grpcServerConfig.getAccessTokenTTLInSeconds());
        streamStore = streamMetadataStoreRef.orElseGet(() -> StreamStoreFactory.createStore(storeClient, segmentHelper, authHelper, controllerExecutor));
        log.info("Created the stream store.");
        streamMetadataTasks = new StreamMetadataTasks(streamStore, bucketStore, taskMetadataStore, segmentHelper, controllerExecutor, eventExecutor, host.getHostId(), authHelper, serviceConfig.getRetentionFrequency().toMillis());
        streamTransactionMetadataTasks = new StreamTransactionMetadataTasks(streamStore, segmentHelper, controllerExecutor, eventExecutor, host.getHostId(), serviceConfig.getTimeoutServiceConfig(), authHelper);
        BucketServiceFactory bucketServiceFactory = new BucketServiceFactory(host.getHostId(), bucketStore, 1000);
        Duration executionDurationRetention = serviceConfig.getRetentionFrequency();
        PeriodicRetention retentionWork = new PeriodicRetention(streamStore, streamMetadataTasks, retentionExecutor, requestTracker);
        retentionService = bucketServiceFactory.createRetentionService(executionDurationRetention, retentionWork::retention, retentionExecutor);
        retentionService.startAsync();
        retentionService.awaitRunning();
        log.info("Started background periodic service for Retention.");
        RetentionServiceHealthContributor retentionServiceHC = new RetentionServiceHealthContributor("retentionService", retentionService);
        healthServiceManager.register(retentionServiceHC);
        Duration executionDurationWatermarking = Duration.ofSeconds(Config.MINIMUM_WATERMARKING_FREQUENCY_IN_SECONDS);
        watermarkingWork = new PeriodicWatermarking(streamStore, bucketStore, clientConfig, watermarkingExecutor, requestTracker);
        watermarkingService = bucketServiceFactory.createWatermarkingService(executionDurationWatermarking, watermarkingWork::watermark, watermarkingExecutor);
        watermarkingService.startAsync();
        watermarkingService.awaitRunning();
        log.info("Started background periodic service for Watermarking.");
        WatermarkingServiceHealthContributor watermarkingServiceHC = new WatermarkingServiceHealthContributor("watermarkingService", watermarkingService);
        healthServiceManager.register(watermarkingServiceHC);
        // Controller has a mechanism to track the currently active controller host instances. On detecting a failure of
        // any controller instance, the failure detector stores the failed HostId in a failed hosts directory (FH), and
        // invokes the taskSweeper.sweepOrphanedTasks for each failed host. When all resources under the failed hostId
        // are processed and deleted, that failed HostId is removed from FH folder.
        // Moreover, on controller process startup, it detects any hostIds not in the currently active set of
        // controllers and starts sweeping tasks orphaned by those hostIds.
        TaskSweeper taskSweeper = new TaskSweeper(taskMetadataStore, host.getHostId(), controllerExecutor, streamMetadataTasks);
        TxnSweeper txnSweeper = new TxnSweeper(streamStore, streamTransactionMetadataTasks, serviceConfig.getTimeoutServiceConfig().getMaxLeaseValue(), controllerExecutor);
        RequestSweeper requestSweeper = new RequestSweeper(streamStore, controllerExecutor, streamMetadataTasks);
        if (serviceConfig.isControllerClusterListenerEnabled()) {
            cluster = new ClusterZKImpl((CuratorFramework) storeClient.getClient(), ClusterType.CONTROLLER);
        }
        kvtMetadataStore = kvtMetaStoreRef.orElseGet(() -> KVTableStoreFactory.createStore(storeClient, segmentHelper, authHelper, controllerExecutor, streamStore));
        kvtMetadataTasks = new TableMetadataTasks(kvtMetadataStore, segmentHelper, controllerExecutor, eventExecutor, host.getHostId(), authHelper);
        controllerService = new ControllerService(kvtMetadataStore, kvtMetadataTasks, streamStore, bucketStore, streamMetadataTasks, streamTransactionMetadataTasks, segmentHelper, controllerExecutor, cluster, requestTracker);
        // Setup event processors.
        setController(new LocalController(controllerService, grpcServerConfig.isAuthorizationEnabled(), grpcServerConfig.getTokenSigningKey()));
        CompletableFuture<Void> eventProcessorFuture = CompletableFuture.completedFuture(null);
        if (serviceConfig.getEventProcessorConfig().isPresent()) {
            // Create ControllerEventProcessor object.
            controllerEventProcessors = new ControllerEventProcessors(host.getHostId(), serviceConfig.getEventProcessorConfig().get(), localController, checkpointStore, streamStore, bucketStore, connectionPool, streamMetadataTasks, streamTransactionMetadataTasks, kvtMetadataStore, kvtMetadataTasks, eventExecutor);
            // Bootstrap and start it asynchronously.
            eventProcessorFuture = controllerEventProcessors.bootstrap(streamTransactionMetadataTasks, streamMetadataTasks, kvtMetadataTasks).thenAcceptAsync(x -> controllerEventProcessors.startAsync(), eventExecutor);
            EventProcessorHealthContributor eventProcessorHC = new EventProcessorHealthContributor("eventProcessor", controllerEventProcessors);
            healthServiceManager.register(eventProcessorHC);
        }
        // Setup and start controller cluster listener after all sweepers have been initialized.
        if (serviceConfig.isControllerClusterListenerEnabled()) {
            List<FailoverSweeper> failoverSweepers = new ArrayList<>();
            failoverSweepers.add(taskSweeper);
            failoverSweepers.add(txnSweeper);
            failoverSweepers.add(requestSweeper);
            if (serviceConfig.getEventProcessorConfig().isPresent()) {
                assert controllerEventProcessors != null;
                failoverSweepers.add(controllerEventProcessors);
            }
            controllerClusterListener = new ControllerClusterListener(host, cluster, controllerExecutor, failoverSweepers);
            controllerClusterListener.startAsync();
            ClusterListenerHealthContributor clusterListenerHC = new ClusterListenerHealthContributor("clusterListener", controllerClusterListener);
            healthServiceManager.register(clusterListenerHC);
        }
        // Start the Health Service.
        healthServiceManager.start();
        // Start RPC server.
        if (serviceConfig.getGRPCServerConfig().isPresent()) {
            grpcServer = new GRPCServer(controllerService, grpcServerConfig, requestTracker);
            grpcServer.startAsync();
            grpcServer.awaitRunning();
            GRPCServerHealthContributor grpcServerHC = new GRPCServerHealthContributor("GRPCServer", grpcServer);
            healthServiceManager.register(grpcServerHC);
        }
        // Start REST server.
        if (serviceConfig.getRestServerConfig().isPresent()) {
            List<Object> resources = new ArrayList<>();
            resources.add(new StreamMetadataResourceImpl(this.localController, controllerService, grpcServer.getAuthHandlerManager(), connectionFactory, clientConfig));
            resources.add(new HealthImpl(grpcServer.getAuthHandlerManager(), healthServiceManager.getEndpoint()));
            resources.add(new PingImpl());
            MetricsProvider.getMetricsProvider().prometheusResource().ifPresent(resources::add);
            restServer = new RESTServer(serviceConfig.getRestServerConfig().get(), Set.copyOf(resources));
            restServer.startAsync();
            restServer.awaitRunning();
        }
        // Wait for controller event processors to start.
        if (serviceConfig.getEventProcessorConfig().isPresent()) {
            // if store client has failed because of session expiration, there are two possibilities where
            // controllerEventProcessors.awaitRunning may be stuck forever -
            // 1. stream creation is retried indefinitely and cannot complete because of zk session expiration
            // 2. event writer after stream creation throws exception.
            // In both of above cases controllerEventProcessors.startAsync may not get called.
            CompletableFuture.anyOf(storeClientFailureFuture, eventProcessorFuture.thenAccept(x -> controllerEventProcessors.awaitRunning())).join();
        }
        // Wait for controller cluster listeners to start.
        if (serviceConfig.isControllerClusterListenerEnabled()) {
            controllerClusterListener.awaitRunning();
        }
    } catch (Exception e) {
        log.error("Failed trying to start controller services", e);
        throw e;
    } finally {
        LoggerHelpers.traceLeave(log, this.objectId, "startUp", traceId);
    }
}
Also used : GRPCServer(io.pravega.controller.server.rpc.grpc.GRPCServer) ControllerEventProcessors(io.pravega.controller.server.eventProcessor.ControllerEventProcessors) CheckpointStore(io.pravega.controller.store.checkpoint.CheckpointStore) StringUtils(org.apache.commons.lang3.StringUtils) InetAddress(java.net.InetAddress) Cluster(io.pravega.common.cluster.Cluster) LocalController(io.pravega.controller.server.eventProcessor.LocalController) HealthServiceManager(io.pravega.shared.health.HealthServiceManager) TaskMetadataStore(io.pravega.controller.store.task.TaskMetadataStore) TxnSweeper(io.pravega.controller.task.Stream.TxnSweeper) Duration(java.time.Duration) PeriodicRetention(io.pravega.controller.server.bucket.PeriodicRetention) WatermarkingServiceHealthContributor(io.pravega.controller.server.health.WatermarkingServiceHealthContributor) URI(java.net.URI) ClusterZKImpl(io.pravega.common.cluster.zkImpl.ClusterZKImpl) ControllerClusterListener(io.pravega.controller.fault.ControllerClusterListener) PeriodicWatermarking(io.pravega.controller.server.bucket.PeriodicWatermarking) RESTServer(io.pravega.shared.rest.RESTServer) Set(java.util.Set) RequestSweeper(io.pravega.controller.task.Stream.RequestSweeper) RequestTracker(io.pravega.common.tracing.RequestTracker) UUID(java.util.UUID) KVTableMetadataStore(io.pravega.controller.store.kvtable.KVTableMetadataStore) CountDownLatch(java.util.concurrent.CountDownLatch) MetricsProvider(io.pravega.shared.metrics.MetricsProvider) RetentionServiceHealthContributor(io.pravega.controller.server.health.RetentionServiceHealthContributor) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CuratorFramework(org.apache.curator.framework.CuratorFramework) ClusterType(io.pravega.common.cluster.ClusterType) Config(io.pravega.controller.util.Config) GRPCServerHealthContributor(io.pravega.controller.server.health.GRPCServerHealthContributor) Optional(java.util.Optional) StoreType(io.pravega.controller.store.client.StoreType) BucketServiceFactory(io.pravega.controller.server.bucket.BucketServiceFactory) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) GRPCServerConfig(io.pravega.controller.server.rpc.grpc.GRPCServerConfig) KVTableStoreFactory(io.pravega.controller.store.kvtable.KVTableStoreFactory) StreamMetrics(io.pravega.controller.metrics.StreamMetrics) StreamStoreFactory(io.pravega.controller.store.stream.StreamStoreFactory) TransactionMetrics(io.pravega.controller.metrics.TransactionMetrics) Getter(lombok.Getter) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) BooleanUtils(io.pravega.common.util.BooleanUtils) CheckpointStoreFactory(io.pravega.controller.store.checkpoint.CheckpointStoreFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) PingImpl(io.pravega.controller.server.rest.resources.PingImpl) EventProcessorHealthContributor(io.pravega.controller.server.health.EventProcessorHealthContributor) StoreClient(io.pravega.controller.store.client.StoreClient) ArrayList(java.util.ArrayList) BucketStore(io.pravega.controller.store.stream.BucketStore) UniformContainerBalancer(io.pravega.controller.fault.UniformContainerBalancer) AccessLevel(lombok.AccessLevel) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) BucketManager(io.pravega.controller.server.bucket.BucketManager) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Host(io.pravega.common.cluster.Host) LoggerHelpers(io.pravega.common.LoggerHelpers) Callbacks(io.pravega.common.function.Callbacks) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) TableMetadataTasks(io.pravega.controller.task.KeyValueTable.TableMetadataTasks) UnknownHostException(java.net.UnknownHostException) FailoverSweeper(io.pravega.controller.fault.FailoverSweeper) HostStoreFactory(io.pravega.controller.store.host.HostStoreFactory) TimeUnit(java.util.concurrent.TimeUnit) TaskStoreFactory(io.pravega.controller.store.task.TaskStoreFactory) SegmentContainerMonitorHealthContributor(io.pravega.controller.server.health.SegmentContainerMonitorHealthContributor) HealthImpl(io.pravega.shared.health.bindings.resources.HealthImpl) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) StreamTransactionMetadataTasks(io.pravega.controller.task.Stream.StreamTransactionMetadataTasks) TaskSweeper(io.pravega.controller.task.TaskSweeper) SegmentContainerMonitor(io.pravega.controller.fault.SegmentContainerMonitor) ClusterListenerHealthContributor(io.pravega.controller.server.health.ClusterListenerHealthContributor) StreamMetadataResourceImpl(io.pravega.controller.server.rest.resources.StreamMetadataResourceImpl) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) ClientConfig(io.pravega.client.ClientConfig) GRPCServerHealthContributor(io.pravega.controller.server.health.GRPCServerHealthContributor) ArrayList(java.util.ArrayList) TxnSweeper(io.pravega.controller.task.Stream.TxnSweeper) CuratorFramework(org.apache.curator.framework.CuratorFramework) BucketServiceFactory(io.pravega.controller.server.bucket.BucketServiceFactory) ControllerEventProcessors(io.pravega.controller.server.eventProcessor.ControllerEventProcessors) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) StreamTransactionMetadataTasks(io.pravega.controller.task.Stream.StreamTransactionMetadataTasks) TaskSweeper(io.pravega.controller.task.TaskSweeper) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) PingImpl(io.pravega.controller.server.rest.resources.PingImpl) UniformContainerBalancer(io.pravega.controller.fault.UniformContainerBalancer) HealthServiceManager(io.pravega.shared.health.HealthServiceManager) TaskMetadataStore(io.pravega.controller.store.task.TaskMetadataStore) SegmentContainerMonitorHealthContributor(io.pravega.controller.server.health.SegmentContainerMonitorHealthContributor) Duration(java.time.Duration) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) EventProcessorHealthContributor(io.pravega.controller.server.health.EventProcessorHealthContributor) ControllerClusterListener(io.pravega.controller.fault.ControllerClusterListener) RetentionServiceHealthContributor(io.pravega.controller.server.health.RetentionServiceHealthContributor) StreamMetadataResourceImpl(io.pravega.controller.server.rest.resources.StreamMetadataResourceImpl) ClusterZKImpl(io.pravega.common.cluster.zkImpl.ClusterZKImpl) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) GRPCServerConfig(io.pravega.controller.server.rpc.grpc.GRPCServerConfig) PeriodicRetention(io.pravega.controller.server.bucket.PeriodicRetention) RequestSweeper(io.pravega.controller.task.Stream.RequestSweeper) CheckpointStore(io.pravega.controller.store.checkpoint.CheckpointStore) RequestTracker(io.pravega.common.tracing.RequestTracker) SegmentContainerMonitor(io.pravega.controller.fault.SegmentContainerMonitor) FailoverSweeper(io.pravega.controller.fault.FailoverSweeper) GRPCServer(io.pravega.controller.server.rpc.grpc.GRPCServer) LocalController(io.pravega.controller.server.eventProcessor.LocalController) HealthImpl(io.pravega.shared.health.bindings.resources.HealthImpl) TableMetadataTasks(io.pravega.controller.task.KeyValueTable.TableMetadataTasks) RESTServer(io.pravega.shared.rest.RESTServer) BucketStore(io.pravega.controller.store.stream.BucketStore) ClientConfig(io.pravega.client.ClientConfig) ClusterListenerHealthContributor(io.pravega.controller.server.health.ClusterListenerHealthContributor) Host(io.pravega.common.cluster.Host) UnknownHostException(java.net.UnknownHostException) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) WatermarkingServiceHealthContributor(io.pravega.controller.server.health.WatermarkingServiceHealthContributor) PeriodicWatermarking(io.pravega.controller.server.bucket.PeriodicWatermarking)

Example 3 with ConnectionFactory

use of io.pravega.client.connection.impl.ConnectionFactory 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, 0L).get();
        @Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(ClientConfig.builder().build());
        @Cleanup ClientFactoryImpl internalCF = new ClientFactoryImpl(NameUtils.INTERNAL_SCOPE_NAME, controller, connectionFactory);
        @Cleanup("shutdownNow") val executor = ExecutorServiceHelpers.newScheduledThreadPool(1, "test");
        @Cleanup ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
        serviceBuilder.initialize();
        StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
        TableStore tableStore = serviceBuilder.createTableStoreService();
        @Cleanup AutoScaleMonitor autoScaleMonitor = new AutoScaleMonitor(store, internalCF, AutoScalerConfig.builder().with(AutoScalerConfig.MUTE_IN_SECONDS, 0).with(AutoScalerConfig.COOLDOWN_IN_SECONDS, 0).build());
        @Cleanup PravegaConnectionListener server = new PravegaConnectionListener(false, false, "localhost", 12345, store, tableStore, autoScaleMonitor.getStatsRecorder(), autoScaleMonitor.getTableSegmentStatsRecorder(), null, null, null, true, serviceBuilder.getLowPriorityExecutor(), Config.TLS_PROTOCOL_VERSION.toArray(new String[Config.TLS_PROTOCOL_VERSION.size()]));
        server.startListening();
        controllerWrapper.awaitRunning();
        controllerWrapper.getControllerService().createScope("test", 0L).get();
        controller.createStream("test", "test", CONFIG).get();
        @Cleanup MockClientFactory clientFactory = new MockClientFactory("test", controller, internalCF.getConnectionPool());
        // Mocking pravega service by putting scale up and scale down requests for the stream
        EventWriterConfig writerConfig = EventWriterConfig.builder().transactionTimeoutTime(30000).build();
        TransactionalEventStreamWriter<String> test = clientFactory.createTransactionalEventWriter("writer", "test", new UTF8StringSerializer(), 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");
        controller.startScale(stream, Collections.singletonList(0L), map).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);
        readerGroupManager.createReaderGroup("readergrp", ReaderGroupConfig.builder().stream("test/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().stream().anyMatch(x -> NameUtils.getEpoch(x.getSegmentId()) > 5)) {
                System.err.println("Success");
                log.info("Success");
                System.exit(0);
            } else {
                throw new NotDoneException();
            }
        }), executor).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 : ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) StreamImpl(io.pravega.client.stream.impl.StreamImpl) Retry(io.pravega.common.util.Retry) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) AutoScaleMonitor(io.pravega.segmentstore.server.host.stat.AutoScaleMonitor) 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) TestingServer(org.apache.curator.test.TestingServer) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) Transaction(io.pravega.client.stream.Transaction) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) TransactionalEventStreamWriter(io.pravega.client.stream.TransactionalEventStreamWriter) 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) lombok.val(lombok.val) EventStreamReader(io.pravega.client.stream.EventStreamReader) Slf4j(lombok.extern.slf4j.Slf4j) Config(io.pravega.controller.util.Config) ReaderConfig(io.pravega.client.stream.ReaderConfig) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Collections(java.util.Collections) Controller(io.pravega.client.control.impl.Controller) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) UTF8StringSerializer(io.pravega.client.stream.impl.UTF8StringSerializer) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ClientConfig(io.pravega.client.ClientConfig) AutoScaleMonitor(io.pravega.segmentstore.server.host.stat.AutoScaleMonitor) HashMap(java.util.HashMap) 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.connection.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Stream(io.pravega.client.stream.Stream) UTF8StringSerializer(io.pravega.client.stream.impl.UTF8StringSerializer) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) TestingServer(org.apache.curator.test.TestingServer) lombok.val(lombok.val) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) TestingServerStarter(io.pravega.test.common.TestingServerStarter) Controller(io.pravega.client.control.impl.Controller) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) 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)

Example 4 with ConnectionFactory

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

the class RetentionTest method retentionTest.

private CompletableFuture<Void> retentionTest(String streamName, boolean sizeBased) throws Exception {
    return CompletableFuture.runAsync(() -> {
        final ClientConfig clientConfig = Utils.buildClientConfig(controllerURI);
        @Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(clientConfig);
        ControllerImpl controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(clientConfig).build(), connectionFactory.getInternalExecutor());
        @Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl(SCOPE, controller, connectionFactory);
        log.info("Invoking Writer test with Controller URI: {}", controllerURI);
        // create a writer
        @Cleanup EventStreamWriter<Serializable> writer = clientFactory.createEventWriter(streamName, new JavaSerializer<>(), EventWriterConfig.builder().build());
        // write an event
        String writeEvent = "event";
        writer.writeEvent(writeEvent);
        if (sizeBased) {
            // since truncation always happens at an event boundary, for size based, we will write two events,
            // so that truncation can happen at the first event.
            writer.writeEvent(writeEvent);
        }
        writer.flush();
        log.debug("Writing event: {} ", writeEvent);
        // sleep for 5 mins -- retention frequency is set to 2 minutes. So in 5 minutes we should definitely have
        // 2 retention cycles, with a stream cut being computed in first cycle and truncation happening on the
        // previously computed streamcut in second cycle.
        // for time based retention, we wrote one event, which would get truncated.
        // for size based retention we wrote two events such that stream would retain at least 1 byte as prescribed by
        // the policy
        Exceptions.handleInterrupted(() -> Thread.sleep(5 * 60 * 1000));
        // create a reader
        ReaderGroupManager groupManager = ReaderGroupManager.withScope(SCOPE, clientConfig);
        String groupName = READER_GROUP + streamName;
        groupManager.createReaderGroup(groupName, ReaderGroupConfig.builder().disableAutomaticCheckpoints().stream(Stream.of(SCOPE, streamName)).build());
        EventStreamReader<String> reader = clientFactory.createReader(UUID.randomUUID().toString(), groupName, new JavaSerializer<>(), ReaderConfig.builder().build());
        if (sizeBased) {
            // we should read one write event back from the stream.
            String event = reader.readNextEvent(6000).getEvent();
            assertEquals(event, writeEvent);
        }
        // verify reader functionality is unaffected post truncation
        String event = "newEvent";
        writer.writeEvent(event);
        log.info("Writing event: {}", event);
        Assert.assertEquals(event, reader.readNextEvent(6000).getEvent());
        log.debug("The stream is already truncated.Simple retention test passed.");
    });
}
Also used : ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) Serializable(java.io.Serializable) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) ClientConfig(io.pravega.client.ClientConfig) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Cleanup(lombok.Cleanup)

Example 5 with ConnectionFactory

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

the class OffsetTruncationTest method offsetTruncationTest.

/**
 * This test verifies that truncation works specifying an offset that applies to multiple segments. To this end,
 * the test first writes a set of events on a Stream (with multiple segments) and truncates it at a specified offset
 * (truncatedEvents). The tests asserts that readers first get a TruncatedDataException as they are attempting to
 * read a truncated segment, and then they only read the remaining events that have not been truncated.
 */
@Test
public void offsetTruncationTest() {
    final int totalEvents = 200;
    final int truncatedEvents = 50;
    final ClientConfig clientConfig = Utils.buildClientConfig(controllerURI);
    @Cleanup ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(clientConfig);
    ControllerImpl controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(clientConfig).build(), connectionFactory.getInternalExecutor());
    @Cleanup ClientFactoryImpl clientFactory = new ClientFactoryImpl(SCOPE, controller, connectionFactory);
    log.info("Invoking offsetTruncationTest test with Controller URI: {}", controllerURI);
    @Cleanup ReaderGroupManager groupManager = ReaderGroupManager.withScope(SCOPE, clientConfig);
    groupManager.createReaderGroup(READER_GROUP, ReaderGroupConfig.builder().stream(Stream.of(SCOPE, STREAM)).build());
    @Cleanup ReaderGroup readerGroup = groupManager.getReaderGroup(READER_GROUP);
    // Write events to the Stream.
    writeEvents(clientFactory, STREAM, totalEvents);
    // Instantiate readers to consume from Stream up to truncatedEvents.
    List<CompletableFuture<Integer>> futures = readEventFutures(clientFactory, READER_GROUP, PARALLELISM, truncatedEvents);
    Futures.allOf(futures).join();
    // Ensure that we have read all the events required before initiating the checkpoint.
    assertEquals("Number of events read is not the expected one.", (Integer) truncatedEvents, futures.stream().map(f -> Futures.getAndHandleExceptions(f, RuntimeException::new)).reduce(Integer::sum).get());
    // Perform truncation on stream segment.
    Checkpoint cp = readerGroup.initiateCheckpoint("truncationCheckpoint", executor).join();
    StreamCut streamCut = cp.asImpl().getPositions().values().iterator().next();
    StreamCut alternativeStreamCut = readerGroup.generateStreamCuts(executor).join().get(Stream.of(SCOPE, STREAM));
    assertEquals("StreamCuts for reader group differ depending on how they are generated.", streamCut, alternativeStreamCut);
    assertTrue(streamManager.truncateStream(SCOPE, STREAM, streamCut));
    // Just after the truncation, read events from the offset defined in truncate call onwards.
    final String newGroupName = READER_GROUP + "new";
    groupManager.createReaderGroup(newGroupName, ReaderGroupConfig.builder().stream(Stream.of(SCOPE, STREAM)).build());
    futures = readEventFutures(clientFactory, newGroupName, PARALLELISM);
    Futures.allOf(futures).join();
    assertEquals("Expected read events: ", totalEvents - truncatedEvents, (int) futures.stream().map(CompletableFuture::join).reduce(Integer::sum).get());
    log.debug("The stream has been successfully truncated at event {}. Offset truncation test passed.", truncatedEvents);
}
Also used : StreamCut(io.pravega.client.stream.StreamCut) MarathonException(mesosphere.marathon.client.MarathonException) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) StreamManager(io.pravega.client.admin.StreamManager) RunWith(org.junit.runner.RunWith) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) ReaderGroup(io.pravega.client.stream.ReaderGroup) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) Service(io.pravega.test.system.framework.services.Service) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Stream(io.pravega.client.stream.Stream) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) Checkpoint(io.pravega.client.stream.Checkpoint) URI(java.net.URI) Utils(io.pravega.test.system.framework.Utils) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) Before(org.junit.Before) Environment(io.pravega.test.system.framework.Environment) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ControllerImplConfig(io.pravega.client.control.impl.ControllerImplConfig) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) RandomFactory(io.pravega.common.hash.RandomFactory) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Futures(io.pravega.common.concurrent.Futures) SystemTestRunner(io.pravega.test.system.framework.SystemTestRunner) Assert.assertEquals(org.junit.Assert.assertEquals) ClientConfig(io.pravega.client.ClientConfig) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) StreamCut(io.pravega.client.stream.StreamCut) ReaderGroup(io.pravega.client.stream.ReaderGroup) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Cleanup(lombok.Cleanup) Checkpoint(io.pravega.client.stream.Checkpoint) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Checkpoint(io.pravega.client.stream.Checkpoint) ClientConfig(io.pravega.client.ClientConfig) Test(org.junit.Test)

Aggregations

ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)52 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)50 Cleanup (lombok.Cleanup)43 Test (org.junit.Test)43 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)39 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)38 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)36 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)32 ClientConfig (io.pravega.client.ClientConfig)27 Stream (io.pravega.client.stream.Stream)21 LocalController (io.pravega.controller.server.eventProcessor.LocalController)18 Controller (io.pravega.client.control.impl.Controller)17 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)17 HashMap (java.util.HashMap)17 StreamImpl (io.pravega.client.stream.impl.StreamImpl)16 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)15 ReaderGroup (io.pravega.client.stream.ReaderGroup)14 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)13 Map (java.util.Map)13 CompletableFuture (java.util.concurrent.CompletableFuture)13