Search in sources :

Example 11 with ReaderGroup

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

the class StreamMetadataResourceImpl method getReaderGroup.

@Override
public void getReaderGroup(final String scopeName, final String readerGroupName, final SecurityContext securityContext, final AsyncResponse asyncResponse) {
    long traceId = LoggerHelpers.traceEnter(log, "getReaderGroup");
    try {
        authenticate(scopeName + "/" + readerGroupName, READ);
    } catch (AuthenticationException e) {
        log.warn("Get reader group for {} failed due to authentication failure.", scopeName + "/" + readerGroupName);
        asyncResponse.resume(Response.status(Status.UNAUTHORIZED).build());
        LoggerHelpers.traceLeave(log, "getReaderGroup", traceId);
        return;
    }
    ReaderGroupManager readerGroupManager = new ReaderGroupManagerImpl(scopeName, this.localController, new ClientFactoryImpl(scopeName, this.localController), this.connectionFactory);
    ReaderGroupProperty readerGroupProperty = new ReaderGroupProperty();
    readerGroupProperty.setScopeName(scopeName);
    readerGroupProperty.setReaderGroupName(readerGroupName);
    CompletableFuture.supplyAsync(() -> {
        ReaderGroup readerGroup = readerGroupManager.getReaderGroup(readerGroupName);
        readerGroupProperty.setOnlineReaderIds(new ArrayList<>(readerGroup.getOnlineReaders()));
        readerGroupProperty.setStreamList(new ArrayList<>(readerGroup.getStreamNames()));
        return Response.status(Status.OK).entity(readerGroupProperty).build();
    }, controllerService.getExecutor()).exceptionally(exception -> {
        log.warn("getReaderGroup for {} failed with exception: ", readerGroupName, exception);
        if (exception.getCause() instanceof InvalidStreamException) {
            return Response.status(Status.NOT_FOUND).build();
        } else {
            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
        }
    }).thenAccept(response -> {
        asyncResponse.resume(response);
        readerGroupManager.close();
        LoggerHelpers.traceLeave(log, "getReaderGroup", 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) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) AuthenticationException(io.pravega.common.auth.AuthenticationException) ReaderGroup(io.pravega.client.stream.ReaderGroup) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) InvalidStreamException(io.pravega.client.stream.InvalidStreamException)

Example 12 with ReaderGroup

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

the class StreamMetadataResourceImpl method listReaderGroups.

@Override
public void listReaderGroups(final String scopeName, final SecurityContext securityContext, final AsyncResponse asyncResponse) {
    long traceId = LoggerHelpers.traceEnter(log, "listReaderGroups");
    try {
        authenticate(scopeName, READ);
    } catch (AuthenticationException e) {
        log.warn("Get reader groups for {} failed due to authentication failure.", scopeName);
        asyncResponse.resume(Response.status(Status.UNAUTHORIZED).build());
        LoggerHelpers.traceLeave(log, "listReaderGroups", traceId);
        return;
    }
    // Each reader group is represented by a stream within the mentioned scope.
    controllerService.listStreamsInScope(scopeName).thenApply(streamsList -> {
        ReaderGroupsList readerGroups = new ReaderGroupsList();
        streamsList.forEach(stream -> {
            if (stream.getStreamName().startsWith(READER_GROUP_STREAM_PREFIX)) {
                ReaderGroupsListReaderGroups readerGroup = new ReaderGroupsListReaderGroups();
                readerGroup.setReaderGroupName(stream.getStreamName().substring(READER_GROUP_STREAM_PREFIX.length()));
                readerGroups.addReaderGroupsItem(readerGroup);
            }
        });
        log.info("Successfully fetched readerGroups for scope: {}", scopeName);
        return Response.status(Status.OK).entity(readerGroups).build();
    }).exceptionally(exception -> {
        if (exception.getCause() instanceof StoreException.DataNotFoundException || exception instanceof StoreException.DataNotFoundException) {
            log.warn("Scope name: {} not found", scopeName);
            return Response.status(Status.NOT_FOUND).build();
        } else {
            log.warn("listReaderGroups for {} failed with exception: ", scopeName, exception);
            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
        }
    }).thenApply(asyncResponse::resume).thenAccept(x -> LoggerHelpers.traceLeave(log, "listReaderGroups", 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) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) AuthenticationException(io.pravega.common.auth.AuthenticationException) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) StoreException(io.pravega.controller.store.stream.StoreException)

Example 13 with ReaderGroup

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

the class ReaderCheckpointTest method readerCheckpointTest.

@Test
public void readerCheckpointTest() {
    @Cleanup ReaderGroupManager readerGroupManager = ReaderGroupManager.withScope(SCOPE, controllerURI);
    ReaderGroup readerGroup = readerGroupManager.createReaderGroup(READER_GROUP_NAME, ReaderGroupConfig.builder().stream(io.pravega.client.stream.Stream.of(SCOPE, STREAM)).build());
    int startInclusive = 1;
    int endExclusive = 100;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(startInclusive, endExclusive);
    // initiate checkpoint100
    Checkpoint checkPoint100 = createCheckPointAndVerify(readerGroup, "batch100");
    // write and read events 100 to 200
    startInclusive = 100;
    endExclusive = 200;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(startInclusive, endExclusive);
    // reset to check point 100
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(checkPoint100).build());
    readEventsAndVerify(100, endExclusive);
    // initiate checkpoint200
    Checkpoint checkPoint200 = createCheckPointAndVerify(readerGroup, "batch200");
    // write and read events 200 to 300
    startInclusive = 200;
    endExclusive = 300;
    log.info("Write events with range [{},{})", startInclusive, endExclusive);
    writeEvents(IntStream.range(startInclusive, endExclusive).boxed().collect(Collectors.toList()));
    readEventsAndVerify(startInclusive, endExclusive);
    // reset back to checkpoint 200
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(checkPoint200).build());
    readEventsAndVerify(200, endExclusive);
    // reset back to checkpoint 100
    readerGroup.resetReaderGroup(ReaderGroupConfig.builder().startFromCheckpoint(checkPoint100).build());
    readEventsAndVerify(100, endExclusive);
    // clean up
    readerGroupManager.deleteReaderGroup(READER_GROUP_NAME);
}
Also used : ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Checkpoint(io.pravega.client.stream.Checkpoint) ReaderGroup(io.pravega.client.stream.ReaderGroup) Cleanup(lombok.Cleanup) Checkpoint(io.pravega.client.stream.Checkpoint) Test(org.junit.Test)

Aggregations

ReaderGroup (io.pravega.client.stream.ReaderGroup)13 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)11 Cleanup (lombok.Cleanup)9 Test (org.junit.Test)9 ClientFactory (io.pravega.client.ClientFactory)7 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)7 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)6 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)6 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)6 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)5 Stream (io.pravega.client.stream.Stream)5 Checkpoint (io.pravega.client.stream.Checkpoint)4 Controller (io.pravega.client.stream.impl.Controller)4 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)3 JavaSerializer (io.pravega.client.stream.impl.JavaSerializer)3 StreamImpl (io.pravega.client.stream.impl.StreamImpl)3 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)3 PravegaConnectionListener (io.pravega.segmentstore.server.host.handler.PravegaConnectionListener)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3