Search in sources :

Example 1 with CacheTransaction

use of org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction in project janusgraph by JanusGraph.

the class ExpirationCacheTest method testExpiration.

private void testExpiration(Duration expirationTime) throws Exception {
    final int numKeys = 100, numCols = 10;
    loadStore(numKeys, numCols);
    // Replace cache with proper times
    cache = getCache(store, expirationTime, Duration.ZERO);
    final StaticBuffer key = BufferUtil.getIntBuffer(81);
    final List<StaticBuffer> keys = new ArrayList<>();
    keys.add(key);
    keys.add(BufferUtil.getIntBuffer(37));
    keys.add(BufferUtil.getIntBuffer(2));
    SliceQuery query = getQuery(2, 8);
    verifyResults(key, keys, query, 6);
    // Modify store directly
    StoreTransaction txs = getStoreTx();
    store.mutate(key, KeyColumnValueStore.NO_ADDITIONS, Lists.newArrayList(BufferUtil.getIntBuffer(5)), txs);
    txs.commit();
    Instant utime = times.getTime();
    // Should still see cached results
    verifyResults(key, keys, query, 6);
    // Sleep half way through expiration time
    times.sleepPast(utime.plus(expirationTime.dividedBy(2)));
    verifyResults(key, keys, query, 6);
    // Sleep past expiration time...
    times.sleepPast(utime.plus(expirationTime));
    // ...and just a little bit longer
    times.sleepFor(Duration.ofMillis(5));
    // Now the results should be different
    verifyResults(key, keys, query, 5);
    // If we modify through cache store...
    CacheTransaction tx = getCacheTx();
    cache.mutateEntries(key, KeyColumnValueStore.NO_ADDITIONS, Lists.newArrayList(getEntry(4, 4)), tx);
    tx.commit();
    store.resetCounter();
    // ...invalidation should happen and the result set is updated immediately
    verifyResults(key, keys, query, 4);
}
Also used : StoreTransaction(org.janusgraph.diskstorage.keycolumnvalue.StoreTransaction) Instant(java.time.Instant) ArrayList(java.util.ArrayList) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) CacheTransaction(org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction) SliceQuery(org.janusgraph.diskstorage.keycolumnvalue.SliceQuery) KeySliceQuery(org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery)

Example 2 with CacheTransaction

use of org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction in project janusgraph by JanusGraph.

the class Backend method beginTransaction.

/**
 * Opens a new transaction against all registered backend system wrapped in one {@link BackendTransaction}.
 *
 * @return
 * @throws BackendException
 */
public BackendTransaction beginTransaction(TransactionConfiguration configuration, KeyInformation.Retriever indexKeyRetriever) throws BackendException {
    StoreTransaction tx = storeManagerLocking.beginTransaction(configuration);
    // Cache
    CacheTransaction cacheTx = new CacheTransaction(tx, storeManagerLocking, bufferSize, maxWriteTime, configuration.hasEnabledBatchLoading());
    // Index transactions
    final Map<String, IndexTransaction> indexTx = new HashMap<>(indexes.size());
    for (Map.Entry<String, IndexProvider> entry : indexes.entrySet()) {
        indexTx.put(entry.getKey(), new IndexTransaction(entry.getValue(), indexKeyRetriever.get(entry.getKey()), configuration, maxWriteTime));
    }
    return new BackendTransaction(cacheTx, configuration, storeFeatures, edgeStore, indexStore, txLogStore, maxReadTime, indexTx, threadPool);
}
Also used : MetricInstrumentedIndexProvider(org.janusgraph.diskstorage.util.MetricInstrumentedIndexProvider) IndexProvider(org.janusgraph.diskstorage.indexing.IndexProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StoreTransaction(org.janusgraph.diskstorage.keycolumnvalue.StoreTransaction) IndexTransaction(org.janusgraph.diskstorage.indexing.IndexTransaction) CacheTransaction(org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 3 with CacheTransaction

use of org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction in project janusgraph by JanusGraph.

the class KCVSCacheTest method testSmallCache.

@Test
public void testSmallCache() throws Exception {
    // numCols must be greater than or equal to 10 as it is assumed below
    final int numKeys = 100, numCols = 10;
    final int repeats = 100, clearEvery = 20, numMulti = 10;
    loadStore(numKeys, numCols);
    // Repeatedly read from cache and clear in between
    int calls = 0;
    assertEquals(calls, store.getSliceCalls());
    for (int t = 0; t < repeats; t++) {
        if (t % clearEvery == 0) {
            cache.clearCache();
            calls += numKeys * 2 + 1;
        }
        CacheTransaction tx = getCacheTx();
        for (int i = 1; i <= numKeys; i++) {
            assertEquals(10, cache.getSlice(getQuery(i, 0, numCols + 1).setLimit(10), tx).size());
            assertEquals(3, cache.getSlice(getQuery(i, 2, 5), tx).size());
        }
        // Multi-query
        final List<StaticBuffer> keys = new ArrayList<>();
        for (int i = 10; i < 10 + numMulti; i++) keys.add(BufferUtil.getIntBuffer(i));
        Map<StaticBuffer, EntryList> result = cache.getSlice(keys, getQuery(4, 9), tx);
        assertEquals(keys.size(), result.size());
        for (StaticBuffer key : keys) assertTrue(result.containsKey(key));
        for (EntryList r : result.values()) {
            assertEquals(5, r.size());
        }
        tx.commit();
        assertEquals(calls, store.getSliceCalls());
    }
    store.resetCounter();
    // Check invalidation
    StaticBuffer key = BufferUtil.getIntBuffer(23);
    final List<StaticBuffer> keys = new ArrayList<>();
    keys.add(key);
    keys.add(BufferUtil.getIntBuffer(12));
    keys.add(BufferUtil.getIntBuffer(5));
    // Read
    CacheTransaction tx = getCacheTx();
    assertEquals(numCols, cache.getSlice(new KeySliceQuery(key, getQuery(0, numCols + 1)), tx).size());
    Map<StaticBuffer, EntryList> result = cache.getSlice(keys, getQuery(2, 8), tx);
    assertEquals(keys.size(), result.size());
    assertEquals(6, result.get(key).size());
    // Update
    final List<Entry> deletions = new ArrayList<>(numCols / 2);
    for (int j = 1; j <= numCols; j = j + 2) deletions.add(getEntry(j, j));
    cache.mutateEntries(key, KeyColumnValueStore.NO_ADDITIONS, deletions, tx);
    tx.commit();
    assertEquals(2, store.getSliceCalls());
    // Ensure updates are correctly read
    tx = getCacheTx();
    assertEquals(numCols / 2, cache.getSlice(new KeySliceQuery(key, getQuery(0, numCols + 1)), tx).size());
    result = cache.getSlice(keys, getQuery(2, 8), tx);
    assertEquals(keys.size(), result.size());
    assertEquals(3, result.get(key).size());
    tx.commit();
    assertEquals(4, store.getSliceCalls());
}
Also used : StaticArrayEntry(org.janusgraph.diskstorage.util.StaticArrayEntry) Entry(org.janusgraph.diskstorage.Entry) ArrayList(java.util.ArrayList) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) EntryList(org.janusgraph.diskstorage.EntryList) CacheTransaction(org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction) KeySliceQuery(org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery) Test(org.junit.jupiter.api.Test)

Example 4 with CacheTransaction

use of org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction in project janusgraph by JanusGraph.

the class MultiWriteKeyColumnValueStoreTest method open.

public void open() throws BackendException {
    manager = openStorageManager();
    tx = new CacheTransaction(manager.beginTransaction(getTxConfig()), manager, bufferSize, Duration.ofMillis(100), true);
    store1 = new NoKCVSCache(manager.openDatabase(storeName1));
    store2 = new NoKCVSCache(manager.openDatabase(storeName2));
}
Also used : NoKCVSCache(org.janusgraph.diskstorage.keycolumnvalue.cache.NoKCVSCache) CacheTransaction(org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction)

Example 5 with CacheTransaction

use of org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction in project janusgraph by JanusGraph.

the class ExpirationCacheTest method testGracePeriod.

private void testGracePeriod(Duration graceWait) throws Exception {
    final int minCleanupTriggerCalls = 5;
    final int numKeys = 100, numCols = 10;
    loadStore(numKeys, numCols);
    // Replace cache with proper times
    cache = getCache(store, Duration.ofDays(200), graceWait);
    final StaticBuffer key = BufferUtil.getIntBuffer(81);
    final List<StaticBuffer> keys = new ArrayList<>();
    keys.add(key);
    keys.add(BufferUtil.getIntBuffer(37));
    keys.add(BufferUtil.getIntBuffer(2));
    SliceQuery query = getQuery(2, 8);
    verifyResults(key, keys, query, 6);
    // If we modify through cache store...
    CacheTransaction tx = getCacheTx();
    cache.mutateEntries(key, KeyColumnValueStore.NO_ADDITIONS, Lists.newArrayList(getEntry(4, 4)), tx);
    tx.commit();
    Instant utime = times.getTime();
    store.resetCounter();
    // ...invalidation should happen and the result set is updated immediately
    verifyResults(key, keys, query, 5);
    assertEquals(2, store.getSliceCalls());
    // however, the key is expired and hence repeated calls need to go through to the store
    verifyResults(key, keys, query, 5);
    assertEquals(4, store.getSliceCalls());
    // however, when we sleep past the grace wait time and trigger a cleanup...
    times.sleepPast(utime.plus(graceWait));
    for (int t = 0; t < minCleanupTriggerCalls; t++) {
        assertEquals(5, cache.getSlice(new KeySliceQuery(key, query), tx).size());
        times.sleepFor(Duration.ofMillis(5));
    }
    // ...the cache should cache results again
    store.resetCounter();
    verifyResults(key, keys, query, 5);
    assertEquals(0, store.getSliceCalls());
    verifyResults(key, keys, query, 5);
    assertEquals(0, store.getSliceCalls());
}
Also used : Instant(java.time.Instant) ArrayList(java.util.ArrayList) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) CacheTransaction(org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction) SliceQuery(org.janusgraph.diskstorage.keycolumnvalue.SliceQuery) KeySliceQuery(org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery) KeySliceQuery(org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery)

Aggregations

CacheTransaction (org.janusgraph.diskstorage.keycolumnvalue.cache.CacheTransaction)6 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)4 KeySliceQuery (org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery)4 ArrayList (java.util.ArrayList)3 Instant (java.time.Instant)2 EntryList (org.janusgraph.diskstorage.EntryList)2 SliceQuery (org.janusgraph.diskstorage.keycolumnvalue.SliceQuery)2 StoreTransaction (org.janusgraph.diskstorage.keycolumnvalue.StoreTransaction)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Entry (org.janusgraph.diskstorage.Entry)1 IndexProvider (org.janusgraph.diskstorage.indexing.IndexProvider)1 IndexTransaction (org.janusgraph.diskstorage.indexing.IndexTransaction)1 NoKCVSCache (org.janusgraph.diskstorage.keycolumnvalue.cache.NoKCVSCache)1 MetricInstrumentedIndexProvider (org.janusgraph.diskstorage.util.MetricInstrumentedIndexProvider)1 StaticArrayEntry (org.janusgraph.diskstorage.util.StaticArrayEntry)1 Test (org.junit.jupiter.api.Test)1