Search in sources :

Example 1 with TableKey

use of io.pravega.segmentstore.contracts.tables.TableKey in project pravega by pravega.

the class ContainerKeyIndex method validateConditionalUpdateFailures.

private CompletableFuture<Void> validateConditionalUpdateFailures(DirectSegmentAccess segment, Map<TableKey, Long> expectedVersions, TimeoutTimer timer) {
    assert !expectedVersions.isEmpty();
    val bucketReader = TableBucketReader.key(segment, this::getBackpointerOffset, this.executor);
    val searches = expectedVersions.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> findBucketEntry(segment, bucketReader, e.getKey().getKey(), e.getValue(), timer)));
    return Futures.allOf(searches.values()).thenRun(() -> {
        val failed = new HashMap<TableKey, Long>();
        for (val e : searches.entrySet()) {
            val actual = e.getValue().join();
            boolean isValid = actual == null ? e.getKey().getVersion() == TableKey.NOT_EXISTS : e.getKey().getVersion() == actual.getVersion();
            if (!isValid) {
                failed.put(e.getKey(), actual == null ? TableKey.NOT_EXISTS : actual.getVersion());
            }
        }
        if (!failed.isEmpty()) {
            throw new CompletionException(new BadKeyVersionException(segment.getInfo().getName(), failed));
        }
    });
}
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) BadKeyVersionException(io.pravega.segmentstore.contracts.tables.BadKeyVersionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CompletionException(java.util.concurrent.CompletionException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with TableKey

use of io.pravega.segmentstore.contracts.tables.TableKey in project pravega by pravega.

the class ContainerKeyIndex method validateConditionalUpdate.

/**
 * Validates a list of UpdateBatchItems against their actual Table Bucket offsets.
 *
 * @param items         A list of {@link TableKeyBatch.Item} instances to validate.
 * @param bucketOffsets A Map of Key Hashes to their corresponding offsets (versions). Each {@link TableKeyBatch.Item}
 *                      will be looked up in this Map (based on the Item's KeyHash) and validated appropriately.
 * @param segmentName   The name of the segment on which the update is performed.
 * @throws KeyNotExistsException  If an UpdateBatchItem's Key does not exist in the Table but the item's version does
 *                                not indicate that the key must not exist.
 * @throws BadKeyVersionException If an UpdateBatchItem's Key does exist in the Table but the item's version is
 *                                different from that key's version.
 */
@SneakyThrows(ConditionalTableUpdateException.class)
private void validateConditionalUpdate(List<TableKeyBatch.Item> items, Map<UUID, Long> bucketOffsets, String segmentName) {
    // Key = Key that failed, Value = Key's bucket offset.
    val badKeyVersions = new HashMap<TableKey, Long>();
    for (val item : items) {
        // Validate compareVersion.
        TableKey key = item.getKey();
        Long bucketOffset = bucketOffsets.get(item.getHash());
        assert key.hasVersion() : "validateConditionalUpdate for TableKey with no compare version";
        if (bucketOffset == TableKey.NOT_EXISTS) {
            if (key.getVersion() != TableKey.NOT_EXISTS) {
                // Key does not exist, but the conditional update provided a specific version.
                throw new KeyNotExistsException(segmentName, key.getKey());
            }
        } else if (bucketOffset != key.getVersion()) {
            // Key does exist, but has the wrong version.
            badKeyVersions.put(key, bucketOffset);
        }
    }
    if (!badKeyVersions.isEmpty()) {
        // Throw the bad key version in bulk - helps speed verification.
        throw new BadKeyVersionException(segmentName, badKeyVersions);
    }
// All validations for all items passed.
}
Also used : lombok.val(lombok.val) BadKeyVersionException(io.pravega.segmentstore.contracts.tables.BadKeyVersionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyNotExistsException(io.pravega.segmentstore.contracts.tables.KeyNotExistsException) AtomicLong(java.util.concurrent.atomic.AtomicLong) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) SneakyThrows(lombok.SneakyThrows)

Example 3 with TableKey

use of io.pravega.segmentstore.contracts.tables.TableKey in project pravega by pravega.

the class TableServiceTests method generateRemovals.

private HashMap<String, ArrayList<TableKey>> generateRemovals(HashMap<BufferView, EntryData> keyInfo, boolean conditional) {
    val result = new HashMap<String, ArrayList<TableKey>>();
    for (val e : keyInfo.entrySet()) {
        val ed = e.getValue();
        TableKey tk = conditional ? TableKey.versioned(e.getKey(), ed.getVersion()) : TableKey.unversioned(e.getKey());
        val segmentUpdate = result.computeIfAbsent(ed.segmentName, ignored -> new ArrayList<>());
        segmentUpdate.add(tk);
    }
    return result;
}
Also used : lombok.val(lombok.val) HashMap(java.util.HashMap) TableKey(io.pravega.segmentstore.contracts.tables.TableKey)

Example 4 with TableKey

use of io.pravega.segmentstore.contracts.tables.TableKey in project pravega by pravega.

the class TableServiceTests method executeOffsetConditonalRemovals.

private void executeOffsetConditonalRemovals(HashMap<String, ArrayList<TableKey>> removals, long tableSegmentOffset, TableStore tableStore) throws Exception {
    val updateResult = removals.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> tableStore.remove(e.getKey(), e.getValue(), tableSegmentOffset, TIMEOUT)));
    Futures.allOf(updateResult.values()).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
}
Also used : lombok.val(lombok.val) BufferViewComparator(io.pravega.common.util.BufferViewComparator) TableSegmentConfig(io.pravega.segmentstore.contracts.tables.TableSegmentConfig) WriterConfig(io.pravega.segmentstore.server.writer.WriterConfig) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ServiceConfig(io.pravega.segmentstore.server.store.ServiceConfig) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) AtomicReference(java.util.concurrent.atomic.AtomicReference) IteratorArgs(io.pravega.segmentstore.contracts.tables.IteratorArgs) ArrayList(java.util.ArrayList) SegmentType(io.pravega.segmentstore.contracts.SegmentType) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) IteratorItem(io.pravega.segmentstore.contracts.tables.IteratorItem) BufferView(io.pravega.common.util.BufferView) InMemoryStorageFactory(io.pravega.segmentstore.storage.mocks.InMemoryStorageFactory) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ContainerConfig(io.pravega.segmentstore.server.containers.ContainerConfig) ReadIndexConfig(io.pravega.segmentstore.server.reading.ReadIndexConfig) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) InMemoryDurableDataLogFactory(io.pravega.segmentstore.storage.mocks.InMemoryDurableDataLogFactory) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Before(org.junit.Before) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) DurableLogConfig(io.pravega.segmentstore.server.logs.DurableLogConfig) lombok.val(lombok.val) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Comparator(java.util.Comparator) Assert(org.junit.Assert) TableEntry(io.pravega.segmentstore.contracts.tables.TableEntry) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with TableKey

use of io.pravega.segmentstore.contracts.tables.TableKey in project pravega by pravega.

the class TableServiceTests method executeRemovals.

private void executeRemovals(HashMap<String, ArrayList<TableKey>> removals, TableStore tableStore) throws Exception {
    val updateResult = removals.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> tableStore.remove(e.getKey(), e.getValue(), TIMEOUT)));
    Futures.allOf(updateResult.values()).get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
}
Also used : lombok.val(lombok.val) BufferViewComparator(io.pravega.common.util.BufferViewComparator) TableSegmentConfig(io.pravega.segmentstore.contracts.tables.TableSegmentConfig) WriterConfig(io.pravega.segmentstore.server.writer.WriterConfig) TableStore(io.pravega.segmentstore.contracts.tables.TableStore) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ServiceConfig(io.pravega.segmentstore.server.store.ServiceConfig) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ServiceBuilderConfig(io.pravega.segmentstore.server.store.ServiceBuilderConfig) AtomicReference(java.util.concurrent.atomic.AtomicReference) IteratorArgs(io.pravega.segmentstore.contracts.tables.IteratorArgs) ArrayList(java.util.ArrayList) SegmentType(io.pravega.segmentstore.contracts.SegmentType) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) IteratorItem(io.pravega.segmentstore.contracts.tables.IteratorItem) BufferView(io.pravega.common.util.BufferView) InMemoryStorageFactory(io.pravega.segmentstore.storage.mocks.InMemoryStorageFactory) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ContainerConfig(io.pravega.segmentstore.server.containers.ContainerConfig) ReadIndexConfig(io.pravega.segmentstore.server.reading.ReadIndexConfig) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) InMemoryDurableDataLogFactory(io.pravega.segmentstore.storage.mocks.InMemoryDurableDataLogFactory) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Before(org.junit.Before) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) DurableLogConfig(io.pravega.segmentstore.server.logs.DurableLogConfig) lombok.val(lombok.val) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Comparator(java.util.Comparator) Assert(org.junit.Assert) TableEntry(io.pravega.segmentstore.contracts.tables.TableEntry) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TableKey (io.pravega.segmentstore.contracts.tables.TableKey)12 lombok.val (lombok.val)11 BufferView (io.pravega.common.util.BufferView)8 Duration (java.time.Duration)8 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 TableEntry (io.pravega.segmentstore.contracts.tables.TableEntry)7 Collectors (java.util.stream.Collectors)7 Futures (io.pravega.common.concurrent.Futures)6 Collections (java.util.Collections)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ByteArraySegment (io.pravega.common.util.ByteArraySegment)5 List (java.util.List)5 RequiredArgsConstructor (lombok.RequiredArgsConstructor)5 Test (org.junit.Test)5 TimeoutTimer (io.pravega.common.TimeoutTimer)4 SegmentType (io.pravega.segmentstore.contracts.SegmentType)4 BadKeyVersionException (io.pravega.segmentstore.contracts.tables.BadKeyVersionException)4 Map (java.util.Map)4