Jackie-Jiang commented on code in PR #13632:
URL: https://github.com/apache/pinot/pull/13632#discussion_r1681843134


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/SegmentLocks.java:
##########
@@ -18,30 +18,25 @@
  */
 package org.apache.pinot.segment.local.utils;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
+import org.apache.commons.lang3.tuple.Pair;
 
 
 public class SegmentLocks {
-  private static final int DEFAULT_NUM_LOCKS = 10000;
-
-  private final Lock[] _locks;
-  private final int _numLocks;
-
-  public SegmentLocks(int numLocks) {
-    _numLocks = numLocks;
-    _locks = new Lock[numLocks];
-    for (int i = 0; i < numLocks; i++) {
-      _locks[i] = new ReentrantLock();
-    }
-  }
-
-  public SegmentLocks() {
-    this(DEFAULT_NUM_LOCKS);
-  }
+  private final LoadingCache<Pair<String, String>, Lock> _locks =
+      CacheBuilder.newBuilder().weakValues().build(new CacheLoader<>() {
+        @Override
+        public Lock load(Pair<String, String> key) {
+          return new ReentrantLock();
+        }
+      });
 
   public Lock getLock(String tableNameWithType, String segmentName) {
-    return _locks[Math.abs((31 * tableNameWithType.hashCode() + 
segmentName.hashCode()) % _numLocks)];
+    return _locks.getUnchecked(Pair.of(tableNameWithType, segmentName));

Review Comment:
   It is using weak values, thus value without reference will be cleaned up



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to