KUDU-1682. Lock contention on table locations cache in Java client

Change-Id: I0f6ba8f4fced6f043f7132fd11078044b004ea21
Reviewed-on: http://gerrit.cloudera.org:8080/4706
Reviewed-by: Adar Dembo <a...@cloudera.com>
Reviewed-by: Dan Burkert <d...@cloudera.com>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/fb9526f9
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/fb9526f9
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/fb9526f9

Branch: refs/heads/master
Commit: fb9526f9ddd53066b617a6d7b6d1478eaa50fe96
Parents: 5195ce5
Author: Jean-Daniel Cryans <jdcry...@apache.org>
Authored: Wed Oct 12 16:58:26 2016 -0700
Committer: Jean-Daniel Cryans <jdcry...@apache.org>
Committed: Thu Oct 13 15:52:36 2016 +0000

----------------------------------------------------------------------
 .../apache/kudu/client/TableLocationsCache.java | 29 ++++++++++++++++----
 1 file changed, 23 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/fb9526f9/java/kudu-client/src/main/java/org/apache/kudu/client/TableLocationsCache.java
----------------------------------------------------------------------
diff --git 
a/java/kudu-client/src/main/java/org/apache/kudu/client/TableLocationsCache.java
 
b/java/kudu-client/src/main/java/org/apache/kudu/client/TableLocationsCache.java
index 0c27bfa..ec7fb48 100644
--- 
a/java/kudu-client/src/main/java/org/apache/kudu/client/TableLocationsCache.java
+++ 
b/java/kudu-client/src/main/java/org/apache/kudu/client/TableLocationsCache.java
@@ -32,6 +32,7 @@ import java.util.Map;
 import java.util.NavigableMap;
 import java.util.TreeMap;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 import javax.annotation.concurrent.GuardedBy;
 import javax.annotation.concurrent.ThreadSafe;
 
@@ -45,22 +46,31 @@ class TableLocationsCache {
   private static final Logger LOG = 
LoggerFactory.getLogger(TableLocationsCache.class);
   private static final Comparator<byte[]> COMPARATOR = 
UnsignedBytes.lexicographicalComparator();
 
-  private final Object monitor = new Object();
+  private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
 
-  @GuardedBy("monitor")
+  @GuardedBy("rwl")
   private final NavigableMap<byte[], Entry> entries = new 
TreeMap<>(COMPARATOR);
 
   public Entry get(byte[] partitionKey) {
+
     if (partitionKey == null) {
       // Master lookup.
-      synchronized (monitor) {
+      rwl.readLock().lock();
+      try {
         Preconditions.checkState(entries.size() <= 1);
         return entries.get(AsyncKuduClient.EMPTY_ARRAY);
+      } finally {
+        rwl.readLock().unlock();
       }
+
     }
+
     Map.Entry<byte[], Entry> entry;
-    synchronized (monitor) {
+    rwl.readLock().lock();
+    try {
       entry = entries.floorEntry(partitionKey);
+    } finally {
+      rwl.readLock().unlock();
     }
 
     if (entry == null ||
@@ -89,9 +99,13 @@ class TableLocationsCache {
       // Master lookup.
       Preconditions.checkArgument(tablets.size() == 1);
       Entry entry = Entry.tablet(tablets.get(0), TimeUnit.DAYS.toMillis(1));
-      synchronized (monitor) {
+
+      rwl.writeLock().lock();
+      try {
         entries.clear();
         entries.put(AsyncKuduClient.EMPTY_ARRAY, entry);
+      } finally {
+        rwl.writeLock().unlock();
       }
       return;
     }
@@ -164,7 +178,8 @@ class TableLocationsCache {
 
     LOG.debug("Discovered table locations:\t{}", newEntries);
 
-    synchronized (monitor) {
+    rwl.writeLock().lock();
+    try {
       // Remove all existing overlapping entries, and add the new entries.
       Map.Entry<byte[], Entry> floorEntry = 
entries.floorEntry(discoveredlowerBound);
       if (floorEntry != null &&
@@ -186,6 +201,8 @@ class TableLocationsCache {
       for (Entry entry : newEntries) {
         entries.put(entry.getLowerBoundPartitionKey(), entry);
       }
+    } finally {
+      rwl.writeLock().unlock();
     }
   }
 

Reply via email to