Skip to content

Commit 93e3745

Browse files
committed
Merge branch '4.11'
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
2 parents 0afba54 + 7e6fddb commit 93e3745

15 files changed

Lines changed: 581 additions & 18 deletions

File tree

api/src/main/java/com/cloud/deploy/DataCenterDeployment.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
2020
import com.cloud.vm.ReservationContext;
2121

22+
import java.util.ArrayList;
23+
import java.util.List;
24+
2225
public class DataCenterDeployment implements DeploymentPlan {
2326
long _dcId;
2427
Long _podId;
@@ -29,6 +32,7 @@ public class DataCenterDeployment implements DeploymentPlan {
2932
ExcludeList _avoids = null;
3033
boolean _recreateDisks;
3134
ReservationContext _context;
35+
List<Long> preferredHostIds = new ArrayList<>();
3236

3337
public DataCenterDeployment(long dataCenterId) {
3438
this(dataCenterId, null, null, null, null, null);
@@ -93,4 +97,14 @@ public ReservationContext getReservationContext() {
9397
return _context;
9498
}
9599

100+
@Override
101+
public void setPreferredHosts(List<Long> hostIds) {
102+
this.preferredHostIds = new ArrayList<>(hostIds);
103+
}
104+
105+
@Override
106+
public List<Long> getPreferredHosts() {
107+
return this.preferredHostIds;
108+
}
109+
96110
}

api/src/main/java/com/cloud/deploy/DeploymentPlan.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
2020
import com.cloud.vm.ReservationContext;
2121

22+
import java.util.List;
23+
2224
/**
2325
*/
2426
public interface DeploymentPlan {
@@ -65,4 +67,8 @@ public interface DeploymentPlan {
6567
Long getPhysicalNetworkId();
6668

6769
ReservationContext getReservationContext();
70+
71+
void setPreferredHosts(List<Long> hostIds);
72+
73+
List<Long> getPreferredHosts();
6874
}

client/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@
438438
<artifactId>cloud-plugin-host-anti-affinity</artifactId>
439439
<version>${project.version}</version>
440440
</dependency>
441+
<dependency>
442+
<groupId>org.apache.cloudstack</groupId>
443+
<artifactId>cloud-plugin-host-affinity</artifactId>
444+
<version>${project.version}</version>
445+
</dependency>
441446
<dependency>
442447
<groupId>org.apache.cloudstack</groupId>
443448
<artifactId>cloud-plugin-api-solidfire-intg-test</artifactId>

core/src/main/resources/META-INF/cloudstack/core/spring-core-registry-core-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
class="org.apache.cloudstack.spring.lifecycle.registry.ExtensionRegistry">
249249
<property name="orderConfigKey" value="affinity.processors.order" />
250250
<property name="orderConfigDefault"
251-
value="HostAntiAffinityProcessor,ExplicitDedicationProcessor" />
251+
value="HostAntiAffinityProcessor,ExplicitDedicationProcessor,HostAffinityProcessor" />
252252
<property name="excludeKey" value="affinity.processors.exclude" />
253253
</bean>
254254

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<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">
20+
<modelVersion>4.0.0</modelVersion>
21+
<name>Apache CloudStack Plugin - Host Affinity Processor</name>
22+
<artifactId>cloud-plugin-host-affinity</artifactId>
23+
<parent>
24+
<artifactId>cloudstack-plugins</artifactId>
25+
<groupId>org.apache.cloudstack</groupId>
26+
<version>4.12.0.0-SNAPSHOT</version>
27+
<relativePath>../../pom.xml</relativePath>
28+
</parent>
29+
</project>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
name=host-affinity
18+
parent=planner
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<beans xmlns="http://www.springframework.org/schema/beans"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xmlns:context="http://www.springframework.org/schema/context"
22+
xmlns:aop="http://www.springframework.org/schema/aop"
23+
xsi:schemaLocation="http://www.springframework.org/schema/beans
24+
http://www.springframework.org/schema/beans/spring-beans.xsd
25+
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
26+
http://www.springframework.org/schema/context
27+
http://www.springframework.org/schema/context/spring-context.xsd"
28+
>
29+
30+
<bean id="HostAffinityProcessor"
31+
class="org.apache.cloudstack.affinity.HostAffinityProcessor">
32+
<property name="name" value="HostAffinityProcessor" />
33+
<property name="type" value="host affinity" />
34+
</bean>
35+
</beans>

0 commit comments

Comments
 (0)