Example usage for org.apache.zookeeper KeeperException.NodeExistsException getMessage

List of usage examples for org.apache.zookeeper KeeperException.NodeExistsException getMessage

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException.NodeExistsException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:com.yahoo.pulsar.broker.cache.ConfigurationCacheService.java

License:Apache License

private void initZK() throws PulsarServerException {

    String[] paths = new String[] { CLUSTERS_ROOT, POLICIES_ROOT };

    // initialize the zk client with values
    try {//from  ww  w .j  av a  2  s.c o m
        ZooKeeper zk = cache.getZooKeeper();
        for (String path : paths) {
            try {
                if (zk.exists(path, false) == null) {
                    ZkUtils.createFullPathOptimistic(zk, path, new byte[0], Ids.OPEN_ACL_UNSAFE,
                            CreateMode.PERSISTENT);
                }
            } catch (KeeperException.NodeExistsException e) {
                // Ok
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new PulsarServerException(e);
    }

}

From source file:com.yahoo.pulsar.broker.cache.LocalZooKeeperCacheService.java

License:Apache License

private void initZK() throws PulsarServerException {
    String[] paths = new String[] { MANAGED_LEDGER_ROOT, OWNER_INFO_ROOT, LOCAL_POLICIES_ROOT };
    // initialize the zk client with values
    try {//from w w  w. j  a va  2  s  . com
        ZooKeeper zk = cache.getZooKeeper();
        for (String path : paths) {
            if (cache.exists(path)) {
                continue;
            }

            try {
                ZkUtils.createFullPathOptimistic(zk, path, new byte[0], Ids.OPEN_ACL_UNSAFE,
                        CreateMode.PERSISTENT);
            } catch (KeeperException.NodeExistsException e) {
                // Ok
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new PulsarServerException(e);
    }

}

From source file:io.flood.registry.zk.ZookeeperRegistry.java

License:Apache License

public void createPersistent(String path) {
    try {/*from   ww w.j  av  a 2s. c o m*/
        client.create().forPath(path);
    } catch (KeeperException.NodeExistsException e) {
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:io.flood.registry.zk.ZookeeperRegistry.java

License:Apache License

public void createEphemeral(String path) {
    try {/*from   ww  w . j a  va 2s. c  o m*/
        client.create().withMode(CreateMode.EPHEMERAL).forPath(path);
    } catch (KeeperException.NodeExistsException e) {
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:org.apache.hcatalog.templeton.tool.ZooKeeperStorage.java

License:Apache License

/**
 * Create a node in ZooKeeper/*w w  w. j av  a2s  . c o m*/
 */
public void create(Type type, String id) throws IOException {
    try {
        String[] paths = getPaths(makeZnode(type, id));
        boolean wasCreated = false;
        for (String znode : paths) {
            try {
                zk.create(znode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                wasCreated = true;
            } catch (KeeperException.NodeExistsException e) {
            }
        }
        if (wasCreated) {
            try {
                // Really not sure if this should go here.  Will have
                // to see how the storage mechanism evolves.
                if (type.equals(Type.JOB)) {
                    JobStateTracker jt = new JobStateTracker(id, zk, false, job_trackingpath);
                    jt.create();
                }
            } catch (Exception e) {
                LOG.warn("Error tracking: " + e.getMessage());
                // If we couldn't create the tracker node, don't
                // create the main node.
                zk.delete(makeZnode(type, id), -1);
            }
        }
        if (zk.exists(makeZnode(type, id), false) == null)
            throw new IOException("Unable to create " + makeZnode(type, id));
        if (wasCreated) {
            try {
                saveField(type, id, "created", Long.toString(System.currentTimeMillis()));
            } catch (NotFoundException nfe) {
                // Wow, something's really wrong.
                throw new IOException("Couldn't write to node " + id, nfe);
            }
        }
    } catch (KeeperException e) {
        throw new IOException("Creating " + id, e);
    } catch (InterruptedException e) {
        throw new IOException("Creating " + id, e);
    }
}

From source file:org.apache.pulsar.broker.service.BrokerService.java

License:Apache License

private void updateDynamicServiceConfiguration() {

    try {//from   w ww .  ja  v a2  s  . c o m
        // create dynamic-config znode if not present
        if (pulsar.getZkClient().exists(BROKER_SERVICE_CONFIGURATION_PATH, false) == null) {
            try {
                byte[] data = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(Maps.newHashMap());
                ZkUtils.createFullPathOptimistic(pulsar.getZkClient(), BROKER_SERVICE_CONFIGURATION_PATH, data,
                        Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException.NodeExistsException e) {
                // Ok
            }
        }
        Optional<Map<String, String>> data = dynamicConfigurationCache.get(BROKER_SERVICE_CONFIGURATION_PATH);
        if (data.isPresent() && data.get() != null) {
            data.get().forEach((key, value) -> {
                try {
                    Field field = ServiceConfiguration.class.getDeclaredField(key);
                    if (field != null && field.isAnnotationPresent(FieldContext.class)) {
                        field.setAccessible(true);
                        field.set(pulsar().getConfiguration(), FieldParser.value(value, field));
                        log.info("Successfully updated {}/{}", key, value);
                    }
                } catch (Exception e) {
                    log.warn("Failed to update service configuration {}/{}, {}", key, value, e.getMessage());
                }
            });
        }
    } catch (Exception e) {
        log.warn("Failed to read zookeeper path [{}]:", BROKER_SERVICE_CONFIGURATION_PATH, e);
    }
    // register a listener: it updates field value and triggers appropriate registered field-listener only if
    // field's value has been changed so, registered doesn't have to update field value in ServiceConfiguration
    dynamicConfigurationCache.registerListener(new ZooKeeperCacheListener<Map<String, String>>() {
        @SuppressWarnings("unchecked")
        @Override
        public void onUpdate(String path, Map<String, String> data, Stat stat) {
            if (BROKER_SERVICE_CONFIGURATION_PATH.equalsIgnoreCase(path) && data != null) {
                data.forEach((configKey, value) -> {
                    Field configField = dynamicConfigurationMap.get(configKey);
                    Object newValue = FieldParser.value(data.get(configKey), configField);
                    if (configField != null) {
                        Consumer listener = configRegisteredListeners.get(configKey);
                        try {
                            Object existingValue = configField.get(pulsar.getConfiguration());
                            configField.set(pulsar.getConfiguration(), newValue);
                            log.info("Successfully updated configuration {}/{}", configKey,
                                    data.get(configKey));
                            if (listener != null && !existingValue.equals(newValue)) {
                                listener.accept(newValue);
                            }
                        } catch (Exception e) {
                            log.error("Failed to update config {}/{}", configKey, newValue);
                        }
                    } else {
                        log.error("Found non-dynamic field in dynamicConfigMap {}/{}", configKey, newValue);
                    }
                });
            }
        }
    });

}

From source file:org.trustedanalytics.auth.gateway.zookeeper.client.KerberosfulZookeeperClient.java

License:Apache License

@Override
public void createZnode(String znodePath, String username, ZookeeperPermission permission) throws Exception {

    try {//from   w  w  w .j a  v  a2s.  c om
        curatorClient.create().creatingParentsIfNeeded()
                .withACL(singletonList(new ACL(permission.getPerms(), new Id("sasl", username))))
                .forPath(pathOps.makePath(znodePath));
    } catch (KeeperException.NodeExistsException e) {
        LOGGER.info("Caught: '" + e.getMessage() + "' while creating node. Ensure ACLs are correct:", e);
        addUserToAcl(znodePath, username, permission);
    }
}

From source file:org.trustedanalytics.auth.gateway.zookeeper.client.KerberoslessZookeeperClient.java

License:Apache License

@Override
public void createZnode(String znodePath, String username, ZookeeperPermission permission) throws Exception {

    try {/*from   w  w  w.j a  v  a  2 s . c  o m*/
        curatorClient.create().creatingParentsIfNeeded().forPath(pathOps.makePath(znodePath));
    } catch (KeeperException.NodeExistsException e) {
        LOGGER.info("Caught: '" + e.getMessage() + "' while creating node. Nothing to do.", e);
    }
}