incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [3/6] git commit: Refactored the lock class out.
Date Tue, 11 Jun 2013 01:53:04 GMT
Refactored the lock class out.


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

Branch: refs/heads/0.1.5
Commit: 126517e2e3205cc66789696ce817f47d43560752
Parents: 6864c34
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Mon Jun 10 20:05:46 2013 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Mon Jun 10 20:05:46 2013 -0400

----------------------------------------------------------------------
 .../blur/manager/indexserver/SafeMode.java      | 65 +------------
 .../blur/zookeeper/ZooKeeperLockManager.java    | 98 ++++++++++++++++++++
 2 files changed, 103 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/126517e2/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 155722f..9625100 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
@@ -18,19 +18,16 @@ package org.apache.blur.manager.indexserver;
  */
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.blur.log.Log;
 import org.apache.blur.log.LogFactory;
 import org.apache.blur.zookeeper.ZkUtils;
+import org.apache.blur.zookeeper.ZooKeeperLockManager;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
-import org.apache.zookeeper.WatchedEvent;
-import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.ZooKeeper;
 
@@ -43,32 +40,23 @@ import org.apache.zookeeper.ZooKeeper;
  * settled and can now come online.
  * 
  */
-public class SafeMode {
+public class SafeMode extends ZooKeeperLockManager {
 
   private static final Log LOG = LogFactory.getLog(SafeMode.class);
   private static final String STARTUP = "STARTUP";
   private static final String SETUP = "SETUP";
 
   private final ZooKeeper zooKeeper;
-  private final String lockPath;
+  
   private final long waitTime;
-  private final Object lock = new Object();
-  private final Watcher watcher = new Watcher() {
-    @Override
-    public void process(WatchedEvent event) {
-      synchronized (lock) {
-        lock.notify();
-      }
-    }
-  };
-  private final Map<String, String> lockMap = new HashMap<String, String>();
+  
   private final String nodePath;
   private final long duplicateNodeTimeout;
 
   public SafeMode(ZooKeeper zooKeeper, String lockPath, String nodePath, TimeUnit waitTimeUnit,
long waitTime,
       TimeUnit duplicateNodeTimeoutTimeUnit, long duplicateNodeTimeout) {
+    super(zooKeeper,lockPath);
     this.zooKeeper = zooKeeper;
-    this.lockPath = lockPath;
     this.waitTime = waitTimeUnit.toMillis(waitTime);
     this.duplicateNodeTimeout = duplicateNodeTimeoutTimeUnit.toNanos(duplicateNodeTimeout);
     this.nodePath = nodePath;
@@ -130,15 +118,6 @@ public class SafeMode {
     return false;
   }
 
-  private void unlock(String name) throws InterruptedException, KeeperException {
-    if (!lockMap.containsKey(name)) {
-      throw new RuntimeException("Lock [" + name + "] has not be created.");
-    }
-    String lockPath = lockMap.get(name);
-    LOG.debug("Unlocking on path [" + lockPath + "] with name [" + name + "]");
-    zooKeeper.delete(lockPath, -1);
-  }
-
   private void register(String node, byte[] data) throws KeeperException, InterruptedException
{
     String p = nodePath + "/" + node;
     long start = System.nanoTime();
@@ -156,38 +135,4 @@ public class SafeMode {
     zooKeeper.create(p, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
   }
 
-  private void lock(String name) throws KeeperException, InterruptedException {
-    if (lockMap.containsKey(name)) {
-      throw new RuntimeException("Lock [" + name + "] already created.");
-    }
-    String newPath = zooKeeper.create(lockPath + "/" + name + "_", null, Ids.OPEN_ACL_UNSAFE,
-        CreateMode.EPHEMERAL_SEQUENTIAL);
-    lockMap.put(name, newPath);
-    while (true) {
-      synchronized (lock) {
-        List<String> children = getOnlyThisLocksChildren(name, zooKeeper.getChildren(lockPath,
watcher));
-        Collections.sort(children);
-        String firstElement = children.get(0);
-        if ((lockPath + "/" + firstElement).equals(newPath)) {
-          // yay!, we got the lock
-          LOG.debug("Lock on path [" + lockPath + "] with name [" + name + "]");
-          return;
-        } else {
-          LOG.debug("Waiting for lock on path [" + lockPath + "] with name [" + name + "]");
-          lock.wait();
-        }
-      }
-    }
-  }
-
-  private List<String> getOnlyThisLocksChildren(String name, List<String> children)
{
-    List<String> result = new ArrayList<String>();
-    for (String c : children) {
-      if (c.startsWith(name + "_")) {
-        result.add(c);
-      }
-    }
-    return result;
-  }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/126517e2/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
new file mode 100644
index 0000000..81cfde9
--- /dev/null
+++ b/blur-util/src/main/java/org/apache/blur/zookeeper/ZooKeeperLockManager.java
@@ -0,0 +1,98 @@
+package org.apache.blur.zookeeper;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.blur.log.Log;
+import org.apache.blur.log.LogFactory;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.apache.zookeeper.ZooKeeper;
+
+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() {
+    @Override
+    public void process(WatchedEvent event) {
+      synchronized (lock) {
+        lock.notify();
+      }
+    }
+  };
+
+  public ZooKeeperLockManager(ZooKeeper zooKeeper, String lockPath) {
+    this.zooKeeper = zooKeeper;
+    this.lockPath = lockPath;
+  }
+
+  public void unlock(String name) throws InterruptedException, KeeperException {
+    if (!lockMap.containsKey(name)) {
+      throw new RuntimeException("Lock [" + name + "] has not be created.");
+    }
+    String lockPath = lockMap.get(name);
+    LOG.debug("Unlocking on path [" + lockPath + "] with name [" + name + "]");
+    zooKeeper.delete(lockPath, -1);
+  }
+
+  public void lock(String name) throws KeeperException, InterruptedException {
+    if (lockMap.containsKey(name)) {
+      throw new RuntimeException("Lock [" + name + "] already created.");
+    }
+    String newPath = zooKeeper.create(lockPath + "/" + name + "_", null, Ids.OPEN_ACL_UNSAFE,
+        CreateMode.EPHEMERAL_SEQUENTIAL);
+    lockMap.put(name, newPath);
+    while (true) {
+      synchronized (lock) {
+        List<String> children = getOnlyThisLocksChildren(name, zooKeeper.getChildren(lockPath,
watcher));
+        Collections.sort(children);
+        String firstElement = children.get(0);
+        if ((lockPath + "/" + firstElement).equals(newPath)) {
+          // yay!, we got the lock
+          LOG.debug("Lock on path [" + lockPath + "] with name [" + name + "]");
+          return;
+        } else {
+          LOG.debug("Waiting for lock on path [" + lockPath + "] with name [" + name + "]");
+          lock.wait();
+        }
+      }
+    }
+  }
+
+  private List<String> getOnlyThisLocksChildren(String name, List<String> children)
{
+    List<String> result = new ArrayList<String>();
+    for (String c : children) {
+      if (c.startsWith(name + "_")) {
+        result.add(c);
+      }
+    }
+    return result;
+  }
+}


Mime
View raw message