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));
}
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);
});
}
})));
}
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;
}
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;
}
Aggregations