Search in sources :

Example 21 with TimeoutTimer

use of io.pravega.common.TimeoutTimer in project pravega by pravega.

the class ContainerKeyIndex method update.

/**
 * Performs a Batch Update or Removal.
 *
 * If {@link TableKeyBatch#isConditional()} returns true, this will execute an atomic Conditional Update/Removal based
 * on the condition items in the batch ({@link TableKeyBatch#getVersionedItems}. The entire TableKeyBatch will be
 * conditioned on those items, including those items that do not have a condition set. The entire TableKeyBatch will
 * either all be committed as one unit or not at all.
 *
 * Otherwise this will perform an Unconditional Update/Removal, where all the {@link TableKeyBatch.Item}s in
 * {@link TableKeyBatch#getItems()} will be applied regardless of whether they already exist or what their versions are.
 *
 * @param segment The Segment to perform the update/removal on.
 * @param batch   The {@link TableKeyBatch} to apply.
 * @param persist A Supplier that, when invoked, will persist the contents of the batch to the Segment and return
 *                a CompletableFuture to indicate when the operation is done, containing the offset at which the
 *                batch has been written to the Segment. This Future must complete successfully before the effects
 *                of the {@link TableKeyBatch} are applied to the in-memory Index or before downstream conditional
 *                updates on the affected keys are initiated.
 * @param timer   Timer for the operation.
 * @return A CompletableFuture that, when completed, will contain a list of offsets (within the Segment) where each
 * of the items in the batch has been persisted. If the update failed, it will be failed with the appropriate exception.
 * Notable exceptions:
 * <ul>
 * <li>{@link KeyNotExistsException} If a Key in the TableKeyBatch does not exist and was conditioned as having to exist.
 * <li>{@link BadKeyVersionException} If a Key does exist but had a version mismatch.
 * </ul>
 */
CompletableFuture<List<Long>> update(DirectSegmentAccess segment, TableKeyBatch batch, Supplier<CompletableFuture<Long>> persist, TimeoutTimer timer) {
    Exceptions.checkNotClosed(this.closed.get(), this);
    Supplier<CompletableFuture<List<Long>>> update;
    if (batch.isConditional()) {
        // Conditional update.
        // Collect all Cache Keys for the Update Items that have a condition on them; we need this on order to
        // serialize execution across them.
        val keys = batch.getVersionedItems().stream().map(item -> Maps.immutableEntry(segment.getSegmentId(), item.getHash())).collect(Collectors.toList());
        // Serialize the execution (queue it up to run only after all other currently queued up conditional updates
        // for touched keys have finished).
        update = () -> this.conditionalUpdateProcessor.add(keys, () -> validateConditionalUpdate(segment, batch, timer).thenComposeAsync(v -> persist.get(), this.executor).thenApplyAsync(batchOffset -> updateCache(segment, batch, batchOffset), this.executor));
    } else {
        // Unconditional update: persist the entries and update the cache.
        update = () -> persist.get().thenApplyAsync(batchOffset -> updateCache(segment, batch, batchOffset), this.executor);
    }
    // Throttle any requests, if needed.
    return this.segmentTracker.throttleIfNeeded(segment, update, batch.getLength());
}
Also used : lombok.val(lombok.val) ScheduledFuture(java.util.concurrent.ScheduledFuture) SneakyThrows(lombok.SneakyThrows) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TimeoutException(java.util.concurrent.TimeoutException) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Map(java.util.Map) AsyncReadResultProcessor(io.pravega.segmentstore.server.reading.AsyncReadResultProcessor) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) SegmentStoreMetrics(io.pravega.segmentstore.server.SegmentStoreMetrics) Predicate(java.util.function.Predicate) NonNull(lombok.NonNull) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletionException(java.util.concurrent.CompletionException) ThreadSafe(javax.annotation.concurrent.ThreadSafe) UUID(java.util.UUID) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Futures(io.pravega.common.concurrent.Futures) ReadResult(io.pravega.segmentstore.contracts.ReadResult) CacheManager(io.pravega.segmentstore.server.CacheManager) ObjectClosedException(io.pravega.common.ObjectClosedException) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) Getter(lombok.Getter) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) BadKeyVersionException(io.pravega.segmentstore.contracts.tables.BadKeyVersionException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TableSegmentNotEmptyException(io.pravega.segmentstore.contracts.tables.TableSegmentNotEmptyException) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) MultiKeySequentialProcessor(io.pravega.common.concurrent.MultiKeySequentialProcessor) ConditionalTableUpdateException(io.pravega.segmentstore.contracts.tables.ConditionalTableUpdateException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamSegmentTruncatedException(io.pravega.segmentstore.contracts.StreamSegmentTruncatedException) TimeoutTimer(io.pravega.common.TimeoutTimer) KeyNotExistsException(io.pravega.segmentstore.contracts.tables.KeyNotExistsException) lombok.val(lombok.val) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Timer(io.pravega.common.Timer) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) AsyncSemaphore(io.pravega.common.concurrent.AsyncSemaphore) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 22 with TimeoutTimer

use of io.pravega.common.TimeoutTimer in project pravega by pravega.

the class ContainerTableExtensionImpl method getInfo.

@Override
public CompletableFuture<TableSegmentInfo> getInfo(String segmentName, Duration timeout) {
    Exceptions.checkNotClosed(this.closed.get(), this);
    logRequest("getInfo", segmentName);
    val timer = new TimeoutTimer(timeout);
    return this.segmentContainer.forSegment(segmentName, timer.getRemaining()).thenComposeAsync(segment -> selectLayout(segment.getInfo()).getInfo(segment, timer.getRemaining()), this.executor);
}
Also used : lombok.val(lombok.val) TimeoutTimer(io.pravega.common.TimeoutTimer)

Example 23 with TimeoutTimer

use of io.pravega.common.TimeoutTimer in project pravega by pravega.

the class ContainerTableExtensionImpl method put.

@Override
public CompletableFuture<List<Long>> put(@NonNull String segmentName, @NonNull List<TableEntry> entries, long tableSegmentOffset, Duration timeout) {
    Exceptions.checkNotClosed(this.closed.get(), this);
    TimeoutTimer timer = new TimeoutTimer(timeout);
    return this.segmentContainer.forSegment(segmentName, timer.getRemaining()).thenComposeAsync(segment -> selectLayout(segment.getInfo()).put(segment, entries, tableSegmentOffset, timer), this.executor);
}
Also used : TimeoutTimer(io.pravega.common.TimeoutTimer)

Example 24 with TimeoutTimer

use of io.pravega.common.TimeoutTimer in project pravega by pravega.

the class ContainerTableExtensionImpl method remove.

@Override
public CompletableFuture<Void> remove(@NonNull String segmentName, @NonNull Collection<TableKey> keys, long tableSegmentOffset, Duration timeout) {
    Exceptions.checkNotClosed(this.closed.get(), this);
    TimeoutTimer timer = new TimeoutTimer(timeout);
    return this.segmentContainer.forSegment(segmentName, timer.getRemaining()).thenComposeAsync(segment -> selectLayout(segment.getInfo()).remove(segment, keys, tableSegmentOffset, timer), this.executor);
}
Also used : TimeoutTimer(io.pravega.common.TimeoutTimer)

Example 25 with TimeoutTimer

use of io.pravega.common.TimeoutTimer in project pravega by pravega.

the class ContainerTableExtensionImpl method get.

@Override
public CompletableFuture<List<TableEntry>> get(@NonNull String segmentName, @NonNull List<BufferView> keys, Duration timeout) {
    Exceptions.checkNotClosed(this.closed.get(), this);
    TimeoutTimer timer = new TimeoutTimer(timeout);
    return this.segmentContainer.forSegment(segmentName, timer.getRemaining()).thenComposeAsync(segment -> selectLayout(segment.getInfo()).get(segment, keys, timer), this.executor);
}
Also used : TimeoutTimer(io.pravega.common.TimeoutTimer)

Aggregations

TimeoutTimer (io.pravega.common.TimeoutTimer)97 lombok.val (lombok.val)55 CompletableFuture (java.util.concurrent.CompletableFuture)52 Duration (java.time.Duration)51 Futures (io.pravega.common.concurrent.Futures)47 Preconditions (com.google.common.base.Preconditions)41 CompletionException (java.util.concurrent.CompletionException)41 Slf4j (lombok.extern.slf4j.Slf4j)41 Collectors (java.util.stream.Collectors)40 SneakyThrows (lombok.SneakyThrows)40 Exceptions (io.pravega.common.Exceptions)39 BufferView (io.pravega.common.util.BufferView)37 Getter (lombok.Getter)37 SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)36 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)34 SegmentMetadata (io.pravega.segmentstore.server.SegmentMetadata)34 RequiredArgsConstructor (lombok.RequiredArgsConstructor)34 ArrayList (java.util.ArrayList)33 List (java.util.List)31