@InterfaceAudience.Private public class MasterAddressTracker extends ZKNodeTracker
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.
Modifier and Type | Field and Description |
---|---|
private List<ServerName> |
backupMasters |
abortable, LOG, node
watcher
Constructor and Description |
---|
MasterAddressTracker(ZKWatcher watcher,
Abortable abortable)
Construct a master address listener with the specified
zookeeper reference. |
Modifier and Type | Method and Description |
---|---|
static boolean |
deleteIfEquals(ZKWatcher zkw,
String content)
delete the master znode if its content is same as the parameter
|
int |
getBackupMasterInfoPort(ServerName sn)
Get the info port of the backup master if it is available.
|
static int |
getBackupMasterInfoPort(ZKWatcher zkw,
ServerName sn)
Get backup master info port.
|
List<ServerName> |
getBackupMasters() |
static List<ServerName> |
getBackupMastersAndRenewWatch(ZKWatcher zkw)
Retrieves the list of registered backup masters and renews a watch on the znode for children
updates.
|
ServerName |
getMasterAddress()
Get the address of the current master if one is available.
|
ServerName |
getMasterAddress(boolean refresh)
Get the address of the current master if one is available.
|
static ServerName |
getMasterAddress(ZKWatcher zkw)
Get master address.
|
int |
getMasterInfoPort()
Get the info port of the current master of one is available.
|
static int |
getMasterInfoPort(ZKWatcher zkw)
Get master info port.
|
boolean |
hasMaster()
Check if there is a master available.
|
private void |
loadBackupMasters() |
void |
nodeChildrenChanged(String path)
Called when an existing node has a child node added or removed.
|
static org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos.Master |
parse(byte[] data) |
protected void |
postStart()
Called after start is called.
|
static boolean |
setMasterAddress(ZKWatcher zkw,
String znode,
ServerName master,
int infoPort)
Set master address into the
master znode or into the backup subdirectory of backup
masters; switch off the passed in znode path. |
(package private) static byte[] |
toByteArray(ServerName sn,
int infoPort) |
blockUntilAvailable, blockUntilAvailable, checkIfBaseNodeAvailable, getData, getNode, nodeCreated, nodeDataChanged, nodeDeleted, start, stop, toString
getWatcher
private volatile List<ServerName> backupMasters
public MasterAddressTracker(ZKWatcher watcher, Abortable abortable)
zookeeper
reference.
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.
watcher
- zk reference and watcherabortable
- abortable in case of fatal errorprivate void loadBackupMasters()
protected void postStart()
ZKNodeTracker
postStart
in class ZKNodeTracker
public void nodeChildrenChanged(String path)
ZKListener
nodeChildrenChanged
in class ZKListener
path
- full path of the node whose children have changedpublic ServerName getMasterAddress()
public int getMasterInfoPort()
public int getBackupMasterInfoPort(ServerName sn)
sn
- server name of backup masterpublic ServerName getMasterAddress(boolean refresh)
refresh
- whether to refresh the data by calling ZK directly.public static ServerName getMasterAddress(ZKWatcher zkw) throws org.apache.zookeeper.KeeperException, IOException
getMasterAddress()
if you do not have an
instance of this tracker in your context.zkw
- ZKWatcher to useorg.apache.zookeeper.KeeperException
- if a ZooKeeper operation failsIOException
- if the address of the ZooKeeper master cannot be retrievedpublic static int getMasterInfoPort(ZKWatcher zkw) throws org.apache.zookeeper.KeeperException, IOException
getMasterInfoPort()
if you do not have an
instance of this tracker in your context.zkw
- ZKWatcher to useorg.apache.zookeeper.KeeperException
- if a ZooKeeper operation failsIOException
- if the address of the ZooKeeper master cannot be retrievedpublic static int getBackupMasterInfoPort(ZKWatcher zkw, ServerName sn) throws org.apache.zookeeper.KeeperException, IOException
getBackupMasterInfoPort(ServerName)
if you do not have an instance of this tracker in your context.zkw
- ZKWatcher to usesn
- ServerName of the backup masterorg.apache.zookeeper.KeeperException
- if a ZooKeeper operation failsIOException
- if the address of the ZooKeeper master cannot be retrievedpublic static boolean setMasterAddress(ZKWatcher zkw, String znode, ServerName master, int infoPort) throws org.apache.zookeeper.KeeperException
master
znode or into the backup subdirectory of backup
masters; switch off the passed in znode
path.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.org.apache.zookeeper.KeeperException
- if a ZooKeeper operation failspublic boolean hasMaster()
static byte[] toByteArray(ServerName sn, int infoPort)
sn
- must not be nullpublic static org.apache.hadoop.hbase.shaded.protobuf.generated.ZooKeeperProtos.Master parse(byte[] data) throws DeserializationException
data
- zookeeper data. may be nullDeserializationException
- if the parsing failspublic static boolean deleteIfEquals(ZKWatcher zkw, String content)
zkw
- must not be nullcontent
- must not be nullpublic List<ServerName> getBackupMasters()
public static List<ServerName> getBackupMastersAndRenewWatch(ZKWatcher zkw) throws InterruptedIOException
zkw
- Zookeeper watcher to useInterruptedIOException
- if there is any issue fetching the required data from Zookeeper.Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.