Search in sources :

Example 6 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testDelete.

/**
 * Test delete
 *
 * @throws Exception Exception if any.
 */
@Test
public void testDelete() throws Exception {
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        assertNull(txn.get(KEY0));
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        Assert.assertNotNull(txn.get(KEY0));
        txn.delete(KEY0);
        assertNull(txn.get(KEY0));
        txn.commit().join();
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        assertNull(txn.get(KEY0));
        // Should be able to recreate
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        assertEquals(txn.get(KEY0), KEY0, VALUE0);
        txn.commit().join();
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        assertEquals(txn.get(KEY0), KEY0, VALUE0);
    }
}
Also used : MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) Test(org.junit.Test)

Example 7 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testLazyCommitWithNormalCommitWithNoBuffer.

/**
 * Tests simple scenario with lazy commit.
 *
 * @throws Exception
 */
@Test
public void testLazyCommitWithNormalCommitWithNoBuffer() throws Exception {
    metadataStore.setMaxEntriesInTxnBuffer(0);
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        // NO value should be found.
        assertNull(txn.get(KEY0));
        assertNull(txn.get(KEY1));
        assertNull(txn.get(KEY2));
        // Create data
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        txn.create(new MockStorageMetadata(KEY1, VALUE1));
        txn.create(new MockStorageMetadata(KEY2, VALUE2));
        assertEquals(txn.get(KEY0), KEY0, VALUE0);
        assertEquals(txn.get(KEY1), KEY1, VALUE1);
        assertEquals(txn.get(KEY2), KEY2, VALUE2);
        // This is normal commit.
        txn.commit().join();
    } catch (Exception e) {
        throw e;
    }
    // make sure they are stored correctly
    assertEquals((MockStorageMetadata) metadataStore.read(KEY0).get().getValue(), KEY0, VALUE0);
    assertEquals((MockStorageMetadata) metadataStore.read(KEY1).get().getValue(), KEY1, VALUE1);
    assertEquals((MockStorageMetadata) metadataStore.read(KEY2).get().getValue(), KEY2, VALUE2);
    try (MetadataTransaction txn1 = metadataStore.beginTransaction(false, KEYS)) {
        // Update
        txn1.update(new MockStorageMetadata(KEY1, VALUE4));
        assertEquals(txn1.get(KEY0), KEY0, VALUE0);
        assertEquals(txn1.get(KEY1), KEY1, VALUE4);
        assertEquals(txn1.get(KEY2), KEY2, VALUE2);
        // delete
        txn1.delete(KEY2);
        assertEquals(txn1.get(KEY0), KEY0, VALUE0);
        assertEquals(txn1.get(KEY1), KEY1, VALUE4);
        assertNull(txn1.get(KEY2));
        // Commit lazily.
        txn1.commit(true).join();
        assertEquals(txn1.get(KEY0), KEY0, VALUE0);
        assertEquals(txn1.get(KEY1), KEY1, VALUE4);
        assertNull(txn1.get(KEY2));
    } catch (Exception e) {
        throw e;
    }
    // make sure the persistent store actually has the correct value.
    assertEquals((MockStorageMetadata) metadataStore.read(KEY0).get().getValue(), KEY0, VALUE0);
    assertEquals((MockStorageMetadata) metadataStore.read(KEY1).get().getValue(), KEY1, VALUE4);
    Assert.assertNull(metadataStore.read(KEY2).get().getValue());
    // Note this transaction is aborted. So any changes made here should be totally ignored.
    try (MetadataTransaction txn2 = metadataStore.beginTransaction(false, KEYS)) {
        assertEquals(txn2.get(KEY0), KEY0, VALUE0);
        assertEquals(txn2.get(KEY1), KEY1, VALUE4);
        assertNull(txn2.get(KEY2));
        txn2.update(new MockStorageMetadata(KEY0, VALUE6));
        txn2.delete(KEY1);
        // This is still
        assertEquals(txn2.get(KEY0), KEY0, VALUE6);
        assertNull(txn2.get(KEY3));
        assertNull(txn2.get(KEY1));
        // abort
        txn2.abort().join();
    } catch (Exception e) {
        throw e;
    }
    // Should have no effect;
    try (MetadataTransaction txn3 = metadataStore.beginTransaction(false, KEYS)) {
        assertEquals(txn3.get(KEY0), KEY0, VALUE0);
        assertEquals(txn3.get(KEY1), KEY1, VALUE4);
        assertNull(txn3.get(KEY2));
    } catch (Exception e) {
        throw e;
    }
    try (MetadataTransaction txn4 = metadataStore.beginTransaction(false, KEYS)) {
        // Update
        txn4.update(new MockStorageMetadata(KEY0, VALUE5));
        txn4.update(new MockStorageMetadata(KEY1, VALUE6));
        assertEquals(txn4.get(KEY0), KEY0, VALUE5);
        assertEquals(txn4.get(KEY1), KEY1, VALUE6);
        assertNull(txn4.get(KEY2));
        // delete
        txn4.delete(KEY1);
        assertEquals(txn4.get(KEY0), KEY0, VALUE5);
        assertNull(txn4.get(KEY1));
        assertNull(txn4.get(KEY2));
        txn4.commit().join();
        assertEquals(txn4.get(KEY0), KEY0, VALUE5);
        assertNull(txn4.get(KEY1));
        assertNull(txn4.get(KEY2));
    } catch (Exception e) {
        throw e;
    }
    // make sure the persistent store is not affected.
    assertEquals((MockStorageMetadata) metadataStore.read(KEY0).get().getValue(), KEY0, VALUE5);
    Assert.assertNull(metadataStore.read(KEY1).get().getValue());
    Assert.assertNull(metadataStore.read(KEY2).get().getValue());
}
Also used : MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) IntentionalException(io.pravega.test.common.IntentionalException) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 8 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testLazyCommitWithNormalCommit.

/**
 * Tests simple scenario with lazy commit.
 *
 * @throws Exception
 */
@Test
public void testLazyCommitWithNormalCommit() throws Exception {
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        // NO value should be found.
        assertNull(txn.get(KEY0));
        assertNull(txn.get(KEY1));
        assertNull(txn.get(KEY2));
        // Create data
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        txn.create(new MockStorageMetadata(KEY1, VALUE1));
        txn.create(new MockStorageMetadata(KEY2, VALUE2));
        assertEquals(txn.get(KEY0), KEY0, VALUE0);
        assertEquals(txn.get(KEY1), KEY1, VALUE1);
        assertEquals(txn.get(KEY2), KEY2, VALUE2);
        txn.commit().join();
    } catch (Exception e) {
        throw e;
    }
    // make sure they are stored correctly
    assertEquals((MockStorageMetadata) metadataStore.read(KEY0).get().getValue(), KEY0, VALUE0);
    assertEquals((MockStorageMetadata) metadataStore.read(KEY1).get().getValue(), KEY1, VALUE1);
    assertEquals((MockStorageMetadata) metadataStore.read(KEY2).get().getValue(), KEY2, VALUE2);
    try (MetadataTransaction txn1 = metadataStore.beginTransaction(false, KEYS)) {
        // Update
        txn1.update(new MockStorageMetadata(KEY1, VALUE4));
        assertEquals(txn1.get(KEY0), KEY0, VALUE0);
        assertEquals(txn1.get(KEY1), KEY1, VALUE4);
        assertEquals(txn1.get(KEY2), KEY2, VALUE2);
        // delete
        txn1.delete(KEY2);
        assertEquals(txn1.get(KEY0), KEY0, VALUE0);
        assertEquals(txn1.get(KEY1), KEY1, VALUE4);
        assertNull(txn1.get(KEY2));
        // Commit lazily.
        txn1.commit(true).join();
        assertEquals(txn1.get(KEY0), KEY0, VALUE0);
        assertEquals(txn1.get(KEY1), KEY1, VALUE4);
        assertNull(txn1.get(KEY2));
    } catch (Exception e) {
        throw e;
    }
    // make sure the persistent store is not affected.
    assertEquals((MockStorageMetadata) metadataStore.read(KEY0).get().getValue(), KEY0, VALUE0);
    assertEquals((MockStorageMetadata) metadataStore.read(KEY1).get().getValue(), KEY1, VALUE1);
    assertEquals((MockStorageMetadata) metadataStore.read(KEY2).get().getValue(), KEY2, VALUE2);
    // Note this transaction is aborted. So any changes made here should be totally ignored.
    try (MetadataTransaction txn2 = metadataStore.beginTransaction(false, KEYS)) {
        assertEquals(txn2.get(KEY0), KEY0, VALUE0);
        assertEquals(txn2.get(KEY1), KEY1, VALUE4);
        assertNull(txn2.get(KEY2));
        txn2.update(new MockStorageMetadata(KEY0, VALUE6));
        txn2.delete(KEY1);
        // This is still
        assertEquals(txn2.get(KEY0), KEY0, VALUE6);
        assertNull(txn2.get(KEY3));
        assertNull(txn2.get(KEY1));
        // abort
        txn2.abort().join();
    } catch (Exception e) {
        throw e;
    }
    // Should have no effect;
    try (MetadataTransaction txn3 = metadataStore.beginTransaction(false, KEYS)) {
        assertEquals(txn3.get(KEY0), KEY0, VALUE0);
        assertEquals(txn3.get(KEY1), KEY1, VALUE4);
        assertNull(txn3.get(KEY2));
    } catch (Exception e) {
        throw e;
    }
    try (MetadataTransaction txn4 = metadataStore.beginTransaction(false, KEYS)) {
        // Update
        txn4.update(new MockStorageMetadata(KEY0, VALUE5));
        txn4.update(new MockStorageMetadata(KEY1, VALUE6));
        assertEquals(txn4.get(KEY0), KEY0, VALUE5);
        assertEquals(txn4.get(KEY1), KEY1, VALUE6);
        assertNull(txn4.get(KEY2));
        // delete
        txn4.delete(KEY1);
        assertEquals(txn4.get(KEY0), KEY0, VALUE5);
        assertNull(txn4.get(KEY1));
        assertNull(txn4.get(KEY2));
        txn4.commit().join();
        assertEquals(txn4.get(KEY0), KEY0, VALUE5);
        assertNull(txn4.get(KEY1));
        assertNull(txn4.get(KEY2));
    } catch (Exception e) {
        throw e;
    }
    // make sure the persistent store is not affected.
    assertEquals((MockStorageMetadata) metadataStore.read(KEY0).get().getValue(), KEY0, VALUE5);
    Assert.assertNull(metadataStore.read(KEY1).get().getValue());
    assertEquals((MockStorageMetadata) metadataStore.read(KEY2).get().getValue(), KEY2, VALUE2);
}
Also used : MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) IntentionalException(io.pravega.test.common.IntentionalException) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 9 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testWithNullExternalStep.

@Test
public void testWithNullExternalStep() throws Exception {
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEY0, KEY1)) {
        assertNull(txn.get(KEY0));
        txn.setExternalCommitStep(null);
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        Assert.assertNotNull(txn.get(KEY0));
        txn.commit().get();
    }
    try (MetadataTransaction txn = metadataStore.beginTransaction(true, KEY1)) {
        assertEquals(txn.get(KEY0), KEY0, VALUE0);
    }
}
Also used : MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) Test(org.junit.Test)

Example 10 with MockStorageMetadata

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

the class ChunkMetadataStoreTests method testGetAll.

@Test
public void testGetAll() throws Exception {
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        // NO value should be found.
        assertNull(txn.get(KEY0));
        assertNull(txn.get(KEY1));
        assertNull(txn.get(KEY3));
        Assert.assertEquals(0, metadataStore.getAllEntries().join().count());
        Assert.assertEquals(0, metadataStore.getAllKeys().join().count());
        // Create data
        txn.create(new MockStorageMetadata(KEY0, VALUE0));
        txn.create(new MockStorageMetadata(KEY1, VALUE1));
        txn.create(new MockStorageMetadata(KEY2, VALUE2));
        Assert.assertEquals(0, metadataStore.getAllEntries().join().count());
        Assert.assertEquals(0, metadataStore.getAllKeys().join().count());
        txn.commit().join();
    }
    val allEntries = metadataStore.getAllEntries().join().map(e -> (MockStorageMetadata) e).collect(Collectors.toMap(StorageMetadata::getKey, s -> s));
    Assert.assertEquals(3, allEntries.size());
    assertEquals(allEntries.get(KEY0), KEY0, VALUE0);
    assertEquals(allEntries.get(KEY1), KEY1, VALUE1);
    assertEquals(allEntries.get(KEY2), KEY2, VALUE2);
    val allKeys = metadataStore.getAllKeys().join().collect(Collectors.toSet());
    Assert.assertTrue(allKeys.contains(KEY0));
    Assert.assertTrue(allKeys.contains(KEY1));
    Assert.assertTrue(allKeys.contains(KEY2));
    try (MetadataTransaction txn = metadataStore.beginTransaction(false, KEYS)) {
        // delete data
        txn.delete(KEY0);
        txn.delete(KEY1);
        txn.commit().join();
    }
    val allEntriesSet = metadataStore.getAllEntries().join().collect(Collectors.toSet());
    val allEntriesAfter = allEntriesSet.stream().map(e -> (MockStorageMetadata) e).collect(Collectors.toMap(StorageMetadata::getKey, s -> s));
    Assert.assertEquals(1, allEntriesAfter.size());
    assertEquals(allEntriesAfter.get(KEY2), KEY2, VALUE2);
    Assert.assertFalse(allEntriesAfter.containsKey(KEY0));
    Assert.assertFalse(allEntriesAfter.containsKey(KEY1));
    val allKeysAfter = metadataStore.getAllKeys().join().collect(Collectors.toSet());
    Assert.assertFalse(allKeysAfter.contains(KEY0));
    Assert.assertFalse(allKeysAfter.contains(KEY1));
    Assert.assertTrue(allKeysAfter.contains(KEY2));
}
Also used : lombok.val(lombok.val) SneakyThrows(lombok.SneakyThrows) InMemoryMetadataStore(io.pravega.segmentstore.storage.mocks.InMemoryMetadataStore) MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ChunkedSegmentStorageConfig(io.pravega.segmentstore.storage.chunklayer.ChunkedSegmentStorageConfig) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Rule(org.junit.Rule) Ignore(org.junit.Ignore) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) After(org.junit.After) Timeout(org.junit.rules.Timeout) Assert(org.junit.Assert) Futures(io.pravega.common.concurrent.Futures) Before(org.junit.Before) MockStorageMetadata(io.pravega.segmentstore.storage.mocks.MockStorageMetadata) 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