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