Search in sources :

Example 1 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testTransactionCommitIllegalStateException.

@Test
public void testTransactionCommitIllegalStateException() throws Exception {
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY0, KEY1)) {
        assertNull(txn.get(KEY0));
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        Assert.assertNotNull(txn.get(KEY0));
        txn.commit().join();
        AssertExtensions.assertThrows("commit should throw an exception", () -> txn.commit(), ex -> ex instanceof IllegalStateException);
        AssertExtensions.assertThrows("commit should throw an exception", () -> txn.commit(true), ex -> ex instanceof IllegalStateException);
        AssertExtensions.assertThrows("commit should throw an exception", () -> txn.commit(true, true), ex -> ex instanceof IllegalStateException);
        AssertExtensions.assertThrows("abort should throw an exception", () -> txn.abort(), ex -> ex instanceof IllegalStateException);
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY0, KEY1)) {
        txn.update(new MockStorageMetadata(KEY0, VALUE0));
        Assert.assertNotNull(txn.get(KEY0));
        txn.abort().join();
        AssertExtensions.assertThrows("create should throw an exception", () -> txn.commit(), ex -> ex instanceof IllegalStateException);
        AssertExtensions.assertThrows("commit should throw an exception", () -> txn.commit(true), ex -> ex instanceof IllegalStateException);
        AssertExtensions.assertThrows("commit should throw an exception", () -> txn.commit(true, true), ex -> ex instanceof IllegalStateException);
        AssertExtensions.assertThrows("abort should throw an exception", () -> txn.abort(), ex -> ex instanceof IllegalStateException);
    }
}
Also used : MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) Test(org.junit.Test)

Example 2 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method simpleScenarioForKey.

@SneakyThrows
private void simpleScenarioForKey(String key) {
    // Create key.
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, key)) {
        txn.create(new MockStorageMetadata(key, VALUE0));
        assertEquals(txn.get(key), key, VALUE0);
        txn.commit().get();
    }
    // Modify
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, key)) {
        assertEquals(txn.get(key), key, VALUE0);
        txn.update(new MockStorageMetadata(key, VALUE1));
        assertEquals(txn.get(key), key, VALUE1);
        txn.commit().get();
    }
    // Delete
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, key)) {
        assertEquals(txn.get(key), key, VALUE1);
        txn.delete(key);
        assertNull(txn.get(key));
        txn.commit().get();
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(true, key)) {
        assertNull(txn.get(key));
    }
}
Also used : MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) SneakyThrows(lombok.SneakyThrows)

Example 3 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testEviction.

@Test
public void testEviction() throws Exception {
    if (!(metadataStore instanceof InMemoryMetadataStore)) {
        return;
    }
    val testMetadataStore = (InMemoryMetadataStore) metadataStore;
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
        txn.create(new MockStorageMetadata(KEY1, VALUE0));
        txn.commit().get();
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
        // Delete data from backing store , the data should be still in buffer.
        testMetadataStore.evictFromCache();
        testMetadataStore.getBackingStore().clear();
        Assert.assertNull(testMetadataStore.getBackingStore().get(KEY1));
        assertEquals(txn.get(KEY1), KEY1, VALUE0);
        // Invoke eviction, this will move data from buffer to the Guava cache.
        testMetadataStore.evictAllEligibleEntriesFromBuffer();
        // But data should be still there in Guava cache.
        // It will be inserted into buffer.
        assertEquals(txn.get(KEY1), KEY1, VALUE0);
        // Forcibly delete it from cache
        testMetadataStore.evictFromCache();
        testMetadataStore.getBackingStore().clear();
        Assert.assertNull(testMetadataStore.getBackingStore().get(KEY1));
        // But data should be still there in buffer.
        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) Test(org.junit.Test)

Example 4 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testEvictionPinnedKeys.

@Test
public void testEvictionPinnedKeys() throws Exception {
    if (!(metadataStore instanceof InMemoryMetadataStore)) {
        return;
    }
    val testMetadataStore = (InMemoryMetadataStore) metadataStore;
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
        val metadata = new MockStorageMetadata(KEY1, VALUE0);
        txn.create(metadata);
        txn.markPinned(metadata);
        txn.commit().get();
        Assert.assertNull(testMetadataStore.getBackingStore().get(KEY1));
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY1)) {
        // Delete data from backing store , the data should be still in buffer.
        testMetadataStore.evictFromCache();
        testMetadataStore.getBackingStore().clear();
        Assert.assertNull(testMetadataStore.getBackingStore().get(KEY1));
        assertEquals(txn.get(KEY1), KEY1, VALUE0);
        // Invoke eviction, this will move data from buffer to the Guava cache.
        testMetadataStore.evictAllEligibleEntriesFromBuffer();
        // Forcibly delete it from cache
        testMetadataStore.evictFromCache();
        testMetadataStore.getBackingStore().clear();
        Assert.assertNull(testMetadataStore.getBackingStore().get(KEY1));
        // But data should be still there in buffer.
        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) Test(org.junit.Test)

Example 5 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testTransactionFailedForMultipleUpdates.

@Test
public void testTransactionFailedForMultipleUpdates() throws Exception {
    // Step 1: Set up
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        assertNull(txn.get(KEY0));
        assertNull(txn.get(KEY1));
        assertNull(txn.get(KEY3));
        // Create data
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        txn.create(new MockStorageMetadata(KEY1, VALUE1));
        txn.create(new MockStorageMetadata(KEY3, VALUE2));
        assertEquals(txn.get(KEY0), KEY0, VALUE0);
        assertEquals(txn.get(KEY1), KEY1, VALUE1);
        assertEquals(txn.get(KEY3), KEY3, VALUE2);
        txn.commit(false).join();
        // Same data after commit.
        assertEquals(txn.get(KEY0), KEY0, VALUE0);
        assertEquals(txn.get(KEY1), KEY1, VALUE1);
        assertEquals(txn.get(KEY3), KEY3, VALUE2);
    } catch (Exception e) {
        throw e;
    }
    // Step 2: Create a test transaction
    try (MetadataTransaction txn0 = metadataStore.beginTransaction(false, KEYS)) {
        // Step 2 A: Make some updates, but don't commit yet.
        txn0.update(new MockStorageMetadata(KEY0, VALUE5));
        assertEquals(txn0.get(KEY0), KEY0, VALUE5);
        assertEquals(txn0.get(KEY3), KEY3, VALUE2);
        // Step 3 : Start a parallel transaction
        try (MetadataTransaction txn2 = metadataStore.beginTransaction(false, KEYS)) {
            // Step 3 A: Make some updates, but don't commit yet.
            txn2.update(new MockStorageMetadata(KEY0, VALUE6));
            assertEquals(txn2.get(KEY0), KEY0, VALUE6);
            assertEquals(txn2.get(KEY3), KEY3, VALUE2);
            // Step 3 B: Commit. It should succeed.
            txn2.commit(false).join();
            assertEquals(txn2.get(KEY0), KEY0, VALUE6);
            assertEquals(txn2.get(KEY3), KEY3, VALUE2);
        } catch (Exception e) {
            throw e;
        }
        // Step 4 : Start a second parallel transaction
        try (MetadataTransaction txn2 = metadataStore.beginTransaction(false, KEYS)) {
            // Step 4 : Data committed by earlier transaction should be visible to other new transactions
            assertEquals(txn2.get(KEY0), KEY0, VALUE6);
            assertEquals(txn2.get(KEY3), KEY3, VALUE2);
        } catch (Exception e) {
            throw e;
        }
        // A committed transactions should not have any effect on this transaction.
        assertEquals(txn0.get(KEY0), KEY0, VALUE5);
        assertEquals(txn0.get(KEY3), KEY3, VALUE2);
        try {
            txn0.commit(false).join();
            Assert.fail("Transaction should be aborted.");
        } catch (CompletionException e) {
            Assert.assertTrue(e.getCause() instanceof StorageMetadataVersionMismatchException);
        }
    }
}
Also used : MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) CompletionException(java.util.concurrent.CompletionException) IntentionalException(io.pravega.test.common.IntentionalException) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Aggregations

MockStorageMetadata (io.pravega.segmentstore.storage.mocks.MockStorageMetadata)25 Test (org.junit.Test)24 CompletionException (java.util.concurrent.CompletionException)13 IntentionalException (io.pravega.test.common.IntentionalException)11 lombok.val (lombok.val)11 InMemoryMetadataStore (io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore)10 CompletableFuture (java.util.concurrent.CompletableFuture)5 ArrayList (java.util.ArrayList)3 SneakyThrows (lombok.SneakyThrows)3 Ignore (org.junit.Ignore)3 Exceptions (io.pravega.common.Exceptions)2 Futures (io.pravega.common.concurrent.Futures)2 StreamSegmentExistsException (io.pravega.segmentstore.contracts.StreamSegmentExistsException)2 TableStore (io.pravega.segmentstore.contracts.tables.TableStore)2 ChunkedSegmentStorageConfig (io.pravega.segmentstore.storage.chunklayer.ChunkedSegmentStorageConfig)2 InMemoryTableStore (io.pravega.segmentstore.storage.mocks.InMemoryTableStore)2 AssertExtensions (io.pravega.test.common.AssertExtensions)2 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)2 Collectors (java.util.stream.Collectors)2 Cleanup (lombok.Cleanup)2