Search in sources :

Example 1 with TXRegionLockRequestImpl

use of org.apache.geode.internal.cache.TXRegionLockRequestImpl in project geode by apache.

the class ClientTXStateStub method obtainLocalLocks.

/**
   * Lock the keys in a local transaction manager
   * 
   * @throws CommitConflictException if the key is already locked by some other transaction
   */
private void obtainLocalLocks() {
    lockReq = new TXLockRequest();
    InternalCache cache = GemFireCacheImpl.getExisting("");
    for (TransactionalOperation txOp : this.recordedOperations) {
        if (ServerRegionOperation.lockKeyForTx(txOp.getOperation())) {
            TXRegionLockRequest rlr = lockReq.getRegionLockRequest(txOp.getRegionName());
            if (rlr == null) {
                rlr = new TXRegionLockRequestImpl(cache.getRegionByPath(txOp.getRegionName()));
                lockReq.addLocalRequest(rlr);
            }
            if (txOp.getOperation() == ServerRegionOperation.PUT_ALL || txOp.getOperation() == ServerRegionOperation.REMOVE_ALL) {
                rlr.addEntryKeys(txOp.getKeys());
            } else {
                rlr.addEntryKey(txOp.getKey());
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("TX: client localLockRequest: {}", lockReq);
    }
    try {
        lockReq.obtain();
    } catch (CommitConflictException e) {
        // cleanup tx artifacts on server
        rollback();
        throw e;
    }
    if (internalAfterLocalLocks != null) {
        internalAfterLocalLocks.run();
    }
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) TXRegionLockRequestImpl(org.apache.geode.internal.cache.TXRegionLockRequestImpl) TXLockRequest(org.apache.geode.internal.cache.TXLockRequest) InternalCache(org.apache.geode.internal.cache.InternalCache) TXRegionLockRequest(org.apache.geode.internal.cache.locks.TXRegionLockRequest)

Example 2 with TXRegionLockRequestImpl

use of org.apache.geode.internal.cache.TXRegionLockRequestImpl in project geode by apache.

the class TXLockServiceDUnitTest method testTXOriginatorRecoveryProcessor.

@Test
public void testTXOriginatorRecoveryProcessor() {
    LogWriterUtils.getLogWriter().info("[testTXOriginatorRecoveryProcessor]");
    final int originatorVM = 0;
    final int grantorVM = 1;
    final int particpantA = 2;
    final int particpantB = 3;
    final List regionLockReqs = new ArrayList();
    regionLockReqs.add(new TXRegionLockRequestImpl("/testTXOriginatorRecoveryProcessor", new HashSet(Arrays.asList(new String[] { "KEY-1", "KEY-2", "KEY-3", "KEY-4" }))));
    // build participants set...
    InternalDistributedMember dmId = null;
    final Set participants = new HashSet();
    for (int i = 1; i <= particpantB; i++) {
        final int finalvm = i;
        dmId = (InternalDistributedMember) Host.getHost(0).getVM(finalvm).invoke(() -> TXLockServiceDUnitTest.fetchDistributionManagerId());
        assertEquals("dmId should not be null for vm " + finalvm, false, dmId == null);
        participants.add(dmId);
    }
    // create grantor
    LogWriterUtils.getLogWriter().info("[testTXOriginatorRecoveryProcessor] grantorVM becomes grantor");
    Host.getHost(0).getVM(grantorVM).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.createDTLS();
        }
    });
    Host.getHost(0).getVM(grantorVM).invoke(() -> TXLockServiceDUnitTest.identifyLockGrantor_DTLS());
    Boolean isGrantor = (Boolean) Host.getHost(0).getVM(grantorVM).invoke(() -> TXLockServiceDUnitTest.isLockGrantor_DTLS());
    assertEquals("isLockGrantor should not be false for DTLS", Boolean.TRUE, isGrantor);
    // have a originatorVM get a txLock with three participants including grantor
    LogWriterUtils.getLogWriter().info("[testTXOriginatorRecoveryProcessor] originatorVM requests txLock");
    Host.getHost(0).getVM(originatorVM).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.createDTLS();
        }
    });
    Host.getHost(0).getVM(originatorVM).invoke(new SerializableRunnable("[testTXOriginatorRecoveryProcessor] originatorVM requests txLock") {

        public void run() {
            TXLockService dtls = TXLockService.getDTLS();
            testTXOriginatorRecoveryProcessor_TXLockId = dtls.txLock(regionLockReqs, participants);
            assertNotNull("testTXOriginatorRecoveryProcessor_TXLockId is null", testTXOriginatorRecoveryProcessor_TXLockId);
        }
    });
    // create dtls in each participant
    Host.getHost(0).getVM(particpantA).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.createDTLS();
        }
    });
    Host.getHost(0).getVM(particpantB).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.createDTLS();
        }
    });
    // disconnect originatorVM without releasing txLock
    /*
     * doesn't currently trigger the DLockLessorDepatureHandler... TODO
     * Host.getHost(0).getVM(originatorVM).invoke(new SerializableRunnable() { public void run() {
     * TXLockService.destroyServices(); } });
     */
    /*
     * Host.getHost(0).getVM(originatorVM).invoke(new SerializableRunnable() { public void run() {
     * InternalDistributedSystem sys = (InternalDistributedSystem)
     * InternalDistributedSystem.getAnyInstance(); if (sys != null) { sys.disconnect(); } } });
     */
    Host.getHost(0).getVM(originatorVM).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.destroyServices();
        }
    });
    Host.getHost(0).getVM(originatorVM).invoke(() -> disconnectFromDS());
    // grantor sends TXOriginatorRecoveryMessage...
    // TODO: verify processing of message? and have test sleep until finished
    sleep(200);
    // verify txLock is released...
    Host.getHost(0).getVM(particpantA).invoke(new SerializableRunnable("[testTXOriginatorRecoveryProcessor] verify txLock is released") {

        public void run() {
            TXLockService dtls = TXLockService.getDTLS();
            testTXOriginatorRecoveryProcessor_TXLockId = dtls.txLock(regionLockReqs, participants);
            assertNotNull("testTXOriginatorRecoveryProcessor_TXLockId is null", testTXOriginatorRecoveryProcessor_TXLockId);
        }
    });
    Host.getHost(0).getVM(particpantA).invoke(new SerializableRunnable("[testTXOriginatorRecoveryProcessor] particpantA releases txLock") {

        public void run() {
            TXLockService dtls = TXLockService.getDTLS();
            dtls.release(testTXOriginatorRecoveryProcessor_TXLockId);
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) TXRegionLockRequestImpl(org.apache.geode.internal.cache.TXRegionLockRequestImpl) ArrayList(java.util.ArrayList) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) 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 3 with TXRegionLockRequestImpl

use of org.apache.geode.internal.cache.TXRegionLockRequestImpl in project geode by apache.

the class TXLockServiceDUnitTest method testTXLock.

@Test
public void testTXLock() {
    LogWriterUtils.getLogWriter().info("[testTXLock]");
    final int grantorVM = 0;
    final int clientA = 1;
    final int clientB = 2;
    final Set participants = Collections.EMPTY_SET;
    final List regionLockReqs = new ArrayList();
    regionLockReqs.add(new TXRegionLockRequestImpl("/testTXLock1", new HashSet(Arrays.asList(new String[] { "KEY-1", "KEY-2", "KEY-3", "KEY-4" }))));
    regionLockReqs.add(new TXRegionLockRequestImpl("/testTXLock2", new HashSet(Arrays.asList(new String[] { "KEY-A", "KEY-B", "KEY-C", "KEY-D" }))));
    // create grantor
    LogWriterUtils.getLogWriter().info("[testTXLock] create grantor");
    Host.getHost(0).getVM(grantorVM).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.createDTLS();
        }
    });
    sleep(20);
    // create client and request txLock
    LogWriterUtils.getLogWriter().info("[testTXLock] create clientA and request txLock");
    Host.getHost(0).getVM(clientA).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.createDTLS();
        }
    });
    Host.getHost(0).getVM(clientA).invoke(new SerializableRunnable("[testTXLock] create clientA and request txLock") {

        public void run() {
            TXLockService dtls = TXLockService.getDTLS();
            testTXLock_TXLockId = dtls.txLock(regionLockReqs, participants);
            assertNotNull("testTXLock_TXLockId is null", testTXLock_TXLockId);
        }
    });
    // create nuther client and request overlapping txLock... verify fails
    LogWriterUtils.getLogWriter().info("[testTXLock] create clientB and fail txLock");
    Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable() {

        public void run() {
            TXLockService.createDTLS();
        }
    });
    Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable() {

        public void run() {
            try {
                TXLockService dtls = TXLockService.getDTLS();
                dtls.txLock(regionLockReqs, participants);
                fail("expected CommitConflictException");
            } catch (CommitConflictException expected) {
            }
        }
    });
    /*
     * try { Host.getHost(0).getVM(clientB).invoke(() -> TXLockServiceDUnitTest.txLock_DTLS(
     * regionLockReqs, participants )); fail("expected CommitConflictException"); } catch
     * (RMIException expected) { assertTrue(expected.getCause() instanceof CommitConflictException);
     * }
     */
    // release txLock
    LogWriterUtils.getLogWriter().info("[testTXLock] clientA releases txLock");
    Host.getHost(0).getVM(clientA).invoke(new SerializableRunnable("[testTXLock] clientA releases txLock") {

        public void run() {
            TXLockService dtls = TXLockService.getDTLS();
            dtls.release(testTXLock_TXLockId);
        }
    });
    sleep(20);
    // try nuther client again and verify success
    LogWriterUtils.getLogWriter().info("[testTXLock] clientB requests txLock");
    Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable("[testTXLock] clientB requests txLock") {

        public void run() {
            TXLockService dtls = TXLockService.getDTLS();
            testTXLock_TXLockId = dtls.txLock(regionLockReqs, participants);
            assertNotNull("testTXLock_TXLockId is null", testTXLock_TXLockId);
        }
    });
    // release txLock
    LogWriterUtils.getLogWriter().info("[testTXLock] clientB releases txLock");
    Host.getHost(0).getVM(clientB).invoke(new SerializableRunnable("[testTXLock] clientB releases txLock") {

        public void run() {
            TXLockService dtls = TXLockService.getDTLS();
            dtls.release(testTXLock_TXLockId);
        }
    });
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) HashSet(java.util.HashSet) Set(java.util.Set) TXRegionLockRequestImpl(org.apache.geode.internal.cache.TXRegionLockRequestImpl) ArrayList(java.util.ArrayList) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) 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 4 with TXRegionLockRequestImpl

use of org.apache.geode.internal.cache.TXRegionLockRequestImpl in project geode by apache.

the class TXLockBatch method toData.

public void toData(DataOutput out) throws IOException {
    InternalDataSerializer.invokeToData(this.txLockId, out);
    InternalDataSerializer.writeSet(this.participants, out);
    if (this.reqs == null) {
        out.writeInt(-1);
    } else {
        out.writeInt(this.reqs.size());
        for (Iterator iter = this.reqs.iterator(); iter.hasNext(); ) {
            TXRegionLockRequestImpl elem = (TXRegionLockRequestImpl) iter.next();
            InternalDataSerializer.invokeToData(elem, out);
        }
    }
}
Also used : TXRegionLockRequestImpl(org.apache.geode.internal.cache.TXRegionLockRequestImpl)

Example 5 with TXRegionLockRequestImpl

use of org.apache.geode.internal.cache.TXRegionLockRequestImpl 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)

Aggregations

TXRegionLockRequestImpl (org.apache.geode.internal.cache.TXRegionLockRequestImpl)6 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 List (java.util.List)4 DLockTest (org.apache.geode.test.junit.categories.DLockTest)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 Test (org.junit.Test)4 Set (java.util.Set)3 CommitConflictException (org.apache.geode.cache.CommitConflictException)2 DLockService (org.apache.geode.distributed.internal.locks.DLockService)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2 DistributionMessageObserver (org.apache.geode.distributed.internal.DistributionMessageObserver)1 DLockRecoverGrantorProcessor (org.apache.geode.distributed.internal.locks.DLockRecoverGrantorProcessor)1 DLockRecoverGrantorMessage (org.apache.geode.distributed.internal.locks.DLockRecoverGrantorProcessor.DLockRecoverGrantorMessage)1 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 TXLockRequest (org.apache.geode.internal.cache.TXLockRequest)1 TXRegionLockRequest (org.apache.geode.internal.cache.locks.TXRegionLockRequest)1