Search in sources :

Example 11 with PrimitiveLongIntMap

use of org.neo4j.collection.primitive.PrimitiveLongIntMap in project neo4j by neo4j.

the class ForsetiClient method reEnterExclusive.

@Override
public boolean reEnterExclusive(ResourceType resourceType, long resourceId) {
    stateHolder.incrementActiveClients(this);
    try {
        PrimitiveLongIntMap heldLocks = exclusiveLockCounts[resourceType.typeId()];
        int heldCount = heldLocks.get(resourceId);
        if (heldCount != -1) {
            // We already have a lock on this, just increment our local reference counter.
            heldLocks.put(resourceId, Math.incrementExact(heldCount));
            return true;
        }
        // We didn't hold a lock already, so we cannot re-enter.
        return false;
    } finally {
        stateHolder.decrementActiveClients();
    }
}
Also used : PrimitiveLongIntMap(org.neo4j.collection.primitive.PrimitiveLongIntMap)

Example 12 with PrimitiveLongIntMap

use of org.neo4j.collection.primitive.PrimitiveLongIntMap in project neo4j by neo4j.

the class ForsetiClient method reEnterShared.

@Override
public boolean reEnterShared(ResourceType resourceType, long resourceId) {
    stateHolder.incrementActiveClients(this);
    try {
        PrimitiveLongIntMap heldShareLocks = sharedLockCounts[resourceType.typeId()];
        PrimitiveLongIntMap heldExclusiveLocks = exclusiveLockCounts[resourceType.typeId()];
        int heldCount = heldShareLocks.get(resourceId);
        if (heldCount != -1) {
            // We already have a lock on this, just increment our local reference counter.
            heldShareLocks.put(resourceId, Math.incrementExact(heldCount));
            return true;
        }
        if (heldExclusiveLocks.containsKey(resourceId)) {
            // We already have an exclusive lock, so just leave that in place. When the exclusive lock is released,
            // it will be automatically downgraded to a shared lock, since we bumped the share lock reference count.
            heldShareLocks.put(resourceId, 1);
            return true;
        }
        // We didn't hold a lock already, so we cannot re-enter.
        return false;
    } finally {
        stateHolder.decrementActiveClients();
    }
}
Also used : PrimitiveLongIntMap(org.neo4j.collection.primitive.PrimitiveLongIntMap)

Example 13 with PrimitiveLongIntMap

use of org.neo4j.collection.primitive.PrimitiveLongIntMap in project neo4j by neo4j.

the class ForsetiClient method acquireShared.

@Override
public void acquireShared(LockTracer tracer, ResourceType resourceType, long... resourceIds) throws AcquireLockTimeoutException {
    hasLocks = true;
    stateHolder.incrementActiveClients(this);
    try {
        // Grab the global lock map we will be using
        ConcurrentMap<Long, ForsetiLockManager.Lock> lockMap = lockMaps[resourceType.typeId()];
        // And grab our local lock maps
        PrimitiveLongIntMap heldShareLocks = sharedLockCounts[resourceType.typeId()];
        PrimitiveLongIntMap heldExclusiveLocks = exclusiveLockCounts[resourceType.typeId()];
        for (long resourceId : resourceIds) {
            // First, check if we already hold this as a shared lock
            int heldCount = heldShareLocks.get(resourceId);
            if (heldCount != -1) {
                // We already have a lock on this, just increment our local reference counter.
                heldShareLocks.put(resourceId, Math.incrementExact(heldCount));
                continue;
            }
            // Second, check if we hold it as an exclusive lock
            if (heldExclusiveLocks.containsKey(resourceId)) {
                // We already have an exclusive lock, so just leave that in place.
                // When the exclusive lock is released, it will be automatically downgraded to a shared lock,
                // since we bumped the share lock reference count.
                heldShareLocks.put(resourceId, 1);
                continue;
            }
            // We don't hold the lock, so we need to grab it via the global lock map
            int tries = 0;
            SharedLock mySharedLock = null;
            long waitStartMillis = clock.millis();
            LockWaitEvent waitEvent = null;
            try {
                // Retry loop
                while (true) {
                    assertValid(waitStartMillis, resourceType, resourceId);
                    // Check if there is a lock for this entity in the map
                    ForsetiLockManager.Lock existingLock = lockMap.get(resourceId);
                    // No lock
                    if (existingLock == null) {
                        // Try to create a new shared lock
                        if (mySharedLock == null) {
                            mySharedLock = new SharedLock(this);
                        }
                        if (lockMap.putIfAbsent(resourceId, mySharedLock) == null) {
                            // Success, we now hold the shared lock.
                            break;
                        } else {
                            continue;
                        }
                    } else // Someone holds shared lock on this entity, try and get in on that action
                    if (existingLock instanceof SharedLock) {
                        if (((SharedLock) existingLock).acquire(this)) {
                            // Success!
                            break;
                        }
                    } else // Someone holds an exclusive lock on this entity
                    if (existingLock instanceof ExclusiveLock) {
                    // We need to wait, just let the loop run.
                    } else {
                        throw new UnsupportedOperationException("Unknown lock type: " + existingLock);
                    }
                    if (waitEvent == null) {
                        waitEvent = tracer.waitForLock(false, resourceType, resourceId);
                    }
                    applyWaitStrategy(resourceType, tries++);
                    // And take note of who we are waiting for. This is used for deadlock detection.
                    markAsWaitingFor(existingLock, resourceType, resourceId);
                }
            } finally {
                if (waitEvent != null) {
                    waitEvent.close();
                }
            }
            // Got the lock, no longer waiting for anyone.
            clearWaitList();
            // Make a local note about the fact that we now hold this lock
            heldShareLocks.put(resourceId, 1);
        }
    } finally {
        stateHolder.decrementActiveClients();
    }
}
Also used : PrimitiveLongIntMap(org.neo4j.collection.primitive.PrimitiveLongIntMap) LockWaitEvent(org.neo4j.kernel.impl.locking.LockWaitEvent) ActiveLock(org.neo4j.kernel.impl.locking.ActiveLock)

Example 14 with PrimitiveLongIntMap

use of org.neo4j.collection.primitive.PrimitiveLongIntMap in project neo4j by neo4j.

the class PrimitiveLongMapTest method longIntKeyVisitorShouldSeeAllEntriesIfItDoesNotBreakOut.

@SuppressWarnings("unchecked")
@Test
public void longIntKeyVisitorShouldSeeAllEntriesIfItDoesNotBreakOut() {
    // GIVEN
    PrimitiveLongIntMap map = Primitive.longIntMap();
    map.put(1, 100);
    map.put(2, 200);
    map.put(3, 300);
    PrimitiveLongVisitor<RuntimeException> visitor = mock(PrimitiveLongVisitor.class);
    // WHEN
    map.visitKeys(visitor);
    // THEN
    verify(visitor).visited(1);
    verify(visitor).visited(2);
    verify(visitor).visited(3);
    verifyNoMoreInteractions(visitor);
}
Also used : PrimitiveLongIntMap(org.neo4j.collection.primitive.PrimitiveLongIntMap) Test(org.junit.Test)

Example 15 with PrimitiveLongIntMap

use of org.neo4j.collection.primitive.PrimitiveLongIntMap in project neo4j by neo4j.

the class PrimitiveLongMapTest method shouldOnlyContainAddedValues_2.

@Test
public void shouldOnlyContainAddedValues_2() throws Exception {
    // GIVEN
    PrimitiveLongIntMap map = Primitive.longIntMap();
    map.put(913910231, 25);
    map.put(102310782, 40);
    map.put(634960377, 32);
    map.put(947168147, 96);
    map.put(947430652, 26);
    map.put(1391472521, 72);
    map.put(7905512, 10);
    map.put(7905512, 2);
    map.put(1391472521, 66);
    map.put(824376092, 79);
    map.remove(750639810);
    map.put(947168147, 61);
    map.put(831409018, 57);
    map.put(241941283, 76);
    map.put(824376092, 45);
    map.remove(2125994926);
    map.put(824376092, 47);
    map.put(1477982280, 1);
    map.remove(2129508263);
    map.put(1477982280, 41);
    map.put(642178985, 69);
    map.put(1447441709, 85);
    map.put(642178985, 27);
    map.put(875840384, 72);
    map.put(1967716733, 55);
    map.put(1965379174, 5);
    map.put(913910231, 40);
    // WHEN/THEN
    boolean existedBefore = map.containsKey(947430652);
    int valueBefore = map.get(947430652);
    int removed = map.remove(947430652);
    boolean existsAfter = map.containsKey(947430652);
    int valueAfter = map.get(947430652);
    assertTrue("947430652 should exist before removing here", existedBefore);
    assertEquals("value before should be 26", 26, valueBefore);
    assertEquals("value returned from remove should be 26", 26, removed);
    assertFalse("947430652 should not exist", existsAfter);
    assertEquals("value after removing should be -1", -1, valueAfter);
}
Also used : PrimitiveLongIntMap(org.neo4j.collection.primitive.PrimitiveLongIntMap) Test(org.junit.Test)

Aggregations

PrimitiveLongIntMap (org.neo4j.collection.primitive.PrimitiveLongIntMap)16 Test (org.junit.Test)7 ActiveLock (org.neo4j.kernel.impl.locking.ActiveLock)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 LockWaitEvent (org.neo4j.kernel.impl.locking.LockWaitEvent)2 HashSet (java.util.HashSet)1 ResourceType (org.neo4j.storageengine.api.lock.ResourceType)1