View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.zookeeper;
20  
21  import java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.InputStreamReader;
24  import java.io.PrintWriter;
25  import java.net.InetSocketAddress;
26  import java.net.Socket;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Deque;
30  import java.util.HashMap;
31  import java.util.LinkedList;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Properties;
35  
36  import javax.security.auth.login.AppConfigurationEntry;
37  import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
38  
39  import org.apache.commons.lang.StringUtils;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  import org.apache.hadoop.conf.Configuration;
43  import org.apache.hadoop.hbase.HBaseConfiguration;
44  import org.apache.hadoop.hbase.HConstants;
45  import org.apache.hadoop.hbase.ServerName;
46  import org.apache.hadoop.hbase.classification.InterfaceAudience;
47  import org.apache.hadoop.hbase.exceptions.DeserializationException;
48  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
49  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
50  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.RegionStoreSequenceIds;
51  import org.apache.hadoop.hbase.util.ByteStringer;
52  import org.apache.hadoop.hbase.util.Bytes;
53  import org.apache.hadoop.hbase.util.Threads;
54  import org.apache.hadoop.hbase.zookeeper.ZKUtil.ZKUtilOp.CreateAndFailSilent;
55  import org.apache.hadoop.hbase.zookeeper.ZKUtil.ZKUtilOp.DeleteNodeFailSilent;
56  import org.apache.hadoop.hbase.zookeeper.ZKUtil.ZKUtilOp.SetData;
57  import org.apache.hadoop.security.SecurityUtil;
58  import org.apache.hadoop.security.authentication.util.KerberosUtil;
59  import org.apache.zookeeper.AsyncCallback;
60  import org.apache.zookeeper.CreateMode;
61  import org.apache.zookeeper.KeeperException;
62  import org.apache.zookeeper.KeeperException.NoNodeException;
63  import org.apache.zookeeper.Op;
64  import org.apache.zookeeper.Watcher;
65  import org.apache.zookeeper.ZooDefs.Ids;
66  import org.apache.zookeeper.ZooDefs.Perms;
67  import org.apache.zookeeper.ZooKeeper;
68  import org.apache.zookeeper.client.ZooKeeperSaslClient;
69  import org.apache.zookeeper.data.ACL;
70  import org.apache.zookeeper.data.Id;
71  import org.apache.zookeeper.data.Stat;
72  import org.apache.zookeeper.proto.CreateRequest;
73  import org.apache.zookeeper.proto.DeleteRequest;
74  import org.apache.zookeeper.proto.SetDataRequest;
75  import org.apache.zookeeper.server.ZooKeeperSaslServer;
76  
77  import com.google.protobuf.InvalidProtocolBufferException;
78  
79  /**
80   * Internal HBase utility class for ZooKeeper.
81   *
82   * <p>Contains only static methods and constants.
83   *
84   * <p>Methods all throw {@link KeeperException} if there is an unexpected
85   * zookeeper exception, so callers of these methods must handle appropriately.
86   * If ZK is required for the operation, the server will need to be aborted.
87   */
88  @InterfaceAudience.Private
89  public class ZKUtil {
90    private static final Log LOG = LogFactory.getLog(ZKUtil.class);
91  
92    // TODO: Replace this with ZooKeeper constant when ZOOKEEPER-277 is resolved.
93    public static final char ZNODE_PATH_SEPARATOR = '/';
94    private static int zkDumpConnectionTimeOut;
95  
96    /**
97     * Creates a new connection to ZooKeeper, pulling settings and ensemble config
98     * from the specified configuration object using methods from {@link ZKConfig}.
99     *
100    * Sets the connection status monitoring watcher to the specified watcher.
101    *
102    * @param conf configuration to pull ensemble and other settings from
103    * @param watcher watcher to monitor connection changes
104    * @return connection to zookeeper
105    * @throws IOException if unable to connect to zk or config problem
106    */
107   public static RecoverableZooKeeper connect(Configuration conf, Watcher watcher)
108   throws IOException {
109     Properties properties = ZKConfig.makeZKProps(conf);
110     String ensemble = ZKConfig.getZKQuorumServersString(properties);
111     return connect(conf, ensemble, watcher);
112   }
113 
114   public static RecoverableZooKeeper connect(Configuration conf, String ensemble,
115       Watcher watcher)
116   throws IOException {
117     return connect(conf, ensemble, watcher, null);
118   }
119 
120   public static RecoverableZooKeeper connect(Configuration conf, String ensemble,
121       Watcher watcher, final String identifier)
122   throws IOException {
123     if(ensemble == null) {
124       throw new IOException("Unable to determine ZooKeeper ensemble");
125     }
126     int timeout = conf.getInt(HConstants.ZK_SESSION_TIMEOUT,
127         HConstants.DEFAULT_ZK_SESSION_TIMEOUT);
128     if (LOG.isTraceEnabled()) {
129       LOG.trace(identifier + " opening connection to ZooKeeper ensemble=" + ensemble);
130     }
131     int retry = conf.getInt("zookeeper.recovery.retry", 3);
132     int retryIntervalMillis =
133       conf.getInt("zookeeper.recovery.retry.intervalmill", 1000);
134     zkDumpConnectionTimeOut = conf.getInt("zookeeper.dump.connection.timeout",
135         1000);
136     return new RecoverableZooKeeper(ensemble, timeout, watcher,
137         retry, retryIntervalMillis, identifier);
138   }
139 
140   /**
141    * Log in the current zookeeper server process using the given configuration
142    * keys for the credential file and login principal.
143    *
144    * <p><strong>This is only applicable when running on secure hbase</strong>
145    * On regular HBase (without security features), this will safely be ignored.
146    * </p>
147    *
148    * @param conf The configuration data to use
149    * @param keytabFileKey Property key used to configure the path to the credential file
150    * @param userNameKey Property key used to configure the login principal
151    * @param hostname Current hostname to use in any credentials
152    * @throws IOException underlying exception from SecurityUtil.login() call
153    */
154   public static void loginServer(Configuration conf, String keytabFileKey,
155       String userNameKey, String hostname) throws IOException {
156     login(conf, keytabFileKey, userNameKey, hostname,
157           ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY,
158           JaasConfiguration.SERVER_KEYTAB_KERBEROS_CONFIG_NAME);
159   }
160 
161   /**
162    * Log in the current zookeeper client using the given configuration
163    * keys for the credential file and login principal.
164    *
165    * <p><strong>This is only applicable when running on secure hbase</strong>
166    * On regular HBase (without security features), this will safely be ignored.
167    * </p>
168    *
169    * @param conf The configuration data to use
170    * @param keytabFileKey Property key used to configure the path to the credential file
171    * @param userNameKey Property key used to configure the login principal
172    * @param hostname Current hostname to use in any credentials
173    * @throws IOException underlying exception from SecurityUtil.login() call
174    */
175   public static void loginClient(Configuration conf, String keytabFileKey,
176       String userNameKey, String hostname) throws IOException {
177     login(conf, keytabFileKey, userNameKey, hostname,
178           ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY,
179           JaasConfiguration.CLIENT_KEYTAB_KERBEROS_CONFIG_NAME);
180   }
181 
182   /**
183    * Log in the current process using the given configuration keys for the
184    * credential file and login principal.
185    *
186    * <p><strong>This is only applicable when running on secure hbase</strong>
187    * On regular HBase (without security features), this will safely be ignored.
188    * </p>
189    *
190    * @param conf The configuration data to use
191    * @param keytabFileKey Property key used to configure the path to the credential file
192    * @param userNameKey Property key used to configure the login principal
193    * @param hostname Current hostname to use in any credentials
194    * @param loginContextProperty property name to expose the entry name
195    * @param loginContextName jaas entry name
196    * @throws IOException underlying exception from SecurityUtil.login() call
197    */
198   private static void login(Configuration conf, String keytabFileKey,
199       String userNameKey, String hostname,
200       String loginContextProperty, String loginContextName)
201       throws IOException {
202     if (!isSecureZooKeeper(conf))
203       return;
204 
205     // User has specified a jaas.conf, keep this one as the good one.
206     // HBASE_OPTS="-Djava.security.auth.login.config=jaas.conf"
207     if (System.getProperty("java.security.auth.login.config") != null)
208       return;
209 
210     // No keytab specified, no auth
211     String keytabFilename = conf.get(keytabFileKey);
212     if (keytabFilename == null) {
213       LOG.warn("no keytab specified for: " + keytabFileKey);
214       return;
215     }
216 
217     String principalConfig = conf.get(userNameKey, System.getProperty("user.name"));
218     String principalName = SecurityUtil.getServerPrincipal(principalConfig, hostname);
219 
220     // Initialize the "jaas.conf" for keyTab/principal,
221     // If keyTab is not specified use the Ticket Cache.
222     // and set the zookeeper login context name.
223     JaasConfiguration jaasConf = new JaasConfiguration(loginContextName,
224         principalName, keytabFilename);
225     javax.security.auth.login.Configuration.setConfiguration(jaasConf);
226     System.setProperty(loginContextProperty, loginContextName);
227   }
228 
229   /**
230    * A JAAS configuration that defines the login modules that we want to use for login.
231    */
232   private static class JaasConfiguration extends javax.security.auth.login.Configuration {
233     private static final String SERVER_KEYTAB_KERBEROS_CONFIG_NAME =
234       "zookeeper-server-keytab-kerberos";
235     private static final String CLIENT_KEYTAB_KERBEROS_CONFIG_NAME =
236       "zookeeper-client-keytab-kerberos";
237 
238     private static final Map<String, String> BASIC_JAAS_OPTIONS =
239       new HashMap<String,String>();
240     static {
241       String jaasEnvVar = System.getenv("HBASE_JAAS_DEBUG");
242       if (jaasEnvVar != null && "true".equalsIgnoreCase(jaasEnvVar)) {
243         BASIC_JAAS_OPTIONS.put("debug", "true");
244       }
245     }
246 
247     private static final Map<String,String> KEYTAB_KERBEROS_OPTIONS =
248       new HashMap<String,String>();
249     static {
250       KEYTAB_KERBEROS_OPTIONS.put("doNotPrompt", "true");
251       KEYTAB_KERBEROS_OPTIONS.put("storeKey", "true");
252       KEYTAB_KERBEROS_OPTIONS.put("refreshKrb5Config", "true");
253       KEYTAB_KERBEROS_OPTIONS.putAll(BASIC_JAAS_OPTIONS);
254     }
255 
256     private static final AppConfigurationEntry KEYTAB_KERBEROS_LOGIN =
257       new AppConfigurationEntry(KerberosUtil.getKrb5LoginModuleName(),
258                                 LoginModuleControlFlag.REQUIRED,
259                                 KEYTAB_KERBEROS_OPTIONS);
260 
261     private static final AppConfigurationEntry[] KEYTAB_KERBEROS_CONF =
262       new AppConfigurationEntry[]{KEYTAB_KERBEROS_LOGIN};
263 
264     private javax.security.auth.login.Configuration baseConfig;
265     private final String loginContextName;
266     private final boolean useTicketCache;
267     private final String keytabFile;
268     private final String principal;
269 
270     public JaasConfiguration(String loginContextName, String principal, String keytabFile) {
271       this(loginContextName, principal, keytabFile, keytabFile == null || keytabFile.length() == 0);
272     }
273 
274     private JaasConfiguration(String loginContextName, String principal,
275                              String keytabFile, boolean useTicketCache) {
276       try {
277         this.baseConfig = javax.security.auth.login.Configuration.getConfiguration();
278       } catch (SecurityException e) {
279         this.baseConfig = null;
280       }
281       this.loginContextName = loginContextName;
282       this.useTicketCache = useTicketCache;
283       this.keytabFile = keytabFile;
284       this.principal = principal;
285       LOG.info("JaasConfiguration loginContextName=" + loginContextName +
286                " principal=" + principal + " useTicketCache=" + useTicketCache +
287                " keytabFile=" + keytabFile);
288     }
289 
290     @Override
291     public AppConfigurationEntry[] getAppConfigurationEntry(String appName) {
292       if (loginContextName.equals(appName)) {
293         if (!useTicketCache) {
294           KEYTAB_KERBEROS_OPTIONS.put("keyTab", keytabFile);
295           KEYTAB_KERBEROS_OPTIONS.put("useKeyTab", "true");
296         }
297         KEYTAB_KERBEROS_OPTIONS.put("principal", principal);
298         KEYTAB_KERBEROS_OPTIONS.put("useTicketCache", useTicketCache ? "true" : "false");
299         return KEYTAB_KERBEROS_CONF;
300       }
301       if (baseConfig != null) return baseConfig.getAppConfigurationEntry(appName);
302       return(null);
303     }
304   }
305 
306   //
307   // Helper methods
308   //
309 
310   /**
311    * Join the prefix znode name with the suffix znode name to generate a proper
312    * full znode name.
313    *
314    * Assumes prefix does not end with slash and suffix does not begin with it.
315    *
316    * @param prefix beginning of znode name
317    * @param suffix ending of znode name
318    * @return result of properly joining prefix with suffix
319    */
320   public static String joinZNode(String prefix, String suffix) {
321     return prefix + ZNODE_PATH_SEPARATOR + suffix;
322   }
323 
324   /**
325    * Returns the full path of the immediate parent of the specified node.
326    * @param node path to get parent of
327    * @return parent of path, null if passed the root node or an invalid node
328    */
329   public static String getParent(String node) {
330     int idx = node.lastIndexOf(ZNODE_PATH_SEPARATOR);
331     return idx <= 0 ? null : node.substring(0, idx);
332   }
333 
334   /**
335    * Get the name of the current node from the specified fully-qualified path.
336    * @param path fully-qualified path
337    * @return name of the current node
338    */
339   public static String getNodeName(String path) {
340     return path.substring(path.lastIndexOf("/")+1);
341   }
342 
343   /**
344    * Get the key to the ZK ensemble for this configuration without
345    * adding a name at the end
346    * @param conf Configuration to use to build the key
347    * @return ensemble key without a name
348    */
349   public static String getZooKeeperClusterKey(Configuration conf) {
350     return getZooKeeperClusterKey(conf, null);
351   }
352 
353   /**
354    * Get the key to the ZK ensemble for this configuration and append
355    * a name at the end
356    * @param conf Configuration to use to build the key
357    * @param name Name that should be appended at the end if not empty or null
358    * @return ensemble key with a name (if any)
359    */
360   public static String getZooKeeperClusterKey(Configuration conf, String name) {
361     String ensemble = conf.get(HConstants.ZOOKEEPER_QUORUM.replaceAll(
362         "[\\t\\n\\x0B\\f\\r]", ""));
363     StringBuilder builder = new StringBuilder(ensemble);
364     builder.append(":");
365     builder.append(conf.get(HConstants.ZOOKEEPER_CLIENT_PORT));
366     builder.append(":");
367     builder.append(conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
368     if (name != null && !name.isEmpty()) {
369       builder.append(",");
370       builder.append(name);
371     }
372     return builder.toString();
373   }
374 
375   /**
376    * Apply the settings in the given key to the given configuration, this is
377    * used to communicate with distant clusters
378    * @param conf configuration object to configure
379    * @param key string that contains the 3 required configuratins
380    * @throws IOException
381    */
382   public static void applyClusterKeyToConf(Configuration conf, String key)
383       throws IOException{
384     String[] parts = transformClusterKey(key);
385     conf.set(HConstants.ZOOKEEPER_QUORUM, parts[0]);
386     conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, parts[1]);
387     conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, parts[2]);
388   }
389 
390   /**
391    * Separate the given key into the three configurations it should contain:
392    * hbase.zookeeper.quorum, hbase.zookeeper.client.port
393    * and zookeeper.znode.parent
394    * @param key
395    * @return the three configuration in the described order
396    * @throws IOException
397    */
398   public static String[] transformClusterKey(String key) throws IOException {
399     String[] parts = key.split(":");
400     if (parts.length != 3) {
401       throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +
402           HConstants.ZOOKEEPER_QUORUM + ":hbase.zookeeper.client.port:"
403           + HConstants.ZOOKEEPER_ZNODE_PARENT);
404     }
405     return parts;
406   }
407 
408   //
409   // Existence checks and watches
410   //
411 
412   /**
413    * Watch the specified znode for delete/create/change events.  The watcher is
414    * set whether or not the node exists.  If the node already exists, the method
415    * returns true.  If the node does not exist, the method returns false.
416    *
417    * @param zkw zk reference
418    * @param znode path of node to watch
419    * @return true if znode exists, false if does not exist or error
420    * @throws KeeperException if unexpected zookeeper exception
421    */
422   public static boolean watchAndCheckExists(ZooKeeperWatcher zkw, String znode)
423   throws KeeperException {
424     try {
425       Stat s = zkw.getRecoverableZooKeeper().exists(znode, zkw);
426       boolean exists = s != null ? true : false;
427       if (exists) {
428         LOG.debug(zkw.prefix("Set watcher on existing znode=" + znode));
429       } else {
430         LOG.debug(zkw.prefix("Set watcher on znode that does not yet exist, " + znode));
431       }
432       return exists;
433     } catch (KeeperException e) {
434       LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e);
435       zkw.keeperException(e);
436       return false;
437     } catch (InterruptedException e) {
438       LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e);
439       zkw.interruptedException(e);
440       return false;
441     }
442   }
443 
444   /**
445    * Watch the specified znode, but only if exists. Useful when watching
446    * for deletions. Uses .getData() (and handles NoNodeException) instead
447    * of .exists() to accomplish this, as .getData() will only set a watch if
448    * the znode exists.
449    * @param zkw zk reference
450    * @param znode path of node to watch
451    * @return true if the watch is set, false if node does not exists
452    * @throws KeeperException if unexpected zookeeper exception
453    */
454   public static boolean setWatchIfNodeExists(ZooKeeperWatcher zkw, String znode)
455       throws KeeperException {
456     try {
457       zkw.getRecoverableZooKeeper().getData(znode, true, null);
458       return true;
459     } catch (NoNodeException e) {
460       return false;
461     } catch (InterruptedException e) {
462       LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e);
463       zkw.interruptedException(e);
464       return false;
465     }
466   }
467 
468   /**
469    * Check if the specified node exists.  Sets no watches.
470    *
471    * @param zkw zk reference
472    * @param znode path of node to watch
473    * @return version of the node if it exists, -1 if does not exist
474    * @throws KeeperException if unexpected zookeeper exception
475    */
476   public static int checkExists(ZooKeeperWatcher zkw, String znode)
477   throws KeeperException {
478     try {
479       Stat s = zkw.getRecoverableZooKeeper().exists(znode, null);
480       return s != null ? s.getVersion() : -1;
481     } catch (KeeperException e) {
482       LOG.warn(zkw.prefix("Unable to set watcher on znode (" + znode + ")"), e);
483       zkw.keeperException(e);
484       return -1;
485     } catch (InterruptedException e) {
486       LOG.warn(zkw.prefix("Unable to set watcher on znode (" + znode + ")"), e);
487       zkw.interruptedException(e);
488       return -1;
489     }
490   }
491 
492   //
493   // Znode listings
494   //
495 
496   /**
497    * Lists the children znodes of the specified znode.  Also sets a watch on
498    * the specified znode which will capture a NodeDeleted event on the specified
499    * znode as well as NodeChildrenChanged if any children of the specified znode
500    * are created or deleted.
501    *
502    * Returns null if the specified node does not exist.  Otherwise returns a
503    * list of children of the specified node.  If the node exists but it has no
504    * children, an empty list will be returned.
505    *
506    * @param zkw zk reference
507    * @param znode path of node to list and watch children of
508    * @return list of children of the specified node, an empty list if the node
509    *          exists but has no children, and null if the node does not exist
510    * @throws KeeperException if unexpected zookeeper exception
511    */
512   public static List<String> listChildrenAndWatchForNewChildren(
513       ZooKeeperWatcher zkw, String znode)
514   throws KeeperException {
515     try {
516       List<String> children = zkw.getRecoverableZooKeeper().getChildren(znode, zkw);
517       return children;
518     } catch(KeeperException.NoNodeException ke) {
519       LOG.debug(zkw.prefix("Unable to list children of znode " + znode + " " +
520           "because node does not exist (not an error)"));
521       return null;
522     } catch (KeeperException e) {
523       LOG.warn(zkw.prefix("Unable to list children of znode " + znode + " "), e);
524       zkw.keeperException(e);
525       return null;
526     } catch (InterruptedException e) {
527       LOG.warn(zkw.prefix("Unable to list children of znode " + znode + " "), e);
528       zkw.interruptedException(e);
529       return null;
530     }
531   }
532 
533   /**
534    * List all the children of the specified znode, setting a watch for children
535    * changes and also setting a watch on every individual child in order to get
536    * the NodeCreated and NodeDeleted events.
537    * @param zkw zookeeper reference
538    * @param znode node to get children of and watch
539    * @return list of znode names, null if the node doesn't exist
540    * @throws KeeperException
541    */
542   public static List<String> listChildrenAndWatchThem(ZooKeeperWatcher zkw,
543       String znode) throws KeeperException {
544     List<String> children = listChildrenAndWatchForNewChildren(zkw, znode);
545     if (children == null) {
546       return null;
547     }
548     for (String child : children) {
549       watchAndCheckExists(zkw, joinZNode(znode, child));
550     }
551     return children;
552   }
553 
554   /**
555    * Lists the children of the specified znode without setting any watches.
556    *
557    * Sets no watches at all, this method is best effort.
558    *
559    * Returns an empty list if the node has no children.  Returns null if the
560    * parent node itself does not exist.
561    *
562    * @param zkw zookeeper reference
563    * @param znode node to get children
564    * @return list of data of children of specified znode, empty if no children,
565    *         null if parent does not exist
566    * @throws KeeperException if unexpected zookeeper exception
567    */
568   public static List<String> listChildrenNoWatch(ZooKeeperWatcher zkw, String znode)
569   throws KeeperException {
570     List<String> children = null;
571     try {
572       // List the children without watching
573       children = zkw.getRecoverableZooKeeper().getChildren(znode, null);
574     } catch(KeeperException.NoNodeException nne) {
575       return null;
576     } catch(InterruptedException ie) {
577       zkw.interruptedException(ie);
578     }
579     return children;
580   }
581 
582   /**
583    * Simple class to hold a node path and node data.
584    * @deprecated Unused
585    */
586   @Deprecated
587   public static class NodeAndData {
588     private String node;
589     private byte [] data;
590     public NodeAndData(String node, byte [] data) {
591       this.node = node;
592       this.data = data;
593     }
594     public String getNode() {
595       return node;
596     }
597     public byte [] getData() {
598       return data;
599     }
600     @Override
601     public String toString() {
602       return node;
603     }
604     public boolean isEmpty() {
605       return (data.length == 0);
606     }
607   }
608 
609   /**
610    * Checks if the specified znode has any children.  Sets no watches.
611    *
612    * Returns true if the node exists and has children.  Returns false if the
613    * node does not exist or if the node does not have any children.
614    *
615    * Used during master initialization to determine if the master is a
616    * failed-over-to master or the first master during initial cluster startup.
617    * If the directory for regionserver ephemeral nodes is empty then this is
618    * a cluster startup, if not then it is not cluster startup.
619    *
620    * @param zkw zk reference
621    * @param znode path of node to check for children of
622    * @return true if node has children, false if not or node does not exist
623    * @throws KeeperException if unexpected zookeeper exception
624    */
625   public static boolean nodeHasChildren(ZooKeeperWatcher zkw, String znode)
626   throws KeeperException {
627     try {
628       return !zkw.getRecoverableZooKeeper().getChildren(znode, null).isEmpty();
629     } catch(KeeperException.NoNodeException ke) {
630       LOG.debug(zkw.prefix("Unable to list children of znode " + znode + " " +
631       "because node does not exist (not an error)"));
632       return false;
633     } catch (KeeperException e) {
634       LOG.warn(zkw.prefix("Unable to list children of znode " + znode), e);
635       zkw.keeperException(e);
636       return false;
637     } catch (InterruptedException e) {
638       LOG.warn(zkw.prefix("Unable to list children of znode " + znode), e);
639       zkw.interruptedException(e);
640       return false;
641     }
642   }
643 
644   /**
645    * Get the number of children of the specified node.
646    *
647    * If the node does not exist or has no children, returns 0.
648    *
649    * Sets no watches at all.
650    *
651    * @param zkw zk reference
652    * @param znode path of node to count children of
653    * @return number of children of specified node, 0 if none or parent does not
654    *         exist
655    * @throws KeeperException if unexpected zookeeper exception
656    */
657   public static int getNumberOfChildren(ZooKeeperWatcher zkw, String znode)
658   throws KeeperException {
659     try {
660       Stat stat = zkw.getRecoverableZooKeeper().exists(znode, null);
661       return stat == null ? 0 : stat.getNumChildren();
662     } catch(KeeperException e) {
663       LOG.warn(zkw.prefix("Unable to get children of node " + znode));
664       zkw.keeperException(e);
665     } catch(InterruptedException e) {
666       zkw.interruptedException(e);
667     }
668     return 0;
669   }
670 
671   //
672   // Data retrieval
673   //
674 
675   /**
676    * Get znode data. Does not set a watcher.
677    * @return ZNode data, null if the node does not exist or if there is an
678    *  error.
679    */
680   public static byte [] getData(ZooKeeperWatcher zkw, String znode)
681       throws KeeperException, InterruptedException {
682     try {
683       byte [] data = zkw.getRecoverableZooKeeper().getData(znode, null, null);
684       logRetrievedMsg(zkw, znode, data, false);
685       return data;
686     } catch (KeeperException.NoNodeException e) {
687       LOG.debug(zkw.prefix("Unable to get data of znode " + znode + " " +
688           "because node does not exist (not an error)"));
689       return null;
690     } catch (KeeperException e) {
691       LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e);
692       zkw.keeperException(e);
693       return null;
694     }
695   }
696 
697   /**
698    * Get the data at the specified znode and set a watch.
699    *
700    * Returns the data and sets a watch if the node exists.  Returns null and no
701    * watch is set if the node does not exist or there is an exception.
702    *
703    * @param zkw zk reference
704    * @param znode path of node
705    * @return data of the specified znode, or null
706    * @throws KeeperException if unexpected zookeeper exception
707    */
708   public static byte [] getDataAndWatch(ZooKeeperWatcher zkw, String znode)
709   throws KeeperException {
710     return getDataInternal(zkw, znode, null, true);
711   }
712 
713   /**
714    * Get the data at the specified znode and set a watch.
715    *
716    * Returns the data and sets a watch if the node exists.  Returns null and no
717    * watch is set if the node does not exist or there is an exception.
718    *
719    * @param zkw zk reference
720    * @param znode path of node
721    * @param stat object to populate the version of the znode
722    * @return data of the specified znode, or null
723    * @throws KeeperException if unexpected zookeeper exception
724    */
725   public static byte[] getDataAndWatch(ZooKeeperWatcher zkw, String znode,
726       Stat stat) throws KeeperException {
727     return getDataInternal(zkw, znode, stat, true);
728   }
729 
730   private static byte[] getDataInternal(ZooKeeperWatcher zkw, String znode, Stat stat,
731       boolean watcherSet)
732       throws KeeperException {
733     try {
734       byte [] data = zkw.getRecoverableZooKeeper().getData(znode, zkw, stat);
735       logRetrievedMsg(zkw, znode, data, watcherSet);
736       return data;
737     } catch (KeeperException.NoNodeException e) {
738       // This log can get pretty annoying when we cycle on 100ms waits.
739       // Enable trace if you really want to see it.
740       LOG.trace(zkw.prefix("Unable to get data of znode " + znode + " " +
741         "because node does not exist (not an error)"));
742       return null;
743     } catch (KeeperException e) {
744       LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e);
745       zkw.keeperException(e);
746       return null;
747     } catch (InterruptedException e) {
748       LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e);
749       zkw.interruptedException(e);
750       return null;
751     }
752   }
753 
754   /**
755    * Get the data at the specified znode without setting a watch.
756    *
757    * Returns the data if the node exists.  Returns null if the node does not
758    * exist.
759    *
760    * Sets the stats of the node in the passed Stat object.  Pass a null stat if
761    * not interested.
762    *
763    * @param zkw zk reference
764    * @param znode path of node
765    * @param stat node status to get if node exists
766    * @return data of the specified znode, or null if node does not exist
767    * @throws KeeperException if unexpected zookeeper exception
768    */
769   public static byte [] getDataNoWatch(ZooKeeperWatcher zkw, String znode,
770       Stat stat)
771   throws KeeperException {
772     try {
773       byte [] data = zkw.getRecoverableZooKeeper().getData(znode, null, stat);
774       logRetrievedMsg(zkw, znode, data, false);
775       return data;
776     } catch (KeeperException.NoNodeException e) {
777       LOG.debug(zkw.prefix("Unable to get data of znode " + znode + " " +
778           "because node does not exist (not necessarily an error)"));
779       return null;
780     } catch (KeeperException e) {
781       LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e);
782       zkw.keeperException(e);
783       return null;
784     } catch (InterruptedException e) {
785       LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e);
786       zkw.interruptedException(e);
787       return null;
788     }
789   }
790 
791   /**
792    * Returns the date of child znodes of the specified znode.  Also sets a watch on
793    * the specified znode which will capture a NodeDeleted event on the specified
794    * znode as well as NodeChildrenChanged if any children of the specified znode
795    * are created or deleted.
796    *
797    * Returns null if the specified node does not exist.  Otherwise returns a
798    * list of children of the specified node.  If the node exists but it has no
799    * children, an empty list will be returned.
800    *
801    * @param zkw zk reference
802    * @param baseNode path of node to list and watch children of
803    * @return list of data of children of the specified node, an empty list if the node
804    *          exists but has no children, and null if the node does not exist
805    * @throws KeeperException if unexpected zookeeper exception
806    * @deprecated Unused
807    */
808   @Deprecated
809   public static List<NodeAndData> getChildDataAndWatchForNewChildren(
810       ZooKeeperWatcher zkw, String baseNode) throws KeeperException {
811     List<String> nodes =
812       ZKUtil.listChildrenAndWatchForNewChildren(zkw, baseNode);
813     if (nodes != null) {
814       List<NodeAndData> newNodes = new ArrayList<NodeAndData>();
815       for (String node : nodes) {
816         String nodePath = ZKUtil.joinZNode(baseNode, node);
817         byte[] data = ZKUtil.getDataAndWatch(zkw, nodePath);
818         newNodes.add(new NodeAndData(nodePath, data));
819       }
820       return newNodes;
821     }
822     return null;
823   }
824 
825   /**
826    * Update the data of an existing node with the expected version to have the
827    * specified data.
828    *
829    * Throws an exception if there is a version mismatch or some other problem.
830    *
831    * Sets no watches under any conditions.
832    *
833    * @param zkw zk reference
834    * @param znode
835    * @param data
836    * @param expectedVersion
837    * @throws KeeperException if unexpected zookeeper exception
838    * @throws KeeperException.BadVersionException if version mismatch
839    * @deprecated Unused
840    */
841   @Deprecated
842   public static void updateExistingNodeData(ZooKeeperWatcher zkw, String znode,
843       byte [] data, int expectedVersion)
844   throws KeeperException {
845     try {
846       zkw.getRecoverableZooKeeper().setData(znode, data, expectedVersion);
847     } catch(InterruptedException ie) {
848       zkw.interruptedException(ie);
849     }
850   }
851 
852   //
853   // Data setting
854   //
855 
856   /**
857    * Sets the data of the existing znode to be the specified data.  Ensures that
858    * the current data has the specified expected version.
859    *
860    * <p>If the node does not exist, a {@link NoNodeException} will be thrown.
861    *
862    * <p>If their is a version mismatch, method returns null.
863    *
864    * <p>No watches are set but setting data will trigger other watchers of this
865    * node.
866    *
867    * <p>If there is another problem, a KeeperException will be thrown.
868    *
869    * @param zkw zk reference
870    * @param znode path of node
871    * @param data data to set for node
872    * @param expectedVersion version expected when setting data
873    * @return true if data set, false if version mismatch
874    * @throws KeeperException if unexpected zookeeper exception
875    */
876   public static boolean setData(ZooKeeperWatcher zkw, String znode,
877       byte [] data, int expectedVersion)
878   throws KeeperException, KeeperException.NoNodeException {
879     try {
880       return zkw.getRecoverableZooKeeper().setData(znode, data, expectedVersion) != null;
881     } catch (InterruptedException e) {
882       zkw.interruptedException(e);
883       return false;
884     }
885   }
886 
887   /**
888    * Set data into node creating node if it doesn't yet exist.
889    * Does not set watch.
890    *
891    * @param zkw zk reference
892    * @param znode path of node
893    * @param data data to set for node
894    * @throws KeeperException
895    */
896   public static void createSetData(final ZooKeeperWatcher zkw, final String znode,
897       final byte [] data)
898   throws KeeperException {
899     if (checkExists(zkw, znode) == -1) {
900       ZKUtil.createWithParents(zkw, znode, data);
901     } else {
902       ZKUtil.setData(zkw, znode, data);
903     }
904   }
905 
906   /**
907    * Sets the data of the existing znode to be the specified data.  The node
908    * must exist but no checks are done on the existing data or version.
909    *
910    * <p>If the node does not exist, a {@link NoNodeException} will be thrown.
911    *
912    * <p>No watches are set but setting data will trigger other watchers of this
913    * node.
914    *
915    * <p>If there is another problem, a KeeperException will be thrown.
916    *
917    * @param zkw zk reference
918    * @param znode path of node
919    * @param data data to set for node
920    * @throws KeeperException if unexpected zookeeper exception
921    */
922   public static void setData(ZooKeeperWatcher zkw, String znode, byte [] data)
923   throws KeeperException, KeeperException.NoNodeException {
924     setData(zkw, (SetData)ZKUtilOp.setData(znode, data));
925   }
926 
927   private static void setData(ZooKeeperWatcher zkw, SetData setData)
928   throws KeeperException, KeeperException.NoNodeException {
929     SetDataRequest sd = (SetDataRequest)toZooKeeperOp(zkw, setData).toRequestRecord();
930     setData(zkw, sd.getPath(), sd.getData(), sd.getVersion());
931   }
932 
933   /**
934    * Returns whether or not secure authentication is enabled
935    * (whether <code>hbase.security.authentication</code> is set to
936    * <code>kerberos</code>.
937    */
938   public static boolean isSecureZooKeeper(Configuration conf) {
939     // Detection for embedded HBase client with jaas configuration
940     // defined for third party programs.
941     try {
942       javax.security.auth.login.Configuration testConfig = javax.security.auth.login.Configuration.getConfiguration();
943       if(testConfig.getAppConfigurationEntry("Client") == null) {
944         return false;
945       }
946     } catch(Exception e) {
947       // No Jaas configuration defined.
948       return false;
949     }
950 
951     // Master & RSs uses hbase.zookeeper.client.*
952     return("kerberos".equalsIgnoreCase(conf.get("hbase.security.authentication")) &&
953          conf.get("hbase.zookeeper.client.keytab.file") != null);
954   }
955 
956   private static ArrayList<ACL> createACL(ZooKeeperWatcher zkw, String node) {
957     if (!node.startsWith(zkw.baseZNode)) {
958       return Ids.OPEN_ACL_UNSAFE;
959     }
960     if (isSecureZooKeeper(zkw.getConfiguration())) {
961       String superUser = zkw.getConfiguration().get("hbase.superuser");
962       ArrayList<ACL> acls = new ArrayList<ACL>();
963       // add permission to hbase supper user
964       if (superUser != null) {
965         acls.add(new ACL(Perms.ALL, new Id("auth", superUser)));
966       }
967       // Certain znodes are accessed directly by the client,
968       // so they must be readable by non-authenticated clients
969       if ((node.equals(zkw.baseZNode) == true) ||
970           (zkw.isAnyMetaReplicaZnode(node)) ||
971           (node.equals(zkw.getMasterAddressZNode()) == true) ||
972           (node.equals(zkw.clusterIdZNode) == true) ||
973           (node.equals(zkw.rsZNode) == true) ||
974           (node.equals(zkw.backupMasterAddressesZNode) == true) ||
975           (node.startsWith(zkw.tableZNode) == true)) {
976         acls.addAll(Ids.CREATOR_ALL_ACL);
977         acls.addAll(Ids.READ_ACL_UNSAFE);
978       } else {
979         acls.addAll(Ids.CREATOR_ALL_ACL);
980       }
981       return acls;
982     } else {
983       return Ids.OPEN_ACL_UNSAFE;
984     }
985   }
986 
987   //
988   // Node creation
989   //
990 
991   /**
992    *
993    * Set the specified znode to be an ephemeral node carrying the specified
994    * data.
995    *
996    * If the node is created successfully, a watcher is also set on the node.
997    *
998    * If the node is not created successfully because it already exists, this
999    * method will also set a watcher on the node.
1000    *
1001    * If there is another problem, a KeeperException will be thrown.
1002    *
1003    * @param zkw zk reference
1004    * @param znode path of node
1005    * @param data data of node
1006    * @return true if node created, false if not, watch set in both cases
1007    * @throws KeeperException if unexpected zookeeper exception
1008    */
1009   public static boolean createEphemeralNodeAndWatch(ZooKeeperWatcher zkw,
1010       String znode, byte [] data)
1011   throws KeeperException {
1012     boolean ret = true;
1013     try {
1014       zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode),
1015           CreateMode.EPHEMERAL);
1016     } catch (KeeperException.NodeExistsException nee) {
1017       ret = false;
1018     } catch (InterruptedException e) {
1019       LOG.info("Interrupted", e);
1020       Thread.currentThread().interrupt();
1021     }
1022     if(!watchAndCheckExists(zkw, znode)) {
1023       // It did exist but now it doesn't, try again
1024       return createEphemeralNodeAndWatch(zkw, znode, data);
1025     }
1026     return ret;
1027   }
1028 
1029   /**
1030    * Creates the specified znode to be a persistent node carrying the specified
1031    * data.
1032    *
1033    * Returns true if the node was successfully created, false if the node
1034    * already existed.
1035    *
1036    * If the node is created successfully, a watcher is also set on the node.
1037    *
1038    * If the node is not created successfully because it already exists, this
1039    * method will also set a watcher on the node but return false.
1040    *
1041    * If there is another problem, a KeeperException will be thrown.
1042    *
1043    * @param zkw zk reference
1044    * @param znode path of node
1045    * @param data data of node
1046    * @return true if node created, false if not, watch set in both cases
1047    * @throws KeeperException if unexpected zookeeper exception
1048    */
1049   public static boolean createNodeIfNotExistsAndWatch(
1050       ZooKeeperWatcher zkw, String znode, byte [] data)
1051   throws KeeperException {
1052     boolean ret = true;
1053     try {
1054       zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode),
1055           CreateMode.PERSISTENT);
1056     } catch (KeeperException.NodeExistsException nee) {
1057       ret = false;
1058     } catch (InterruptedException e) {
1059       zkw.interruptedException(e);
1060       return false;
1061     }
1062     try {
1063       zkw.getRecoverableZooKeeper().exists(znode, zkw);
1064     } catch (InterruptedException e) {
1065       zkw.interruptedException(e);
1066       return false;
1067     }
1068     return ret;
1069   }
1070 
1071   /**
1072    * Creates the specified znode with the specified data but does not watch it.
1073    *
1074    * Returns the znode of the newly created node
1075    *
1076    * If there is another problem, a KeeperException will be thrown.
1077    *
1078    * @param zkw zk reference
1079    * @param znode path of node
1080    * @param data data of node
1081    * @param createMode specifying whether the node to be created is ephemeral and/or sequential
1082    * @return true name of the newly created znode or null
1083    * @throws KeeperException if unexpected zookeeper exception
1084    */
1085   public static String createNodeIfNotExistsNoWatch(ZooKeeperWatcher zkw, String znode,
1086       byte[] data, CreateMode createMode) throws KeeperException {
1087 
1088     String createdZNode = null;
1089     try {
1090       createdZNode = zkw.getRecoverableZooKeeper().create(znode, data,
1091           createACL(zkw, znode), createMode);
1092     } catch (KeeperException.NodeExistsException nee) {
1093       return znode;
1094     } catch (InterruptedException e) {
1095       zkw.interruptedException(e);
1096       return null;
1097     }
1098     return createdZNode;
1099   }
1100 
1101   /**
1102    * Creates the specified node with the specified data and watches it.
1103    *
1104    * <p>Throws an exception if the node already exists.
1105    *
1106    * <p>The node created is persistent and open access.
1107    *
1108    * <p>Returns the version number of the created node if successful.
1109    *
1110    * @param zkw zk reference
1111    * @param znode path of node to create
1112    * @param data data of node to create
1113    * @return version of node created
1114    * @throws KeeperException if unexpected zookeeper exception
1115    * @throws KeeperException.NodeExistsException if node already exists
1116    */
1117   public static int createAndWatch(ZooKeeperWatcher zkw,
1118       String znode, byte [] data)
1119   throws KeeperException, KeeperException.NodeExistsException {
1120     try {
1121       zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode),
1122           CreateMode.PERSISTENT);
1123       Stat stat = zkw.getRecoverableZooKeeper().exists(znode, zkw);
1124       if (stat == null){
1125         // Likely a race condition. Someone deleted the znode.
1126         throw KeeperException.create(KeeperException.Code.SYSTEMERROR,
1127             "ZK.exists returned null (i.e.: znode does not exist) for znode=" + znode);
1128       }
1129      return stat.getVersion();
1130     } catch (InterruptedException e) {
1131       zkw.interruptedException(e);
1132       return -1;
1133     }
1134   }
1135 
1136   /**
1137    * Async creates the specified node with the specified data.
1138    *
1139    * <p>Throws an exception if the node already exists.
1140    *
1141    * <p>The node created is persistent and open access.
1142    *
1143    * @param zkw zk reference
1144    * @param znode path of node to create
1145    * @param data data of node to create
1146    * @param cb
1147    * @param ctx
1148    * @throws KeeperException if unexpected zookeeper exception
1149    * @throws KeeperException.NodeExistsException if node already exists
1150    */
1151   public static void asyncCreate(ZooKeeperWatcher zkw,
1152       String znode, byte [] data, final AsyncCallback.StringCallback cb,
1153       final Object ctx) {
1154     zkw.getRecoverableZooKeeper().getZooKeeper().create(znode, data,
1155         createACL(zkw, znode), CreateMode.PERSISTENT, cb, ctx);
1156   }
1157 
1158   /**
1159    * Creates the specified node, iff the node does not exist.  Does not set a
1160    * watch and fails silently if the node already exists.
1161    *
1162    * The node created is persistent and open access.
1163    *
1164    * @param zkw zk reference
1165    * @param znode path of node
1166    * @throws KeeperException if unexpected zookeeper exception
1167    */
1168   public static void createAndFailSilent(ZooKeeperWatcher zkw,
1169       String znode) throws KeeperException {
1170     createAndFailSilent(zkw, znode, new byte[0]);
1171   }
1172 
1173   /**
1174    * Creates the specified node containing specified data, iff the node does not exist.  Does
1175    * not set a watch and fails silently if the node already exists.
1176    *
1177    * The node created is persistent and open access.
1178    *
1179    * @param zkw zk reference
1180    * @param znode path of node
1181    * @param data a byte array data to store in the znode
1182    * @throws KeeperException if unexpected zookeeper exception
1183    */
1184   public static void createAndFailSilent(ZooKeeperWatcher zkw,
1185       String znode, byte[] data)
1186   throws KeeperException {
1187     createAndFailSilent(zkw,
1188         (CreateAndFailSilent)ZKUtilOp.createAndFailSilent(znode, data));
1189   }
1190 
1191   private static void createAndFailSilent(ZooKeeperWatcher zkw, CreateAndFailSilent cafs)
1192   throws KeeperException {
1193     CreateRequest create = (CreateRequest)toZooKeeperOp(zkw, cafs).toRequestRecord();
1194     String znode = create.getPath();
1195     try {
1196       RecoverableZooKeeper zk = zkw.getRecoverableZooKeeper();
1197       if (zk.exists(znode, false) == null) {
1198         zk.create(znode, create.getData(), create.getAcl(), CreateMode.fromFlag(create.getFlags()));
1199       }
1200     } catch(KeeperException.NodeExistsException nee) {
1201     } catch(KeeperException.NoAuthException nee){
1202       try {
1203         if (null == zkw.getRecoverableZooKeeper().exists(znode, false)) {
1204           // If we failed to create the file and it does not already exist.
1205           throw(nee);
1206         }
1207       } catch (InterruptedException ie) {
1208         zkw.interruptedException(ie);
1209       }
1210 
1211     } catch(InterruptedException ie) {
1212       zkw.interruptedException(ie);
1213     }
1214   }
1215 
1216   /**
1217    * Creates the specified node and all parent nodes required for it to exist.
1218    *
1219    * No watches are set and no errors are thrown if the node already exists.
1220    *
1221    * The nodes created are persistent and open access.
1222    *
1223    * @param zkw zk reference
1224    * @param znode path of node
1225    * @throws KeeperException if unexpected zookeeper exception
1226    */
1227   public static void createWithParents(ZooKeeperWatcher zkw, String znode)
1228   throws KeeperException {
1229     createWithParents(zkw, znode, new byte[0]);
1230   }
1231 
1232   /**
1233    * Creates the specified node and all parent nodes required for it to exist.  The creation of
1234    * parent znodes is not atomic with the leafe znode creation but the data is written atomically
1235    * when the leaf node is created.
1236    *
1237    * No watches are set and no errors are thrown if the node already exists.
1238    *
1239    * The nodes created are persistent and open access.
1240    *
1241    * @param zkw zk reference
1242    * @param znode path of node
1243    * @throws KeeperException if unexpected zookeeper exception
1244    */
1245   public static void createWithParents(ZooKeeperWatcher zkw, String znode, byte[] data)
1246   throws KeeperException {
1247     try {
1248       if(znode == null) {
1249         return;
1250       }
1251       zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode),
1252           CreateMode.PERSISTENT);
1253     } catch(KeeperException.NodeExistsException nee) {
1254       return;
1255     } catch(KeeperException.NoNodeException nne) {
1256       createWithParents(zkw, getParent(znode));
1257       createWithParents(zkw, znode, data);
1258     } catch(InterruptedException ie) {
1259       zkw.interruptedException(ie);
1260     }
1261   }
1262 
1263   //
1264   // Deletes
1265   //
1266 
1267   /**
1268    * Delete the specified node.  Sets no watches.  Throws all exceptions.
1269    */
1270   public static void deleteNode(ZooKeeperWatcher zkw, String node)
1271   throws KeeperException {
1272     deleteNode(zkw, node, -1);
1273   }
1274 
1275   /**
1276    * Delete the specified node with the specified version.  Sets no watches.
1277    * Throws all exceptions.
1278    */
1279   public static boolean deleteNode(ZooKeeperWatcher zkw, String node,
1280       int version)
1281   throws KeeperException {
1282     try {
1283       zkw.getRecoverableZooKeeper().delete(node, version);
1284       return true;
1285     } catch(KeeperException.BadVersionException bve) {
1286       return false;
1287     } catch(InterruptedException ie) {
1288       zkw.interruptedException(ie);
1289       return false;
1290     }
1291   }
1292 
1293   /**
1294    * Deletes the specified node.  Fails silent if the node does not exist.
1295    * @param zkw
1296    * @param node
1297    * @throws KeeperException
1298    */
1299   public static void deleteNodeFailSilent(ZooKeeperWatcher zkw, String node)
1300   throws KeeperException {
1301     deleteNodeFailSilent(zkw,
1302       (DeleteNodeFailSilent)ZKUtilOp.deleteNodeFailSilent(node));
1303   }
1304 
1305   private static void deleteNodeFailSilent(ZooKeeperWatcher zkw,
1306       DeleteNodeFailSilent dnfs) throws KeeperException {
1307     DeleteRequest delete = (DeleteRequest)toZooKeeperOp(zkw, dnfs).toRequestRecord();
1308     try {
1309       zkw.getRecoverableZooKeeper().delete(delete.getPath(), delete.getVersion());
1310     } catch(KeeperException.NoNodeException nne) {
1311     } catch(InterruptedException ie) {
1312       zkw.interruptedException(ie);
1313     }
1314   }
1315 
1316 
1317   /**
1318    * Delete the specified node and all of it's children.
1319    * <p>
1320    * If the node does not exist, just returns.
1321    * <p>
1322    * Sets no watches. Throws all exceptions besides dealing with deletion of
1323    * children.
1324    */
1325   public static void deleteNodeRecursively(ZooKeeperWatcher zkw, String node)
1326   throws KeeperException {
1327     try {
1328       List<String> children = ZKUtil.listChildrenNoWatch(zkw, node);
1329       // the node is already deleted, so we just finish
1330       if (children == null) return;
1331 
1332       if(!children.isEmpty()) {
1333         for(String child : children) {
1334           deleteNodeRecursively(zkw, joinZNode(node, child));
1335         }
1336       }
1337       //Zookeeper Watches are one time triggers; When children of parent nodes are deleted recursively. 
1338       //Must set another watch, get notified of delete node   
1339       if (zkw.getRecoverableZooKeeper().exists(node, zkw) != null){
1340         zkw.getRecoverableZooKeeper().delete(node, -1);
1341       }
1342     } catch(InterruptedException ie) {
1343       zkw.interruptedException(ie);
1344     }
1345   }
1346 
1347   /**
1348    * Delete all the children of the specified node but not the node itself.
1349    *
1350    * Sets no watches.  Throws all exceptions besides dealing with deletion of
1351    * children.
1352    *
1353    * If hbase.zookeeper.useMulti is true, use ZooKeeper's multi-update functionality.
1354    * Otherwise, run the list of operations sequentially.
1355    *
1356    * @throws KeeperException
1357    */
1358   public static void deleteChildrenRecursively(ZooKeeperWatcher zkw, String node)
1359       throws KeeperException {
1360     deleteChildrenRecursivelyMultiOrSequential(zkw, true, node);
1361   }
1362 
1363   /**
1364    * Delete all the children of the specified node but not the node itself. This
1365    * will first traverse the znode tree for listing the children and then delete
1366    * these znodes using multi-update api or sequential based on the specified
1367    * configurations.
1368    * <p>
1369    * Sets no watches. Throws all exceptions besides dealing with deletion of
1370    * children.
1371    * <p>
1372    * If hbase.zookeeper.useMulti is true, use ZooKeeper's multi-update
1373    * functionality. Otherwise, run the list of operations sequentially.
1374    * <p>
1375    * If all of the following are true:
1376    * <ul>
1377    * <li>runSequentialOnMultiFailure is true
1378    * <li>hbase.zookeeper.useMulti is true
1379    * </ul>
1380    * on calling multi, we get a ZooKeeper exception that can be handled by a
1381    * sequential call(*), we retry the operations one-by-one (sequentially).
1382    *
1383    * @param zkw
1384    *          - zk reference
1385    * @param runSequentialOnMultiFailure
1386    *          - if true when we get a ZooKeeper exception that could retry the
1387    *          operations one-by-one (sequentially)
1388    * @param pathRoots
1389    *          - path of the parent node(s)
1390    * @throws KeeperException.NotEmptyException
1391    *           if node has children while deleting
1392    * @throws KeeperException
1393    *           if unexpected ZooKeeper exception
1394    * @throws IllegalArgumentException
1395    *           if an invalid path is specified
1396    */
1397   public static void deleteChildrenRecursivelyMultiOrSequential(
1398       ZooKeeperWatcher zkw, boolean runSequentialOnMultiFailure,
1399       String... pathRoots) throws KeeperException {
1400     if (pathRoots == null || pathRoots.length <= 0) {
1401       LOG.warn("Given path is not valid!");
1402       return;
1403     }
1404     List<ZKUtilOp> ops = new ArrayList<ZKUtil.ZKUtilOp>();
1405     for (String eachRoot : pathRoots) {
1406       List<String> children = listChildrenBFSNoWatch(zkw, eachRoot);
1407       // Delete the leaves first and eventually get rid of the root
1408       for (int i = children.size() - 1; i >= 0; --i) {
1409         ops.add(ZKUtilOp.deleteNodeFailSilent(children.get(i)));
1410       }
1411     }
1412     // atleast one element should exist
1413     if (ops.size() > 0) {
1414       multiOrSequential(zkw, ops, runSequentialOnMultiFailure);
1415     }
1416   }
1417 
1418   /**
1419    * BFS Traversal of all the children under path, with the entries in the list,
1420    * in the same order as that of the traversal. Lists all the children without
1421    * setting any watches.
1422    *
1423    * @param zkw
1424    *          - zk reference
1425    * @param znode
1426    *          - path of node
1427    * @return list of children znodes under the path
1428    * @throws KeeperException
1429    *           if unexpected ZooKeeper exception
1430    */
1431   private static List<String> listChildrenBFSNoWatch(ZooKeeperWatcher zkw,
1432       final String znode) throws KeeperException {
1433     Deque<String> queue = new LinkedList<String>();
1434     List<String> tree = new ArrayList<String>();
1435     queue.add(znode);
1436     while (true) {
1437       String node = queue.pollFirst();
1438       if (node == null) {
1439         break;
1440       }
1441       List<String> children = listChildrenNoWatch(zkw, node);
1442       if (children == null) {
1443         continue;
1444       }
1445       for (final String child : children) {
1446         final String childPath = node + "/" + child;
1447         queue.add(childPath);
1448         tree.add(childPath);
1449       }
1450     }
1451     return tree;
1452   }
1453 
1454   /**
1455    * Represents an action taken by ZKUtil, e.g. createAndFailSilent.
1456    * These actions are higher-level than ZKOp actions, which represent
1457    * individual actions in the ZooKeeper API, like create.
1458    */
1459   public abstract static class ZKUtilOp {
1460     private String path;
1461 
1462     private ZKUtilOp(String path) {
1463       this.path = path;
1464     }
1465 
1466     /**
1467      * @return a createAndFailSilent ZKUtilOp
1468      */
1469     public static ZKUtilOp createAndFailSilent(String path, byte[] data) {
1470       return new CreateAndFailSilent(path, data);
1471     }
1472 
1473     /**
1474      * @return a deleteNodeFailSilent ZKUtilOP
1475      */
1476     public static ZKUtilOp deleteNodeFailSilent(String path) {
1477       return new DeleteNodeFailSilent(path);
1478     }
1479 
1480     /**
1481      * @return a setData ZKUtilOp
1482      */
1483     public static ZKUtilOp setData(String path, byte [] data) {
1484       return new SetData(path, data);
1485     }
1486 
1487     /**
1488      * @return path to znode where the ZKOp will occur
1489      */
1490     public String getPath() {
1491       return path;
1492     }
1493 
1494     /**
1495      * ZKUtilOp representing createAndFailSilent in ZooKeeper
1496      * (attempt to create node, ignore error if already exists)
1497      */
1498     public static class CreateAndFailSilent extends ZKUtilOp {
1499       private byte [] data;
1500 
1501       private CreateAndFailSilent(String path, byte [] data) {
1502         super(path);
1503         this.data = data;
1504       }
1505 
1506       public byte[] getData() {
1507         return data;
1508       }
1509 
1510       @Override
1511       public boolean equals(Object o) {
1512         if (this == o) return true;
1513         if (!(o instanceof CreateAndFailSilent)) return false;
1514 
1515         CreateAndFailSilent op = (CreateAndFailSilent) o;
1516         return getPath().equals(op.getPath()) && Arrays.equals(data, op.data);
1517       }
1518 
1519       @Override
1520       public int hashCode() {
1521         int ret = 17 + getPath().hashCode() * 31;
1522         return ret * 31 + Bytes.hashCode(data);
1523       }
1524     }
1525 
1526     /**
1527      * ZKUtilOp representing deleteNodeFailSilent in ZooKeeper
1528      * (attempt to delete node, ignore error if node doesn't exist)
1529      */
1530     public static class DeleteNodeFailSilent extends ZKUtilOp {
1531       private DeleteNodeFailSilent(String path) {
1532         super(path);
1533       }
1534 
1535       @Override
1536       public boolean equals(Object o) {
1537         if (this == o) return true;
1538         if (!(o instanceof DeleteNodeFailSilent)) return false;
1539 
1540         return super.equals(o);
1541       }
1542 
1543       @Override
1544       public int hashCode() {
1545         return getPath().hashCode();
1546       }
1547     }
1548 
1549     /**
1550      * ZKUtilOp representing setData in ZooKeeper
1551      */
1552     public static class SetData extends ZKUtilOp {
1553       private byte [] data;
1554 
1555       private SetData(String path, byte [] data) {
1556         super(path);
1557         this.data = data;
1558       }
1559 
1560       public byte[] getData() {
1561         return data;
1562       }
1563 
1564       @Override
1565       public boolean equals(Object o) {
1566         if (this == o) return true;
1567         if (!(o instanceof SetData)) return false;
1568 
1569         SetData op = (SetData) o;
1570         return getPath().equals(op.getPath()) && Arrays.equals(data, op.data);
1571       }
1572 
1573       @Override
1574       public int hashCode() {
1575         int ret = getPath().hashCode();
1576         return ret * 31 + Bytes.hashCode(data);
1577       }
1578     }
1579   }
1580 
1581   /**
1582    * Convert from ZKUtilOp to ZKOp
1583    */
1584   private static Op toZooKeeperOp(ZooKeeperWatcher zkw, ZKUtilOp op)
1585   throws UnsupportedOperationException {
1586     if(op == null) return null;
1587 
1588     if (op instanceof CreateAndFailSilent) {
1589       CreateAndFailSilent cafs = (CreateAndFailSilent)op;
1590       return Op.create(cafs.getPath(), cafs.getData(), createACL(zkw, cafs.getPath()),
1591         CreateMode.PERSISTENT);
1592     } else if (op instanceof DeleteNodeFailSilent) {
1593       DeleteNodeFailSilent dnfs = (DeleteNodeFailSilent)op;
1594       return Op.delete(dnfs.getPath(), -1);
1595     } else if (op instanceof SetData) {
1596       SetData sd = (SetData)op;
1597       return Op.setData(sd.getPath(), sd.getData(), -1);
1598     } else {
1599       throw new UnsupportedOperationException("Unexpected ZKUtilOp type: "
1600         + op.getClass().getName());
1601     }
1602   }
1603 
1604   /**
1605    * If hbase.zookeeper.useMulti is true, use ZooKeeper's multi-update functionality.
1606    * Otherwise, run the list of operations sequentially.
1607    *
1608    * If all of the following are true:
1609    * - runSequentialOnMultiFailure is true
1610    * - hbase.zookeeper.useMulti is true
1611    * - on calling multi, we get a ZooKeeper exception that can be handled by a sequential call(*)
1612    * Then:
1613    * - we retry the operations one-by-one (sequentially)
1614    *
1615    * Note *: an example is receiving a NodeExistsException from a "create" call.  Without multi,
1616    * a user could call "createAndFailSilent" to ensure that a node exists if they don't care who
1617    * actually created the node (i.e. the NodeExistsException from ZooKeeper is caught).
1618    * This will cause all operations in the multi to fail, however, because
1619    * the NodeExistsException that zk.create throws will fail the multi transaction.
1620    * In this case, if the previous conditions hold, the commands are run sequentially, which should
1621    * result in the correct final state, but means that the operations will not run atomically.
1622    *
1623    * @throws KeeperException
1624    */
1625   public static void multiOrSequential(ZooKeeperWatcher zkw, List<ZKUtilOp> ops,
1626       boolean runSequentialOnMultiFailure) throws KeeperException {
1627     if (ops == null) return;
1628     boolean useMulti = zkw.getConfiguration().getBoolean(HConstants.ZOOKEEPER_USEMULTI, false);
1629 
1630     if (useMulti) {
1631       List<Op> zkOps = new LinkedList<Op>();
1632       for (ZKUtilOp op : ops) {
1633         zkOps.add(toZooKeeperOp(zkw, op));
1634       }
1635       try {
1636         zkw.getRecoverableZooKeeper().multi(zkOps);
1637       } catch (KeeperException ke) {
1638        switch (ke.code()) {
1639          case NODEEXISTS:
1640          case NONODE:
1641          case BADVERSION:
1642          case NOAUTH:
1643            // if we get an exception that could be solved by running sequentially
1644            // (and the client asked us to), then break out and run sequentially
1645            if (runSequentialOnMultiFailure) {
1646              LOG.info("On call to ZK.multi, received exception: " + ke.toString() + "."
1647                + "  Attempting to run operations sequentially because"
1648                + " runSequentialOnMultiFailure is: " + runSequentialOnMultiFailure + ".");
1649              processSequentially(zkw, ops);
1650              break;
1651            }
1652           default:
1653             throw ke;
1654         }
1655       } catch (InterruptedException ie) {
1656         zkw.interruptedException(ie);
1657       }
1658     } else {
1659       // run sequentially
1660       processSequentially(zkw, ops);
1661     }
1662 
1663   }
1664 
1665   private static void processSequentially(ZooKeeperWatcher zkw, List<ZKUtilOp> ops)
1666       throws KeeperException, NoNodeException {
1667     for (ZKUtilOp op : ops) {
1668       if (op instanceof CreateAndFailSilent) {
1669         createAndFailSilent(zkw, (CreateAndFailSilent) op);
1670       } else if (op instanceof DeleteNodeFailSilent) {
1671         deleteNodeFailSilent(zkw, (DeleteNodeFailSilent) op);
1672       } else if (op instanceof SetData) {
1673         setData(zkw, (SetData) op);
1674       } else {
1675         throw new UnsupportedOperationException("Unexpected ZKUtilOp type: "
1676             + op.getClass().getName());
1677       }
1678     }
1679   }
1680 
1681   //
1682   // ZooKeeper cluster information
1683   //
1684 
1685   /** @return String dump of everything in ZooKeeper. */
1686   public static String dump(ZooKeeperWatcher zkw) {
1687     StringBuilder sb = new StringBuilder();
1688     try {
1689       sb.append("HBase is rooted at ").append(zkw.baseZNode);
1690       sb.append("\nActive master address: ");
1691       try {
1692         sb.append(MasterAddressTracker.getMasterAddress(zkw));
1693       } catch (IOException e) {
1694         sb.append("<<FAILED LOOKUP: " + e.getMessage() + ">>");
1695       }
1696       sb.append("\nBackup master addresses:");
1697       for (String child : listChildrenNoWatch(zkw,
1698                                               zkw.backupMasterAddressesZNode)) {
1699         sb.append("\n ").append(child);
1700       }
1701       sb.append("\nRegion server holding hbase:meta: "
1702         + new MetaTableLocator().getMetaRegionLocation(zkw));
1703       Configuration conf = HBaseConfiguration.create();
1704       int numMetaReplicas = conf.getInt(HConstants.META_REPLICAS_NUM,
1705                HConstants.DEFAULT_META_REPLICA_NUM);
1706       for (int i = 1; i < numMetaReplicas; i++) {
1707         sb.append("\nRegion server holding hbase:meta, replicaId " + i + " "
1708                     + new MetaTableLocator().getMetaRegionLocation(zkw, i));
1709       }
1710       sb.append("\nRegion servers:");
1711       for (String child : listChildrenNoWatch(zkw, zkw.rsZNode)) {
1712         sb.append("\n ").append(child);
1713       }
1714       try {
1715         getReplicationZnodesDump(zkw, sb);
1716       } catch (KeeperException ke) {
1717         LOG.warn("Couldn't get the replication znode dump", ke);
1718       }
1719       sb.append("\nQuorum Server Statistics:");
1720       String[] servers = zkw.getQuorum().split(",");
1721       for (String server : servers) {
1722         sb.append("\n ").append(server);
1723         try {
1724           String[] stat = getServerStats(server, ZKUtil.zkDumpConnectionTimeOut);
1725 
1726           if (stat == null) {
1727             sb.append("[Error] invalid quorum server: " + server);
1728             break;
1729           }
1730 
1731           for (String s : stat) {
1732             sb.append("\n  ").append(s);
1733           }
1734         } catch (Exception e) {
1735           sb.append("\n  ERROR: ").append(e.getMessage());
1736         }
1737       }
1738     } catch (KeeperException ke) {
1739       sb.append("\nFATAL ZooKeeper Exception!\n");
1740       sb.append("\n" + ke.getMessage());
1741     }
1742     return sb.toString();
1743   }
1744 
1745   /**
1746    * Appends replication znodes to the passed StringBuilder.
1747    * @param zkw
1748    * @param sb
1749    * @throws KeeperException
1750    */
1751   private static void getReplicationZnodesDump(ZooKeeperWatcher zkw, StringBuilder sb)
1752       throws KeeperException {
1753     String replicationZNodeName = zkw.getConfiguration().get("zookeeper.znode.replication",
1754       "replication");
1755     String replicationZnode = joinZNode(zkw.baseZNode, replicationZNodeName);
1756     if (ZKUtil.checkExists(zkw, replicationZnode) == -1) return;
1757     // do a ls -r on this znode
1758     sb.append("\n").append(replicationZnode).append(": ");
1759     List<String> children = ZKUtil.listChildrenNoWatch(zkw, replicationZnode);
1760     for (String child : children) {
1761       String znode = joinZNode(replicationZnode, child);
1762       if (child.equals(zkw.getConfiguration().get("zookeeper.znode.replication.peers", "peers"))) {
1763         appendPeersZnodes(zkw, znode, sb);
1764       } else if (child.equals(zkw.getConfiguration().
1765           get("zookeeper.znode.replication.rs", "rs"))) {
1766         appendRSZnodes(zkw, znode, sb);
1767       }
1768     }
1769   }
1770 
1771   private static void appendRSZnodes(ZooKeeperWatcher zkw, String znode, StringBuilder sb)
1772       throws KeeperException {
1773     List<String> stack = new LinkedList<String>();
1774     stack.add(znode);
1775     do {
1776       String znodeToProcess = stack.remove(stack.size() - 1);
1777       sb.append("\n").append(znodeToProcess).append(": ");
1778       byte[] data;
1779       try {
1780         data = ZKUtil.getData(zkw, znodeToProcess);
1781       } catch (InterruptedException e) {
1782         zkw.interruptedException(e);
1783         return;
1784       }
1785       if (data != null && data.length > 0) { // log position
1786         long position = 0;
1787         try {
1788           position = ZKUtil.parseWALPositionFrom(ZKUtil.getData(zkw, znodeToProcess));
1789           sb.append(position);
1790         } catch (DeserializationException ignored) {
1791         } catch (InterruptedException e) {
1792           zkw.interruptedException(e);
1793           return;
1794         }
1795       }
1796       for (String zNodeChild : ZKUtil.listChildrenNoWatch(zkw, znodeToProcess)) {
1797         stack.add(ZKUtil.joinZNode(znodeToProcess, zNodeChild));
1798       }
1799     } while (stack.size() > 0);
1800   }
1801 
1802   private static void appendPeersZnodes(ZooKeeperWatcher zkw, String peersZnode,
1803     StringBuilder sb) throws KeeperException {
1804     int pblen = ProtobufUtil.lengthOfPBMagic();
1805     sb.append("\n").append(peersZnode).append(": ");
1806     for (String peerIdZnode : ZKUtil.listChildrenNoWatch(zkw, peersZnode)) {
1807       String znodeToProcess = ZKUtil.joinZNode(peersZnode, peerIdZnode);
1808       byte[] data;
1809       try {
1810         data = ZKUtil.getData(zkw, znodeToProcess);
1811       } catch (InterruptedException e) {
1812         zkw.interruptedException(e);
1813         return;
1814       }
1815       // parse the data of the above peer znode.
1816       try {
1817       String clusterKey = ZooKeeperProtos.ReplicationPeer.newBuilder().
1818         mergeFrom(data, pblen, data.length - pblen).getClusterkey();
1819       sb.append("\n").append(znodeToProcess).append(": ").append(clusterKey);
1820       // add the peer-state.
1821       appendPeerState(zkw, znodeToProcess, sb);
1822       } catch (InvalidProtocolBufferException ipbe) {
1823         LOG.warn("Got Exception while parsing peer: " + znodeToProcess, ipbe);
1824       }
1825     }
1826   }
1827 
1828   private static void appendPeerState(ZooKeeperWatcher zkw, String znodeToProcess,
1829       StringBuilder sb) throws KeeperException, InvalidProtocolBufferException {
1830     String peerState = zkw.getConfiguration().get("zookeeper.znode.replication.peers.state",
1831       "peer-state");
1832     int pblen = ProtobufUtil.lengthOfPBMagic();
1833     for (String child : ZKUtil.listChildrenNoWatch(zkw, znodeToProcess)) {
1834       if (!child.equals(peerState)) continue;
1835       String peerStateZnode = ZKUtil.joinZNode(znodeToProcess, child);
1836       sb.append("\n").append(peerStateZnode).append(": ");
1837       byte[] peerStateData;
1838       try {
1839         peerStateData = ZKUtil.getData(zkw, peerStateZnode);
1840         sb.append(ZooKeeperProtos.ReplicationState.newBuilder()
1841             .mergeFrom(peerStateData, pblen, peerStateData.length - pblen).getState().name());
1842       } catch (InterruptedException e) {
1843         zkw.interruptedException(e);
1844         return;
1845       }
1846     }
1847   }
1848 
1849   /**
1850    * Gets the statistics from the given server.
1851    *
1852    * @param server  The server to get the statistics from.
1853    * @param timeout  The socket timeout to use.
1854    * @return The array of response strings.
1855    * @throws IOException When the socket communication fails.
1856    */
1857   public static String[] getServerStats(String server, int timeout)
1858   throws IOException {
1859     String[] sp = server.split(":");
1860     if (sp == null || sp.length == 0) {
1861       return null;
1862     }
1863 
1864     String host = sp[0];
1865     int port = sp.length > 1 ? Integer.parseInt(sp[1])
1866         : HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT;
1867 
1868     Socket socket = new Socket();
1869     InetSocketAddress sockAddr = new InetSocketAddress(host, port);
1870     socket.connect(sockAddr, timeout);
1871 
1872     socket.setSoTimeout(timeout);
1873     PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
1874     BufferedReader in = new BufferedReader(new InputStreamReader(
1875       socket.getInputStream()));
1876     out.println("stat");
1877     out.flush();
1878     ArrayList<String> res = new ArrayList<String>();
1879     while (true) {
1880       String line = in.readLine();
1881       if (line != null) {
1882         res.add(line);
1883       } else {
1884         break;
1885       }
1886     }
1887     socket.close();
1888     return res.toArray(new String[res.size()]);
1889   }
1890 
1891   private static void logRetrievedMsg(final ZooKeeperWatcher zkw,
1892       final String znode, final byte [] data, final boolean watcherSet) {
1893     if (!LOG.isTraceEnabled()) return;
1894     LOG.trace(zkw.prefix("Retrieved " + ((data == null)? 0: data.length) +
1895       " byte(s) of data from znode " + znode +
1896       (watcherSet? " and set watcher; ": "; data=") +
1897       (data == null? "null": data.length == 0? "empty": (
1898           znode.startsWith(ZooKeeperWatcher.META_ZNODE_PREFIX)?
1899             getServerNameOrEmptyString(data):
1900           znode.startsWith(zkw.backupMasterAddressesZNode)?
1901             getServerNameOrEmptyString(data):
1902           StringUtils.abbreviate(Bytes.toStringBinary(data), 32)))));
1903   }
1904 
1905   private static String getServerNameOrEmptyString(final byte [] data) {
1906     try {
1907       return ServerName.parseFrom(data).toString();
1908     } catch (DeserializationException e) {
1909       return "";
1910     }
1911   }
1912 
1913   /**
1914    * Waits for HBase installation's base (parent) znode to become available.
1915    * @throws IOException on ZK errors
1916    */
1917   public static void waitForBaseZNode(Configuration conf) throws IOException {
1918     LOG.info("Waiting until the base znode is available");
1919     String parentZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT,
1920         HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
1921     ZooKeeper zk = new ZooKeeper(ZKConfig.getZKQuorumServersString(conf),
1922         conf.getInt(HConstants.ZK_SESSION_TIMEOUT,
1923         HConstants.DEFAULT_ZK_SESSION_TIMEOUT), EmptyWatcher.instance);
1924 
1925     final int maxTimeMs = 10000;
1926     final int maxNumAttempts = maxTimeMs / HConstants.SOCKET_RETRY_WAIT_MS;
1927 
1928     KeeperException keeperEx = null;
1929     try {
1930       try {
1931         for (int attempt = 0; attempt < maxNumAttempts; ++attempt) {
1932           try {
1933             if (zk.exists(parentZNode, false) != null) {
1934               LOG.info("Parent znode exists: " + parentZNode);
1935               keeperEx = null;
1936               break;
1937             }
1938           } catch (KeeperException e) {
1939             keeperEx = e;
1940           }
1941           Threads.sleepWithoutInterrupt(HConstants.SOCKET_RETRY_WAIT_MS);
1942         }
1943       } finally {
1944         zk.close();
1945       }
1946     } catch (InterruptedException ex) {
1947       Thread.currentThread().interrupt();
1948     }
1949 
1950     if (keeperEx != null) {
1951       throw new IOException(keeperEx);
1952     }
1953   }
1954 
1955   /**
1956    * Convert a {@link DeserializationException} to a more palatable {@link KeeperException}.
1957    * Used when can't let a {@link DeserializationException} out w/o changing public API.
1958    * @param e Exception to convert
1959    * @return Converted exception
1960    */
1961   public static KeeperException convert(final DeserializationException e) {
1962     KeeperException ke = new KeeperException.DataInconsistencyException();
1963     ke.initCause(e);
1964     return ke;
1965   }
1966 
1967   /**
1968    * Recursively print the current state of ZK (non-transactional)
1969    * @param root name of the root directory in zk to print
1970    * @throws KeeperException
1971    */
1972   public static void logZKTree(ZooKeeperWatcher zkw, String root) {
1973     if (!LOG.isDebugEnabled()) return;
1974     LOG.debug("Current zk system:");
1975     String prefix = "|-";
1976     LOG.debug(prefix + root);
1977     try {
1978       logZKTree(zkw, root, prefix);
1979     } catch (KeeperException e) {
1980       throw new RuntimeException(e);
1981     }
1982   }
1983 
1984   /**
1985    * Helper method to print the current state of the ZK tree.
1986    * @see #logZKTree(ZooKeeperWatcher, String)
1987    * @throws KeeperException if an unexpected exception occurs
1988    */
1989   protected static void logZKTree(ZooKeeperWatcher zkw, String root, String prefix) throws KeeperException {
1990     List<String> children = ZKUtil.listChildrenNoWatch(zkw, root);
1991     if (children == null) return;
1992     for (String child : children) {
1993       LOG.debug(prefix + child);
1994       String node = ZKUtil.joinZNode(root.equals("/") ? "" : root, child);
1995       logZKTree(zkw, node, prefix + "---");
1996     }
1997   }
1998 
1999   /**
2000    * @param position
2001    * @return Serialized protobuf of <code>position</code> with pb magic prefix prepended suitable
2002    *         for use as content of an wal position in a replication queue.
2003    */
2004   public static byte[] positionToByteArray(final long position) {
2005     byte[] bytes = ZooKeeperProtos.ReplicationHLogPosition.newBuilder().setPosition(position)
2006         .build().toByteArray();
2007     return ProtobufUtil.prependPBMagic(bytes);
2008   }
2009 
2010   /**
2011    * @param bytes - Content of a WAL position znode.
2012    * @return long - The current WAL position.
2013    * @throws DeserializationException
2014    */
2015   public static long parseWALPositionFrom(final byte[] bytes) throws DeserializationException {
2016     if (bytes == null) {
2017       throw new DeserializationException("Unable to parse null WAL position.");
2018     }
2019     if (ProtobufUtil.isPBMagicPrefix(bytes)) {
2020       int pblen = ProtobufUtil.lengthOfPBMagic();
2021       ZooKeeperProtos.ReplicationHLogPosition.Builder builder =
2022           ZooKeeperProtos.ReplicationHLogPosition.newBuilder();
2023       ZooKeeperProtos.ReplicationHLogPosition position;
2024       try {
2025         position = builder.mergeFrom(bytes, pblen, bytes.length - pblen).build();
2026       } catch (InvalidProtocolBufferException e) {
2027         throw new DeserializationException(e);
2028       }
2029       return position.getPosition();
2030     } else {
2031       if (bytes.length > 0) {
2032         return Bytes.toLong(bytes);
2033       }
2034       return 0;
2035     }
2036   }
2037 
2038   /**
2039    * @param regionLastFlushedSequenceId the flushed sequence id of a region which is the min of its
2040    *          store max seq ids
2041    * @param storeSequenceIds column family to sequence Id map
2042    * @return Serialized protobuf of <code>RegionSequenceIds</code> with pb magic prefix prepended
2043    *         suitable for use to filter wal edits in distributedLogReplay mode
2044    */
2045   public static byte[] regionSequenceIdsToByteArray(final Long regionLastFlushedSequenceId,
2046       final Map<byte[], Long> storeSequenceIds) {
2047     ZooKeeperProtos.RegionStoreSequenceIds.Builder regionSequenceIdsBuilder =
2048         ZooKeeperProtos.RegionStoreSequenceIds.newBuilder();
2049     ZooKeeperProtos.StoreSequenceId.Builder storeSequenceIdBuilder =
2050         ZooKeeperProtos.StoreSequenceId.newBuilder();
2051     if (storeSequenceIds != null) {
2052       for (Map.Entry<byte[], Long> e : storeSequenceIds.entrySet()){
2053         byte[] columnFamilyName = e.getKey();
2054         Long curSeqId = e.getValue();
2055         storeSequenceIdBuilder.setFamilyName(ByteStringer.wrap(columnFamilyName));
2056         storeSequenceIdBuilder.setSequenceId(curSeqId);
2057         regionSequenceIdsBuilder.addStoreSequenceId(storeSequenceIdBuilder.build());
2058         storeSequenceIdBuilder.clear();
2059       }
2060     }
2061     regionSequenceIdsBuilder.setLastFlushedSequenceId(regionLastFlushedSequenceId);
2062     byte[] result = regionSequenceIdsBuilder.build().toByteArray();
2063     return ProtobufUtil.prependPBMagic(result);
2064   }
2065 
2066   /**
2067    * @param bytes Content of serialized data of RegionStoreSequenceIds
2068    * @return a RegionStoreSequenceIds object
2069    * @throws DeserializationException
2070    */
2071   public static RegionStoreSequenceIds parseRegionStoreSequenceIds(final byte[] bytes)
2072       throws DeserializationException {
2073     if (bytes == null || !ProtobufUtil.isPBMagicPrefix(bytes)) {
2074       throw new DeserializationException("Unable to parse RegionStoreSequenceIds.");
2075     }
2076     RegionStoreSequenceIds.Builder regionSequenceIdsBuilder =
2077         ZooKeeperProtos.RegionStoreSequenceIds.newBuilder();
2078     int pblen = ProtobufUtil.lengthOfPBMagic();
2079     RegionStoreSequenceIds storeIds = null;
2080     try {
2081       storeIds = regionSequenceIdsBuilder.mergeFrom(bytes, pblen, bytes.length - pblen).build();
2082     } catch (InvalidProtocolBufferException e) {
2083       throw new DeserializationException(e);
2084     }
2085     return storeIds;
2086   }
2087 }