protected void merge(CounterSubTaskState patchBody) { this.customProperties = mergeCustomProperties(this.customProperties, patchBody.customProperties); } }
private Map<String, HostSelection> buildHostSelectionMap(QueryResult rpQueryResult) { Collection<ComputeState> computes = rpQueryResult.computesByLink.values(); final Map<String, HostSelection> initHostSelectionMap = new LinkedHashMap<>( computes.size()); for (ComputeState computeState : computes) { final HostSelection hostSelection = new HostSelection(); hostSelection.hostLink = computeState.documentSelfLink; hostSelection.resourcePoolLinks = rpQueryResult.rpLinksByComputeLink .get(computeState.documentSelfLink); hostSelection.deploymentPolicyLink = computeState.customProperties .get(ContainerHostService.CUSTOM_PROPERTY_DEPLOYMENT_POLICY); hostSelection.availableMemory = getPropertyLong( computeState.customProperties, ContainerHostService.DOCKER_HOST_AVAILABLE_MEMORY_PROP_NAME) .orElse(Long.MAX_VALUE); hostSelection.clusterStore = computeState.customProperties .get(ContainerHostService.DOCKER_HOST_CLUSTER_STORE_PROP_NAME); hostSelection.plugins = computeState.customProperties .get(ContainerHostService.DOCKER_HOST_PLUGINS_PROP_NAME); hostSelection.name = computeState.name != null ? computeState.name : "N/A"; hostSelection.hostType = ContainerHostUtil.getDeclaredContainerHostType(computeState); initHostSelectionMap.put(hostSelection.hostLink, hostSelection); } return initHostSelectionMap; }
/** * Perform shallow merge of ServiceDocuments using reflection * * @param copyTo * @param copyFrom */ public static void mergeServiceDocuments(ServiceDocument copyTo, ServiceDocument copyFrom) { mergeServiceDocuments(copyTo, copyFrom, SHALLOW_MERGE_STRATEGY); }
private ComputeStats getContainerHostStats(ComputeState compute, ComputeDescription description) { ComputeStats stats = new ComputeStats(); stats.totalMemoryBytes = PropertyUtils.getPropertyLong(compute.customProperties, ContainerHostService.DOCKER_HOST_TOTAL_MEMORY_PROP_NAME).orElse(0L); stats.cpuCoreCount = PropertyUtils.getPropertyLong(compute.customProperties, ContainerHostService.DOCKER_HOST_NUM_CORES_PROP_NAME).orElse(1L); stats.availableMemoryBytes = PropertyUtils.getPropertyLong(compute.customProperties, ContainerHostService.DOCKER_HOST_AVAILABLE_MEMORY_PROP_NAME).orElse( stats.totalMemoryBytes); stats.cpuUsage = PropertyUtils.getPropertyDouble(compute.customProperties, ContainerHostService.DOCKER_HOST_CPU_USAGE_PCT_PROP_NAME).orElse(0.0); return stats; }
@Override public void handlePatch(Operation patch) { CompositeDescription currentState = getState(patch); CompositeDescription patchBody = patch.getBody(CompositeDescription.class); ServiceDocumentDescription docDesc = getDocumentTemplate().documentDescription; String currentSignature = Utils.computeSignature(currentState, docDesc); currentState.name = mergeProperty(currentState.name, patchBody.name); currentState.status = mergeProperty(currentState.status, patchBody.status); currentState.lastPublished = mergeProperty(currentState.lastPublished, patchBody.lastPublished); if (patchBody.parentDescriptionLink != null && patchBody.parentDescriptionLink.trim().isEmpty()) { currentState.parentDescriptionLink = null; } else { currentState.parentDescriptionLink = mergeProperty(currentState.parentDescriptionLink, patchBody.parentDescriptionLink); } currentState.descriptionLinks = mergeProperty(currentState.descriptionLinks, patchBody.descriptionLinks); currentState.customProperties = mergeCustomProperties( currentState.customProperties, patchBody.customProperties); String newSignature = Utils.computeSignature(currentState, docDesc); // if the signature hasn't change we shouldn't modify the state if (currentSignature.equals(newSignature)) { currentState = null; patch.setStatusCode(Operation.STATUS_CODE_NOT_MODIFIED); } patch.setBody(currentState).complete(); }
@Override public void handlePatch(Operation patch) { ContainerNetworkDescription currentState = getState(patch); ContainerNetworkDescription patchBody = getValidInputFrom(patch, currentState); ServiceDocumentDescription docDesc = getDocumentTemplate().documentDescription; String currentSignature = Utils.computeSignature(currentState, docDesc); PropertyUtils.mergeServiceDocuments(currentState, patchBody, NetworkUtils.SHALLOW_MERGE_SKIP_MAPS_STRATEGY); PropertyUtils.mergeCustomProperties(currentState.options, patchBody.options); if (patchBody.parentDescriptionLink != null && patchBody.parentDescriptionLink.trim().isEmpty()) { currentState.parentDescriptionLink = null; } String newSignature = Utils.computeSignature(currentState, docDesc); boolean changed = !newSignature.equals(currentSignature); if (!changed) { patch.setStatusCode(Operation.STATUS_CODE_NOT_MODIFIED); } patch.complete(); }
ePZClusterDto.name = resourcePoolState.name; ePZClusterDto.clusterCreationTimeMicros = PropertyUtils .getPropertyLong(resourcePoolState.customProperties, ClusterService.CLUSTER_CREATION_TIME_MICROS_CUSTOM_PROP) .orElse(0L); ePZClusterDto.details = PropertyUtils.getPropertyString(resourcePoolState.customProperties, ClusterService.CLUSTER_DETAILS_CUSTOM_PROP).orElse(""); ePZClusterDto.totalMemory = resourcePoolState.maxMemoryBytes == null ? 0 : resourcePoolState.maxMemoryBytes; ePZClusterDto.memoryUsage = ePZClusterDto.totalMemory - PropertyUtils.getPropertyLong( resourcePoolState.customProperties, ContainerHostDataCollectionService.RESOURCE_POOL_AVAILABLE_MEMORY_CUSTOM_PROP) .orElse(0L); ePZClusterDto.cpuUsage = PropertyUtils.getPropertyDouble( resourcePoolState.customProperties, ContainerHostDataCollectionService.RESOURCE_POOL_CPU_USAGE_CUSTOM_PROP) : ClusterType.DOCKER; ePZClusterDto.type = ClusterType .valueOf(PropertyUtils.getPropertyString(resourcePoolState.customProperties, ClusterService.CLUSTER_TYPE_CUSTOM_PROP) .orElse(type.toString())); if (PlacementZoneUtil.isSchedulerPlacementZone(resourcePoolState)) { ePZClusterDto.address = computeStates.get(0).address; ePZClusterDto.totalCpu = PropertyUtils.getPropertyDouble( computeStates.get(0).customProperties, ContainerHostService.DOCKER_HOST_NUM_CORES_PROP_NAME)
String currentSignature = Utils.computeSignature(currentState, docDesc); currentState.name = PropertyUtils.mergeProperty(currentState.name, patchBody.name); currentState.compositeDescriptionLink = PropertyUtils.mergeProperty( currentState.compositeDescriptionLink, patchBody.compositeDescriptionLink); currentState.customProperties = PropertyUtils.mergeCustomProperties(currentState .customProperties, patchBody.customProperties); currentState.componentLinks = PropertyUtils.mergeLists(currentState.componentLinks, patchBody.componentLinks); currentState.tenantLinks = PropertyUtils.mergeLists(currentState.tenantLinks, patchBody.tenantLinks);
private DeferredResult<List<String>> collectTrustCert(ContainerHostRemovalTaskState state) { List<DeferredResult<String>> a = state.resourceLinks.stream().map(resourceLink -> sendWithDeferredResult(Operation .createGet(this, resourceLink), ComputeState.class) .thenApply(o -> PropertyUtils .getPropertyString(o.customProperties, ComputeConstants.HOST_TRUST_CERTS_PROP_NAME) .orElse(null)) ).collect(Collectors.toList()); return DeferredResult.allOf(a); }
public static String getAuthConfigFile(ServiceHost host) { return PropertyUtils.getValue(host, AUTH_CONFIG_FILE); }
@Override protected void customStateValidationAndMerge(Operation patch, ReservationTaskState patchBody, ReservationTaskState currentState) { if (SubStage.QUERYING_GLOBAL == patchBody.taskSubStage) { // In this case try global group instead of the provided one. currentState.tenantLinks = null; } // override without merging currentState.resourcePoolsPerGroupPlacementLinks = PropertyUtils.mergeProperty( currentState.resourcePoolsPerGroupPlacementLinks, patchBody.resourcePoolsPerGroupPlacementLinks); }
@SuppressWarnings("unchecked") public static <T> T getValue(Object instance, String fieldName) { try { Field field = findField(instance.getClass(), fieldName); return (T) field.get(instance); } catch (Exception e) { return null; } }
@Override public void handlePatch(Operation patch) { ContainerVolumeDescription currentState = getState(patch); ContainerVolumeDescription patchBody = getValidInputFrom(patch, true); ServiceDocumentDescription docDesc = getDocumentTemplate().documentDescription; String currentSignature = Utils.computeSignature(currentState, docDesc); PropertyUtils.mergeServiceDocuments(currentState, patchBody); PropertyUtils.mergeCustomProperties(currentState.customProperties, patchBody.customProperties); if (patchBody.parentDescriptionLink != null && patchBody.parentDescriptionLink.trim().isEmpty()) { currentState.parentDescriptionLink = null; } String newSignature = Utils.computeSignature(currentState, docDesc); boolean changed = !newSignature.equals(currentSignature); if (!changed) { patch.setStatusCode(Operation.STATUS_CODE_NOT_MODIFIED); } patch.complete(); }
for (ComputeState computeState : computeStates) { Long hostTotalMemory = PropertyUtils.getPropertyLong(computeState.customProperties, ContainerHostService.DOCKER_HOST_TOTAL_MEMORY_PROP_NAME).orElse(Long.MAX_VALUE); Long numCores = PropertyUtils.getPropertyLong(computeState.customProperties, ContainerHostService.DOCKER_HOST_NUM_CORES_PROP_NAME).orElse(1L); Double hostCpuUsage = PropertyUtils.getPropertyDouble(computeState.customProperties, ContainerHostService.DOCKER_HOST_CPU_USAGE_PCT_PROP_NAME).orElse(0.0); .getPropertyLong(computeState.customProperties, ContainerHostService.DOCKER_HOST_AVAILABLE_MEMORY_PROP_NAME) .orElse(PropertyUtils .getPropertyLong(computeState.customProperties, ContainerHostService.DOCKER_HOST_TOTAL_MEMORY_PROP_NAME) .orElse(0L));
static String namespacePrefix(KubernetesContext context, KubernetesDescription description, String apiVersionPrefix) { // If there is a namespace in the description, use it if (description != null && description.namespace != null && !description.namespace.isEmpty()) { return namespacePrefix(context, description.namespace, apiVersionPrefix); } // Otherwise, use the namespace stored in the k8s context // or default to the default namespace String namespace = KubernetesHostConstants.KUBERNETES_HOST_DEFAULT_NAMESPACE; if (context.host != null && context.host.customProperties != null) { namespace = PropertyUtils .getPropertyString(context.host.customProperties, KubernetesHostConstants.KUBERNETES_HOST_NAMESPACE_PROP_NAME) .orElse(namespace); } return namespacePrefix(context, namespace, apiVersionPrefix); }
private static String getAuthConfigFile(ServiceHost host) { return PropertyUtils.getValue(host, AUTH_CONFIG_FILE); }
private void prepareContext(final CompositionTaskState state, final CompositeDescriptionExpanded compositeDesc) { if (compositeDesc == null) { getCompositeDescription(state, (compDesc) -> prepareContext(state, compDesc)); return; } state.customProperties = mergeProperty(compositeDesc.customProperties, state.customProperties); if (state.customProperties == null) { state.customProperties = new HashMap<>(); } // add contextId if not added String contextId; if ((contextId = state.customProperties.get(FIELD_NAME_CONTEXT_ID_KEY)) == null) { contextId = getSelfId(); state.customProperties.put(FIELD_NAME_CONTEXT_ID_KEY, contextId); } proceedTo(SubStage.CONTEXT_PREPARED, s -> { s.customProperties = state.customProperties; s.descName = compositeDesc.name; s.externalSchedulerTaskLink = getExternalSchedulerTaskLink(compositeDesc); }); }
@SuppressWarnings("unchecked") private static Object tryGetValueFromCustomProperties(Class<?> type, Object value, String fieldName) { Object result = null; Field field = PropertyUtils.findField(type, ResourceState.FIELD_NAME_CUSTOM_PROPERTIES); if (field != null) { try { Map<String, Object> customProperties = (Map<String, Object>) field.get(value); result = customProperties.get(fieldName); } catch (ReflectiveOperationException e) { // Do nothing here } } return result; }
private static void transformDriversToComponentTemplate( ContainerNetworkDescription description) { Map<String, String> customProps = new HashMap<>(); if (description.driver != null) { customProps.put(ContainerNetworkDescription.CUSTOM_PROPERTY_NETWORK_DRIVER, description.driver); description.driver = null; } if (description.ipam != null && description.ipam.driver != null) { customProps.put(ContainerNetworkDescription.CUSTOM_PROPERTY_IPAM_DRIVER, description.ipam.driver); description.ipam.driver = null; } if (!customProps.isEmpty()) { description.customProperties = PropertyUtils .mergeCustomProperties(description.customProperties, customProps); } }
/** * Returns whether the projects state signature was changed after the patch. */ private DeferredResult<Boolean> handleProjectPatch(ProjectState currentState, ProjectState patchState) { ServiceDocumentDescription docDesc = getDocumentTemplate().documentDescription; String currentSignature = Utils.computeSignature(currentState, docDesc); DeferredResult<Integer> projectIndex; if (currentState.customProperties == null) { projectIndex = generateProjectIndex(); } else { projectIndex = DeferredResult.completed(Integer.parseInt( currentState.customProperties.get(CUSTOM_PROPERTY_PROJECT_INDEX))); } return projectIndex.thenApply(index -> { Map<String, String> mergedProperties = PropertyUtils.mergeCustomProperties( currentState.customProperties, patchState.customProperties); PropertyUtils.mergeServiceDocuments(currentState, patchState); currentState.customProperties = mergedProperties; handleProjectIndex(index, currentState); String newSignature = Utils.computeSignature(currentState, docDesc); return !currentSignature.equals(newSignature); }); }