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