Search in sources :

Example 41 with Timeout

use of org.junit.rules.Timeout in project pravega by pravega.

the class IndexReaderWriterTests method checkIndex.

private void checkIndex(Collection<BufferView> allKeys, Map<Long, BufferView> existingKeysByOffset, IndexWriter w, KeyHasher hasher, SegmentMock segment) {
    val timer = new TimeoutTimer(TIMEOUT);
    // Group all keys by their full hash (each hash should translate to a bucket), and make sure they're ordered by
    // offset (in descending order - so we can verify backpointer ordering).
    val existingKeys = existingKeysByOffset.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
    val keysByHash = allKeys.stream().map(key -> new BucketUpdate.KeyInfo(key, existingKeys.getOrDefault(key, NO_OFFSET), existingKeys.getOrDefault(key, NO_OFFSET))).sorted(// Reverse order.
    (k1, k2) -> Long.compare(k2.getOffset(), k1.getOffset())).collect(Collectors.groupingBy(keyInfo -> hasher.hash(keyInfo.getKey())));
    int existentBucketCount = 0;
    val buckets = w.locateBuckets(segment, keysByHash.keySet(), timer).join();
    for (val e : keysByHash.entrySet()) {
        val hash = e.getKey();
        val keys = e.getValue();
        val bucket = buckets.get(hash);
        Assert.assertNotNull("No bucket found for hash " + hash, bucket);
        boolean allDeleted = keys.stream().allMatch(k -> k.getOffset() == NO_OFFSET);
        Assert.assertNotEquals("Only expecting inexistent bucket when all its keys are deleted " + hash, allDeleted, bucket.exists());
        val bucketOffsets = w.getBucketOffsets(segment, bucket, timer).join();
        // Verify that we didn't return too many or too few keys.
        if (allDeleted) {
            Assert.assertEquals("Not expecting any offsets to be returned for bucket: " + hash, 0, bucketOffsets.size());
        } else {
            AssertExtensions.assertGreaterThan("Expected at least one offset to be returned for bucket: " + hash, 0, bucketOffsets.size());
            existentBucketCount++;
        }
        AssertExtensions.assertLessThanOrEqual("Too many offsets returned for bucket: " + hash, keys.size(), bucketOffsets.size());
        // Verify returned keys are as expected.
        for (int i = 0; i < bucketOffsets.size(); i++) {
            long actualOffset = bucketOffsets.get(i);
            long expectedOffset = keys.get(i).getOffset();
            String id = String.format("{%s[%s]}", hash, i);
            // In this loop, we do not expect to have Deleted Keys. If our Expected Offset indicates this key should
            // have been deleted, then getBucketOffsets() should not have returned this.
            Assert.assertNotEquals("Expecting a deleted key but found existing one: " + id, NO_OFFSET, expectedOffset);
            Assert.assertEquals("Unexpected key offset in bucket " + id, expectedOffset, actualOffset);
        }
        if (bucketOffsets.size() < keys.size()) {
            val prevKeyOffset = keys.get(bucketOffsets.size()).getOffset();
            Assert.assertEquals("Missing key from bucket " + hash, NO_OFFSET, prevKeyOffset);
        }
    }
    checkEntryCount(existingKeysByOffset.size(), segment);
    checkBucketCount(existentBucketCount, segment);
}
Also used : lombok.val(lombok.val) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) StreamSegmentInformation(io.pravega.segmentstore.contracts.StreamSegmentInformation) AssertExtensions(io.pravega.test.common.AssertExtensions) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) HashSet(java.util.HashSet) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) Attributes(io.pravega.segmentstore.contracts.Attributes) ImmutableMap(com.google.common.collect.ImmutableMap) TimeoutTimer(io.pravega.common.TimeoutTimer) Collection(java.util.Collection) AttributeId(io.pravega.segmentstore.contracts.AttributeId) lombok.val(lombok.val) Test(org.junit.Test) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) SegmentMock(io.pravega.segmentstore.server.SegmentMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Comparator(java.util.Comparator) Assert(org.junit.Assert) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TimeoutTimer(io.pravega.common.TimeoutTimer)

Example 42 with Timeout

use of org.junit.rules.Timeout in project pravega by pravega.

the class ContainerKeyIndexTests method testRecoveryTimeout.

/**
 * Checks the ability for the {@link ContainerKeyIndex} class to cancel recovery-bound tasks if recovery took too long.
 */
@Test
public void testRecoveryTimeout() throws Exception {
    val spyExecutor = Mockito.spy(executorService());
    @Cleanup val context = new TestContext();
    @Cleanup val index = context.createIndex(context.defaultConfig, spyExecutor);
    // Setup the segment with initial attributes.
    val iw = new IndexWriter(HASHER, executorService());
    // Generate initial set of keys.
    val keys = generateUnversionedKeys(BATCH_SIZE, context);
    long offset = 0;
    val hashes = new ArrayList<UUID>();
    val keysWithOffsets = new HashMap<UUID, KeyWithOffset>();
    for (val k : keys) {
        val hash = HASHER.hash(k.getKey());
        hashes.add(hash);
        keysWithOffsets.put(hash, new KeyWithOffset(k.getKey(), offset));
        offset += k.getKey().getLength();
    }
    // Write some garbage data to the segment, but make it longer than the threshold to trigger pre-caching; we don't
    // want to deal with that now since we can't control its runtime.
    context.segment.append(new ByteArraySegment(new byte[TEST_MAX_TAIL_CACHE_PRE_INDEX_LENGTH + 1]), null, TIMEOUT).join();
    // Update the index, but keep the LastIndexedOffset at 0.
    val buckets = iw.locateBuckets(context.segment, keysWithOffsets.keySet(), context.timer).join();
    Collection<BucketUpdate> bucketUpdates = buckets.entrySet().stream().map(e -> {
        val builder = BucketUpdate.forBucket(e.getValue());
        val ko = keysWithOffsets.get(e.getKey());
        builder.withKeyUpdate(new BucketUpdate.KeyUpdate(ko.key, ko.offset, ko.offset, false));
        return builder.build();
    }).collect(Collectors.toList());
    iw.updateBuckets(context.segment, bucketUpdates, 0L, 0, keysWithOffsets.size(), TIMEOUT).join();
    // Setup a mock Executor.schedule call that returns a future over which we have full control. This will save us
    // from having to wait for the recovery timeout to actually expire.
    val timeoutFuture = new AtomicReference<ScheduledFuture<?>>();
    val timeoutCallback = new AtomicReference<Callable<?>>();
    Mockito.doAnswer(arg -> {
        timeoutCallback.set(arg.getArgument(0));
        timeoutFuture.set((ScheduledFuture<?>) arg.callRealMethod());
        return timeoutFuture.get();
    }).when(spyExecutor).schedule(Mockito.<Callable<Boolean>>any(), Mockito.anyLong(), Mockito.any());
    // Issue the first request. We intend to time it out.
    val getBucketOffset1 = index.getBucketOffsets(context.segment, hashes, context.timer);
    TestUtils.await(() -> timeoutFuture.get() != null, 5, TIMEOUT.toMillis());
    // Time-out the call.
    timeoutCallback.get().call();
    AssertExtensions.assertSuppliedFutureThrows("Request did not fail when recovery timed out.", () -> getBucketOffset1, ex -> ex instanceof TimeoutException);
    // Wait for the future to be unregistered before continuing.
    TestUtils.await(timeoutFuture.get()::isDone, 5, TIMEOUT.toMillis());
    // Verify that a new operation will be unblocked if we notify that the recovery completed successfully.
    timeoutFuture.set(null);
    timeoutCallback.set(null);
    val getBucketOffset2 = index.getBucketOffsets(context.segment, hashes, context.timer);
    // Wait for the timeout future to be registered. We'll verify if it gets cancelled next.
    TestUtils.await(() -> timeoutFuture.get() != null, 5, TIMEOUT.toMillis());
    index.notifyIndexOffsetChanged(context.segment.getSegmentId(), context.segment.getInfo().getLength(), 0);
    val result1 = getBucketOffset2.get(TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
    val expected1 = new HashMap<UUID, Long>();
    keysWithOffsets.forEach((k, o) -> expected1.put(k, o.offset));
    AssertExtensions.assertMapEquals("Unexpected result from getBucketOffsets() after a retry.", expected1, result1);
    AssertExtensions.assertEventuallyEquals("Expected the internal timeout future to be cancelled after a successful recovery.", true, timeoutFuture.get()::isCancelled, 5, TIMEOUT.toMillis());
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) Random(java.util.Random) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Map(java.util.Map) CachePolicy(io.pravega.segmentstore.server.CachePolicy) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) CancellationException(java.util.concurrent.CancellationException) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) DirectMemoryCache(io.pravega.segmentstore.storage.cache.DirectMemoryCache) TestUtils(io.pravega.test.common.TestUtils) Futures(io.pravega.common.concurrent.Futures) CacheManager(io.pravega.segmentstore.server.CacheManager) ObjectClosedException(io.pravega.common.ObjectClosedException) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) BadKeyVersionException(io.pravega.segmentstore.contracts.tables.BadKeyVersionException) CacheStorage(io.pravega.segmentstore.storage.cache.CacheStorage) ArrayList(java.util.ArrayList) TableSegmentNotEmptyException(io.pravega.segmentstore.contracts.tables.TableSegmentNotEmptyException) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) SegmentType(io.pravega.segmentstore.contracts.SegmentType) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) TimeoutTimer(io.pravega.common.TimeoutTimer) KeyNotExistsException(io.pravega.segmentstore.contracts.tables.KeyNotExistsException) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) SegmentMock(io.pravega.segmentstore.server.SegmentMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) Rule(org.junit.Rule) Comparator(java.util.Comparator) Assert(org.junit.Assert) TableEntry(io.pravega.segmentstore.contracts.tables.TableEntry) Collections(java.util.Collections) ByteArraySegment(io.pravega.common.util.ByteArraySegment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Cleanup(lombok.Cleanup) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 43 with Timeout

use of org.junit.rules.Timeout in project pravega by pravega.

the class ContainerKeyIndexTests method checkBackpointers.

private void checkBackpointers(List<UpdateItem> updates, TestContext context) {
    val sortedUpdates = updates.stream().sorted(Comparator.comparingLong(u -> u.offset.get())).collect(Collectors.toList());
    val highestUpdate = sortedUpdates.get(sortedUpdates.size() - 1);
    val highestUpdateHashes = highestUpdate.batch.getItems().stream().map(TableKeyBatch.Item::getHash).collect(Collectors.toList());
    Map<UUID, Long> backpointerSources = context.index.getBucketOffsets(context.segment, highestUpdateHashes, context.timer).join();
    for (int updateId = sortedUpdates.size() - 1; updateId >= 0; updateId--) {
        // Generate the expected backpointers.
        Map<UUID, Long> expectedBackpointers = new HashMap<>();
        if (updateId == 0) {
            // For the first update, we do not expect any.
            backpointerSources.keySet().forEach(k -> expectedBackpointers.put(k, -1L));
        } else {
            // For any other updates, it's whatever got executed before this one.
            val previousUpdate = sortedUpdates.get(updateId - 1);
            for (int i = 0; i < previousUpdate.batch.getItems().size(); i++) {
                expectedBackpointers.put(previousUpdate.batch.getItems().get(i).getHash(), previousUpdate.update.join().get(i));
            }
        }
        // Fetch the actual values.
        Map<UUID, Long> actualBackpointers = backpointerSources.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> context.index.getBackpointerOffset(context.segment, e.getValue(), TIMEOUT).join()));
        // Check and move on.
        Assert.assertEquals("Unexpected backpointer count for update " + updateId, expectedBackpointers.size(), actualBackpointers.size());
        for (val e : expectedBackpointers.entrySet()) {
            val a = actualBackpointers.get(e.getKey());
            Assert.assertNotNull("No backpointer for update " + updateId, a);
            Assert.assertEquals("Unexpected backpointer for update " + updateId, e.getValue(), a);
        }
        backpointerSources = expectedBackpointers;
    }
    // Check unindexed key hashes.
    val unindexedHashes = context.index.getUnindexedKeyHashes(context.segment).join().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getSegmentOffset()));
    val expectedHashes = highestUpdate.batch.getItems().stream().collect(Collectors.toMap(TableKeyBatch.Item::getHash, i -> highestUpdate.offset.get() + i.getOffset()));
    AssertExtensions.assertMapEquals("Unexpected result from getUnindexedKeyHashes", expectedHashes, unindexedHashes);
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) Random(java.util.Random) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Map(java.util.Map) CachePolicy(io.pravega.segmentstore.server.CachePolicy) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) CancellationException(java.util.concurrent.CancellationException) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) DirectMemoryCache(io.pravega.segmentstore.storage.cache.DirectMemoryCache) TestUtils(io.pravega.test.common.TestUtils) Futures(io.pravega.common.concurrent.Futures) CacheManager(io.pravega.segmentstore.server.CacheManager) ObjectClosedException(io.pravega.common.ObjectClosedException) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) BadKeyVersionException(io.pravega.segmentstore.contracts.tables.BadKeyVersionException) CacheStorage(io.pravega.segmentstore.storage.cache.CacheStorage) ArrayList(java.util.ArrayList) TableSegmentNotEmptyException(io.pravega.segmentstore.contracts.tables.TableSegmentNotEmptyException) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) SegmentType(io.pravega.segmentstore.contracts.SegmentType) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) TimeoutTimer(io.pravega.common.TimeoutTimer) KeyNotExistsException(io.pravega.segmentstore.contracts.tables.KeyNotExistsException) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) SegmentMock(io.pravega.segmentstore.server.SegmentMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) Rule(org.junit.Rule) Comparator(java.util.Comparator) Assert(org.junit.Assert) TableEntry(io.pravega.segmentstore.contracts.tables.TableEntry) Collections(java.util.Collections) HashMap(java.util.HashMap) AtomicLong(java.util.concurrent.atomic.AtomicLong) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap)

Example 44 with Timeout

use of org.junit.rules.Timeout in project pravega by pravega.

the class ContainerKeyIndexTests method testGetBucketOffsetDirect.

/**
 * Tests the {@link ContainerKeyIndex#getBucketOffsetDirect} method.
 */
@Test
public void testGetBucketOffsetDirect() {
    final int segmentLength = 1;
    final long updateBatchLength = 100000;
    final long noCacheOffset = updateBatchLength;
    final long lowerCacheOffset = noCacheOffset + updateBatchLength;
    final long higherCacheOffset = lowerCacheOffset + updateBatchLength;
    @Cleanup val context = new TestContext();
    context.segment.append(new ByteArraySegment(new byte[segmentLength]), null, TIMEOUT).join();
    // Setup the segment with initial attributes.
    val iw = new IndexWriter(HASHER, executorService());
    // Generate keys.
    // First 1/3 of the keys do not exist in the cache.
    // Second 1/3 of the keys exist in the cache, but have an offset lower than in the Index.
    // Last 1/3 of the keys exist in the cache and have an offset higher than in the Index.
    val keys = generateUnversionedKeys(BATCH_SIZE, context);
    val keysWithOffsets = new HashMap<UUID, KeyWithOffset>();
    val noCacheKeys = new ArrayList<TableKey>();
    val lowerCacheOffsetKeys = new ArrayList<TableKey>();
    val higherCacheOffsetKeys = new ArrayList<TableKey>();
    for (int i = 0; i < keys.size(); i++) {
        val k = keys.get(i);
        val hash = HASHER.hash(k.getKey());
        if (i < keys.size() / 3) {
            // Does not exist in the cache.
            noCacheKeys.add(k);
            keysWithOffsets.put(hash, new KeyWithOffset(k.getKey(), noCacheOffset));
        } else if (i < keys.size() * 2 / 3) {
            // Exists in the cache, but with a lower offset than in the index.
            lowerCacheOffsetKeys.add(k);
            keysWithOffsets.put(hash, new KeyWithOffset(k.getKey(), lowerCacheOffset));
        } else {
            // Exists in the cache with a higher offset than in the index.
            higherCacheOffsetKeys.add(k);
            keysWithOffsets.put(hash, new KeyWithOffset(k.getKey(), higherCacheOffset));
        }
    }
    // Update everything in the underlying index.
    val buckets = iw.locateBuckets(context.segment, keysWithOffsets.keySet(), context.timer).join();
    Collection<BucketUpdate> bucketUpdates = buckets.entrySet().stream().map(e -> {
        val builder = BucketUpdate.forBucket(e.getValue());
        val ko = keysWithOffsets.get(e.getKey());
        builder.withKeyUpdate(new BucketUpdate.KeyUpdate(ko.key, ko.offset, ko.offset, false));
        return builder.build();
    }).collect(Collectors.toList());
    iw.updateBuckets(context.segment, bucketUpdates, 0L, segmentLength, 0, TIMEOUT).join();
    // Update cache, and immediately clear out the tail section as we want to simulate a case where the values are already
    // thought to be indexed already.
    context.index.update(context.segment, toUpdateBatch(lowerCacheOffsetKeys), () -> CompletableFuture.completedFuture(lowerCacheOffset - updateBatchLength), context.timer).join();
    context.index.update(context.segment, toUpdateBatch(higherCacheOffsetKeys), () -> CompletableFuture.completedFuture(higherCacheOffset + BATCH_SIZE), context.timer).join();
    context.index.notifyIndexOffsetChanged(context.segment.getSegmentId(), higherCacheOffset + updateBatchLength, 0);
    // Check results. The expected offsets should already be stored in keysWithOffsets.
    for (val k : keys) {
        val hash = HASHER.hash(k.getKey());
        val actualOffset = context.index.getBucketOffsetDirect(context.segment, hash, context.timer).join();
        val expectedOffset = keysWithOffsets.get(hash).offset;
        Assert.assertEquals("Unexpected result from getBucketOffsetDirect.", expectedOffset, (long) actualOffset);
        val cachedOffset = context.index.getBucketOffsets(context.segment, Collections.singleton(hash), context.timer).join().get(hash);
        Assert.assertEquals("Unexpected result from getBucketOffsets.", expectedOffset, (long) cachedOffset);
    }
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) Random(java.util.Random) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Map(java.util.Map) CachePolicy(io.pravega.segmentstore.server.CachePolicy) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) CancellationException(java.util.concurrent.CancellationException) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) DirectMemoryCache(io.pravega.segmentstore.storage.cache.DirectMemoryCache) TestUtils(io.pravega.test.common.TestUtils) Futures(io.pravega.common.concurrent.Futures) CacheManager(io.pravega.segmentstore.server.CacheManager) ObjectClosedException(io.pravega.common.ObjectClosedException) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) BadKeyVersionException(io.pravega.segmentstore.contracts.tables.BadKeyVersionException) CacheStorage(io.pravega.segmentstore.storage.cache.CacheStorage) ArrayList(java.util.ArrayList) TableSegmentNotEmptyException(io.pravega.segmentstore.contracts.tables.TableSegmentNotEmptyException) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) SegmentType(io.pravega.segmentstore.contracts.SegmentType) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Timeout(org.junit.rules.Timeout) TimeoutTimer(io.pravega.common.TimeoutTimer) KeyNotExistsException(io.pravega.segmentstore.contracts.tables.KeyNotExistsException) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) SegmentMock(io.pravega.segmentstore.server.SegmentMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito(org.mockito.Mockito) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) Rule(org.junit.Rule) Comparator(java.util.Comparator) Assert(org.junit.Assert) TableEntry(io.pravega.segmentstore.contracts.tables.TableEntry) Collections(java.util.Collections) ByteArraySegment(io.pravega.common.util.ByteArraySegment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 45 with Timeout

use of org.junit.rules.Timeout in project pravega by pravega.

the class WriterTableProcessorTests method getRemovalLengths.

private long getRemovalLengths(long truncationOffset, List<TestBatchData> batches, TestContext context) throws Exception {
    val candidates = batches.stream().flatMap(b -> b.operations.stream()).filter(op -> op.getStreamSegmentOffset() >= truncationOffset).iterator();
    val expectedEntries = batches.get(batches.size() - 1).expectedEntries;
    long result = 0;
    while (candidates.hasNext()) {
        val op = candidates.next();
        val opData = new byte[(int) op.getLength()];
        val bytesRead = context.segmentMock.read(op.getStreamSegmentOffset(), (int) op.getLength(), TIMEOUT).readRemaining(opData, TIMEOUT);
        assert bytesRead == opData.length;
        val entryHeader = context.serializer.readHeader(new ByteArraySegment(opData).getBufferViewReader());
        if (!expectedEntries.containsKey(new ByteArraySegment(opData, entryHeader.getKeyOffset(), entryHeader.getKeyLength()))) {
            result += op.getLength();
        } else {
            break;
        }
    }
    return result;
}
Also used : lombok.val(lombok.val) ObjectClosedException(io.pravega.common.ObjectClosedException) TableAttributes(io.pravega.segmentstore.contracts.tables.TableAttributes) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cleanup(lombok.Cleanup) HashMap(java.util.HashMap) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) ArrayList(java.util.ArrayList) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Map(java.util.Map) Timeout(org.junit.rules.Timeout) Operation(io.pravega.segmentstore.server.logs.operations.Operation) TableKey(io.pravega.segmentstore.contracts.tables.TableKey) TimeoutTimer(io.pravega.common.TimeoutTimer) lombok.val(lombok.val) Test(org.junit.Test) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) SegmentMock(io.pravega.segmentstore.server.SegmentMock) StreamSegmentMetadata(io.pravega.segmentstore.server.containers.StreamSegmentMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) List(java.util.List) AttributeUpdateCollection(io.pravega.segmentstore.contracts.AttributeUpdateCollection) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) CachedStreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.CachedStreamSegmentAppendOperation) StreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Preconditions(com.google.common.base.Preconditions) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) Assert(org.junit.Assert) TableEntry(io.pravega.segmentstore.contracts.tables.TableEntry) Collections(java.util.Collections) ByteArraySegment(io.pravega.common.util.ByteArraySegment)

Aggregations

Timeout (org.junit.rules.Timeout)92 Rule (org.junit.Rule)91 Test (org.junit.Test)91 Assert (org.junit.Assert)84 AssertExtensions (io.pravega.test.common.AssertExtensions)81 Duration (java.time.Duration)81 ArrayList (java.util.ArrayList)80 lombok.val (lombok.val)79 TimeUnit (java.util.concurrent.TimeUnit)78 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)77 Cleanup (lombok.Cleanup)73 Collections (java.util.Collections)72 CompletableFuture (java.util.concurrent.CompletableFuture)72 HashMap (java.util.HashMap)69 ByteArraySegment (io.pravega.common.util.ByteArraySegment)68 Collectors (java.util.stream.Collectors)68 List (java.util.List)66 AtomicReference (java.util.concurrent.atomic.AtomicReference)66 IntentionalException (io.pravega.test.common.IntentionalException)62 Map (java.util.Map)62