ResourceLimitAttributesProvisioner { public static void provisionMemory( InternalMachineConfig machineConfig, long memoryLimit, long memoryRequest, long defaultMemoryLimit, long defaultMemoryRequest) { if (defaultMemoryRequest > defaultMemoryLimit) { defaultMemoryRequest = defaultMemoryLimit; LOG.warn( "Requested default container resource limit is less than default request. Request parameter will be ignored."); } if (memoryLimit <= 0 && defaultMemoryLimit > 0) { memoryLimit = defaultMemoryLimit; } if (memoryRequest <= 0 && defaultMemoryRequest > 0) { memoryRequest = defaultMemoryRequest; } if (memoryRequest > memoryLimit) { memoryRequest = memoryLimit; } final Map<String, String> attributes = machineConfig.getAttributes(); String configuredLimit = attributes.get(MEMORY_LIMIT_ATTRIBUTE); String configuredRequest = attributes.get(MEMORY_REQUEST_ATTRIBUTE); if (isNullOrEmpty(configuredLimit) || Long.parseLong(configuredLimit) <= 0) { attributes.put(MEMORY_LIMIT_ATTRIBUTE, String.valueOf(memoryLimit)); } if (isNullOrEmpty(configuredRequest) || Long.parseLong(configuredRequest) <= 0) { attributes.put(MEMORY_REQUEST_ATTRIBUTE, String.valueOf(memoryRequest)); } }  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 testSetsRamDefaultAttributesWhenTheyAreMissingInConfigAndNotPassedInRecipe() { long defaultMemoryLimit = 2048L; long defaultMemoryRequest = 1024L; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, 0L, 0L, defaultMemoryLimit, defaultMemoryRequest); long memLimit = Long.parseLong(machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE)); long memRequest = Long.parseLong(machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE)); assertEquals(memLimit, defaultMemoryLimit); assertEquals(memRequest, defaultMemoryRequest); }
@Test public void testRamDefaultMemoryRequestIsIgnoredIfGreaterThanDefaultRamLimit() { long defaultMemoryLimit = 1024L; long defaultMemoryRequest = 2048L; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, 0L, 0L, defaultMemoryLimit, defaultMemoryRequest); long memLimit = Long.parseLong(machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE)); long memRequest = Long.parseLong(machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE)); assertEquals(memLimit, defaultMemoryLimit); assertEquals(memRequest, defaultMemoryLimit); }
@Test public void testSkipDefaultMemoryAttributesWhenTheyAreNegative() { long defaultMemoryLimit = -1L; long defaultMemoryRequest = -1024L; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, 0L, 0L, defaultMemoryLimit, defaultMemoryRequest); long memLimit = Long.parseLong(machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE)); long memRequest = Long.parseLong(machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE)); assertEquals(memLimit, 0L); assertEquals(memRequest, 0L); }
@Test public void testRamAttributesAreTakenFromRecipeWhenNotPresentInConfig() { long defaultMemoryLimit = 1024L; long defaultMemoryRequest = 2048L; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); long recipeLimit = 4096L; long recipeRequest = 2048L; ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, recipeLimit, recipeRequest, defaultMemoryLimit, defaultMemoryRequest); assertEquals( machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals( machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(recipeRequest)); }
@Test public void testWhenRamAttributesTakenFromRecipeAreInconsistentAndNotPresentInConfigRequestIsIgnored() { long defaultMemoryLimit = 1024L; long defaultMemoryRequest = 2048L; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); long recipeLimit = 2048L; long recipeRequest = 4096L; ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, recipeLimit, recipeRequest, defaultMemoryLimit, defaultMemoryRequest); assertEquals( machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals( machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(recipeLimit)); }
@Test public void testWhenRamAttributesArePresentInMachineConfigValuesInRecipeAreIgnored() { long defaultMemoryLimit = 1024L; long defaultMemoryRequest = 2048L; InternalMachineConfig machineConfig = mockInternalMachineConfig( ImmutableMap.of(MEMORY_LIMIT_ATTRIBUTE, "1526", MEMORY_REQUEST_ATTRIBUTE, "512")); long recipeLimit = 4096L; long recipeRequest = 2048L; ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, recipeLimit, recipeRequest, defaultMemoryLimit, defaultMemoryRequest); assertEquals(machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(1526L)); assertEquals(machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(512L)); }
@Test public void testWhenRamAttributesArePresentInMachineAreNegativeDefaultsShouldBeApplied() { long defaultMemoryLimit = 2048L; long defaultMemoryRequest = 1024L; Map<String, String> attributes = new HashMap<>(); attributes.put(MEMORY_LIMIT_ATTRIBUTE, "-1"); attributes.put(MEMORY_REQUEST_ATTRIBUTE, "-1"); InternalMachineConfig machineConfig = mockInternalMachineConfig(attributes); ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, 0L, 0L, defaultMemoryLimit, defaultMemoryRequest); assertEquals( machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(defaultMemoryLimit)); assertEquals( machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(defaultMemoryRequest)); }
@Test public void testWhenRamRequestAttributeIsPresentInMachineConfigValuesInRecipeAreIgnored() { long defaultMemoryLimit = 1024L; long defaultMemoryRequest = 2048L; Map<String, String> attributes = new HashMap<>(); attributes.put(MEMORY_REQUEST_ATTRIBUTE, "512"); InternalMachineConfig machineConfig = mockInternalMachineConfig(attributes); long recipeLimit = 4096L; long recipeRequest = 2048L; ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, recipeLimit, recipeRequest, defaultMemoryLimit, defaultMemoryRequest); assertEquals( machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals(machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(512L)); }
@Test public void testWhenRamLimitAttributeIsPresentInMachineConfigValuesInRecipeAreIgnored() { long defaultMemoryLimit = 1024L; long defaultMemoryRequest = 2048L; Map<String, String> attributes = new HashMap<>(); attributes.put(MEMORY_LIMIT_ATTRIBUTE, "1526"); InternalMachineConfig machineConfig = mockInternalMachineConfig(attributes); long recipeLimit = 4096L; long recipeRequest = 2048L; ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, recipeLimit, recipeRequest, defaultMemoryLimit, defaultMemoryRequest); assertEquals(machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(1526L)); assertEquals( machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(defaultMemoryRequest)); }
@Test public void testWhenRamAttributesAreNotPresentInMachineConfigAndOnlyRequestIsProvidedInRecipe() { long defaultMemoryLimit = 2048L; long defaultMemoryRequest = 1024L; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); long recipeRequest = 1526L; ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, 0, recipeRequest, defaultMemoryLimit, defaultMemoryRequest); assertEquals( machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(defaultMemoryLimit)); assertEquals( machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(recipeRequest)); }
@Test public void testWhenRamAttributesAreNotPresentInMachineConfigAndOnlyLimitIsProvidedInRecipe() { long defaultMemoryLimit = 2048L; long defaultMemoryRequest = 1024L; InternalMachineConfig machineConfig = mockInternalMachineConfig(new HashMap<>()); long recipeLimit = 1526L; ResourceLimitAttributesProvisioner.provisionMemory( machineConfig, recipeLimit, 0, defaultMemoryLimit, defaultMemoryRequest); assertEquals( machineConfig.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE), String.valueOf(recipeLimit)); assertEquals( machineConfig.getAttributes().get(MEMORY_REQUEST_ATTRIBUTE), String.valueOf(defaultMemoryRequest)); }