Copilot commented on code in PR #14083:
URL: https://github.com/apache/cloudstack/pull/14083#discussion_r4157591107


##########
plugins/affinity-group-processors/host-tag-affinity/src/main/java/org/apache/cloudstack/affinity/HostTagAffinityProcessor.java:
##########
@@ -0,0 +1,97 @@
+// 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 hosts
+ * carrying that tag in the VM's zone have their deployment priority raised. A 
preference, not a
+ * constraint, so no host is excluded. 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());

Review Comment:
   The priority hint does not provide the advertised zone-wide preference when 
a zone has multiple clusters. `FirstFitPlanner` orders clusters by capacity, 
and `DeploymentPlanningManagerImpl.checkClustersforDestination` returns from 
the first viable cluster; host priorities are only applied later to hosts 
within that cluster (`DeploymentPlanningManagerImpl.java:1340-1391,1728-1741`). 
Thus an untagged host in an earlier cluster wins even when a tagged host is 
available in a later cluster. Please make tagged-host priority influence 
cluster ordering as well and add a multi-cluster test.



##########
plugins/pom.xml:
##########
@@ -52,6 +52,7 @@
         <module>affinity-group-processors/host-anti-affinity</module>
         <module>affinity-group-processors/non-strict-host-affinity</module>
         
<module>affinity-group-processors/non-strict-host-anti-affinity</module>
+        <module>affinity-group-processors/host-tag-affinity</module>

Review Comment:
   This module is out of order despite the enclosing `<!-- keep in alphabetic 
order -->` requirement. Place `host-tag-affinity` with the other `host-*` 
modules before the `non-strict-*` entries.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to