@InterfaceAudience.Private public class ZKWatcher extends Object implements org.apache.zookeeper.Watcher, Abortable, Closeable
This is the only class that implements Watcher
. Other internal
classes which need to be notified of ZooKeeper events must register with
the local instance of this watcher via registerListener(org.apache.hadoop.hbase.zookeeper.ZKListener)
.
This class also holds and manages the connection to ZooKeeper. Code to deal with connection related events and exceptions are handled here.
Constructor and Description |
---|
ZKWatcher(org.apache.hadoop.conf.Configuration conf,
String identifier,
Abortable abortable)
Instantiate a ZooKeeper connection and watcher.
|
ZKWatcher(org.apache.hadoop.conf.Configuration conf,
String identifier,
Abortable abortable,
boolean canCreateBaseZNode)
Instantiate a ZooKeeper connection and watcher.
|
ZKWatcher(org.apache.hadoop.conf.Configuration conf,
String identifier,
Abortable abortable,
boolean canCreateBaseZNode,
boolean clientZK)
Instantiate a ZooKeeper connection and watcher.
|
Modifier and Type | Method and Description |
---|---|
void |
abort(String why,
Throwable e)
Abort the server or client.
|
void |
checkAndSetZNodeAcls()
On master start, we check the znode ACLs under the root directory and set the ACLs properly
if needed.
|
void |
close()
Close the connection to ZooKeeper.
|
org.apache.hadoop.conf.Configuration |
getConfiguration() |
List<ZKListener> |
getListeners()
Get a copy of current registered listeners
|
List<String> |
getMetaReplicaNodes()
Get the znodes corresponding to the meta replicas from ZK
|
int |
getNumberOfListeners() |
String |
getQuorum()
Get the quorum address of this instance.
|
RecoverableZooKeeper |
getRecoverableZooKeeper()
Get the connection to ZooKeeper.
|
ZNodePaths |
getZNodePaths()
Get the znodePaths.
|
void |
interruptedException(InterruptedException ie)
Handles InterruptedExceptions in client calls.
|
void |
interruptedExceptionNoThrow(InterruptedException ie,
boolean throwLater)
Log the InterruptedException and interrupt current thread
|
boolean |
isAborted()
Check if the server or client was aborted.
|
static boolean |
isSuperUserId(String[] superUsers,
org.apache.zookeeper.data.Id id) |
void |
keeperException(org.apache.zookeeper.KeeperException ke)
Handles KeeperExceptions in client calls.
|
String |
prefix(String str)
Adds this instance's identifier as a prefix to the passed
str |
void |
process(org.apache.zookeeper.WatchedEvent event)
Method called from ZooKeeper for events and connection status.
|
void |
reconnectAfterExpiration() |
void |
registerListener(ZKListener listener)
Register the specified listener to receive ZooKeeper events.
|
void |
registerListenerFirst(ZKListener listener)
Register the specified listener to receive ZooKeeper events and add it as
the first in the list of current listeners.
|
void |
sync(String path)
Forces a synchronization of this ZooKeeper client connection.
|
String |
toString() |
void |
unregisterAllListeners()
Clean all existing listeners
|
void |
unregisterListener(ZKListener listener) |
protected Abortable abortable
public ZKWatcher(org.apache.hadoop.conf.Configuration conf, String identifier, Abortable abortable) throws ZooKeeperConnectionException, IOException
identifier
- string that is passed to RecoverableZookeeper to be used as
identifier for this instance. Use null for default.IOException
- if the connection to ZooKeeper failsZooKeeperConnectionException
- if the client can't connect to ZooKeeperpublic ZKWatcher(org.apache.hadoop.conf.Configuration conf, String identifier, Abortable abortable, boolean canCreateBaseZNode) throws IOException, ZooKeeperConnectionException
conf
- the configuration to useidentifier
- string that is passed to RecoverableZookeeper to be used as identifier for
this instance. Use null for default.abortable
- Can be null if there is on error there is no host to abort: e.g. client
context.canCreateBaseZNode
- true if a base ZNode can be createdIOException
- if the connection to ZooKeeper failsZooKeeperConnectionException
- if the client can't connect to ZooKeeperpublic ZKWatcher(org.apache.hadoop.conf.Configuration conf, String identifier, Abortable abortable, boolean canCreateBaseZNode, boolean clientZK) throws IOException, ZooKeeperConnectionException
conf
- the configuration to useidentifier
- string that is passed to RecoverableZookeeper to be used as identifier for
this instance. Use null for default.abortable
- Can be null if there is on error there is no host to abort: e.g. client
context.canCreateBaseZNode
- true if a base ZNode can be createdclientZK
- whether this watcher is set to access client ZKIOException
- if the connection to ZooKeeper failsZooKeeperConnectionException
- if the connection to Zookeeper fails when create base
ZNodespublic void checkAndSetZNodeAcls()
public static boolean isSuperUserId(String[] superUsers, org.apache.zookeeper.data.Id id)
public String prefix(String str)
str
str
- String to amend.public List<String> getMetaReplicaNodes() throws org.apache.zookeeper.KeeperException
org.apache.zookeeper.KeeperException
- if a ZooKeeper operation failspublic void registerListener(ZKListener listener)
listener
- the listener to registerpublic void registerListenerFirst(ZKListener listener)
listener
- the listener to registerpublic void unregisterListener(ZKListener listener)
public void unregisterAllListeners()
public List<ZKListener> getListeners()
public int getNumberOfListeners()
public RecoverableZooKeeper getRecoverableZooKeeper()
public void reconnectAfterExpiration() throws IOException, org.apache.zookeeper.KeeperException, InterruptedException
IOException
org.apache.zookeeper.KeeperException
InterruptedException
public String getQuorum()
public ZNodePaths getZNodePaths()
Mainly used for mocking as mockito can not mock a field access.
public void process(org.apache.zookeeper.WatchedEvent event)
Valid events are passed along to listeners. Connection status changes are dealt with locally.
process
in interface org.apache.zookeeper.Watcher
public void sync(String path) throws org.apache.zookeeper.KeeperException
Executing this method before running other methods will ensure that the subsequent operations are up-to-date and consistent as of the time that the sync is complete.
This is used for compareAndSwap type operations where we need to read the data of an existing node and delete or transition that node, utilizing the previously read version and data. We want to ensure that the version read is up-to-date from when we begin the operation.
org.apache.zookeeper.KeeperException
public void keeperException(org.apache.zookeeper.KeeperException ke) throws org.apache.zookeeper.KeeperException
This may be temporary but for now this gives one place to deal with these.
TODO: Currently this method rethrows the exception to let the caller handle
ke
- the exception to rethroworg.apache.zookeeper.KeeperException
- if a ZooKeeper operation failspublic void interruptedException(InterruptedException ie) throws org.apache.zookeeper.KeeperException
ie
- the InterruptedException instance thrownorg.apache.zookeeper.KeeperException
- the exception to throw, transformed from the InterruptedExceptionpublic void interruptedExceptionNoThrow(InterruptedException ie, boolean throwLater)
ie
- The IterruptedException to logthrowLater
- Whether we will throw the exception latterpublic void close()
close
in interface Closeable
close
in interface AutoCloseable
public org.apache.hadoop.conf.Configuration getConfiguration()
public void abort(String why, Throwable e)
Abortable
Copyright © 2007–2019 Cloudera. All rights reserved.