Search in sources :

Example 6 with ZooLock

use of org.apache.accumulo.fate.zookeeper.ZooLock in project accumulo by apache.

the class ZooLockTest method testDeleteWaiting.

@Test(timeout = 10000)
public void testDeleteWaiting() throws Exception {
    String parent = "/zltest-" + this.hashCode() + "-l" + pdCount.incrementAndGet();
    ZooReaderWriter zk = ZooReaderWriter.getInstance(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes());
    zk.mkdirs(parent);
    ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes(), parent);
    Assert.assertFalse(zl.isLocked());
    TestALW lw = new TestALW();
    zl.lockAsync(lw, "test1".getBytes());
    lw.waitForChanges(1);
    Assert.assertTrue(lw.locked);
    Assert.assertTrue(zl.isLocked());
    Assert.assertNull(lw.exception);
    Assert.assertNull(lw.reason);
    ZooLock zl2 = new ZooLock(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes(), parent);
    TestALW lw2 = new TestALW();
    zl2.lockAsync(lw2, "test2".getBytes());
    Assert.assertFalse(lw2.locked);
    Assert.assertFalse(zl2.isLocked());
    ZooLock zl3 = new ZooLock(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes(), parent);
    TestALW lw3 = new TestALW();
    zl3.lockAsync(lw3, "test3".getBytes());
    List<String> children = zk.getChildren(parent);
    Collections.sort(children);
    zk.delete(parent + "/" + children.get(1), -1);
    lw2.waitForChanges(1);
    Assert.assertFalse(lw2.locked);
    Assert.assertNotNull(lw2.exception);
    Assert.assertNull(lw2.reason);
    zk.delete(parent + "/" + children.get(0), -1);
    lw.waitForChanges(2);
    Assert.assertEquals(LockLossReason.LOCK_DELETED, lw.reason);
    Assert.assertNull(lw.exception);
    lw3.waitForChanges(1);
    Assert.assertTrue(lw3.locked);
    Assert.assertTrue(zl3.isLocked());
    Assert.assertNull(lw3.exception);
    Assert.assertNull(lw3.reason);
    zl3.unlock();
}
Also used : ZooReaderWriter(org.apache.accumulo.fate.zookeeper.ZooReaderWriter) ZooLock(org.apache.accumulo.fate.zookeeper.ZooLock) Test(org.junit.Test)

Example 7 with ZooLock

use of org.apache.accumulo.fate.zookeeper.ZooLock in project accumulo by apache.

the class ZooLockTest method testUnexpectedEvent.

@Test(timeout = 10000)
public void testUnexpectedEvent() throws Exception {
    String parent = "/zltest-" + this.hashCode() + "-l" + pdCount.incrementAndGet();
    ConnectedWatcher watcher = new ConnectedWatcher();
    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 30000, watcher);
    zk.addAuthInfo("digest", "secret".getBytes());
    while (!watcher.isConnected()) {
        Thread.sleep(200);
    }
    zk.create(parent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes(), parent);
    Assert.assertFalse(zl.isLocked());
    // would not expect data to be set on this node, but it should not cause problems.....
    zk.setData(parent, "foo".getBytes(), -1);
    TestALW lw = new TestALW();
    zl.lockAsync(lw, "test1".getBytes());
    lw.waitForChanges(1);
    Assert.assertTrue(lw.locked);
    Assert.assertTrue(zl.isLocked());
    Assert.assertNull(lw.exception);
    Assert.assertNull(lw.reason);
    // would not expect data to be set on this node either
    zk.setData(zl.getLockPath(), "bar".getBytes(), -1);
    zk.delete(zl.getLockPath(), -1);
    lw.waitForChanges(2);
    Assert.assertEquals(LockLossReason.LOCK_DELETED, lw.reason);
    Assert.assertNull(lw.exception);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooLock(org.apache.accumulo.fate.zookeeper.ZooLock) Test(org.junit.Test)

Aggregations

ZooLock (org.apache.accumulo.fate.zookeeper.ZooLock)7 Test (org.junit.Test)7 ZooReaderWriter (org.apache.accumulo.fate.zookeeper.ZooReaderWriter)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 Field (java.lang.reflect.Field)1