You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by GitBox <gi...@apache.org> on 2022/11/21 21:14:12 UTC

[GitHub] [helix] NealSun96 commented on a diff in pull request #2289: Improve AssignmentMetadataStore with double-checked locking

NealSun96 commented on code in PR #2289:
URL: https://github.com/apache/helix/pull/2289#discussion_r1028520149


##########
helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/AssignmentMetadataStore.java:
##########
@@ -61,36 +62,42 @@ protected AssignmentMetadataStore(BucketDataAccessor bucketDataAccessor, String
     _bestPossiblePath = String.format(BEST_POSSIBLE_TEMPLATE, clusterName, ASSIGNMENT_METADATA_KEY);
   }
 
-  public synchronized Map<String, ResourceAssignment> getBaseline() {
+  public Map<String, ResourceAssignment> getBaseline() {
     // Return the in-memory baseline. If null, read from ZK. This is to minimize reads from ZK
     if (_globalBaseline == null) {
-      try {
-        HelixProperty baseline =
-            _dataAccessor.compressedBucketRead(_baselinePath, HelixProperty.class);
-        _globalBaseline = splitAssignments(baseline);
-      } catch (ZkNoNodeException ex) {
-        // Metadata does not exist, so return an empty map
-        _globalBaseline = new HashMap<>();
+      // double-checked locking
+      synchronized (this) {
+        if (_globalBaseline == null) {
+          _globalBaseline = fetchAssignmentOrDefault(_baselinePath);
+        }
       }
     }
     return _globalBaseline;
   }
 
-  public synchronized Map<String, ResourceAssignment> getBestPossibleAssignment() {
+  public Map<String, ResourceAssignment> getBestPossibleAssignment() {
     // Return the in-memory baseline. If null, read from ZK. This is to minimize reads from ZK
     if (_bestPossibleAssignment == null) {
-      try {
-        HelixProperty baseline =
-            _dataAccessor.compressedBucketRead(_bestPossiblePath, HelixProperty.class);
-        _bestPossibleAssignment = splitAssignments(baseline);
-      } catch (ZkNoNodeException ex) {
-        // Metadata does not exist, so return an empty map
-        _bestPossibleAssignment = new HashMap<>();
+      // double-checked locking
+      synchronized (this) {
+        if (_bestPossibleAssignment == null) {
+          _bestPossibleAssignment = fetchAssignmentOrDefault(_bestPossiblePath);
+        }
       }
     }
     return _bestPossibleAssignment;
   }
 
+  private Map<String, ResourceAssignment> fetchAssignmentOrDefault(String path) {
+    try {

Review Comment:
   Could you make it explicit in a comment that this function must be called in a synchronized setting? This is my worry in terms of "clarity". 



-- 
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: reviews-unsubscribe@helix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org