org.apache.hadoop.hbase.zookeeper
Class ZKAssign

java.lang.Object
  extended by org.apache.hadoop.hbase.zookeeper.ZKAssign

public class ZKAssign
extends Object

Utility class for doing region assignment in ZooKeeper. This class extends stuff done in ZKUtil to cover specific assignment operations.

Contains only static methods and constants.

Used by both the Master and RegionServer.

All valid transitions outlined below:

MASTER

  1. Master creates an unassigned node as OFFLINE. - Cluster startup and table enabling.
  2. Master forces an existing unassigned node to OFFLINE. - RegionServer failure. - Allows transitions from all states to OFFLINE.
  3. Master deletes an unassigned node that was in a OPENED state. - Normal region transitions. Besides cluster startup, no other deletions of unassigned nodes is allowed.
  4. Master deletes all unassigned nodes regardless of state. - Cluster startup before any assignment happens.

REGIONSERVER

  1. RegionServer creates an unassigned node as CLOSING. - All region closes will do this in response to a CLOSE RPC from Master. - A node can never be transitioned to CLOSING, only created.
  2. RegionServer transitions an unassigned node from CLOSING to CLOSED. - Normal region closes. CAS operation.
  3. RegionServer transitions an unassigned node from OFFLINE to OPENING. - All region opens will do this in response to an OPEN RPC from the Master. - Normal region opens. CAS operation.
  4. RegionServer transitions an unassigned node from OPENING to OPENED. - Normal region opens. CAS operation.


Constructor Summary
ZKAssign()
           
 
Method Summary
static void asyncCreateNodeOffline(ZooKeeperWatcher zkw, HRegionInfo region, String serverName, org.apache.zookeeper.AsyncCallback.StringCallback cb, Object ctx)
          Creates an unassigned node in the OFFLINE state for the specified region.
static void blockUntilNoRIT(ZooKeeperWatcher zkw)
          Blocks until there are no node in regions in transition.
static void blockUntilRIT(ZooKeeperWatcher zkw)
          Blocks until there is at least one node in regions in transition.
static int createNodeClosing(ZooKeeperWatcher zkw, HRegionInfo region, String serverName)
          Creates a new unassigned node in the CLOSING state for the specified region.
static void createNodeOffline(ZooKeeperWatcher zkw, HRegionInfo region, String serverName)
          Creates a new unassigned node in the OFFLINE state for the specified region.
static void createNodeOffline(ZooKeeperWatcher zkw, HRegionInfo region, String serverName, EventHandler.EventType event)
           
static boolean createOrForceNodeOffline(ZooKeeperWatcher zkw, HRegionInfo region, String serverName)
          Creates or force updates an unassigned node to the OFFLINE state for the specified region.
static void deleteAllNodes(ZooKeeperWatcher zkw)
          Deletes all unassigned nodes regardless of their state.
static boolean deleteClosedNode(ZooKeeperWatcher zkw, String regionName)
          Deletes an existing unassigned node that is in the CLOSED state for the specified region.
static boolean deleteClosingNode(ZooKeeperWatcher zkw, HRegionInfo region)
          Deletes an existing unassigned node that is in the CLOSING state for the specified region.
static void deleteNodeFailSilent(ZooKeeperWatcher watcher, HRegionInfo regionInfo)
          Delete the assignment node regardless of its current state.
static boolean deleteOfflineNode(ZooKeeperWatcher zkw, String regionName)
          Deletes an existing unassigned node that is in the OFFLINE state for the specified region.
static boolean deleteOpenedNode(ZooKeeperWatcher zkw, String regionName)
          Deletes an existing unassigned node that is in the OPENED state for the specified region.
static void forceNodeOffline(ZooKeeperWatcher zkw, HRegionInfo region, String serverName)
          Forces an existing unassigned node to the OFFLINE state for the specified region.
static RegionTransitionData getData(ZooKeeperWatcher zkw, String pathOrRegionName)
          Gets the current data in the unassigned node for the specified region name or fully-qualified path.
static RegionTransitionData getDataNoWatch(ZooKeeperWatcher zkw, String pathOrRegionName, org.apache.zookeeper.data.Stat stat)
          Gets the current data in the unassigned node for the specified region name or fully-qualified path.
static String getNodeName(ZooKeeperWatcher zkw, String regionName)
          Gets the full path node name for the unassigned node for the specified region.
static String getRegionName(ZooKeeperWatcher zkw, String path)
          Gets the region name from the full path node name of an unassigned node.
static int retransitionNodeOpening(ZooKeeperWatcher zkw, HRegionInfo region, String serverName, int expectedVersion)
          Retransitions an existing unassigned node for the specified region which is currently in the OPENING state to be in the OPENING state.
static int transitionNode(ZooKeeperWatcher zkw, HRegionInfo region, String serverName, EventHandler.EventType beginState, EventHandler.EventType endState, int expectedVersion)
          Private method that actually performs unassigned node transitions.
static int transitionNodeClosed(ZooKeeperWatcher zkw, HRegionInfo region, String serverName, int expectedVersion)
          Transitions an existing unassigned node for the specified region which is currently in the CLOSING state to be in the CLOSED state.
static int transitionNodeOpened(ZooKeeperWatcher zkw, HRegionInfo region, String serverName, int expectedVersion)
          Transitions an existing unassigned node for the specified region which is currently in the OPENING state to be in the OPENED state.
static int transitionNodeOpening(ZooKeeperWatcher zkw, HRegionInfo region, String serverName)
          Transitions an existing unassigned node for the specified region which is currently in the OFFLINE state to be in the OPENING state.
static int transitionNodeOpening(ZooKeeperWatcher zkw, HRegionInfo region, String serverName, EventHandler.EventType beginState)
           
static boolean verifyRegionState(ZooKeeperWatcher zkw, HRegionInfo region, EventHandler.EventType expectedState)
          Verifies that the specified region is in the specified state in ZooKeeper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ZKAssign

public ZKAssign()
Method Detail

getNodeName

public static String getNodeName(ZooKeeperWatcher zkw,
                                 String regionName)
Gets the full path node name for the unassigned node for the specified region.

Parameters:
zkw - zk reference
regionName - region name
Returns:
full path node name

getRegionName

public static String getRegionName(ZooKeeperWatcher zkw,
                                   String path)
Gets the region name from the full path node name of an unassigned node.

Parameters:
path - full zk path
Returns:
region name

createNodeOffline

public static void createNodeOffline(ZooKeeperWatcher zkw,
                                     HRegionInfo region,
                                     String serverName)
                              throws org.apache.zookeeper.KeeperException,
                                     org.apache.zookeeper.KeeperException.NodeExistsException
Creates a new unassigned node in the OFFLINE state for the specified region.

Does not transition nodes from other states. If a node already exists for this region, a KeeperException.NodeExistsException will be thrown.

Sets a watcher on the unassigned region node if the method is successful.

This method should only be used during cluster startup and the enabling of a table.

Parameters:
zkw - zk reference
region - region to be created as offline
serverName - server event originates from
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NodeExistsException - if node already exists

createNodeOffline

public static void createNodeOffline(ZooKeeperWatcher zkw,
                                     HRegionInfo region,
                                     String serverName,
                                     EventHandler.EventType event)
                              throws org.apache.zookeeper.KeeperException,
                                     org.apache.zookeeper.KeeperException.NodeExistsException
Throws:
org.apache.zookeeper.KeeperException
org.apache.zookeeper.KeeperException.NodeExistsException

asyncCreateNodeOffline

public static void asyncCreateNodeOffline(ZooKeeperWatcher zkw,
                                          HRegionInfo region,
                                          String serverName,
                                          org.apache.zookeeper.AsyncCallback.StringCallback cb,
                                          Object ctx)
                                   throws org.apache.zookeeper.KeeperException
Creates an unassigned node in the OFFLINE state for the specified region.

Runs asynchronously. Depends on no pre-existing znode.

Sets a watcher on the unassigned region node.

Parameters:
zkw - zk reference
region - region to be created as offline
serverName - server event originates from
cb -
ctx -
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NodeExistsException - if node already exists

forceNodeOffline

public static void forceNodeOffline(ZooKeeperWatcher zkw,
                                    HRegionInfo region,
                                    String serverName)
                             throws org.apache.zookeeper.KeeperException,
                                    org.apache.zookeeper.KeeperException.NoNodeException
Forces an existing unassigned node to the OFFLINE state for the specified region.

Does not create a new node. If a node does not already exist for this region, a KeeperException.NoNodeException will be thrown.

Sets a watcher on the unassigned region node if the method is successful.

This method should only be used during recovery of regionserver failure.

Parameters:
zkw - zk reference
region - region to be forced as offline
serverName - server event originates from
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NoNodeException - if node does not exist

createOrForceNodeOffline

public static boolean createOrForceNodeOffline(ZooKeeperWatcher zkw,
                                               HRegionInfo region,
                                               String serverName)
                                        throws org.apache.zookeeper.KeeperException
Creates or force updates an unassigned node to the OFFLINE state for the specified region.

Attempts to create the node but if it exists will force it to transition to and OFFLINE state.

Sets a watcher on the unassigned region node if the method is successful.

This method should be used when assigning a region.

Parameters:
zkw - zk reference
region - region to be created as offline
serverName - server event originates from
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NodeExistsException - if node already exists

deleteOpenedNode

public static boolean deleteOpenedNode(ZooKeeperWatcher zkw,
                                       String regionName)
                                throws org.apache.zookeeper.KeeperException,
                                       org.apache.zookeeper.KeeperException.NoNodeException
Deletes an existing unassigned node that is in the OPENED state for the specified region.

If a node does not already exist for this region, a KeeperException.NoNodeException will be thrown.

No watcher is set whether this succeeds or not.

Returns false if the node was not in the proper state but did exist.

This method is used during normal region transitions when a region finishes successfully opening. This is the Master acknowledging completion of the specified regions transition.

Parameters:
zkw - zk reference
regionName - opened region to be deleted from zk
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NoNodeException - if node does not exist

deleteOfflineNode

public static boolean deleteOfflineNode(ZooKeeperWatcher zkw,
                                        String regionName)
                                 throws org.apache.zookeeper.KeeperException,
                                        org.apache.zookeeper.KeeperException.NoNodeException
Deletes an existing unassigned node that is in the OFFLINE state for the specified region.

If a node does not already exist for this region, a KeeperException.NoNodeException will be thrown.

No watcher is set whether this succeeds or not.

Returns false if the node was not in the proper state but did exist.

This method is used during master failover when the regions on an RS that has died are all set to OFFLINE before being processed.

Parameters:
zkw - zk reference
regionName - closed region to be deleted from zk
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NoNodeException - if node does not exist

deleteClosedNode

public static boolean deleteClosedNode(ZooKeeperWatcher zkw,
                                       String regionName)
                                throws org.apache.zookeeper.KeeperException,
                                       org.apache.zookeeper.KeeperException.NoNodeException
Deletes an existing unassigned node that is in the CLOSED state for the specified region.

If a node does not already exist for this region, a KeeperException.NoNodeException will be thrown.

No watcher is set whether this succeeds or not.

Returns false if the node was not in the proper state but did exist.

This method is used during table disables when a region finishes successfully closing. This is the Master acknowledging completion of the specified regions transition to being closed.

Parameters:
zkw - zk reference
regionName - closed region to be deleted from zk
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NoNodeException - if node does not exist

deleteClosingNode

public static boolean deleteClosingNode(ZooKeeperWatcher zkw,
                                        HRegionInfo region)
                                 throws org.apache.zookeeper.KeeperException,
                                        org.apache.zookeeper.KeeperException.NoNodeException
Deletes an existing unassigned node that is in the CLOSING state for the specified region.

If a node does not already exist for this region, a KeeperException.NoNodeException will be thrown.

No watcher is set whether this succeeds or not.

Returns false if the node was not in the proper state but did exist.

This method is used during table disables when a region finishes successfully closing. This is the Master acknowledging completion of the specified regions transition to being closed.

Parameters:
zkw - zk reference
region - closing region to be deleted from zk
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NoNodeException - if node does not exist

deleteAllNodes

public static void deleteAllNodes(ZooKeeperWatcher zkw)
                           throws org.apache.zookeeper.KeeperException
Deletes all unassigned nodes regardless of their state.

No watchers are set.

This method is used by the Master during cluster startup to clear out any existing state from other cluster runs.

Parameters:
zkw - zk reference
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

createNodeClosing

public static int createNodeClosing(ZooKeeperWatcher zkw,
                                    HRegionInfo region,
                                    String serverName)
                             throws org.apache.zookeeper.KeeperException,
                                    org.apache.zookeeper.KeeperException.NodeExistsException
Creates a new unassigned node in the CLOSING state for the specified region.

Does not transition nodes from any states. If a node already exists for this region, a KeeperException.NodeExistsException will be thrown.

If creation is successful, returns the version number of the CLOSING node created.

Does not set any watches.

This method should only be used by a RegionServer when initiating a close of a region after receiving a CLOSE RPC from the Master.

Parameters:
zkw - zk reference
region - region to be created as closing
serverName - server event originates from
Returns:
version of node after transition, -1 if unsuccessful transition
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception
KeeperException.NodeExistsException - if node already exists

transitionNodeClosed

public static int transitionNodeClosed(ZooKeeperWatcher zkw,
                                       HRegionInfo region,
                                       String serverName,
                                       int expectedVersion)
                                throws org.apache.zookeeper.KeeperException
Transitions an existing unassigned node for the specified region which is currently in the CLOSING state to be in the CLOSED state.

Does not transition nodes from other states. If for some reason the node could not be transitioned, the method returns -1. If the transition is successful, the version of the node after transition is returned.

This method can fail and return false for three different reasons:

Does not set any watches.

This method should only be used by a RegionServer when initiating a close of a region after receiving a CLOSE RPC from the Master.

Parameters:
zkw - zk reference
region - region to be transitioned to closed
serverName - server event originates from
Returns:
version of node after transition, -1 if unsuccessful transition
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

transitionNodeOpening

public static int transitionNodeOpening(ZooKeeperWatcher zkw,
                                        HRegionInfo region,
                                        String serverName)
                                 throws org.apache.zookeeper.KeeperException
Transitions an existing unassigned node for the specified region which is currently in the OFFLINE state to be in the OPENING state.

Does not transition nodes from other states. If for some reason the node could not be transitioned, the method returns -1. If the transition is successful, the version of the node written as OPENING is returned.

This method can fail and return -1 for three different reasons:

Does not set any watches.

This method should only be used by a RegionServer when initiating an open of a region after receiving an OPEN RPC from the Master.

Parameters:
zkw - zk reference
region - region to be transitioned to opening
serverName - server event originates from
Returns:
version of node after transition, -1 if unsuccessful transition
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

transitionNodeOpening

public static int transitionNodeOpening(ZooKeeperWatcher zkw,
                                        HRegionInfo region,
                                        String serverName,
                                        EventHandler.EventType beginState)
                                 throws org.apache.zookeeper.KeeperException
Throws:
org.apache.zookeeper.KeeperException

retransitionNodeOpening

public static int retransitionNodeOpening(ZooKeeperWatcher zkw,
                                          HRegionInfo region,
                                          String serverName,
                                          int expectedVersion)
                                   throws org.apache.zookeeper.KeeperException
Retransitions an existing unassigned node for the specified region which is currently in the OPENING state to be in the OPENING state.

Does not transition nodes from other states. If for some reason the node could not be transitioned, the method returns -1. If the transition is successful, the version of the node rewritten as OPENING is returned.

This method can fail and return -1 for three different reasons:

Does not set any watches.

This method should only be used by a RegionServer when initiating an open of a region after receiving an OPEN RPC from the Master.

Parameters:
zkw - zk reference
region - region to be transitioned to opening
serverName - server event originates from
Returns:
version of node after transition, -1 if unsuccessful transition
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

transitionNodeOpened

public static int transitionNodeOpened(ZooKeeperWatcher zkw,
                                       HRegionInfo region,
                                       String serverName,
                                       int expectedVersion)
                                throws org.apache.zookeeper.KeeperException
Transitions an existing unassigned node for the specified region which is currently in the OPENING state to be in the OPENED state.

Does not transition nodes from other states. If for some reason the node could not be transitioned, the method returns -1. If the transition is successful, the version of the node after transition is returned.

This method can fail and return false for three different reasons:

Does not set any watches.

This method should only be used by a RegionServer when completing the open of a region.

Parameters:
zkw - zk reference
region - region to be transitioned to opened
serverName - server event originates from
Returns:
version of node after transition, -1 if unsuccessful transition
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

transitionNode

public static int transitionNode(ZooKeeperWatcher zkw,
                                 HRegionInfo region,
                                 String serverName,
                                 EventHandler.EventType beginState,
                                 EventHandler.EventType endState,
                                 int expectedVersion)
                          throws org.apache.zookeeper.KeeperException
Private method that actually performs unassigned node transitions.

Attempts to transition the unassigned node for the specified region from the expected state to the state in the specified transition data.

Method first reads existing data and verifies it is in the expected state. If the node does not exist or the node is not in the expected state, the method returns -1. If the transition is successful, the version number of the node following the transition is returned.

If the read state is what is expected, it attempts to write the new state and data into the node. When doing this, it includes the expected version (determined when the existing state was verified) to ensure that only one transition is successful. If there is a version mismatch, the method returns -1.

If the write is successful, no watch is set and the method returns true.

Parameters:
zkw - zk reference
region - region to be transitioned to opened
serverName - server event originates from
endState - state to transition node to if all checks pass
beginState - state the node must currently be in to do transition
expectedVersion - expected version of data before modification, or -1
Returns:
version of node after transition, -1 if unsuccessful transition
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

getData

public static RegionTransitionData getData(ZooKeeperWatcher zkw,
                                           String pathOrRegionName)
                                    throws org.apache.zookeeper.KeeperException
Gets the current data in the unassigned node for the specified region name or fully-qualified path.

Returns null if the region does not currently have a node.

Sets a watch on the node if the node exists.

Parameters:
zkw - zk reference
pathOrRegionName - fully-specified path or region name
Returns:
data for the unassigned node
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

getDataNoWatch

public static RegionTransitionData getDataNoWatch(ZooKeeperWatcher zkw,
                                                  String pathOrRegionName,
                                                  org.apache.zookeeper.data.Stat stat)
                                           throws org.apache.zookeeper.KeeperException
Gets the current data in the unassigned node for the specified region name or fully-qualified path.

Returns null if the region does not currently have a node.

Does not set a watch.

Parameters:
zkw - zk reference
pathOrRegionName - fully-specified path or region name
stat - object to store node info into on getData call
Returns:
data for the unassigned node or null if node does not exist
Throws:
org.apache.zookeeper.KeeperException - if unexpected zookeeper exception

deleteNodeFailSilent

public static void deleteNodeFailSilent(ZooKeeperWatcher watcher,
                                        HRegionInfo regionInfo)
                                 throws org.apache.zookeeper.KeeperException
Delete the assignment node regardless of its current state.

Fail silent even if the node does not exist at all.

Parameters:
watcher -
regionInfo -
Throws:
org.apache.zookeeper.KeeperException

blockUntilNoRIT

public static void blockUntilNoRIT(ZooKeeperWatcher zkw)
                            throws org.apache.zookeeper.KeeperException,
                                   InterruptedException
Blocks until there are no node in regions in transition.

Used in testing only.

Parameters:
zkw - zk reference
Throws:
org.apache.zookeeper.KeeperException
InterruptedException

blockUntilRIT

public static void blockUntilRIT(ZooKeeperWatcher zkw)
                          throws org.apache.zookeeper.KeeperException,
                                 InterruptedException
Blocks until there is at least one node in regions in transition.

Used in testing only.

Parameters:
zkw - zk reference
Throws:
org.apache.zookeeper.KeeperException
InterruptedException

verifyRegionState

public static boolean verifyRegionState(ZooKeeperWatcher zkw,
                                        HRegionInfo region,
                                        EventHandler.EventType expectedState)
                                 throws org.apache.zookeeper.KeeperException
Verifies that the specified region is in the specified state in ZooKeeper.

Returns true if region is in transition and in the specified state in ZooKeeper. Returns false if the region does not exist in ZK or is in a different state.

Method synchronizes() with ZK so will yield an up-to-date result but is a slow read.

Parameters:
zkw -
region -
expectedState -
Returns:
true if region exists and is in expected state
Throws:
org.apache.zookeeper.KeeperException


Copyright © 2011 The Apache Software Foundation. All Rights Reserved.