Search in sources :

Example 16 with DLockService

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

the class ManagementListener method handleEvent.

/**
   * Handles various GFE resource life-cycle methods vis-a-vis Management and Monitoring
   * 
   * It checks for race conditions cases by calling shouldProceed();
   *
   * @param event Management event for which invocation has happened
   * @param resource the GFE resource type
   */
public void handleEvent(ResourceEvent event, Object resource) {
    if (!shouldProceed(event)) {
        return;
    }
    switch(event) {
        case CACHE_CREATE:
            InternalCache createdCache = (InternalCache) resource;
            adapter.handleCacheCreation(createdCache);
            break;
        case CACHE_REMOVE:
            InternalCache removedCache = (InternalCache) resource;
            adapter.handleCacheRemoval(removedCache);
            break;
        case REGION_CREATE:
            Region createdRegion = (Region) resource;
            adapter.handleRegionCreation(createdRegion);
            break;
        case REGION_REMOVE:
            Region removedRegion = (Region) resource;
            adapter.handleRegionRemoval(removedRegion);
            break;
        case DISKSTORE_CREATE:
            DiskStore createdDisk = (DiskStore) resource;
            adapter.handleDiskCreation(createdDisk);
            break;
        case DISKSTORE_REMOVE:
            DiskStore removedDisk = (DiskStore) resource;
            adapter.handleDiskRemoval(removedDisk);
            break;
        case GATEWAYRECEIVER_CREATE:
            GatewayReceiver createdRecv = (GatewayReceiver) resource;
            adapter.handleGatewayReceiverCreate(createdRecv);
            break;
        case GATEWAYRECEIVER_START:
            GatewayReceiver startedRecv = (GatewayReceiver) resource;
            adapter.handleGatewayReceiverStart(startedRecv);
            break;
        case GATEWAYRECEIVER_STOP:
            GatewayReceiver stoppededRecv = (GatewayReceiver) resource;
            adapter.handleGatewayReceiverStop(stoppededRecv);
            break;
        case GATEWAYSENDER_CREATE:
            GatewaySender sender = (GatewaySender) resource;
            adapter.handleGatewaySenderCreation(sender);
            break;
        case GATEWAYSENDER_START:
            GatewaySender startedSender = (GatewaySender) resource;
            adapter.handleGatewaySenderStart(startedSender);
            break;
        case GATEWAYSENDER_STOP:
            GatewaySender stoppedSender = (GatewaySender) resource;
            adapter.handleGatewaySenderStop(stoppedSender);
            break;
        case GATEWAYSENDER_PAUSE:
            GatewaySender pausedSender = (GatewaySender) resource;
            adapter.handleGatewaySenderPaused(pausedSender);
            break;
        case GATEWAYSENDER_RESUME:
            GatewaySender resumedSender = (GatewaySender) resource;
            adapter.handleGatewaySenderResumed(resumedSender);
            break;
        case LOCKSERVICE_CREATE:
            DLockService createdLockService = (DLockService) resource;
            adapter.handleLockServiceCreation(createdLockService);
            break;
        case LOCKSERVICE_REMOVE:
            DLockService removedLockService = (DLockService) resource;
            adapter.handleLockServiceRemoval(removedLockService);
            break;
        case MANAGER_CREATE:
            adapter.handleManagerCreation();
            break;
        case MANAGER_START:
            adapter.handleManagerStart();
            break;
        case MANAGER_STOP:
            adapter.handleManagerStop();
            break;
        case ASYNCEVENTQUEUE_CREATE:
            AsyncEventQueue queue = (AsyncEventQueue) resource;
            adapter.handleAsyncEventQueueCreation(queue);
            break;
        case ASYNCEVENTQUEUE_REMOVE:
            AsyncEventQueue removedQueue = (AsyncEventQueue) resource;
            adapter.handleAsyncEventQueueRemoval(removedQueue);
            break;
        case SYSTEM_ALERT:
            AlertDetails details = (AlertDetails) resource;
            adapter.handleSystemNotification(details);
            break;
        case CACHE_SERVER_START:
            CacheServer startedServer = (CacheServer) resource;
            adapter.handleCacheServerStart(startedServer);
            break;
        case CACHE_SERVER_STOP:
            CacheServer stoppedServer = (CacheServer) resource;
            adapter.handleCacheServerStop(stoppedServer);
            break;
        case LOCATOR_START:
            Locator loc = (Locator) resource;
            adapter.handleLocatorStart(loc);
            break;
        case CACHE_SERVICE_CREATE:
            CacheService service = (CacheService) resource;
            adapter.handleCacheServiceCreation(service);
            break;
        default:
            break;
    }
}
Also used : DiskStore(org.apache.geode.cache.DiskStore) GatewaySender(org.apache.geode.cache.wan.GatewaySender) Locator(org.apache.geode.distributed.Locator) AlertDetails(org.apache.geode.management.internal.AlertDetails) AsyncEventQueue(org.apache.geode.cache.asyncqueue.AsyncEventQueue) DLockService(org.apache.geode.distributed.internal.locks.DLockService) InternalCache(org.apache.geode.internal.cache.InternalCache) Region(org.apache.geode.cache.Region) GatewayReceiver(org.apache.geode.cache.wan.GatewayReceiver) CacheServer(org.apache.geode.cache.server.CacheServer) CacheService(org.apache.geode.internal.cache.CacheService)

Example 17 with DLockService

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

the class DistributedLockServiceDUnitTest method tryLock.

/**
   * Accessed via reflection. DO NOT REMOVE
   */
protected static Boolean tryLock(String serviceName, Object name, Long wait) {
    DLockService service = DLockService.getInternalServiceNamed(serviceName);
    boolean locked = service.lock(name, wait.longValue(), -1, true);
    return Boolean.valueOf(locked);
}
Also used : DLockService(org.apache.geode.distributed.internal.locks.DLockService)

Example 18 with DLockService

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

the class DistributedLockServiceDUnitTest method testCreateDestroy.

@Test
public void testCreateDestroy() throws Exception {
    final String serviceName = getUniqueName();
    final String abc = "abc";
    // create and destroy dls
    assertNull(DistributedLockService.getServiceNamed(serviceName));
    DistributedLockService service = DistributedLockService.create(serviceName, getSystem());
    assertSame(service, DistributedLockService.getServiceNamed(serviceName));
    DistributedLockService.destroy(serviceName);
    // assert attempt to use dls throws LockServiceDestroyedException
    try {
        service.lock(abc, -1, -1);
        fail("didn't get LockServiceDestroyedException");
    } catch (LockServiceDestroyedException ex) {
    }
    // assert that destroyed dls is no longer available
    service = DistributedLockService.getServiceNamed(serviceName);
    assertNull("" + service, service);
    // recreate the dls
    service = DistributedLockService.create(serviceName, getSystem());
    assertTrue(!((DLockService) service).isDestroyed());
    ((DLockService) service).checkDestroyed();
    // get the same dls from another thread and hold a lock
    Thread thread = new Thread(new Runnable() {

        public void run() {
            DistributedLockService dls = DistributedLockService.getServiceNamed(serviceName);
            assertTrue(!((DLockService) dls).isDestroyed());
            ((DLockService) dls).checkDestroyed();
            // get lock on abc and hold it
            dls.lock(abc, -1, -1);
        }
    });
    thread.start();
    ThreadUtils.join(thread, 30 * 1000);
    // start a new thread to wait for lock on abc
    AsyncInvocation remoteWaitingThread = Host.getHost(0).getVM(0).invokeAsync(new SerializableRunnable() {

        public void run() {
            DistributedLockService dls = DistributedLockService.create(serviceName, getSystem());
            try {
                // waiting to get lock abc
                dls.lock(abc, -1, -1);
                fail("remoteWaitingThread got lock after dls destroyed");
            } catch (LockServiceDestroyedException expected) {
                return;
            }
            fail("remoteWaitingThread lock failed to throw LockServiceDestroyedException");
        }
    });
    // loop will handle race condition with 1 sec sleep and retry
    int retry = 10;
    for (int i = 0; i < retry; i++) {
        try {
            // destroy DLS and free up remoteWaitingThread
            Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

                public void run() {
                    DistributedLockService.destroy(serviceName);
                }
            });
        } catch (RMIException e) {
            // race condition: remoteWaitingThread probably hasn't created DLS yet
            if (i < retry && e.getCause() instanceof IllegalArgumentException) {
                sleep(1000);
                continue;
            } else {
                throw e;
            }
        }
        // completed so break out of loop
        break;
    }
    DistributedLockService.destroy(serviceName);
    // make sure remoteWaitingThread stopped waiting and threw LockServiceDestroyedException
    ThreadUtils.join(remoteWaitingThread, 10 * 1000);
    if (remoteWaitingThread.exceptionOccurred()) {
        Throwable e = remoteWaitingThread.getException();
        org.apache.geode.test.dunit.Assert.fail(e.getMessage(), e);
    }
    // make sure LockServiceDestroyedException is thrown
    try {
        service.lock(abc, -1, -1);
        fail("didn't get LockServiceDestroyedException");
    } catch (LockServiceDestroyedException ex) {
    }
    // make sure getServiceNamed returns null
    service = DistributedLockService.getServiceNamed(serviceName);
    assertNull("" + service, service);
}
Also used : RMIException(org.apache.geode.test.dunit.RMIException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) DLockService(org.apache.geode.distributed.internal.locks.DLockService) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) RemoteThread(org.apache.geode.distributed.internal.locks.RemoteThread) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 19 with DLockService

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

the class DistributedLockServiceDUnitTest method identifyLockGrantor.

/**
   * Accessed via reflection. DO NOT REMOVE
   * 
   * @param serviceName
   * @return
   */
public static InternalDistributedMember identifyLockGrantor(String serviceName) {
    DLockService service = (DLockService) DistributedLockService.getServiceNamed(serviceName);
    assertNotNull(service);
    InternalDistributedMember grantor = service.getLockGrantorId().getLockGrantorMember();
    assertNotNull(grantor);
    logInfo("In identifyLockGrantor - grantor is " + grantor);
    return grantor;
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DLockService(org.apache.geode.distributed.internal.locks.DLockService)

Example 20 with DLockService

use of org.apache.geode.distributed.internal.locks.DLockService 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

DLockService (org.apache.geode.distributed.internal.locks.DLockService)26 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)8 Test (org.junit.Test)8 DLockTest (org.apache.geode.test.junit.categories.DLockTest)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)6 HashSet (java.util.HashSet)5 VM (org.apache.geode.test.dunit.VM)5 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)4 Serializable (java.io.Serializable)3 DLockToken (org.apache.geode.distributed.internal.locks.DLockToken)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Region (org.apache.geode.cache.Region)2 DLockGrantor (org.apache.geode.distributed.internal.locks.DLockGrantor)2 DLockRecoverGrantorProcessor (org.apache.geode.distributed.internal.locks.DLockRecoverGrantorProcessor)2 DLockRemoteToken (org.apache.geode.distributed.internal.locks.DLockRemoteToken)2 RemoteThread (org.apache.geode.distributed.internal.locks.RemoteThread)2