Search in sources :

Example 1 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ClientFactoryImpl method createRevisionedStreamClient.

@Override
public <T> RevisionedStreamClient<T> createRevisionedStreamClient(String streamName, Serializer<T> serializer, SynchronizerConfig config) {
    log.info("Creating revisioned stream client for stream: {} with synchronizer configuration: {}", streamName, config);
    Segment segment = new Segment(scope, streamName, 0);
    SegmentInputStream in = inFactory.createInputStreamForSegment(segment);
    // Segment sealed is not expected for Revisioned Stream Client.
    Consumer<Segment> segmentSealedCallBack = s -> {
        throw new IllegalStateException("RevisionedClient: Segmentsealed exception observed for segment:" + s);
    };
    String delegationToken = Futures.getAndHandleExceptions(controller.getOrRefreshDelegationTokenFor(segment.getScope(), segment.getStreamName()), RuntimeException::new);
    SegmentOutputStream out = outFactory.createOutputStreamForSegment(segment, segmentSealedCallBack, config.getEventWriterConfig(), delegationToken);
    SegmentMetadataClient meta = metaFactory.createSegmentMetadataClient(segment, delegationToken);
    return new RevisionedStreamClientImpl<>(segment, in, out, meta, serializer, controller, delegationToken);
}
Also used : StateSynchronizer(io.pravega.client.state.StateSynchronizer) Segment(io.pravega.client.segment.impl.Segment) EventStreamWriter(io.pravega.client.stream.EventStreamWriter) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) Supplier(java.util.function.Supplier) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) Update(io.pravega.client.state.Update) Stream(io.pravega.client.stream.Stream) RevisionedStreamClient(io.pravega.client.state.RevisionedStreamClient) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) SegmentInputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentInputStreamFactoryImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) EventWriterConfig(io.pravega.client.stream.EventWriterConfig) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) BatchClientImpl(io.pravega.client.batch.impl.BatchClientImpl) SegmentInputStreamFactory(io.pravega.client.segment.impl.SegmentInputStreamFactory) StateSynchronizerImpl(io.pravega.client.state.impl.StateSynchronizerImpl) lombok.val(lombok.val) EventStreamReader(io.pravega.client.stream.EventStreamReader) InitialUpdate(io.pravega.client.state.InitialUpdate) Revisioned(io.pravega.client.state.Revisioned) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) SegmentOutputStreamFactory(io.pravega.client.segment.impl.SegmentOutputStreamFactory) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) SegmentOutputStreamFactoryImpl(io.pravega.client.segment.impl.SegmentOutputStreamFactoryImpl) UpdateOrInitSerializer(io.pravega.client.state.impl.UpdateOrInitSerializer) ConnectionFactoryImpl(io.pravega.client.netty.impl.ConnectionFactoryImpl) ClientFactory(io.pravega.client.ClientFactory) BatchClient(io.pravega.client.batch.BatchClient) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ReaderConfig(io.pravega.client.stream.ReaderConfig) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Futures(io.pravega.common.concurrent.Futures) ClientConfig(io.pravega.client.ClientConfig) SegmentInputStream(io.pravega.client.segment.impl.SegmentInputStream) RevisionedStreamClientImpl(io.pravega.client.state.impl.RevisionedStreamClientImpl) SegmentOutputStream(io.pravega.client.segment.impl.SegmentOutputStream) Segment(io.pravega.client.segment.impl.Segment) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient)

Example 2 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ControllerImpl method convert.

private TxnSegments convert(CreateTxnResponse response) {
    NavigableMap<Double, Segment> rangeMap = new TreeMap<>();
    for (SegmentRange r : response.getActiveSegmentsList()) {
        rangeMap.put(r.getMaxKey(), ModelHelper.encode(r.getSegmentId()));
    }
    StreamSegments segments = new StreamSegments(rangeMap, response.getDelegationToken());
    return new TxnSegments(segments, ModelHelper.encode(response.getTxnId()));
}
Also used : SegmentRange(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentRange) TreeMap(java.util.TreeMap) Segment(io.pravega.client.segment.impl.Segment)

Example 3 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ControllerImpl method getKnownSegmentsInRange.

/*
     * This method fetches the segments of a stream which definitely reside between the segments represented by
     * lowerBound and the upperBound using the invariant that segment numbers monotonically increase.
     *
     * @param lowerBound StreamCut representing the segments of the starting point.
     * @param upperBound Segments representing the ending point.
     * @return Segments which reside between fromStreamCut and currentSegments.
     */
private List<Segment> getKnownSegmentsInRange(final StreamCut lowerBound, final Collection<Segment> upperBound) {
    int highestCut = lowerBound.asImpl().getPositions().keySet().stream().mapToInt(s -> s.getSegmentNumber()).max().getAsInt();
    int lowestCurrent = upperBound.stream().mapToInt(s -> s.getSegmentNumber()).min().getAsInt();
    if (highestCut >= lowestCurrent) {
        return Collections.emptyList();
    }
    final List<Segment> result = new ArrayList<>(lowestCurrent - highestCut);
    for (int num = highestCut + 1; num < lowestCurrent; num++) {
        result.add(new Segment(lowerBound.asImpl().getStream().getScope(), lowerBound.asImpl().getStream().getStreamName(), num));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Segment(io.pravega.client.segment.impl.Segment)

Example 4 with Segment

use of io.pravega.client.segment.impl.Segment in project pravega by pravega.

the class ControllerImpl method getSuccessors.

@Override
public CompletableFuture<StreamSegmentSuccessors> getSuccessors(final StreamCut from) {
    Exceptions.checkNotClosed(closed.get(), this);
    Stream stream = from.asImpl().getStream();
    long traceId = LoggerHelpers.traceEnter(log, "getSuccessorsFromCut", stream);
    val currentSegments = getAndHandleExceptions(getCurrentSegments(stream.getScope(), stream.getStreamName()), RuntimeException::new);
    String delegationToken = currentSegments.getDelegationToken();
    final Set<Segment> unread = getSegmentsInRange(from.asImpl(), currentSegments.getSegments());
    LoggerHelpers.traceLeave(log, "getSuccessorsFromCut", traceId);
    return CompletableFuture.completedFuture(new StreamSegmentSuccessors(unread, delegationToken));
}
Also used : lombok.val(lombok.val) StatusRuntimeException(io.grpc.StatusRuntimeException) Stream(io.pravega.client.stream.Stream) Segment(io.pravega.client.segment.impl.Segment)

Example 5 with Segment

use of io.pravega.client.segment.impl.Segment 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)

Aggregations

Segment (io.pravega.client.segment.impl.Segment)285 Test (org.junit.Test)200 Cleanup (lombok.Cleanup)125 HashMap (java.util.HashMap)79 Controller (io.pravega.client.control.impl.Controller)74 Stream (io.pravega.client.stream.Stream)69 MockController (io.pravega.client.stream.mock.MockController)67 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)57 StreamCut (io.pravega.client.stream.StreamCut)57 PravegaNodeUri (io.pravega.shared.protocol.netty.PravegaNodeUri)53 AtomicLong (java.util.concurrent.atomic.AtomicLong)53 ReaderGroupConfig (io.pravega.client.stream.ReaderGroupConfig)51 SegmentOutputStream (io.pravega.client.segment.impl.SegmentOutputStream)50 MockSegmentStreamFactory (io.pravega.client.stream.mock.MockSegmentStreamFactory)46 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)44 StreamCutImpl (io.pravega.client.stream.impl.StreamCutImpl)42 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)41 ArrayList (java.util.ArrayList)40 Map (java.util.Map)40 CompletableFuture (java.util.concurrent.CompletableFuture)37