[ https://issues.apache.org/jira/browse/GEODE-7678?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17096000#comment-17096000 ]
ASF GitHub Bot commented on GEODE-7678: --------------------------------------- agingade commented on a change in pull request #4987: URL: https://github.com/apache/geode/pull/4987#discussion_r417709662 ########## File path: geode-core/src/main/java/org/apache/geode/internal/cache/ClearPartitionedRegion.java ########## @@ -0,0 +1,379 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.internal.cache; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; + +import org.apache.logging.log4j.Logger; + +import org.apache.geode.CancelException; +import org.apache.geode.cache.Operation; +import org.apache.geode.cache.partition.PartitionRegionHelper; +import org.apache.geode.distributed.internal.DistributionManager; +import org.apache.geode.distributed.internal.MembershipListener; +import org.apache.geode.distributed.internal.ReplyException; +import org.apache.geode.distributed.internal.membership.InternalDistributedMember; +import org.apache.geode.internal.cache.versions.RegionVersionVector; +import org.apache.geode.logging.internal.log4j.api.LogService; + +public class ClearPartitionedRegion { + + private static final Logger logger = LogService.getLogger(); + + private final PartitionedRegion partitionedRegion; + + private LockForListenerAndClientNotification lockForListenerAndClientNotification = + new LockForListenerAndClientNotification(); + + private volatile boolean membershipchange = false; + + public ClearPartitionedRegion(PartitionedRegion partitionedRegion) { + this.partitionedRegion = partitionedRegion; + partitionedRegion.getDistributionManager() + .addMembershipListener(new ClearPartitionedRegionListener()); + } + + public boolean isLockedForListenerAndClientNotification() { + return lockForListenerAndClientNotification.isLocked(); + } + + void acquireDistributedClearLock(String clearLock) { + try { + partitionedRegion.getPartitionedRegionLockService().lock(clearLock, -1, -1); + } catch (IllegalStateException e) { + partitionedRegion.lockCheckReadiness(); + throw e; + } + } + + void releaseDistributedClearLock(String clearLock) { + try { + partitionedRegion.getPartitionedRegionLockService().unlock(clearLock); + } catch (IllegalStateException e) { + partitionedRegion.lockCheckReadiness(); + } catch (Exception ex) { + logger.warn("Caught exception while unlocking clear distributed lock", ex.getMessage()); + } + } + + void obtainLockForClear(RegionEventImpl event) { + sendClearRegionMessage(event, + ClearPartitionedRegionMessage.OperationType.OP_LOCK_FOR_PR_CLEAR); + obtainClearLockLocal(partitionedRegion.getDistributionManager().getId()); + } + + void releaseLockForClear(RegionEventImpl event) { + sendClearRegionMessage(event, + ClearPartitionedRegionMessage.OperationType.OP_UNLOCK_FOR_PR_CLEAR); + releaseClearLockLocal(); + } + + void clearRegion(RegionEventImpl regionEvent, boolean cacheWrite, + RegionVersionVector vector) { + clearRegionLocal(regionEvent, cacheWrite, null); + sendClearRegionMessage(regionEvent, + ClearPartitionedRegionMessage.OperationType.OP_PR_CLEAR); + } + + private void waitForPrimary() { + boolean retry; + int retryTime = 2 * 60 * 1000 /* partitionedRegion.getRetryTimeout() */; + PartitionedRegion.RetryTimeKeeper retryTimer = new PartitionedRegion.RetryTimeKeeper(retryTime); + do { + retry = false; + for (BucketRegion bucketRegion : partitionedRegion.getDataStore() + .getAllLocalBucketRegions()) { + if (!bucketRegion.getBucketAdvisor().hasPrimary()) { + if (retryTimer.overMaximum()) { + PRHARedundancyProvider.timedOut(partitionedRegion, null, null, + "do clear. Missing primary bucket", + retryTime); + } + retryTimer.waitForBucketsRecovery(); + retry = true; + } + } + } while (retry); + } + + public void clearRegionLocal(RegionEventImpl regionEvent, boolean cacheWrite, + RegionVersionVector vector) { + // Synchronized to handle the requester departure. + synchronized (lockForListenerAndClientNotification) { + if (partitionedRegion.getDataStore() != null) { + partitionedRegion.getDataStore().lockBucketCreationForRegionClear(); + try { + boolean retry; + do { + retry = false; + for (BucketRegion localPrimaryBucketRegion : partitionedRegion.getDataStore() + .getAllLocalPrimaryBucketRegions()) { + if (localPrimaryBucketRegion.size() > 0) { + localPrimaryBucketRegion.clear(); + } + } + if (membershipchange) { + membershipchange = false; + retry = true; + waitForPrimary(); Review comment: The retry should happen with the membership change. We know or could estimate approximate time it takes to elect a secondary to primary when there is a member departure; the reason it is in the waitForPrimary. If we encounter timeout with clear; we need to adjust the timeout; similar approach is used other time consuming operation. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > Partitioned Region clear operations must invoke cache level listeners > --------------------------------------------------------------------- > > Key: GEODE-7678 > URL: https://issues.apache.org/jira/browse/GEODE-7678 > Project: Geode > Issue Type: Sub-task > Components: regions > Reporter: Nabarun Nag > Priority: Major > Labels: GeodeCommons > > Clear operations are successful and CacheListener.afterRegionClear(), > CacheWriter.beforeRegionClear() are invoked. > > Acceptance : > * DUnit tests validating the above behavior. > * Test coverage to when a member departs in this scenario > * Test coverage to when a member restarts in this scenario > * Unit tests with complete code coverage for the newly written code. > -- This message was sent by Atlassian Jira (v8.3.4#803005)