Search in sources :

Example 1 with RecoveryLock

use of org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock in project geode by apache.

the class PartitionRegionHelper method assignBucketsToPartitions.

/**
   * Decide which partitions will host which buckets. Gemfire normally assigns buckets to partitions
   * as needed when data is added to a partitioned region. This method provides way to assign all of
   * the buckets without putting any data in partition region. This method should not be called
   * until all of the partitions are running because it will divide the buckets between the running
   * partitions. If the buckets are already assigned this method will have no effect.
   * 
   * This method will block until all buckets are assigned.
   * 
   * @param region The region which should have it's buckets assigned.
   * @throws IllegalStateException if the provided region is something other than a
   *         {@linkplain DataPolicy#PARTITION partitioned Region}
   * @since GemFire 6.0
   */
public static void assignBucketsToPartitions(Region<?, ?> region) {
    PartitionedRegion pr = isPartitionedCheck(region);
    RecoveryLock lock = null;
    try {
        lock = pr.getRecoveryLock();
        lock.lock();
        for (int i = 0; i < getNumberOfBuckets(pr); i++) {
            // This method will return quickly if the bucket already exists
            pr.createBucket(i, 0, null);
        }
    } finally {
        if (lock != null) {
            lock.unlock();
        }
    }
}
Also used : PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RecoveryLock(org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock)

Example 2 with RecoveryLock

use of org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock in project geode by apache.

the class PartitionedRegionRebalanceOp method execute.

/**
   * Do the actual rebalance
   * 
   * @return the details of the rebalance.
   */
public Set<PartitionRebalanceInfo> execute() {
    long start = System.nanoTime();
    InternalResourceManager resourceManager = InternalResourceManager.getInternalResourceManager(leaderRegion.getCache());
    MembershipListener listener = new MembershipChangeListener();
    if (isRebalance) {
        InternalResourceManager.getResourceObserver().rebalancingStarted(targetRegion);
    } else {
        InternalResourceManager.getResourceObserver().recoveryStarted(targetRegion);
    }
    RecoveryLock lock = null;
    try {
        if (!checkAndSetColocatedRegions()) {
            return Collections.emptySet();
        }
        // have full redundancy.
        if (!isRebalanceNecessary()) {
            return Collections.emptySet();
        }
        if (!simulate) {
            lock = leaderRegion.getRecoveryLock();
            lock.lock();
        }
        // have fixed it already.
        if (!isRebalanceNecessary()) {
            return Collections.emptySet();
        }
        // register a listener to notify us if the new members leave or join.
        // When a membership change occurs, we want to restart the rebalancing
        // from the beginning.
        // TODO rebalance - we should really add a membership listener to ALL of
        // the colocated regions.
        leaderRegion.getRegionAdvisor().addMembershipListener(listener);
        PartitionedRegionLoadModel model = null;
        InternalCache cache = leaderRegion.getCache();
        Map<PartitionedRegion, InternalPRInfo> detailsMap = fetchDetails(cache);
        BucketOperatorWrapper serialOperator = getBucketOperator(detailsMap);
        ParallelBucketOperator parallelOperator = new ParallelBucketOperator(MAX_PARALLEL_OPERATIONS, cache.getDistributionManager().getWaitingThreadPool(), serialOperator);
        model = buildModel(parallelOperator, detailsMap, resourceManager);
        for (PartitionRebalanceDetailsImpl details : serialOperator.getDetailSet()) {
            details.setPartitionMemberDetailsBefore(model.getPartitionedMemberDetails(details.getRegionPath()));
        }
        director.initialize(model);
        for (; ; ) {
            if (cancelled.get()) {
                return Collections.emptySet();
            }
            if (membershipChange) {
                membershipChange = false;
                // refetch the partitioned region details after
                // a membership change.
                debug("Rebalancing {} detected membership changes. Refetching details", leaderRegion);
                if (this.stats != null) {
                    this.stats.incRebalanceMembershipChanges(1);
                }
                model.waitForOperations();
                detailsMap = fetchDetails(cache);
                model = buildModel(parallelOperator, detailsMap, resourceManager);
                director.membershipChanged(model);
            }
            leaderRegion.checkClosed();
            cache.getCancelCriterion().checkCancelInProgress(null);
            if (logger.isDebugEnabled()) {
                logger.debug("Rebalancing {} Model:{}\n", leaderRegion, model);
            }
            if (!director.nextStep()) {
                // Stop when the director says we can't rebalance any more.
                break;
            }
        }
        debug("Rebalancing {} complete. Model:{}\n", leaderRegion, model);
        long end = System.nanoTime();
        for (PartitionRebalanceDetailsImpl details : serialOperator.getDetailSet()) {
            if (!simulate) {
                details.setTime(end - start);
            }
            details.setPartitionMemberDetailsAfter(model.getPartitionedMemberDetails(details.getRegionPath()));
        }
        return Collections.<PartitionRebalanceInfo>unmodifiableSet(serialOperator.getDetailSet());
    } finally {
        if (lock != null) {
            try {
                lock.unlock();
            } catch (CancelException e) {
            // lock service has been destroyed
            } catch (Exception e) {
                logger.error(LocalizedMessage.create(LocalizedStrings.PartitionedRegionRebalanceOp_UNABLE_TO_RELEASE_RECOVERY_LOCK), e);
            }
        }
        try {
            if (isRebalance) {
                InternalResourceManager.getResourceObserver().rebalancingFinished(targetRegion);
            } else {
                InternalResourceManager.getResourceObserver().recoveryFinished(targetRegion);
            }
        } catch (Exception e) {
            logger.error(LocalizedMessage.create(LocalizedStrings.PartitionedRegionRebalanceOp_ERROR_IN_RESOURCE_OBSERVER), e);
        }
        try {
            leaderRegion.getRegionAdvisor().removeMembershipListener(listener);
        } catch (Exception e) {
            logger.error(LocalizedMessage.create(LocalizedStrings.PartitionedRegionRebalanceOp_ERROR_IN_RESOURCE_OBSERVER), e);
        }
    }
}
Also used : PartitionRebalanceInfo(org.apache.geode.cache.partition.PartitionRebalanceInfo) InternalCache(org.apache.geode.internal.cache.InternalCache) RecoveryLock(org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock) PartitionRebalanceDetailsImpl(org.apache.geode.internal.cache.control.PartitionRebalanceDetailsImpl) CancelException(org.apache.geode.CancelException) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) PartitionedRegionLoadModel(org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ParallelBucketOperator(org.apache.geode.internal.cache.partitioned.rebalance.ParallelBucketOperator) BucketOperatorWrapper(org.apache.geode.internal.cache.partitioned.rebalance.BucketOperatorWrapper) CancelException(org.apache.geode.CancelException) MembershipListener(org.apache.geode.distributed.internal.MembershipListener)

Example 3 with RecoveryLock

use of org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock in project geode by apache.

the class CreateMissingBucketsTask method run2.

@Override
public void run2() {
    PartitionedRegion leaderRegion = ColocationHelper.getLeaderRegion(redundancyProvider.prRegion);
    RecoveryLock lock = leaderRegion.getRecoveryLock();
    lock.lock();
    try {
        createMissingBuckets(redundancyProvider.prRegion);
    } finally {
        lock.unlock();
    }
}
Also used : PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RecoveryLock(org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock)

Aggregations

PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)3 RecoveryLock (org.apache.geode.internal.cache.PartitionedRegion.RecoveryLock)3 CancelException (org.apache.geode.CancelException)1 PartitionRebalanceInfo (org.apache.geode.cache.partition.PartitionRebalanceInfo)1 MembershipListener (org.apache.geode.distributed.internal.MembershipListener)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 InternalResourceManager (org.apache.geode.internal.cache.control.InternalResourceManager)1 PartitionRebalanceDetailsImpl (org.apache.geode.internal.cache.control.PartitionRebalanceDetailsImpl)1 BucketOperatorWrapper (org.apache.geode.internal.cache.partitioned.rebalance.BucketOperatorWrapper)1 ParallelBucketOperator (org.apache.geode.internal.cache.partitioned.rebalance.ParallelBucketOperator)1 PartitionedRegionLoadModel (org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel)1