accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject [accumulo] branch 1.9 updated: fixes #768 fix race condition in generation of table map (#973)
Date Sat, 23 Feb 2019 23:17:05 GMT
This is an automated email from the ASF dual-hosted git repository.

kturner pushed a commit to branch 1.9
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/1.9 by this push:
     new 89cfb66  fixes #768 fix race condition in generation of table map (#973)
89cfb66 is described below

commit 89cfb66db6db731391b35d0ab373627fe7da4d0c
Author: Keith Turner <kturner@apache.org>
AuthorDate: Sat Feb 23 18:17:00 2019 -0500

    fixes #768 fix race condition in generation of table map (#973)
---
 .../apache/accumulo/core/client/impl/TableMap.java | 12 ++++++
 .../apache/accumulo/core/client/impl/Tables.java   | 48 +++++++++-------------
 2 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/client/impl/TableMap.java b/core/src/main/java/org/apache/accumulo/core/client/impl/TableMap.java
index 8bd2db3..aabfac8 100644
--- a/core/src/main/java/org/apache/accumulo/core/client/impl/TableMap.java
+++ b/core/src/main/java/org/apache/accumulo/core/client/impl/TableMap.java
@@ -42,7 +42,15 @@ public class TableMap {
   private final Map<String,String> tableNameToIdMap;
   private final Map<String,String> tableIdToNameMap;
 
+  private final ZooCache zooCache;
+  private final long updateCount;
+
   public TableMap(Instance instance, ZooCache zooCache) {
+
+    this.zooCache = zooCache;
+    // important to read this first
+    this.updateCount = zooCache.getUpdateCount();
+
     List<String> tableIds = zooCache.getChildren(ZooUtil.getRoot(instance) + Constants.ZTABLES);
     Map<String,String> namespaceIdToNameMap = new HashMap<>();
     ImmutableMap.Builder<String,String> tableNameToIdBuilder = new ImmutableMap.Builder<>();
@@ -98,4 +106,8 @@ public class TableMap {
   public Map<String,String> getIdtoNameMap() {
     return tableIdToNameMap;
   }
+
+  public boolean isCurrent(ZooCache zc) {
+    return this.zooCache == zc && this.updateCount == zc.getUpdateCount();
+  }
 }
diff --git a/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java b/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java
index e9117bc..b3a6d67 100644
--- a/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java
+++ b/core/src/main/java/org/apache/accumulo/core/client/impl/Tables.java
@@ -36,8 +36,6 @@ import org.apache.accumulo.core.util.Pair;
 import org.apache.accumulo.core.zookeeper.ZooUtil;
 import org.apache.accumulo.fate.zookeeper.ZooCache;
 import org.apache.accumulo.fate.zookeeper.ZooCacheFactory;
-import org.apache.zookeeper.WatchedEvent;
-import org.apache.zookeeper.Watcher;
 
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
@@ -52,8 +50,6 @@ public class Tables {
   // frequently
   private static Cache<String,TableMap> instanceToMapCache = CacheBuilder.newBuilder()
       .expireAfterAccess(10, TimeUnit.MINUTES).build();
-  private static Cache<String,ZooCache> instanceToZooCache = CacheBuilder.newBuilder()
-      .expireAfterAccess(10, TimeUnit.MINUTES).build();
 
   /**
    * Return the cached ZooCache for provided instance. ZooCache is initially created with
a watcher
@@ -65,25 +61,8 @@ public class Tables {
       sm.checkPermission(TABLES_PERMISSION);
     }
 
-    final String uuid = instance.getInstanceID();
-
-    try {
-      return instanceToZooCache.get(uuid, new Callable<ZooCache>() {
-        @Override
-        public ZooCache call() {
-          final String zks = instance.getZooKeepers();
-          final int timeOut = instance.getZooKeepersSessionTimeOut();
-          return new ZooCacheFactory().getZooCache(zks, timeOut, new Watcher() {
-            @Override
-            public void process(WatchedEvent watchedEvent) {
-              instanceToMapCache.invalidate(uuid);
-            }
-          });
-        }
-      });
-    } catch (ExecutionException e) {
-      throw new RuntimeException(e);
-    }
+    return new ZooCacheFactory().getZooCache(instance.getZooKeepers(),
+        instance.getZooKeepersSessionTimeOut());
   }
 
   public static String getTableId(Instance instance, String tableName)
@@ -132,22 +111,35 @@ public class Tables {
 
   /**
    * Get the TableMap from the cache. A new one will be populated when needed. Cache is cleared
-   * manually by calling {@link #clearCache(Instance)} or automatically cleared by ZooCache
watcher
-   * created in {@link #getZooCache(Instance)}. See ACCUMULO-4778.
+   * manually by calling {@link #clearCache(Instance)}
    */
   private static TableMap getTableMap(final Instance instance) {
+
     TableMap map;
+
+    final ZooCache zc = getZooCache(instance);
+
+    map = getTableMap(instance, zc);
+
+    if (!map.isCurrent(zc)) {
+      instanceToMapCache.invalidate(instance.getInstanceID());
+      map = getTableMap(instance, zc);
+    }
+
+    return map;
+  }
+
+  private static TableMap getTableMap(final Instance instance, final ZooCache zc) {
     try {
-      map = instanceToMapCache.get(instance.getInstanceID(), new Callable<TableMap>()
{
+      return instanceToMapCache.get(instance.getInstanceID(), new Callable<TableMap>()
{
         @Override
         public TableMap call() {
-          return new TableMap(instance, getZooCache(instance));
+          return new TableMap(instance, zc);
         }
       });
     } catch (ExecutionException e) {
       throw new RuntimeException(e);
     }
-    return map;
   }
 
   public static boolean exists(Instance instance, String tableId) {


Mime
View raw message