Example usage for org.apache.zookeeper ZooKeeper setACL

List of usage examples for org.apache.zookeeper ZooKeeper setACL

Introduction

In this page you can find the example usage for org.apache.zookeeper ZooKeeper setACL.

Prototype

public Stat setACL(final String path, List<ACL> acl, int aclVersion)
        throws KeeperException, InterruptedException 

Source Link

Document

Set the ACL for the node of the given path if such a node exists and the given aclVersion matches the acl version of the node.

Usage

From source file:com.boundary.zoocreeper.Restore.java

License:Apache License

private void restoreNode(ZooKeeper zk, BackupZNode zNode) throws KeeperException, InterruptedException {
    createPath(zk, getParentPath(zNode.path));
    try {/*  w w  w  .  j a  va  2s.  c o m*/
        zk.create(zNode.path, zNode.data, zNode.acls, CreateMode.PERSISTENT);
        LOGGER.info("Created node: {}", zNode.path);
    } catch (NodeExistsException e) {
        if (options.overwriteExisting) {
            // TODO: Compare with current data / acls
            zk.setACL(zNode.path, zNode.acls, -1);
            zk.setData(zNode.path, zNode.data, -1);
        } else {
            LOGGER.warn("Node already exists: {}", zNode.path);
        }
    }
}

From source file:com.github.mosuka.zookeeper.nicli.command.SetAclCommand.java

License:Apache License

@Override
public void run(Map<String, Object> parameters) {
    try {/* ww w  .ja va 2s .  com*/
        String path = (String) parameters.get("path");
        String acl = (String) parameters.get("acl");
        int version = parameters.containsKey("version") ? (Integer) parameters.get("version") : DEFAULT_VERSION;
        boolean withStat = parameters.containsKey("with_stat") ? (Boolean) parameters.get("with_stat")
                : DEFAULT_WITH_STAT;

        ZooKeeper zk = getZookeeperConnection().getZooKeeper();

        List<ACL> aclObj = acl.length() > 0 ? ACLUtil.parseACLs(acl) : Ids.OPEN_ACL_UNSAFE;
        Stat stat = zk.setACL(path, aclObj, version);

        if (stat != null && withStat) {
            putResponse("stat", StatUtil.stat2Map(stat));
        }

        setStatus(Command.STATUS_SUCCESS);
        setMessage(Command.SUCCESS_MESSAGE);
    } catch (KeeperException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (InterruptedException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (ClassCastException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    } catch (NullPointerException e) {
        setStatus(Command.STATUS_ERROR);
        setMessage(e.getMessage());
    }
}

From source file:org.apache.bookkeeper.zookeeper.BkZooKeeperClient.java

License:Apache License

@Override
public Stat setACL(final String path, final List<ACL> acl, final int version)
        throws KeeperException, InterruptedException {
    return BkZooWorker.syncCallWithRetries(this, new ZooCallable<Stat>() {

        @Override//ww  w.  j  a  va 2s  .com
        public String toString() {
            return String.format("setACL (%s, acl = %s, version = %d)", path, acl, version);
        }

        @Override
        public Stat call() throws KeeperException, InterruptedException {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                return BkZooKeeperClient.super.setACL(path, acl, version);
            }
            return zkHandle.setACL(path, acl, version);
        }

    }, operationRetryPolicy, rateLimiter, setACLStats);
}

From source file:org.apache.bookkeeper.zookeeper.ZooKeeperClient.java

License:Apache License

@Override
public Stat setACL(final String path, final List<ACL> acl, final int version)
        throws KeeperException, InterruptedException {
    return ZooWorker.syncCallWithRetries(this, new ZooCallable<Stat>() {

        @Override//from w w  w .  j  a va 2  s  .c o m
        public String toString() {
            return String.format("setACL (%s, acl = %s, version = %d)", path, acl, version);
        }

        @Override
        public Stat call() throws KeeperException, InterruptedException {
            ZooKeeper zkHandle = zk.get();
            if (null == zkHandle) {
                return ZooKeeperClient.super.setACL(path, acl, version);
            }
            return zkHandle.setACL(path, acl, version);
        }

    }, operationRetryPolicy, rateLimiter, setACLStats);
}

From source file:org.apache.hadoop.hbase.TestZooKeeper.java

License:Apache License

/**
 * A test for HBASE-3238//from  ww  w .j  a v a 2s  .co m
 * @throws IOException A connection attempt to zk failed
 * @throws InterruptedException One of the non ZKUtil actions was interrupted
 * @throws KeeperException Any of the zookeeper connections had a
 * KeeperException
 */
@Test
public void testCreateSilentIsReallySilent() throws InterruptedException, KeeperException, IOException {
    Configuration c = TEST_UTIL.getConfiguration();

    String aclZnode = "/aclRoot";
    String quorumServers = ZKConfig.getZKQuorumServersString(c);
    int sessionTimeout = 5 * 1000; // 5 seconds
    ZooKeeper zk = new ZooKeeper(quorumServers, sessionTimeout, EmptyWatcher.instance);
    zk.addAuthInfo("digest", "hbase:rox".getBytes());

    // Assumes the  root of the ZooKeeper space is writable as it creates a node
    // wherever the cluster home is defined.
    ZooKeeperWatcher zk2 = new ZooKeeperWatcher(TEST_UTIL.getConfiguration(), "testCreateSilentIsReallySilent",
            null);

    // Save the previous ACL
    Stat s = null;
    List<ACL> oldACL = null;
    while (true) {
        try {
            s = new Stat();
            oldACL = zk.getACL("/", s);
            break;
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                LOG.warn("Possibly transient ZooKeeper exception", e);
                Threads.sleep(100);
                break;
            default:
                throw e;
            }
        }
    }

    // I set this acl after the attempted creation of the cluster home node.
    // Add retries in case of retryable zk exceptions.
    while (true) {
        try {
            zk.setACL("/", ZooDefs.Ids.CREATOR_ALL_ACL, -1);
            break;
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                LOG.warn("Possibly transient ZooKeeper exception: " + e);
                Threads.sleep(100);
                break;
            default:
                throw e;
            }
        }
    }

    while (true) {
        try {
            zk.create(aclZnode, null, ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
            break;
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                LOG.warn("Possibly transient ZooKeeper exception: " + e);
                Threads.sleep(100);
                break;
            default:
                throw e;
            }
        }
    }
    zk.close();
    ZKUtil.createAndFailSilent(zk2, aclZnode);

    // Restore the ACL
    ZooKeeper zk3 = new ZooKeeper(quorumServers, sessionTimeout, EmptyWatcher.instance);
    zk3.addAuthInfo("digest", "hbase:rox".getBytes());
    try {
        zk3.setACL("/", oldACL, -1);
    } finally {
        zk3.close();
    }
}

From source file:org.apache.hadoop.hbase.zookeeper.ZkAclReset.java

License:Apache License

private static void resetAcls(final ZooKeeper zk, final String znode) throws Exception {
    List<String> children = zk.getChildren(znode, false);
    if (children != null) {
        for (String child : children) {
            resetAcls(zk, znode + '/' + child);
        }//from   ww  w  .j  a va2s. c o  m
    }
    LOG.info(" - reset acl for " + znode);
    zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1);
}

From source file:org.apache.nifi.toolkit.zkmigrator.ZooKeeperMigrator.java

License:Apache License

private Stat transmitNode(ZooKeeper zooKeeper, DataStatAclNode node) {
    Preconditions.checkNotNull(zooKeeper, "zooKeeper must not be null");
    Preconditions.checkNotNull(node, "node must not be null");
    try {/*w  ww  . ja va  2s .  c  om*/
        LOGGER.debug("attempting to transfer node to {} with ACL {}: {}", zooKeeperEndpointConfig,
                node.getAcls(), node);
        // set data without caring what the previous version of the data at that path
        zooKeeper.setData(node.getPath(), node.getData(), -1);
        zooKeeper.setACL(node.getPath(), node.getAcls(), -1);
        LOGGER.info("transferred node {} in {}", node, zooKeeperEndpointConfig);
    } catch (InterruptedException | KeeperException e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throw new RuntimeException(
                String.format("unable to transmit data to %s for path %s", zooKeeper, node.getPath()), e);
    }
    return node.getStat();
}

From source file:org.apache.niolex.address.ext.ZKOperatorTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Test//from  w  ww .ja  v  a 2s . c o m
public void testRemoveACLTree() throws Exception {
    ZooKeeper zkback = zkop.zooKeeper();
    Field f = FieldUtil.getField(ZKConnector.class, "zk");
    ZooKeeper zk = mock(ZooKeeper.class);
    KeeperException throwable1 = KeeperException.create(KeeperException.Code.BADVERSION);
    KeeperException throwable2 = KeeperException.create(KeeperException.Code.APIERROR);
    when(zk.setACL(anyString(), anyList(), anyInt())).thenThrow(throwable1, throwable2, throwable1, throwable2);
    //
    List<ACL> acl = OPMain.getCDR4Server();
    FieldUtil.setFieldValue(zkop, f, zk);
    boolean flag = false;
    try {
        zkop.removeACL("/localhost:9001", acl);
    } catch (ZKException e) {
        flag = true;
        assertEquals(e.getCode(), ZKException.Code.OTHER);
    }
    assertTrue(flag);
    // ------
    flag = false;
    try {
        zkop.addACL("/localhost:9001", acl);
    } catch (ZKException e) {
        flag = true;
        assertEquals(e.getCode(), ZKException.Code.OTHER);
    }
    assertTrue(flag);
    FieldUtil.setFieldValue(zkop, f, zkback);
}