Search in sources :

Example 6 with DistributedLockService

use of org.apache.geode.distributed.DistributedLockService in project geode by apache.

the class TXLockServiceDUnitTest method remoteCreateService.

/**
   * Creates a new DistributedLockService in a remote VM.
   * 
   * @param name The name of the newly-created DistributedLockService. It is recommended that the
   *        name of the Region be the {@link #getUniqueName()} of the test, or at least derive from
   *        it.
   */
protected static void remoteCreateService(String name) {
    DistributedLockService newService = DistributedLockService.create(name, system);
    logInfo("Created " + newService);
}
Also used : DistributedLockService(org.apache.geode.distributed.DistributedLockService)

Example 7 with DistributedLockService

use of org.apache.geode.distributed.DistributedLockService in project geode by apache.

the class DLockManagementDUnitTest method verifyLockServiceMXBeanInMember.

private void verifyLockServiceMXBeanInMember(final VM memberVM) {
    memberVM.invoke("verifyLockServiceMXBeanInManager", () -> {
        DistributedLockService lockService = DistributedLockService.getServiceNamed(LOCK_SERVICE_NAME);
        lockService.lock("lockObject_" + identifyPid(), MAX_WAIT_MILLIS, -1);
        ManagementService service = this.managementTestRule.getManagementService();
        LockServiceMXBean lockServiceMXBean = service.getLocalLockServiceMBean(LOCK_SERVICE_NAME);
        assertThat(lockServiceMXBean).isNotNull();
        String[] listHeldLock = lockServiceMXBean.listHeldLocks();
        assertThat(listHeldLock).hasSize(1);
        Map<String, String> lockThreadMap = lockServiceMXBean.listThreadsHoldingLock();
        assertThat(lockThreadMap).hasSize(1);
    });
}
Also used : SystemManagementService(org.apache.geode.management.internal.SystemManagementService) DistributedLockService(org.apache.geode.distributed.DistributedLockService)

Example 8 with DistributedLockService

use of org.apache.geode.distributed.DistributedLockService in project geode by apache.

the class AutoBalancerIntegrationJUnitTest method testLockAlreadyTakenElsewhere.

@Test
public void testLockAlreadyTakenElsewhere() throws InterruptedException {
    DistributedLockService dls = new GeodeCacheFacade().getDLS();
    assertTrue(dls.lock(AutoBalancer.AUTO_BALANCER_LOCK, 0, -1));
    final AtomicBoolean success = new AtomicBoolean(true);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            CacheOperationFacade cacheFacade = new GeodeCacheFacade();
            success.set(cacheFacade.acquireAutoBalanceLock());
        }
    });
    thread.start();
    thread.join();
    assertFalse(success.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GeodeCacheFacade(org.apache.geode.cache.util.AutoBalancer.GeodeCacheFacade) CacheOperationFacade(org.apache.geode.cache.util.AutoBalancer.CacheOperationFacade) DistributedLockService(org.apache.geode.distributed.DistributedLockService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with DistributedLockService

use of org.apache.geode.distributed.DistributedLockService in project geode by apache.

the class BucketAdvisor method getPrimaryLock.

/**
   * Lazily gets the lock for acquiring primary lock. Caller must handle null. If DLS, Cache, or
   * DistributedSystem are shutting down then null will be returned. If DLS does not yet exist and
   * createDLS is false then null will be returned.
   * 
   * @param createDLS true will create DLS if it does not exist
   * @return distributed lock indicating primary member or null
   */
DistributedMemberLock getPrimaryLock(boolean createDLS) {
    synchronized (this) {
        if (this.primaryLock == null) {
            DistributedLockService dls = DistributedLockService.getServiceNamed(PartitionedRegionHelper.PARTITION_LOCK_SERVICE_NAME);
            if (dls == null) {
                if (!createDLS || getProxyBucketRegion().getCache().isClosed()) {
                    // cache closure has destroyed the DLS
                    return null;
                }
                try {
                    // TODO: call GemFireCache#getPartitionedRegionLockService
                    dls = DLockService.create(PartitionedRegionHelper.PARTITION_LOCK_SERVICE_NAME, getAdvisee().getSystem(), true, /* distributed */
                    true, /* destroyOnDisconnect */
                    true);
                } catch (IllegalArgumentException e) {
                    // indicates that the DLS is already created
                    dls = DistributedLockService.getServiceNamed(PartitionedRegionHelper.PARTITION_LOCK_SERVICE_NAME);
                    if (dls == null) {
                        // ok, caller will loop if necessary
                        return null;
                    }
                }// perhaps: DistributedSystemUnavailableException
                 catch (IllegalStateException e) {
                    // create still throws IllegalStateException if isDisconnecting is true
                    return null;
                } catch (DistributedSystemDisconnectedException e) {
                    // this would certainly prevent us from creating a DLS... messy
                    return null;
                }
            }
            this.primaryLock = new DistributedMemberLock(dls, getAdvisee().getName(), DistributedMemberLock.NON_EXPIRING_LEASE, DistributedMemberLock.LockReentryPolicy.PREVENT_SILENTLY);
        }
        return this.primaryLock;
    }
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) DistributedLockService(org.apache.geode.distributed.DistributedLockService) DistributedMemberLock(org.apache.geode.distributed.internal.locks.DistributedMemberLock)

Example 10 with DistributedLockService

use of org.apache.geode.distributed.DistributedLockService in project geode by apache.

the class IndexRepositoryFactory method computeIndexRepository.

public IndexRepository computeIndexRepository(final Integer bucketId, LuceneSerializer serializer, LuceneIndexImpl index, PartitionedRegion userRegion, final IndexRepository oldRepository) throws IOException {
    LuceneIndexForPartitionedRegion indexForPR = (LuceneIndexForPartitionedRegion) index;
    final PartitionedRegion fileRegion = indexForPR.getFileAndChunkRegion();
    BucketRegion fileAndChunkBucket = getMatchingBucket(fileRegion, bucketId);
    BucketRegion dataBucket = getMatchingBucket(userRegion, bucketId);
    boolean success = false;
    if (fileAndChunkBucket == null) {
        if (oldRepository != null) {
            oldRepository.cleanup();
        }
        return null;
    }
    if (!fileAndChunkBucket.getBucketAdvisor().isPrimary()) {
        if (oldRepository != null) {
            oldRepository.cleanup();
        }
        return null;
    }
    if (oldRepository != null && !oldRepository.isClosed()) {
        return oldRepository;
    }
    if (oldRepository != null) {
        oldRepository.cleanup();
    }
    DistributedLockService lockService = getLockService();
    String lockName = getLockName(fileAndChunkBucket);
    while (!lockService.lock(lockName, 100, -1)) {
        if (!fileAndChunkBucket.getBucketAdvisor().isPrimary()) {
            return null;
        }
    }
    final IndexRepository repo;
    try {
        RegionDirectory dir = new RegionDirectory(getBucketTargetingMap(fileAndChunkBucket, bucketId), indexForPR.getFileSystemStats());
        IndexWriterConfig config = new IndexWriterConfig(indexForPR.getAnalyzer());
        IndexWriter writer = new IndexWriter(dir, config);
        repo = new IndexRepositoryImpl(fileAndChunkBucket, writer, serializer, indexForPR.getIndexStats(), dataBucket, lockService, lockName);
        success = true;
        return repo;
    } catch (IOException e) {
        logger.info("Exception thrown while constructing Lucene Index for bucket:" + bucketId + " for file region:" + fileAndChunkBucket.getFullPath());
        throw e;
    } finally {
        if (!success) {
            lockService.unlock(lockName);
        }
    }
}
Also used : IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) BucketRegion(org.apache.geode.internal.cache.BucketRegion) IndexWriter(org.apache.lucene.index.IndexWriter) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) DistributedLockService(org.apache.geode.distributed.DistributedLockService) IOException(java.io.IOException) RegionDirectory(org.apache.geode.cache.lucene.internal.directory.RegionDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) IndexRepositoryImpl(org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImpl)

Aggregations

DistributedLockService (org.apache.geode.distributed.DistributedLockService)12 GeodeCacheFacade (org.apache.geode.cache.util.AutoBalancer.GeodeCacheFacade)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 Test (org.junit.Test)3 DistributedMemberLock (org.apache.geode.distributed.internal.locks.DistributedMemberLock)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CacheException (org.apache.geode.cache.CacheException)1 RegionDirectory (org.apache.geode.cache.lucene.internal.directory.RegionDirectory)1 IndexRepository (org.apache.geode.cache.lucene.internal.repository.IndexRepository)1 IndexRepositoryImpl (org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImpl)1 CacheOperationFacade (org.apache.geode.cache.util.AutoBalancer.CacheOperationFacade)1 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)1 LockServiceDestroyedException (org.apache.geode.distributed.LockServiceDestroyedException)1 BucketRegion (org.apache.geode.internal.cache.BucketRegion)1 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)1 SystemManagementService (org.apache.geode.management.internal.SystemManagementService)1 IgnoredException (org.apache.geode.test.dunit.IgnoredException)1 IndexWriter (org.apache.lucene.index.IndexWriter)1