-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add a soft host-tag VM placement preference (affinity processor) #14083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nagaboinaramgopal
wants to merge
4
commits into
apache:main
Choose a base branch
from
nagaboinaramgopal:pr/host-tag-affinity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+330
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f7317b2
affinity: add soft host-tag placement preference
nagaboinaramgopal f367d2e
affinity: make the host-tag affinity group admin-controlled
nagaboinaramgopal f1f1fa9
plugins: keep the host-tag-affinity module in alphabetical order
nagaboinaramgopal a47f48d
affinity: scope the host-tag preference to the selected cluster in th…
nagaboinaramgopal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
plugins/affinity-group-processors/host-tag-affinity/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>cloud-plugin-host-tag-affinity</artifactId> | ||
| <name>Apache CloudStack Plugin - Host Tag Affinity Processor</name> | ||
| <parent> | ||
| <groupId>org.apache.cloudstack</groupId> | ||
| <artifactId>cloudstack-plugins</artifactId> | ||
| <version>24.0.0-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
| </project> |
98 changes: 98 additions & 0 deletions
98
...t-tag-affinity/src/main/java/org/apache/cloudstack/affinity/HostTagAffinityProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package org.apache.cloudstack.affinity; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import org.apache.cloudstack.affinity.dao.AffinityGroupDao; | ||
| import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; | ||
| import org.apache.commons.collections.CollectionUtils; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import com.cloud.deploy.DeploymentPlan; | ||
| import com.cloud.deploy.DeploymentPlanner.ExcludeList; | ||
| import com.cloud.exception.AffinityConflictException; | ||
| import com.cloud.host.Host; | ||
| import com.cloud.host.HostVO; | ||
| import com.cloud.host.dao.HostDao; | ||
| import com.cloud.vm.VirtualMachine; | ||
| import com.cloud.vm.VirtualMachineProfile; | ||
|
|
||
| /** | ||
| * Soft VM-to-host placement preference: the affinity group name is treated as a host tag, and within | ||
| * the cluster the planner selects, hosts carrying that tag have their deployment priority raised. A | ||
| * preference, not a constraint, so no host is excluded, and it does not change which cluster is chosen. | ||
| * Note: the priority channel is not honored by automatic DRS. | ||
| */ | ||
| public class HostTagAffinityProcessor extends AffinityProcessorBase implements AffinityGroupProcessor { | ||
|
|
||
| @Inject | ||
| protected AffinityGroupDao affinityGroupDao; | ||
| @Inject | ||
| protected AffinityGroupVMMapDao affinityGroupVMMapDao; | ||
| @Inject | ||
| protected HostDao hostDao; | ||
|
|
||
| @Override | ||
| public void process(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid, List<VirtualMachine> vmList) throws AffinityConflictException { | ||
| VirtualMachine vm = vmProfile.getVirtualMachine(); | ||
| List<AffinityGroupVMMapVO> vmGroupMappings = affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType()); | ||
|
|
||
| for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) { | ||
| if (vmGroupMapping != null) { | ||
| processAffinityGroup(vmGroupMapping, plan, vm); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isAdminControlledGroup() { | ||
| return true; | ||
| } | ||
|
|
||
| protected void processAffinityGroup(AffinityGroupVMMapVO vmGroupMapping, DeploymentPlan plan, VirtualMachine vm) { | ||
| AffinityGroupVO group = affinityGroupDao.findById(vmGroupMapping.getAffinityGroupId()); | ||
| if (group == null || StringUtils.isBlank(group.getName())) { | ||
| return; | ||
| } | ||
|
|
||
| String hostTag = group.getName(); | ||
| List<HostVO> preferredHosts = hostDao.listByHostTag(Host.Type.Routing, null, null, vm.getDataCenterId(), hostTag); | ||
| if (CollectionUtils.isEmpty(preferredHosts)) { | ||
| if (logger.isDebugEnabled()) { | ||
| logger.debug(String.format("No hosts carry tag [%s] in zone %s for VM %s; host-tag affinity is a no-op.", | ||
| hostTag, vm.getDataCenterId(), vm)); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| for (HostVO host : preferredHosts) { | ||
| Integer priority = adjustHostPriority(plan, host.getId()); | ||
|
|
||
| if (logger.isDebugEnabled()) { | ||
| logger.debug(String.format("Raised host %s priority to %s (VM %s prefers hosts tagged [%s]).", | ||
| host.getId(), priority, vm, hostTag)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected Integer adjustHostPriority(DeploymentPlan plan, Long hostId) { | ||
| plan.adjustHostPriority(hostId, DeploymentPlan.HostPriorityAdjustment.HIGHER); | ||
| return plan.getHostPriorities().get(hostId); | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
...t-tag-affinity/src/main/resources/META-INF/cloudstack/host-tag-affinity/module.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| name=host-tag-affinity | ||
| parent=planner |
37 changes: 37 additions & 0 deletions
37
...main/resources/META-INF/cloudstack/host-tag-affinity/spring-host-tag-affinity-context.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
| <beans xmlns="http://www.springframework.org/schema/beans" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns:context="http://www.springframework.org/schema/context" | ||
| xmlns:aop="http://www.springframework.org/schema/aop" | ||
| xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
| http://www.springframework.org/schema/beans/spring-beans.xsd | ||
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd | ||
| http://www.springframework.org/schema/context | ||
| http://www.springframework.org/schema/context/spring-context.xsd" | ||
| > | ||
|
|
||
| <bean id="HostTagAffinityProcessor" | ||
| class="org.apache.cloudstack.affinity.HostTagAffinityProcessor"> | ||
| <property name="name" value="HostTagAffinityProcessor" /> | ||
| <property name="type" value="host tag affinity" /> | ||
| </bean> | ||
|
|
||
|
|
||
| </beans> |
141 changes: 141 additions & 0 deletions
141
...g-affinity/src/test/java/org/apache/cloudstack/affinity/HostTagAffinityProcessorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.cloudstack.affinity; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.ArgumentMatchers.isNull; | ||
| import static org.mockito.ArgumentMatchers.nullable; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.cloudstack.affinity.dao.AffinityGroupDao; | ||
| import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.Spy; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| import com.cloud.deploy.DataCenterDeployment; | ||
| import com.cloud.deploy.DeployDestination; | ||
| import com.cloud.deploy.DeploymentPlanner.ExcludeList; | ||
| import com.cloud.host.Host; | ||
| import com.cloud.host.HostVO; | ||
| import com.cloud.host.dao.HostDao; | ||
| import com.cloud.vm.VirtualMachine; | ||
| import com.cloud.vm.VirtualMachineProfile; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class HostTagAffinityProcessorTest { | ||
|
|
||
| @Spy | ||
| @InjectMocks | ||
| HostTagAffinityProcessor processor = new HostTagAffinityProcessor(); | ||
|
|
||
| @Mock | ||
| AffinityGroupVMMapDao affinityGroupVMMapDao; | ||
| @Mock | ||
| AffinityGroupDao affinityGroupDao; | ||
| @Mock | ||
| HostDao hostDao; | ||
|
|
||
| long vmId = 10L; | ||
| long affinityGroupId = 20L; | ||
| long zoneId = 2L; | ||
| long host2Id = 3L; | ||
| long host3Id = 4L; | ||
| String groupName = "gold"; | ||
|
|
||
| private VirtualMachineProfile mockVmProfile() { | ||
| VirtualMachine vm = Mockito.mock(VirtualMachine.class); | ||
| when(vm.getId()).thenReturn(vmId); | ||
| when(vm.getDataCenterId()).thenReturn(zoneId); | ||
| VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class); | ||
| when(vmProfile.getVirtualMachine()).thenReturn(vm); | ||
| return vmProfile; | ||
| } | ||
|
|
||
| private void stubGroupMembership() { | ||
| List<AffinityGroupVMMapVO> vmGroupMappings = new ArrayList<>(); | ||
| vmGroupMappings.add(new AffinityGroupVMMapVO(affinityGroupId, vmId)); | ||
| when(affinityGroupVMMapDao.findByVmIdType(eq(vmId), nullable(String.class))).thenReturn(vmGroupMappings); | ||
| AffinityGroupVO group = Mockito.mock(AffinityGroupVO.class); | ||
| when(affinityGroupDao.findById(affinityGroupId)).thenReturn(group); | ||
| when(group.getName()).thenReturn(groupName); | ||
| } | ||
|
|
||
| @Test | ||
| public void testProcessRaisesPriorityForTaggedHosts() { | ||
| VirtualMachineProfile vmProfile = mockVmProfile(); | ||
| stubGroupMembership(); | ||
|
|
||
| HostVO host2 = Mockito.mock(HostVO.class); | ||
| when(host2.getId()).thenReturn(host2Id); | ||
| HostVO host3 = Mockito.mock(HostVO.class); | ||
| when(host3.getId()).thenReturn(host3Id); | ||
| when(hostDao.listByHostTag(eq(Host.Type.Routing), isNull(), isNull(), eq(zoneId), eq(groupName))) | ||
| .thenReturn(Arrays.asList(host2, host3)); | ||
|
|
||
| DataCenterDeployment plan = new DataCenterDeployment(zoneId); | ||
| ExcludeList avoid = new ExcludeList(); | ||
|
|
||
| processor.process(vmProfile, plan, avoid); | ||
|
|
||
| // Both tagged hosts get raised to priority 1 (DEFAULT 0 -> HIGHER +1); nothing is excluded. | ||
| Assert.assertEquals(2, plan.getHostPriorities().size()); | ||
| Assert.assertEquals(Integer.valueOf(1), plan.getHostPriorities().get(host2Id)); | ||
| Assert.assertEquals(Integer.valueOf(1), plan.getHostPriorities().get(host3Id)); | ||
| Assert.assertFalse("soft preference: no host may be excluded", avoid.shouldAvoid(host2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testProcessNoMatchingHostsIsNoOp() { | ||
| VirtualMachineProfile vmProfile = mockVmProfile(); | ||
| stubGroupMembership(); | ||
| when(hostDao.listByHostTag(eq(Host.Type.Routing), isNull(), isNull(), eq(zoneId), eq(groupName))) | ||
| .thenReturn(Collections.emptyList()); | ||
|
|
||
| DataCenterDeployment plan = new DataCenterDeployment(zoneId); | ||
| ExcludeList avoid = new ExcludeList(); | ||
|
|
||
| processor.process(vmProfile, plan, avoid); | ||
|
|
||
| Assert.assertTrue(plan.getHostPriorities().isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCheckAlwaysTrue() throws Exception { | ||
| // A soft preference must never fail a planned destination. | ||
| Assert.assertTrue(processor.check(Mockito.mock(VirtualMachineProfile.class), Mockito.mock(DeployDestination.class))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGroupIsAdminControlled() { | ||
| // Host tags are operator-set, so this group type is admin-controlled. | ||
| Assert.assertTrue(processor.isAdminControlledGroup()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if the tagged host is in a different cluster than the first one the planner tries? looks like the higher priority only reorders hosts inside one cluster, so the vm could still land somewhere else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you're right. The priority bump only reorders hosts inside whichever cluster the planner ends up choosing, so if the tagged host sits in a cluster the planner reaches later, an untagged host in an earlier cluster can still win. I can keep this scoped as a within-cluster preference and fix the wording so it doesn't claim more than it does, or make it bias cluster ordering too so it holds across the whole zone (that one's a bigger change to how clusters get picked). Do you have a preference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keeping it inside one cluster and fixing the wording sounds good to me, thats how the existing non-strict host affinity works too. changing how clusters get picked feels like its own pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, thanks. I've scoped it as a within-cluster preference and fixed the wording in both the code and the description so it doesn't imply anything zone-wide. Cluster ordering can be its own PR later if we decide we want it.