incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [1/2] git commit: Minor updates to ZK lock.
Date Wed, 27 Nov 2013 14:55:00 GMT
Updated Branches:
  refs/heads/master 772facd97 -> 54be093f3


Minor updates to ZK lock.


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

Branch: refs/heads/master
Commit: e9a5c3d5856d9ac2c91705ea12ed96d325415c51
Parents: 772facd
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Wed Nov 27 08:50:51 2013 -0500
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Wed Nov 27 09:54:40 2013 -0500

----------------------------------------------------------------------
 .../blur/manager/indexserver/SafeMode.java      |  6 +--
 .../blur/zookeeper/ZooKeeperLockManager.java    | 47 +++++++++++---------
 2 files changed, 28 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/e9a5c3d5/blur-core/src/main/java/org/apache/blur/manager/indexserver/SafeMode.java
----------------------------------------------------------------------
diff --git a/blur-core/src/main/java/org/apache/blur/manager/indexserver/SafeMode.java b/blur-core/src/main/java/org/apache/blur/manager/indexserver/SafeMode.java
index 6260adc..3648be0 100644
--- a/blur-core/src/main/java/org/apache/blur/manager/indexserver/SafeMode.java
+++ b/blur-core/src/main/java/org/apache/blur/manager/indexserver/SafeMode.java
@@ -89,8 +89,8 @@ public class SafeMode extends ZooKeeperLockManager {
     long startingWaitTime = System.currentTimeMillis();
     List<String> prev = null;
     while (true) {
-      synchronized (lock) {
-        List<String> children = new ArrayList<String>(zooKeeper.getChildren(nodePath,
watcher));
+      synchronized (_lock) {
+        List<String> children = new ArrayList<String>(zooKeeper.getChildren(nodePath,
_watcher));
         Collections.sort(children);
         if (children.equals(prev)) {
           LOG.info("Clustered has settled.");
@@ -99,7 +99,7 @@ public class SafeMode extends ZooKeeperLockManager {
           prev = children;
           LOG.info("Waiting for cluster to settle, current size [" + children.size() + "]
total time waited so far ["
               + (System.currentTimeMillis() - startingWaitTime) + " ms] waiting another ["
+ waitTime + " ms].");
-          lock.wait(waitTime);
+          _lock.wait(waitTime);
         }
       }
     }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/e9a5c3d5/blur-util/src/main/java/org/apache/blur/zookeeper/ZooKeeperLockManager.java
----------------------------------------------------------------------
diff --git a/blur-util/src/main/java/org/apache/blur/zookeeper/ZooKeeperLockManager.java b/blur-util/src/main/java/org/apache/blur/zookeeper/ZooKeeperLockManager.java
index 3813837..c67a97f 100644
--- a/blur-util/src/main/java/org/apache/blur/zookeeper/ZooKeeperLockManager.java
+++ b/blur-util/src/main/java/org/apache/blur/zookeeper/ZooKeeperLockManager.java
@@ -21,6 +21,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.blur.log.Log;
 import org.apache.blur.log.LogFactory;
@@ -35,26 +36,28 @@ public class ZooKeeperLockManager {
 
   private static final Log LOG = LogFactory.getLog(ZooKeeperLockManager.class);
 
-  protected final Map<String, String> lockMap = new HashMap<String, String>();
-  protected final String lockPath;
-  protected final ZooKeeper zooKeeper;
-  protected final Object lock = new Object();
-  protected final Watcher watcher = new Watcher() {
+  protected final Map<String, String> _lockMap = new HashMap<String, String>();
+  protected final String _lockPath;
+  protected final ZooKeeper _zooKeeper;
+  protected final Object _lock = new Object();
+  protected final long _timeout;
+  protected final Watcher _watcher = new Watcher() {
     @Override
     public void process(WatchedEvent event) {
-      synchronized (lock) {
-        lock.notify();
+      synchronized (_lock) {
+        _lock.notify();
       }
     }
   };
 
   public ZooKeeperLockManager(ZooKeeper zooKeeper, String lockPath) {
-    this.zooKeeper = zooKeeper;
-    this.lockPath = lockPath;
+    _zooKeeper = zooKeeper;
+    _lockPath = lockPath;
+    _timeout = TimeUnit.SECONDS.toMillis(1);
   }
 
   public int getNumberOfLockNodesPresent(String name) throws KeeperException, InterruptedException
{
-    List<String> children = zooKeeper.getChildren(lockPath, false);
+    List<String> children = _zooKeeper.getChildren(_lockPath, false);
     int count = 0;
     for (String s : children) {
       if (s.startsWith(name + "_")) {
@@ -65,33 +68,33 @@ public class ZooKeeperLockManager {
   }
 
   public void unlock(String name) throws InterruptedException, KeeperException {
-    if (!lockMap.containsKey(name)) {
+    if (!_lockMap.containsKey(name)) {
       throw new RuntimeException("Lock [" + name + "] has not be created.");
     }
-    String lockPath = lockMap.remove(name);
+    String lockPath = _lockMap.remove(name);
     LOG.debug("Unlocking on path [" + lockPath + "] with name [" + name + "]");
-    zooKeeper.delete(lockPath, -1);
+    _zooKeeper.delete(lockPath, -1);
   }
 
   public void lock(String name) throws KeeperException, InterruptedException {
-    if (lockMap.containsKey(name)) {
+    if (_lockMap.containsKey(name)) {
       throw new RuntimeException("Lock [" + name + "] already created.");
     }
-    String newPath = zooKeeper.create(lockPath + "/" + name + "_", null, Ids.OPEN_ACL_UNSAFE,
+    String newPath = _zooKeeper.create(_lockPath + "/" + name + "_", null, Ids.OPEN_ACL_UNSAFE,
         CreateMode.EPHEMERAL_SEQUENTIAL);
-    lockMap.put(name, newPath);
+    _lockMap.put(name, newPath);
     while (true) {
-      synchronized (lock) {
-        List<String> children = getOnlyThisLocksChildren(name, zooKeeper.getChildren(lockPath,
watcher));
+      synchronized (_lock) {
+        List<String> children = getOnlyThisLocksChildren(name, _zooKeeper.getChildren(_lockPath,
_watcher));
         Collections.sort(children);
         String firstElement = children.get(0);
-        if ((lockPath + "/" + firstElement).equals(newPath)) {
+        if ((_lockPath + "/" + firstElement).equals(newPath)) {
           // yay!, we got the lock
-          LOG.debug("Lock on path [" + lockPath + "] with name [" + name + "]");
+          LOG.debug("Lock on path [" + _lockPath + "] with name [" + name + "]");
           return;
         } else {
-          LOG.debug("Waiting for lock on path [" + lockPath + "] with name [" + name + "]");
-          lock.wait();
+          LOG.debug("Waiting for lock on path [" + _lockPath + "] with name [" + name + "]");
+          _lock.wait(_timeout);
         }
       }
     }


Mime
View raw message