View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.zookeeper;
21  
22  import java.io.IOException;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.concurrent.CopyOnWriteArrayList;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.Abortable;
32  import org.apache.hadoop.hbase.HConstants;
33  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
34  import org.apache.hadoop.hbase.util.Threads;
35  import org.apache.zookeeper.KeeperException;
36  import org.apache.zookeeper.WatchedEvent;
37  import org.apache.zookeeper.Watcher;
38  import org.apache.zookeeper.ZooKeeper;
39  
40  /**
41   * Acts as the single ZooKeeper Watcher.  One instance of this is instantiated
42   * for each Master, RegionServer, and client process.
43   *
44   * <p>This is the only class that implements {@link Watcher}.  Other internal
45   * classes which need to be notified of ZooKeeper events must register with
46   * the local instance of this watcher via {@link #registerListener}.
47   *
48   * <p>This class also holds and manages the connection to ZooKeeper.  Code to
49   * deal with connection related events and exceptions are handled here.
50   */
51  public class ZooKeeperWatcher implements Watcher, Abortable {
52    private static final Log LOG = LogFactory.getLog(ZooKeeperWatcher.class);
53  
54    // Identifiier for this watcher (for logging only).  Its made of the prefix
55    // passed on construction and the zookeeper sessionid.
56    private String identifier;
57  
58    // zookeeper quorum
59    private String quorum;
60  
61    // zookeeper connection
62    private ZooKeeper zooKeeper;
63  
64    // abortable in case of zk failure
65    private Abortable abortable;
66  
67    // listeners to be notified
68    private final List<ZooKeeperListener> listeners =
69      new CopyOnWriteArrayList<ZooKeeperListener>();
70  
71    // set of unassigned nodes watched
72    private Set<String> unassignedNodes = new HashSet<String>();
73  
74    // node names
75  
76    // base znode for this cluster
77    public String baseZNode;
78    // znode containing location of server hosting root region
79    public String rootServerZNode;
80    // znode containing ephemeral nodes of the regionservers
81    public String rsZNode;
82    // znode of currently active master
83    public String masterAddressZNode;
84    // znode containing the current cluster state
85    public String clusterStateZNode;
86    // znode used for region transitioning and assignment
87    public String assignmentZNode;
88    // znode used for table disabling/enabling
89    public String tableZNode;
90  
91    private final Configuration conf;
92  
93    private final Exception constructorCaller;
94  
95    /**
96     * Instantiate a ZooKeeper connection and watcher.
97     * @param descriptor Descriptive string that is added to zookeeper sessionid
98     * and used as identifier for this instance.
99     * @throws IOException
100    * @throws ZooKeeperConnectionException
101    */
102   public ZooKeeperWatcher(Configuration conf, String descriptor,
103       Abortable abortable)
104   throws IOException, ZooKeeperConnectionException {
105     this.conf = conf;
106     // Capture a stack trace now.  Will print it out later if problem so we can
107     // distingush amongst the myriad ZKWs.
108     try {
109       throw new Exception("ZKW CONSTRUCTOR STACK TRACE FOR DEBUGGING");
110     } catch (Exception e) {
111       this.constructorCaller = e;
112     }
113     this.quorum = ZKConfig.getZKQuorumServersString(conf);
114     // Identifier will get the sessionid appended later below down when we
115     // handle the syncconnect event.
116     this.identifier = descriptor;
117     this.abortable = abortable;
118     setNodeNames(conf);
119     this.zooKeeper = ZKUtil.connect(conf, quorum, this, descriptor);
120     try {
121       // Create all the necessary "directories" of znodes
122       // TODO: Move this to an init method somewhere so not everyone calls it?
123 
124       // The first call against zk can fail with connection loss.  Seems common.
125       // Apparently this is recoverable.  Retry a while.
126       // See http://wiki.apache.org/hadoop/ZooKeeper/ErrorHandling
127       // TODO: Generalize out in ZKUtil.
128       long wait = conf.getLong("hbase.zookeeper.recoverable.waittime", 10000);
129       long finished = System.currentTimeMillis() + wait;
130       KeeperException ke = null;
131       do {
132         try {
133           ZKUtil.createAndFailSilent(this, baseZNode);
134           ke = null;
135           break;
136         } catch (KeeperException.ConnectionLossException e) {
137           if (LOG.isDebugEnabled() && (isFinishedRetryingRecoverable(finished))) {
138             LOG.debug("Retrying zk create for another " +
139               (finished - System.currentTimeMillis()) +
140               "ms; set 'hbase.zookeeper.recoverable.waittime' to change " +
141               "wait time); " + e.getMessage());
142           }
143           ke = e;
144         }
145       } while (isFinishedRetryingRecoverable(finished));
146       // Convert connectionloss exception to ZKCE.
147       if (ke != null) throw new ZooKeeperConnectionException(ke);
148       ZKUtil.createAndFailSilent(this, assignmentZNode);
149       ZKUtil.createAndFailSilent(this, rsZNode);
150       ZKUtil.createAndFailSilent(this, tableZNode);
151     } catch (KeeperException e) {
152       LOG.error(prefix("Unexpected KeeperException creating base node"), e);
153       throw new IOException(e);
154     }
155   }
156 
157   private boolean isFinishedRetryingRecoverable(final long finished) {
158     return System.currentTimeMillis() < finished;
159   }
160 
161   @Override
162   public String toString() {
163     return this.identifier;
164   }
165 
166   /**
167    * Adds this instance's identifier as a prefix to the passed <code>str</code>
168    * @param str String to amend.
169    * @return A new string with this instance's identifier as prefix: e.g.
170    * if passed 'hello world', the returned string could be
171    */
172   public String prefix(final String str) {
173     return this.toString() + " " + str;
174   }
175 
176   /**
177    * Set the local variable node names using the specified configuration.
178    */
179   private void setNodeNames(Configuration conf) {
180     baseZNode = conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT,
181         HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
182     rootServerZNode = ZKUtil.joinZNode(baseZNode,
183         conf.get("zookeeper.znode.rootserver", "root-region-server"));
184     rsZNode = ZKUtil.joinZNode(baseZNode,
185         conf.get("zookeeper.znode.rs", "rs"));
186     masterAddressZNode = ZKUtil.joinZNode(baseZNode,
187         conf.get("zookeeper.znode.master", "master"));
188     clusterStateZNode = ZKUtil.joinZNode(baseZNode,
189         conf.get("zookeeper.znode.state", "shutdown"));
190     assignmentZNode = ZKUtil.joinZNode(baseZNode,
191         conf.get("zookeeper.znode.unassigned", "unassigned"));
192     tableZNode = ZKUtil.joinZNode(baseZNode,
193         conf.get("zookeeper.znode.tableEnableDisable", "table"));
194   }
195 
196   /**
197    * Register the specified listener to receive ZooKeeper events.
198    * @param listener
199    */
200   public void registerListener(ZooKeeperListener listener) {
201     listeners.add(listener);
202   }
203 
204   /**
205    * Register the specified listener to receive ZooKeeper events and add it as
206    * the first in the list of current listeners.
207    * @param listener
208    */
209   public void registerListenerFirst(ZooKeeperListener listener) {
210     listeners.add(0, listener);
211   }
212 
213   /**
214    * Get the connection to ZooKeeper.
215    * @return connection reference to zookeeper
216    */
217   public ZooKeeper getZooKeeper() {
218     return zooKeeper;
219   }
220 
221   /**
222    * Get the quorum address of this instance.
223    * @return quorum string of this zookeeper connection instance
224    */
225   public String getQuorum() {
226     return quorum;
227   }
228 
229   /**
230    * Method called from ZooKeeper for events and connection status.
231    *
232    * Valid events are passed along to listeners.  Connection status changes
233    * are dealt with locally.
234    */
235   @Override
236   public void process(WatchedEvent event) {
237     LOG.debug(prefix("Received ZooKeeper Event, " +
238         "type=" + event.getType() + ", " +
239         "state=" + event.getState() + ", " +
240         "path=" + event.getPath()));
241 
242     switch(event.getType()) {
243 
244       // If event type is NONE, this is a connection status change
245       case None: {
246         connectionEvent(event);
247         break;
248       }
249 
250       // Otherwise pass along to the listeners
251 
252       case NodeCreated: {
253         for(ZooKeeperListener listener : listeners) {
254           listener.nodeCreated(event.getPath());
255         }
256         break;
257       }
258 
259       case NodeDeleted: {
260         for(ZooKeeperListener listener : listeners) {
261           listener.nodeDeleted(event.getPath());
262         }
263         break;
264       }
265 
266       case NodeDataChanged: {
267         for(ZooKeeperListener listener : listeners) {
268           listener.nodeDataChanged(event.getPath());
269         }
270         break;
271       }
272 
273       case NodeChildrenChanged: {
274         for(ZooKeeperListener listener : listeners) {
275           listener.nodeChildrenChanged(event.getPath());
276         }
277         break;
278       }
279     }
280   }
281 
282   // Connection management
283 
284   /**
285    * Called when there is a connection-related event via the Watcher callback.
286    *
287    * If Disconnected or Expired, this should shutdown the cluster. But, since
288    * we send a KeeperException.SessionExpiredException along with the abort
289    * call, it's possible for the Abortable to catch it and try to create a new
290    * session with ZooKeeper. This is what the client does in HCM.
291    *
292    * @param event
293    */
294   private void connectionEvent(WatchedEvent event) {
295     switch(event.getState()) {
296       case SyncConnected:
297         // Now, this callback can be invoked before the this.zookeeper is set.
298         // Wait a little while.
299         long finished = System.currentTimeMillis() +
300           this.conf.getLong("hbase.zookeeper.watcher.sync.connected.wait", 2000);
301         while (System.currentTimeMillis() < finished) {
302           Threads.sleep(1);
303           if (this.zooKeeper != null) break;
304         }
305         if (this.zooKeeper == null) {
306           LOG.error("ZK is null on connection event -- see stack trace " +
307             "for the stack trace when constructor was called on this zkw",
308             this.constructorCaller);
309           throw new NullPointerException("ZK is null");
310         }
311         this.identifier = this.identifier + "-0x" +
312           Long.toHexString(this.zooKeeper.getSessionId());
313         // Update our identifier.  Otherwise ignore.
314         LOG.debug(this.identifier + " connected");
315         break;
316 
317       // Abort the server if Disconnected or Expired
318       // TODO: Åny reason to handle these two differently?
319       case Disconnected:
320         LOG.debug(prefix("Received Disconnected from ZooKeeper, ignoring"));
321         break;
322 
323       case Expired:
324         String msg = prefix(this.identifier + " received expired from " +
325           "ZooKeeper, aborting");
326         // TODO: One thought is to add call to ZooKeeperListener so say,
327         // ZooKeperNodeTracker can zero out its data values.
328         if (this.abortable != null) this.abortable.abort(msg,
329             new KeeperException.SessionExpiredException());
330         break;
331     }
332   }
333 
334   /**
335    * Forces a synchronization of this ZooKeeper client connection.
336    * <p>
337    * Executing this method before running other methods will ensure that the
338    * subsequent operations are up-to-date and consistent as of the time that
339    * the sync is complete.
340    * <p>
341    * This is used for compareAndSwap type operations where we need to read the
342    * data of an existing node and delete or transition that node, utilizing the
343    * previously read version and data.  We want to ensure that the version read
344    * is up-to-date from when we begin the operation.
345    */
346   public void sync(String path) {
347     this.zooKeeper.sync(path, null, null);
348   }
349 
350   /**
351    * Get the set of already watched unassigned nodes.
352    * @return Set of Nodes.
353    */
354   public Set<String> getNodes() {
355     return unassignedNodes;
356   }
357 
358   /**
359    * Handles KeeperExceptions in client calls.
360    *
361    * This may be temporary but for now this gives one place to deal with these.
362    *
363    * TODO: Currently this method rethrows the exception to let the caller handle
364    *
365    * @param ke
366    * @throws KeeperException
367    */
368   public void keeperException(KeeperException ke)
369   throws KeeperException {
370     LOG.error(prefix("Received unexpected KeeperException, re-throwing exception"), ke);
371     throw ke;
372   }
373 
374   /**
375    * Handles InterruptedExceptions in client calls.
376    *
377    * This may be temporary but for now this gives one place to deal with these.
378    *
379    * TODO: Currently, this method does nothing.
380    *       Is this ever expected to happen?  Do we abort or can we let it run?
381    *       Maybe this should be logged as WARN?  It shouldn't happen?
382    *
383    * @param ie
384    */
385   public void interruptedException(InterruptedException ie) {
386     LOG.debug(prefix("Received InterruptedException, doing nothing here"), ie);
387     // At least preserver interrupt.
388     Thread.currentThread().interrupt();
389     // no-op
390   }
391 
392   /**
393    * Close the connection to ZooKeeper.
394    * @throws InterruptedException
395    */
396   public void close() {
397     try {
398       if (zooKeeper != null) {
399         zooKeeper.close();
400 //        super.close();
401       }
402     } catch (InterruptedException e) {
403     }
404   }
405 
406   @Override
407   public void abort(String why, Throwable e) {
408     this.abortable.abort(why, e);
409   }
410 }