DaanHoogland commented on a change in pull request #2630: Host Affinity plugin
URL: https://github.com/apache/cloudstack/pull/2630#discussion_r186646609
 
 

 ##########
 File path: 
plugins/affinity-group-processors/host-affinity/src/org/apache/cloudstack/affinity/HostAffinityProcessor.java
 ##########
 @@ -0,0 +1,119 @@
+// 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 java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.ArrayList;
+
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import com.cloud.vm.VMInstanceVO;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.log4j.Logger;
+
+import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
+import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
+import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
+
+import com.cloud.deploy.DeployDestination;
+import com.cloud.deploy.DeploymentPlan;
+import com.cloud.deploy.DeploymentPlanner.ExcludeList;
+import com.cloud.exception.AffinityConflictException;
+import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.dao.VMInstanceDao;
+
+public class HostAffinityProcessor extends AffinityProcessorBase implements 
AffinityGroupProcessor {
+
+    private static final Logger s_logger = 
Logger.getLogger(HostAffinityProcessor.class);
+
+    @Inject
+    protected VMInstanceDao _vmInstanceDao;
+    @Inject
+    protected AffinityGroupDao _affinityGroupDao;
+    @Inject
+    protected AffinityGroupVMMapDao _affinityGroupVMMapDao;
+
+    @Inject
+    protected ConfigurationDao _configDao;
+
+    @Override
+    public void process(VirtualMachineProfile vmProfile, DeploymentPlan plan, 
ExcludeList avoid) throws AffinityConflictException {
+        VirtualMachine vm = vmProfile.getVirtualMachine();
+        List<AffinityGroupVMMapVO> vmGroupMappings = 
_affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType());
+        for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
+            if (vmGroupMapping != null) {
+                AffinityGroupVO group = 
_affinityGroupDao.findById(vmGroupMapping.getAffinityGroupId());
+
+                if (s_logger.isDebugEnabled()) {
+                    s_logger.debug("Processing affinity group " + 
group.getName() + " for VM Id: " + vm.getId());
+                }
+
+                List<Long> groupVMIds = 
_affinityGroupVMMapDao.listVmIdsByAffinityGroup(group.getId());
+                groupVMIds.remove(vm.getId());
+
+                Set<Long> hostIds = new HashSet<>();
+                for (Long groupVMId : groupVMIds) {
+                    VMInstanceVO groupVM = _vmInstanceDao.findById(groupVMId);
+                    hostIds.add(groupVM.getHostId());
+                }
+                List<Long> preferredHostIds = new ArrayList<>();
+                preferredHostIds.addAll(hostIds);
+                plan.setPreferredHosts(preferredHostIds);
+            }
+        }
+    }
+
+    @Override
+    public boolean configure(final String name, final Map<String, Object> 
params) throws ConfigurationException {
+        super.configure(name, params);
+        return true;
+    }
+
+    @Override
+    public boolean check(VirtualMachineProfile vmProfile, DeployDestination 
plannedDestination) throws AffinityConflictException {
+        if (plannedDestination.getHost() == null) {
+            return true;
+        }
+        long plannedHostId = plannedDestination.getHost().getId();
+
+        VirtualMachine vm = vmProfile.getVirtualMachine();
+
+        List<AffinityGroupVMMapVO> vmGroupMappings = 
_affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType());
+
+        for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) {
 
 Review comment:
   same here

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to