use of org.apache.helix.HelixProperty in project helix by apache.
the class ZKHelixDataAccessor method getProperty.
@Override
public <T extends HelixProperty> T getProperty(PropertyKey key) {
PropertyType type = key.getType();
String path = key.getPath();
int options = constructOptions(type);
ZNRecord record = null;
try {
Stat stat = new Stat();
record = _baseDataAccessor.get(path, stat, options);
if (record != null) {
record.setCreationTime(stat.getCtime());
record.setModifiedTime(stat.getMtime());
record.setVersion(stat.getVersion());
}
} catch (ZkNoNodeException e) {
// OK
}
switch(type) {
case CURRENTSTATES:
case IDEALSTATES:
case EXTERNALVIEW:
// check if bucketized
if (record != null) {
HelixProperty property = new HelixProperty(record);
int bucketSize = property.getBucketSize();
if (bucketSize > 0) {
// @see HELIX-574
// clean up list and map fields in case we write to parent node by mistake
property.getRecord().getMapFields().clear();
property.getRecord().getListFields().clear();
List<ZNRecord> childRecords = _baseDataAccessor.getChildren(path, null, options);
ZNRecord assembledRecord = new ZNRecordAssembler().assemble(childRecords);
// merge with parent node value
if (assembledRecord != null) {
record.getSimpleFields().putAll(assembledRecord.getSimpleFields());
record.getListFields().putAll(assembledRecord.getListFields());
record.getMapFields().putAll(assembledRecord.getMapFields());
}
}
}
break;
default:
break;
}
@SuppressWarnings("unchecked") T t = (T) HelixProperty.convertToTypedInstance(key.getTypeClass(), record);
return t;
}
use of org.apache.helix.HelixProperty in project helix by apache.
the class CallbackHandler method subscribeForChanges.
private void subscribeForChanges(NotificationContext.Type callbackType, String path, boolean watchChild) {
long start = System.currentTimeMillis();
if (_eventTypes.contains(EventType.NodeDataChanged) || _eventTypes.contains(EventType.NodeCreated) || _eventTypes.contains(EventType.NodeDeleted)) {
if (logger.isDebugEnabled()) {
logger.debug("Subscribing data change listener to path:" + path);
}
subscribeDataChange(path, callbackType);
}
if (_eventTypes.contains(EventType.NodeChildrenChanged)) {
if (logger.isDebugEnabled()) {
logger.debug("Subscribing child change listener to path:" + path);
}
subscribeChildChange(path, callbackType);
if (watchChild) {
if (logger.isDebugEnabled()) {
logger.debug("Subscribing data change listener to all children for path:" + path);
}
try {
switch(_changeType) {
case CURRENT_STATE:
case IDEAL_STATE:
case EXTERNAL_VIEW:
case TARGET_EXTERNAL_VIEW:
{
// check if bucketized
BaseDataAccessor<ZNRecord> baseAccessor = new ZkBaseDataAccessor<>(_zkClient);
List<ZNRecord> records = baseAccessor.getChildren(path, null, 0);
for (ZNRecord record : records) {
HelixProperty property = new HelixProperty(record);
String childPath = path + "/" + record.getId();
int bucketSize = property.getBucketSize();
if (bucketSize > 0) {
// subscribe both data-change and child-change on bucketized parent node
// data-change gives a delete-callback which is used to remove watch
subscribeChildChange(childPath, callbackType);
subscribeDataChange(childPath, callbackType);
// subscribe data-change on bucketized child
List<String> bucketizedChildNames = _zkClient.getChildren(childPath);
if (bucketizedChildNames != null) {
for (String bucketizedChildName : bucketizedChildNames) {
String bucketizedChildPath = childPath + "/" + bucketizedChildName;
subscribeDataChange(bucketizedChildPath, callbackType);
}
}
} else {
subscribeDataChange(childPath, callbackType);
}
}
break;
}
default:
{
List<String> childNames = _zkClient.getChildren(path);
if (childNames != null) {
for (String childName : childNames) {
String childPath = path + "/" + childName;
subscribeDataChange(childPath, callbackType);
}
}
break;
}
}
} catch (ZkNoNodeException e) {
logger.warn("fail to subscribe child/data change. path: " + path + ", listener: " + _listener, e);
}
}
}
long end = System.currentTimeMillis();
logger.info("Subscribing to path:" + path + " took:" + (end - start));
}
Aggregations