Search in sources :

Example 21 with DLockService

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

the class DLockManagementDUnitTest method createLockServiceGrantor.

private void createLockServiceGrantor(final VM memberVM) {
    memberVM.invoke("createLockServiceGrantor", () -> {
        assertThat(DistributedLockService.getServiceNamed(LOCK_SERVICE_NAME)).isNull();
        DLockService lockService = (DLockService) DistributedLockService.create(LOCK_SERVICE_NAME, this.managementTestRule.getCache().getDistributedSystem());
        DistributedMember grantor = lockService.getLockGrantorId().getLockGrantorMember();
        assertThat(grantor).isNotNull();
        LockServiceMXBean lockServiceMXBean = awaitLockServiceMXBean(LOCK_SERVICE_NAME);
        assertThat(lockServiceMXBean).isNotNull();
        assertThat(lockServiceMXBean.isDistributed()).isTrue();
        assertThat(lockServiceMXBean.getName()).isEqualTo(LOCK_SERVICE_NAME);
        assertThat(lockServiceMXBean.isLockGrantor()).isTrue();
        assertThat(lockServiceMXBean.fetchGrantorMember()).isEqualTo(this.managementTestRule.getDistributedMember().getId());
    });
}
Also used : DLockService(org.apache.geode.distributed.internal.locks.DLockService) DistributedMember(org.apache.geode.distributed.DistributedMember)

Example 22 with DLockService

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

the class TXLockServiceDUnitTest method testTXRecoverGrantorMessageProcessor.

@Test
public void testTXRecoverGrantorMessageProcessor() throws Exception {
    TXLockService.createDTLS();
    checkDLockRecoverGrantorMessageProcessor();
    /*
     * call TXRecoverGrantorMessageProcessor.process directly to make sure that correct behavior
     * occurs
     */
    // get txLock and hold it
    final Set participants = Collections.EMPTY_SET;
    final List regionLockReqs = new ArrayList();
    regionLockReqs.add(new TXRegionLockRequestImpl("/testTXRecoverGrantorMessageProcessor", new HashSet(Arrays.asList(new String[] { "KEY-1", "KEY-2", "KEY-3", "KEY-4" }))));
    TXLockService dtls = TXLockService.getDTLS();
    TXLockId txLockId = dtls.txLock(regionLockReqs, participants);
    // async call TXRecoverGrantorMessageProcessor.process
    final DLockService dlock = ((TXLockServiceImpl) dtls).getInternalDistributedLockService();
    final TestDLockRecoverGrantorProcessor testProc = new TestDLockRecoverGrantorProcessor(dlock.getDistributionManager(), Collections.EMPTY_SET);
    assertEquals("No valid processorId", true, testProc.getProcessorId() > -1);
    final DLockRecoverGrantorProcessor.DLockRecoverGrantorMessage msg = new DLockRecoverGrantorProcessor.DLockRecoverGrantorMessage();
    msg.setServiceName(dlock.getName());
    msg.setProcessorId(testProc.getProcessorId());
    msg.setSender(dlock.getDistributionManager().getId());
    Thread thread = new Thread(() -> {
        TXRecoverGrantorMessageProcessor proc = (TXRecoverGrantorMessageProcessor) dlock.getDLockRecoverGrantorMessageProcessor();
        proc.processDLockRecoverGrantorMessage(dlock.getDistributionManager(), msg);
    });
    thread.setName("TXLockServiceDUnitTest thread");
    thread.setDaemon(true);
    thread.start();
    await("waiting for recovery message to block").atMost(999, TimeUnit.MILLISECONDS).until(() -> {
        return ((TXLockServiceImpl) dtls).isRecovering();
    });
    dtls.release(txLockId);
    // check results to verify no locks were provided in the reply
    await("waiting for thread to exit").atMost(30, TimeUnit.SECONDS).until(() -> {
        return !thread.isAlive();
    });
    assertFalse(((TXLockServiceImpl) dtls).isRecovering());
    assertEquals("testTXRecoverGrantor_replyCode_PASS is false", true, testTXRecoverGrantor_replyCode_PASS);
    assertEquals("testTXRecoverGrantor_heldLocks_PASS is false", true, testTXRecoverGrantor_heldLocks_PASS);
}
Also used : DLockRecoverGrantorProcessor(org.apache.geode.distributed.internal.locks.DLockRecoverGrantorProcessor) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) DLockRecoverGrantorMessage(org.apache.geode.distributed.internal.locks.DLockRecoverGrantorProcessor.DLockRecoverGrantorMessage) DLockRecoverGrantorMessage(org.apache.geode.distributed.internal.locks.DLockRecoverGrantorProcessor.DLockRecoverGrantorMessage) TXRegionLockRequestImpl(org.apache.geode.internal.cache.TXRegionLockRequestImpl) DLockService(org.apache.geode.distributed.internal.locks.DLockService) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 23 with DLockService

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

the class TXLockServiceDUnitTest method testTXGrantorMigration.

@Test
public void testTXGrantorMigration() throws Exception {
    // first make sure some other VM is the grantor
    Host.getHost(0).getVM(0).invoke("become lock grantor", () -> {
        TXLockService.createDTLS();
        TXLockService vm0dtls = TXLockService.getDTLS();
        DLockService vm0dlock = ((TXLockServiceImpl) vm0dtls).getInternalDistributedLockService();
        vm0dlock.becomeLockGrantor();
    });
    TXLockService.createDTLS();
    checkDLockRecoverGrantorMessageProcessor();
    /*
     * call TXRecoverGrantorMessageProcessor.process directly to make sure that correct behavior
     * occurs
     */
    // get txLock and hold it
    final List regionLockReqs = new ArrayList();
    regionLockReqs.add(new TXRegionLockRequestImpl("/testTXRecoverGrantorMessageProcessor2", new HashSet(Arrays.asList(new String[] { "KEY-1", "KEY-2", "KEY-3", "KEY-4" }))));
    TXLockService dtls = TXLockService.getDTLS();
    TXLockId txLockId = dtls.txLock(regionLockReqs, Collections.EMPTY_SET);
    final DLockService dlock = ((TXLockServiceImpl) dtls).getInternalDistributedLockService();
    // GEODE-2024: now cause grantor migration while holding the recoveryReadLock.
    // It will lock up in TXRecoverGrantorMessageProcessor until the recoveryReadLock
    // is released. Demonstrate that dtls.release() does not block forever and releases the
    // recoveryReadLock
    // allowing grantor migration to finish
    // create an observer that will block recovery messages from being processed
    MessageObserver observer = new MessageObserver();
    DistributionMessageObserver.setInstance(observer);
    try {
        System.out.println("starting thread to take over being lock grantor from vm0");
        // become the grantor - this will block waiting for a reply to the message blocked by the
        // observer
        Thread thread = new Thread(() -> {
            dlock.becomeLockGrantor();
        });
        thread.setName("TXLockServiceDUnitTest thread2");
        thread.setDaemon(true);
        thread.start();
        await("waiting for recovery to begin").atMost(10, TimeUnit.SECONDS).until(() -> {
            return observer.isPreventingProcessing();
        });
        // spawn a thread that will unblock message processing
        // so that TXLockServiceImpl's "recovering" variable will be set
        System.out.println("starting a thread to unblock recovery in 5 seconds");
        Thread unblockThread = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException("sleep interrupted");
            }
            System.out.println("releasing block of recovery message processing");
            observer.releasePreventionOfProcessing();
        });
        unblockThread.setName("TXLockServiceDUnitTest unblockThread");
        unblockThread.setDaemon(true);
        unblockThread.start();
        // release txLock - this will block until unblockThread tells the observer
        // that it can process its message. Then it should release the recovery read-lock
        // allowing the grantor to finish recovery
        System.out.println("releasing transaction locks, which should block for a bit");
        dtls.release(txLockId);
        await("waiting for recovery to finish").atMost(10, TimeUnit.SECONDS).until(() -> {
            return !((TXLockServiceImpl) dtls).isRecovering();
        });
    } finally {
        observer.releasePreventionOfProcessing();
        DistributionMessageObserver.setInstance(null);
    }
}
Also used : DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) ArrayList(java.util.ArrayList) TXRegionLockRequestImpl(org.apache.geode.internal.cache.TXRegionLockRequestImpl) DLockService(org.apache.geode.distributed.internal.locks.DLockService) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 24 with DLockService

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

the class TXLockServiceDUnitTest method identifyLockGrantor.

private 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 25 with DLockService

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

the class TXLockServiceDUnitTest method isLockGrantor_DTLS.

/**
   * Accessed via reflection. DO NOT REMOVE.
   */
protected static Boolean isLockGrantor_DTLS() {
    TXLockService dtls = TXLockService.getDTLS();
    if (true) {
        DLockService service = DLockService.getInternalServiceNamed(((TXLockServiceImpl) dtls).getInternalDistributedLockService().getName());
        assertNotNull(service);
        assertEquals("DTLS and DLock should both report same isLockGrantor result", true, dtls.isLockGrantor() == service.isLockGrantor());
    }
    Boolean result = Boolean.valueOf(dtls.isLockGrantor());
    logInfo("isLockGrantor_DTLS: " + result);
    return result;
}
Also used : DLockService(org.apache.geode.distributed.internal.locks.DLockService)

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