pivotal-eshu commented on code in PR #7596:
URL: https://github.com/apache/geode/pull/7596#discussion_r855677359


##########
geode-core/src/main/java/org/apache/geode/internal/cache/BucketRegion.java:
##########
@@ -629,6 +642,76 @@ public void handleWANEvent(EntryEventImpl event) {
     }
   }
 
+  boolean needToWaitForColocatedBucketsBecomePrimary() {
+    if (hasChildRegion()) {
+      synchronized (this) {
+        return notPrimary;
+      }
+    }
+    return false;
+  }
+
+  boolean hasChildRegion() {
+    return 
ColocationHelper.getFirstColocatedNonShadowChildRegions(partitionedRegion).size()
 > 0;
+  }
+
+  void waitForAllChildColocatedBucketsBecomePrimary() {
+    synchronized (allChildBucketsBecomePrimaryLock) {
+      if (!alreadyInWaitForAllChildBucketsToBecomePrimary) {
+        alreadyInWaitForAllChildBucketsToBecomePrimary = true;
+        executeCheckIfAllChildBucketsBecomePrimary();
+      }
+      while (!allChildBucketsBecomePrimary && getBucketAdvisor().isPrimary()) {
+        // if no longer primary, no need to wait as the operation should fail
+        // with PrimaryBucketException later.
+        try {
+          allChildBucketsBecomePrimaryLock.wait(10);
+        } catch (InterruptedException e) {
+          Thread.currentThread().interrupt();
+          getCache().getCancelCriterion().checkCancelInProgress(e);
+        }
+      }
+    }
+  }
+
+  void executeCheckIfAllChildBucketsBecomePrimary() {
+    if (waitForAllChildBucketsToBecomePrimaryExecutor == null) {
+      waitForAllChildBucketsToBecomePrimaryExecutor =
+          
LoggingExecutors.newSingleThreadExecutor("CheckPrimaryForColocation", true);
+    }
+    waitForAllChildBucketsToBecomePrimaryExecutor
+        .execute(this::checkIfAllChildBucketsBecomePrimary);
+  }
+
+  void checkIfAllChildBucketsBecomePrimary() {
+    try {
+      while 
(!getBucketAdvisor().checkIfAllColocatedChildBucketsBecomePrimary()) {
+        if (!getBucketAdvisor().isPrimary()) {
+          // This parent bucket is no longer a primary, no need to wait.
+          return;
+        }
+      }
+      synchronized (allChildBucketsBecomePrimaryLock) {
+        allChildBucketsBecomePrimary = true;
+        notPrimary = false;

Review Comment:
   NotPrimary is intended to used for early return (if there is no colocated 
regions for this bucket), we only need to check once once notPrimary is set to 
false. I think to use same variable may cause confusion.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to