本文整理匯總了Java中org.apache.zookeeper.KeeperException.create方法的典型用法代碼示例。如果您正苦於以下問題:Java KeeperException.create方法的具體用法?Java KeeperException.create怎麽用?Java KeeperException.create使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.zookeeper.KeeperException
的用法示例。
在下文中一共展示了KeeperException.create方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: waitForZKConnectionEvent
import org.apache.zookeeper.KeeperException; //導入方法依賴的package包/類
/**
* Waits for the next event from ZooKeeper to arrive.
*
* @param connectionTimeoutMs zookeeper connection timeout in milliseconds
* @throws KeeperException if the connection attempt times out. This will
* be a ZooKeeper ConnectionLoss exception code.
* @throws IOException if interrupted while connecting to ZooKeeper
*/
private void waitForZKConnectionEvent(int connectionTimeoutMs)
throws KeeperException, IOException {
try {
if (!hasReceivedEvent.await(connectionTimeoutMs, TimeUnit.MILLISECONDS)) {
LOG.error("Connection timed out: couldn't connect to ZooKeeper in "
+ connectionTimeoutMs + " milliseconds");
zk.close();
throw KeeperException.create(Code.CONNECTIONLOSS);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException(
"Interrupted when connecting to zookeeper server", e);
}
}
示例2: createAndWatch
import org.apache.zookeeper.KeeperException; //導入方法依賴的package包/類
/**
* Creates the specified node with the specified data and watches it.
*
* <p>Throws an exception if the node already exists.
*
* <p>The node created is persistent and open access.
*
* <p>Returns the version number of the created node if successful.
*
* @param zkw zk reference
* @param znode path of node to create
* @param data data of node to create
* @return version of node created
* @throws KeeperException if unexpected zookeeper exception
* @throws KeeperException.NodeExistsException if node already exists
*/
public static int createAndWatch(ZooKeeperWatcher zkw,
String znode, byte [] data)
throws KeeperException, KeeperException.NodeExistsException {
try {
zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode),
CreateMode.PERSISTENT);
Stat stat = zkw.getRecoverableZooKeeper().exists(znode, zkw);
if (stat == null){
// Likely a race condition. Someone deleted the znode.
throw KeeperException.create(KeeperException.Code.SYSTEMERROR,
"ZK.exists returned null (i.e.: znode does not exist) for znode=" + znode);
}
return stat.getVersion();
} catch (InterruptedException e) {
zkw.interruptedException(e);
return -1;
}
}
示例3: multi
import org.apache.zookeeper.KeeperException; //導入方法依賴的package包/類
private List<OpResult> multi(ZooKeeper zk, Iterable<Op> ops)
throws KeeperException, InterruptedException {
if (useAsync) {
final MultiResult res = new MultiResult();
zk.multi(ops, new MultiCallback() {
@Override
public void processResult(int rc, String path, Object ctx,
List<OpResult> opResults) {
synchronized (res) {
res.rc = rc;
res.results = opResults;
res.finished = true;
res.notifyAll();
}
}
}, null);
synchronized (res) {
while (!res.finished) {
res.wait();
}
}
if (KeeperException.Code.OK.intValue() != res.rc) {
KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc));
throw ke;
}
return res.results;
} else {
return zk.multi(ops);
}
}
示例4: commit
import org.apache.zookeeper.KeeperException; //導入方法依賴的package包/類
private List<OpResult> commit(Transaction txn)
throws KeeperException, InterruptedException {
if (useAsync) {
final MultiResult res = new MultiResult();
txn.commit(new MultiCallback() {
@Override
public void processResult(int rc, String path, Object ctx,
List<OpResult> opResults) {
synchronized (res) {
res.rc = rc;
res.results = opResults;
res.finished = true;
res.notifyAll();
}
}
}, null);
synchronized (res) {
while (!res.finished) {
res.wait();
}
}
if (KeeperException.Code.OK.intValue() != res.rc) {
KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc));
throw ke;
}
return res.results;
} else {
return txn.commit();
}
}
示例5: reconfigure
import org.apache.zookeeper.KeeperException; //導入方法依賴的package包/類
/**
* Reconfigure - add/remove servers. Return the new configuration.
* @param joiningServers
* a comma separated list of servers being added (incremental reconfiguration)
* @param leavingServers
* a comma separated list of servers being removed (incremental reconfiguration)
* @param newMembers
* a comma separated list of new membership (non-incremental reconfiguration)
* @param fromConfig
* version of the current configuration
* (optional - causes reconfiguration to throw an exception if configuration is no longer current)
* @param stat the stat of /zookeeper/config znode will be copied to this
* parameter if not null.
* @return new configuration
* @throws InterruptedException If the server transaction is interrupted.
* @throws KeeperException If the server signals an error with a non-zero error code.
*/
public byte[] reconfigure(String joiningServers, String leavingServers,
String newMembers, long fromConfig, Stat stat) throws KeeperException, InterruptedException {
RequestHeader h = new RequestHeader();
h.setType(ZooDefs.OpCode.reconfig);
ReconfigRequest request = new ReconfigRequest(joiningServers, leavingServers, newMembers, fromConfig);
GetDataResponse response = new GetDataResponse();
ReplyHeader r = cnxn.submitRequest(h, request, response, null);
if (r.getErr() != 0) {
throw KeeperException.create(KeeperException.Code.get(r.getErr()), "");
}
if (stat != null) {
DataTree.copyStat(response.getStat(), stat);
}
return response.getData();
}
示例6: deleteNode
import org.apache.zookeeper.KeeperException; //導入方法依賴的package包/類
/**
* Deletes an existing unassigned node that is in the specified state for the
* specified region.
*
* <p>If a node does not already exist for this region, a
* {@link org.apache.zookeeper.KeeperException.NoNodeException} will be thrown.
*
* <p>No watcher is set whether this succeeds or not.
*
* <p>Returns false if the node was not in the proper state but did exist.
*
* <p>This method is used when a region finishes opening/closing.
* The Master acknowledges completion
* of the specified regions transition to being closed/opened.
*
* @param zkw zk reference
* @param encodedRegionName region to be deleted from zk
* @param expectedState state region must be in for delete to complete
* @param serverName the expected region transition target server name
* @param expectedVersion of the znode that is to be deleted.
* If expectedVersion need not be compared while deleting the znode
* pass -1
* @throws KeeperException if unexpected zookeeper exception
* @throws KeeperException.NoNodeException if node does not exist
*/
public static boolean deleteNode(ZooKeeperWatcher zkw, String encodedRegionName,
EventType expectedState, ServerName serverName, int expectedVersion)
throws KeeperException, KeeperException.NoNodeException {
if (LOG.isTraceEnabled()) {
LOG.trace(zkw.prefix("Deleting existing unassigned " +
"node " + encodedRegionName + " in expected state " + expectedState));
}
String node = getNodeName(zkw, encodedRegionName);
zkw.sync(node);
Stat stat = new Stat();
byte [] bytes = ZKUtil.getDataNoWatch(zkw, node, stat);
if (bytes == null) {
// If it came back null, node does not exist.
throw KeeperException.create(Code.NONODE);
}
RegionTransition rt = getRegionTransition(bytes);
EventType et = rt.getEventType();
if (!et.equals(expectedState)) {
LOG.warn(zkw.prefix("Attempting to delete unassigned node " + encodedRegionName + " in " +
expectedState + " state but node is in " + et + " state"));
return false;
}
// Verify the server transition happens on is not changed
if (serverName != null && !rt.getServerName().equals(serverName)) {
LOG.warn(zkw.prefix("Attempting to delete unassigned node " + encodedRegionName
+ " with target " + serverName + " but node has " + rt.getServerName()));
return false;
}
if (expectedVersion != -1
&& stat.getVersion() != expectedVersion) {
LOG.warn("The node " + encodedRegionName + " we are trying to delete is not" +
" the expected one. Got a version mismatch");
return false;
}
if(!ZKUtil.deleteNode(zkw, node, stat.getVersion())) {
LOG.warn(zkw.prefix("Attempting to delete " +
"unassigned node " + encodedRegionName + " in " + expectedState +
" state but after verifying state, we got a version mismatch"));
return false;
}
LOG.debug(zkw.prefix("Deleted unassigned node " +
encodedRegionName + " in expected state " + expectedState));
return true;
}