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.master;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.hbase.HServerAddress;
25  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper;
26  import org.apache.zookeeper.WatchedEvent;
27  import org.apache.zookeeper.Watcher;
28  import org.apache.zookeeper.Watcher.Event.EventType;
29  
30  import java.util.concurrent.atomic.AtomicBoolean;
31  
32  
33  /**
34   * ZooKeeper watcher for the master address.  Also watches the cluster state
35   * flag so will shutdown this master if cluster has been shutdown.
36   * <p>Used by the Master.  Waits on the master address ZNode delete event.  When
37   * multiple masters are brought up, they race to become master by writing their
38   * address to ZooKeeper. Whoever wins becomes the master, and the rest wait for
39   * that ephemeral node in ZooKeeper to evaporate (meaning the master went down),
40   * at which point they try to write their own address to become the new master.
41   */
42  class ZKMasterAddressWatcher implements Watcher {
43    private static final Log LOG = LogFactory.getLog(ZKMasterAddressWatcher.class);
44  
45    private ZooKeeperWrapper zookeeper;
46    private final AtomicBoolean requestShutdown;
47  
48    /**
49     * Create this watcher using passed ZooKeeperWrapper instance.
50     * @param zk ZooKeeper
51     * @param flag Flag to set to request shutdown.
52     */
53    ZKMasterAddressWatcher(final ZooKeeperWrapper zk, final AtomicBoolean flag) {
54      this.requestShutdown = flag;
55      this.zookeeper = zk;
56    }
57  
58    /**
59     * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
60     */
61    @Override
62    public synchronized void process (WatchedEvent event) {
63      EventType type = event.getType();
64      LOG.debug(("Got event " + type + " with path " + event.getPath()));
65      if (type.equals(EventType.NodeDeleted)) {
66        if (event.getPath().equals(this.zookeeper.clusterStateZNode)) {
67          LOG.info("Cluster shutdown while waiting, shutting down" +
68            " this master.");
69          this.requestShutdown.set(true);
70        } else {
71          LOG.debug("Master address ZNode deleted, notifying waiting masters");
72          notifyAll();
73        }
74      } else if(type.equals(EventType.NodeCreated) &&
75          event.getPath().equals(this.zookeeper.clusterStateZNode)) {
76        LOG.debug("Resetting watch on cluster state node.");
77        this.zookeeper.setClusterStateWatch(this);
78      }
79    }
80  
81    /**
82     * Wait for master address to be available. This sets a watch in ZooKeeper and
83     * blocks until the master address ZNode gets deleted.
84     */
85    public synchronized void waitForMasterAddressAvailability() {
86      while (zookeeper.readMasterAddress(this) != null) {
87        try {
88          LOG.debug("Waiting for master address ZNode to be deleted " +
89            "(Also watching cluster state node)");
90          this.zookeeper.setClusterStateWatch(this);
91          wait();
92        } catch (InterruptedException e) {
93        }
94      }
95    }
96  
97    /**
98     * Write address to zookeeper.  Parks here until we successfully write our
99     * address (or until cluster shutdown).
100    * @param address Address whose format is HServerAddress.toString
101    */
102   boolean writeAddressToZooKeeper(
103       final HServerAddress address, boolean retry) {
104     do {
105       waitForMasterAddressAvailability();
106       // Check if we need to shutdown instead of taking control
107       if (this.requestShutdown.get()) {
108         LOG.debug("Won't start Master because cluster is shuting down");
109         return false;
110       }
111       if(this.zookeeper.writeMasterAddress(address)) {
112         this.zookeeper.setClusterState(true);
113         this.zookeeper.setClusterStateWatch(this);
114         // Watch our own node
115         this.zookeeper.readMasterAddress(this);
116         return true;
117       }
118     } while(retry);
119     return false;
120   }
121 
122   /**
123    * Reset the ZK in case a new connection is required
124    * @param zookeeper new instance
125    */
126   public void setZookeeper(ZooKeeperWrapper zookeeper) {
127     this.zookeeper = zookeeper;
128   }
129 }