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 org.apache.hadoop.hbase.Abortable; 23 import org.apache.hadoop.hbase.HServerAddress; 24 import org.apache.hadoop.hbase.catalog.RootLocationEditor; 25 import org.apache.hadoop.hbase.regionserver.RegionServerServices; 26 import org.apache.hadoop.hbase.util.Bytes; 27 28 /** 29 * Tracks the root region server location node in zookeeper. 30 * Root region location is set by {@link RootLocationEditor} usually called 31 * out of <code>RegionServerServices</code>. 32 * This class has a watcher on the root location and notices changes. 33 */ 34 public class RootRegionTracker extends ZooKeeperNodeTracker { 35 /** 36 * Creates a root region location tracker. 37 * 38 * <p>After construction, use {@link #start} to kick off tracking. 39 * 40 * @param watcher 41 * @param abortable 42 */ 43 public RootRegionTracker(ZooKeeperWatcher watcher, Abortable abortable) { 44 super(watcher, watcher.rootServerZNode, abortable); 45 } 46 47 /** 48 * Checks if the root region location is available. 49 * @return true if root region location is available, false if not 50 */ 51 public boolean isLocationAvailable() { 52 return super.getData() != null; 53 } 54 55 /** 56 * Gets the root region location, if available. Null if not. Does not block. 57 * @return server address for server hosting root region, null if none available 58 * @throws InterruptedException 59 */ 60 public HServerAddress getRootRegionLocation() throws InterruptedException { 61 return dataToHServerAddress(super.getData()); 62 } 63 64 /** 65 * Gets the root region location, if available, and waits for up to the 66 * specified timeout if not immediately available. 67 * @param timeout maximum time to wait, in millis 68 * @return server address for server hosting root region, null if timed out 69 * @throws InterruptedException if interrupted while waiting 70 */ 71 public HServerAddress waitRootRegionLocation(long timeout) 72 throws InterruptedException { 73 return dataToHServerAddress(super.blockUntilAvailable(timeout)); 74 } 75 76 /* 77 * @param data 78 * @return Returns null if <code>data</code> is null else converts passed data 79 * to an HServerAddress instance. 80 */ 81 private static HServerAddress dataToHServerAddress(final byte [] data) { 82 return data == null ? null: new HServerAddress(Bytes.toString(data)); 83 } 84 }