Search in sources :

Example 1 with CuratorOp

use of org.apache.curator.framework.api.transaction.CuratorOp in project dcos-commons by mesosphere.

the class CuratorPersister method recursiveDelete.

@Override
public void recursiveDelete(String unprefixedPath) throws PersisterException {
    final String path = withFrameworkPrefix(unprefixedPath);
    if (path.equals(serviceRootPath)) {
        // Special case: If we're being told to delete root, we should instead delete the contents
        // OF root. We don't have access to delete the root-level '/dcos-service-<svcname>' node
        // itself, despite having created it.
        /* Effective 5/8/2017:
       * We cannot delete the root node of a service directly.
       * This is due to the ACLs on the global DC/OS ZK.
       *
       * The root has:
       * [zk: localhost:2181(CONNECTED) 1] getAcl /
       * 'world,'anyone
       * : cr
       * 'ip,'127.0.0.1
       * : cdrwa
       *
       * Our service nodes have:
       * [zk: localhost:2181(CONNECTED) 0] getAcl /dcos-service-hello-world
       * 'world,'anyone
       * : cdrwa
       *
       * The best we can do is to wipe everything under the root node. A proposed way to "fix"
       * things lives at https://jira.mesosphere.com/browse/INFINITY-1470.
       */
        LOGGER.debug("Deleting children of root {}", path);
        try {
            List<CuratorOp> operations = new ArrayList<>();
            operations.add(client.transactionOp().check().forPath(serviceRootPath));
            Set<String> pendingDeletePaths = new HashSet<>();
            for (String child : client.getChildren().forPath(serviceRootPath)) {
                // Custom logic for root-level children: don't delete the lock node
                if (child.equals(CuratorLocker.LOCK_PATH_NAME)) {
                    continue;
                }
                String childPath = PersisterUtils.joinPaths(serviceRootPath, child);
                deleteChildrenOf(client, childPath, operations, pendingDeletePaths);
                operations.add(client.transactionOp().delete().forPath(childPath));
            }
            client.transaction().forOperations(operations);
        } catch (Exception e) {
            // SUPPRESS CHECKSTYLE IllegalCatch
            throw new PersisterException(Reason.STORAGE_ERROR, String.format("Unable to delete children of root %s: %s", path, e.getMessage()), e);
        }
        // Need to explicitly set null or else curator will return a zero-bytes value later:
        set(unprefixedPath, null);
    } else {
        // Normal case: Delete node itself and any/all children.
        LOGGER.debug("Deleting {} (and any children)", path);
        try {
            client.delete().deletingChildrenIfNeeded().forPath(path);
        } catch (KeeperException.NoNodeException e) {
            throw new PersisterException(Reason.NOT_FOUND, String.format("Path to delete does not exist: %s", path), e);
        } catch (Exception e) {
            // SUPPRESS CHECKSTYLE IllegalCatch
            throw new PersisterException(Reason.STORAGE_ERROR, String.format("Unable to delete %s", path), e);
        }
    }
}
Also used : CuratorOp(org.apache.curator.framework.api.transaction.CuratorOp) ArrayList(java.util.ArrayList) PersisterException(com.mesosphere.sdk.storage.PersisterException) PersisterException(com.mesosphere.sdk.storage.PersisterException) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Example 2 with CuratorOp

use of org.apache.curator.framework.api.transaction.CuratorOp in project druid by druid-io.

the class AnnouncerTest method testSanity.

@Test(timeout = 60_000L)
public void testSanity() throws Exception {
    curator.start();
    curator.blockUntilConnected();
    Announcer announcer = new Announcer(curator, exec);
    announcer.initializeAddedChildren();
    final byte[] billy = StringUtils.toUtf8("billy");
    final String testPath1 = "/test1";
    final String testPath2 = "/somewhere/test2";
    announcer.announce(testPath1, billy);
    Assert.assertNull("/test1 does not exists", curator.checkExists().forPath(testPath1));
    Assert.assertNull("/somewhere/test2 does not exists", curator.checkExists().forPath(testPath2));
    announcer.start();
    while (!announcer.getAddedChildren().contains("/test1")) {
        Thread.sleep(100);
    }
    try {
        Assert.assertArrayEquals("/test1 has data", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertNull("/somewhere/test2 still does not exist", curator.checkExists().forPath(testPath2));
        announcer.announce(testPath2, billy);
        Assert.assertArrayEquals("/test1 still has data", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals("/somewhere/test2 has data", billy, curator.getData().decompressed().forPath(testPath2));
        final CountDownLatch latch = new CountDownLatch(1);
        curator.getCuratorListenable().addListener(new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) {
                if (event.getType() == CuratorEventType.CREATE && event.getPath().equals(testPath1)) {
                    latch.countDown();
                }
            }
        });
        final CuratorOp deleteOp = curator.transactionOp().delete().forPath(testPath1);
        final Collection<CuratorTransactionResult> results = curator.transaction().forOperations(deleteOp);
        Assert.assertEquals(1, results.size());
        final CuratorTransactionResult result = results.iterator().next();
        // assert delete
        Assert.assertEquals(Code.OK.intValue(), result.getError());
        Assert.assertTrue("Wait for /test1 to be created", timing.forWaiting().awaitLatch(latch));
        Assert.assertArrayEquals("expect /test1 data is restored", billy, curator.getData().decompressed().forPath(testPath1));
        Assert.assertArrayEquals("expect /somewhere/test2 is still there", billy, curator.getData().decompressed().forPath(testPath2));
        announcer.unannounce(testPath1);
        Assert.assertNull("expect /test1 unannounced", curator.checkExists().forPath(testPath1));
        Assert.assertArrayEquals("expect /somewhere/test2 is still still there", billy, curator.getData().decompressed().forPath(testPath2));
    } finally {
        announcer.stop();
    }
    Assert.assertNull("expect /test1 remains unannounced", curator.checkExists().forPath(testPath1));
    Assert.assertNull("expect /somewhere/test2 unannounced", curator.checkExists().forPath(testPath2));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorOp(org.apache.curator.framework.api.transaction.CuratorOp) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorTransactionResult(org.apache.curator.framework.api.transaction.CuratorTransactionResult) Test(org.junit.Test)

Aggregations

CuratorOp (org.apache.curator.framework.api.transaction.CuratorOp)2 PersisterException (com.mesosphere.sdk.storage.PersisterException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)1 CuratorListener (org.apache.curator.framework.api.CuratorListener)1 CuratorTransactionResult (org.apache.curator.framework.api.transaction.CuratorTransactionResult)1 KeeperException (org.apache.zookeeper.KeeperException)1 Test (org.junit.Test)1