Search in sources :

Example 1 with StreamSegmentRecord

use of io.pravega.controller.store.stream.records.StreamSegmentRecord in project pravega by pravega.

the class ControllerMetadataJsonSerializerTest method testStreamTruncationRecord.

@Test
public void testStreamTruncationRecord() {
    Map<StreamSegmentRecord, Integer> span = new HashMap<>();
    span.put(StreamSegmentRecord.newSegmentRecord(0, 0, 0L, 0.0, 1.0), 0);
    span.put(StreamSegmentRecord.newSegmentRecord(1, 0, 0L, 0.0, 1.0), 0);
    Map<Long, Long> streamCut = new HashMap<>();
    streamCut.put(0L, 0L);
    Set<Long> set = new HashSet<>();
    set.add(0L);
    StreamTruncationRecord record = new StreamTruncationRecord(ImmutableMap.copyOf(streamCut), ImmutableMap.copyOf(span), ImmutableSet.copyOf(set), ImmutableSet.copyOf(set), 0L, true);
    testRecordSerialization(record, StreamTruncationRecord.class);
}
Also used : StreamSegmentRecord(io.pravega.controller.store.stream.records.StreamSegmentRecord) StreamTruncationRecord(io.pravega.controller.store.stream.records.StreamTruncationRecord) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with StreamSegmentRecord

use of io.pravega.controller.store.stream.records.StreamSegmentRecord in project pravega by pravega.

the class ControllerService method createTransaction.

/**
 * Creates transaction where a new txn id is generated and the txn segments and metadata is created.
 *
 * @param scope scope
 * @param stream stream name
 * @param lease lease for transaction.
 * @param requestId request id
 * @return Transaction state future
 */
@SuppressWarnings("ReturnCount")
public CompletableFuture<Pair<UUID, List<SegmentRange>>> createTransaction(final String scope, final String stream, final long lease, final long requestId) {
    Exceptions.checkNotNullOrEmpty(scope, "scope");
    Exceptions.checkNotNullOrEmpty(stream, "stream");
    Timer timer = new Timer();
    OperationContext context = streamStore.createStreamContext(scope, stream, requestId);
    return streamStore.getConfiguration(scope, stream, context, executor).thenCompose(streamConfig -> streamTransactionMetadataTasks.createTxn(scope, stream, lease, requestId, streamConfig.getRolloverSizeBytes())).thenApply(pair -> {
        VersionedTransactionData data = pair.getKey();
        List<StreamSegmentRecord> segments = pair.getValue();
        return new ImmutablePair<>(data.getId(), getSegmentRanges(segments, scope, stream));
    }).handle((result, ex) -> {
        if (ex != null) {
            TransactionMetrics.getInstance().createTransactionFailed(scope, stream);
            throw new CompletionException(ex);
        }
        TransactionMetrics.getInstance().createTransaction(scope, stream, timer.getElapsed());
        return result;
    });
}
Also used : OperationContext(io.pravega.controller.store.stream.OperationContext) StreamSegmentRecord(io.pravega.controller.store.stream.records.StreamSegmentRecord) LoggerFactory(org.slf4j.LoggerFactory) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) KeyValueTableConfiguration(io.pravega.client.tables.KeyValueTableConfiguration) SecureRandom(java.security.SecureRandom) Cluster(io.pravega.common.cluster.Cluster) TagLogger(io.pravega.common.tracing.TagLogger) StoreException(io.pravega.controller.store.stream.StoreException) Pair(org.apache.commons.lang3.tuple.Pair) Duration(java.time.Duration) Map(java.util.Map) SubscribersResponse(io.pravega.controller.stream.api.grpc.v1.Controller.SubscribersResponse) Controller(io.pravega.controller.stream.api.grpc.v1.Controller) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) ImmutableMap(com.google.common.collect.ImmutableMap) CompletionException(java.util.concurrent.CompletionException) RequestTracker(io.pravega.common.tracing.RequestTracker) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) KVTableMetadataStore(io.pravega.controller.store.kvtable.KVTableMetadataStore) List(java.util.List) SegmentRecord(io.pravega.controller.store.SegmentRecord) VersionedTransactionData(io.pravega.controller.store.stream.VersionedTransactionData) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) PingTxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.PingTxnStatus) Futures(io.pravega.common.concurrent.Futures) SegmentId(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentId) OperationContext(io.pravega.controller.store.stream.OperationContext) StreamMetrics(io.pravega.controller.metrics.StreamMetrics) CreateReaderGroupResponse(io.pravega.controller.stream.api.grpc.v1.Controller.CreateReaderGroupResponse) TransactionMetrics(io.pravega.controller.metrics.TransactionMetrics) Getter(lombok.Getter) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) ModelHelper(io.pravega.client.control.impl.ModelHelper) Exceptions(io.pravega.common.Exceptions) KeyValueTableConfigResponse(io.pravega.controller.stream.api.grpc.v1.Controller.KeyValueTableConfigResponse) 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) UpdateSubscriberStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateSubscriberStatus) ArrayList(java.util.ArrayList) BucketStore(io.pravega.controller.store.stream.BucketStore) NodeUri(io.pravega.controller.stream.api.grpc.v1.Controller.NodeUri) DeleteKVTableStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteKVTableStatus) ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) ScaleResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ScaleResponse) UpdateReaderGroupResponse(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateReaderGroupResponse) NameUtils(io.pravega.shared.NameUtils) Executor(java.util.concurrent.Executor) Timer(io.pravega.common.Timer) SegmentRange(io.pravega.controller.stream.api.grpc.v1.Controller.SegmentRange) TableMetadataTasks(io.pravega.controller.task.KeyValueTable.TableMetadataTasks) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) StreamTransactionMetadataTasks(io.pravega.controller.task.Stream.StreamTransactionMetadataTasks) TxnStatus(io.pravega.controller.stream.api.grpc.v1.Controller.TxnStatus) ClusterException(io.pravega.common.cluster.ClusterException) Preconditions(com.google.common.base.Preconditions) State(io.pravega.controller.store.stream.State) DeleteReaderGroupStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteReaderGroupStatus) RandomFactory(io.pravega.common.hash.RandomFactory) CreateKeyValueTableStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateKeyValueTableStatus) AllArgsConstructor(lombok.AllArgsConstructor) Comparator(java.util.Comparator) ReaderGroupConfigResponse(io.pravega.controller.stream.api.grpc.v1.Controller.ReaderGroupConfigResponse) Timer(io.pravega.common.Timer) CompletionException(java.util.concurrent.CompletionException) List(java.util.List) ArrayList(java.util.ArrayList) VersionedTransactionData(io.pravega.controller.store.stream.VersionedTransactionData)

Example 3 with StreamSegmentRecord

use of io.pravega.controller.store.stream.records.StreamSegmentRecord in project pravega by pravega.

the class PersistentStreamBase method createHistoryRecords.

private CompletionStage<Void> createHistoryRecords(int startingSegmentNumber, CreateStreamResponse createStreamResponse, OperationContext context) {
    Preconditions.checkNotNull(context, "operation context cannot be null");
    final int numSegments = createStreamResponse.getConfiguration().getScalingPolicy().getMinNumSegments();
    // create epoch 0 record
    final double keyRangeChunk = 1.0 / numSegments;
    long creationTime = createStreamResponse.getTimestamp();
    final ImmutableList.Builder<StreamSegmentRecord> builder = ImmutableList.builder();
    IntStream.range(0, numSegments).boxed().forEach(x -> builder.add(newSegmentRecord(0, startingSegmentNumber + x, creationTime, x * keyRangeChunk, (x + 1) * keyRangeChunk)));
    EpochRecord epoch0 = new EpochRecord(0, 0, builder.build(), creationTime, 0L, 0L);
    return createEpochRecord(epoch0, context).thenCompose(r -> createHistoryChunk(epoch0, context)).thenCompose(r -> createSealedSegmentSizeMapShardIfAbsent(0, context)).thenCompose(r -> createRetentionSetDataIfAbsent(new RetentionSet(ImmutableList.of()), context)).thenCompose(r -> createCurrentEpochRecordDataIfAbsent(epoch0, context));
}
Also used : StreamSegmentRecord(io.pravega.controller.store.stream.records.StreamSegmentRecord) SneakyThrows(lombok.SneakyThrows) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) TagLogger(io.pravega.common.tracing.TagLogger) VersionedMetadata(io.pravega.controller.store.VersionedMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) DataNotFoundException(io.pravega.controller.store.stream.StoreException.DataNotFoundException) EpochTransitionRecord(io.pravega.controller.store.stream.records.EpochTransitionRecord) ImmutableSet(com.google.common.collect.ImmutableSet) StreamCutReferenceRecord(io.pravega.controller.store.stream.records.StreamCutReferenceRecord) StreamTruncationRecord(io.pravega.controller.store.stream.records.StreamTruncationRecord) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) ActiveTxnRecord(io.pravega.controller.store.stream.records.ActiveTxnRecord) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Optional(java.util.Optional) HistoryTimeSeries(io.pravega.controller.store.stream.records.HistoryTimeSeries) Futures(io.pravega.common.concurrent.Futures) IntStream(java.util.stream.IntStream) CompletedTxnRecord(io.pravega.controller.store.stream.records.CompletedTxnRecord) CommittingTransactionsRecord(io.pravega.controller.store.stream.records.CommittingTransactionsRecord) NameUtils.computeSegmentId(io.pravega.shared.NameUtils.computeSegmentId) Exceptions(io.pravega.common.Exceptions) HistoryTimeSeriesRecord(io.pravega.controller.store.stream.records.HistoryTimeSeriesRecord) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RetentionSet(io.pravega.controller.store.stream.records.RetentionSet) DATA_NOT_FOUND_PREDICATE(io.pravega.controller.store.stream.AbstractStreamMetadataStore.DATA_NOT_FOUND_PREDICATE) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) NameUtils.getSegmentNumber(io.pravega.shared.NameUtils.getSegmentNumber) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) StateRecord(io.pravega.controller.store.stream.records.StateRecord) StreamSubscriber(io.pravega.controller.store.stream.records.StreamSubscriber) LinkedList(java.util.LinkedList) RecordHelper(io.pravega.controller.store.stream.records.RecordHelper) SimpleEntry(java.util.AbstractMap.SimpleEntry) LongSummaryStatistics(java.util.LongSummaryStatistics) CollectionHelpers(io.pravega.common.util.CollectionHelpers) SealedSegmentsMapShard(io.pravega.controller.store.stream.records.SealedSegmentsMapShard) NameUtils(io.pravega.shared.NameUtils) Executor(java.util.concurrent.Executor) WriterMark(io.pravega.controller.store.stream.records.WriterMark) lombok.val(lombok.val) StreamCutRecord(io.pravega.controller.store.stream.records.StreamCutRecord) AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamConfigurationRecord(io.pravega.controller.store.stream.records.StreamConfigurationRecord) EpochRecord(io.pravega.controller.store.stream.records.EpochRecord) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Version(io.pravega.controller.store.Version) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Collections(java.util.Collections) StreamSegmentRecord(io.pravega.controller.store.stream.records.StreamSegmentRecord) EpochRecord(io.pravega.controller.store.stream.records.EpochRecord) ImmutableList(com.google.common.collect.ImmutableList) RetentionSet(io.pravega.controller.store.stream.records.RetentionSet)

Example 4 with StreamSegmentRecord

use of io.pravega.controller.store.stream.records.StreamSegmentRecord in project pravega by pravega.

the class PersistentStreamBase method computeTruncationRecord.

private CompletableFuture<StreamTruncationRecord> computeTruncationRecord(StreamTruncationRecord previous, Map<Long, Long> streamCut, ImmutableMap<StreamSegmentRecord, Integer> span, OperationContext context) {
    log.debug(context.getRequestId(), "computing truncation for stream {}/{}", scope, name);
    // find segments between "previous" stream cut and current stream cut. these are segments to delete.
    // Note: exclude segments in current streamcut
    CompletableFuture<Map<StreamSegmentRecord, Integer>> previousSpanFuture = previous.getSpan().isEmpty() ? getEpochRecord(0, context).thenApply(epoch -> convertToSpan(epoch)) : CompletableFuture.completedFuture(previous.getSpan());
    return previousSpanFuture.thenCompose(spanFrom -> segmentsBetweenStreamCutSpans(spanFrom, span, context)).thenCompose(segmentsBetween -> sizeBetweenStreamCuts(previous.getStreamCut(), streamCut, segmentsBetween, context).thenApply(sizeBetween -> {
        ImmutableSet.Builder<Long> builder = ImmutableSet.builder();
        segmentsBetween.stream().map(StreamSegmentRecord::segmentId).filter(x -> !streamCut.containsKey(x)).forEach(builder::add);
        return new StreamTruncationRecord(ImmutableMap.copyOf(streamCut), span, previous.getDeletedSegments(), builder.build(), previous.getSizeTill() + sizeBetween, true);
    }));
}
Also used : StreamSegmentRecord(io.pravega.controller.store.stream.records.StreamSegmentRecord) SneakyThrows(lombok.SneakyThrows) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) TagLogger(io.pravega.common.tracing.TagLogger) VersionedMetadata(io.pravega.controller.store.VersionedMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) DataNotFoundException(io.pravega.controller.store.stream.StoreException.DataNotFoundException) EpochTransitionRecord(io.pravega.controller.store.stream.records.EpochTransitionRecord) ImmutableSet(com.google.common.collect.ImmutableSet) StreamCutReferenceRecord(io.pravega.controller.store.stream.records.StreamCutReferenceRecord) StreamTruncationRecord(io.pravega.controller.store.stream.records.StreamTruncationRecord) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) ActiveTxnRecord(io.pravega.controller.store.stream.records.ActiveTxnRecord) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Optional(java.util.Optional) HistoryTimeSeries(io.pravega.controller.store.stream.records.HistoryTimeSeries) Futures(io.pravega.common.concurrent.Futures) IntStream(java.util.stream.IntStream) CompletedTxnRecord(io.pravega.controller.store.stream.records.CompletedTxnRecord) CommittingTransactionsRecord(io.pravega.controller.store.stream.records.CommittingTransactionsRecord) NameUtils.computeSegmentId(io.pravega.shared.NameUtils.computeSegmentId) Exceptions(io.pravega.common.Exceptions) HistoryTimeSeriesRecord(io.pravega.controller.store.stream.records.HistoryTimeSeriesRecord) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RetentionSet(io.pravega.controller.store.stream.records.RetentionSet) DATA_NOT_FOUND_PREDICATE(io.pravega.controller.store.stream.AbstractStreamMetadataStore.DATA_NOT_FOUND_PREDICATE) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) NameUtils.getSegmentNumber(io.pravega.shared.NameUtils.getSegmentNumber) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) StateRecord(io.pravega.controller.store.stream.records.StateRecord) StreamSubscriber(io.pravega.controller.store.stream.records.StreamSubscriber) LinkedList(java.util.LinkedList) RecordHelper(io.pravega.controller.store.stream.records.RecordHelper) SimpleEntry(java.util.AbstractMap.SimpleEntry) LongSummaryStatistics(java.util.LongSummaryStatistics) CollectionHelpers(io.pravega.common.util.CollectionHelpers) SealedSegmentsMapShard(io.pravega.controller.store.stream.records.SealedSegmentsMapShard) NameUtils(io.pravega.shared.NameUtils) Executor(java.util.concurrent.Executor) WriterMark(io.pravega.controller.store.stream.records.WriterMark) lombok.val(lombok.val) StreamCutRecord(io.pravega.controller.store.stream.records.StreamCutRecord) AtomicLong(java.util.concurrent.atomic.AtomicLong) StreamConfigurationRecord(io.pravega.controller.store.stream.records.StreamConfigurationRecord) EpochRecord(io.pravega.controller.store.stream.records.EpochRecord) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Version(io.pravega.controller.store.Version) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Collections(java.util.Collections) StreamTruncationRecord(io.pravega.controller.store.stream.records.StreamTruncationRecord) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 5 with StreamSegmentRecord

use of io.pravega.controller.store.stream.records.StreamSegmentRecord in project pravega by pravega.

the class PersistentStreamBase method startTruncation.

@Override
public CompletableFuture<Void> startTruncation(final Map<Long, Long> streamCut, OperationContext context) {
    Preconditions.checkNotNull(context, "operation context cannot be null");
    return getTruncationRecord(context).thenCompose(existing -> {
        Preconditions.checkNotNull(existing);
        Preconditions.checkArgument(!existing.getObject().isUpdating());
        long mostRecent = getMostRecent(streamCut);
        long oldest = getOldest(streamCut);
        int epochLow = NameUtils.getEpoch(oldest);
        int epochHigh = NameUtils.getEpoch(mostRecent);
        return fetchEpochs(epochLow, epochHigh, true, context).thenCompose(epochs -> {
            boolean isValid = isStreamCutValidInternal(streamCut, epochLow, epochs);
            Exceptions.checkArgument(isValid, "streamCut", "invalid stream cut");
            ImmutableMap<StreamSegmentRecord, Integer> span = computeStreamCutSpanInternal(streamCut, epochLow, epochHigh, epochs);
            StreamTruncationRecord previous = existing.getObject();
            // check greater than
            Exceptions.checkArgument(streamCutEqualOrAfter(streamCut, span, previous.getStreamCut(), previous.getSpan()), "StreamCut", "Supplied streamcut is behind previous truncation point");
            return computeTruncationRecord(previous, streamCut, span, context).thenCompose(prop -> Futures.toVoid(setTruncationData(new VersionedMetadata<>(prop, existing.getVersion()), context)));
        });
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamSegmentRecord(io.pravega.controller.store.stream.records.StreamSegmentRecord) StreamTruncationRecord(io.pravega.controller.store.stream.records.StreamTruncationRecord)

Aggregations

StreamSegmentRecord (io.pravega.controller.store.stream.records.StreamSegmentRecord)31 ArrayList (java.util.ArrayList)23 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)20 Futures (io.pravega.common.concurrent.Futures)20 HashMap (java.util.HashMap)20 List (java.util.List)20 Map (java.util.Map)20 UUID (java.util.UUID)20 Collectors (java.util.stream.Collectors)20 Exceptions (io.pravega.common.Exceptions)19 EpochRecord (io.pravega.controller.store.stream.records.EpochRecord)19 Lists (com.google.common.collect.Lists)18 VersionedMetadata (io.pravega.controller.store.VersionedMetadata)18 NameUtils (io.pravega.shared.NameUtils)18 Test (org.junit.Test)18 ImmutableMap (com.google.common.collect.ImmutableMap)17 EpochTransitionRecord (io.pravega.controller.store.stream.records.EpochTransitionRecord)17 StreamConfigurationRecord (io.pravega.controller.store.stream.records.StreamConfigurationRecord)17 StreamTruncationRecord (io.pravega.controller.store.stream.records.StreamTruncationRecord)17 NameUtils.computeSegmentId (io.pravega.shared.NameUtils.computeSegmentId)17