Repository: helix Updated Branches: refs/heads/master 16f4a9b7c -> 03e8580ec
Add instance support for CriteriaEvalutator Project: http://git-wip-us.apache.org/repos/asf/helix/repo Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/03e8580e Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/03e8580e Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/03e8580e Branch: refs/heads/master Commit: 03e8580ec21fe233a56dc664b4fb02368bfa6943 Parents: 16f4a9b Author: Junkai Xue <[email protected]> Authored: Thu Jan 11 13:32:50 2018 -0800 Committer: Junkai Xue <[email protected]> Committed: Thu Jan 11 13:32:50 2018 -0800 ---------------------------------------------------------------------- .../main/java/org/apache/helix/PropertyKey.java | 8 +++ .../helix/messaging/CriteriaEvaluator.java | 68 +++++++++++--------- 2 files changed, 46 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/helix/blob/03e8580e/helix-core/src/main/java/org/apache/helix/PropertyKey.java ---------------------------------------------------------------------- diff --git a/helix-core/src/main/java/org/apache/helix/PropertyKey.java b/helix-core/src/main/java/org/apache/helix/PropertyKey.java index 20cb833..fb4b4cc 100644 --- a/helix-core/src/main/java/org/apache/helix/PropertyKey.java +++ b/helix-core/src/main/java/org/apache/helix/PropertyKey.java @@ -310,6 +310,14 @@ public class PropertyKey { public PropertyKey instances() { return new PropertyKey(PropertyType.INSTANCES, null, _clusterName); } + + /** + * Get a property key associated with specified instance + * @return {@link PropertyKey} + */ + public PropertyKey instance(String instanceName) { + return new PropertyKey(PropertyType.INSTANCES, null, _clusterName, instanceName); + } /** * Get a property key associated with {@link Message} for an instance http://git-wip-us.apache.org/repos/asf/helix/blob/03e8580e/helix-core/src/main/java/org/apache/helix/messaging/CriteriaEvaluator.java ---------------------------------------------------------------------- diff --git a/helix-core/src/main/java/org/apache/helix/messaging/CriteriaEvaluator.java b/helix-core/src/main/java/org/apache/helix/messaging/CriteriaEvaluator.java index 398ef2b..bf0f103 100644 --- a/helix-core/src/main/java/org/apache/helix/messaging/CriteriaEvaluator.java +++ b/helix-core/src/main/java/org/apache/helix/messaging/CriteriaEvaluator.java @@ -41,6 +41,7 @@ import com.google.common.collect.Sets; public class CriteriaEvaluator { private static Logger logger = Logger.getLogger(CriteriaEvaluator.class); + public static final String MATCH_ALL_SYM = "%"; /** * Examine persisted data to match wildcards in {@link Criteria} @@ -55,38 +56,29 @@ public class CriteriaEvaluator { List<HelixProperty> properties; DataSource dataSource = recipientCriteria.getDataSource(); - if (dataSource == DataSource.EXTERNALVIEW) { - String resourceName = recipientCriteria.getResource(); - if (Strings.isNullOrEmpty(resourceName)) { - properties = accessor.getChildValues(keyBuilder.externalViews()); - } else { - HelixProperty data = accessor.getProperty(keyBuilder.externalView(resourceName)); - if (data == null) { - throw new HelixException( - String.format("Specified resource %s externalView is not found!", resourceName)); - } - properties = Collections.singletonList(data); - } - } else if (dataSource == DataSource.IDEALSTATES) { - String resourceName = recipientCriteria.getResource(); - if (Strings.isNullOrEmpty(resourceName)) { - properties = accessor.getChildValues(keyBuilder.idealStates()); - } else { - HelixProperty data = accessor.getProperty(keyBuilder.idealStates(resourceName)); - if (data == null) { - throw new HelixException( - String.format("Specified resource %s idealState is not found!", resourceName)); - } - properties = Collections.singletonList(data); - } - } else if (dataSource == DataSource.LIVEINSTANCES) { - properties = accessor.getChildValues(keyBuilder.liveInstances()); - } else if (dataSource == DataSource.INSTANCES) { - properties = accessor.getChildValues(keyBuilder.instances()); - } else { + String resourceName = recipientCriteria.getResource(); + String instanceName = recipientCriteria.getInstanceName(); + + switch (dataSource) { + case EXTERNALVIEW: + properties = getProperty(accessor, resourceName, keyBuilder.externalViews(), + keyBuilder.externalView(resourceName), DataSource.EXTERNALVIEW.name()); + break; + case IDEALSTATES: + properties = getProperty(accessor, resourceName, keyBuilder.idealStates(), + keyBuilder.idealStates(resourceName), DataSource.IDEALSTATES.name()); + break; + case LIVEINSTANCES: + properties = getProperty(accessor, instanceName, keyBuilder.liveInstances(), + keyBuilder.liveInstance(instanceName), DataSource.LIVEINSTANCES.name()); + break; + case INSTANCES: + properties = getProperty(accessor, instanceName, keyBuilder.instances(), + keyBuilder.instance(instanceName), DataSource.INSTANCES.name()); + break; + default: return Lists.newArrayList(); } - // flatten the data List<ZNRecordRow> allRows = ZNRecordRow.flatten(HelixProperty.convertToList(properties)); @@ -172,4 +164,20 @@ public class CriteriaEvaluator { Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); return p.matcher(value).matches(); } + + private List<HelixProperty> getProperty(HelixDataAccessor accessor, String dataSpec, + PropertyKey propertyKeys, PropertyKey propertyKey, String dataType) { + List<HelixProperty> properties; + if (Strings.isNullOrEmpty(dataSpec) || dataSpec.equals(MATCH_ALL_SYM)) { + properties = accessor.getChildValues(propertyKeys); + } else { + HelixProperty data = accessor.getProperty(propertyKey); + if (data == null) { + throw new HelixException( + String.format("Specified %s %s is not found!", dataType, dataSpec)); + } + properties = Collections.singletonList(data); + } + return properties; + } }
