Class MasterAddressTracker


@Private public class MasterAddressTracker extends ZKNodeTracker
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 Details

  • Constructor Details

    • MasterAddressTracker

      public MasterAddressTracker(ZKWatcher watcher, Abortable abortable)
      Construct a master address listener with the specified 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.

      Parameters:
      watcher - zk reference and watcher
      abortable - abortable in case of fatal error
  • Method Details

    • loadBackupMasters

      private void loadBackupMasters()
    • postStart

      protected void postStart()
      Description copied from class: ZKNodeTracker
      Called after start is called. Sub classes could implement this method to load more data on zk.
      Overrides:
      postStart in class ZKNodeTracker
    • nodeChildrenChanged

      public void nodeChildrenChanged(String path)
      Description copied from class: ZKListener
      Called when an existing node has a child node added or removed.
      Overrides:
      nodeChildrenChanged in class ZKListener
      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

      public int 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

      public ServerName getMasterAddress(boolean refresh)
      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 of getMasterAddress() 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 fails
      IOException - 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 of getMasterInfoPort() 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 fails
      IOException - 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 of getBackupMasterInfoPort(ServerName) if you do not have an instance of this tracker in your context.
      Parameters:
      zkw - ZKWatcher to use
      sn - 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 fails
      IOException - 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 the master znode or into the backup subdirectory of backup masters; switch off the passed in znode path.
      Parameters:
      zkw - The ZKWatcher to use.
      znode - Where to create the znode; could be at the top level or it could be under backup masters
      master - 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

      public boolean hasMaster()
      Check if there is a master available.
      Returns:
      true if there is a master set, false if not.
    • toByteArray

      static byte[] toByteArray(ServerName sn, int infoPort)
      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

      public static boolean deleteIfEquals(ZKWatcher zkw, String content)
      delete the master znode if its content is same as the parameter
      Parameters:
      zkw - must not be null
      content - must not be null
    • getBackupMasters

    • getBackupMastersAndRenewWatch

      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.