Advertisement
gusto2

ZooKeeper distributed lock samples

Jun 17th, 2014
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. package com.apogado.test.distlock.test;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import org.apache.zookeeper.CreateMode;
  6. import org.apache.zookeeper.KeeperException;
  7. import org.apache.zookeeper.WatchedEvent;
  8. import org.apache.zookeeper.Watcher;
  9. import org.apache.zookeeper.ZooDefs;
  10. import org.apache.zookeeper.ZooKeeper;
  11. import org.apache.zookeeper.data.ACL;
  12. import org.apache.zookeeper.data.Stat;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15.  
  16. /**
  17.  *
  18.  * @author Gabriel
  19.  */
  20. public class LockTest {
  21.  
  22.     private static final Logger logger = LoggerFactory.getLogger(LockTest.class);
  23.     public static final String CONN_STRING = "localhost:2181";
  24.     ZooKeeper zookeeperServer;
  25.     final Watcher watcher = new TestWatcher();
  26.  
  27.     @org.junit.Before
  28.     public void setup() {
  29.         try {
  30.             synchronized (watcher) {
  31.                 this.zookeeperServer = new ZooKeeper(CONN_STRING, 3000, watcher);
  32.                 watcher.wait(3000);
  33.             }
  34.             ZooKeeper.States state = this.zookeeperServer.getState();
  35.             logger.info("state: {}", this.zookeeperServer.getState());
  36.             org.junit.Assert.assertEquals(ZooKeeper.States.CONNECTED, state);
  37.         } catch (Exception ex) {
  38.             logger.error("setup", ex);
  39.             org.junit.Assert.fail(ex.toString());
  40.         }
  41.  
  42.     }
  43.  
  44.     @org.junit.After
  45.     public void cleanup() {
  46.         try {
  47.             if (this.zookeeperServer != null) {
  48.                 this.zookeeperServer.close();
  49.             }
  50.         } catch (Exception ex) {
  51.             logger.warn("cleanup", ex);
  52.         }
  53.     }
  54.  
  55.     @org.junit.Test
  56.     public void testLock() {
  57.         try {
  58.             String parentNode = "/lock";
  59.             String lockNodePath = "/lock/test";
  60.             logger.info("Node lock path: {}", lockNodePath);
  61.  
  62.             Stat stat;
  63.             stat = this.zookeeperServer.exists("/cxf-locator", false);
  64.             org.junit.Assert.assertNotNull(stat);
  65.             logger.info("stat immediate: {}", stat);
  66.  
  67.  
  68.             try {
  69.                 stat = this.zookeeperServer.exists("/nonexisting", false);
  70.                 org.junit.Assert.assertNull(stat);
  71.             } catch (KeeperException ex) {
  72.                 logger.info("Node doesn't exist: " + ex.getMessage());
  73.             }
  74.  
  75.             // create parent lock
  76.             logger.info("Checking for the parent node");
  77.             stat = this.zookeeperServer.exists(parentNode, false);
  78.             if (stat == null) {
  79.                 logger.info("Parent node doesn't exist, creating one");
  80.                 this.zookeeperServer.create(parentNode, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  81.             } else {
  82.                 logger.info("Parent node exists");
  83.             }
  84.  
  85.             // create a lock node
  86.             String path = this.zookeeperServer.create(lockNodePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
  87.             logger.info("Created lock path: {}", path);
  88.  
  89.             // check the lock is created
  90.             stat = this.zookeeperServer.exists(lockNodePath, false);
  91.             org.junit.Assert.assertNotNull(stat);
  92.             logger.info("Lock data: {}", stat.toString());
  93.  
  94. //            logger.info("Sleeping to allow manual check");
  95. //            Thread.sleep(10000);
  96.  
  97.             // add node watcher
  98.             TestWatcher nodeWatcher = new TestWatcher();
  99.             stat = this.zookeeperServer.exists(lockNodePath, nodeWatcher);
  100.  
  101.             // set data
  102.             this.zookeeperServer.setData(lockNodePath, "stop".getBytes(), stat.getVersion());
  103.             synchronized (nodeWatcher) {
  104.                 logger.info("Waiting to be notified");
  105.                 nodeWatcher.wait(1000);
  106.                 logger.info("Notified: {}", nodeWatcher.isNotified());
  107.                 org.junit.Assert.assertTrue(nodeWatcher.isNotified());
  108.             }
  109.  
  110.             // delete lock
  111.             try {
  112.                 this.zookeeperServer.delete(path, -1);
  113.                 stat = this.zookeeperServer.exists(lockNodePath, false);
  114.                 org.junit.Assert.assertNull(stat);
  115.                 logger.info("lock node deleted");
  116.             } catch (KeeperException ke) {
  117.                 logger.error("failed to delete a lock node", ke);
  118.                 org.junit.Assert.fail(ke.toString());
  119.             }
  120.  
  121.         } catch (Exception ex) {
  122.             logger.warn("testLock: ", ex);
  123.             org.junit.Assert.fail(ex.toString());
  124.         }
  125.     }
  126.  
  127.     public class TestWatcher implements Watcher {
  128.  
  129.         private boolean notified;
  130.  
  131.         public void process(WatchedEvent we) {
  132.             logger.info("watcher event: {}", we.toString());
  133.             synchronized (this) {
  134.                 setNotified(true);
  135.                 this.notify();
  136.             }
  137.         }
  138.  
  139.         public boolean isNotified() {
  140.             return notified;
  141.         }
  142.  
  143.         public void setNotified(boolean notified) {
  144.             this.notified = notified;
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement