Search in sources :

Example 21 with HelixProperty

use of org.apache.helix.HelixProperty in project helix by apache.

the class JobQueuesResource method getHostedEntitiesRepresentation.

StringRepresentation getHostedEntitiesRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
    // Get all resources
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    Map<String, HelixProperty> resourceConfigMap = accessor.getChildValuesMap(keyBuilder.resourceConfigs());
    // Create the result
    ZNRecord hostedEntitiesRecord = new ZNRecord("JobQueues");
    // Filter out non-workflow resources
    Iterator<Map.Entry<String, HelixProperty>> it = resourceConfigMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, HelixProperty> e = it.next();
        HelixProperty resource = e.getValue();
        Map<String, String> simpleFields = resource.getRecord().getSimpleFields();
        boolean isTerminable = resource.getRecord().getBooleanField(WorkflowConfig.WorkflowConfigProperty.Terminable.name(), true);
        if (!simpleFields.containsKey(WorkflowConfig.WorkflowConfigProperty.TargetState.name()) || !simpleFields.containsKey(WorkflowConfig.WorkflowConfigProperty.Dag.name()) || isTerminable) {
            it.remove();
        }
    }
    // Populate the result
    List<String> allResources = Lists.newArrayList(resourceConfigMap.keySet());
    hostedEntitiesRecord.setListField("JobQueues", allResources);
    StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixProperty(org.apache.helix.HelixProperty) StringRepresentation(org.restlet.representation.StringRepresentation) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord)

Example 22 with HelixProperty

use of org.apache.helix.HelixProperty in project helix by apache.

the class WorkflowsResource method getHostedEntitiesRepresentation.

StringRepresentation getHostedEntitiesRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
    // Get all resources
    ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
    HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
    PropertyKey.Builder keyBuilder = accessor.keyBuilder();
    Map<String, HelixProperty> resourceConfigMap = accessor.getChildValuesMap(keyBuilder.resourceConfigs());
    // Create the result
    ZNRecord hostedEntitiesRecord = new ZNRecord("Workflows");
    // Filter out non-workflow resources
    Iterator<Map.Entry<String, HelixProperty>> it = resourceConfigMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, HelixProperty> e = it.next();
        HelixProperty resource = e.getValue();
        Map<String, String> simpleFields = resource.getRecord().getSimpleFields();
        if (!simpleFields.containsKey(WorkflowConfig.WorkflowConfigProperty.TargetState.name()) || !simpleFields.containsKey(WorkflowConfig.WorkflowConfigProperty.Dag.name())) {
            it.remove();
        }
    }
    // Populate the result
    List<String> allResources = Lists.newArrayList(resourceConfigMap.keySet());
    hostedEntitiesRecord.setListField("WorkflowList", allResources);
    StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixProperty(org.apache.helix.HelixProperty) StringRepresentation(org.restlet.representation.StringRepresentation) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord)

Example 23 with HelixProperty

use of org.apache.helix.HelixProperty in project helix by apache.

the class ZKHelixDataAccessor method createChildren.

@Override
public <T extends HelixProperty> boolean[] createChildren(List<PropertyKey> keys, List<T> children) {
    // TODO: add validation
    int options = -1;
    List<String> paths = new ArrayList<String>();
    List<ZNRecord> records = new ArrayList<ZNRecord>();
    for (int i = 0; i < keys.size(); i++) {
        PropertyKey key = keys.get(i);
        PropertyType type = key.getType();
        String path = key.getPath();
        paths.add(path);
        HelixProperty value = children.get(i);
        records.add(value.getRecord());
        options = constructOptions(type);
    }
    return _baseDataAccessor.createChildren(paths, records, options);
}
Also used : HelixProperty(org.apache.helix.HelixProperty) ArrayList(java.util.ArrayList) PropertyType(org.apache.helix.PropertyType) ZNRecord(org.apache.helix.ZNRecord) PropertyKey(org.apache.helix.PropertyKey)

Example 24 with HelixProperty

use of org.apache.helix.HelixProperty in project helix by apache.

the class ZKHelixDataAccessor method getChildValues.

@Override
public <T extends HelixProperty> List<T> getChildValues(PropertyKey key, boolean throwException) {
    PropertyType type = key.getType();
    String parentPath = key.getPath();
    int options = constructOptions(type);
    List<T> childValues = new ArrayList<T>();
    List<ZNRecord> children;
    if (throwException) {
        children = _baseDataAccessor.getChildren(parentPath, null, options, 1, 0);
    } else {
        children = _baseDataAccessor.getChildren(parentPath, null, options);
    }
    if (children != null) {
        for (ZNRecord record : children) {
            switch(type) {
                case CURRENTSTATES:
                case IDEALSTATES:
                case EXTERNALVIEW:
                    if (record != null) {
                        HelixProperty property = new HelixProperty(record);
                        int bucketSize = property.getBucketSize();
                        if (bucketSize > 0) {
                            // TODO: fix this if record.id != pathName
                            String childPath = parentPath + "/" + record.getId();
                            List<ZNRecord> childRecords;
                            if (throwException) {
                                childRecords = _baseDataAccessor.getChildren(childPath, null, options, 1, 0);
                            } else {
                                childRecords = _baseDataAccessor.getChildren(childPath, 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;
            }
            if (record != null) {
                @SuppressWarnings("unchecked") T t = (T) HelixProperty.convertToTypedInstance(key.getTypeClass(), record);
                childValues.add(t);
            }
        }
    }
    return childValues;
}
Also used : HelixProperty(org.apache.helix.HelixProperty) ArrayList(java.util.ArrayList) PropertyType(org.apache.helix.PropertyType) ZNRecordAssembler(org.apache.helix.ZNRecordAssembler) ZNRecord(org.apache.helix.ZNRecord)

Example 25 with HelixProperty

use of org.apache.helix.HelixProperty in project helix by apache.

the class ZKHelixDataAccessor method createStateModelDef.

@Override
public boolean createStateModelDef(StateModelDefinition stateModelDef) {
    String path = PropertyPathBuilder.stateModelDef(_clusterName, stateModelDef.getId());
    HelixProperty property = getProperty(new PropertyKey.Builder(_clusterName).stateModelDef(stateModelDef.getId()));
    // Set new StateModelDefinition if it is different from old one.
    if (property != null) {
        // StateModelDefinition need to be updated
        if (!new StateModelDefinition(property.getRecord()).equals(stateModelDef)) {
            return stateModelDef.isValid() && _baseDataAccessor.set(path, stateModelDef.getRecord(), AccessOption.PERSISTENT);
        }
    } else {
        // StateModeDefinition does not exist
        return stateModelDef.isValid() && _baseDataAccessor.create(path, stateModelDef.getRecord(), AccessOption.PERSISTENT);
    }
    // StateModelDefinition exists but not need to be updated
    return true;
}
Also used : HelixProperty(org.apache.helix.HelixProperty) StateModelDefinition(org.apache.helix.model.StateModelDefinition) Builder(org.apache.helix.PropertyKey.Builder) PropertyPathBuilder(org.apache.helix.PropertyPathBuilder)

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