PlacementHostSelectionTaskState placementTask = new PlacementHostSelectionTaskState(); placementTask.documentSelfLink = getSelfId(); placementTask.resourceDescriptionLink = state.resourceDescriptionLink;
@Test public void testProvisionContainerWhenAnotherAlreadyProvisionedAndHasAffinityRules() throws Throwable { //Create a container A which has affinity to B. Deploy A, then deploy B. B should be placed on the same host as A String secondContainerName = "second_container"; ContainerDescription desc2 = createDescriptions(secondContainerName, 2, null).get(0); ContainerDescription desc1 = createDescriptions(CONTAINER_NAME, 2, new String[] { secondContainerName + HARD.getValue() }).get(0); String hostLink1 = initialHostLinks.get(2); createContainer(desc1, hostLink1); filter = new ServiceAffinityHostFilter(host, desc2); expectedLinks = Arrays.asList(hostLink1); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); Throwable e = filter(expectedLinks); if (e != null) { fail("Container should be placed exactly on host " + hostLink1); } }
PlacementHostSelectionTaskState placementTask = new PlacementHostSelectionTaskState(); placementTask.documentSelfLink = getSelfId(); placementTask.resourceDescriptionLink = state.resourceDescriptionLink;
PlacementHostSelectionTaskState placementTask = new PlacementHostSelectionTaskState(); placementTask.documentSelfLink = getSelfId() + "-reservation" + (isGlobal(state) ? "-global" : "");
@Test public void testSelectHostExistingContainerWithLinkNoAlias() throws Throwable { // create composite component CompositeComponent component = createComponent("App-With-Links-Test"); // create 2 container descriptions with links wordpress -> mysql ContainerDescription desc1 = createDescription("mysql", null); // mysql link has no alias (only service) ContainerDescription desc2 = createDescription("wordpress", new String[] {"mysql"}); // create 2 containers in the component createContainer(component, desc1); createContainer(component, desc2); String contextId = extractId(component.documentSelfLink); state.contextId = contextId; state.addCustomProperty(RequestUtils.FIELD_NAME_CONTEXT_ID_KEY, contextId); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); // place a new container which another container has a link to it (worpress -> mysql) filter = new ClusterServiceLinkAffinityHostFilter(host, desc1); // filter selects the host where existing mysql is placed expectedLinks = Arrays.asList(initialHostLinks.get(0)); Throwable e = filter(expectedLinks); if (e != null) { fail("Unexpected exception: " + e); } }
@Test public void testSelectHostExistingContainerWithoutLink() throws Throwable { // create composite component CompositeComponent component = createComponent("App-With-Links-Test"); // create 2 container descriptions (no links between them) ContainerDescription desc1 = createDescription("mysql", null); ContainerDescription desc2 = createDescription("wordpress", null); // create 2 containers in the component createContainer(component, desc1); createContainer(component, desc2); String contextId = extractId(component.documentSelfLink); state.contextId = contextId; state.addCustomProperty(RequestUtils.FIELD_NAME_CONTEXT_ID_KEY, contextId); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); // place a new container (no links from or to it) filter = new ClusterServiceLinkAffinityHostFilter(host, desc1); // filter selects all the available hosts Throwable e = filter(expectedLinks); if (e != null) { fail("Unexpected exception: " + e); } }
@Test public void testSelectHostExistingContainerWithLink() throws Throwable { // create composite component CompositeComponent component = createComponent("App-With-Links-Test"); // create 2 container descriptions with links wordpress -> mysql ContainerDescription desc1 = createDescription("mysql", null); ContainerDescription desc2 = createDescription("wordpress", new String[] {"mysql:mysql"}); // create 2 containers in the component createContainer(component, desc1); createContainer(component, desc2); String contextId = extractId(component.documentSelfLink); state.contextId = contextId; state.addCustomProperty(RequestUtils.FIELD_NAME_CONTEXT_ID_KEY, contextId); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); // place a new container which another container has a link to it (worpress -> mysql) filter = new ClusterServiceLinkAffinityHostFilter(host, desc1); // filter selects the host where existing mysql is placed expectedLinks = Arrays.asList(initialHostLinks.get(0)); Throwable e = filter(expectedLinks); if (e != null) { fail("Unexpected exception: " + e); } }
@Test public void testProvisionContainerWhenAnotherAlreadyProvisionedAndHasVolumesFromRules() throws Throwable { //Deploy containers B (volumes from) A. Then deploy A again, A should be placed on the same host as the other two ContainerDescription desc = createDescriptionsWithVolumesFrom(CONTAINER_NAME, 5, null) .get(0); ContainerState container = createContainer(desc); assertEquals(UriUtils.buildUriPath( CompositeComponentFactoryService.SELF_LINK, state.contextId), container.compositeComponentLink); String[] volumesFrom = new String[] { CONTAINER_NAME }; ContainerDescription volumesFromDesc = createDescriptionsWithVolumesFrom( "random-name36", 1, volumesFrom).get(0); createContainer(volumesFromDesc, container.parentLink); filter = new VolumesFromAffinityHostFilter(host, desc); Collection<String> expectedFilteredHostLinks = Arrays.asList(container.parentLink); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); Throwable e = filter(expectedFilteredHostLinks); if (e != null) { fail("Unexpected exception: " + e); } }
@Override @Before public void setUp() throws Throwable { super.setUp(); state = new PlacementHostSelectionTaskState(); state.contextId = UUID.randomUUID().toString(); state.customProperties = new HashMap<>(); state.resourceCount = 1; state.customProperties.put(RequestUtils.FIELD_NAME_CONTEXT_ID_KEY, UUID.randomUUID().toString()); state.resourcePoolLinks = Collections.singletonList(createResourcePool().documentSelfLink); initialHostLinks = new ArrayList<>(); initialHostLinks.add(createDockerHost( createDockerHostDescription(), createResourcePool(), true).documentSelfLink); initialHostLinks.add(createDockerHost( createDockerHostDescription(), createResourcePool(), true).documentSelfLink); initialHostLinks.add(createDockerHost( createDockerHostDescription(), createResourcePool(), true).documentSelfLink); filter = new ClusterAntiAffinityHostFilter(host, containerDesc); expectedLinks = new ArrayList<>(initialHostLinks); }
@Override public void filter( PlacementHostSelectionTaskService.PlacementHostSelectionTaskState state, Map<String, HostSelection> hostSelectionMap, HostSelectionFilterCompletion callback) { if (!isActive() && state.getCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP) == null) { callback.complete(hostSelectionMap, null); return; } host.log( Level.INFO, "Filter for containerDesc property [%s], value: [%s] and contextId: [%s] is active for placement host selection task: %s", affinityPropertyName, getAffinity(), state.contextId, state.documentSelfLink); findContainerDescriptions(state, hostSelectionMap, callback, getDescQuery()); }
private PlacementHostSelectionTaskState createHostPlacementTask(String containerDescLink, int resourceCount, boolean expectError) throws Throwable { PlacementHostSelectionTaskState placementTask = new PlacementHostSelectionTaskState(); placementTask.documentSelfLink = UUID.randomUUID().toString(); placementTask.resourceDescriptionLink = containerDescLink; placementTask.resourcePoolLinks = new ArrayList<>(); placementTask.resourcePoolLinks.add(resourcePool.documentSelfLink); placementTask.resourceCount = resourceCount; placementTask.resourceType = ResourceType.CONTAINER_TYPE.getName(); placementTask.contextId = contextId; placementTask.serviceTaskCallback = ServiceTaskCallback.createEmpty(); placementTask.documentExpirationTimeMicros = ServiceUtils .getDefaultTaskExpirationTimeInMicros(); return placeTask(placementTask, expectError); }
@Test public void testProvisionContainerWhenAnotherAlreadyProvisionedAndHasAntiAffinityRules() throws Throwable { //Create a container A which has anti affinity to B. Deploy A, then deploy B. B should NOT be placed on the same host as A ContainerDescription desc1 = createDescriptions(CONTAINER_NAME1, new String[] { ANTI_AFFINITY_PREFIX + CONTAINER_NAME2 }); String hostLink2 = initialHostLinks.get(1); createContainer(desc1, hostLink2); ContainerDescription desc2 = createDescriptions(CONTAINER_NAME2); filter = new ServiceAntiAffinityHostFilter(host, desc2); expectedLinks.remove(hostLink2); state.addCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP, "true"); filter(expectedLinks); }
@Before public void setup() { placementHostSelectionTaskState = new PlacementHostSelectionTaskState(); placementHostSelectionTaskState.contextId = UUID.randomUUID().toString(); host = Mockito.mock(VerificationHost.class); Mockito.when(host.getUri()).thenReturn(URI.create("http://mocked.verification.host:1234")); Answer<Void> realMethodAnswer = invocation -> { invocation.callRealMethod(); return null; }; Mockito.doAnswer(realMethodAnswer).when(host).completeIteration(); Mockito.doAnswer(realMethodAnswer).when(host).failIteration(Mockito.any(Throwable.class)); }
@Override public void filter(PlacementHostSelectionTaskState state, Map<String, HostSelection> hostSelectionMap, HostSelectionFilterCompletion callback) { if (!isActive() && state.getCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP) == null) { callback.complete(hostSelectionMap, null); return; } host.log( Level.INFO, "Filter for containerDesc property [%s], value: [%s] and contextId: [%s] is active for placement host selection task: %s", affinityPropertyName, getAffinity(), state.contextId, state.documentSelfLink); findContainerDescriptions(state, hostSelectionMap, callback, getDescQuery()); }
@Override public void filter(PlacementHostSelectionTaskState state, Map<String, HostSelection> hostSelectionMap, HostSelectionFilterCompletion callback) { // filter is applied for all clustering operations if (!isActive() || state.getCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP) == null) { callback.complete(hostSelectionMap, null); return; } findCompositeDescriptions(state, hostSelectionMap, callback); }
@Override public void filter(final PlacementHostSelectionTaskState state, final Map<String, HostSelection> hostSelectionMap, final HostSelectionFilterCompletion callback) { //In case this is a clustering operation we want to continue even if desc._cluster <= 1 if (!isActive() && state.resourceCount <= 1 && state.getCustomProperty(RequestUtils.CLUSTERING_OPERATION_CUSTOM_PROP) == null) { callback.complete(hostSelectionMap, null); return; } findContainers(state, hostSelectionMap, callback); }