use of io.pravega.segmentstore.storage.mocks.MockStorageMetadata 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);
}
}
}
use of io.pravega.segmentstore.storage.mocks.MockStorageMetadata 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);
}
}
}
use of io.pravega.segmentstore.storage.mocks.MockStorageMetadata 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);
}
use of io.pravega.segmentstore.storage.mocks.MockStorageMetadata in project pravega by pravega.
the class ChunkMetadataStoreTests method testReadonlyTransaction.
@Test
public void testReadonlyTransaction() 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();
}
try (MetadataTransaction txn = metadataStore.beginTransaction(true, KEY0, KEY1)) {
assertNotNull(txn.get(KEY0));
Assert.assertNotNull(txn.get(KEY0));
AssertExtensions.assertThrows("create should throw an exception", () -> txn.create(new MockStorageMetadata(KEY0, VALUE0)), ex -> ex instanceof IllegalStateException);
AssertExtensions.assertThrows("create should throw an exception", () -> txn.update(new MockStorageMetadata(KEY0, VALUE0)), ex -> ex instanceof IllegalStateException);
AssertExtensions.assertThrows("create should throw an exception", () -> txn.delete(KEY0), ex -> ex instanceof IllegalStateException);
AssertExtensions.assertThrows("create should throw an exception", () -> txn.commit(), ex -> ex instanceof IllegalStateException);
AssertExtensions.assertThrows("create should throw an exception", () -> txn.commit(true), ex -> ex instanceof IllegalStateException);
AssertExtensions.assertThrows("create should throw an exception", () -> txn.commit(true, true), ex -> ex instanceof IllegalStateException);
}
}
use of io.pravega.segmentstore.storage.mocks.MockStorageMetadata in project pravega by pravega.
the class ChunkMetadataStoreTests method testSimpleScenario.
/**
* Test a simple scenario.
*
* @throws Exception Exception if any.
*/
@Test
public void testSimpleScenario() 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));
// Create data
txn.create(new MockStorageMetadata(KEY0, VALUE0));
txn.create(new MockStorageMetadata(KEY1, VALUE1));
txn.create(new MockStorageMetadata(KEY2, VALUE2));
// Do not access data before
txn.create(new MockStorageMetadata(KEY4, VALUE4));
assertEquals(txn.get(KEY0), KEY0, VALUE0);
assertEquals(txn.get(KEY1), KEY1, VALUE1);
assertEquals(txn.get(KEY2), KEY2, VALUE2);
assertEquals(txn.get(KEY4), KEY4, VALUE4);
// Update
txn.update(new MockStorageMetadata(KEY1, VALUE4));
assertEquals(txn.get(KEY0), KEY0, VALUE0);
assertEquals(txn.get(KEY1), KEY1, VALUE4);
assertEquals(txn.get(KEY2), KEY2, VALUE2);
// create
txn.create(new MockStorageMetadata(KEY3, VALUE5));
assertEquals(txn.get(KEY0), KEY0, VALUE0);
assertEquals(txn.get(KEY1), KEY1, VALUE4);
assertEquals(txn.get(KEY2), KEY2, VALUE2);
assertEquals(txn.get(KEY3), KEY3, VALUE5);
// delete
txn.delete(KEY2);
txn.delete(KEY3);
assertEquals(txn.get(KEY0), KEY0, VALUE0);
assertEquals(txn.get(KEY1), KEY1, VALUE4);
assertNull(txn.get(KEY3));
txn.commit().join();
assertEquals(txn.get(KEY0), KEY0, VALUE0);
assertEquals(txn.get(KEY1), KEY1, VALUE4);
assertEquals(txn.get(KEY4), KEY4, VALUE4);
assertNull(txn.get(KEY2));
assertNull(txn.get(KEY3));
} 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, VALUE4);
Assert.assertNull(metadataStore.read(KEY2).get().getValue());
Assert.assertNull(metadataStore.read(KEY3).get().getValue());
// See the effect of transaction after words
try (MetadataTransaction txn2 = metadataStore.beginTransaction(false, KEYS)) {
assertEquals(txn2.get(KEY0), KEY0, VALUE0);
assertEquals(txn2.get(KEY1), KEY1, VALUE4);
assertNull(txn2.get(KEY3));
txn2.update(new MockStorageMetadata(KEY0, VALUE6));
txn2.delete(KEY1);
assertEquals(txn2.get(KEY0), KEY0, VALUE6);
assertNull(txn2.get(KEY3));
assertNull(txn2.get(KEY1));
// Implicitly aborted
} 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));
assertNull(txn3.get(KEY3));
} 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, VALUE4);
Assert.assertNull(metadataStore.read(KEY2).get().getValue());
Assert.assertNull(metadataStore.read(KEY3).get().getValue());
}
Aggregations