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.Closeable;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.concurrent.CopyOnWriteArrayList;
29  import java.util.concurrent.CountDownLatch;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.hadoop.conf.Configuration;
34  import org.apache.hadoop.hbase.Abortable;
35  import org.apache.hadoop.hbase.HConstants;
36  import org.apache.hadoop.hbase.HRegionInfo;
37  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
38  import org.apache.hadoop.hbase.classification.InterfaceAudience;
39  import org.apache.zookeeper.KeeperException;
40  import org.apache.zookeeper.WatchedEvent;
41  import org.apache.zookeeper.Watcher;
42  import org.apache.zookeeper.ZooDefs;
43  import org.apache.zookeeper.data.ACL;
44  
45  /**
46   * Acts as the single ZooKeeper Watcher.  One instance of this is instantiated
47   * for each Master, RegionServer, and client process.
48   *
49   * <p>This is the only class that implements {@link Watcher}.  Other internal
50   * classes which need to be notified of ZooKeeper events must register with
51   * the local instance of this watcher via {@link #registerListener}.
52   *
53   * <p>This class also holds and manages the connection to ZooKeeper.  Code to
54   * deal with connection related events and exceptions are handled here.
55   */
56  @InterfaceAudience.Private
57  public class ZooKeeperWatcher implements Watcher, Abortable, Closeable {
58    private static final Log LOG = LogFactory.getLog(ZooKeeperWatcher.class);
59  
60    // Identifier for this watcher (for logging only).  It is made of the prefix
61    // passed on construction and the zookeeper sessionid.
62    private String prefix;
63    private String identifier;
64  
65    // zookeeper quorum
66    private String quorum;
67  
68    // zookeeper connection
69    private RecoverableZooKeeper recoverableZooKeeper;
70  
71    // abortable in case of zk failure
72    protected Abortable abortable;
73    // Used if abortable is null
74    private boolean aborted = false;
75  
76    // listeners to be notified
77    private final List<ZooKeeperListener> listeners =
78      new CopyOnWriteArrayList<ZooKeeperListener>();
79  
80    // Used by ZKUtil:waitForZKConnectionIfAuthenticating to wait for SASL
81    // negotiation to complete
82    public CountDownLatch saslLatch = new CountDownLatch(1);
83  
84    // node names
85  
86    // base znode for this cluster
87    public String baseZNode;
88    //znodes containing the locations of the servers hosting the meta replicas
89    private Map<Integer,String> metaReplicaZnodes = new HashMap<Integer, String>();
90    // znode containing ephemeral nodes of the regionservers
91    public String rsZNode;
92    // znode containing ephemeral nodes of the draining regionservers
93    public String drainingZNode;
94    // znode of currently active master
95    private String masterAddressZNode;
96    // znode of this master in backup master directory, if not the active master
97    public String backupMasterAddressesZNode;
98    // znode containing the current cluster state
99    public String clusterStateZNode;
100   // znode used for table disabling/enabling
101   @Deprecated
102   public String tableZNode;
103   // znode containing the unique cluster ID
104   public String clusterIdZNode;
105   // znode used for log splitting work assignment
106   public String splitLogZNode;
107   // znode containing the state of the load balancer
108   public String balancerZNode;
109   // znode containing the lock for the tables
110   public String tableLockZNode;
111   // znode containing the state of recovering regions
112   public String recoveringRegionsZNode;
113   // znode containing namespace descriptors
114   public static String namespaceZNode = "namespace";
115 
116   public final static String META_ZNODE_PREFIX = "meta-region-server";
117 
118   private final Configuration conf;
119 
120   private final Exception constructorCaller;
121 
122   /**
123    * Instantiate a ZooKeeper connection and watcher.
124    * @param identifier string that is passed to RecoverableZookeeper to be used as
125    * identifier for this instance. Use null for default.
126    * @throws IOException
127    * @throws ZooKeeperConnectionException
128    */
129   public ZooKeeperWatcher(Configuration conf, String identifier,
130       Abortable abortable) throws ZooKeeperConnectionException, IOException {
131     this(conf, identifier, abortable, false);
132   }
133 
134   /**
135    * Instantiate a ZooKeeper connection and watcher.
136    * @param conf
137    * @param identifier string that is passed to RecoverableZookeeper to be used as identifier for
138    *          this instance. Use null for default.
139    * @param abortable Can be null if there is on error there is no host to abort: e.g. client
140    *          context.
141    * @param canCreateBaseZNode
142    * @throws IOException
143    * @throws ZooKeeperConnectionException
144    */
145   public ZooKeeperWatcher(Configuration conf, String identifier,
146       Abortable abortable, boolean canCreateBaseZNode)
147   throws IOException, ZooKeeperConnectionException {
148     this.conf = conf;
149     // Capture a stack trace now.  Will print it out later if problem so we can
150     // distingush amongst the myriad ZKWs.
151     try {
152       throw new Exception("ZKW CONSTRUCTOR STACK TRACE FOR DEBUGGING");
153     } catch (Exception e) {
154       this.constructorCaller = e;
155     }
156     this.quorum = ZKConfig.getZKQuorumServersString(conf);
157     this.prefix = identifier;
158     // Identifier will get the sessionid appended later below down when we
159     // handle the syncconnect event.
160     this.identifier = identifier + "0x0";
161     this.abortable = abortable;
162     setNodeNames(conf);
163     this.recoverableZooKeeper = ZKUtil.connect(conf, quorum, this, identifier);
164     if (canCreateBaseZNode) {
165       createBaseZNodes();
166     }
167   }
168 
169   private void createBaseZNodes() throws ZooKeeperConnectionException {
170     try {
171       // Create all the necessary "directories" of znodes
172       ZKUtil.createWithParents(this, baseZNode);
173       ZKUtil.createAndFailSilent(this, rsZNode);
174       ZKUtil.createAndFailSilent(this, drainingZNode);
175       ZKUtil.createAndFailSilent(this, tableZNode);
176       ZKUtil.createAndFailSilent(this, splitLogZNode);
177       ZKUtil.createAndFailSilent(this, backupMasterAddressesZNode);
178       ZKUtil.createAndFailSilent(this, tableLockZNode);
179       ZKUtil.createAndFailSilent(this, recoveringRegionsZNode);
180     } catch (KeeperException e) {
181       throw new ZooKeeperConnectionException(
182           prefix("Unexpected KeeperException creating base node"), e);
183     }
184   }
185 
186   @Override
187   public String toString() {
188     return this.identifier + ", quorum=" + quorum + ", baseZNode=" + baseZNode;
189   }
190 
191   /**
192    * Adds this instance's identifier as a prefix to the passed <code>str</code>
193    * @param str String to amend.
194    * @return A new string with this instance's identifier as prefix: e.g.
195    * if passed 'hello world', the returned string could be
196    */
197   public String prefix(final String str) {
198     return this.toString() + " " + str;
199   }
200 
201   /**
202    * Set the local variable node names using the specified configuration.
203    */
204   private void setNodeNames(Configuration conf) {
205     baseZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT,
206         HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
207     metaReplicaZnodes.put(0, ZKUtil.joinZNode(baseZNode,
208            conf.get("zookeeper.znode.metaserver", "meta-region-server")));
209     int numMetaReplicas = conf.getInt(HConstants.META_REPLICAS_NUM,
210             HConstants.DEFAULT_META_REPLICA_NUM);
211     for (int i = 1; i < numMetaReplicas; i++) {
212       String str = ZKUtil.joinZNode(baseZNode,
213         conf.get("zookeeper.znode.metaserver", "meta-region-server") + "-" + i);
214       metaReplicaZnodes.put(i, str);
215     }
216     rsZNode = ZKUtil.joinZNode(baseZNode,
217         conf.get("zookeeper.znode.rs", "rs"));
218     drainingZNode = ZKUtil.joinZNode(baseZNode,
219         conf.get("zookeeper.znode.draining.rs", "draining"));
220     masterAddressZNode = ZKUtil.joinZNode(baseZNode,
221         conf.get("zookeeper.znode.master", "master"));
222     backupMasterAddressesZNode = ZKUtil.joinZNode(baseZNode,
223         conf.get("zookeeper.znode.backup.masters", "backup-masters"));
224     clusterStateZNode = ZKUtil.joinZNode(baseZNode,
225         conf.get("zookeeper.znode.state", "running"));
226     tableZNode = ZKUtil.joinZNode(baseZNode,
227         conf.get("zookeeper.znode.tableEnableDisable", "table"));
228     clusterIdZNode = ZKUtil.joinZNode(baseZNode,
229         conf.get("zookeeper.znode.clusterId", "hbaseid"));
230     splitLogZNode = ZKUtil.joinZNode(baseZNode,
231         conf.get("zookeeper.znode.splitlog", HConstants.SPLIT_LOGDIR_NAME));
232     balancerZNode = ZKUtil.joinZNode(baseZNode,
233         conf.get("zookeeper.znode.balancer", "balancer"));
234     tableLockZNode = ZKUtil.joinZNode(baseZNode,
235         conf.get("zookeeper.znode.tableLock", "table-lock"));
236     recoveringRegionsZNode = ZKUtil.joinZNode(baseZNode,
237         conf.get("zookeeper.znode.recovering.regions", "recovering-regions"));
238     namespaceZNode = ZKUtil.joinZNode(baseZNode,
239         conf.get("zookeeper.znode.namespace", "namespace"));
240   }
241 
242   /**
243    * Is the znode of any meta replica
244    * @param node
245    * @return true or false
246    */
247   public boolean isAnyMetaReplicaZnode(String node) {
248     if (metaReplicaZnodes.values().contains(node)) {
249       return true;
250     }
251     return false;
252   }
253 
254   /**
255    * Is it the default meta replica's znode
256    * @param node
257    * @return true or false
258    */
259   public boolean isDefaultMetaReplicaZnode(String node) {
260     if (getZNodeForReplica(HRegionInfo.DEFAULT_REPLICA_ID).equals(node)) {
261       return true;
262     }
263     return false;
264   }
265 
266   /**
267    * Get the znodes corresponding to the meta replicas from ZK
268    * @return list of znodes
269    * @throws KeeperException
270    */
271   public List<String> getMetaReplicaNodes() throws KeeperException {
272     List<String> childrenOfBaseNode = ZKUtil.listChildrenNoWatch(this, baseZNode);
273     List<String> metaReplicaNodes = new ArrayList<String>(2);
274     String pattern = conf.get("zookeeper.znode.metaserver","meta-region-server");
275     for (String child : childrenOfBaseNode) {
276       if (child.startsWith(pattern)) metaReplicaNodes.add(child);
277     }
278     return metaReplicaNodes;
279   }
280 
281   /**
282    * Get the znode string corresponding to a replicaId
283    * @param replicaId
284    * @return znode
285    */
286   public String getZNodeForReplica(int replicaId) {
287     String str = metaReplicaZnodes.get(replicaId);
288     // return a newly created path but don't update the cache of paths
289     // This is mostly needed for tests that attempt to create meta replicas
290     // from outside the master
291     if (str == null) {
292       str = ZKUtil.joinZNode(baseZNode,
293           conf.get("zookeeper.znode.metaserver", "meta-region-server") + "-" + replicaId);
294     }
295     return str;
296   }
297 
298   /**
299    * Parse the meta replicaId from the passed znode
300    * @param znode
301    * @return replicaId
302    */
303   public int getMetaReplicaIdFromZnode(String znode) {
304     String pattern = conf.get("zookeeper.znode.metaserver","meta-region-server");
305     if (znode.equals(pattern)) return HRegionInfo.DEFAULT_REPLICA_ID;
306     // the non-default replicas are of the pattern meta-region-server-<replicaId>
307     String nonDefaultPattern = pattern + "-"; 
308     return Integer.parseInt(znode.substring(nonDefaultPattern.length()));
309   }
310 
311   /**
312    * Register the specified listener to receive ZooKeeper events.
313    * @param listener
314    */
315   public void registerListener(ZooKeeperListener listener) {
316     listeners.add(listener);
317   }
318 
319   /**
320    * Register the specified listener to receive ZooKeeper events and add it as
321    * the first in the list of current listeners.
322    * @param listener
323    */
324   public void registerListenerFirst(ZooKeeperListener listener) {
325     listeners.add(0, listener);
326   }
327 
328   public void unregisterListener(ZooKeeperListener listener) {
329     listeners.remove(listener);
330   }
331 
332   /**
333    * Clean all existing listeners
334    */
335   public void unregisterAllListeners() {
336     listeners.clear();
337   }
338 
339   /**
340    * Get a copy of current registered listeners
341    */
342   public List<ZooKeeperListener> getListeners() {
343     return new ArrayList<ZooKeeperListener>(listeners);
344   }
345 
346   /**
347    * @return The number of currently registered listeners
348    */
349   public int getNumberOfListeners() {
350     return listeners.size();
351   }
352 
353   /**
354    * Get the connection to ZooKeeper.
355    * @return connection reference to zookeeper
356    */
357   public RecoverableZooKeeper getRecoverableZooKeeper() {
358     return recoverableZooKeeper;
359   }
360 
361   public void reconnectAfterExpiration() throws IOException, KeeperException, InterruptedException {
362     recoverableZooKeeper.reconnectAfterExpiration();
363   }
364 
365   /**
366    * Get the quorum address of this instance.
367    * @return quorum string of this zookeeper connection instance
368    */
369   public String getQuorum() {
370     return quorum;
371   }
372 
373   /**
374    * @return the base znode of this zookeeper connection instance.
375    */
376   public String getBaseZNode() {
377     return baseZNode;
378   }
379 
380   /**
381    * Method called from ZooKeeper for events and connection status.
382    * <p>
383    * Valid events are passed along to listeners.  Connection status changes
384    * are dealt with locally.
385    */
386   @Override
387   public void process(WatchedEvent event) {
388     LOG.debug(prefix("Received ZooKeeper Event, " +
389         "type=" + event.getType() + ", " +
390         "state=" + event.getState() + ", " +
391         "path=" + event.getPath()));
392 
393     switch(event.getType()) {
394 
395       // If event type is NONE, this is a connection status change
396       case None: {
397         connectionEvent(event);
398         break;
399       }
400 
401       // Otherwise pass along to the listeners
402 
403       case NodeCreated: {
404         for(ZooKeeperListener listener : listeners) {
405           listener.nodeCreated(event.getPath());
406         }
407         break;
408       }
409 
410       case NodeDeleted: {
411         for(ZooKeeperListener listener : listeners) {
412           listener.nodeDeleted(event.getPath());
413         }
414         break;
415       }
416 
417       case NodeDataChanged: {
418         for(ZooKeeperListener listener : listeners) {
419           listener.nodeDataChanged(event.getPath());
420         }
421         break;
422       }
423 
424       case NodeChildrenChanged: {
425         for(ZooKeeperListener listener : listeners) {
426           listener.nodeChildrenChanged(event.getPath());
427         }
428         break;
429       }
430     }
431   }
432 
433   // Connection management
434 
435   /**
436    * Called when there is a connection-related event via the Watcher callback.
437    * <p>
438    * If Disconnected or Expired, this should shutdown the cluster. But, since
439    * we send a KeeperException.SessionExpiredException along with the abort
440    * call, it's possible for the Abortable to catch it and try to create a new
441    * session with ZooKeeper. This is what the client does in HCM.
442    * <p>
443    * @param event
444    */
445   private void connectionEvent(WatchedEvent event) {
446     switch(event.getState()) {
447       case SyncConnected:
448         // Now, this callback can be invoked before the this.zookeeper is set.
449         // Wait a little while.
450         long finished = System.currentTimeMillis() +
451           this.conf.getLong("hbase.zookeeper.watcher.sync.connected.wait", 2000);
452         while (System.currentTimeMillis() < finished) {
453           try {
454             Thread.sleep(1);
455           } catch (InterruptedException e) {
456             LOG.warn("Interrupted while sleeping");
457             throw new RuntimeException("Interrupted while waiting for" +
458                 " recoverableZooKeeper is set");
459           }
460           if (this.recoverableZooKeeper != null) break;
461         }
462 
463         if (this.recoverableZooKeeper == null) {
464           LOG.error("ZK is null on connection event -- see stack trace " +
465             "for the stack trace when constructor was called on this zkw",
466             this.constructorCaller);
467           throw new NullPointerException("ZK is null");
468         }
469         this.identifier = this.prefix + "-0x" +
470           Long.toHexString(this.recoverableZooKeeper.getSessionId());
471         // Update our identifier.  Otherwise ignore.
472         LOG.debug(this.identifier + " connected");
473         break;
474 
475       // Abort the server if Disconnected or Expired
476       case Disconnected:
477         LOG.debug(prefix("Received Disconnected from ZooKeeper, ignoring"));
478         break;
479 
480       case Expired:
481         String msg = prefix(this.identifier + " received expired from " +
482           "ZooKeeper, aborting");
483         // TODO: One thought is to add call to ZooKeeperListener so say,
484         // ZooKeeperNodeTracker can zero out its data values.
485         if (this.abortable != null) {
486           this.abortable.abort(msg, new KeeperException.SessionExpiredException());
487         }
488         break;
489 
490       case ConnectedReadOnly:
491       case SaslAuthenticated:
492       case AuthFailed:
493         break;
494 
495       default:
496         throw new IllegalStateException("Received event is not valid: " + event.getState());
497     }
498   }
499 
500   /**
501    * Forces a synchronization of this ZooKeeper client connection.
502    * <p>
503    * Executing this method before running other methods will ensure that the
504    * subsequent operations are up-to-date and consistent as of the time that
505    * the sync is complete.
506    * <p>
507    * This is used for compareAndSwap type operations where we need to read the
508    * data of an existing node and delete or transition that node, utilizing the
509    * previously read version and data.  We want to ensure that the version read
510    * is up-to-date from when we begin the operation.
511    */
512   public void sync(String path) throws KeeperException {
513     this.recoverableZooKeeper.sync(path, null, null);
514   }
515 
516   /**
517    * Handles KeeperExceptions in client calls.
518    * <p>
519    * This may be temporary but for now this gives one place to deal with these.
520    * <p>
521    * TODO: Currently this method rethrows the exception to let the caller handle
522    * <p>
523    * @param ke
524    * @throws KeeperException
525    */
526   public void keeperException(KeeperException ke)
527   throws KeeperException {
528     LOG.error(prefix("Received unexpected KeeperException, re-throwing exception"), ke);
529     throw ke;
530   }
531 
532   /**
533    * Handles InterruptedExceptions in client calls.
534    * <p>
535    * This may be temporary but for now this gives one place to deal with these.
536    * <p>
537    * TODO: Currently, this method does nothing.
538    *       Is this ever expected to happen?  Do we abort or can we let it run?
539    *       Maybe this should be logged as WARN?  It shouldn't happen?
540    * <p>
541    * @param ie
542    */
543   public void interruptedException(InterruptedException ie) {
544     LOG.debug(prefix("Received InterruptedException, doing nothing here"), ie);
545     // At least preserver interrupt.
546     Thread.currentThread().interrupt();
547     // no-op
548   }
549 
550   /**
551    * Close the connection to ZooKeeper.
552    *
553    * @throws InterruptedException
554    */
555   public void close() {
556     try {
557       if (recoverableZooKeeper != null) {
558         recoverableZooKeeper.close();
559       }
560     } catch (InterruptedException e) {
561       Thread.currentThread().interrupt();
562     }
563   }
564 
565   public Configuration getConfiguration() {
566     return conf;
567   }
568 
569   @Override
570   public void abort(String why, Throwable e) {
571     if (this.abortable != null) this.abortable.abort(why, e);
572     else this.aborted = true;
573   }
574 
575   @Override
576   public boolean isAborted() {
577     return this.abortable == null? this.aborted: this.abortable.isAborted();
578   }
579 
580   /**
581    * @return Path to the currently active master.
582    */
583   public String getMasterAddressZNode() {
584     return this.masterAddressZNode;
585   }
586 
587 }