Search in sources :

Example 1 with CreateStreamRequest

use of io.pravega.controller.server.rest.generated.model.CreateStreamRequest in project pravega by pravega.

the class StreamMetadataResourceImpl method createStream.

/**
 * Implementation of createStream REST API.
 *
 * @param scopeName           The scope name of stream.
 * @param createStreamRequest The object conforming to createStream request json.
 * @param securityContext     The security for API access.
 * @param asyncResponse       AsyncResponse provides means for asynchronous server side response processing.
 */
@Override
public void createStream(final String scopeName, final CreateStreamRequest createStreamRequest, final SecurityContext securityContext, final AsyncResponse asyncResponse) {
    long traceId = LoggerHelpers.traceEnter(log, "createStream");
    try {
        NameUtils.validateUserStreamName(createStreamRequest.getStreamName());
    } catch (IllegalArgumentException | NullPointerException e) {
        log.warn("Create stream failed due to invalid stream name {}", createStreamRequest.getStreamName());
        asyncResponse.resume(Response.status(Status.BAD_REQUEST).build());
        LoggerHelpers.traceLeave(log, "createStream", traceId);
        return;
    }
    try {
        authenticate(scopeName + "/" + createStreamRequest.getStreamName(), READ_UPDATE);
    } catch (AuthenticationException e) {
        log.warn("Create stream for {} failed due to authentication failure.", createStreamRequest.getStreamName());
        asyncResponse.resume(Response.status(Status.UNAUTHORIZED).build());
        LoggerHelpers.traceLeave(log, "createStream", traceId);
        return;
    }
    StreamConfiguration streamConfiguration = ModelHelper.getCreateStreamConfig(createStreamRequest, scopeName);
    controllerService.createStream(streamConfiguration, System.currentTimeMillis()).thenApply(streamStatus -> {
        Response resp = null;
        if (streamStatus.getStatus() == CreateStreamStatus.Status.SUCCESS) {
            log.info("Successfully created stream: {}/{}", scopeName, streamConfiguration.getStreamName());
            resp = Response.status(Status.CREATED).entity(ModelHelper.encodeStreamResponse(streamConfiguration)).build();
        } else if (streamStatus.getStatus() == CreateStreamStatus.Status.STREAM_EXISTS) {
            log.warn("Stream already exists: {}/{}", scopeName, streamConfiguration.getStreamName());
            resp = Response.status(Status.CONFLICT).build();
        } else if (streamStatus.getStatus() == CreateStreamStatus.Status.SCOPE_NOT_FOUND) {
            log.warn("Scope not found: {}", scopeName);
            resp = Response.status(Status.NOT_FOUND).build();
        } else if (streamStatus.getStatus() == CreateStreamStatus.Status.INVALID_STREAM_NAME) {
            log.warn("Invalid stream name: {}", streamConfiguration.getStreamName());
            resp = Response.status(Status.BAD_REQUEST).build();
        } else {
            log.warn("createStream failed for : {}/{}", scopeName, streamConfiguration.getStreamName());
            resp = Response.status(Status.INTERNAL_SERVER_ERROR).build();
        }
        return resp;
    }).exceptionally(exception -> {
        log.warn("createStream for {}/{} failed {}: ", scopeName, streamConfiguration.getStreamName(), exception);
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }).thenApply(asyncResponse::resume).thenAccept(x -> LoggerHelpers.traceLeave(log, "createStream", traceId));
}
Also used : AuthenticationException(io.pravega.common.auth.AuthenticationException) Arrays(java.util.Arrays) ApiV1(io.pravega.controller.server.rest.v1.ApiV1) READ(io.pravega.auth.AuthHandler.Permissions.READ) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) AuthHandler(io.pravega.auth.AuthHandler) SecurityContext(javax.ws.rs.core.SecurityContext) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) ReaderGroup(io.pravega.client.stream.ReaderGroup) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) LocalController(io.pravega.controller.server.eventProcessor.LocalController) StoreException(io.pravega.controller.store.stream.StoreException) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Map(java.util.Map) ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) INTERNAL_NAME_PREFIX(io.pravega.shared.NameUtils.INTERNAL_NAME_PREFIX) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) CreateScopeRequest(io.pravega.controller.server.rest.generated.model.CreateScopeRequest) Collectors(java.util.stream.Collectors) CreateStreamRequest(io.pravega.controller.server.rest.generated.model.CreateStreamRequest) READER_GROUP_STREAM_PREFIX(io.pravega.shared.NameUtils.READER_GROUP_STREAM_PREFIX) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) HttpHeaders(javax.ws.rs.core.HttpHeaders) StreamState(io.pravega.controller.server.rest.generated.model.StreamState) Response(javax.ws.rs.core.Response) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) CompletableFuture(java.util.concurrent.CompletableFuture) UpdateStreamRequest(io.pravega.controller.server.rest.generated.model.UpdateStreamRequest) ArrayList(java.util.ArrayList) READ_UPDATE(io.pravega.auth.AuthHandler.Permissions.READ_UPDATE) ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Status(javax.ws.rs.core.Response.Status) LoggerHelpers(io.pravega.common.LoggerHelpers) ControllerService(io.pravega.controller.server.ControllerService) NameUtils(io.pravega.shared.NameUtils) Iterator(java.util.Iterator) ScopeProperty(io.pravega.controller.server.rest.generated.model.ScopeProperty) ModelHelper(io.pravega.controller.server.rest.ModelHelper) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) PravegaAuthManager(io.pravega.controller.server.rpc.auth.PravegaAuthManager) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) AuthenticationException(io.pravega.common.auth.AuthenticationException) StreamConfiguration(io.pravega.client.stream.StreamConfiguration)

Example 2 with CreateStreamRequest

use of io.pravega.controller.server.rest.generated.model.CreateStreamRequest 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);
    // 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);
    createStreamRequest.setStreamName(stream1);
    createStreamRequest.setScalingPolicy(scalingConfig);
    createStreamRequest.setRetentionPolicy(retentionConfig);
    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());
    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 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);
    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 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());
    assertEquals("Get stream stream1 response", stream1, response.readEntity(StreamProperty.class).getStreamName());
    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();
    try (StreamManager streamManager = new StreamManagerImpl(createController(controllerUri, inlineExecutor))) {
        log.info("Creating scope: {}", testScope);
        streamManager.createScope(testScope);
        log.info("Creating stream: {}", testStream1);
        StreamConfiguration streamConf1 = StreamConfiguration.builder().scope(testScope).streamName(testStream1).scalingPolicy(ScalingPolicy.fixed(1)).build();
        streamManager.createStream(testScope, testStream1, streamConf1);
        log.info("Creating stream: {}", testStream2);
        StreamConfiguration streamConf2 = StreamConfiguration.builder().scope(testScope).streamName(testStream2).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 (ClientFactory clientFactory = new ClientFactoryImpl(testScope, createController(controllerUri, inlineExecutor));
        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(testStream1));
    assertTrue(readerGroupProperty.getStreamList().contains(testStream2));
    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 : ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) Invocation(javax.ws.rs.client.Invocation) ClientFactory(io.pravega.client.ClientFactory) 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) 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) 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)

Example 3 with CreateStreamRequest

use of io.pravega.controller.server.rest.generated.model.CreateStreamRequest in project pravega by pravega.

the class ModelHelperTest method testGetCreateStreamConfig.

@Test
public void testGetCreateStreamConfig() {
    ScalingConfig scalingConfig = new ScalingConfig();
    scalingConfig.setType(ScalingConfig.TypeEnum.FIXED_NUM_SEGMENTS);
    scalingConfig.setMinSegments(2);
    CreateStreamRequest createStreamRequest = new CreateStreamRequest();
    createStreamRequest.setStreamName("stream");
    createStreamRequest.setScalingPolicy(scalingConfig);
    StreamConfiguration streamConfig = getCreateStreamConfig(createStreamRequest, "scope");
    Assert.assertEquals(ScalingPolicy.ScaleType.FIXED_NUM_SEGMENTS, streamConfig.getScalingPolicy().getScaleType());
    Assert.assertEquals(2, streamConfig.getScalingPolicy().getMinNumSegments());
    Assert.assertNull(streamConfig.getRetentionPolicy());
    scalingConfig.setType(ScalingConfig.TypeEnum.BY_RATE_IN_EVENTS_PER_SEC);
    scalingConfig.setTargetRate(123);
    scalingConfig.setScaleFactor(2);
    RetentionConfig retentionConfig = new RetentionConfig();
    retentionConfig.setType(RetentionConfig.TypeEnum.LIMITED_DAYS);
    retentionConfig.setValue(1234L);
    createStreamRequest.setStreamName("stream");
    createStreamRequest.setScalingPolicy(scalingConfig);
    createStreamRequest.setRetentionPolicy(retentionConfig);
    streamConfig = getCreateStreamConfig(createStreamRequest, "scope");
    Assert.assertEquals(ScalingPolicy.ScaleType.BY_RATE_IN_EVENTS_PER_SEC, streamConfig.getScalingPolicy().getScaleType());
    Assert.assertEquals("scope", streamConfig.getScope());
    Assert.assertEquals("stream", streamConfig.getStreamName());
    Assert.assertEquals(2, streamConfig.getScalingPolicy().getScaleFactor());
    Assert.assertEquals(123, streamConfig.getScalingPolicy().getTargetRate());
    Assert.assertEquals(RetentionPolicy.RetentionType.TIME, streamConfig.getRetentionPolicy().getRetentionType());
    Assert.assertEquals(Duration.ofDays(1234L).toMillis(), streamConfig.getRetentionPolicy().getRetentionParam());
    scalingConfig.setType(ScalingConfig.TypeEnum.BY_RATE_IN_KBYTES_PER_SEC);
    scalingConfig.setTargetRate(1234);
    scalingConfig.setScaleFactor(23);
    retentionConfig.setType(RetentionConfig.TypeEnum.LIMITED_SIZE_MB);
    retentionConfig.setValue(12345L);
    createStreamRequest.setStreamName("stream");
    createStreamRequest.setScalingPolicy(scalingConfig);
    createStreamRequest.setRetentionPolicy(retentionConfig);
    streamConfig = getCreateStreamConfig(createStreamRequest, "scope");
    Assert.assertEquals(ScalingPolicy.ScaleType.BY_RATE_IN_KBYTES_PER_SEC, streamConfig.getScalingPolicy().getScaleType());
    Assert.assertEquals(23, streamConfig.getScalingPolicy().getScaleFactor());
    Assert.assertEquals(1234, streamConfig.getScalingPolicy().getTargetRate());
    Assert.assertEquals(RetentionPolicy.RetentionType.SIZE, streamConfig.getRetentionPolicy().getRetentionType());
    Assert.assertEquals(12345L * 1024 * 1024, streamConfig.getRetentionPolicy().getRetentionParam());
}
Also used : ScalingConfig(io.pravega.controller.server.rest.generated.model.ScalingConfig) CreateStreamRequest(io.pravega.controller.server.rest.generated.model.CreateStreamRequest) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) RetentionConfig(io.pravega.controller.server.rest.generated.model.RetentionConfig) Test(org.junit.Test)

Example 4 with CreateStreamRequest

use of io.pravega.controller.server.rest.generated.model.CreateStreamRequest in project pravega by pravega.

the class StreamMetaDataTests method testCreateStream.

/**
 * Test for createStream REST API.
 *
 * @throws ExecutionException
 * @throws InterruptedException
 */
@Test
public void testCreateStream() throws ExecutionException, InterruptedException {
    String streamResourceURI = getURI() + "v1/scopes/" + scope1 + "/streams";
    // Test to create a stream which doesn't exist
    when(mockControllerService.createStream(any(), anyLong())).thenReturn(createStreamStatus);
    Response response = addAuthHeaders(client.target(streamResourceURI).request()).buildPost(Entity.json(createStreamRequest)).invoke();
    assertEquals("Create Stream Status", 201, response.getStatus());
    StreamProperty streamResponseActual = response.readEntity(StreamProperty.class);
    testExpectedVsActualObject(streamResponseExpected, streamResponseActual);
    response.close();
    // Test to create a stream which doesn't exist and has no Retention Policy set.
    when(mockControllerService.createStream(any(), anyLong())).thenReturn(createStreamStatus);
    response = addAuthHeaders(client.target(streamResourceURI).request()).buildPost(Entity.json(createStreamRequest4)).invoke();
    assertEquals("Create Stream Status", 201, response.getStatus());
    streamResponseActual = response.readEntity(StreamProperty.class);
    testExpectedVsActualObject(streamResponseExpected2, streamResponseActual);
    response.close();
    // Test to create a stream with internal stream name
    final CreateStreamRequest streamRequest = new CreateStreamRequest();
    streamRequest.setStreamName(NameUtils.getInternalNameForStream("stream"));
    when(mockControllerService.createStream(any(), anyLong())).thenReturn(createStreamStatus2);
    response = addAuthHeaders(client.target(streamResourceURI).request()).buildPost(Entity.json(streamRequest)).invoke();
    assertEquals("Create Stream Status", 400, response.getStatus());
    response.close();
    // Test to create a stream which doesn't exist and have Scaling Policy FIXED_NUM_SEGMENTS
    when(mockControllerService.createStream(any(), anyLong())).thenReturn(createStreamStatus);
    response = addAuthHeaders(client.target(streamResourceURI).request()).buildPost(Entity.json(createStreamRequest5)).invoke();
    assertEquals("Create Stream Status", 201, response.getStatus());
    streamResponseActual = response.readEntity(StreamProperty.class);
    testExpectedVsActualObject(streamResponseExpected3, streamResponseActual);
    response.close();
    // Test to create a stream that already exists
    when(mockControllerService.createStream(any(), anyLong())).thenReturn(createStreamStatus2);
    response = addAuthHeaders(client.target(streamResourceURI).request()).buildPost(Entity.json(createStreamRequest)).invoke();
    assertEquals("Create Stream Status", 409, response.getStatus());
    response.close();
    // Test for validation of create stream request object
    when(mockControllerService.createStream(any(), anyLong())).thenReturn(createStreamStatus3);
    response = addAuthHeaders(client.target(streamResourceURI).request()).buildPost(Entity.json(createStreamRequest2)).invoke();
    // TODO: Server should be returning 400 here, change this once issue
    // https://github.com/pravega/pravega/issues/531 is fixed.
    assertEquals("Create Stream Status", 500, response.getStatus());
    response.close();
    // Test create stream for non-existent scope
    when(mockControllerService.createStream(any(), anyLong())).thenReturn(createStreamStatus4);
    response = addAuthHeaders(client.target(streamResourceURI).request()).buildPost(Entity.json(createStreamRequest3)).invoke();
    assertEquals("Create Stream Status for non-existent scope", 404, response.getStatus());
    response.close();
}
Also used : Response(javax.ws.rs.core.Response) CreateStreamRequest(io.pravega.controller.server.rest.generated.model.CreateStreamRequest) StreamProperty(io.pravega.controller.server.rest.generated.model.StreamProperty) Test(org.junit.Test)

Example 5 with CreateStreamRequest

use of io.pravega.controller.server.rest.generated.model.CreateStreamRequest in project pravega by pravega.

the class ControllerRestApiTest method restApiTests.

@Test(timeout = 300000)
public void restApiTests() {
    Service conService = Utils.createPravegaControllerService(null);
    List<URI> ctlURIs = conService.getServiceDetails();
    URI controllerRESTUri = ctlURIs.get(1);
    Invocation.Builder builder;
    Response response;
    restServerURI = "http://" + controllerRESTUri.getHost() + ":" + controllerRESTUri.getPort();
    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);
    // 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);
    createStreamRequest.setStreamName(stream1);
    createStreamRequest.setScalingPolicy(scalingConfig);
    createStreamRequest.setRetentionPolicy(retentionConfig);
    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());
    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 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);
    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 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());
    assertEquals("Get stream stream1 response", stream1, response.readEntity(StreamProperty.class).getStreamName());
    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 = ctlURIs.get(0);
    try (StreamManager streamManager = new StreamManagerImpl(ClientConfig.builder().controllerURI(controllerUri).build())) {
        log.info("Creating scope: {}", testScope);
        streamManager.createScope(testScope);
        log.info("Creating stream: {}", testStream1);
        StreamConfiguration streamConf1 = StreamConfiguration.builder().scope(testScope).streamName(testStream1).scalingPolicy(ScalingPolicy.fixed(1)).build();
        streamManager.createStream(testScope, testStream1, streamConf1);
        log.info("Creating stream: {}", testStream2);
        StreamConfiguration streamConf2 = StreamConfiguration.builder().scope(testScope).streamName(testStream2).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);
    @Cleanup("shutdown") InlineExecutor executor = new InlineExecutor();
    Controller controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(controllerUri).build()).build(), executor);
    try (ClientFactory clientFactory = new ClientFactoryImpl(testScope, controller);
        ReaderGroupManager readerGroupManager = ReaderGroupManager.withScope(testScope, ClientConfig.builder().controllerURI(controllerUri).build())) {
        final ReaderGroupConfig config = ReaderGroupConfig.builder().stream(Stream.of(testScope, testStream1)).stream(Stream.of(testScope, testStream2)).build();
        readerGroupManager.createReaderGroup(readerGroupName1, config);
        readerGroupManager.createReaderGroup(readerGroupName2, config);
        clientFactory.createReader(reader1, readerGroupName1, new JavaSerializer<Long>(), ReaderConfig.builder().build());
        clientFactory.createReader(reader2, readerGroupName1, new JavaSerializer<Long>(), ReaderConfig.builder().build());
    }
    // Verify the reader group info using REST APIs.
    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(testStream1));
    assertTrue(readerGroupProperty.getStreamList().contains(testStream2));
    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 : ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) Invocation(javax.ws.rs.client.Invocation) ClientFactory(io.pravega.client.ClientFactory) 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) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ScalingConfig(io.pravega.controller.server.rest.generated.model.ScalingConfig) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) ControllerImpl(io.pravega.client.stream.impl.ControllerImpl) Service(io.pravega.test.system.framework.services.Service) StreamProperty(io.pravega.controller.server.rest.generated.model.StreamProperty) Controller(io.pravega.client.stream.impl.Controller) 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

CreateStreamRequest (io.pravega.controller.server.rest.generated.model.CreateStreamRequest)5 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)4 Test (org.junit.Test)4 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)3 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)3 CreateScopeRequest (io.pravega.controller.server.rest.generated.model.CreateScopeRequest)3 ReaderGroupProperty (io.pravega.controller.server.rest.generated.model.ReaderGroupProperty)3 ReaderGroupsList (io.pravega.controller.server.rest.generated.model.ReaderGroupsList)3 ReaderGroupsListReaderGroups (io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups)3 RetentionConfig (io.pravega.controller.server.rest.generated.model.RetentionConfig)3 ScalingConfig (io.pravega.controller.server.rest.generated.model.ScalingConfig)3 StreamProperty (io.pravega.controller.server.rest.generated.model.StreamProperty)3 StreamState (io.pravega.controller.server.rest.generated.model.StreamState)3 StreamsList (io.pravega.controller.server.rest.generated.model.StreamsList)3 UpdateStreamRequest (io.pravega.controller.server.rest.generated.model.UpdateStreamRequest)3 Response (javax.ws.rs.core.Response)3 ClientFactory (io.pravega.client.ClientFactory)2 StreamManager (io.pravega.client.admin.StreamManager)2 StreamManagerImpl (io.pravega.client.admin.impl.StreamManagerImpl)2 InlineExecutor (io.pravega.test.common.InlineExecutor)2