Search in sources :

Example 1 with DistributedMemberLock

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

the class CreateRegionFunction method getDistributedLock.

private DistributedMemberLock getDistributedLock() {
    String dlsName = this.regionConfigurationsRegion.getName();
    DistributedLockService lockService = initializeDistributedLockService(dlsName);
    String lockToken = dlsName + "_token";
    return new DistributedMemberLock(lockService, lockToken);
}
Also used : DistributedLockService(org.apache.geode.distributed.DistributedLockService) DistributedMemberLock(org.apache.geode.distributed.internal.locks.DistributedMemberLock)

Example 2 with DistributedMemberLock

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

the class CreateRegionFunction method createRegion.

private RegionStatus createRegion(RegionConfiguration configuration) {
    // Get a distributed lock
    DistributedMemberLock dml = getDistributedLock();
    if (this.cache.getLogger().fineEnabled()) {
        this.cache.getLogger().fine(this + ": Attempting to lock " + dml);
    }
    long start = 0, end = 0;
    RegionStatus status = null;
    try {
        if (this.cache.getLogger().fineEnabled()) {
            start = System.currentTimeMillis();
        }
        // Obtain a lock on the distributed lock
        dml.lockInterruptibly();
        if (this.cache.getLogger().fineEnabled()) {
            end = System.currentTimeMillis();
            this.cache.getLogger().fine(this + ": Obtained lock on " + dml + " in " + (end - start) + " ms");
        }
        // Attempt to get the region again after the lock has been obtained
        String regionName = configuration.getRegionName();
        Region region = this.cache.getRegion(regionName);
        // while holding the lock. This will create the region in all VMs.
        if (region == null) {
            this.regionConfigurationsRegion.put(regionName, configuration);
            // Retrieve the region after creating it
            region = this.cache.getRegion(regionName);
            // (e.g. the region attributes id were invalid)
            if (region == null) {
                status = RegionStatus.INVALID;
            } else {
                // Create the PR buckets if necessary)
                if (region instanceof PartitionedRegion) {
                    PartitionedRegion pr = (PartitionedRegion) region;
                    createBuckets(pr);
                }
                status = RegionStatus.VALID;
            }
        } else {
            status = RegionStatus.VALID;
            try {
                RegionHelper.validateRegion(this.cache, configuration, region);
            } catch (Exception e) {
                if (!e.getMessage().equals(LocalizedStrings.RegionAttributesCreation_CACHELISTENERS_ARE_NOT_THE_SAME.toLocalizedString())) {
                    this.cache.getLogger().warning(e);
                }
                status = RegionStatus.INVALID;
            }
        }
    } catch (Exception e) {
        StringBuilder builder = new StringBuilder();
        builder.append(this).append(": Caught Exception attempting to create region named ").append(configuration.getRegionName()).append(":");
        this.cache.getLogger().warning(builder.toString(), e);
        status = RegionStatus.INVALID;
    } finally {
        // Unlock the distributed lock
        try {
            dml.unlock();
        } catch (Exception ignore) {
        }
    }
    return status;
}
Also used : PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) DistributedMemberLock(org.apache.geode.distributed.internal.locks.DistributedMemberLock) IOException(java.io.IOException)

Example 3 with DistributedMemberLock

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

Aggregations

DistributedMemberLock (org.apache.geode.distributed.internal.locks.DistributedMemberLock)3 DistributedLockService (org.apache.geode.distributed.DistributedLockService)2 IOException (java.io.IOException)1 Region (org.apache.geode.cache.Region)1 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)1 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)1