本文整理匯總了Java中org.apache.zookeeper.data.Stat.getVersion方法的典型用法代碼示例。如果您正苦於以下問題:Java Stat.getVersion方法的具體用法?Java Stat.getVersion怎麽用?Java Stat.getVersion使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.zookeeper.data.Stat
的用法示例。
在下文中一共展示了Stat.getVersion方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setDataWithVersion
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
/**
* CAS更新指定節點數據內容
* @param path 節點path
* @param payload 數據內容
* @param version 版本號
* @return
* @throws Exception
*/
@Override
public int setDataWithVersion(String path, byte[] payload, int version) throws Exception {
try {
Stat stat = null;
if (version != -1) {
stat = client.setData().withVersion(version).forPath(path, payload);
} else {
stat = client.setData().forPath(path, payload);
}
if (stat != null) {
//logger.info("CAS設置數據成功,path:" + path );
return stat.getVersion();
} else {
logger.error("CAS設置數據失敗,path:" + path);
return -1;
}
} catch (KeeperException.BadVersionException ex) {
logger.error("CAS設置數據失敗,path:" + path);
return -1;
}
}
示例2: modelModifiedExternally
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
@Override
protected void modelModifiedExternally() {
ZnodeModel znodeModel = getModel();
if (znodeModel.isDestroyed()) {
return;
}
ZnodeModelFormEditor editor = (ZnodeModelFormEditor) getEditor();
Znode znode = znodeModel.getData();
Stat stat = znode.getStat();
if (!isDirty() || stat.getVersion() == editor.getLastModificationVersion()) {
_ActiveEditor.syncZnodeModelData();
updateToolbarLabelText();
}
else {
editor.setActivePage(ID);
setInfoText(EXTERNAL_MODIFICATION_INFO_TEXT);
}
}
示例3: processResult
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
public void processResult(int rc, String path,
Object ctx, byte[] data, Stat stat) {
if (rc == KeeperException.Code.NONODE.intValue()) {
// we can just ignore because the child watcher takes care of this
return;
}
if (rc != KeeperException.Code.OK.intValue()) {
zk.getData(myNode, (Watcher)ctx, this, ctx);
}
int currVer = stat.getVersion();
if (currVer != lastVer) {
String parts[] = new String(data).split(" ", 2);
myInstance.configure(parts[1]);
lastVer = currVer;
}
}
示例4: createAndWatch
import org.apache.zookeeper.data.Stat; //導入方法依賴的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;
}
}
示例5: testSetData
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
@Test
public void testSetData() throws Exception {
final ZooKeeper zk = createClient();
ZooKeeperMain zkMain = new ZooKeeperMain(zk);
String cmdstring1 = "create -e /node4 data";
String cmdstring2 = "set /node4 " + "data";
String cmdstring3 = "delete /node4";
Stat stat = new Stat();
int version = 0;
zkMain.cl.parseCommand(cmdstring1);
Assert.assertTrue(zkMain.processZKCmd(zkMain.cl));
stat = zk.exists("/node4", true);
version = stat.getVersion();
zkMain.cl.parseCommand(cmdstring2);
Assert.assertFalse(zkMain.processZKCmd(zkMain.cl));
stat = zk.exists("/node4", true);
Assert.assertEquals(version + 1, stat.getVersion());
zkMain.cl.parseCommand(cmdstring3);
Assert.assertFalse(zkMain.processZKCmd(zkMain.cl));
}
示例6: updateAcquiredLock
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
/**
* Update state as to indicate that a lock is held
* @param createdZNode The lock znode
* @throws IOException If an unrecoverable ZooKeeper error occurs
*/
protected void updateAcquiredLock(String createdZNode) throws IOException {
Stat stat = new Stat();
byte[] data = null;
Exception ex = null;
try {
data = ZKUtil.getDataNoWatch(zkWatcher, createdZNode, stat);
} catch (KeeperException e) {
LOG.warn("Cannot getData for znode:" + createdZNode, e);
ex = e;
}
if (data == null) {
LOG.error("Can't acquire a lock on a non-existent node " + createdZNode);
throw new IllegalStateException("ZNode " + createdZNode +
"no longer exists!", ex);
}
AcquiredLock newLock = new AcquiredLock(createdZNode, stat.getVersion());
if (!acquiredLock.compareAndSet(null, newLock)) {
LOG.error("The lock " + fullyQualifiedZNode +
" has already been acquired by another process!");
throw new IllegalStateException(fullyQualifiedZNode +
" is held by another process");
}
}
示例7: getVersion
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
/**
* 獲得節點的version號,如果節點不存在,返回 -1
* @param path
* @return
* @throws Exception
*/
@Override
public int getVersion(String path) throws Exception {
Stat stat = this.getStat(path);
if (stat != null) {
return stat.getVersion();
} else {
return -1;
}
}
示例8: setZNode
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
@PUT
@Produces( { MediaType.APPLICATION_JSON, "application/javascript",
MediaType.APPLICATION_XML })
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public Response setZNode(
@PathParam("path") String path,
@QueryParam("callback") String callback,
@DefaultValue("-1") @QueryParam("version") String versionParam,
@DefaultValue("base64") @QueryParam("dataformat") String dataformat,
@DefaultValue("false") @QueryParam("null") String setNull,
@Context UriInfo ui, byte[] data) throws InterruptedException,
KeeperException {
ensurePathNotNull(path);
int version;
try {
version = Integer.parseInt(versionParam);
} catch (NumberFormatException e) {
throw new WebApplicationException(Response.status(
Response.Status.BAD_REQUEST).entity(
new ZError(ui.getRequestUri().toString(), path
+ " bad version " + versionParam)).build());
}
if (setNull.equals("true")) {
data = null;
}
Stat stat = zk.setData(path, data, version);
ZStat zstat = new ZStat(path, ui.getAbsolutePath().toString(), null,
null, stat.getCzxid(), stat.getMzxid(), stat.getCtime(), stat
.getMtime(), stat.getVersion(), stat.getCversion(),
stat.getAversion(), stat.getEphemeralOwner(), stat
.getDataLength(), stat.getNumChildren(), stat
.getPzxid());
return Response.status(Response.Status.OK).entity(
new JSONWithPadding(zstat, callback)).build();
}
示例9: checkClosingState
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
/**
*
* @param zkw zk reference
* @param region region to be closed
* @param expectedVersion expected version of the znode
* @return true if the znode exists, has the right version and the right state. False otherwise.
* @throws KeeperException
*/
public static boolean checkClosingState(ZooKeeperWatcher zkw, HRegionInfo region,
int expectedVersion) throws KeeperException {
final String encoded = getNodeName(zkw, region.getEncodedName());
zkw.sync(encoded);
// Read existing data of the node
Stat stat = new Stat();
byte[] existingBytes = ZKUtil.getDataNoWatch(zkw, encoded, stat);
if (existingBytes == null) {
LOG.warn(zkw.prefix("Attempt to check the " +
"closing node for " + encoded +
". The node does not exist"));
return false;
}
if (expectedVersion != -1 && stat.getVersion() != expectedVersion) {
LOG.warn(zkw.prefix("Attempt to check the " +
"closing node for " + encoded +
". The node existed but was version " + stat.getVersion() +
" not the expected version " + expectedVersion));
return false;
}
RegionTransition rt = getRegionTransition(existingBytes);
if (!EventType.M_ZK_REGION_CLOSING.equals(rt.getEventType())) {
LOG.warn(zkw.prefix("Attempt to check the " +
"closing node for " + encoded +
". The node existed but was in an unexpected state: " + rt.getEventType()));
return false;
}
return true;
}
示例10: pullFromZK
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
/**
* Pulls data from ZooKeeper. If isInit is false, it will only parse the
* next secret and version. If isInit is true, it will also parse the current
* and previous secrets, and the next rollover date; it will also init the
* secrets. Hence, isInit should only be true on startup.
* @param isInit see description above
*/
private synchronized void pullFromZK(boolean isInit) {
try {
Stat stat = new Stat();
byte[] bytes = client.getData().storingStatIn(stat).forPath(path);
ByteBuffer bb = ByteBuffer.wrap(bytes);
int dataVersion = bb.getInt();
if (dataVersion > DATA_VERSION) {
throw new IllegalStateException("Cannot load data from ZooKeeper; it"
+ "was written with a newer version");
}
int nextSecretLength = bb.getInt();
byte[] nextSecret = new byte[nextSecretLength];
bb.get(nextSecret);
this.nextSecret = nextSecret;
zkVersion = stat.getVersion();
if (isInit) {
int currentSecretLength = bb.getInt();
byte[] currentSecret = new byte[currentSecretLength];
bb.get(currentSecret);
int previousSecretLength = bb.getInt();
byte[] previousSecret = null;
if (previousSecretLength > 0) {
previousSecret = new byte[previousSecretLength];
bb.get(previousSecret);
}
super.initSecrets(currentSecret, previousSecret);
nextRolloverDate = bb.getLong();
}
} catch (Exception ex) {
LOG.error("An unexpected exception occurred while pulling data from"
+ "ZooKeeper", ex);
}
}
示例11: updateReloadTaskItemFlag
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
public long updateReloadTaskItemFlag(String taskType) throws Exception{
String baseTaskType = ScheduleUtil.splitBaseTaskTypeFromTaskType(taskType);
String zkPath = this.PATH_BaseTaskType + "/" + baseTaskType
+ "/" + taskType + "/" + this.PATH_Server;
Stat stat = this.getZooKeeper().setData(zkPath,"reload=true".getBytes(),-1);
return stat.getVersion();
}
示例12: getReloadTaskItemFlag
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
public long getReloadTaskItemFlag(String taskType) throws Exception{
String baseTaskType = ScheduleUtil.splitBaseTaskTypeFromTaskType(taskType);
String zkPath = this.PATH_BaseTaskType + "/" + baseTaskType
+ "/" + taskType + "/" + this.PATH_Server;
Stat stat = new Stat();
this.getZooKeeper().getData(zkPath, false, stat);
return stat.getVersion();
}
示例13: attemptToOwnTask
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
/**
* Try to own the task by transitioning the zk node data from UNASSIGNED to OWNED.
* <p>
* This method is also used to periodically heartbeat the task progress by transitioning the node
* from OWNED to OWNED.
* <p>
* @param isFirstTime shows whther it's the first attempt.
* @param zkw zk wathcer
* @param server name
* @param task to own
* @param taskZKVersion version of the task in zk
* @return non-negative integer value when task can be owned by current region server otherwise -1
*/
protected static int attemptToOwnTask(boolean isFirstTime, ZooKeeperWatcher zkw,
ServerName server, String task, RecoveryMode mode, int taskZKVersion) {
int latestZKVersion = FAILED_TO_OWN_TASK;
try {
SplitLogTask slt = new SplitLogTask.Owned(server, mode);
Stat stat = zkw.getRecoverableZooKeeper().setData(task, slt.toByteArray(), taskZKVersion);
if (stat == null) {
LOG.warn("zk.setData() returned null for path " + task);
SplitLogCounters.tot_wkr_task_heartbeat_failed.incrementAndGet();
return FAILED_TO_OWN_TASK;
}
latestZKVersion = stat.getVersion();
SplitLogCounters.tot_wkr_task_heartbeat.incrementAndGet();
return latestZKVersion;
} catch (KeeperException e) {
if (!isFirstTime) {
if (e.code().equals(KeeperException.Code.NONODE)) {
LOG.warn("NONODE failed to assert ownership for " + task, e);
} else if (e.code().equals(KeeperException.Code.BADVERSION)) {
LOG.warn("BADVERSION failed to assert ownership for " + task, e);
} else {
LOG.warn("failed to assert ownership for " + task, e);
}
}
} catch (InterruptedException e1) {
LOG.warn("Interrupted while trying to assert ownership of " + task + " "
+ StringUtils.stringifyException(e1));
Thread.currentThread().interrupt();
}
SplitLogCounters.tot_wkr_task_heartbeat_failed.incrementAndGet();
return FAILED_TO_OWN_TASK;
}
示例14: deleteNode
import org.apache.zookeeper.data.Stat; //導入方法依賴的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;
}
示例15: confirmNodeOpening
import org.apache.zookeeper.data.Stat; //導入方法依賴的package包/類
/**
* Confirm an existing unassigned node for the specified region which is
* currently in the OPENING state to be still in the OPENING state on
* the specified server.
*
* <p>If for some reason the check fails, the method returns -1. Otherwise,
* the version of the node (same as the expected version) is returned.
*
* <p>This method can fail and return -1 for three different reasons:
* <ul><li>Unassigned node for this region does not exist</li>
* <li>Unassigned node for this region is not in OPENING state</li>
* <li>After verifying OPENING state, the server name or the version of the
* doesn't match)</li>
* </ul>
*
* <p>Does not set any watches.
*
* <p>This method should only be used by a RegionServer when initiating an
* open of a region after receiving an OPEN RPC from the Master.
*
* @param zkw zk reference
* @param region region to be transitioned to opening
* @param serverName server transition happens on
* @return version of node after transition, -1 if unsuccessful transition
* @throws KeeperException if unexpected zookeeper exception
*/
public static int confirmNodeOpening(ZooKeeperWatcher zkw,
HRegionInfo region, ServerName serverName, int expectedVersion)
throws KeeperException {
String encoded = region.getEncodedName();
if(LOG.isDebugEnabled()) {
LOG.debug(zkw.prefix("Attempting to retransition opening state of node " +
HRegionInfo.prettyPrint(encoded)));
}
String node = getNodeName(zkw, encoded);
zkw.sync(node);
// Read existing data of the node
Stat stat = new Stat();
byte [] existingBytes = ZKUtil.getDataNoWatch(zkw, node, stat);
if (existingBytes == null) {
// Node no longer exists. Return -1. It means unsuccessful transition.
return -1;
}
RegionTransition rt = getRegionTransition(existingBytes);
// Verify it is the expected version
if (expectedVersion != -1 && stat.getVersion() != expectedVersion) {
LOG.warn(zkw.prefix("Attempt to retransition the opening state of the " +
"unassigned node for " + encoded + " failed, " +
"the node existed but was version " + stat.getVersion() +
" not the expected version " + expectedVersion));
return -1;
}
// Verify it is in expected state
EventType et = rt.getEventType();
if (!et.equals(EventType.RS_ZK_REGION_OPENING)) {
String existingServer = (rt.getServerName() == null)
? "<unknown>" : rt.getServerName().toString();
LOG.warn(zkw.prefix("Attempt to retransition the opening state of the unassigned node for "
+ encoded + " failed, the node existed but was in the state " + et +
" set by the server " + existingServer));
return -1;
}
return expectedVersion;
}