ResourceLimitAttributesProvisioner { public static void provisionCPU( InternalMachineConfig machineConfig, float cpuLimit, float cpuRequest, float defaultCPULimit, float defaultCPURequest) { if (defaultCPURequest > defaultCPULimit) { defaultCPURequest = defaultCPULimit; LOG.warn( "Requested default container resource limit is less than default request. Request parameter will be ignored."); } if (cpuLimit <= 0 && defaultCPULimit > 0) { cpuLimit = defaultCPULimit; } if (cpuRequest <= 0 && defaultCPURequest > 0) { cpuRequest = defaultCPURequest; } if (cpuRequest > cpuLimit) { cpuRequest = cpuLimit; } final Map<String, String> attributes = machineConfig.getAttributes(); String configuredLimit = attributes.get(CPU_LIMIT_ATTRIBUTE); String configuredRequest = attributes.get(CPU_REQUEST_ATTRIBUTE); if (isNullOrEmpty(configuredLimit) || Float.parseFloat(configuredLimit) <= 0) { attributes.put(CPU_LIMIT_ATTRIBUTE, Float.toString(cpuLimit)); } if (isNullOrEmpty(configuredRequest) || Float.parseFloat(configuredRequest) <= 0) { attributes.put(CPU_REQUEST_ATTRIBUTE, Float.toString(cpuRequest)); } }  static void provisionMemory(
      InternalMachineConfig machineConfig,
      long memoryLimit,
      long memoryRequest,
      long defaultMemoryLimit,
      long defaultMemoryRequest); static void provisionCPU(
      InternalMachineConfig machineConfig,
      float cpuLimit,
      float cpuRequest,
      float defaultCPULimit,
      float defaultCPURequest);  }
@Test public void testSetsCPUDefaultAttributesWhenTheyAreMissingInConfigAndNotPassedInRecipe() { float defaultCPULimit = 0.500f; float defaultCPURequest = 0.200f; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, 0, 0, defaultCPULimit, defaultCPURequest); float cpuLimit = Float.parseFloat(machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE)); float cpuRequest = Float.parseFloat(machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE)); assertEquals(cpuLimit, defaultCPULimit); assertEquals(cpuRequest, defaultCPURequest); }
@Test public void testSkipDefaultCPUAttributesWhenTheyAreNegative() { float defaultCPULimit = -1; float defaultCPURequest = -1; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, 0, 0, defaultCPULimit, defaultCPURequest); float cpuLimit = Float.parseFloat(machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE)); float cpuRequest = Float.parseFloat(machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE)); assertEquals(cpuLimit, 0); assertEquals(cpuRequest, 0); }
@Test public void testRamDefaultCPURequestIsIgnoredIfGreaterThanDefaultCPULimit() { float defaultCPULimit = 0.2f; float defaultCPURequest = 0.5f; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, 0L, 0L, defaultCPULimit, defaultCPURequest); float memLimit = Float.parseFloat(machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE)); float memRequest = Float.parseFloat(machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE)); assertEquals(memLimit, defaultCPULimit); assertEquals(memRequest, defaultCPULimit); }
@Test public void testCPUAttributesAreTakenFromRecipeWhenNotPresentInConfig() { float defaultCPULimit = 1.0f; float defaultCPURequest = 0.2f; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); float recipeLimit = 4f; float recipeRequest = 2f; ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, recipeLimit, recipeRequest, defaultCPULimit, defaultCPURequest); assertEquals( machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals( machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(recipeRequest)); }
@Test public void testWhenCPUAttributesTakenFromRecipeAreInconsistentAndNotPresentInConfigRequestIsIgnored() { float defaultCPULimit = 0.2f; float defaultCPURequest = 0.5f; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); float recipeLimit = 0.3f; float recipeRequest = 0.6f; ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, recipeLimit, recipeRequest, defaultCPULimit, defaultCPURequest); assertEquals( machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals( machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(recipeLimit)); }
@Test public void testWhenCPUAttributesArePresentInMachineConfigValuesInRecipeAreIgnored() { float defaultCPULimit = 0.5f; float defaultCPURequest = 0.2f; InternalMachineConfig machineConfig = mockInternalMachineConfig( ImmutableMap.of(CPU_LIMIT_ATTRIBUTE, "0.512", CPU_REQUEST_ATTRIBUTE, "0.152")); float recipeLimit = 0.6f; float recipeRequest = 0.3f; ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, recipeLimit, recipeRequest, defaultCPULimit, defaultCPURequest); assertEquals(machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(0.512f)); assertEquals(machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(0.152f)); }
@Test public void testWhenCPURequestAttributeIsPresentInMachineConfigValuesInRecipeAreIgnored() { float defaultCPULimit = 0.5f; float defaultCPURequest = 0.2f; Map<String, String> attributes = new HashMap<>(); attributes.put(CPU_REQUEST_ATTRIBUTE, "0.512"); InternalMachineConfig machineConfig = mockInternalMachineConfig(attributes); float recipeLimit = 0.6f; float recipeRequest = 0.3f; ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, recipeLimit, recipeRequest, defaultCPULimit, defaultCPURequest); assertEquals( machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals(machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(0.512f)); }
@Test public void testWhenCPULimitAttributeIsPresentInMachineConfigValuesInRecipeAreIgnored() { float defaultCPULimit = 0.5f; float defaultCPURequest = 0.2f; Map<String, String> attributes = new HashMap<>(); attributes.put(CPU_LIMIT_ATTRIBUTE, "0.152"); InternalMachineConfig machineConfig = mockInternalMachineConfig(attributes); float recipeLimit = 0.6f; float recipeRequest = 0.3f; ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, recipeLimit, recipeRequest, defaultCPULimit, defaultCPURequest); assertEquals(machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(0.152f)); assertEquals( machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(recipeRequest)); }
@Test public void testWhenCPULimitAttributeIsPresentInMachineAreNegativeDefaultsShouldBeApplied() { float defaultCPULimit = 0.5f; float defaultCPURequest = 0.2f; Map<String, String> attributes = new HashMap<>(); attributes.put(CPU_LIMIT_ATTRIBUTE, "-1"); attributes.put(CPU_REQUEST_ATTRIBUTE, "-1"); InternalMachineConfig machineConfig = mockInternalMachineConfig(attributes); ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, 0, 0, defaultCPULimit, defaultCPURequest); assertEquals( machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(defaultCPULimit)); assertEquals( machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(defaultCPURequest)); }
@Test public void testWhenCPUAttributesAreNotPresentInMachineConfigAndOnlyRequestIsProvidedInRecipe() { float defaultCPULimit = 0.5f; float defaultCPURequest = 0.2f; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); float recipeRequest = 0.1526f; ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, 0, recipeRequest, defaultCPULimit, defaultCPURequest); assertEquals( machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(defaultCPULimit)); assertEquals( machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(recipeRequest)); }
@Test public void testWhenCPUAttributesAreNotPresentInMachineConfigAndOnlyLimitIsProvidedInRecipe() { float defaultCPULimit = 0.5f; float defaultCPURequest = 0.2f; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); float recipeLimit = 0.152f; ResourceLimitAttributesProvisioner.provisionCPU( machineConfig, recipeLimit, 0, defaultCPULimit, defaultCPURequest); assertEquals( machineConfig.getAttributes().get(CPU_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals( machineConfig.getAttributes().get(CPU_REQUEST_ATTRIBUTE), String.valueOf(recipeLimit)); }