|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.affinity; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.ArrayList; |
| 23 | + |
| 24 | +import javax.inject.Inject; |
| 25 | + |
| 26 | +import com.cloud.vm.VMInstanceVO; |
| 27 | +import org.apache.commons.collections.CollectionUtils; |
| 28 | +import org.apache.log4j.Logger; |
| 29 | + |
| 30 | +import org.apache.cloudstack.affinity.dao.AffinityGroupDao; |
| 31 | +import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; |
| 32 | + |
| 33 | +import com.cloud.deploy.DeployDestination; |
| 34 | +import com.cloud.deploy.DeploymentPlan; |
| 35 | +import com.cloud.deploy.DeploymentPlanner.ExcludeList; |
| 36 | +import com.cloud.exception.AffinityConflictException; |
| 37 | +import com.cloud.vm.VirtualMachine; |
| 38 | +import com.cloud.vm.VirtualMachineProfile; |
| 39 | +import com.cloud.vm.dao.VMInstanceDao; |
| 40 | + |
| 41 | +public class HostAffinityProcessor extends AffinityProcessorBase implements AffinityGroupProcessor { |
| 42 | + |
| 43 | + private static final Logger s_logger = Logger.getLogger(HostAffinityProcessor.class); |
| 44 | + |
| 45 | + @Inject |
| 46 | + protected VMInstanceDao _vmInstanceDao; |
| 47 | + @Inject |
| 48 | + protected AffinityGroupDao _affinityGroupDao; |
| 49 | + @Inject |
| 50 | + protected AffinityGroupVMMapDao _affinityGroupVMMapDao; |
| 51 | + |
| 52 | + @Override |
| 53 | + public void process(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid) throws AffinityConflictException { |
| 54 | + VirtualMachine vm = vmProfile.getVirtualMachine(); |
| 55 | + List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType()); |
| 56 | + if (CollectionUtils.isNotEmpty(vmGroupMappings)) { |
| 57 | + for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) { |
| 58 | + processAffinityGroup(vmGroupMapping, plan, vm); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Process Affinity Group for VM deployment |
| 65 | + */ |
| 66 | + protected void processAffinityGroup(AffinityGroupVMMapVO vmGroupMapping, DeploymentPlan plan, VirtualMachine vm) { |
| 67 | + AffinityGroupVO group = _affinityGroupDao.findById(vmGroupMapping.getAffinityGroupId()); |
| 68 | + s_logger.debug("Processing affinity group " + group.getName() + " for VM Id: " + vm.getId()); |
| 69 | + |
| 70 | + List<Long> groupVMIds = _affinityGroupVMMapDao.listVmIdsByAffinityGroup(group.getId()); |
| 71 | + groupVMIds.remove(vm.getId()); |
| 72 | + |
| 73 | + List<Long> preferredHosts = getPreferredHostsFromGroupVMIds(groupVMIds); |
| 74 | + plan.setPreferredHosts(preferredHosts); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get host ids set from vm ids list |
| 79 | + */ |
| 80 | + protected Set<Long> getHostIdSet(List<Long> vmIds) { |
| 81 | + Set<Long> hostIds = new HashSet<>(); |
| 82 | + for (Long groupVMId : vmIds) { |
| 83 | + VMInstanceVO groupVM = _vmInstanceDao.findById(groupVMId); |
| 84 | + hostIds.add(groupVM.getHostId()); |
| 85 | + } |
| 86 | + return hostIds; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get preferred host ids list from the affinity group VMs |
| 91 | + */ |
| 92 | + protected List<Long> getPreferredHostsFromGroupVMIds(List<Long> vmIds) { |
| 93 | + return new ArrayList<>(getHostIdSet(vmIds)); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean check(VirtualMachineProfile vmProfile, DeployDestination plannedDestination) throws AffinityConflictException { |
| 98 | + if (plannedDestination.getHost() == null) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + long plannedHostId = plannedDestination.getHost().getId(); |
| 102 | + VirtualMachine vm = vmProfile.getVirtualMachine(); |
| 103 | + List<AffinityGroupVMMapVO> vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), getType()); |
| 104 | + |
| 105 | + if (CollectionUtils.isNotEmpty(vmGroupMappings)) { |
| 106 | + for (AffinityGroupVMMapVO vmGroupMapping : vmGroupMappings) { |
| 107 | + if (!checkAffinityGroup(vmGroupMapping, vm, plannedHostId)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Check Affinity Group |
| 118 | + */ |
| 119 | + protected boolean checkAffinityGroup(AffinityGroupVMMapVO vmGroupMapping, VirtualMachine vm, long plannedHostId) { |
| 120 | + List<Long> groupVMIds = _affinityGroupVMMapDao.listVmIdsByAffinityGroup(vmGroupMapping.getAffinityGroupId()); |
| 121 | + groupVMIds.remove(vm.getId()); |
| 122 | + |
| 123 | + Set<Long> hostIds = getHostIdSet(groupVMIds); |
| 124 | + return CollectionUtils.isEmpty(hostIds) || hostIds.contains(plannedHostId); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments