Search in sources :

Example 26 with HelixProperty

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;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) HelixProperty(org.apache.helix.HelixProperty) PropertyType(org.apache.helix.PropertyType) ZNRecordAssembler(org.apache.helix.ZNRecordAssembler) ZNRecord(org.apache.helix.ZNRecord)

Example 27 with HelixProperty

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));
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) HelixProperty(org.apache.helix.HelixProperty) BaseDataAccessor(org.apache.helix.BaseDataAccessor) List(java.util.List) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

HelixProperty (org.apache.helix.HelixProperty)27 PropertyKey (org.apache.helix.PropertyKey)16 ZNRecord (org.apache.helix.ZNRecord)15 HelixDataAccessor (org.apache.helix.HelixDataAccessor)9 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 PropertyType (org.apache.helix.PropertyType)5 ZkClient (org.apache.helix.manager.zk.ZkClient)5 IdealState (org.apache.helix.model.IdealState)5 HashMap (java.util.HashMap)4 InstanceConfig (org.apache.helix.model.InstanceConfig)4 List (java.util.List)3 HelixException (org.apache.helix.HelixException)3 Builder (org.apache.helix.PropertyKey.Builder)3 ZNRecordAssembler (org.apache.helix.ZNRecordAssembler)3 Test (org.testng.annotations.Test)3 StatsSnapshot (com.github.ambry.server.StatsSnapshot)2 Date (java.util.Date)2 Set (java.util.Set)2