Class MasterAddressTracker
java.lang.Object
org.apache.hadoop.hbase.zookeeper.ZKListener
org.apache.hadoop.hbase.zookeeper.ZKNodeTracker
org.apache.hadoop.hbase.zookeeper.MasterAddressTracker
Manages the location of the current active Master for the RegionServer.
Listens for ZooKeeper events related to the master address. The node /master will
contain the address of the current master. This listener is interested in
NodeDeleted and NodeCreated events on /master.
Utilizes ZKNodeTracker for zk interactions.
You can get the current master via getMasterAddress() or via
getMasterAddress(ZKWatcher) if you do not have a running instance of this Tracker in
your context.
This class also includes utility for interacting with the master znode, for writing and reading the znode content.
-
Field Summary
FieldsFields inherited from class org.apache.hadoop.hbase.zookeeper.ZKNodeTracker
abortable, LOG, nodeFields inherited from class org.apache.hadoop.hbase.zookeeper.ZKListener
watcher -
Constructor Summary
ConstructorsConstructorDescriptionMasterAddressTracker(ZKWatcher watcher, Abortable abortable) Construct a master address listener with the specifiedzookeeperreference. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleandeleteIfEquals(ZKWatcher zkw, String content) delete the master znode if its content is same as the parameterintGet the info port of the backup master if it is available.static intgetBackupMasterInfoPort(ZKWatcher zkw, ServerName sn) Get backup master info port.static List<ServerName>Retrieves the list of registered backup masters and renews a watch on the znode for children updates.Get the address of the current master if one is available.getMasterAddress(boolean refresh) Get the address of the current master if one is available.static ServerNameGet master address.intGet the info port of the current master of one is available.static intGet master info port.booleanCheck if there is a master available.private voidvoidnodeChildrenChanged(String path) Called when an existing node has a child node added or removed.static org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos.Masterparse(byte[] data) protected voidCalled after start is called.static booleansetMasterAddress(ZKWatcher zkw, String znode, ServerName master, int infoPort) Set master address into themasterznode or into the backup subdirectory of backup masters; switch off the passed inznodepath.(package private) static byte[]toByteArray(ServerName sn, int infoPort) Methods inherited from class org.apache.hadoop.hbase.zookeeper.ZKNodeTracker
blockUntilAvailable, blockUntilAvailable, checkIfBaseNodeAvailable, getData, getNode, nodeCreated, nodeDataChanged, nodeDeleted, start, stop, toStringMethods inherited from class org.apache.hadoop.hbase.zookeeper.ZKListener
getWatcher
-
Field Details
-
backupMasters
-
-
Constructor Details
-
MasterAddressTracker
Construct a master address listener with the specifiedzookeeperreference.This constructor does not trigger any actions, you must call methods explicitly. Normally you will just want to execute
ZKNodeTracker.start()to begin tracking of the master address.- Parameters:
watcher- zk reference and watcherabortable- abortable in case of fatal error
-
-
Method Details
-
loadBackupMasters
-
postStart
Description copied from class:ZKNodeTrackerCalled after start is called. Sub classes could implement this method to load more data on zk.- Overrides:
postStartin classZKNodeTracker
-
nodeChildrenChanged
Description copied from class:ZKListenerCalled when an existing node has a child node added or removed.- Overrides:
nodeChildrenChangedin classZKListener- Parameters:
path- full path of the node whose children have changed
-
getMasterAddress
Get the address of the current master if one is available. Returns null if no current master.- Returns:
- Server name or null if timed out.
-
getMasterInfoPort
Get the info port of the current master of one is available. Return 0 if no current master or zookeeper is unavailable- Returns:
- info port or 0 if timed out
-
getBackupMasterInfoPort
Get the info port of the backup master if it is available. Return 0 if no backup master or zookeeper is unavailable- Parameters:
sn- server name of backup master- Returns:
- info port or 0 if timed out or exceptions
-
getMasterAddress
Get the address of the current master if one is available. Returns null if no current master. If refresh is set, try to load the data from ZK again, otherwise, cached data will be used.- Parameters:
refresh- whether to refresh the data by calling ZK directly.- Returns:
- Server name or null if timed out.
-
getMasterAddress
public static ServerName getMasterAddress(ZKWatcher zkw) throws org.apache.zookeeper.KeeperException, IOException Get master address. Use this instead ofgetMasterAddress()if you do not have an instance of this tracker in your context.- Parameters:
zkw- ZKWatcher to use- Returns:
- ServerName stored in the the master address znode or null if no znode present.
- Throws:
org.apache.zookeeper.KeeperException- if a ZooKeeper operation failsIOException- if the address of the ZooKeeper master cannot be retrieved
-
getMasterInfoPort
public static int getMasterInfoPort(ZKWatcher zkw) throws org.apache.zookeeper.KeeperException, IOException Get master info port. Use this instead ofgetMasterInfoPort()if you do not have an instance of this tracker in your context.- Parameters:
zkw- ZKWatcher to use- Returns:
- master info port in the the master address znode or null if no znode present. // TODO can't return null for 'int' return type. non-static verison returns 0
- Throws:
org.apache.zookeeper.KeeperException- if a ZooKeeper operation failsIOException- if the address of the ZooKeeper master cannot be retrieved
-
getBackupMasterInfoPort
public static int getBackupMasterInfoPort(ZKWatcher zkw, ServerName sn) throws org.apache.zookeeper.KeeperException, IOException Get backup master info port. Use this instead ofgetBackupMasterInfoPort(ServerName)if you do not have an instance of this tracker in your context.- Parameters:
zkw- ZKWatcher to usesn- ServerName of the backup master- Returns:
- backup master info port in the the master address znode or 0 if no znode present.
- Throws:
org.apache.zookeeper.KeeperException- if a ZooKeeper operation failsIOException- if the address of the ZooKeeper master cannot be retrieved
-
setMasterAddress
public static boolean setMasterAddress(ZKWatcher zkw, String znode, ServerName master, int infoPort) throws org.apache.zookeeper.KeeperException Set master address into themasterznode or into the backup subdirectory of backup masters; switch off the passed inznodepath.- Parameters:
zkw- The ZKWatcher to use.znode- Where to create the znode; could be at the top level or it could be under backup mastersmaster- ServerName of the current master must not be null.- Returns:
- true if node created, false if not; a watch is set in both cases
- Throws:
org.apache.zookeeper.KeeperException- if a ZooKeeper operation fails
-
hasMaster
Check if there is a master available.- Returns:
- true if there is a master set, false if not.
-
toByteArray
- Parameters:
sn- must not be null- Returns:
- Content of the master znode as a serialized pb with the pb magic as prefix.
-
parse
public static org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos.Master parse(byte[] data) throws DeserializationException - Parameters:
data- zookeeper data. may be null- Returns:
- pb object of master, null if no active master
- Throws:
DeserializationException- if the parsing fails
-
deleteIfEquals
delete the master znode if its content is same as the parameter- Parameters:
zkw- must not be nullcontent- must not be null
-
getBackupMasters
-
getBackupMastersAndRenewWatch
public static List<ServerName> getBackupMastersAndRenewWatch(ZKWatcher zkw) throws InterruptedIOException Retrieves the list of registered backup masters and renews a watch on the znode for children updates.- Parameters:
zkw- Zookeeper watcher to use- Returns:
- List of backup masters.
- Throws:
InterruptedIOException- if there is any issue fetching the required data from Zookeeper.
-