org.apache.hadoop.hbase.zookeeper
Class ZooKeeperWrapper

java.lang.Object
  extended by org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper
All Implemented Interfaces:
org.apache.zookeeper.Watcher

public class ZooKeeperWrapper
extends Object
implements org.apache.zookeeper.Watcher

Wraps a ZooKeeper instance and adds HBase specific functionality. This class provides methods to: - read/write/delete the root region location in ZooKeeper. - set/check out of safe mode flag. ------------------------------------------ The following STATIC ZNodes are created: ------------------------------------------ - parentZNode : All the HBase directories are hosted under this parent node, default = "/hbase" - rsZNode : This is the directory where the RS's create ephemeral nodes. The master watches these nodes, and their expiry indicates RS death. The default location is "/hbase/rs" ------------------------------------------ The following DYNAMIC ZNodes are created: ------------------------------------------ - rootRegionZNode : Specifies the RS hosting root. - masterElectionZNode : ZNode used for election of the primary master when there are secondaries. All the masters race to write their addresses into this location, the one that succeeds is the primary. Others block. - clusterStateZNode : Determines if the cluster is running. Its default location is "/hbase/shutdown". It always has a value of "up". If present with the valus, cluster is up and running. If deleted, the cluster is shutting down. - rgnsInTransitZNode : All the nodes under this node are names of regions in transition. The first byte of the data for each of these nodes is the event type. This is used to deserialize the rest of the data.


Nested Class Summary
static class ZooKeeperWrapper.ZNodePathAndData
           
 
Nested classes/interfaces inherited from interface org.apache.zookeeper.Watcher
org.apache.zookeeper.Watcher.Event
 
Field Summary
 String clusterStateZNode
           
protected static org.apache.commons.logging.Log LOG
           
 String parentZNode
           
 
Method Summary
 void clearRSDirectory()
          Method used to make sure the region server directory is empty.
 void close()
          Close this ZooKeeper session.
static ZooKeeperWrapper createInstance(org.apache.hadoop.conf.Configuration conf, String name)
           
 void createOrUpdateUnassignedRegion(String regionName, byte[] data)
          This method will create a new region in transition entry in ZK with the speficied data if none exists.
 String createZNodeIfNotExists(String zNodeName)
           
 String createZNodeIfNotExists(String zNodeName, byte[] data, org.apache.zookeeper.CreateMode createMode, boolean watch)
           
 boolean deleteRootRegionLocation()
          Delete ZNode containing root region location.
 void deleteUnassignedRegion(String regionName)
           
 void deleteZNode(String znode)
          Unrecursive deletion of specified znode
 void deleteZNode(String znode, boolean recursive)
          Optionnally recursive deletion of specified znode
 void deleteZNode(String zNodeName, int version)
           
 String dump()
           
 boolean ensureExists(String znode)
          Make sure this znode exists by creating it if it's missing
 boolean exists(String znode, boolean watch)
           
 byte[] getData(String parentZNode, String znode)
           
 byte[] getDataAndWatch(String parentZNode, String znode, org.apache.zookeeper.Watcher watcher)
           
static ZooKeeperWrapper getInstance(org.apache.hadoop.conf.Configuration conf, String name)
           
 String getMasterElectionZNode()
          Get the path of the masterElectionZNode
 String getParentZNode()
          Get the path of the parent ZNode
 String getQuorumServers()
           
 String getRegionInTransitionZNode()
          Get the znode that has all the regions in transition.
 int getRSDirectoryCount()
           
 String getRsZNode()
          Get the path of this region server's znode
 String[] getServerStats(String server)
          Gets the statistics from the given server.
 String[] getServerStats(String server, int timeout)
          Gets the statistics from the given server.
 long getSessionID()
          This is for testing KeeperException.SessionExpiredException.
 byte[] getSessionPassword()
          This is for testing KeeperException.SessionExpiredException.
 String getZNode(String parentZNode, String znodeName)
           
 String getZNodePathForHBase(String znodeName)
           
 org.apache.zookeeper.ZooKeeper getZooKeeper()
           
static String getZookeeperClusterKey(org.apache.hadoop.conf.Configuration conf)
          Get the key to the ZK ensemble for this configuration without adding a name at the end
static String getZookeeperClusterKey(org.apache.hadoop.conf.Configuration conf, String name)
          Get the key to the ZK ensemble for this configuration and append a name at the end
 boolean isConnected()
           
 List<String> listZnodes(String znode)
          List all znodes in the specified path
 List<String> listZnodes(String znode, org.apache.zookeeper.Watcher watcher)
          List all znodes in the specified path and set a watcher on each
 void process(org.apache.zookeeper.WatchedEvent event)
          This is the primary ZK watcher
 HServerAddress readMasterAddress(org.apache.zookeeper.Watcher watcher)
          Read master address and set a watch on it.
 HServerAddress readMasterAddressOrThrow()
          Read address of master server.
 HServerAddress readRootRegionLocation()
          Read location of server storing root region.
 byte[] readZNode(String znodeName, org.apache.zookeeper.data.Stat stat)
           
 void reconnectToZk()
           
 void registerListener(org.apache.zookeeper.Watcher watcher)
           
 List<HServerAddress> scanAddressDirectory(String znode, org.apache.zookeeper.Watcher watcher)
          Scan a directory of address data.
 List<HServerAddress> scanRSDirectory()
          Scans the regions servers directory
 List<HServerAddress> scanRSDirectory(org.apache.zookeeper.Watcher watcher)
          Scans the regions servers directory and sets a watch on each znode
 boolean setClusterState(boolean up)
          Set the cluster state, up or down
 void setClusterStateWatch()
          Watch the state of the cluster, up or down
 void unregisterListener(org.apache.zookeeper.Watcher watcher)
           
 boolean updateRSLocationGetWatch(HServerInfo info, org.apache.zookeeper.Watcher watcher)
          Update the RS address and set a watcher on the znode
 void updateUnassignedRegion(String regionName, byte[] data)
          Given a region name and some data, this method updates the region znode data under the UNASSGINED znode with the latest data.
 List<ZooKeeperWrapper.ZNodePathAndData> watchAndGetNewChildren(String znode)
          Atomically adds a watch and reads data from the unwatched znodes in the UNASSGINED region.
 boolean watchMasterAddress(org.apache.zookeeper.Watcher watcher)
          Set a watcher on the master address ZNode.
 void watchZNode(String zNodeName)
           
 boolean writeMasterAddress(HServerAddress address)
          Write address of master to ZooKeeper.
 boolean writeRootRegionLocation(HServerAddress address)
          Write root region location to ZooKeeper.
 boolean writeRSLocation(HServerInfo info)
          Write in ZK this RS startCode and address.
 boolean writeZNode(String znodeName, byte[] data, int version, boolean watch)
           
 void writeZNode(String parentPath, String child, String strData)
          Write a znode and fail if it already exists
 void writeZNode(String parentPath, String child, String strData, boolean failOnWrite)
          Write (and optionally over-write) a znode
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

protected static final org.apache.commons.logging.Log LOG

parentZNode

public final String parentZNode

clusterStateZNode

public final String clusterStateZNode
Method Detail

getInstance

public static ZooKeeperWrapper getInstance(org.apache.hadoop.conf.Configuration conf,
                                           String name)

createInstance

public static ZooKeeperWrapper createInstance(org.apache.hadoop.conf.Configuration conf,
                                              String name)

reconnectToZk

public void reconnectToZk()
                   throws IOException
Throws:
IOException

registerListener

public void registerListener(org.apache.zookeeper.Watcher watcher)

unregisterListener

public void unregisterListener(org.apache.zookeeper.Watcher watcher)

process

public void process(org.apache.zookeeper.WatchedEvent event)
This is the primary ZK watcher

Specified by:
process in interface org.apache.zookeeper.Watcher
See Also:
Watcher.process(org.apache.zookeeper.WatchedEvent)

dump

public String dump()
Returns:
String dump of everything in ZooKeeper.

getServerStats

public String[] getServerStats(String server)
                        throws IOException
Gets the statistics from the given server. Uses a 1 minute timeout.

Parameters:
server - The server to get the statistics from.
Returns:
The array of response strings.
Throws:
IOException - When the socket communication fails.

getServerStats

public String[] getServerStats(String server,
                               int timeout)
                        throws IOException
Gets the statistics from the given server.

Parameters:
server - The server to get the statistics from.
timeout - The socket timeout to use.
Returns:
The array of response strings.
Throws:
IOException - When the socket communication fails.

exists

public boolean exists(String znode,
                      boolean watch)

getZooKeeper

public org.apache.zookeeper.ZooKeeper getZooKeeper()
Returns:
ZooKeeper used by this wrapper.

getSessionID

public long getSessionID()
This is for testing KeeperException.SessionExpiredException. See HBASE-1232.

Returns:
long session ID of this ZooKeeper session.

getSessionPassword

public byte[] getSessionPassword()
This is for testing KeeperException.SessionExpiredException. See HBASE-1232.

Returns:
byte[] password of this ZooKeeper session.

getQuorumServers

public String getQuorumServers()
Returns:
host:port list of quorum servers.

isConnected

public boolean isConnected()
Returns:
true if currently connected to ZooKeeper, false otherwise.

readRootRegionLocation

public HServerAddress readRootRegionLocation()
Read location of server storing root region.

Returns:
HServerAddress pointing to server serving root region or null if there was a problem reading the ZNode.

readMasterAddressOrThrow

public HServerAddress readMasterAddressOrThrow()
                                        throws IOException
Read address of master server.

Returns:
HServerAddress of master server.
Throws:
IOException - if there's a problem reading the ZNode.

readMasterAddress

public HServerAddress readMasterAddress(org.apache.zookeeper.Watcher watcher)
Read master address and set a watch on it.

Parameters:
watcher - Watcher to set on master address ZNode if not null.
Returns:
HServerAddress of master or null if there was a problem reading the ZNode. The watcher is set only if the result is not null.

setClusterStateWatch

public void setClusterStateWatch()
Watch the state of the cluster, up or down

Parameters:
watcher - Watcher to set on cluster state node

setClusterState

public boolean setClusterState(boolean up)
Set the cluster state, up or down

Parameters:
up - True to write the node, false to delete it
Returns:
true if it worked, else it's false

watchMasterAddress

public boolean watchMasterAddress(org.apache.zookeeper.Watcher watcher)
Set a watcher on the master address ZNode. The watcher will be set unless an exception occurs with ZooKeeper.

Parameters:
watcher - Watcher to set on master address ZNode.
Returns:
true if watcher was set, false otherwise.

ensureExists

public boolean ensureExists(String znode)
Make sure this znode exists by creating it if it's missing

Parameters:
znode - full path to znode
Returns:
true if it works

deleteRootRegionLocation

public boolean deleteRootRegionLocation()
Delete ZNode containing root region location.

Returns:
true if operation succeeded, false otherwise.

deleteZNode

public void deleteZNode(String znode)
                 throws org.apache.zookeeper.KeeperException,
                        InterruptedException
Unrecursive deletion of specified znode

Parameters:
znode -
Throws:
org.apache.zookeeper.KeeperException
InterruptedException

deleteZNode

public void deleteZNode(String znode,
                        boolean recursive)
                 throws org.apache.zookeeper.KeeperException,
                        InterruptedException
Optionnally recursive deletion of specified znode

Parameters:
znode -
recursive -
Throws:
org.apache.zookeeper.KeeperException
InterruptedException

writeRootRegionLocation

public boolean writeRootRegionLocation(HServerAddress address)
Write root region location to ZooKeeper. If address is null, delete ZNode. containing root region location.

Parameters:
address - HServerAddress to write to ZK.
Returns:
true if operation succeeded, false otherwise.

writeMasterAddress

public boolean writeMasterAddress(HServerAddress address)
Write address of master to ZooKeeper.

Parameters:
address - HServerAddress of master.
Returns:
true if operation succeeded, false otherwise.

writeRSLocation

public boolean writeRSLocation(HServerInfo info)
Write in ZK this RS startCode and address. Ensures that the full path exists.

Parameters:
info - The RS info
Returns:
true if the location was written, false if it failed

updateRSLocationGetWatch

public boolean updateRSLocationGetWatch(HServerInfo info,
                                        org.apache.zookeeper.Watcher watcher)
Update the RS address and set a watcher on the znode

Parameters:
info - The RS info
watcher - The watcher to put on the znode
Returns:
true if the update is done, false if it failed

scanRSDirectory

public List<HServerAddress> scanRSDirectory()
Scans the regions servers directory

Returns:
A list of server addresses

scanRSDirectory

public List<HServerAddress> scanRSDirectory(org.apache.zookeeper.Watcher watcher)
Scans the regions servers directory and sets a watch on each znode

Parameters:
watcher - a watch to use for each znode
Returns:
A list of server addresses

clearRSDirectory

public void clearRSDirectory()
Method used to make sure the region server directory is empty.


getRSDirectoryCount

public int getRSDirectoryCount()
Returns:
the number of region server znodes in the RS directory

close

public void close()
Close this ZooKeeper session.


getZNode

public String getZNode(String parentZNode,
                       String znodeName)

getZNodePathForHBase

public String getZNodePathForHBase(String znodeName)

getMasterElectionZNode

public String getMasterElectionZNode()
Get the path of the masterElectionZNode

Returns:
the path to masterElectionZNode

getParentZNode

public String getParentZNode()
Get the path of the parent ZNode

Returns:
path of that znode

scanAddressDirectory

public List<HServerAddress> scanAddressDirectory(String znode,
                                                 org.apache.zookeeper.Watcher watcher)
Scan a directory of address data.

Parameters:
znode - The parent node
watcher - The watcher to put on the found znodes, if not null
Returns:
The directory contents

listZnodes

public List<String> listZnodes(String znode)
List all znodes in the specified path

Parameters:
znode - path to list
Returns:
a list of all the znodes

listZnodes

public List<String> listZnodes(String znode,
                               org.apache.zookeeper.Watcher watcher)
List all znodes in the specified path and set a watcher on each

Parameters:
znode - path to list
watcher - watch to set, can be null
Returns:
a list of all the znodes

getData

public byte[] getData(String parentZNode,
                      String znode)

getDataAndWatch

public byte[] getDataAndWatch(String parentZNode,
                              String znode,
                              org.apache.zookeeper.Watcher watcher)

writeZNode

public void writeZNode(String parentPath,
                       String child,
                       String strData)
                throws InterruptedException,
                       org.apache.zookeeper.KeeperException
Write a znode and fail if it already exists

Parameters:
parentPath - parent path to the new znode
child - name of the znode
strData - data to insert
Throws:
InterruptedException
org.apache.zookeeper.KeeperException

writeZNode

public void writeZNode(String parentPath,
                       String child,
                       String strData,
                       boolean failOnWrite)
                throws InterruptedException,
                       org.apache.zookeeper.KeeperException
Write (and optionally over-write) a znode

Parameters:
parentPath - parent path to the new znode
child - name of the znode
strData - data to insert
failOnWrite - true if an exception should be returned if the znode already exists, false if it should be overwritten
Throws:
InterruptedException
org.apache.zookeeper.KeeperException

getZookeeperClusterKey

public static String getZookeeperClusterKey(org.apache.hadoop.conf.Configuration conf)
Get the key to the ZK ensemble for this configuration without adding a name at the end

Parameters:
conf - Configuration to use to build the key
Returns:
ensemble key without a name

getZookeeperClusterKey

public static String getZookeeperClusterKey(org.apache.hadoop.conf.Configuration conf,
                                            String name)
Get the key to the ZK ensemble for this configuration and append a name at the end

Parameters:
conf - Configuration to use to build the key
name - Name that should be appended at the end if not empty or null
Returns:
ensemble key with a name (if any)

getRegionInTransitionZNode

public String getRegionInTransitionZNode()
Get the znode that has all the regions in transition.

Returns:
path to znode

getRsZNode

public String getRsZNode()
Get the path of this region server's znode

Returns:
path to znode

deleteZNode

public void deleteZNode(String zNodeName,
                        int version)

createZNodeIfNotExists

public String createZNodeIfNotExists(String zNodeName)

watchZNode

public void watchZNode(String zNodeName)

createZNodeIfNotExists

public String createZNodeIfNotExists(String zNodeName,
                                     byte[] data,
                                     org.apache.zookeeper.CreateMode createMode,
                                     boolean watch)

readZNode

public byte[] readZNode(String znodeName,
                        org.apache.zookeeper.data.Stat stat)
                 throws IOException
Throws:
IOException

writeZNode

public boolean writeZNode(String znodeName,
                          byte[] data,
                          int version,
                          boolean watch)
                   throws IOException
Throws:
IOException

updateUnassignedRegion

public void updateUnassignedRegion(String regionName,
                                   byte[] data)
Given a region name and some data, this method updates the region znode data under the UNASSGINED znode with the latest data. This method will update the znode data only if it already exists.

Parameters:
regionName - - encoded name of the region
data - - new serialized data to update the region znode

createOrUpdateUnassignedRegion

public void createOrUpdateUnassignedRegion(String regionName,
                                           byte[] data)
This method will create a new region in transition entry in ZK with the speficied data if none exists. If one already exists, it will update the data with whatever is passed in.

Parameters:
regionName - - encoded name of the region
data - - serialized data for the region znode

deleteUnassignedRegion

public void deleteUnassignedRegion(String regionName)

watchAndGetNewChildren

public List<ZooKeeperWrapper.ZNodePathAndData> watchAndGetNewChildren(String znode)
Atomically adds a watch and reads data from the unwatched znodes in the UNASSGINED region. This works because the master is the only person deleting nodes.

Parameters:
znode -
Returns:


Copyright © 2010 Apache Software Foundation. All Rights Reserved.