Search in sources :

Example 1 with Locker

use of org.janusgraph.diskstorage.locking.Locker in project janusgraph by JanusGraph.

the class LockKeyColumnValueStoreTest method testLocksOnMultipleStores.

@Test
public void testLocksOnMultipleStores() throws Exception {
    // the number of stores must be a multiple of 3
    final int numStores = 6;
    final StaticBuffer key = BufferUtil.getLongBuffer(1);
    final StaticBuffer col = BufferUtil.getLongBuffer(2);
    final StaticBuffer val2 = BufferUtil.getLongBuffer(8);
    // Create mocks
    LockerProvider mockLockerProvider = createStrictMock(LockerProvider.class);
    Locker mockLocker = createStrictMock(Locker.class);
    // Create EVCSManager with mockLockerProvider
    ExpectedValueCheckingStoreManager expManager = new ExpectedValueCheckingStoreManager(manager[0], "multi_store_lock_mgr", mockLockerProvider, Duration.ofMillis(100L));
    // Begin EVCTransaction
    BaseTransactionConfig txCfg = StandardBaseTransactionConfig.of(times);
    ExpectedValueCheckingTransaction tx = expManager.beginTransaction(txCfg);
    // openDatabase calls getLocker, and we do it numStores times
    expect(mockLockerProvider.getLocker(anyObject(String.class))).andReturn(mockLocker).times(numStores);
    // acquireLock calls writeLock, and we do it 2/3 * numStores times
    mockLocker.writeLock(eq(new KeyColumn(key, col)), eq(tx.getConsistentTx()));
    expectLastCall().times(numStores / 3 * 2);
    // mutateMany calls checkLocks, and we do it 2/3 * numStores times
    mockLocker.checkLocks(tx.getConsistentTx());
    expectLastCall().times(numStores / 3 * 2);
    replay(mockLockerProvider);
    replay(mockLocker);
    /*
         * Acquire a lock on several distinct stores (numStores total distinct
         * stores) and build mutations.
         */
    ImmutableMap.Builder<String, Map<StaticBuffer, KCVMutation>> builder = ImmutableMap.builder();
    for (int i = 0; i < numStores; i++) {
        String storeName = "multi_store_lock_" + i;
        KeyColumnValueStore s = expManager.openDatabase(storeName);
        if (i % 3 < 2)
            s.acquireLock(key, col, null, tx);
        if (i % 3 > 0) {
            builder.put(storeName, ImmutableMap.of(key, new KCVMutation(ImmutableList.of(StaticArrayEntry.of(col, val2)), ImmutableList.of())));
        }
    }
    // Mutate
    expManager.mutateMany(builder.build(), tx);
    // Shutdown
    expManager.close();
    // Check the mocks
    verify(mockLockerProvider);
    verify(mockLocker);
}
Also used : Locker(org.janusgraph.diskstorage.locking.Locker) ConsistentKeyLocker(org.janusgraph.diskstorage.locking.consistentkey.ConsistentKeyLocker) ExpectedValueCheckingStoreManager(org.janusgraph.diskstorage.locking.consistentkey.ExpectedValueCheckingStoreManager) StandardBaseTransactionConfig(org.janusgraph.diskstorage.util.StandardBaseTransactionConfig) LockerProvider(org.janusgraph.diskstorage.locking.LockerProvider) ExpectedValueCheckingTransaction(org.janusgraph.diskstorage.locking.consistentkey.ExpectedValueCheckingTransaction) KeyColumn(org.janusgraph.diskstorage.util.KeyColumn) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 2 with Locker

use of org.janusgraph.diskstorage.locking.Locker in project janusgraph by JanusGraph.

the class Backend method openManagedLocker.

private static Locker openManagedLocker(String classname, String lockerName) {
    try {
        Class c = Class.forName(classname);
        Constructor constructor = c.getConstructor();
        Object instance = constructor.newInstance();
        Method method = c.getMethod("openLocker", String.class);
        Object o = method.invoke(instance, lockerName);
        return (Locker) o;
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Could not find implementation class: " + classname);
    } catch (InstantiationException | ClassCastException e) {
        throw new IllegalArgumentException("Could not instantiate implementation: " + classname, e);
    } catch (NoSuchMethodException e) {
        throw new IllegalArgumentException("Could not find method when configuring locking for: " + classname, e);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException("Could not access method when configuring locking for: " + classname, e);
    } catch (InvocationTargetException e) {
        throw new IllegalArgumentException("Could not invoke method when configuring locking for: " + classname, e);
    }
}
Also used : Locker(org.janusgraph.diskstorage.locking.Locker) ConsistentKeyLocker(org.janusgraph.diskstorage.locking.consistentkey.ConsistentKeyLocker) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with Locker

use of org.janusgraph.diskstorage.locking.Locker in project janusgraph by JanusGraph.

the class ExpectedValueCheckingTransaction method checkAllLocks.

/**
 * Check all locks attempted by earlier
 * {@link KeyColumnValueStore#acquireLock(StaticBuffer, StaticBuffer, StaticBuffer, StoreTransaction)}
 * calls using this transaction.
 *
 * @throws org.janusgraph.diskstorage.BackendException
 */
void checkAllLocks() throws BackendException {
    StoreTransaction lt = getConsistentTx();
    for (ExpectedValueCheckingStore store : expectedValuesByStore.keySet()) {
        Locker locker = store.getLocker();
        // Ignore locks on stores without a locker
        if (null == locker)
            continue;
        locker.checkLocks(lt);
    }
}
Also used : Locker(org.janusgraph.diskstorage.locking.Locker)

Example 4 with Locker

use of org.janusgraph.diskstorage.locking.Locker in project janusgraph by JanusGraph.

the class Backend method getLocker.

@Override
public Locker getLocker(String lockerName) {
    Preconditions.checkNotNull(lockerName);
    Locker l = lockers.get(lockerName);
    if (null == l) {
        l = lockerCreator.apply(lockerName);
        final Locker x = lockers.putIfAbsent(lockerName, l);
        if (null != x) {
            l = x;
        }
    }
    return l;
}
Also used : Locker(org.janusgraph.diskstorage.locking.Locker) ConsistentKeyLocker(org.janusgraph.diskstorage.locking.consistentkey.ConsistentKeyLocker)

Aggregations

Locker (org.janusgraph.diskstorage.locking.Locker)4 ConsistentKeyLocker (org.janusgraph.diskstorage.locking.consistentkey.ConsistentKeyLocker)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 LockerProvider (org.janusgraph.diskstorage.locking.LockerProvider)1 ExpectedValueCheckingStoreManager (org.janusgraph.diskstorage.locking.consistentkey.ExpectedValueCheckingStoreManager)1 ExpectedValueCheckingTransaction (org.janusgraph.diskstorage.locking.consistentkey.ExpectedValueCheckingTransaction)1 KeyColumn (org.janusgraph.diskstorage.util.KeyColumn)1 StandardBaseTransactionConfig (org.janusgraph.diskstorage.util.StandardBaseTransactionConfig)1 Test (org.junit.Test)1