Search in sources :

Example 11 with Segment

use of io.pravega.controller.store.stream.Segment in project pravega by pravega.

the class TableHelper method getSizeTillStreamCut.

/**
 * A method to compute size of stream in bytes from start till given stream cut.
 * Note: this computed size is absolute size and even if the stream has been truncated, this size is computed for the
 * entire amount of data that was written into the stream.
 *
 * @param indexTable index table for the stream
 * @param historyTable history table for the stream
 * @param segmentTable segment table for the stream
 * @param streamCut stream cut to compute size till
 * @param sealedSegmentsRecord record for all the sealed segments for the given stream.
 * @return size (in bytes) of stream till the given stream cut.
 */
public static long getSizeTillStreamCut(final byte[] indexTable, final byte[] historyTable, final byte[] segmentTable, final Map<Integer, Long> streamCut, final SealedSegmentsRecord sealedSegmentsRecord) {
    Preconditions.checkNotNull(streamCut);
    Preconditions.checkNotNull(indexTable);
    Preconditions.checkNotNull(historyTable);
    Preconditions.checkNotNull(sealedSegmentsRecord);
    Preconditions.checkNotNull(segmentTable);
    Preconditions.checkArgument(!streamCut.isEmpty());
    Map<Integer, Integer> epochCutMap = computeEpochCutMap(historyTable, indexTable, segmentTable, streamCut);
    Map<Segment, Integer> cutMapSegments = transform(segmentTable, epochCutMap);
    AtomicLong size = new AtomicLong();
    Map<Integer, Long> sealedSegmentSizeMap = sealedSegmentsRecord.getSealedSegmentsSizeMap();
    // add sizes for segments in stream cut
    streamCut.forEach((key, value) -> size.addAndGet(value));
    int highestEpoch = epochCutMap.values().stream().max(Comparator.naturalOrder()).orElse(Integer.MIN_VALUE);
    Optional<HistoryRecord> historyRecordOpt = HistoryRecord.readRecord(historyTable, 0, true);
    // start with epoch 0 and go all the way upto epochCutMap.highEpoch
    while (historyRecordOpt.isPresent() && historyRecordOpt.get().getEpoch() <= highestEpoch) {
        HistoryRecord historyRecord = historyRecordOpt.get();
        int epoch = historyRecord.getEpoch();
        size.addAndGet(historyRecord.getSegments().stream().filter(epochSegmentNumber -> {
            Segment epochSegment = getSegment(epochSegmentNumber, segmentTable);
            return cutMapSegments.entrySet().stream().noneMatch(cutSegment -> cutSegment.getKey().getNumber() == epochSegment.getNumber() || (cutSegment.getKey().overlaps(epochSegment) && cutSegment.getValue() <= epoch));
        }).map(sealedSegmentSizeMap::get).reduce((x, y) -> x + y).orElse(0L));
        historyRecordOpt = HistoryRecord.fetchNext(historyRecord, historyTable, true);
    }
    return size.get();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Segment(io.pravega.controller.store.stream.Segment)

Example 12 with Segment

use of io.pravega.controller.store.stream.Segment in project pravega by pravega.

the class TableHelper method computeToDelete.

private static Set<Integer> computeToDelete(Map<Segment, Integer> epochCutMap, byte[] historyTable, byte[] segmentTable, Set<Integer> deletedSegments) {
    Set<Integer> toDelete = new HashSet<>();
    int highestEpoch = epochCutMap.values().stream().max(Comparator.naturalOrder()).orElse(Integer.MIN_VALUE);
    Optional<HistoryRecord> historyRecordOpt = HistoryRecord.readRecord(historyTable, 0, true);
    // start with epoch 0 and go all the way upto epochCutMap.highEpoch
    while (historyRecordOpt.isPresent() && historyRecordOpt.get().getEpoch() <= highestEpoch) {
        HistoryRecord historyRecord = historyRecordOpt.get();
        int epoch = historyRecord.getEpoch();
        toDelete.addAll(historyRecord.getSegments().stream().filter(epochSegmentNumber -> {
            Segment epochSegment = getSegment(epochSegmentNumber, segmentTable);
            // toDelete.add(epoch.segment overlaps cut.segment && epoch < cut.segment.epoch)
            return !deletedSegments.contains(epochSegmentNumber) && epochCutMap.entrySet().stream().noneMatch(cutSegment -> cutSegment.getKey().getNumber() == epochSegment.getNumber() || (cutSegment.getKey().overlaps(epochSegment) && cutSegment.getValue() <= epoch));
        }).collect(Collectors.toSet()));
        historyRecordOpt = HistoryRecord.fetchNext(historyRecord, historyTable, true);
    }
    return toDelete;
}
Also used : IntStream(java.util.stream.IntStream) ImmutableSet(com.google.common.collect.ImmutableSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImmutableMap(com.google.common.collect.ImmutableMap) Exceptions(io.pravega.common.Exceptions) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractMap(java.util.AbstractMap) List(java.util.List) Lists(com.google.common.collect.Lists) StoreException(io.pravega.controller.store.stream.StoreException) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Segment(io.pravega.controller.store.stream.Segment) Comparator(java.util.Comparator) Collections(java.util.Collections) Segment(io.pravega.controller.store.stream.Segment) HashSet(java.util.HashSet)

Example 13 with Segment

use of io.pravega.controller.store.stream.Segment in project pravega by pravega.

the class ControllerService method getSegmentsImmediatelyFollowing.

public CompletableFuture<Map<SegmentRange, List<Integer>>> getSegmentsImmediatelyFollowing(SegmentId segment) {
    Preconditions.checkNotNull(segment, "segment");
    OperationContext context = streamStore.createContext(segment.getStreamInfo().getScope(), segment.getStreamInfo().getStream());
    return streamStore.getSuccessors(segment.getStreamInfo().getScope(), segment.getStreamInfo().getStream(), segment.getSegmentNumber(), context, executor).thenComposeAsync(successors -> Futures.keysAllOfWithResults(successors.entrySet().stream().collect(Collectors.toMap(entry -> streamStore.getSegment(segment.getStreamInfo().getScope(), segment.getStreamInfo().getStream(), entry.getKey(), context, executor).thenApply(seg -> ModelHelper.createSegmentRange(segment.getStreamInfo().getScope(), segment.getStreamInfo().getStream(), seg.getNumber(), seg.getKeyStart(), seg.getKeyEnd())), Map.Entry::getValue))), executor);
}
Also used : OperationContext(io.pravega.controller.store.stream.OperationContext) SegmentId(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentId) OperationContext(io.pravega.controller.store.stream.OperationContext) Getter(lombok.Getter) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) Exceptions(io.pravega.common.Exceptions) ScaleStatusResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleStatusResponse) TxnState(io.pravega.controller.stream.api.grpc.v1.Controller.TxnState) CompletableFuture(java.util.concurrent.CompletableFuture) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ArrayList(java.util.ArrayList) NodeUri(io.pravega.controller.stream.api.grpc.v1.Controller.NodeUri) Cluster(io.pravega.common.cluster.Cluster) ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) Pair(org.apache.commons.lang3.tuple.Pair) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) Map(java.util.Map) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) Segment(io.pravega.controller.store.stream.Segment) ModelHelper(io.pravega.client.stream.impl.ModelHelper) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) ScaleResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleResponse) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) NameUtils(io.pravega.shared.NameUtils) Executor(java.util.concurrent.Executor) UUID(java.util.UUID) SegmentRange(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentRange) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) TxnId(io.pravega.controller.stream.api.grpc.v1.Controller.TxnId) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) StreamTransactionMetadataTasks(io.pravega.controller.task.Stream.StreamTransactionMetadataTasks) VersionedTransactionData(io.pravega.controller.store.stream.VersionedTransactionData) TxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.TxnStatus) ClusterException(io.pravega.common.cluster.ClusterException) Preconditions(com.google.common.base.Preconditions) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) Comparator(java.util.Comparator) AllArgsConstructor(lombok.AllArgsConstructor) Lombok(lombok.Lombok) PingTxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnStatus) Futures(io.pravega.common.concurrent.Futures) Map(java.util.Map)

Aggregations

Segment (io.pravega.controller.store.stream.Segment)13 List (java.util.List)10 ArrayList (java.util.ArrayList)8 HostControllerStore (io.pravega.controller.store.host.HostControllerStore)7 StreamMetadataStore (io.pravega.controller.store.stream.StreamMetadataStore)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 Collectors (java.util.stream.Collectors)7 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)6 SegmentHelper (io.pravega.controller.server.SegmentHelper)6 AbstractMap (java.util.AbstractMap)6 UUID (java.util.UUID)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 ClientConfig (io.pravega.client.ClientConfig)5 ConnectionFactoryImpl (io.pravega.client.netty.impl.ConnectionFactoryImpl)5 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)5 Futures (io.pravega.common.concurrent.Futures)5 SegmentHelperMock (io.pravega.controller.mocks.SegmentHelperMock)5 HostStoreFactory (io.pravega.controller.store.host.HostStoreFactory)5 HostMonitorConfigImpl (io.pravega.controller.store.host.impl.HostMonitorConfigImpl)5 StreamStoreFactory (io.pravega.controller.store.stream.StreamStoreFactory)5