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;
21  
22  import org.apache.hadoop.hbase.util.Bytes;
23  import org.apache.hadoop.hbase.zookeeper.ZooKeeperNodeTracker;
24  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
25  
26  /**
27   * Manages the location of the current active Master for this RegionServer.
28   * <p>
29   * Listens for ZooKeeper events related to the master address. The node
30   * <code>/master</code> will contain the address of the current master.
31   * This listener is interested in
32   * <code>NodeDeleted</code> and <code>NodeCreated</code> events on
33   * <code>/master</code>.
34   * <p>
35   * Utilizes {@link ZooKeeperNodeTracker} for zk interactions.
36   * <p>
37   * You can get the current master via {@link #getMasterAddress()}
38   */
39  public class MasterAddressTracker extends ZooKeeperNodeTracker {
40    /**
41     * Construct a master address listener with the specified
42     * <code>zookeeper</code> reference.
43     * <p>
44     * This constructor does not trigger any actions, you must call methods
45     * explicitly.  Normally you will just want to execute {@link #start()} to
46     * begin tracking of the master address.
47     *
48     * @param watcher zk reference and watcher
49     * @param abortable abortable in case of fatal error
50     */
51    public MasterAddressTracker(ZooKeeperWatcher watcher, Abortable abortable) {
52      super(watcher, watcher.masterAddressZNode, abortable);
53    }
54  
55    /**
56     * Get the address of the current master if one is available.  Returns null
57     * if no current master.
58     * @return Server name or null if timed out.
59     */
60    public ServerName getMasterAddress() {
61      return bytesToServerName(super.getData(false));
62    }
63  
64    /**
65     * Check if there is a master available.
66     * @return true if there is a master set, false if not.
67     */
68    public boolean hasMaster() {
69      return super.getData(false) != null;
70    }
71  
72    /**
73     * @param bytes Byte array of {@link ServerName#toString()}
74     * @return A {@link ServerName} instance.
75     */
76    private ServerName bytesToServerName(final byte [] bytes) {
77      return bytes == null ? null: ServerName.parseVersionedServerName(bytes);
78    }
79  }