forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserConcentratedPodPlanner.java
More file actions
141 lines (118 loc) · 5.73 KB
/
UserConcentratedPodPlanner.java
File metadata and controls
141 lines (118 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 com.cloud.deploy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.utils.Pair;
import com.cloud.vm.VirtualMachineProfile;
public class UserConcentratedPodPlanner extends FirstFitPlanner implements DeploymentClusterPlanner {
private static final Logger s_logger = Logger.getLogger(UserConcentratedPodPlanner.class);
/**
* This method should reorder the given list of Cluster Ids by applying any necessary heuristic
* for this planner
* For UserConcentratedPodPlanner we need to order the clusters in a zone across pods, by considering those pods first which have more number of VMs for this account
* This reordering is not done incase the clusters within single pod are passed when the allocation is applied at pod-level.
* @return List<Long> ordered list of Cluster Ids
*/
@Override
protected List<Long> reorderClusters(long id, boolean isZone, Pair<List<Long>, Map<Long, Double>> clusterCapacityInfo, VirtualMachineProfile vmProfile,
DeploymentPlan plan) {
List<Long> clusterIdsByCapacity = clusterCapacityInfo.first();
if (vmProfile.getOwner() == null || !isZone) {
return clusterIdsByCapacity;
}
return applyUserConcentrationPodHeuristicToClusters(id, clusterIdsByCapacity, vmProfile.getOwner().getAccountId());
}
private List<Long> applyUserConcentrationPodHeuristicToClusters(long zoneId, List<Long> prioritizedClusterIds, long accountId) {
//user has VMs in certain pods. - prioritize those pods first
//UserConcentratedPod strategy
List<Long> clusterList = new ArrayList<Long>();
List<Long> podIds = listPodsByUserConcentration(zoneId, accountId);
if (!podIds.isEmpty()) {
clusterList = reorderClustersByPods(prioritizedClusterIds, podIds);
} else {
clusterList = prioritizedClusterIds;
}
return clusterList;
}
private List<Long> reorderClustersByPods(List<Long> clusterIds, List<Long> podIds) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Reordering cluster list as per pods ordered by user concentration");
}
Map<Long, List<Long>> podClusterMap = clusterDao.getPodClusterIdMap(clusterIds);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Pod To cluster Map is: " + podClusterMap);
}
List<Long> reorderedClusters = new ArrayList<Long>();
for (Long pod : podIds) {
if (podClusterMap.containsKey(pod)) {
List<Long> clustersOfThisPod = podClusterMap.get(pod);
if (clustersOfThisPod != null) {
for (Long clusterId : clusterIds) {
if (clustersOfThisPod.contains(clusterId)) {
reorderedClusters.add(clusterId);
}
}
clusterIds.removeAll(clustersOfThisPod);
}
}
}
reorderedClusters.addAll(clusterIds);
if (s_logger.isTraceEnabled()) {
s_logger.trace("Reordered cluster list: " + reorderedClusters);
}
return reorderedClusters;
}
protected List<Long> listPodsByUserConcentration(long zoneId, long accountId) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Applying UserConcentratedPod heuristic for account: " + accountId);
}
List<Long> prioritizedPods = vmDao.listPodIdsHavingVmsforAccount(zoneId, accountId);
if (s_logger.isTraceEnabled()) {
s_logger.trace("List of pods to be considered, after applying UserConcentratedPod heuristic: " + prioritizedPods);
}
return prioritizedPods;
}
/**
* This method should reorder the given list of Pod Ids by applying any necessary heuristic
* for this planner
* For UserConcentratedPodPlanner we need to order the pods by considering those pods first which have more number of VMs for this account
* @return List<Long> ordered list of Pod Ids
*/
@Override
protected List<Long> reorderPods(Pair<List<Long>, Map<Long, Double>> podCapacityInfo, VirtualMachineProfile vmProfile, DeploymentPlan plan) {
List<Long> podIdsByCapacity = podCapacityInfo.first();
if (vmProfile.getOwner() == null) {
return podIdsByCapacity;
}
long accountId = vmProfile.getOwner().getAccountId();
//user has VMs in certain pods. - prioritize those pods first
//UserConcentratedPod strategy
List<Long> podIds = listPodsByUserConcentration(plan.getDataCenterId(), accountId);
if (!podIds.isEmpty()) {
//remove pods that don't have capacity for this vm
podIds.retainAll(podIdsByCapacity);
podIdsByCapacity.removeAll(podIds);
podIds.addAll(podIdsByCapacity);
return podIds;
} else {
return podIdsByCapacity;
}
}
}