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