byte[] existingBytes = ZKUtil.getDataNoWatch(zkw, node, stat);
if (existingBytes == null) {
// Node no longer exists. Return -1. It means unsuccessful transition.
return -1;
}
EntityGroupTransaction rt = getEntityGroupTransition(existingBytes);
// Verify it is the expected version
if (expectedVersion != -1 && stat.getVersion() != expectedVersion) {
LOG.warn(zkw.prefix("Attempt to transition the " + "unassigned node for "
+ encoded + " from " + beginState + " to " + endState + " failed, "
+ "the node existed but was version " + stat.getVersion()
+ " not the expected version " + expectedVersion));
return -1;
} else if (beginState.equals(EventType.M_ZK_ENTITYGROUP_OFFLINE)
&& endState.equals(EventType.FSERVER_ZK_ENTITYGROUP_OPENING)
&& expectedVersion == -1 && stat.getVersion() != 0) {
// the below check ensures that double assignment doesnot happen.
// When the node is created for the first time then the expected version
// that is passed will be -1 and the version in znode will be 0.
// In all other cases the version in znode will be > 0.
LOG.warn(zkw.prefix("Attempt to transition the " + "unassigned node for "
+ encoded + " from " + beginState + " to " + endState + " 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(beginState)) {
LOG.warn(zkw.prefix("Attempt to transition the " + "unassigned node for "
+ encoded + " from " + beginState + " to " + endState + " failed, "
+ "the node existed but was in the state " + et
+ " set by the server " + serverName));
return -1;
}
// Write new data, ensuring data has not changed since we last read it
try {
rt = EntityGroupTransaction.createEntityGroupTransition(endState,
entityGroup.getEntityGroupName(), serverName, payload);
if (!ZKUtil.setData(zkw, node, rt.toByteArray(), stat.getVersion())) {
LOG.warn(zkw.prefix("Attempt to transition the "
+ "unassigned node for " + encoded + " from " + beginState + " to "
+ endState + " failed, "
+ "the node existed and was in the expected state but then when "
+ "setting data we got a version mismatch"));