Search in sources :

Example 1 with StreamInfo

use of io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo in project pravega by pravega.

the class ControllerImpl method getSegmentsAtTime.

@Override
public CompletableFuture<Map<Segment, Long>> getSegmentsAtTime(final Stream stream, final long timestamp) {
    Exceptions.checkNotClosed(closed.get(), this);
    Preconditions.checkNotNull(stream, "stream");
    long traceId = LoggerHelpers.traceEnter(log, "getSegmentsAtTime", stream, timestamp);
    final CompletableFuture<SegmentsAtTime> result = this.retryConfig.runAsync(() -> {
        RPCAsyncCallback<SegmentsAtTime> callback = new RPCAsyncCallback<>();
        StreamInfo streamInfo = ModelHelper.createStreamInfo(stream.getScope(), stream.getStreamName());
        GetSegmentsRequest request = GetSegmentsRequest.newBuilder().setStreamInfo(streamInfo).setTimestamp(timestamp).build();
        client.getSegments(request, callback);
        return callback.getFuture();
    }, this.executor);
    return result.thenApply(segments -> {
        log.debug("Received the following data from the controller {}", segments.getSegmentsList());
        return segments.getSegmentsList().stream().collect(Collectors.toMap(location -> ModelHelper.encode(location.getSegmentId()), location -> location.getOffset()));
    }).whenComplete((x, e) -> {
        if (e != null) {
            log.warn("getSegmentsAtTime failed: ", e);
        }
        LoggerHelpers.traceLeave(log, "getSegmentsAtTime", traceId);
    });
}
Also used : StreamCut(io.pravega.client.stream.StreamCut) ManagedChannel(io.grpc.ManagedChannel) Retry(io.pravega.common.util.Retry) NegotiationType(io.grpc.netty.NegotiationType) MoreCallCredentials(io.grpc.auth.MoreCallCredentials) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) PingTxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnRequest) SegmentRanges(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentRanges) GetSegmentsRequest(io.pravega.controller.stream.api.grpc.v1.Controller.GetSegmentsRequest) StreamObserver(io.grpc.stub.StreamObserver) Stream(io.pravega.client.stream.Stream) Map(java.util.Map) Collectors.summarizingInt(java.util.stream.Collectors.summarizingInt) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) Transaction(io.pravega.client.stream.Transaction) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) Collection(java.util.Collection) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) NavigableMap(java.util.NavigableMap) Collectors(java.util.stream.Collectors) List(java.util.List) SSLException(javax.net.ssl.SSLException) Slf4j(lombok.extern.slf4j.Slf4j) CreateTxnResponse(io.pravega.controller.stream.api.grpc.v1.Controller.CreateTxnResponse) PingTxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnStatus) Futures(io.pravega.common.concurrent.Futures) Segment(io.pravega.client.segment.impl.Segment) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) ScaleStatusRequest(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleStatusRequest) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScaleStatusResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleStatusResponse) ScopeInfo(io.pravega.controller.stream.api.grpc.v1.Controller.ScopeInfo) TxnState(io.pravega.controller.stream.api.grpc.v1.Controller.TxnState) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) SuccessorResponse(io.pravega.controller.stream.api.grpc.v1.Controller.SuccessorResponse) PingFailedException(io.pravega.client.stream.PingFailedException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) NodeUri(io.pravega.controller.stream.api.grpc.v1.Controller.NodeUri) ScaleRequest(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleRequest) CreateTxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.CreateTxnRequest) StreamInfo(io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ControllerServiceGrpc(io.pravega.controller.stream.api.grpc.v1.ControllerServiceGrpc) ScaleResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleResponse) LoggerHelpers(io.pravega.common.LoggerHelpers) Futures.getAndHandleExceptions(io.pravega.common.concurrent.Futures.getAndHandleExceptions) lombok.val(lombok.val) SegmentRange(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentRange) File(java.io.File) StatusRuntimeException(io.grpc.StatusRuntimeException) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) TimeUnit(java.util.concurrent.TimeUnit) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) TreeMap(java.util.TreeMap) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) TxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.TxnStatus) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) Preconditions(com.google.common.base.Preconditions) RoundRobinLoadBalancerFactory(io.grpc.util.RoundRobinLoadBalancerFactory) SegmentsAtTime(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentsAtTime) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TxnFailedException(io.pravega.client.stream.TxnFailedException) TxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.TxnRequest) ArrayDeque(java.util.ArrayDeque) SegmentValidityResponse(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentValidityResponse) Collections(java.util.Collections) GrpcSslContexts(io.grpc.netty.GrpcSslContexts) GetSegmentsRequest(io.pravega.controller.stream.api.grpc.v1.Controller.GetSegmentsRequest) StreamInfo(io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo) SegmentsAtTime(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentsAtTime)

Example 2 with StreamInfo

use of io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo in project pravega by pravega.

the class ControllerImplTest method setup.

@Before
public void setup() throws IOException {
    // Setup test server generating different success and failure responses.
    testServerImpl = new ControllerServiceImplBase() {

        @Override
        public void createStream(StreamConfig request, StreamObserver<CreateStreamStatus> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1") || request.getStreamInfo().getStream().equals("stream8")) {
                responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream3")) {
                responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.SCOPE_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream4")) {
                responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.STREAM_EXISTS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream5")) {
                responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.INVALID_STREAM_NAME).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("streamparallel")) {
                // Simulating delay in sending response.
                Exceptions.handleInterrupted(() -> Thread.sleep(500));
                responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("streamdelayed")) {
                // Simulating delay in sending response. This is used for the keepalive test,
                // where response time > 30 seconds is required to simulate a failure.
                Exceptions.handleInterrupted(() -> Thread.sleep(40000));
                responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("streamretryfailure")) {
                responseObserver.onError(Status.UNKNOWN.withDescription("Transport error").asRuntimeException());
            } else if (request.getStreamInfo().getStream().equals("streamretrysuccess")) {
                if (retryAttempts.incrementAndGet() > 3) {
                    responseObserver.onNext(CreateStreamStatus.newBuilder().setStatus(CreateStreamStatus.Status.SUCCESS).build());
                    responseObserver.onCompleted();
                } else {
                    responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
                }
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void updateStream(StreamConfig request, StreamObserver<UpdateStreamStatus> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream3")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.SCOPE_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream4")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.STREAM_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream5")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.UNRECOGNIZED).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void truncateStream(Controller.StreamCut request, StreamObserver<UpdateStreamStatus> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream3")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.SCOPE_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream4")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.STREAM_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream5")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.UNRECOGNIZED).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void sealStream(StreamInfo request, StreamObserver<UpdateStreamStatus> responseObserver) {
            if (request.getStream().equals("stream1")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("stream2")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("stream3")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.SCOPE_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("stream4")) {
                responseObserver.onNext(UpdateStreamStatus.newBuilder().setStatus(UpdateStreamStatus.Status.STREAM_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void deleteStream(StreamInfo request, StreamObserver<DeleteStreamStatus> responseObserver) {
            if (request.getStream().equals("stream1")) {
                responseObserver.onNext(DeleteStreamStatus.newBuilder().setStatus(DeleteStreamStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("stream2")) {
                responseObserver.onNext(DeleteStreamStatus.newBuilder().setStatus(DeleteStreamStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("stream3")) {
                responseObserver.onNext(DeleteStreamStatus.newBuilder().setStatus(DeleteStreamStatus.Status.STREAM_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("stream4")) {
                responseObserver.onNext(DeleteStreamStatus.newBuilder().setStatus(DeleteStreamStatus.Status.STREAM_NOT_SEALED).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void getCurrentSegments(StreamInfo request, StreamObserver<SegmentRanges> responseObserver) {
            if (request.getStream().equals("stream1")) {
                responseObserver.onNext(SegmentRanges.newBuilder().addSegmentRanges(ModelHelper.createSegmentRange("scope1", "stream1", 6, 0.0, 0.4)).addSegmentRanges(ModelHelper.createSegmentRange("scope1", "stream1", 7, 0.4, 1.0)).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("stream8")) {
                responseObserver.onNext(SegmentRanges.newBuilder().addSegmentRanges(ModelHelper.createSegmentRange("scope1", "stream8", 9, 0.0, 0.5)).addSegmentRanges(ModelHelper.createSegmentRange("scope1", "stream8", 10, 0.5, 1.0)).build());
                responseObserver.onCompleted();
            } else if (request.getStream().equals("streamparallel")) {
                Exceptions.handleInterrupted(() -> Thread.sleep(500));
                responseObserver.onNext(SegmentRanges.newBuilder().addSegmentRanges(ModelHelper.createSegmentRange("scope1", "streamparallel", 0, 0.0, 0.4)).addSegmentRanges(ModelHelper.createSegmentRange("scope1", "streamparallel", 1, 0.4, 1.0)).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void getSegments(GetSegmentsRequest request, StreamObserver<SegmentsAtTime> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                SegmentId segment1 = ModelHelper.createSegmentId("scope1", "stream1", 0);
                SegmentId segment2 = ModelHelper.createSegmentId("scope1", "stream1", 1);
                responseObserver.onNext(SegmentsAtTime.newBuilder().addSegments(SegmentLocation.newBuilder().setSegmentId(segment1).setOffset(10).build()).addSegments(SegmentLocation.newBuilder().setSegmentId(segment2).setOffset(20).build()).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream8")) {
                SegmentId segment1 = ModelHelper.createSegmentId("scope1", "stream8", 0);
                SegmentId segment2 = ModelHelper.createSegmentId("scope1", "stream8", 1);
                SegmentId segment3 = ModelHelper.createSegmentId("scope1", "stream8", 2);
                responseObserver.onNext(SegmentsAtTime.newBuilder().addSegments(SegmentLocation.newBuilder().setSegmentId(segment1).setOffset(10).build()).addSegments(SegmentLocation.newBuilder().setSegmentId(segment2).setOffset(20).build()).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void getSegmentsImmediatlyFollowing(SegmentId request, StreamObserver<SuccessorResponse> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                Map<SegmentId, Pair<Double, Double>> result = new HashMap<>();
                if (request.getSegmentNumber() == 0) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream1", 2), Pair.of(0.0, 0.25));
                    result.put(ModelHelper.createSegmentId("scope1", "stream1", 3), Pair.of(0.25, 0.5));
                } else if (request.getSegmentNumber() == 1) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream1", 4), Pair.of(0.5, 0.75));
                    result.put(ModelHelper.createSegmentId("scope1", "stream1", 5), Pair.of(0.75, 1.0));
                } else if (request.getSegmentNumber() == 2 || request.getSegmentNumber() == 3) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream1", 6), Pair.of(0.0, 0.5));
                } else if (request.getSegmentNumber() == 4 || request.getSegmentNumber() == 5) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream1", 7), Pair.of(0.5, 0.25));
                }
                val builder = SuccessorResponse.newBuilder();
                for (Entry<SegmentId, Pair<Double, Double>> entry : result.entrySet()) {
                    builder.addSegments(SuccessorResponse.SegmentEntry.newBuilder().setSegment(Controller.SegmentRange.newBuilder().setSegmentId(entry.getKey()).setMinKey(entry.getValue().getLeft()).setMaxKey(entry.getValue().getRight()).build()).addValue(10 * entry.getKey().getSegmentNumber()).build());
                }
                responseObserver.onNext(builder.build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream8")) {
                Map<SegmentId, Pair<Double, Double>> result = new HashMap<>();
                if (request.getSegmentNumber() == 0) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 3), Pair.of(0.0, 0.2));
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 4), Pair.of(0.2, 0.33));
                } else if (request.getSegmentNumber() == 1) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 5), Pair.of(0.33, 0.5));
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 6), Pair.of(0.5, 0.66));
                } else if (request.getSegmentNumber() == 2) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 7), Pair.of(0.66, 0.8));
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 8), Pair.of(0.8, 1.0));
                } else if (request.getSegmentNumber() == 3 || request.getSegmentNumber() == 4 || request.getSegmentNumber() == 5) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 9), Pair.of(0.0, 0.5));
                } else if (request.getSegmentNumber() == 6 || request.getSegmentNumber() == 7 || request.getSegmentNumber() == 8) {
                    result.put(ModelHelper.createSegmentId("scope1", "stream8", 10), Pair.of(0.5, 1.0));
                }
                val builder = SuccessorResponse.newBuilder();
                for (Entry<SegmentId, Pair<Double, Double>> entry : result.entrySet()) {
                    builder.addSegments(SuccessorResponse.SegmentEntry.newBuilder().setSegment(Controller.SegmentRange.newBuilder().setSegmentId(entry.getKey()).setMinKey(entry.getValue().getLeft()).setMaxKey(entry.getValue().getRight()).build()).addValue(10 * entry.getKey().getSegmentNumber()).build());
                }
                responseObserver.onNext(builder.build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void scale(ScaleRequest request, StreamObserver<ScaleResponse> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(ScaleResponse.newBuilder().setStatus(ScaleResponse.ScaleStreamStatus.STARTED).addSegments(ModelHelper.createSegmentRange("scope1", "stream1", 0, 0.0, 0.5)).addSegments(ModelHelper.createSegmentRange("scope1", "stream1", 1, 0.5, 1.0)).setEpoch(0).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void checkScale(ScaleStatusRequest request, StreamObserver<ScaleStatusResponse> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(ScaleStatusResponse.newBuilder().setStatus(ScaleStatusResponse.ScaleStatus.SUCCESS).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void getURI(SegmentId request, StreamObserver<NodeUri> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(NodeUri.newBuilder().setEndpoint("localhost").setPort(SERVICE_PORT).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void isSegmentValid(SegmentId request, StreamObserver<SegmentValidityResponse> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(SegmentValidityResponse.newBuilder().setResponse(true).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                responseObserver.onNext(SegmentValidityResponse.newBuilder().setResponse(false).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void createTransaction(CreateTxnRequest request, StreamObserver<Controller.CreateTxnResponse> responseObserver) {
            Controller.CreateTxnResponse.Builder builder = Controller.CreateTxnResponse.newBuilder();
            if (request.getStreamInfo().getStream().equals("stream1")) {
                builder.setTxnId(TxnId.newBuilder().setHighBits(11L).setLowBits(22L).build());
                builder.addActiveSegments(ModelHelper.createSegmentRange("scope1", "stream1", 0, 0.0, 0.5));
                builder.addActiveSegments(ModelHelper.createSegmentRange("scope1", "stream1", 1, 0.5, 1.0));
                responseObserver.onNext(builder.build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                builder.addActiveSegments(ModelHelper.createSegmentRange("scope1", "stream2", 0, 0.0, 1.0));
                builder.setTxnId(TxnId.newBuilder().setHighBits(33L).setLowBits(44L).build());
                responseObserver.onNext(builder.build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void commitTransaction(TxnRequest request, StreamObserver<Controller.TxnStatus> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream3")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.STREAM_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream4")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.TRANSACTION_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void abortTransaction(TxnRequest request, StreamObserver<Controller.TxnStatus> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream3")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.STREAM_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream4")) {
                responseObserver.onNext(Controller.TxnStatus.newBuilder().setStatus(Controller.TxnStatus.Status.TRANSACTION_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void pingTransaction(PingTxnRequest request, StreamObserver<PingTxnStatus> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(PingTxnStatus.newBuilder().setStatus(PingTxnStatus.Status.OK).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void checkTransactionState(TxnRequest request, StreamObserver<TxnState> responseObserver) {
            if (request.getStreamInfo().getStream().equals("stream1")) {
                responseObserver.onNext(TxnState.newBuilder().setState(TxnState.State.OPEN).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream2")) {
                responseObserver.onNext(TxnState.newBuilder().setState(TxnState.State.UNKNOWN).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream3")) {
                responseObserver.onNext(TxnState.newBuilder().setState(TxnState.State.COMMITTING).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream4")) {
                responseObserver.onNext(TxnState.newBuilder().setState(TxnState.State.COMMITTED).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream5")) {
                responseObserver.onNext(TxnState.newBuilder().setState(TxnState.State.ABORTING).build());
                responseObserver.onCompleted();
            } else if (request.getStreamInfo().getStream().equals("stream6")) {
                responseObserver.onNext(TxnState.newBuilder().setState(TxnState.State.ABORTED).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void createScope(ScopeInfo request, StreamObserver<CreateScopeStatus> responseObserver) {
            if (request.getScope().equals("scope1")) {
                responseObserver.onNext(CreateScopeStatus.newBuilder().setStatus(CreateScopeStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getScope().equals("scope2")) {
                responseObserver.onNext(CreateScopeStatus.newBuilder().setStatus(CreateScopeStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getScope().equals("scope3")) {
                responseObserver.onNext(CreateScopeStatus.newBuilder().setStatus(CreateScopeStatus.Status.INVALID_SCOPE_NAME).build());
                responseObserver.onCompleted();
            } else if (request.getScope().equals("scope4")) {
                responseObserver.onNext(CreateScopeStatus.newBuilder().setStatus(CreateScopeStatus.Status.SCOPE_EXISTS).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }

        @Override
        public void getDelegationToken(io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo request, io.grpc.stub.StreamObserver<io.pravega.controller.stream.api.grpc.v1.Controller.DelegationToken> responseObserver) {
            responseObserver.onNext(Controller.DelegationToken.newBuilder().setDelegationToken("token").build());
            responseObserver.onCompleted();
        }

        @Override
        public void deleteScope(ScopeInfo request, StreamObserver<DeleteScopeStatus> responseObserver) {
            if (request.getScope().equals("scope1")) {
                responseObserver.onNext(DeleteScopeStatus.newBuilder().setStatus(DeleteScopeStatus.Status.SUCCESS).build());
                responseObserver.onCompleted();
            } else if (request.getScope().equals("scope2")) {
                responseObserver.onNext(DeleteScopeStatus.newBuilder().setStatus(DeleteScopeStatus.Status.FAILURE).build());
                responseObserver.onCompleted();
            } else if (request.getScope().equals("scope3")) {
                responseObserver.onNext(DeleteScopeStatus.newBuilder().setStatus(DeleteScopeStatus.Status.SCOPE_NOT_EMPTY).build());
                responseObserver.onCompleted();
            } else if (request.getScope().equals("scope4")) {
                responseObserver.onNext(DeleteScopeStatus.newBuilder().setStatus(DeleteScopeStatus.Status.SCOPE_NOT_FOUND).build());
                responseObserver.onCompleted();
            } else {
                responseObserver.onError(Status.INTERNAL.withDescription("Server error").asRuntimeException());
            }
        }
    };
    serverPort = TestUtils.getAvailableListenPort();
    serverBuilder = NettyServerBuilder.forPort(serverPort).addService(testServerImpl);
    if (testSecure) {
        serverBuilder = serverBuilder.useTransportSecurity(new File("../config/cert.pem"), new File("../config/key.pem"));
        creds = new DefaultCredentials("1111_aaaa", "admin");
    }
    testGRPCServer = serverBuilder.build().start();
    executor = Executors.newSingleThreadScheduledExecutor();
    controllerClient = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).credentials(new DefaultCredentials("1111_aaaa", "admin")).trustStore("../config/cert.pem").build()).retryAttempts(1).build(), executor);
}
Also used : ScaleStatusRequest(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleStatusRequest) HashMap(java.util.HashMap) NettyServerBuilder(io.grpc.netty.NettyServerBuilder) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) PingTxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnRequest) CreateTxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.CreateTxnRequest) TxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.TxnRequest) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) SuccessorResponse(io.pravega.controller.stream.api.grpc.v1.Controller.SuccessorResponse) ScaleRequest(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleRequest) lombok.val(lombok.val) ScaleStatusResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleStatusResponse) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) NodeUri(io.pravega.controller.stream.api.grpc.v1.Controller.NodeUri) PingTxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnRequest) TxnState(io.pravega.controller.stream.api.grpc.v1.Controller.TxnState) SegmentRanges(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentRanges) GetSegmentsRequest(io.pravega.controller.stream.api.grpc.v1.Controller.GetSegmentsRequest) CreateTxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.CreateTxnRequest) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) SegmentsAtTime(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentsAtTime) SegmentValidityResponse(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentValidityResponse) PingTxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnStatus) Entry(java.util.Map.Entry) StreamInfo(io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo) Pair(org.apache.commons.lang3.tuple.Pair) ControllerServiceImplBase(io.pravega.controller.stream.api.grpc.v1.ControllerServiceGrpc.ControllerServiceImplBase) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) SegmentId(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentId) StreamConfig(io.pravega.controller.stream.api.grpc.v1.Controller.StreamConfig) ScaleResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleResponse) ScopeInfo(io.pravega.controller.stream.api.grpc.v1.Controller.ScopeInfo) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) StreamInfo(io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo) PingTxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnStatus) Before(org.junit.Before)

Example 3 with StreamInfo

use of io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo in project pravega by pravega.

the class ControllerServiceImplTest method createTransactionFailureTest.

@Test
public void createTransactionFailureTest() {
    createScopeAndStream(SCOPE1, STREAM1, ScalingPolicy.fixed(4));
    StreamInfo streamInfo = ModelHelper.createStreamInfo(SCOPE1, STREAM1);
    // Invalid lease
    CreateTxnRequest request = CreateTxnRequest.newBuilder().setStreamInfo(streamInfo).setLease(-1).setScaleGracePeriod(10000).build();
    ResultObserver<CreateTxnResponse> resultObserver = new ResultObserver<>();
    this.controllerService.createTransaction(request, resultObserver);
    AssertExtensions.assertThrows("Lease lower bound violated ", resultObserver::get, e -> checkGRPCException(e, IllegalArgumentException.class));
    // Invalid ScaleGracePeriod
    request = CreateTxnRequest.newBuilder().setStreamInfo(streamInfo).setLease(10000).setScaleGracePeriod(-1).build();
    ResultObserver<CreateTxnResponse> resultObserver3 = new ResultObserver<>();
    this.controllerService.createTransaction(request, resultObserver3);
    AssertExtensions.assertThrows("Lease lower bound violated ", resultObserver3::get, e -> checkGRPCException(e, IllegalArgumentException.class));
}
Also used : CreateTxnResponse(io.pravega.controller.stream.api.grpc.v1.Controller.CreateTxnResponse) CreateTxnRequest(io.pravega.controller.stream.api.grpc.v1.Controller.CreateTxnRequest) StreamInfo(io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo) Test(org.junit.Test)

Example 4 with StreamInfo

use of io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo in project pravega by pravega.

the class SegmentHelper method getSegmentInfo.

public CompletableFuture<WireCommands.StreamSegmentInfo> getSegmentInfo(String scope, String stream, int segmentNumber, HostControllerStore hostControllerStore, ConnectionFactory clientCF, String delegationToken) {
    final CompletableFuture<WireCommands.StreamSegmentInfo> result = new CompletableFuture<>();
    final Controller.NodeUri uri = getSegmentUri(scope, stream, segmentNumber, hostControllerStore);
    final WireCommandType type = WireCommandType.GET_STREAM_SEGMENT_INFO;
    final FailingReplyProcessor replyProcessor = new FailingReplyProcessor() {

        @Override
        public void connectionDropped() {
            log.warn("getSegmentInfo {}/{}/{} connectionDropped", scope, stream, segmentNumber);
            result.completeExceptionally(new WireCommandFailedException(type, WireCommandFailedException.Reason.ConnectionDropped));
        }

        @Override
        public void wrongHost(WireCommands.WrongHost wrongHost) {
            log.warn("getSegmentInfo {}/{}/{} WrongHost", scope, stream, segmentNumber);
            result.completeExceptionally(new WireCommandFailedException(type, WireCommandFailedException.Reason.UnknownHost));
        }

        @Override
        public void streamSegmentInfo(WireCommands.StreamSegmentInfo streamInfo) {
            log.info("getSegmentInfo {}/{}/{} got response", scope, stream, segmentNumber);
            result.complete(streamInfo);
        }

        @Override
        public void processingFailure(Exception error) {
            log.error("getSegmentInfo {}/{}/{} failed", scope, stream, segmentNumber, error);
            result.completeExceptionally(error);
        }

        @Override
        public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
            result.completeExceptionally(new WireCommandFailedException(new AuthenticationException(authTokenCheckFailed.toString()), type, WireCommandFailedException.Reason.AuthFailed));
        }
    };
    WireCommands.GetStreamSegmentInfo request = new WireCommands.GetStreamSegmentInfo(idGenerator.get(), Segment.getScopedName(scope, stream, segmentNumber), delegationToken);
    sendRequestAsync(request, replyProcessor, result, clientCF, ModelHelper.encode(uri));
    return result;
}
Also used : AuthenticationException(io.pravega.common.auth.AuthenticationException) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) WireCommandType(io.pravega.shared.protocol.netty.WireCommandType) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) AuthenticationException(io.pravega.common.auth.AuthenticationException) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) CompletableFuture(java.util.concurrent.CompletableFuture) WireCommands(io.pravega.shared.protocol.netty.WireCommands)

Aggregations

CreateTxnRequest (io.pravega.controller.stream.api.grpc.v1.Controller.CreateTxnRequest)3 StreamInfo (io.pravega.controller.stream.api.grpc.v1.Controller.StreamInfo)3 NettyChannelBuilder (io.grpc.netty.NettyChannelBuilder)2 CreateScopeStatus (io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus)2 CreateStreamStatus (io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus)2 DeleteScopeStatus (io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus)2 DeleteStreamStatus (io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus)2 GetSegmentsRequest (io.pravega.controller.stream.api.grpc.v1.Controller.GetSegmentsRequest)2 NodeUri (io.pravega.controller.stream.api.grpc.v1.Controller.NodeUri)2 PingTxnRequest (io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnRequest)2 PingTxnStatus (io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnStatus)2 ScaleRequest (io.pravega.controller.stream.api.grpc.v1.Controller.ScaleRequest)2 ScaleResponse (io.pravega.controller.stream.api.grpc.v1.Controller.ScaleResponse)2 ScaleStatusRequest (io.pravega.controller.stream.api.grpc.v1.Controller.ScaleStatusRequest)2 ScaleStatusResponse (io.pravega.controller.stream.api.grpc.v1.Controller.ScaleStatusResponse)2 ScopeInfo (io.pravega.controller.stream.api.grpc.v1.Controller.ScopeInfo)2 SegmentRanges (io.pravega.controller.stream.api.grpc.v1.Controller.SegmentRanges)2 SegmentValidityResponse (io.pravega.controller.stream.api.grpc.v1.Controller.SegmentValidityResponse)2 SegmentsAtTime (io.pravega.controller.stream.api.grpc.v1.Controller.SegmentsAtTime)2 SuccessorResponse (io.pravega.controller.stream.api.grpc.v1.Controller.SuccessorResponse)2