Search in sources :

Example 71 with ClientFactoryImpl

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

the class ReadWriteAndScaleWithFailoverTest method setup.

@Before
public void setup() {
    // Get zk details to verify if controller, SSS are running
    Service zkService = Utils.createZookeeperService();
    List<URI> zkUris = zkService.getServiceDetails();
    log.debug("Zookeeper service details: {}", zkUris);
    // get the zk ip details and pass it to  host, controller
    URI zkUri = zkUris.get(0);
    // Verify controller is running.
    controllerInstance = Utils.createPravegaControllerService(zkUri);
    List<URI> conURIs = controllerInstance.getServiceDetails();
    log.info("Pravega Controller service instance details: {}", conURIs);
    assertFalse(conURIs.isEmpty());
    // Fetch all the RPC endpoints and construct the client URIs.
    final List<String> uris = conURIs.stream().filter(ISGRPC).map(URI::getAuthority).collect(Collectors.toList());
    log.debug("controller uris {}", uris);
    controllerURIDirect = URI.create((Utils.TLS_AND_AUTH_ENABLED ? TLS : TCP) + String.join(",", uris));
    log.info("Controller Service direct URI: {}", controllerURIDirect);
    // Verify segment store is running.
    segmentStoreInstance = Utils.createPravegaSegmentStoreService(zkUri, controllerURIDirect);
    List<URI> segmentStoreUris = segmentStoreInstance.getServiceDetails();
    assertFalse(segmentStoreUris.isEmpty());
    log.info("Pravega Segmentstore service instance details: {}", segmentStoreUris);
    // num. of readers + num. of writers + 1 to run checkScale operation
    executorService = ExecutorServiceHelpers.newScheduledThreadPool(NUM_READERS + NUM_WRITERS + 1, "ReadWriteAndScaleWithFailoverTest-main");
    controllerExecutorService = ExecutorServiceHelpers.newScheduledThreadPool(2, "ReadWriteAndScaleWithFailoverTest-controller");
    final ClientConfig clientConfig = Utils.buildClientConfig(controllerURIDirect);
    // get Controller Uri
    controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(clientConfig).maxBackoffMillis(5000).build(), controllerExecutorService);
    testState = new TestState(false);
    streamManager = new StreamManagerImpl(clientConfig);
    createScopeAndStream(scope, SCALE_STREAM, config, streamManager);
    log.info("Scope passed to client factory {}", scope);
    clientFactory = new ClientFactoryImpl(scope, controller, clientConfig);
    readerGroupManager = ReaderGroupManager.withScope(scope, clientConfig);
}
Also used : ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) Service(io.pravega.test.system.framework.services.Service) StreamManagerImpl(io.pravega.client.admin.impl.StreamManagerImpl) ClientConfig(io.pravega.client.ClientConfig) URI(java.net.URI) Before(org.junit.Before)

Example 72 with ClientFactoryImpl

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

the class SetupUtils method startAllServices.

/**
 * Start all pravega related services required for the test deployment.
 *
 * @param numThreads the number of threads for the internal client threadpool.
 * @param enableAuth set to enale authentication
 * @param enableTls set to enable tls
 * @throws Exception on any errors.
 */
public void startAllServices(Integer numThreads, boolean enableAuth, boolean enableTls) throws Exception {
    if (!this.started.compareAndSet(false, true)) {
        log.warn("Services already started, not attempting to start again");
        return;
    }
    if (enableAuth) {
        clientConfigBuilder = clientConfigBuilder.credentials(new DefaultCredentials(SecurityConfigDefaults.AUTH_ADMIN_PASSWORD, SecurityConfigDefaults.AUTH_ADMIN_USERNAME));
    }
    if (enableTls) {
        clientConfigBuilder = clientConfigBuilder.trustStore(pathToConfig() + SecurityConfigDefaults.TLS_CA_CERT_FILE_NAME).controllerURI(URI.create("tls://localhost:" + controllerRPCPort)).validateHostName(false);
    } else {
        clientConfigBuilder = clientConfigBuilder.controllerURI(URI.create("tcp://localhost:" + controllerRPCPort));
    }
    this.executor = ExecutorServiceHelpers.newScheduledThreadPool(2, "Controller pool");
    this.controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(getClientConfig()).build(), executor);
    this.clientFactory = new ClientFactoryImpl(scope, controller, getClientConfig());
    // Start zookeeper.
    this.zkTestServer = new TestingServerStarter().start();
    this.zkTestServer.start();
    // Start Pravega Service.
    this.serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
    this.serviceBuilder.initialize();
    StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
    TableStore tableStore = serviceBuilder.createTableStoreService();
    this.server = new PravegaConnectionListener(enableTls, false, "localhost", servicePort, store, tableStore, SegmentStatsRecorder.noOp(), TableSegmentStatsRecorder.noOp(), new PassingTokenVerifier(), pathToConfig() + SecurityConfigDefaults.TLS_SERVER_CERT_FILE_NAME, pathToConfig() + SecurityConfigDefaults.TLS_SERVER_PRIVATE_KEY_FILE_NAME, true, serviceBuilder.getLowPriorityExecutor(), SecurityConfigDefaults.TLS_PROTOCOL_VERSION);
    this.server.startListening();
    log.info("Started Pravega Service");
    this.adminListener = new AdminConnectionListener(enableTls, false, "localhost", adminPort, store, tableStore, new PassingTokenVerifier(), pathToConfig() + SecurityConfigDefaults.TLS_SERVER_CERT_FILE_NAME, pathToConfig() + SecurityConfigDefaults.TLS_SERVER_PRIVATE_KEY_FILE_NAME, SecurityConfigDefaults.TLS_PROTOCOL_VERSION);
    this.adminListener.startListening();
    log.info("AdminConnectionListener started successfully.");
    // Start Controller.
    this.controllerWrapper = new ControllerWrapper(this.zkTestServer.getConnectString(), false, true, controllerRPCPort, "localhost", servicePort, Config.HOST_STORE_CONTAINER_COUNT, controllerRESTPort, enableAuth, pathToConfig() + SecurityConfigDefaults.AUTH_HANDLER_INPUT_FILE_NAME, "secret", true, 600, enableTls, SecurityConfigDefaults.TLS_PROTOCOL_VERSION, pathToConfig() + SecurityConfigDefaults.TLS_SERVER_CERT_FILE_NAME, pathToConfig() + SecurityConfigDefaults.TLS_SERVER_PRIVATE_KEY_FILE_NAME, pathToConfig() + SecurityConfigDefaults.TLS_SERVER_KEYSTORE_NAME, pathToConfig() + SecurityConfigDefaults.TLS_PASSWORD_FILE_NAME);
    this.controllerWrapper.awaitRunning();
    this.controllerWrapper.getController().createScope(scope).get();
    log.info("Initialized Pravega Controller");
}
Also used : DefaultCredentials(io.pravega.shared.security.auth.DefaultCredentials) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) TestingServerStarter(io.pravega.test.common.TestingServerStarter) AdminConnectionListener(io.pravega.segmentstore.server.host.handler.AdminConnectionListener) PassingTokenVerifier(io.pravega.segmentstore.server.host.delegationtoken.PassingTokenVerifier) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ControllerWrapper(io.pravega.test.integration.demo.ControllerWrapper) TableStore(io.pravega.segmentstore.contracts.tables.TableStore)

Example 73 with ClientFactoryImpl

use of io.pravega.client.stream.impl.ClientFactoryImpl 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();
        ClientFactoryImpl internalCF = new ClientFactoryImpl(NameUtils.INTERNAL_SCOPE_NAME, controller, new SocketConnectionFactoryImpl(ClientConfig.builder().build()));
        @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
        @Cleanup 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();
            }
        }), 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());
        System.exit(-1);
    }
    System.exit(0);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) lombok.val(lombok.val) Arrays(java.util.Arrays) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) Retry(io.pravega.common.util.Retry) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) AutoScaleMonitor(io.pravega.segmentstore.server.host.stat.AutoScaleMonitor) 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) TestingServer(org.apache.curator.test.TestingServer) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) 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) Slf4j(lombok.extern.slf4j.Slf4j) Config(io.pravega.controller.util.Config) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Controller(io.pravega.client.control.impl.Controller) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) MockClientFactory(io.pravega.client.stream.mock.MockClientFactory) ClientConfig(io.pravega.client.ClientConfig) AutoScaleMonitor(io.pravega.segmentstore.server.host.stat.AutoScaleMonitor) TestingServerStarter(io.pravega.test.common.TestingServerStarter) Controller(io.pravega.client.control.impl.Controller) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) 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) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl)

Example 74 with ClientFactoryImpl

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

the class EndToEndAutoScaleDownTest 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();
        controllerWrapper.getControllerService().createScope(NameUtils.INTERNAL_SCOPE_NAME, 0L).get();
        ClientFactoryImpl internalCF = new ClientFactoryImpl(NameUtils.INTERNAL_SCOPE_NAME, controller, new SocketConnectionFactoryImpl(ClientConfig.builder().build()));
        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).with(AutoScalerConfig.CACHE_CLEANUP_IN_SECONDS, 5).with(AutoScalerConfig.CACHE_EXPIRY_IN_SECONDS, 30).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();
        Stream stream = new StreamImpl("test", "test");
        Map<Double, Double> map = new HashMap<>();
        map.put(0.0, 0.33);
        map.put(0.33, 0.66);
        map.put(0.66, 1.0);
        @Cleanup("shutdownNow") ScheduledExecutorService executor = ExecutorServiceHelpers.newScheduledThreadPool(1, "test");
        controller.scaleStream(stream, Collections.singletonList(0L), map, executor).getFuture().get();
        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();
            }
        }), 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());
        System.exit(-1);
    }
    System.exit(0);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) 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) Cleanup(lombok.Cleanup) HashMap(java.util.HashMap) 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) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TestingServer(org.apache.curator.test.TestingServer) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) AutoScalerConfig(io.pravega.segmentstore.server.host.stat.AutoScalerConfig) NameUtils(io.pravega.shared.NameUtils) Slf4j(lombok.extern.slf4j.Slf4j) Config(io.pravega.controller.util.Config) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) Controller(io.pravega.client.control.impl.Controller) ClientConfig(io.pravega.client.ClientConfig) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AutoScaleMonitor(io.pravega.segmentstore.server.host.stat.AutoScaleMonitor) TestingServerStarter(io.pravega.test.common.TestingServerStarter) HashMap(java.util.HashMap) Controller(io.pravega.client.control.impl.Controller) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Cleanup(lombok.Cleanup) PravegaConnectionListener(io.pravega.segmentstore.server.host.handler.PravegaConnectionListener) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) StreamImpl(io.pravega.client.stream.impl.StreamImpl) Stream(io.pravega.client.stream.Stream)

Example 75 with ClientFactoryImpl

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

the class ControllerRestApiTest method restApiTests.

@Test
public void restApiTests() {
    Invocation.Builder builder;
    Response response;
    restServerURI = SETUP_UTILS.getControllerRestUri().toString();
    log.info("REST Server URI: {}", restServerURI);
    // TEST REST server status, ping test
    resourceURl = new StringBuilder(restServerURI).append("/ping").toString();
    webTarget = client.target(resourceURl);
    builder = webTarget.request();
    response = builder.get();
    assertEquals("Ping test", OK.getStatusCode(), response.getStatus());
    log.info("REST Server is running. Ping successful.");
    final String scope1 = RandomStringUtils.randomAlphanumeric(10);
    final String stream1 = RandomStringUtils.randomAlphanumeric(10);
    // TEST CreateScope POST http://controllerURI:Port/v1/scopes
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes").toString();
    webTarget = client.target(resourceURl);
    final CreateScopeRequest createScopeRequest = new CreateScopeRequest();
    createScopeRequest.setScopeName(scope1);
    builder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
    response = builder.post(Entity.json(createScopeRequest));
    assertEquals("Create scope status", CREATED.getStatusCode(), response.getStatus());
    Assert.assertEquals("Create scope response", scope1, response.readEntity(ScopeProperty.class).getScopeName());
    log.info("Create scope: {} successful ", scope1);
    // Create another scope for empty stream test later.
    final String scope2 = RandomStringUtils.randomAlphanumeric(10);
    final CreateScopeRequest createScopeRequest1 = new CreateScopeRequest();
    createScopeRequest1.setScopeName(scope2);
    builder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
    response = builder.post(Entity.json(createScopeRequest1));
    assertEquals("Create scope status", CREATED.getStatusCode(), response.getStatus());
    Assert.assertEquals("Create scope response", scope2, response.readEntity(ScopeProperty.class).getScopeName());
    // TEST CreateStream POST  http://controllerURI:Port/v1/scopes/{scopeName}/streams
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams").toString();
    webTarget = client.target(resourceURl);
    CreateStreamRequest createStreamRequest = new CreateStreamRequest();
    ScalingConfig scalingConfig = new ScalingConfig();
    scalingConfig.setType(ScalingConfig.TypeEnum.FIXED_NUM_SEGMENTS);
    scalingConfig.setTargetRate(2);
    scalingConfig.scaleFactor(2);
    scalingConfig.minSegments(2);
    RetentionConfig retentionConfig = new RetentionConfig();
    retentionConfig.setType(RetentionConfig.TypeEnum.LIMITED_DAYS);
    retentionConfig.setValue(123L);
    TagsList tagsList = new TagsList();
    tagsList.add("testTag");
    createStreamRequest.setStreamName(stream1);
    createStreamRequest.setScalingPolicy(scalingConfig);
    createStreamRequest.setRetentionPolicy(retentionConfig);
    createStreamRequest.setStreamTags(tagsList);
    createStreamRequest.setTimestampAggregationTimeout(1000L);
    createStreamRequest.setRolloverSizeBytes(1024L);
    builder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
    response = builder.post(Entity.json(createStreamRequest));
    assertEquals("Create stream status", CREATED.getStatusCode(), response.getStatus());
    final StreamProperty streamPropertyResponse = response.readEntity(StreamProperty.class);
    assertEquals("Scope name in response", scope1, streamPropertyResponse.getScopeName());
    assertEquals("Stream name in response", stream1, streamPropertyResponse.getStreamName());
    assertEquals("TimestampAggregationTimeout in response", 1000L, (long) streamPropertyResponse.getTimestampAggregationTimeout());
    assertEquals("RolloverSizeBytes in response", 1024L, (long) streamPropertyResponse.getRolloverSizeBytes());
    log.info("Create stream: {} successful", stream1);
    // Test listScopes  GET http://controllerURI:Port/v1/scopes/{scopeName}/streams
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes").toString();
    webTarget = client.target(resourceURl);
    builder = webTarget.request();
    response = builder.get();
    assertEquals("List scopes", OK.getStatusCode(), response.getStatus());
    log.info("List scopes successful");
    // Test listStream GET /v1/scopes/scope1/streams
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams").toString();
    webTarget = client.target(resourceURl);
    builder = webTarget.request();
    response = builder.get();
    assertEquals("List streams", OK.getStatusCode(), response.getStatus());
    Assert.assertEquals("List streams size", 1, response.readEntity(StreamsList.class).getStreams().size());
    log.info("List streams successful");
    // Test listStream GET /v1/scopes/scope1/streams for tags
    response = client.target(resourceURl).queryParam("filter_type", "tag").queryParam("filter_value", "testTag").request().get();
    assertEquals("List streams", OK.getStatusCode(), response.getStatus());
    Assert.assertEquals("List streams size", 1, response.readEntity(StreamsList.class).getStreams().size());
    response = client.target(resourceURl).queryParam("filter_type", "tag").queryParam("filter_value", "randomTag").request().get();
    assertEquals("List streams", OK.getStatusCode(), response.getStatus());
    Assert.assertEquals("List streams size", 0, response.readEntity(StreamsList.class).getStreams().size());
    log.info("List streams with tag successful");
    response = client.target(resourceURl).queryParam("filter_type", "showInternalStreams").request().get();
    assertEquals("List streams", OK.getStatusCode(), response.getStatus());
    assertTrue(response.readEntity(StreamsList.class).getStreams().get(0).getStreamName().startsWith("_MARK"));
    log.info("List streams with showInternalStreams successful");
    // Test for the case when the scope is empty.
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope2 + "/streams").toString();
    response = client.target(resourceURl).request().get();
    assertEquals("List streams", OK.getStatusCode(), response.getStatus());
    Assert.assertEquals("List streams size", 0, response.readEntity(StreamsList.class).getStreams().size());
    // Test getScope
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1).toString();
    response = client.target(resourceURl).request().get();
    assertEquals("Get scope status", OK.getStatusCode(), response.getStatus());
    assertEquals("Get scope scope1 response", scope1, response.readEntity(ScopeProperty.class).getScopeName());
    log.info("Get scope successful");
    // Test updateStream
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1).toString();
    UpdateStreamRequest updateStreamRequest = new UpdateStreamRequest();
    ScalingConfig scalingConfig1 = new ScalingConfig();
    scalingConfig1.setType(ScalingConfig.TypeEnum.FIXED_NUM_SEGMENTS);
    scalingConfig1.setTargetRate(2);
    // update existing scaleFactor from 2 to 3
    scalingConfig1.scaleFactor(3);
    // update existing minSegments from 2 to 4
    scalingConfig1.minSegments(4);
    updateStreamRequest.setScalingPolicy(scalingConfig1);
    updateStreamRequest.setRetentionPolicy(retentionConfig);
    updateStreamRequest.setTimestampAggregationTimeout(2000L);
    updateStreamRequest.setRolloverSizeBytes(2048L);
    response = client.target(resourceURl).request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(updateStreamRequest));
    assertEquals("Update stream status", OK.getStatusCode(), response.getStatus());
    assertEquals("Verify updated property", 4, response.readEntity(StreamProperty.class).getScalingPolicy().getMinSegments().intValue());
    log.info("Update stream successful");
    // Test scaling event list GET /v1/scopes/scope1/streams/stream1
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1 + "/scaling-events").toString();
    response = client.target(resourceURl).queryParam("from", 0L).queryParam("to", System.currentTimeMillis()).request().get();
    List<ScaleMetadata> scaleMetadataListResponse = response.readEntity(new GenericType<List<ScaleMetadata>>() {
    });
    assertEquals(2, scaleMetadataListResponse.size());
    assertEquals(2, scaleMetadataListResponse.get(0).getSegments().size());
    assertEquals(4, scaleMetadataListResponse.get(1).getSegments().size());
    // Test getStream
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1).toString();
    response = client.target(resourceURl).request().get();
    assertEquals("Get stream status", OK.getStatusCode(), response.getStatus());
    StreamProperty responseProperty = response.readEntity(StreamProperty.class);
    assertEquals("Get stream stream1 response", stream1, responseProperty.getStreamName());
    assertEquals("Get stream stream1 response TimestampAggregationTimeout", (long) responseProperty.getTimestampAggregationTimeout(), 2000L);
    assertEquals("Get stream stream1 RolloverSizeBytes", (long) responseProperty.getRolloverSizeBytes(), 2048L);
    log.info("Get stream successful");
    // Test updateStreamState
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1 + "/state").toString();
    StreamState streamState = new StreamState();
    streamState.setStreamState(StreamState.StreamStateEnum.SEALED);
    response = client.target(resourceURl).request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(streamState));
    assertEquals("UpdateStreamState status", OK.getStatusCode(), response.getStatus());
    assertEquals("UpdateStreamState status in response", streamState.getStreamState(), response.readEntity(StreamState.class).getStreamState());
    log.info("Update stream state successful");
    // Test deleteStream
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1).toString();
    response = client.target(resourceURl).request().delete();
    assertEquals("DeleteStream status", NO_CONTENT.getStatusCode(), response.getStatus());
    log.info("Delete stream successful");
    // Test deleteScope
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1).toString();
    response = client.target(resourceURl).request().delete();
    assertEquals("Get scope status", NO_CONTENT.getStatusCode(), response.getStatus());
    log.info("Delete Scope successful");
    // Test reader groups APIs.
    // Prepare the streams and readers using the admin client.
    final String testScope = RandomStringUtils.randomAlphanumeric(10);
    final String testStream1 = RandomStringUtils.randomAlphanumeric(10);
    final String testStream2 = RandomStringUtils.randomAlphanumeric(10);
    URI controllerUri = SETUP_UTILS.getControllerUri();
    @Cleanup("shutdown") InlineExecutor inlineExecutor = new InlineExecutor();
    ClientConfig clientConfig = ClientConfig.builder().build();
    try (ConnectionPool cp = new ConnectionPoolImpl(clientConfig, new SocketConnectionFactoryImpl(clientConfig));
        StreamManager streamManager = new StreamManagerImpl(createController(controllerUri, inlineExecutor), cp)) {
        log.info("Creating scope: {}", testScope);
        streamManager.createScope(testScope);
        log.info("Creating stream: {}", testStream1);
        StreamConfiguration streamConf1 = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build();
        streamManager.createStream(testScope, testStream1, streamConf1);
        log.info("Creating stream: {}", testStream2);
        StreamConfiguration streamConf2 = StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build();
        streamManager.createStream(testScope, testStream2, streamConf2);
    }
    final String readerGroupName1 = RandomStringUtils.randomAlphanumeric(10);
    final String readerGroupName2 = RandomStringUtils.randomAlphanumeric(10);
    final String reader1 = RandomStringUtils.randomAlphanumeric(10);
    final String reader2 = RandomStringUtils.randomAlphanumeric(10);
    try (ClientFactoryImpl clientFactory = new ClientFactoryImpl(testScope, createController(controllerUri, inlineExecutor), clientConfig);
        ReaderGroupManager readerGroupManager = ReaderGroupManager.withScope(testScope, ClientConfig.builder().controllerURI(controllerUri).build())) {
        readerGroupManager.createReaderGroup(readerGroupName1, ReaderGroupConfig.builder().stream(Stream.of(testScope, testStream1)).stream(Stream.of(testScope, testStream2)).build());
        readerGroupManager.createReaderGroup(readerGroupName2, ReaderGroupConfig.builder().stream(Stream.of(testScope, testStream1)).stream(Stream.of(testScope, testStream2)).build());
        clientFactory.createReader(reader1, readerGroupName1, new JavaSerializer<Long>(), ReaderConfig.builder().build());
        clientFactory.createReader(reader2, readerGroupName1, new JavaSerializer<Long>(), ReaderConfig.builder().build());
    }
    // Test fetching readergroups.
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + testScope + "/readergroups").toString();
    response = client.target(resourceURl).request().get();
    assertEquals("Get readergroups status", OK.getStatusCode(), response.getStatus());
    ReaderGroupsList readerGroupsList = response.readEntity(ReaderGroupsList.class);
    assertEquals("Get readergroups size", 2, readerGroupsList.getReaderGroups().size());
    assertTrue(readerGroupsList.getReaderGroups().contains(new ReaderGroupsListReaderGroups().readerGroupName(readerGroupName1)));
    assertTrue(readerGroupsList.getReaderGroups().contains(new ReaderGroupsListReaderGroups().readerGroupName(readerGroupName2)));
    log.info("Get readergroups successful");
    // Test fetching readergroup info.
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + testScope + "/readergroups/" + readerGroupName1).toString();
    response = client.target(resourceURl).request().get();
    assertEquals("Get readergroup properties status", OK.getStatusCode(), response.getStatus());
    ReaderGroupProperty readerGroupProperty = response.readEntity(ReaderGroupProperty.class);
    assertEquals("Get readergroup name", readerGroupName1, readerGroupProperty.getReaderGroupName());
    assertEquals("Get readergroup scope name", testScope, readerGroupProperty.getScopeName());
    assertEquals("Get readergroup streams size", 2, readerGroupProperty.getStreamList().size());
    assertTrue(readerGroupProperty.getStreamList().contains(Stream.of(testScope, testStream1).getScopedName()));
    assertTrue(readerGroupProperty.getStreamList().contains(Stream.of(testScope, testStream2).getScopedName()));
    assertEquals("Get readergroup onlinereaders size", 2, readerGroupProperty.getOnlineReaderIds().size());
    assertTrue(readerGroupProperty.getOnlineReaderIds().contains(reader1));
    assertTrue(readerGroupProperty.getOnlineReaderIds().contains(reader2));
    // Test readergroup or scope not found.
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + testScope + "/readergroups/" + "unknownreadergroup").toString();
    response = client.target(resourceURl).request().get();
    assertEquals("Get readergroup properties status", NOT_FOUND.getStatusCode(), response.getStatus());
    resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + "unknownscope" + "/readergroups/" + readerGroupName1).toString();
    response = client.target(resourceURl).request().get();
    assertEquals("Get readergroup properties status", NOT_FOUND.getStatusCode(), response.getStatus());
    log.info("Get readergroup properties successful");
    log.info("Test restApiTests passed successfully!");
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) Invocation(javax.ws.rs.client.Invocation) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) StreamManagerImpl(io.pravega.client.admin.impl.StreamManagerImpl) URI(java.net.URI) Cleanup(lombok.Cleanup) StreamState(io.pravega.controller.server.rest.generated.model.StreamState) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) UpdateStreamRequest(io.pravega.controller.server.rest.generated.model.UpdateStreamRequest) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) InlineExecutor(io.pravega.test.common.InlineExecutor) CreateStreamRequest(io.pravega.controller.server.rest.generated.model.CreateStreamRequest) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) List(java.util.List) TagsList(io.pravega.controller.server.rest.generated.model.TagsList) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) ClientConfig(io.pravega.client.ClientConfig) ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ScalingConfig(io.pravega.controller.server.rest.generated.model.ScalingConfig) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) StreamProperty(io.pravega.controller.server.rest.generated.model.StreamProperty) TagsList(io.pravega.controller.server.rest.generated.model.TagsList) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) RetentionConfig(io.pravega.controller.server.rest.generated.model.RetentionConfig) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) Response(javax.ws.rs.core.Response) CreateScopeRequest(io.pravega.controller.server.rest.generated.model.CreateScopeRequest) StreamManager(io.pravega.client.admin.StreamManager) Test(org.junit.Test)

Aggregations

ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)80 Test (org.junit.Test)63 Cleanup (lombok.Cleanup)59 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)54 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)50 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)44 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)40 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)39 ClientConfig (io.pravega.client.ClientConfig)37 Stream (io.pravega.client.stream.Stream)27 Controller (io.pravega.client.control.impl.Controller)25 StreamImpl (io.pravega.client.stream.impl.StreamImpl)24 HashMap (java.util.HashMap)23 ReaderGroup (io.pravega.client.stream.ReaderGroup)19 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)19 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)19 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)18 URI (java.net.URI)18 CompletableFuture (java.util.concurrent.CompletableFuture)18 ControllerImpl (io.pravega.client.control.impl.ControllerImpl)17