Search in sources :

Example 26 with InMemoryMetadataStore

use of io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore in project pravega by pravega.

the class ChunkMetadataStoreTests method testEvictionFromBufferInParallel.

@Test
public void testEvictionFromBufferInParallel() {
    if (metadataStore instanceof InMemoryMetadataStore) {
        metadataStore.setMaxEntriesInCache(10);
        metadataStore.setMaxEntriesInTxnBuffer(10);
        val futures = new ArrayList<CompletableFuture<Void>>();
        for (int i = 0; i < 10000; i++) {
            final int k = i;
            futures.add(CompletableFuture.runAsync(() -> simpleScenarioForKey("Key" + k)));
        }
        Futures.allOf(futures);
    }
}
Also used : lombok.val(lombok.val) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 27 with InMemoryMetadataStore

use of io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore in project pravega by pravega.

the class ChunkMetadataStoreTests method testCommitFailureAfterEvictionFromBuffer.

@Test
public void testCommitFailureAfterEvictionFromBuffer() throws Exception {
    if (metadataStore instanceof InMemoryMetadataStore) {
        val testMetadataStore = (InMemoryMetadataStore) metadataStore;
        // Add some data
        try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
            txn.create(new MockStorageMetadata(KEY1, VALUE0));
            txn.commit().get();
        }
        // Force eviction of all cache
        testMetadataStore.evictAllEligibleEntriesFromBuffer();
        testMetadataStore.evictFromCache();
        // Set hook to cause failure on next commit.
        testMetadataStore.setWriteCallback(dummy -> CompletableFuture.failedFuture(new IntentionalException("Intentional")));
        // This txn will fail to commit.
        try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
            val metadata = (MockStorageMetadata) txn.get(KEY1).get();
            metadata.setValue(VALUE1);
            txn.update(metadata);
            txn.commit().get();
            Assert.fail();
        } catch (Exception e) {
            Assert.assertTrue(Exceptions.unwrap(e) instanceof IntentionalException);
        }
        // disable hook.
        testMetadataStore.setWriteCallback(null);
        // Validate that object in buffer remains unchanged.
        try (MetadataTransaction txn = metadataStore.beginTransaction(true, KEY1)) {
            assertEquals(txn.get(KEY1), KEY1, VALUE0);
        }
    }
}
Also used : lombok.val(lombok.val) MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) IntentionalException(io.pravega.test.common.IntentionalException) IntentionalException(io.pravega.test.common.IntentionalException) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 28 with InMemoryMetadataStore

use of io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore in project pravega by pravega.

the class ChunkMetadataStoreTests method testCommitSafetyWithSubset.

/**
 * Test that only one of the concurrent commit can proceed and others fail.
 *
 * @throws Exception Exception if any.
 */
@Test
public void testCommitSafetyWithSubset() throws Exception {
    if (metadataStore instanceof InMemoryMetadataStore) {
        // Set up the callback so that you can block writeAll call, to create situation where commit is in flight but not finished.
        final CompletableFuture<Void> futureToBlockOn = new CompletableFuture<Void>();
        ((InMemoryMetadataStore) metadataStore).setWriteCallback(transactionDataList -> {
            // Block only on KEY0.
            if (transactionDataList.stream().filter(t -> t.getKey().equals(KEY0)).findAny().isPresent()) {
                return futureToBlockOn.thenApplyAsync(v2 -> null, executorService());
            } else {
                return CompletableFuture.completedFuture(null);
            }
        });
        CompletableFuture<Void> commitFuture;
        try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY0, KEY1)) {
            assertNull(txn.get(KEY0));
            txn.create(new MockStorageMetadata(KEY0, VALUE0));
            Assert.assertNotNull(txn.get(KEY0));
            // Do not wait , this call should block
            commitFuture = txn.commit();
        }
        try {
            // Try commit on all keys
            try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
                assertNull(txn.get(KEY0));
                txn.create(new MockStorageMetadata(KEY0, VALUE1));
                assertEquals(txn.get(KEY0), KEY0, VALUE1);
                AssertExtensions.assertFutureThrows("Transaction should fail.", txn.commit(), ex -> ex instanceof StorageMetadataVersionMismatchException);
            }
            // Try commit only on only one key.
            try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY0)) {
                assertNull(txn.get(KEY0));
                txn.create(new MockStorageMetadata(KEY0, VALUE1));
                assertEquals(txn.get(KEY0), KEY0, VALUE1);
                AssertExtensions.assertFutureThrows("Transaction should fail.", txn.commit(), ex -> ex instanceof StorageMetadataVersionMismatchException);
            }
            // Try commit only on only one key.
            try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
                assertNull(txn.get(KEY0));
                txn.create(new MockStorageMetadata(KEY0, VALUE1));
                assertEquals(txn.get(KEY0), KEY0, VALUE1);
                AssertExtensions.assertFutureThrows("Transaction should fail.", txn.commit(), ex -> ex instanceof StorageMetadataVersionMismatchException);
            }
            // Make sure transactions on unrelated keys works.
            try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY4)) {
                assertNull(txn.get(KEY4));
                txn.create(new MockStorageMetadata(KEY4, VALUE4));
                assertEquals(txn.get(KEY4), KEY4, VALUE4);
                txn.commit().join();
            }
            try (MetadataTransaction txn = metadataStore.beginTransaction(true, KEY4)) {
                assertEquals(txn.get(KEY4), KEY4, VALUE4);
            }
        } finally {
            futureToBlockOn.complete(null);
        }
        commitFuture.join();
        // make sure the valid transaction went through.
        try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY0)) {
            assertEquals(txn.get(KEY0), KEY0, VALUE0);
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) Test(org.junit.Test)

Example 29 with InMemoryMetadataStore

use of io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore in project pravega by pravega.

the class ChunkMetadataStoreTests method testParallelReadsAfterEviction.

@Test
public void testParallelReadsAfterEviction() throws Exception {
    if (!(metadataStore instanceof InMemoryMetadataStore)) {
        return;
    }
    val testMetadataStore = (InMemoryMetadataStore) metadataStore;
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
        txn.create(new MockStorageMetadata(KEY1, VALUE1));
        txn.commit().get();
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY2)) {
        val metadata = new MockStorageMetadata(KEY2, VALUE2);
        txn.create(metadata);
        txn.markPinned(metadata);
        txn.commit().get();
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY4)) {
        val metadata = new MockStorageMetadata(KEY4, VALUE4);
        txn.create(metadata);
        txn.commit(true).get();
    }
    testMetadataStore.evictFromCache();
    testMetadataStore.getBackingStore().clear();
    Assert.assertNull(testMetadataStore.getBackingStore().get(KEY1));
    // Invoke eviction, this will move data from buffer to the Guava cache.
    testMetadataStore.evictAllEligibleEntriesFromBuffer();
    val futures = new ArrayList<CompletableFuture<Void>>();
    for (int i = 0; i < 100; i++) {
        futures.add(CompletableFuture.runAsync(() -> {
            readKey(KEY1, VALUE1);
            readKey(KEY2, VALUE2);
            readKey(KEY4, VALUE4);
            evictAll(testMetadataStore);
        }));
    }
    Futures.allOf(futures);
    evictAll(testMetadataStore);
    val futures2 = new ArrayList<CompletableFuture<Void>>();
    for (int i = 0; i < 100; i++) {
        futures.add(CompletableFuture.runAsync(() -> {
            readKey(KEY1, VALUE1);
            readKey(KEY2, VALUE2);
            readKey(KEY4, VALUE4);
            testMetadataStore.evictFromCache();
            // Invoke eviction, this will move data from buffer to the Guava cache.
            testMetadataStore.evictAllEligibleEntriesFromBuffer();
        }));
    }
    Futures.allOf(futures2);
    evictAll(testMetadataStore);
    val futures3 = new ArrayList<CompletableFuture<Void>>();
    for (int i = 0; i < 100; i++) {
        futures.add(CompletableFuture.runAsync(() -> {
            readKey(KEY1, VALUE1);
            readKey(KEY2, VALUE2);
            readKey(KEY4, VALUE4);
        }));
    }
    Futures.allOf(futures3);
}
Also used : lombok.val(lombok.val) MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 30 with InMemoryMetadataStore

use of io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore in project pravega by pravega.

the class ChunkMetadataStoreTests method setUp.

@Before
public void setUp() throws Exception {
    super.before();
    metadataStore = new InMemoryMetadataStore(ChunkedSegmentStorageConfig.DEFAULT_CONFIG, executorService());
}
Also used : InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) Before(org.junit.Before)

Aggregations

InMemoryMetadataStore (io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore)33 lombok.val (lombok.val)30 Test (org.junit.Test)25 Cleanup (lombok.Cleanup)20 BaseMetadataStore (io.pravega.segmentstore.storage.metadata.BaseMetadataStore)11 NoOpChunkStorage (io.pravega.segmentstore.storage.noop.NoOpChunkStorage)11 IntentionalException (io.pravega.test.common.IntentionalException)11 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InMemoryTaskQueueManager (io.pravega.segmentstore.storage.mocks.InMemoryTaskQueueManager)10 StorageFullException (io.pravega.segmentstore.storage.StorageFullException)9 StorageNotPrimaryException (io.pravega.segmentstore.storage.StorageNotPrimaryException)9 MockStorageMetadata (io.pravega.segmentstore.storage.mocks.MockStorageMetadata)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 CompletionException (java.util.concurrent.CompletionException)8 SegmentRollingPolicy (io.pravega.segmentstore.storage.SegmentRollingPolicy)6 StorageMetadataException (io.pravega.segmentstore.storage.metadata.StorageMetadataException)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 StorageMetadataVersionMismatchException (io.pravega.segmentstore.storage.metadata.StorageMetadataVersionMismatchException)5 StorageMetadataWritesFencedOutException (io.pravega.segmentstore.storage.metadata.StorageMetadataWritesFencedOutException)5 IOException (java.io.IOException)5