Search in sources :

Example 1 with ZooLock

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

the class ZooLockTest method testDeleteParent.

@Test(timeout = 10000)
public void testDeleteParent() throws Exception {
    String parent = "/zltest-" + this.hashCode() + "-l" + pdCount.incrementAndGet();
    ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes(), parent);
    Assert.assertFalse(zl.isLocked());
    ZooReaderWriter zk = ZooReaderWriter.getInstance(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes());
    // intentionally created parent after lock
    zk.mkdirs(parent);
    zk.delete(parent, -1);
    zk.mkdirs(parent);
    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);
    zl.unlock();
}
Also used : ZooReaderWriter(org.apache.accumulo.fate.zookeeper.ZooReaderWriter) ZooLock(org.apache.accumulo.fate.zookeeper.ZooLock) Test(org.junit.Test)

Example 2 with ZooLock

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

the class ZooLockTest method testTryLock.

@Test(timeout = 10000)
public void testTryLock() throws Exception {
    String parent = "/zltest-" + this.hashCode() + "-l" + pdCount.incrementAndGet();
    ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 1000, "digest", "secret".getBytes(), parent);
    ConnectedWatcher watcher = new ConnectedWatcher();
    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 1000, watcher);
    zk.addAuthInfo("digest", "secret".getBytes());
    while (!watcher.isConnected()) {
        Thread.sleep(200);
    }
    for (int i = 0; i < 10; i++) {
        zk.create(parent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.delete(parent, -1);
    }
    zk.create(parent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    TestALW lw = new TestALW();
    boolean ret = zl.tryLock(lw, "test1".getBytes());
    Assert.assertTrue(ret);
    // make sure still watching parent even though a lot of events occurred for the parent
    synchronized (zl) {
        Field field = zl.getClass().getDeclaredField("watchingParent");
        field.setAccessible(true);
        Assert.assertTrue((Boolean) field.get(zl));
    }
    zl.unlock();
}
Also used : Field(java.lang.reflect.Field) ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooLock(org.apache.accumulo.fate.zookeeper.ZooLock) Test(org.junit.Test)

Example 3 with ZooLock

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

the class ZooLockTest method testNoParent.

@Test(timeout = 10000)
public void testNoParent() throws Exception {
    String parent = "/zltest-" + this.hashCode() + "-l" + pdCount.incrementAndGet();
    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.assertFalse(lw.locked);
    Assert.assertFalse(zl.isLocked());
    Assert.assertNotNull(lw.exception);
    Assert.assertNull(lw.reason);
}
Also used : ZooLock(org.apache.accumulo.fate.zookeeper.ZooLock) Test(org.junit.Test)

Example 4 with ZooLock

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

the class ZooLockTest method testDeleteLock.

@Test(timeout = 10000)
public void testDeleteLock() 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);
    zk.delete(zl.getLockPath(), -1);
    lw.waitForChanges(2);
    Assert.assertEquals(LockLossReason.LOCK_DELETED, lw.reason);
    Assert.assertNull(lw.exception);
}
Also used : ZooReaderWriter(org.apache.accumulo.fate.zookeeper.ZooReaderWriter) ZooLock(org.apache.accumulo.fate.zookeeper.ZooLock) Test(org.junit.Test)

Example 5 with ZooLock

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

the class ZooLockTest method testChangeData.

@Test(timeout = 10000)
public void testChangeData() throws Exception {
    String parent = "/zltest-" + this.hashCode() + "-l" + pdCount.incrementAndGet();
    ConnectedWatcher watcher = new ConnectedWatcher();
    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 1000, 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(), 1000, "digest", "secret".getBytes(), parent);
    TestALW lw = new TestALW();
    zl.lockAsync(lw, "test1".getBytes());
    Assert.assertEquals("test1", new String(zk.getData(zl.getLockPath(), null, null)));
    zl.replaceLockData("test2".getBytes());
    Assert.assertEquals("test2", new String(zk.getData(zl.getLockPath(), null, null)));
}
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