@Override protected void validateStateOnStart(CompositionSubTaskState state) throws IllegalArgumentException { boolean descAndResourcesEmpty = (state.resourceDescriptionLink == null || state.resourceDescriptionLink .isEmpty()) && (state.resourceLinks == null || state.resourceLinks.isEmpty()); assertTrue(!descAndResourcesEmpty, "resourceDescriptionLink and resourceLinks are empty"); }
private static double memoryConversion(double inMagnitude, String inUnit, String outUnit, boolean isDecimal) { int base = isDecimal ? 1000 : 1024; List<String> listOfUnits = isDecimal ? DECIMAL_MEMORY_UNITS : BINARY_MEMORY_UNITS; int inPow = listOfUnits.indexOf(inUnit.toLowerCase()); assertTrue(inPow >= 0, "inMagnitude set to incorrect unit."); int outPow = listOfUnits.indexOf(outUnit.toLowerCase()); assertTrue(outPow >= 0, "outMagnitude set to incorrect unit."); int pow = inPow - outPow; return inMagnitude * Math.pow(base, pow); }
private Map<String, HostSelection> selectHostPerResourceName(Collection<String> resourceNames, Collection<HostSelection> hostSelections) { AssertUtil.assertTrue(resourceNames.size() <= hostSelections.size(), "There should be a selected host for each resource"); Map<String, HostSelection> resourceNameToHostSelection = new HashMap<>(); Iterator<String> rnIterator = resourceNames.iterator(); Iterator<HostSelection> hsIterator = hostSelections.iterator(); while (rnIterator.hasNext() && hsIterator.hasNext()) { resourceNameToHostSelection.put(rnIterator.next(), hsIterator.next()); } return resourceNameToHostSelection; }
/** * Sets the maximum number of retries for the task. If 0, the task will not be retried. If * <code>null</code>, the number of retry delays will be implied. It is OK to specify for * maxRetries a number greater than the number of specified delays. The last delay will be * repeated in this case. */ public RetriableTaskBuilder<T> withMaximumRetries(Integer maxRetries) { assertTaskNotExecuted(); if (maxRetries != null) { AssertUtil.assertTrue(maxRetries >= 0, "maxRetries must be a non-negative integer"); } this.taskConfiguration.maxRetries = maxRetries; return this; }
public static Query addTenantGroupAndUserClause(String tenantLink) { List<String> listValues = null; if (tenantLink != null && !tenantLink.isEmpty()) { AssertUtil.assertTrue(tenantLink.startsWith(MultiTenantDocument.TENANTS_PREFIX) || tenantLink.startsWith(MultiTenantDocument.USERS_PREFIX), String.format("tenantLink does not have %s or %s prefix.", MultiTenantDocument.TENANTS_PREFIX, MultiTenantDocument.USERS_PREFIX)); listValues = Collections.singletonList(tenantLink); } return addTenantGroupAndUserClause(listValues); }
/** * Sets the retry delays for this task. They will be used in sequence until the maximum number * of retries are exceeded. If the maximum number of retries is bigger than the number of * delays, the last delay will be repeated. */ public RetriableTaskBuilder<T> withRetryDelays(Long... retryDelays) { assertTaskNotExecuted(); AssertUtil.assertNotNull(retryDelays, "retryDelays"); AssertUtil.assertTrue(retryDelays.length > 0, "retryDelays must contain at least one delay declaration"); AssertUtil.assertTrue(Stream.of(retryDelays).noneMatch(l -> l == null || l < 0), "retryDelays must contain only non-negative integers"); this.taskConfiguration.retryDelays = Collections .unmodifiableList(Arrays.asList(retryDelays)); return this; }
/** * Sets the retry delays for this task. They will be used in sequence until the maximum number * of retries are exceeded. If the maximum number of retries is bigger than the number of * delays, the last delay will be repeated. */ public RetriableTaskBuilder<T> withRetryDelays(List<Long> retryDelays) { assertTaskNotExecuted(); AssertUtil.assertNotNull(retryDelays, "retryDelays"); AssertUtil.assertTrue(retryDelays.size() > 0, "retryDelays must contain at least one delay declaration"); AssertUtil.assertTrue(retryDelays.stream().noneMatch(l -> l == null || l < 0), "retryDelays must contain only non-negative integers"); this.taskConfiguration.retryDelays = Collections.unmodifiableList(retryDelays); return this; }
private void performPut(Operation put, CompositeDescription putBody) { try { AssertUtil.assertTrue(putBody.getClass().equals(CompositeDescription.class), "State should be instance of CompositeDescription, not of any subclass."); this.setState(put, putBody); put.setBody(putBody).complete(); } catch (Throwable e) { put.fail(e); } }
assertTrue(state.memoryLimit == 0 || state.memoryLimit >= mem, "Memory limit must be at least " + mem + " bytes (or 0 if no limit)."); assertTrue(state.memorySwapLimit >= -1, "Memory swap limit must be greater or equal to -1."); assertTrue(state.maximumRetryCount >= 0, "Max retry count must be a non-negative integer."); assertTrue(state.networkMode.matches("bridge|host|none|container:\\w+"), "Network mode must be one of none, bridge, host, container:<name|id>."); assertTrue(state.restartPolicy.matches("no|always|on-failure"), "Restart policy must be one of no, on-failure, always.");
private void validate(ContainerHostSpec hostSpec) { final ComputeState cs = hostSpec.hostState; AssertUtil.assertNotNull(cs, "computeState"); AssertUtil.assertNotEmpty(cs.address, "address"); AssertUtil.assertNotEmpty(cs.customProperties, "customProperties"); String adapterDockerType = cs.customProperties.get(HOST_DOCKER_ADAPTER_TYPE_PROP_NAME); AssertUtil.assertNotEmpty(adapterDockerType, HOST_DOCKER_ADAPTER_TYPE_PROP_NAME); DockerAdapterType adapterType = DockerAdapterType.valueOf(adapterDockerType); AssertUtil.assertNotNull(adapterType, "adapterType"); cs.address = cs.address.trim(); String kubernetesNamespace = cs.customProperties.get( KubernetesHostConstants.KUBERNETES_HOST_NAMESPACE_PROP_NAME); if (kubernetesNamespace == null || kubernetesNamespace.isEmpty()) { cs.customProperties.put( KubernetesHostConstants.KUBERNETES_HOST_NAMESPACE_PROP_NAME, KubernetesHostConstants.KUBERNETES_HOST_DEFAULT_NAMESPACE); } else { kubernetesNamespace = kubernetesNamespace.trim(); AssertUtil.assertTrue(!kubernetesNamespace.contains("/") && !kubernetesNamespace.contains("\\"), "Namespace cannot contain" + " slashes."); AssertUtil.assertTrue(kubernetesNamespace.matches( KubernetesHostConstants.KUBERNETES_NAMESPACE_REGEX), "Invalid namespace."); cs.customProperties.put( KubernetesHostConstants.KUBERNETES_HOST_NAMESPACE_PROP_NAME, kubernetesNamespace); } }
private void validateTaskAndCalculateOptionalFields() { assertTaskNotExecuted(); AssertUtil.assertNotNullOrEmpty(taskConfiguration.taskId, "taskId"); AssertUtil.assertNotNull(taskConfiguration.serviceHost, "serviceHost"); AssertUtil.assertNotNull(taskConfiguration.taskFunction, "taskBody"); AssertUtil.assertNotNull(taskConfiguration.retryDelays, "retryDelays"); AssertUtil.assertNotNull(taskConfiguration.retryDelaysTimeUnit, "retryDelaysTimeUnit"); if (taskConfiguration.maxRetries == null) { taskConfiguration.maxRetries = taskConfiguration.retryDelays.size(); } else { AssertUtil.assertTrue(taskConfiguration.maxRetries >= 0, "maxRetries must be a non-negative integer"); } }
private List<ContainerHostType> getSupportedHostTypes( PlacementHostSelectionTaskState state, ComponentDescription desc) { ServiceDocument serviceDocument = desc.getServiceDocument(); if (serviceDocument instanceof CompositeDescription) { CompositeDescription cd = (CompositeDescription) serviceDocument; List<ContainerHostType> commonSupportedHostTypes = null; for (String descriptionLink : cd.descriptionLinks) { ComponentMeta meta = CompositeComponentRegistry .metaByDescriptionLink(descriptionLink); ResourceType resourceType = ResourceType.fromName(meta.resourceType); List<ContainerHostType> supportedHostTypes = ContainerHostUtil .getContainerHostTypesForResourceType(resourceType); if (commonSupportedHostTypes == null) { commonSupportedHostTypes = supportedHostTypes; } else { AssertUtil.assertTrue(commonSupportedHostTypes.equals(supportedHostTypes), "supported host types are not the same for different components"); } } return commonSupportedHostTypes; } else { ResourceType resourceType = ResourceType.fromName(state.resourceType); return ContainerHostUtil.getContainerHostTypesForResourceType(resourceType); } }
assertNotNull(state, "state should not be null"); assertNotNull(groupLinks, "groupLinks should not be null"); assertTrue(groupLinks.size() > 0, "There should be at least one groupLinks"); assertNotNull(resourceGroupLinks, "No resource groups found at [" + groupLinks + "]"); assertTrue(resourceGroupLinks.size() == 1, "There should only be " + "one resource group at [" + groupLinks + "]");
assertNotNull(authConfig.email, "email"); assertNotNull(authConfig.serveraddress, "serveraddress"); assertTrue(REGISTRY_USER.equals(authConfig.username), "X-Registry-Auth: username does not equal " + REGISTRY_USER); assertTrue(REGISTRY_PASSWORD.equals(authConfig.password), "X-Registry-Auth: password does not equal " + REGISTRY_PASSWORD);
private void inspectVolumeWithRetry(ContainerVolumeState volume, int retry) { AssertUtil.assertTrue(retry > 0, "Negative retry count."); AdapterRequest request = new AdapterRequest(); request.resourceReference = UriUtils.buildPublicUri(getHost(), volume.documentSelfLink); request.operationTypeId = VolumeOperationType.INSPECT.id; request.serviceTaskCallback = ServiceTaskCallback.createEmpty(); final int retriesRemaining = retry - 1; final int delaySeconds = (VOLUME_INSPECT_RETRY_COUNT - retriesRemaining) * VOLUME_INSPECT_RETRY_INTERVAL_SECONDS; sendRequest(Operation.createPatch(this, volume.adapterManagementReference.toString()) .setBodyNoCloning(request) .setCompletion((o, ex) -> { if (ex != null) { logWarning("Error while inspect volume: %s. Reties left: %d. Error: %s", volume.documentSelfLink, retriesRemaining, Utils.toString(ex)); if (retriesRemaining > 0) { getHost().schedule(() -> inspectVolumeWithRetry(volume, retriesRemaining), delaySeconds, TimeUnit.SECONDS); } } })); }