private String ensureNodeExists(ZooKeeper zooKeeper, String path, CreateMode createMode) { try { LOGGER.debug("attempting to create node at {}", path); final ArrayList<ACL> acls = ZooDefs.Ids.OPEN_ACL_UNSAFE; final String createNodePath = zooKeeper.create(path, new byte[0], acls, createMode); LOGGER.info("created node at {}, acls: {}, createMode: {}", createNodePath, acls, createMode); return createNodePath; } catch (KeeperException e) { if (KeeperException.Code.NONODE.equals(e.code())) { final List<String> pathTokens = Splitter.on('/').omitEmptyStrings().trimResults().splitToList(path); final String parentPath = "/" + Joiner.on('/').skipNulls().join(pathTokens.subList(0, pathTokens.size() - 1)); LOGGER.debug("node doesn't exist, recursively attempting to create node at {}", parentPath); ensureNodeExists(zooKeeper, parentPath, CreateMode.PERSISTENT); LOGGER.debug("recursively created node at {}", parentPath); LOGGER.debug("retrying attempt to create node at {}", path); return ensureNodeExists(zooKeeper, path, createMode); } else if (KeeperException.Code.NODEEXISTS.equals(e.code())) { return path; } else { throw new RuntimeException(String.format("unable to create node at path %s, ZooKeeper returned %s", path, e.code()), e); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(String.format("unable to create node at path %s", path), e); } }
} catch (KeeperException e) { if (!isFirstTime) { if (e.code().equals(KeeperException.Code.NONODE)) { LOG.warn("NONODE failed to assert ownership for " + task, e); } else if (e.code().equals(KeeperException.Code.BADVERSION)) { LOG.warn("BADVERSION failed to assert ownership for " + task, e); } else {
private void createIfDoesntExist(String path, byte[] data, boolean force, ZooKeeper zooKeeper) throws InterruptedException, KeeperException { try { zooKeeper.create(path, data, defaultAcl(), CreateMode.PERSISTENT); } catch (KeeperException ke) { //Explicit Overwrite if (KeeperException.Code.NODEEXISTS.equals(ke.code())) { if (force) { zooKeeper.delete(path, -1); zooKeeper.create(path, data, defaultAcl(), CreateMode.PERSISTENT); } } else { throw ke; } } }
public boolean deleteNode(NodePath path, boolean canHaveChildren) throws KeeperException, InterruptedException { try { zk.delete(path.toString(), -1); return true; } catch (KeeperException e) { if (e.code().equals(Code.NOTEMPTY) && canHaveChildren) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Some other client created children nodes in the node" + path + " concurrently. Therefore, we can not delete it."); } return false; } else { throw e; } } }
public boolean deleteNode(NodePath path, boolean canHaveChildren) throws KeeperException, InterruptedException { try { zk.delete(path.toString(), -1); return true; } catch (KeeperException e) { if (e.code().equals(Code.NOTEMPTY) && canHaveChildren) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Some other client created children nodes in the node" + path + " concurrently. Therefore, we can not delete it."); } return false; } else { throw e; } } }
private void create(String path, String value, String encoding) { byte[] data = value.getBytes(UTF_8); if ("base64".equals(encoding)) data = Base64.decodeBase64(data); try { try { zk.putPersistentData(path, data, overwrite ? NodeExistsPolicy.OVERWRITE : NodeExistsPolicy.FAIL); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NODEEXISTS)) throw new RuntimeException(path + " exists. Remove it first."); throw e; } } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public void dropUser(String user) throws AccumuloSecurityException { try { synchronized (zooCache) { IZooReaderWriter zoo = ZooReaderWriter.getRetryingInstance(); zoo.recursiveDelete(ZKUserPath + "/" + user + ZKUserAuths, NodeMissingPolicy.SKIP); zooCache.clear(ZKUserPath + "/" + user); } } catch (InterruptedException e) { log.error(e, e); throw new RuntimeException(e); } catch (KeeperException e) { log.error(e, e); if (e.code().equals(KeeperException.Code.NONODE)) throw new AccumuloSecurityException(user, SecurityErrorCode.USER_DOESNT_EXIST, e); throw new AccumuloSecurityException(user, SecurityErrorCode.CONNECTION_ERROR, e); } }
@Override public void dropUser(String user) throws AccumuloSecurityException { try { synchronized (zooCache) { zooCache.clear(); ZooReaderWriter.getRetryingInstance().recursiveDelete(ZKUserPath + "/" + user, NodeMissingPolicy.FAIL); } } catch (InterruptedException e) { log.error(e, e); throw new RuntimeException(e); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NONODE)) throw new AccumuloSecurityException(user, SecurityErrorCode.USER_DOESNT_EXIST, e); log.error(e, e); throw new AccumuloSecurityException(user, SecurityErrorCode.CONNECTION_ERROR, e); } }
private void create(String path, String value, String encoding) { byte[] data = value.getBytes(UTF_8); if ("base64".equals(encoding)) data = Base64.decodeBase64(data); try { try { zk.putPersistentData(path, data, overwrite ? NodeExistsPolicy.OVERWRITE : NodeExistsPolicy.FAIL); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NODEEXISTS)) throw new RuntimeException(path + " exists. Remove it first."); throw e; } } catch (Exception e) { throw new RuntimeException(e); } } }
public boolean failedDueTo(Code... codes) { if (exception instanceof KeeperException) { for (Code code : codes) { if (code.equals(((KeeperException)exception).code())) { return true; } } } return false; } }
if (policy.equals(NodeMissingPolicy.SKIP) && e.code().equals(KeeperException.Code.NONODE)) return; throw e;
} catch (KeeperException e) { if (!KeeperException.Code.NONODE.equals(e.code())) { throw e;
/** * Initialize the queuedWork set with the work already sent out */ @Override protected void initializeQueuedWork() { if (queuedWork != null) { return; } queuedWork = new HashSet<>(); while (true) { try { queuedWork.addAll(workQueue.getWorkQueued()); return; } catch (KeeperException e) { if (KeeperException.Code.NONODE.equals(e.code())) { log.warn("Could not find ZK root for replication work queue, will retry", e); sleepUninterruptibly(500, TimeUnit.MILLISECONDS); continue; } log.error("Error reading existing queued replication work from ZooKeeper", e); throw new RuntimeException("Error reading existing queued replication work from ZooKeeper", e); } catch (InterruptedException e) { log.error("Error reading existing queued replication work from ZooKeeper", e); throw new RuntimeException("Error reading existing queued replication work from ZooKeeper", e); } } }
@Override public void cleanUser(String user) throws AccumuloSecurityException { try { synchronized (zooCache) { zoo.recursiveDelete(ZKUserPath + "/" + user + ZKUserSysPerms, NodeMissingPolicy.SKIP); zoo.recursiveDelete(ZKUserPath + "/" + user + ZKUserTablePerms, NodeMissingPolicy.SKIP); zoo.recursiveDelete(ZKUserPath + "/" + user + ZKUserNamespacePerms, NodeMissingPolicy.SKIP); zooCache.clear(ZKUserPath + "/" + user); } } catch (InterruptedException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } catch (KeeperException e) { log.error("{}", e.getMessage(), e); if (e.code().equals(KeeperException.Code.NONODE)) throw new AccumuloSecurityException(user, SecurityErrorCode.USER_DOESNT_EXIST, e); throw new AccumuloSecurityException(user, SecurityErrorCode.CONNECTION_ERROR, e); } }
private void create(String path, String value, String encoding) { byte[] data = value.getBytes(UTF_8); if ("base64".equals(encoding)) data = Base64.getDecoder().decode(data); try { try { zk.putPersistentData(path, data, overwrite ? NodeExistsPolicy.OVERWRITE : NodeExistsPolicy.FAIL); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NODEEXISTS)) throw new RuntimeException(path + " exists. Remove it first."); throw e; } } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public void createUser(String principal, AuthenticationToken token) throws AccumuloSecurityException { try { if (!(token instanceof PasswordToken)) throw new AccumuloSecurityException(principal, SecurityErrorCode.INVALID_TOKEN); PasswordToken pt = (PasswordToken) token; constructUser(principal, ZKSecurityTool.createPass(pt.getPassword())); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NODEEXISTS)) throw new AccumuloSecurityException(principal, SecurityErrorCode.USER_EXISTS, e); throw new AccumuloSecurityException(principal, SecurityErrorCode.CONNECTION_ERROR, e); } catch (InterruptedException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } catch (AccumuloException e) { log.error("{}", e.getMessage(), e); throw new AccumuloSecurityException(principal, SecurityErrorCode.DEFAULT_SECURITY_ERROR, e); } }
@Override public synchronized void createUser(String principal, AuthenticationToken token) throws AccumuloSecurityException { if (!(token instanceof KerberosToken)) { throw new UnsupportedOperationException( "Expected a KerberosToken but got a " + token.getClass().getSimpleName()); } try { createUserNodeInZk(Base64.getEncoder().encodeToString(principal.getBytes(UTF_8))); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NODEEXISTS)) { throw new AccumuloSecurityException(principal, SecurityErrorCode.USER_EXISTS, e); } log.error("Failed to create user in ZooKeeper", e); throw new AccumuloSecurityException(principal, SecurityErrorCode.CONNECTION_ERROR, e); } catch (InterruptedException e) { log.error("Interrupted trying to create node for user", e); throw new RuntimeException(e); } }
@Override public void dropUser(String user) throws AccumuloSecurityException { try { synchronized (zooCache) { zooCache.clear(); context.getZooReaderWriter().recursiveDelete(ZKUserPath + "/" + user, NodeMissingPolicy.FAIL); } } catch (InterruptedException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NONODE)) { throw new AccumuloSecurityException(user, SecurityErrorCode.USER_DOESNT_EXIST, e); } log.error("{}", e.getMessage(), e); throw new AccumuloSecurityException(user, SecurityErrorCode.CONNECTION_ERROR, e); } }
@Override public void dropUser(String user) throws AccumuloSecurityException { try { synchronized (zooCache) { IZooReaderWriter zoo = context.getZooReaderWriter(); zoo.recursiveDelete(ZKUserPath + "/" + user + ZKUserAuths, NodeMissingPolicy.SKIP); zooCache.clear(ZKUserPath + "/" + user); } } catch (InterruptedException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } catch (KeeperException e) { log.error("{}", e.getMessage(), e); if (e.code().equals(KeeperException.Code.NONODE)) throw new AccumuloSecurityException(user, SecurityErrorCode.USER_DOESNT_EXIST, e); throw new AccumuloSecurityException(user, SecurityErrorCode.CONNECTION_ERROR, e); } }
private void cleanupHbckZnode() { try { if (zkw != null && hbckZodeCreated) { ZKUtil.deleteNode(zkw, hbckEphemeralNodePath); hbckZodeCreated = false; } } catch (KeeperException e) { // Ignore if (!e.code().equals(KeeperException.Code.NONODE)) { LOG.warn("Delete HBCK znode " + hbckEphemeralNodePath + " failed ", e); } } }