@Override @SuppressWarnings({"unchecked", "rawtypes"}) public List<LockDefinition> extractAllocationLockDefinitions(Instance instance) { Map env = DataAccessor.fields(instance) .withKey(InstanceConstants.FIELD_ENVIRONMENT) .as(jsonMapper, Map.class); List<LockDefinition> lockDefs = extractAllocationLockDefinitionsFromEnv(env); Map labels = DataAccessor.fields(instance) .withKey(InstanceConstants.FIELD_LABELS) .as(jsonMapper, Map.class); if (labels == null) { return lockDefs; } Iterator<Map.Entry> iter = labels.entrySet().iterator(); while (iter.hasNext()) { Map.Entry affinityDef = iter.next(); String key = ((String) affinityDef.getKey()).toLowerCase(); String valueStr = (String) affinityDef.getValue(); valueStr = valueStr == null ? "" : valueStr.toLowerCase(); if (instance != null) { valueStr = evaluateMacros(valueStr, instance); } String opStr = ""; if (key.startsWith(ContainerLabelAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER_LABEL)) { opStr = key.substring( ContainerLabelAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER_LABEL.length()); List<AffinityConstraintDefinition> defs = extractAffinityConstraintDefinitionFromLabel(opStr, valueStr, true); for (AffinityConstraintDefinition def : defs) { lockDefs.add( new AllocateConstraintLock(AllocateConstraintLock.Type.AFFINITY, def.getValue())); } } else if (key.startsWith(ContainerAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER)) { opStr = key.substring(ContainerAffinityConstraint.LABEL_HEADER_AFFINITY_CONTAINER.length()); List<AffinityConstraintDefinition> defs = extractAffinityConstraintDefinitionFromLabel(opStr, valueStr, false); for (AffinityConstraintDefinition def : defs) { lockDefs.add( new AllocateConstraintLock(AllocateConstraintLock.Type.AFFINITY, def.getValue())); } } } return lockDefs; }
@SuppressWarnings({"unchecked", "rawtypes"}) private List<LockDefinition> extractAllocationLockDefinitionsFromEnv(Map env) { List<LockDefinition> constraints = new ArrayList<LockDefinition>(); if (env != null) { Set<String> affinityDefinitions = env.keySet(); for (String affinityDef : affinityDefinitions) { if (affinityDef == null) { continue; } if (affinityDef.startsWith(ContainerAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER)) { affinityDef = affinityDef.substring( ContainerAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER.length()); AffinityConstraintDefinition def = extractAffinitionConstraintDefinitionFromEnv(affinityDef); if (def != null && !StringUtils.isEmpty(def.getValue())) { constraints.add( new AllocateConstraintLock(AllocateConstraintLock.Type.AFFINITY, def.getValue())); } } else if (affinityDef.startsWith( ContainerLabelAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER_LABEL)) { affinityDef = affinityDef.substring( ContainerLabelAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER_LABEL.length()); AffinityConstraintDefinition def = extractAffinitionConstraintDefinitionFromEnv(affinityDef); if (def != null && !StringUtils.isEmpty(def.getKey())) { constraints.add( new AllocateConstraintLock(AllocateConstraintLock.Type.AFFINITY, def.getValue())); } } } } return constraints; }
@Override @SuppressWarnings({"unchecked", "rawtypes"}) public List<Constraint> extractConstraintsFromEnv(Map env) { List<Constraint> constraints = new ArrayList<Constraint>(); if (env != null) { Set<String> affinityDefinitions = env.keySet(); for (String affinityDef : affinityDefinitions) { if (affinityDef == null) { continue; } if (affinityDef.startsWith(ContainerAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER)) { affinityDef = affinityDef.substring( ContainerAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER.length()); AffinityConstraintDefinition def = extractAffinitionConstraintDefinitionFromEnv(affinityDef); if (def != null && !StringUtils.isEmpty(def.getValue())) { constraints.add(new ContainerAffinityConstraint(def, objectManager, instanceDao)); } } else if (affinityDef.startsWith(HostAffinityConstraint.ENV_HEADER_AFFINITY_HOST_LABEL)) { affinityDef = affinityDef.substring(HostAffinityConstraint.ENV_HEADER_AFFINITY_HOST_LABEL.length()); AffinityConstraintDefinition def = extractAffinitionConstraintDefinitionFromEnv(affinityDef); if (def != null && !StringUtils.isEmpty(def.getKey())) { constraints.add(new HostAffinityConstraint(def, allocatorDao)); } } else if (affinityDef.startsWith( ContainerLabelAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER_LABEL)) { affinityDef = affinityDef.substring( ContainerLabelAffinityConstraint.ENV_HEADER_AFFINITY_CONTAINER_LABEL.length()); AffinityConstraintDefinition def = extractAffinitionConstraintDefinitionFromEnv(affinityDef); if (def != null && !StringUtils.isEmpty(def.getKey())) { constraints.add(new ContainerLabelAffinityConstraint(def, allocatorDao)); } } } } return constraints; }