Search in sources :

Example 1 with DLockRequestMessage

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

the class DLockGrantor method postReleaseReadLock.

/**
   * guarded.By {@link #suspendLock}
   */
private void postReleaseReadLock(RemoteThread rThread, Object lock) {
    final boolean isDebugEnabled_DLS = logger.isTraceEnabled(LogMarker.DLS);
    // handle release of regular lock
    // boolean permitSuspend = false;
    Integer integer = (Integer) readLockCountMap.get(rThread);
    int readLockCount = integer == null ? 0 : integer.intValue();
    // Assert.assertTrue(readLockCount > 0, rThread + " not found in " + readLockCountMap); // KIRK
    if (readLockCount < 1) {
        // hit bug 35749
        if (isDebugEnabled_DLS) {
            logger.trace(LogMarker.DLS, "[postReleaseReadLock] no locks are currently held by {}", rThread);
        }
        return;
    }
    readLockCount--;
    if (readLockCount == 0) {
        readLockCountMap.remove(rThread);
    } else {
        readLockCountMap.put(rThread, Integer.valueOf(readLockCount));
    }
    totalReadLockCount--;
    if (totalReadLockCount < 0) {
        if (isDebugEnabled_DLS) {
            logger.trace(LogMarker.DLS, "Total readlock count has dropped to {} for {}", totalReadLockCount, this);
        }
    }
    if (totalReadLockCount == 0 && !suspendQueue.isEmpty()) {
        final DLockRequestMessage nextRequest = (DLockRequestMessage) suspendQueue.getFirst();
        if (nextRequest.isSuspendLockingRequest()) {
            suspendLocking(nextRequest.getRemoteThread(), nextRequest.getLockId());
            writeLockWaiters--;
            permittedRequests.add(suspendQueue.removeFirst());
            checkWriteLockWaiters();
        } else {
            String s = new StringBuilder("\n (readLockCount=").append(readLockCount).append(", totalReadLockCount=").append(totalReadLockCount).append(", writeLockWaiters=").append(writeLockWaiters).append(",\nsuspendQueue=").append(suspendQueue).append(",\npermittedRequests=").append(permittedRequests).toString();
            logger.warn(LocalizedMessage.create(LocalizedStrings.DLockGrantor_RELEASED_REGULAR_LOCK_WITH_WAITING_READ_LOCK_0, s));
            Assert.assertTrue(false, LocalizedStrings.DLockGrantor_RELEASED_REGULAR_LOCK_WITH_WAITING_READ_LOCK_0.toString(s));
        }
    }
    if (isDebugEnabled_DLS) {
        logger.trace(LogMarker.DLS, "[postReleaseReadLock] new status {}", displayStatus(rThread, null));
    }
    checkTotalReadLockCount();
}
Also used : DLockRequestMessage(org.apache.geode.distributed.internal.locks.DLockRequestProcessor.DLockRequestMessage)

Example 2 with DLockRequestMessage

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

the class DistributedLockServiceDUnitTest method testDestroyLockServiceBeforeGrantRequest.

@Test
public void testDestroyLockServiceBeforeGrantRequest() throws Throwable {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    final String serviceName = getUniqueName();
    vm0.invoke(new SerializableRunnable("Create the grantor") {

        public void run() {
            connectDistributedSystem();
            final DistributedLockService service = DistributedLockService.create(serviceName, dlstSystem);
            // lock and unlock to make sure this vm is grantor
            assertTrue(service.lock("obj", -1, -1));
            service.unlock("obj");
        }
    });
    DistributionMessageObserver.setInstance(new DistributionMessageObserver() {

        @Override
        public void beforeSendMessage(DistributionManager dm, DistributionMessage message) {
            if (message instanceof DLockRequestMessage) {
                DistributedLockService.destroy(serviceName);
            }
        }
    });
    connectDistributedSystem();
    final DistributedLockService service = DistributedLockService.create(serviceName, dlstSystem);
    try {
        service.lock("obj", -1, -1);
        fail("The lock service should have been destroyed");
    } catch (LockServiceDestroyedException expected) {
    // Do nothing
    }
    vm0.invoke(new SerializableRunnable("check to make sure the lock is not orphaned") {

        public void run() {
            final DistributedLockService service = DistributedLockService.getServiceNamed(serviceName);
            // lock and unlock to make sure this vm is grantor
            assertTrue(service.lock("obj", -1, -1));
            service.unlock("obj");
        }
    });
}
Also used : DLockRequestMessage(org.apache.geode.distributed.internal.locks.DLockRequestProcessor.DLockRequestMessage) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 3 with DLockRequestMessage

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

the class DLockGrantor method checkWriteLockWaiters.

/**
   * Verify the waiters (for debugging)
   * 
   * guarded.By {@link #suspendLock}
   */
private void checkWriteLockWaiters() {
    if (!DEBUG_SUSPEND_LOCK) {
        return;
    }
    Assert.assertHoldsLock(this.suspendLock, true);
    int result = 0;
    Iterator it = this.suspendQueue.iterator();
    while (it.hasNext()) {
        DLockRequestMessage r = (DLockRequestMessage) it.next();
        if (r.isSuspendLockingRequest()) {
            result++;
        }
    }
    // while
    Assert.assertTrue(result == this.writeLockWaiters);
}
Also used : DLockRequestMessage(org.apache.geode.distributed.internal.locks.DLockRequestProcessor.DLockRequestMessage)

Example 4 with DLockRequestMessage

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

the class DLockGrantor method postReleaseSuspendLock.

/**
   * guarded.By {@link #suspendLock}
   */
private void postReleaseSuspendLock(RemoteThread rThread, Object lock) {
    if (!isLockingSuspendedBy(rThread)) {
        // hit bug related to 35749
        if (logger.isTraceEnabled(LogMarker.DLS)) {
            logger.trace(LogMarker.DLS, "[postReleaseSuspendLock] locking is no longer suspended by {}", rThread);
        }
        return;
    }
    boolean resume = true;
    Integer integer = (Integer) readLockCountMap.get(rThread);
    int readLockCount = integer == null ? 0 : integer.intValue();
    if (readLockCount == 0 && !suspendQueue.isEmpty()) {
        final DLockRequestMessage nextRequest = (DLockRequestMessage) suspendQueue.getFirst();
        if (nextRequest.isSuspendLockingRequest()) {
            resume = false;
            // final RemoteThread myRemoteThread = nextRequest.getRemoteThread();
            // hand-off suspendLocking while under sync...
            resumeLocking();
            suspendLocking(nextRequest.getRemoteThread(), nextRequest.getLockId());
            permittedRequests.add(suspendQueue.removeFirst());
            writeLockWaiters--;
            checkWriteLockWaiters();
        }
    }
    if (resume) {
        resumeLocking();
        // drain readLocks from suspendQueue into permittedRequests queue
        while (!suspendQueue.isEmpty()) {
            final DLockRequestMessage nextRequest = (DLockRequestMessage) suspendQueue.getFirst();
            if (nextRequest.isSuspendLockingRequest()) {
                Assert.assertTrue(writeLockWaiters > 0, "SuspendLocking request is waiting but writeLockWaiters is 0");
                break;
            }
            RemoteThread nextRThread = nextRequest.getRemoteThread();
            integer = (Integer) readLockCountMap.get(nextRThread);
            readLockCount = integer == null ? 0 : integer.intValue();
            readLockCount++;
            readLockCountMap.put(nextRThread, Integer.valueOf(readLockCount));
            totalReadLockCount++;
            checkTotalReadLockCount();
            permittedRequests.add(suspendQueue.removeFirst());
        }
    }
    if (logger.isTraceEnabled(LogMarker.DLS)) {
        logger.trace(LogMarker.DLS, "[postReleaseSuspendLock] new status {}", displayStatus(rThread, null));
    }
}
Also used : DLockRequestMessage(org.apache.geode.distributed.internal.locks.DLockRequestProcessor.DLockRequestMessage)

Example 5 with DLockRequestMessage

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

the class DLockGrantor method handleSuspendTimeouts.

/**
   * Iterates through a copy of suspendQueue and handles any requests that have timed out.
   * <p>
   * Synchronizes on suspendLock.
   * 
   * @return the next smallest timeout in the suspendQueue
   */
protected long handleSuspendTimeouts() {
    long smallestTimeout = Long.MAX_VALUE;
    synchronized (suspendLock) {
        if (suspendQueue.isEmpty())
            return smallestTimeout;
        if (isDestroyed())
            return smallestTimeout;
    }
    List timeouts = new ArrayList();
    List copySuspendQueue = null;
    synchronized (suspendLock) {
        copySuspendQueue = new ArrayList(suspendQueue);
    }
    for (Iterator iter = copySuspendQueue.iterator(); iter.hasNext(); ) {
        DLockRequestMessage req = (DLockRequestMessage) iter.next();
        if (req.checkForTimeout()) {
            // sends DLockResponseMessage if timeout
            cleanupSuspendState(req);
            timeouts.add(req);
        } else {
            long timeout = req.getTimeoutTS();
            if (timeout < smallestTimeout) {
                smallestTimeout = timeout;
            }
        }
    }
    int localDebugHandleSuspendTimeouts = 0;
    synchronized (suspendLock) {
        localDebugHandleSuspendTimeouts = debugHandleSuspendTimeouts;
    }
    if (localDebugHandleSuspendTimeouts > 0) {
        try {
            logger.info(LogMarker.DLS, LocalizedMessage.create(LocalizedStrings.DLockGrantor_DEBUGHANDLESUSPENDTIMEOUTS_SLEEPING_FOR__0, localDebugHandleSuspendTimeouts));
            Thread.sleep(localDebugHandleSuspendTimeouts);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    if (!timeouts.isEmpty()) {
        synchronized (suspendLock) {
            if (writeLockWaiters > 0) {
                // suspenders exist... must iterate through for safe removal
                for (Iterator iter = timeouts.iterator(); iter.hasNext(); ) {
                    DLockRequestMessage req = (DLockRequestMessage) iter.next();
                    // attempt to remove timed out req from suspendQueue
                    if (suspendQueue.remove(req)) {
                        // request was still in suspendQueue, so check if suspender
                        if (req.isSuspendLockingRequest()) {
                            writeLockWaiters--;
                        }
                    }
                }
            // for
            } else {
                // no suspenders so safe to removeAll
                Assert.assertTrue(writeLockWaiters == 0, "Grantor state writeLockWaiters changed while holding suspendLock");
                suspendQueue.removeAll(timeouts);
            }
            checkWriteLockWaiters();
        }
    // synchronized
    }
    return smallestTimeout;
}
Also used : DLockRequestMessage(org.apache.geode.distributed.internal.locks.DLockRequestProcessor.DLockRequestMessage) IdentityArrayList(org.apache.geode.internal.cache.IdentityArrayList) IdentityArrayList(org.apache.geode.internal.cache.IdentityArrayList)

Aggregations

DLockRequestMessage (org.apache.geode.distributed.internal.locks.DLockRequestProcessor.DLockRequestMessage)6 IdentityArrayList (org.apache.geode.internal.cache.IdentityArrayList)2 LockServiceDestroyedException (org.apache.geode.distributed.LockServiceDestroyedException)1 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)1 DistributionMessage (org.apache.geode.distributed.internal.DistributionMessage)1 DistributionMessageObserver (org.apache.geode.distributed.internal.DistributionMessageObserver)1 Host (org.apache.geode.test.dunit.Host)1 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)1 VM (org.apache.geode.test.dunit.VM)1 DLockTest (org.apache.geode.test.junit.categories.DLockTest)1 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)1 Test (org.junit.Test)1