Search in sources :

Example 1 with DLockToken

use of org.apache.geode.distributed.internal.locks.DLockToken in project geode by apache.

the class LockServiceMBeanBridge method listHeldLocks.

/**
   * 
   * @return currently held lock by this lock Service
   */
public String[] listHeldLocks() {
    Map<Object, DLockToken> tokenMap = lockService.snapshotService();
    Iterator<Object> it = tokenMap.keySet().iterator();
    List<String> listOfLocks = new ArrayList<String>();
    int j = 0;
    while (it.hasNext()) {
        Object lockObject = it.next();
        DLockToken token = tokenMap.get(lockObject);
        if (token.getUsageCount() > 0) {
            // As lock objects are of Object type, this is the best
            // info we can give
            listOfLocks.add(lockObject.toString());
        }
    }
    if (listOfLocks.size() > 0) {
        String[] retStr = new String[listOfLocks.size()];
        listOfLocks.toArray(retStr);
        return retStr;
    }
    return ManagementConstants.NO_DATA_STRING;
}
Also used : DLockToken(org.apache.geode.distributed.internal.locks.DLockToken) ArrayList(java.util.ArrayList)

Example 2 with DLockToken

use of org.apache.geode.distributed.internal.locks.DLockToken in project geode by apache.

the class DistributedLockServiceDUnitTest method testGrantTokenCleanup.

// static volatile boolean startedThreadVM2_testTokenCleanup;
// static volatile boolean finishedThreadVM2_testTokenCleanup;
// static volatile DLockToken grantorDLockToken_testTokenCleanup;
@Test
public void testGrantTokenCleanup() throws Exception {
    final String dlsName = getUniqueName();
    final VM vmGrantor = Host.getHost(0).getVM(0);
    final VM vm1 = Host.getHost(0).getVM(1);
    // final VM vm2 = Host.getHost(0).getVM(2);
    final String key1 = "key1";
    // vmGrantor creates grantor
    vmGrantor.invoke(new SerializableRunnable() {

        public void run() {
            LogWriterUtils.getLogWriter().info("[testGrantTokenCleanup] vmGrantor creates grantor");
            connectDistributedSystem();
            DistributedLockService dls = DistributedLockService.create(dlsName, getSystem());
            assertTrue(dls.lock(key1, -1, -1));
            assertTrue(dls.isLockGrantor());
            DLockGrantor grantor = ((DLockService) dls).getGrantor();
            assertNotNull(grantor);
            DLockGrantor.DLockGrantToken grantToken = grantor.getGrantToken(key1);
            assertNotNull(grantToken);
            LogWriterUtils.getLogWriter().info("[testGrantTokenCleanup] vmGrantor unlocks key1");
            dls.unlock(key1);
            assertNull(grantor.getGrantToken(key1));
        }
    });
    if (true)
        // TODO: remove early-out and complete this test
        return;
    // vm1 locks and frees key1
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            LogWriterUtils.getLogWriter().info("[testTokenCleanup] vm1 locks key1");
            connectDistributedSystem();
            DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
            assertTrue(dls.lock(key1, -1, -1));
            LogWriterUtils.getLogWriter().info("[testTokenCleanup] vm1 frees key1");
            dls.unlock(key1);
            // token for key1 still exists until freeResources is called
            assertNotNull(dls.getToken(key1));
            dls.freeResources(key1);
            // make sure token for key1 is gone
            DLockToken token = dls.getToken(key1);
            assertNull("Failed with bug 38180: " + token, token);
            // make sure there are NO tokens at all
            Collection tokens = dls.getTokens();
            assertEquals("Failed with bug 38180: tokens=" + tokens, 0, tokens.size());
        }
    });
    // vmGrantor frees key1
    vmGrantor.invoke(new SerializableRunnable() {

        public void run() {
            LogWriterUtils.getLogWriter().info("[testTokenCleanup] vmGrantor frees key1");
            DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
            if (true)
                // TODO: remove this when 38180/38179 are fixed
                return;
            // check for bug 38180...
            // make sure token for key1 is gone
            DLockToken token = dls.getToken(key1);
            assertNull("Failed with bug 38180: " + token, token);
            // make sure there are NO tokens at all
            Collection tokens = dls.getTokens();
            assertEquals("Failed with bug 38180: tokens=" + tokens, 0, tokens.size());
            // check for bug 38179...
            // make sure there are NO grant tokens at all
            DLockGrantor grantor = dls.getGrantor();
            Collection grantTokens = grantor.getGrantTokens();
            assertEquals("Failed with bug 38179: grantTokens=" + grantTokens, 0, grantTokens.size());
        // dls.freeResources(key1);
        // TODO: assert that DLockGrantToken for key1 is gone
        }
    });
}
Also used : DLockToken(org.apache.geode.distributed.internal.locks.DLockToken) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) DLockService(org.apache.geode.distributed.internal.locks.DLockService) Collection(java.util.Collection) DLockGrantor(org.apache.geode.distributed.internal.locks.DLockGrantor) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 3 with DLockToken

use of org.apache.geode.distributed.internal.locks.DLockToken in project geode by apache.

the class DLockDependencyMonitor method getHeldResources.

public Set<Dependency<Serializable, Thread>> getHeldResources(Thread[] allThreads) {
    InternalDistributedSystem ds = InternalDistributedSystem.getAnyInstance();
    if (ds == null) {
        return Collections.emptySet();
    }
    Set<Dependency<Serializable, Thread>> results = new HashSet<Dependency<Serializable, Thread>>();
    Map<String, DLockService> services = DLockService.snapshotAllServices();
    for (Map.Entry<String, DLockService> entry : services.entrySet()) {
        String serviceName = entry.getKey();
        DLockService service = entry.getValue();
        Map<Object, DLockToken> tokens = service.snapshotService();
        for (Map.Entry<Object, DLockToken> tokenEntry : tokens.entrySet()) {
            Object tokenName = tokenEntry.getKey();
            DLockToken token = tokenEntry.getValue();
            synchronized (token) {
                Thread holdingThread = token.getThread();
                if (holdingThread != null) {
                    results.add(new Dependency(new LockId(serviceName, (Serializable) tokenName), holdingThread));
                }
            }
        }
    }
    return results;
}
Also used : Serializable(java.io.Serializable) DLockToken(org.apache.geode.distributed.internal.locks.DLockToken) DLockService(org.apache.geode.distributed.internal.locks.DLockService) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with DLockToken

use of org.apache.geode.distributed.internal.locks.DLockToken in project geode by apache.

the class LockServiceMBeanBridge method listThreadsHoldingLock.

/**
   * Returns a list of thread which are blocked on some Object
   * 
   * @return map of object Name and thread holding lock on that object
   */
public Map<String, String> listThreadsHoldingLock() {
    Map<Object, DLockToken> tokenMap = lockService.snapshotService();
    Iterator<Object> it = tokenMap.keySet().iterator();
    Map<String, String> listOfLocks = new HashMap<String, String>();
    int j = 0;
    while (it.hasNext()) {
        Object lockObject = it.next();
        DLockToken token = tokenMap.get(lockObject);
        if (token.getUsageCount() > 0) {
            listOfLocks.put(lockObject.toString(), token.getThreadName());
        }
    }
    return listOfLocks;
}
Also used : DLockToken(org.apache.geode.distributed.internal.locks.DLockToken) HashMap(java.util.HashMap)

Example 5 with DLockToken

use of org.apache.geode.distributed.internal.locks.DLockToken in project geode by apache.

the class DistributedLockServiceDUnitTest method testTokenCleanup.

@Test
public void testTokenCleanup() throws Exception {
    final String dlsName = getUniqueName();
    final VM vmGrantor = Host.getHost(0).getVM(0);
    final VM vm1 = Host.getHost(0).getVM(1);
    // final VM vm2 = Host.getHost(0).getVM(2);
    final String key1 = "key1";
    // vmGrantor creates grantor
    vmGrantor.invoke(new SerializableRunnable() {

        public void run() {
            LogWriterUtils.getLogWriter().info("[testTokenCleanup] vmGrantor creates grantor");
            connectDistributedSystem();
            DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
            assertTrue(dls.lock(key1, -1, -1));
            assertTrue(dls.isLockGrantor());
            assertNotNull(dls.getToken(key1));
            dls.unlock(key1);
            assertNotNull(dls.getToken(key1));
            // token should be removed when freeResources is called
            dls.freeResources(key1);
            // assertNull(dls.getToken(key1));
            DLockToken token = dls.getToken(key1);
            assertNull("Failed with bug 38180: " + token, token);
            // make sure there are NO tokens at all
            Collection tokens = dls.getTokens();
            assertEquals("Failed with bug 38180: tokens=" + tokens, 0, tokens.size());
        }
    });
    // vm1 locks and frees key1
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            LogWriterUtils.getLogWriter().info("[testTokenCleanup] vm1 locks key1");
            connectDistributedSystem();
            DLockService dls = (DLockService) DistributedLockService.create(dlsName, getSystem());
            assertTrue(dls.lock(key1, -1, -1));
            assertFalse(dls.isLockGrantor());
            assertNotNull(dls.getToken(key1));
            dls.unlock(key1);
            assertNotNull(dls.getToken(key1));
            dls.freeResources(key1);
            // assertNull(dls.getToken(key1));
            DLockToken token = dls.getToken(key1);
            assertNull("Failed with bug 38180: " + token, token);
            // make sure there are NO tokens at all
            Collection tokens = dls.getTokens();
            assertEquals("Failed with bug 38180: tokens=" + tokens, 0, tokens.size());
        }
    });
    // vm1 tests recursion
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            LogWriterUtils.getLogWriter().info("[testTokenCleanup] vm1 tests recursion");
            connectDistributedSystem();
            DLockService dls = (DLockService) DistributedLockService.getServiceNamed(dlsName);
            // 1
            assertTrue(dls.lock(key1, -1, -1));
            assertEquals(1, dls.getToken(key1).getUsageCount());
            // 2
            assertTrue(dls.lock(key1, -1, -1));
            assertEquals(2, dls.getToken(key1).getUsageCount());
            // 3
            assertTrue(dls.lock(key1, -1, -1));
            assertEquals(3, dls.getToken(key1).getUsageCount());
            DLockToken token0 = dls.getToken(key1);
            assertNotNull(token0);
            Collection tokens = dls.getTokens();
            assertTrue(tokens.contains(token0));
            assertEquals(1, tokens.size());
            // 1
            dls.unlock(key1);
            assertEquals(2, dls.getToken(key1).getUsageCount());
            dls.freeResources(key1);
            DLockToken token1 = dls.getToken(key1);
            assertNotNull(token1);
            assertEquals(token0, token1);
            tokens = dls.getTokens();
            assertTrue(tokens.contains(token1));
            assertEquals(1, tokens.size());
            // 2
            dls.unlock(key1);
            assertEquals(1, dls.getToken(key1).getUsageCount());
            dls.freeResources(key1);
            assertNotNull(dls.getToken(key1));
            DLockToken token2 = dls.getToken(key1);
            assertNotNull(token2);
            assertEquals(token0, token2);
            tokens = dls.getTokens();
            assertTrue(tokens.contains(token2));
            assertEquals(1, tokens.size());
            // 3
            dls.unlock(key1);
            assertEquals(0, dls.getToken(key1).getUsageCount());
            dls.freeResources(key1);
            DLockToken token3 = dls.getToken(key1);
            assertNull("Failed with bug 38180: " + token3, token3);
            // make sure there are NO tokens at all
            tokens = dls.getTokens();
            assertEquals("Failed with bug 38180: tokens=" + tokens, 0, tokens.size());
        }
    });
}
Also used : DLockToken(org.apache.geode.distributed.internal.locks.DLockToken) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) DLockService(org.apache.geode.distributed.internal.locks.DLockService) Collection(java.util.Collection) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Aggregations

DLockToken (org.apache.geode.distributed.internal.locks.DLockToken)5 DLockService (org.apache.geode.distributed.internal.locks.DLockService)3 Collection (java.util.Collection)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2 VM (org.apache.geode.test.dunit.VM)2 DLockTest (org.apache.geode.test.junit.categories.DLockTest)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 DLockGrantor (org.apache.geode.distributed.internal.locks.DLockGrantor)1