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