use of org.I0Itec.zkclient.exception.ZkBadVersionException in project samza by apache.
the class ZkUtils method validateZkVersion.
// validate that Zk protocol currently used by the job is the same as in this participant
public void validateZkVersion() {
// Version of the protocol is written into root znode. If root does not exist yet we need to create one.
String rootPath = keyBuilder.getRootPath();
if (!zkClient.exists(rootPath)) {
try {
// attempt to create the root with the correct version
zkClient.createPersistent(rootPath, ZK_PROTOCOL_VERSION);
LOG.info("Created zk root node: " + rootPath + " with zk version " + ZK_PROTOCOL_VERSION);
return;
} catch (ZkNodeExistsException e) {
// ignoring
LOG.warn("root path " + rootPath + " already exists.");
}
}
// if exists, verify the version
Stat stat = new Stat();
String version = zkClient.readData(rootPath, stat);
if (version == null) {
// for backward compatibility, if no value - assume 1.0
try {
zkClient.writeData(rootPath, "1.0", stat.getVersion());
} catch (ZkBadVersionException e) {
// if the write failed with ZkBadVersionException it means someone else already wrote a version, so we can ignore it.
}
// re-read the updated version
version = zkClient.readData(rootPath);
}
LOG.info("Current version for zk root node: " + rootPath + " is " + version + ", expected version is " + ZK_PROTOCOL_VERSION);
if (!version.equals(ZK_PROTOCOL_VERSION)) {
throw new SamzaException("ZK Protocol mismatch. Expected " + ZK_PROTOCOL_VERSION + "; found " + version);
}
}
use of org.I0Itec.zkclient.exception.ZkBadVersionException in project otter by alibaba.
the class SystemArbitrateEvent method switchWarmup.
/**
* 手工触发一次主备切换
*/
public void switchWarmup(Long channelId, Long pipelineId) {
String path = ManagePathUtils.getMainStem(channelId, pipelineId);
try {
while (true) {
Stat stat = new Stat();
byte[] bytes = zookeeper.readData(path, stat);
MainStemEventData mainStemData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
mainStemData.setActive(false);
try {
zookeeper.writeData(path, JsonUtils.marshalToByte(mainStemData), stat.getVersion());
logger.warn("relase channelId[{}],pipelineId[{}] mainstem successed! ", channelId, pipelineId);
break;
} catch (ZkBadVersionException e) {
// ignore , retrying
}
}
} catch (ZkNoNodeException e) {
// ignore
} catch (ZkException e) {
throw new ArbitrateException("releaseMainStem", pipelineId.toString(), e);
}
}
use of org.I0Itec.zkclient.exception.ZkBadVersionException in project otter by alibaba.
the class MainStemMonitorTest method switchWarmup.
/**
* 手工触发一次主备切换
*/
private void switchWarmup(Long channelId, Long pipelineId) {
String path = ManagePathUtils.getMainStem(channelId, pipelineId);
try {
while (true) {
Stat stat = new Stat();
byte[] bytes = zookeeper.readData(path, stat);
MainStemEventData mainStemData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
mainStemData.setActive(false);
try {
zookeeper.writeData(path, JsonUtils.marshalToByte(mainStemData), stat.getVersion());
break;
} catch (ZkBadVersionException e) {
// ignore , retrying
}
}
} catch (ZkNoNodeException e) {
// ignore
} catch (ZkException e) {
throw new ArbitrateException("releaseMainStem", pipelineId.toString(), e);
}
}
use of org.I0Itec.zkclient.exception.ZkBadVersionException in project otter by alibaba.
the class ZkClientx method updateDataSerialized.
/**
* Updates data of an existing znode. The current content of the znode is passed to the {@link DataUpdater} that is
* passed into this method, which returns the new content. The new content is only written back to ZooKeeper if
* nobody has modified the given znode in between. If a concurrent change has been detected the new data of the
* znode is passed to the updater once again until the new contents can be successfully written back to ZooKeeper.
*
* @param <T>
* @param path The path of the znode.
* @param updater Updater that creates the new contents.
*/
public <T extends Object> void updateDataSerialized(String path, DataUpdater<T> updater) {
Stat stat = new Stat();
boolean retry;
do {
retry = false;
try {
T oldData = (T) readData(path, stat);
T newData = updater.update(oldData);
writeData(path, newData, stat.getVersion());
} catch (ZkBadVersionException e) {
retry = true;
}
} while (retry);
}
Aggregations