Search in sources :

Example 6 with Preconditions

use of com.google.common.base.Preconditions in project pravega by pravega.

the class WriteOperation method call.

@Override
public CompletableFuture<Void> call() {
    // Validate preconditions.
    checkPreconditions();
    log.debug("{} write - started op={}, segment={}, offset={} length={}.", chunkedSegmentStorage.getLogPrefix(), System.identityHashCode(this), handle.getSegmentName(), offset, length);
    val streamSegmentName = handle.getSegmentName();
    return ChunkedSegmentStorage.tryWith(chunkedSegmentStorage.getMetadataStore().beginTransaction(false, handle.getSegmentName()), txn -> {
        didSegmentLayoutChange = false;
        // Retrieve metadata.
        return txn.get(streamSegmentName).thenComposeAsync(storageMetadata -> {
            segmentMetadata = (SegmentMetadata) storageMetadata;
            // Validate preconditions.
            checkState();
            isSystemSegment = chunkedSegmentStorage.isStorageSystemSegment(segmentMetadata);
            // Check if this is a first write after ownership changed.
            isFirstWriteAfterFailover = segmentMetadata.isOwnershipChanged();
            lastChunkMetadata.set(null);
            chunkHandle = null;
            bytesRemaining.set(length);
            currentOffset.set(offset);
            return getLastChunk(txn).thenComposeAsync(v -> writeData(txn).thenComposeAsync(vv -> commit(txn).thenApplyAsync(vvvv -> postCommit(), chunkedSegmentStorage.getExecutor()).exceptionally(this::handleException), chunkedSegmentStorage.getExecutor()).thenRunAsync(this::logEnd, chunkedSegmentStorage.getExecutor()), chunkedSegmentStorage.getExecutor());
        }, chunkedSegmentStorage.getExecutor());
    }, chunkedSegmentStorage.getExecutor()).exceptionally(ex -> (Void) handleException(ex));
}
Also used : lombok.val(lombok.val) Exceptions(io.pravega.common.Exceptions) StorageNotPrimaryException(io.pravega.segmentstore.storage.StorageNotPrimaryException) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) StorageFullException(io.pravega.segmentstore.storage.StorageFullException) AtomicReference(java.util.concurrent.atomic.AtomicReference) SLTS_WRITE_LATENCY(io.pravega.segmentstore.storage.chunklayer.ChunkStorageMetrics.SLTS_WRITE_LATENCY) ArrayList(java.util.ArrayList) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SLTS_WRITE_INSTANT_TPUT(io.pravega.segmentstore.storage.chunklayer.ChunkStorageMetrics.SLTS_WRITE_INSTANT_TPUT) MetadataTransaction(io.pravega.segmentstore.storage.metadata.MetadataTransaction) SLTS_SYSTEM_WRITE_LATENCY(io.pravega.segmentstore.storage.chunklayer.ChunkStorageMetrics.SLTS_SYSTEM_WRITE_LATENCY) ChunkMetadata(io.pravega.segmentstore.storage.metadata.ChunkMetadata) LoggerHelpers(io.pravega.common.LoggerHelpers) StorageMetadataWritesFencedOutException(io.pravega.segmentstore.storage.metadata.StorageMetadataWritesFencedOutException) SLTS_SYSTEM_NUM_CHUNKS_ADDED(io.pravega.segmentstore.storage.chunklayer.ChunkStorageMetrics.SLTS_SYSTEM_NUM_CHUNKS_ADDED) lombok.val(lombok.val) CompletionException(java.util.concurrent.CompletionException) SLTS_NUM_CHUNKS_ADDED(io.pravega.segmentstore.storage.chunklayer.ChunkStorageMetrics.SLTS_NUM_CHUNKS_ADDED) Timer(io.pravega.common.Timer) AtomicLong(java.util.concurrent.atomic.AtomicLong) SLTS_SYSTEM_WRITE_BYTES(io.pravega.segmentstore.storage.chunklayer.ChunkStorageMetrics.SLTS_SYSTEM_WRITE_BYTES) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) SLTS_WRITE_BYTES(io.pravega.segmentstore.storage.chunklayer.ChunkStorageMetrics.SLTS_WRITE_BYTES) Preconditions(com.google.common.base.Preconditions) BoundedInputStream(io.pravega.common.io.BoundedInputStream) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) InputStream(java.io.InputStream) SegmentMetadata(io.pravega.segmentstore.storage.metadata.SegmentMetadata)

Example 7 with Preconditions

use of com.google.common.base.Preconditions in project pravega by pravega.

the class PersistentStreamBase method submitScale.

/**
 * This method attempts to start a new scale workflow. For this it first computes epoch transition and stores it
 * in the metadastore.
 * This method can be called by manual scale or during the processing of auto-scale event. Which means there could be
 * concurrent calls to this method.
 *
 * @param segmentsToSeal segments that will be sealed at the end of this scale operation.
 * @param newRanges      key ranges of new segments to be created
 * @param scaleTimestamp scaling timestamp
 * @return : list of newly created segments with current epoch
 */
@Override
public CompletableFuture<VersionedMetadata<EpochTransitionRecord>> submitScale(final List<Long> segmentsToSeal, final List<Map.Entry<Double, Double>> newRanges, final long scaleTimestamp, final VersionedMetadata<EpochTransitionRecord> existing, OperationContext context) {
    Preconditions.checkNotNull(context, "Operation context cannot be null");
    return verifyNotSealed(context).thenCompose(v -> {
        if (existing == null) {
            return getEpochTransition(context);
        } else {
            return CompletableFuture.completedFuture(existing);
        }
    }).thenCompose(record -> getActiveEpochRecord(true, context).thenCompose(currentEpoch -> getConfiguration(context).thenCompose(config -> {
        if (!record.getObject().equals(EpochTransitionRecord.EMPTY)) {
            // and new ranges are identical). else throw scale conflict exception
            if (!RecordHelper.verifyRecordMatchesInput(segmentsToSeal, newRanges, false, record.getObject())) {
                log.debug(context.getRequestId(), "scale conflict, another scale operation is ongoing");
                throw new EpochTransitionOperationExceptions.ConflictException();
            }
            return CompletableFuture.completedFuture(record);
        } else {
            // check input is valid and satisfies preconditions
            if (!RecordHelper.canScaleFor(segmentsToSeal, currentEpoch)) {
                return updateEpochTransitionNode(new VersionedMetadata<>(EpochTransitionRecord.EMPTY, record.getVersion()), context).thenApply(x -> {
                    log.warn(context.getRequestId(), "scale precondition failed {}", segmentsToSeal);
                    throw new EpochTransitionOperationExceptions.PreConditionFailureException();
                });
            }
            if (!RecordHelper.validateInputRange(segmentsToSeal, newRanges, currentEpoch)) {
                log.error(context.getRequestId(), "scale input invalid {} {}", segmentsToSeal, newRanges);
                throw new EpochTransitionOperationExceptions.InputInvalidException();
            }
            int numberOfSegmentsPostScale = currentEpoch.getSegments().size() - segmentsToSeal.size() + newRanges.size();
            if (numberOfSegmentsPostScale < config.getScalingPolicy().getMinNumSegments()) {
                log.warn(context.getRequestId(), "Scale cannot be performed as Min Segment Count will not hold {} {}", segmentsToSeal, newRanges);
                throw new EpochTransitionOperationExceptions.PreConditionFailureException();
            }
            EpochTransitionRecord epochTransition = RecordHelper.computeEpochTransition(currentEpoch, segmentsToSeal, newRanges, scaleTimestamp);
            return updateEpochTransitionNode(new VersionedMetadata<>(epochTransition, record.getVersion()), context).thenApply(version -> {
                log.info(context.getRequestId(), "scale for stream {}/{} accepted. Segments to seal = {}", scope, name, epochTransition.getSegmentsToSeal());
                return new VersionedMetadata<>(epochTransition, version);
            });
        }
    })));
}
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) EpochTransitionRecord(io.pravega.controller.store.stream.records.EpochTransitionRecord) VersionedMetadata(io.pravega.controller.store.VersionedMetadata)

Example 8 with Preconditions

use of com.google.common.base.Preconditions in project android_packages_apps_Dialer by LineageOS.

the class RefreshAnnotatedCallLogWorker method isDirty.

private ListenableFuture<Boolean> isDirty() {
    List<ListenableFuture<Boolean>> isDirtyFutures = new ArrayList<>();
    for (CallLogDataSource dataSource : dataSources.getDataSourcesIncludingSystemCallLog()) {
        ListenableFuture<Boolean> dataSourceDirty = dataSource.isDirty();
        isDirtyFutures.add(dataSourceDirty);
        String eventName = String.format(Metrics.IS_DIRTY_TEMPLATE, dataSource.getLoggingName());
        futureTimer.applyTiming(dataSourceDirty, eventName, LogCatMode.LOG_VALUES);
    }
    // Simultaneously invokes isDirty on all data sources, returning as soon as one returns true.
    ListenableFuture<Boolean> isDirtyFuture = DialerFutures.firstMatching(isDirtyFutures, Preconditions::checkNotNull, false);
    futureTimer.applyTiming(isDirtyFuture, Metrics.IS_DIRTY_EVENT_NAME, LogCatMode.LOG_VALUES);
    return isDirtyFuture;
}
Also used : Preconditions(com.google.common.base.Preconditions) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CallLogDataSource(com.android.dialer.calllog.datasources.CallLogDataSource)

Example 9 with Preconditions

use of com.google.common.base.Preconditions in project android_packages_apps_Dialer by MoKee.

the class RefreshAnnotatedCallLogWorker method isDirty.

private ListenableFuture<Boolean> isDirty() {
    List<ListenableFuture<Boolean>> isDirtyFutures = new ArrayList<>();
    for (CallLogDataSource dataSource : dataSources.getDataSourcesIncludingSystemCallLog()) {
        ListenableFuture<Boolean> dataSourceDirty = dataSource.isDirty(appContext);
        isDirtyFutures.add(dataSourceDirty);
        String eventName = String.format(Metrics.IS_DIRTY_TEMPLATE, dataSource.getClass().getSimpleName());
        futureTimer.applyTiming(dataSourceDirty, eventName, LogCatMode.LOG_VALUES);
    }
    // Simultaneously invokes isDirty on all data sources, returning as soon as one returns true.
    ListenableFuture<Boolean> isDirtyFuture = DialerFutures.firstMatching(isDirtyFutures, Preconditions::checkNotNull, false);
    futureTimer.applyTiming(isDirtyFuture, Metrics.IS_DIRTY_EVENT_NAME, LogCatMode.LOG_VALUES);
    return isDirtyFuture;
}
Also used : Preconditions(com.google.common.base.Preconditions) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CallLogDataSource(com.android.dialer.calllog.datasources.CallLogDataSource)

Aggregations

Preconditions (com.google.common.base.Preconditions)9 Exceptions (io.pravega.common.Exceptions)7 Futures (io.pravega.common.concurrent.Futures)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 CompletionException (java.util.concurrent.CompletionException)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 lombok.val (lombok.val)7 LoggerHelpers (io.pravega.common.LoggerHelpers)6 Timer (io.pravega.common.Timer)6 SegmentHandle (io.pravega.segmentstore.storage.SegmentHandle)6 ChunkMetadata (io.pravega.segmentstore.storage.metadata.ChunkMetadata)6 MetadataTransaction (io.pravega.segmentstore.storage.metadata.MetadataTransaction)6 SegmentMetadata (io.pravega.segmentstore.storage.metadata.SegmentMetadata)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Callable (java.util.concurrent.Callable)6 Slf4j (lombok.extern.slf4j.Slf4j)6 StorageNotPrimaryException (io.pravega.segmentstore.storage.StorageNotPrimaryException)5 StorageMetadataWritesFencedOutException (io.pravega.segmentstore.storage.metadata.StorageMetadataWritesFencedOutException)5 NameUtils (io.pravega.shared.NameUtils)5