public List<String> getChildren(String path) throws KeeperException, InterruptedException { RetryCounter retryCounter = retryCounterFactory.create(); while (true) { try { return zk.getChildren(path, true); } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case OPERATIONTIMEOUT: LOG.warn("Possibly transient ZooKeeper exception: " + e); if (!retryCounter.shouldRetry()) { LOG.error( "ZooKeeper getChildren failed after " + retryCounter.getMaxRetries() + " retries"); throw e; } break; default: throw e; } } retryCounter.sleepUntilNextRetry(); retryCounter.useRetry(); } }
@Override public void run() { if (isSynchronizedWithZooKeeper.get() || !zkClient.isConnected() || !started.get()) { return; } if (checkVersion.getAndSet(false)) { try { synchronized (lastStatusVersionMonitor) { final Stat stat = zkClient.getZookeeper().exists(path, null); if (stat != null && zkClient.getZookeeper().getSessionId() == stat.getEphemeralOwner()) { zkClient.getZookeeper().delete(path, lastStatusVersion); } } } catch (InterruptedException e) { LOG.info("Interrupted"); checkVersion.set(true); } catch (KeeperException e) { LOG.info("exception " + e.getMessage()); checkVersion.set(true); } } LOG.info( "We are out-of-sync, have a zookeeper connection, and are started, trying reclaim: " + path + this); tryClaim(); }
public static String obtainUpgradingLock(ZooKeeper zk, String name) { String created = null; try { if (null == name) { created = zk.create("/upgrading_lock", new byte[] {}, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } else { created = zk.create( "/" + name + "/upgrading_lock", new byte[] {}, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } if (created == null) { System.out.println("Failed to obtain upgrading lock."); return null; } return created; }
/** * Place the host:port advertisement for the Monitor's Log4j listener in ZooKeeper * * @param conf configuration for the instance * @param instanceId instanceId for the instance * @param hostAddress Address that monitor process is bound to */ public static void startLogListener( AccumuloConfiguration conf, String instanceId, String hostAddress) { try { SocketServer server = new SocketServer(conf.getPort(Property.MONITOR_LOG4J_PORT)); // getLocalPort will return the actual ephemeral port used when '0' was provided. String logForwardingAddr = hostAddress + ":" + server.getLocalPort(); log.debug("Setting monitor log4j log-forwarding address to: " + logForwardingAddr); final String path = ZooUtil.getRoot(instanceId) + Constants.ZMONITOR_LOG4J_ADDR; final ZooReaderWriter zoo = ZooReaderWriter.getInstance(); // Delete before we try to re-create in case the previous session hasn't yet expired try { zoo.delete(path, -1); } catch (KeeperException e) { // We don't care if the node is already gone if (!KeeperException.Code.NONODE.equals(e.code())) { throw e; } } zoo.putEphemeralData(path, logForwardingAddr.getBytes(UTF_8)); new Daemon(server).start(); } catch (Throwable t) { log.info("Unable to start/advertise Log4j listener for log-forwarding to monitor", t); } }
@Override public void initializeSecurity(TCredentials credentials, String principal, byte[] token) throws AccumuloSecurityException { try { // remove old settings from zookeeper first, if any IZooReaderWriter zoo = ZooReaderWriter.getInstance(); synchronized (zooCache) { zooCache.clear(); if (zoo.exists(ZKUserPath)) { zoo.recursiveDelete(ZKUserPath, NodeMissingPolicy.SKIP); log.info("Removed " + ZKUserPath + "/" + " from zookeeper"); } // prep parent node of users with root username zoo.putPersistentData(ZKUserPath, principal.getBytes(UTF_8), NodeExistsPolicy.FAIL); constructUser(principal, ZKSecurityTool.createPass(token)); } } catch (KeeperException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } catch (InterruptedException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } catch (AccumuloException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } }
/** * Removes the lock or associated znode if you no longer require the lock. this also removes your * request in the queue for locking in case you do not already hold the lock. * * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper. */ public synchronized void unlock() throws RuntimeException { if (!isClosed() && id != null) { // we don't need to retry this operation in the case of failure // as ZK will remove ephemeral files and we don't wanna hang // this process when closing if we cannot reconnect to ZK try { ZooKeeperOperation zopdel = new ZooKeeperOperation() { public boolean execute() throws KeeperException, InterruptedException { zookeeper.delete(id, -1); return Boolean.TRUE; } }; zopdel.execute(); } catch (InterruptedException e) { LOG.warn("Caught: " + e, e); // set that we have been interrupted. Thread.currentThread().interrupt(); } catch (KeeperException.NoNodeException e) { // do nothing } catch (KeeperException e) { LOG.warn("Caught: " + e, e); throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); } finally { if (callback != null) { callback.lockReleased(); } id = null; } } }
@Override public void changePassword(String principal, AuthenticationToken token) throws AccumuloSecurityException { if (!(token instanceof PasswordToken)) throw new AccumuloSecurityException(principal, SecurityErrorCode.INVALID_TOKEN); PasswordToken pt = (PasswordToken) token; if (userExists(principal)) { try { synchronized (zooCache) { zooCache.clear(ZKUserPath + "/" + principal); ZooReaderWriter.getInstance() .putPrivatePersistentData( ZKUserPath + "/" + principal, ZKSecurityTool.createPass(pt.getPassword()), NodeExistsPolicy.OVERWRITE); } } catch (KeeperException e) { log.error("{}", e.getMessage(), 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); } } else throw new AccumuloSecurityException( principal, SecurityErrorCode.USER_DOESNT_EXIST); // user doesn't exist }
/** * Utility - return true if the given exception is retry-able * * @param exception exception to check * @return true/false */ public static boolean isRetryException(Throwable exception) { if (exception instanceof KeeperException) { KeeperException keeperException = (KeeperException) exception; return shouldRetry(keeperException.code().intValue()); } return false; }
private String createSequential(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException { RetryCounter retryCounter = retryCounterFactory.create(); boolean first = true; String newPath = path + this.identifier; while (true) { try { if (!first) { // Check if we succeeded on a previous attempt String previousResult = findPreviousSequentialNode(newPath); if (previousResult != null) { return previousResult; } } first = false; return zk.create(newPath, data, acl, createMode); } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case SESSIONEXPIRED: case OPERATIONTIMEOUT: retryOrThrow(retryCounter, e, "create"); break; default: throw e; } } retryCounter.sleepUntilNextRetry(); retryCounter.useRetry(); } }
/** * Creates the serialized value of the object and stores this in ZooKeeper under the path. It * updates the lastStatusVersion. It does not set a watcher for the path. */ private void updateCoordinateData() throws CoordinateMissingException, CloudnameException { if (!started.get()) { throw new IllegalStateException("Not started."); } if (!zkClient.isConnected()) { throw new CloudnameException("No proper connection with zookeeper."); } synchronized (lastStatusVersionMonitor) { try { Stat stat = zkClient .getZookeeper() .setData( path, zkCoordinateData.snapshot().serialize().getBytes(Util.CHARSET_NAME), lastStatusVersion); LOG.fine("Updated coordinate, latest version is " + stat.getVersion()); lastStatusVersion = stat.getVersion(); } catch (KeeperException.NoNodeException e) { throw new CoordinateMissingException("Coordinate does not exist " + path); } catch (KeeperException e) { throw new CloudnameException( "ZooKeeper errror in updateCoordinateData: " + e.getMessage(), e); } catch (UnsupportedEncodingException e) { throw new CloudnameException(e); } catch (InterruptedException e) { throw new CloudnameException(e); } catch (IOException e) { throw new CloudnameException(e); } } }
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException { RetryCounter retryCounter = retryCounterFactory.create(); while (true) { try { // 120227 by DaeJin Choi - What about Metadata of data ( ex: Magic // number and so on ) return zk.getData(path, watcher, stat); } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case OPERATIONTIMEOUT: LOG.warn("Possibly transient ZooKeeper exception: " + e); if (!retryCounter.shouldRetry()) { LOG.error( "ZooKeeper getData failed after " + retryCounter.getMaxRetries() + " retries"); throw e; } break; default: throw e; } } retryCounter.sleepUntilNextRetry(); retryCounter.useRetry(); } }
/** * Barrier constructor * * @param address * @param root * @param size */ Barrier(String address, String root, int size) { super(address); this.input = input; this.size = size; // Create barrier node if (zk != null) { try { Stat s = zk.exists(root, false); if (s == null) { zk.create(input, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (KeeperException e) { System.out.println("Keeper exception when instantiating queue: " + e.toString()); } catch (InterruptedException e) { System.out.println("Interrupted exception"); } } // My node name try { name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString()); } catch (UnknownHostException e) { System.out.println(e.toString()); } }
public String createEphemeralSequential(final String path, final byte[] data) throws Exception { RetryCounter retryCounter = retryCounterFactory.create(); while (true) { try { return zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case OPERATIONTIMEOUT: LOG.warn("Possibly transient ZooKeeper exception: " + e); if (!retryCounter.shouldRetry()) { LOG.error( "ZooKeeper create failed after " + retryCounter.getMaxRetries() + " retries"); throw e; } break; default: throw e; } } retryCounter.sleepUntilNextRetry(); retryCounter.useRetry(); } }
/** * Lista os {@link DataNode}s conectados no momento ao ClusterService. * * @return datanodes * @throws KeeperException * @throws InterruptedException * @throws IOException */ public static List<NodeStatus> getDatanodesConnected() { List<NodeStatus> datanodes = new ArrayList<NodeStatus>(); try { List<String> nodesIds = zk.getChildren(ZkConf.DATANODES_PATH, true); LOG.info("Cluster com " + nodesIds.size() + " datanode(s) ativo(s) no momento."); for (String hostName : nodesIds) { String path = ZkConf.DATANODES_PATH + "/" + hostName; byte[] bytes = zk.getData(path, true, null); NodeStatus node = (NodeStatus) Serializer.toObject(bytes); datanodes.add(node); } } catch (ClassNotFoundException e) { LOG.error(e.getMessage(), e); } catch (KeeperException e) { LOG.error(e.getMessage(), e); } catch (InterruptedException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } return datanodes; }
public void createPersistent(final String path, final byte[] data) throws KeeperException, InterruptedException { RetryCounter retryCounter = retryCounterFactory.create(); while (true) { try { zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return; } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case OPERATIONTIMEOUT: LOG.warn("Possibly transient ZooKeeper exception: " + e); if (!retryCounter.shouldRetry()) { LOG.error( "ZooKeeper create failed after " + retryCounter.getMaxRetries() + " retries"); throw e; } break; default: throw e; } } retryCounter.sleepUntilNextRetry(); retryCounter.useRetry(); } }
@Test public void testStormZkRoot() { String stormRoot = "/storm-zk-root"; List<String> children = null; try { children = zkClient.get().getChildren(stormRoot, null); } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (String string : children) { System.out.println(string); } }
public void displayConfig() { try { String value = store.read(ConfigUpdater.CONFIG, this); System.out.printf("取出值 %s from %s\n", ConfigUpdater.CONFIG, value); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
void setWatcher() { try { Stat s = zk.exists("/root", true); if (s != null) { zk.getData("/root", false, s); } } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
/** {@inheritDoc} */ @Override public boolean remove(Object o) { try { return this.removeUnsynchronized(o); } catch (InterruptedException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } catch (KeeperException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } catch (IOException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } }
public static synchronized void managerDatanodesCreated(String hostName, NodeStatus nodeLocal) { try { if (nodeLocal.getNodesResponding().contains(hostName)) { String path = ZkConf.DATANODES_PATH + "/" + HOSTNAME; nodeLocal.getNodesResponding().remove(nodeLocal.getNodesResponding().indexOf(hostName)); zk.setData(path, Serializer.fromObject(nodeLocal), -1); } if (HOSTNAME.equals(hostName)) { loadDada(); if (managerDatanodesResponding.containsKey(hostName)) { managerDatanodesResponding.remove(hostName); zk.setData( ZkConf.MANAGER_NODES_RESPONDING, Serializer.fromObject((Serializable) managerDatanodesResponding), -1); } if (datanodesDesconnected.contains(hostName)) { datanodesDesconnected.remove(hostName); if (historicSendDatanodesDesconnected.containsKey(hostName)) { historicSendDatanodesDesconnected.remove(hostName); } zk.setData( ZkConf.DATANODES_DESCONNECTED, Serializer.fromObject((Serializable) datanodesDesconnected), -1); zk.setData( ZkConf.HISTORIC_SEND, Serializer.fromObject((Serializable) historicSendDatanodesDesconnected), -1); } if (datanodesConnected.size() > 1) { new SupportReplyText().checkRepliesText(datanodesReplyReceive); new SupportReplyImage().checkRepliesImage(datanodesReplyReceive); } clear(); } } catch (KeeperException e) { LOG.error(e.getMessage(), e); } catch (InterruptedException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } }
@Result(success = "/WEB-INF/pages/group/viewAdd.jsp", fail = "/WEB-INF/pages/group/viewAdd.jsp") @Override public String add(HttpServletRequest req, HttpServletResponse resp) { String groupname = param(req, "groupname"); groupname = groupname.toUpperCase(); Group model = new Group(); model.setGroupname(groupname); setAttr(req, MODEL, model); if (StringUtils.isBlank(groupname)) { setAttr(req, TIP_NAME_KEY, "请输入分组名"); return FAIL; } // 屏蔽原始节点名[zookeeper][serverCluster] if (StringUtils.equals(groupname, ZOOKEEPER_KEY.toUpperCase()) || StringUtils.equals(groupname, SERVER_CLUSTER_KEY.toUpperCase())) { setAttr(req, TIP_NAME_KEY, "对不起,不能创建内置分组名"); model.setGroupname(""); return FAIL; } // 业务 if (model.isExist(groupname)) { setAttr(req, TIP_NAME_KEY, "对不起,分组名[" + groupname + "]已被使用,请换个分组名后重试!"); model.setGroupname(""); return FAIL; } // 添加组服务zookeeper节点 ZooKeeper zk = (ZooKeeper) context.getAttribute(ZOOKEEPER_KEY); Stat groupExist; try { String groupNode = "/" + groupname; groupExist = zk.exists(groupNode, new AutoDataWatcher(zk)); if (groupExist == null) { log.debug("创建分组节点:" + groupNode); zk.create(groupNode, new byte[] {}, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { log.debug("已存在分组节点" + groupNode + ",不能创建..."); } } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } // 保存到数据库 if (model.save() > 0) { setAttr(req, TIP_NAME_KEY, "恭喜您,成功添加新分组: [" + groupname + "]"); model.setGroupname(""); return SUCCESS; } else { setAttr(req, TIP_NAME_KEY, "系统故障,请稍候重试"); return FAIL; } }
SelfWatch(String address) { try { zk = new ZooKeeper(address, 600000, this); // 在创建ZooKeeper时第三个参数负责设置该类的默认构造函数 zk.create("/root", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } catch (IOException e) { e.printStackTrace(); zk = null; } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
protected KeeperException.Code createFromByte(String path, byte[] data, CreateMode mode) { try { zooKeeper.create(path, data, acl, mode); } catch (KeeperException e) { return e.code(); } catch (Exception e) { return KeeperException.Code.SYSTEMERROR; } return KeeperException.Code.OK; }
public static boolean createIfNotExists( ZooKeeperConnection zooKeeper, String path, byte[] value, CreateMode createMode) throws KeeperException, InterruptedException { if (zooKeeper.exists(path, false) == null) { try { zooKeeper.create(path, value, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode); } catch (KeeperException e) { if (e.code() != KeeperException.Code.NODEEXISTS) throw e; return false; } return true; } return false; }
/** * {@inheritDoc} NB: Nulls cannot be represented by this collection. Attempting to add one will * cause an {@link IllegalArgumentException} to be thrown. */ @Override public boolean add(T o) { if (o == null) throw new IllegalArgumentException("nulls not allowed"); try { return this.addUnsynchronized(o); } catch (KeeperException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } catch (InterruptedException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } catch (IOException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } }
/** {@inheritDoc} */ @Override public void clear() { synchronized (this.elements) { try { for (String s : this.keeper.getChildren(this.znode, this.watcher)) this.keeper.delete(this.znode + '/' + s, -1); this.synchronize(); } catch (KeeperException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } catch (InterruptedException e) { throw new RuntimeException(e.getClass().getSimpleName() + " caught", e); } } }
public boolean deletePath(String path, ZookeeperConnection zkConn) throws IOException { if (zkConn != null) { try { zkConn.createZookeeper().delete(path, -1); zkConn.close(); } catch (InterruptedException e) { e.printStackTrace(); return false; } catch (KeeperException e) { e.printStackTrace(); return false; } } return true; }
private List<ReplicationPeer> listValidReplicationPeers() { Map<String, ReplicationPeerConfig> peers = listPeerConfigs(); if (peers == null || peers.size() <= 0) { return null; } List<ReplicationPeer> validPeers = new ArrayList<ReplicationPeer>(peers.size()); for (Entry<String, ReplicationPeerConfig> peerEntry : peers.entrySet()) { String peerId = peerEntry.getKey(); String clusterKey = peerEntry.getValue().getClusterKey(); Configuration peerConf = new Configuration(this.connection.getConfiguration()); Stat s = null; try { ZKUtil.applyClusterKeyToConf(peerConf, clusterKey); Pair<ReplicationPeerConfig, Configuration> pair = this.replicationPeers.getPeerConf(peerId); ReplicationPeer peer = new ReplicationPeerZKImpl(peerConf, peerId, pair.getFirst()); s = zkw.getRecoverableZooKeeper() .exists(peerConf.get(HConstants.ZOOKEEPER_ZNODE_PARENT), null); if (null == s) { LOG.info(peerId + ' ' + clusterKey + " is invalid now."); continue; } validPeers.add(peer); } catch (ReplicationException e) { LOG.warn( "Failed to get valid replication peers. " + "Error connecting to peer cluster with peerId=" + peerId); LOG.debug("Failure details to get valid replication peers.", e); continue; } catch (KeeperException e) { LOG.warn( "Failed to get valid replication peers. KeeperException code=" + e.code().intValue()); LOG.debug("Failure details to get valid replication peers.", e); continue; } catch (InterruptedException e) { LOG.warn("Failed to get valid replication peers due to InterruptedException."); LOG.debug("Failure details to get valid replication peers.", e); Thread.currentThread().interrupt(); continue; } catch (IOException e) { LOG.warn("Failed to get valid replication peers due to IOException."); LOG.debug("Failure details to get valid replication peers.", e); continue; } } return validPeers; }
/** * Constructor * * @param connectString * @param root */ FIFOQueue(String connectString, String root) throws IOException { super(connectString); this.root = root; if (zk != null) { try { Stat s = zk.exists(root, false); if (s == null) { zk.create(root, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } catch (KeeperException e) { System.out.println("NO:" + e.getMessage()); } catch (InterruptedException e) { System.out.println("NO:" + e.getMessage()); } } }
private void enode_test_1() throws IOException, InterruptedException, KeeperException { checkRoot(); String parentName = testDirOnZK; String nodeName = parentName + "/enode_abc"; ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); Stat stat = zk.exists(parentName, false); if (stat == null) { try { zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException ke) { fail("Creating node " + parentName + ke.getMessage()); } } try { zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } catch (KeeperException ke) { int code = ke.getCode(); boolean valid = code == KeeperException.Code.NodeExists; if (!valid) { fail("Unexpected exception code for createin: " + ke.getMessage()); } } stat = zk.exists(nodeName, false); if (stat == null) { fail("node " + nodeName + " should exist"); } System.out.println("Closing client with sessionid: 0x" + Long.toHexString(zk.getSessionId())); zk.close(); zk = new ZooKeeper(hostPort, 10000, this); for (int i = 0; i < 10; i++) { System.out.println("i = " + i); stat = zk.exists(nodeName, false); if (stat != null) { System.out.println("node " + nodeName + " should not exist after reconnection close"); } else { System.out.println("node " + nodeName + " is gone after reconnection close!"); break; } Thread.sleep(5000); } deleteZKDir(zk, nodeName); zk.close(); }