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 * 59 * @return server address of current active master, or null if none available 60 */ 61 public HServerAddress getMasterAddress() { 62 byte [] data = super.getData(); 63 return data == null ? null : new HServerAddress(Bytes.toString(data)); 64 } 65 66 /** 67 * Check if there is a master available. 68 * @return true if there is a master set, false if not. 69 */ 70 public boolean hasMaster() { 71 return super.getData() != null; 72 } 73 74 /** 75 * Get the address of the current master. If no master is available, method 76 * will block until one is available, the thread is interrupted, or timeout 77 * has passed. 78 * 79 * @param timeout maximum time to wait for master in millis, 0 for forever 80 * @return server address of current active master, null if timed out 81 * @throws InterruptedException if the thread is interrupted while waiting 82 */ 83 public synchronized HServerAddress waitForMaster(long timeout) 84 throws InterruptedException { 85 byte [] data = super.blockUntilAvailable(); 86 return data == null ? null : new HServerAddress(Bytes.toString(data)); 87 } 88 }