-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathDeploymentPlanner.java
More file actions
323 lines (273 loc) · 10.3 KB
/
DeploymentPlanner.java
File metadata and controls
323 lines (273 loc) · 10.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// 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.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import com.cloud.dc.DataCenter;
import com.cloud.dc.Pod;
import com.cloud.exception.CloudException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InsufficientServerCapacityException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.host.Host;
import com.cloud.org.Cluster;
import com.cloud.storage.StoragePool;
import com.cloud.utils.component.Adapter;
import com.cloud.vm.VirtualMachineProfile;
/**
*/
public interface DeploymentPlanner extends Adapter {
/**
* plan is called to determine where a virtual machine should be running.
*
* @param vm
* virtual machine.
* @param plan
* deployment plan that tells you where it's being deployed to.
* @param avoid
* avoid these data centers, pods, clusters, or hosts.
* @return DeployDestination for that virtual machine.
*/
@Deprecated
DeployDestination plan(VirtualMachineProfile vm, DeploymentPlan plan, ExcludeList avoid) throws InsufficientServerCapacityException;
/**
* canHandle is called before plan to determine if the plan can do the allocation. Planers should be exclusive so
* planner writer must
* make sure only one planer->canHandle return true in the planner list
*
* @param vm
* virtual machine.
* @param plan
* deployment plan that tells you where it's being deployed to.
* @param avoid
* avoid these data centers, pods, clusters, or hosts.
* @return true if it's okay to allocate; false or not
*/
boolean canHandle(VirtualMachineProfile vm, DeploymentPlan plan, ExcludeList avoid);
public enum AllocationAlgorithm {
random, firstfit, userdispersing, userconcentratedpod_random, userconcentratedpod_firstfit, firstfitleastconsumed
}
public enum PlannerResourceUsage {
Shared, Dedicated;
}
public static class ExcludeList implements Serializable {
private static final long serialVersionUID = -482175549460148301L;
protected static Logger LOGGER = LogManager.getLogger(ExcludeList.class);
private Set<Long> _dcIds;
private Set<Long> _podIds;
private Set<Long> _clusterIds;
private Set<Long> _hostIds;
private Set<Long> _poolIds;
public ExcludeList() {
}
public ExcludeList(Set<Long> dcIds, Set<Long> podIds, Set<Long> clusterIds, Set<Long> hostIds, Set<Long> poolIds) {
if (dcIds != null) {
this._dcIds = new HashSet<Long>(dcIds);
}
if (podIds != null) {
this._podIds = new HashSet<Long>(podIds);
}
if (clusterIds != null) {
this._clusterIds = new HashSet<Long>(clusterIds);
}
if (hostIds != null) {
this._hostIds = new HashSet<Long>(hostIds);
}
if (poolIds != null) {
this._poolIds = new HashSet<Long>(poolIds);
}
}
private void logAvoid(Class<?> scope, CloudException e) {
Long id = null;
if (e instanceof InsufficientCapacityException) {
id = ((InsufficientCapacityException) e).getId();
} else if (e instanceof ResourceUnavailableException) {
id = ((ResourceUnavailableException) e).getResourceId();
} else {
LOGGER.debug("Failed to log avoided component due to unexpected exception type [{}].", e.getMessage());
return;
}
LOGGER.debug("Adding {} [{}] to the avoid set due to [{}].", scope.getSimpleName(), id, e.getMessage());
}
public boolean add(InsufficientCapacityException e) {
Class<?> scope = e.getScope();
if (scope == null) {
return false;
}
logAvoid(scope, e);
if (Host.class.isAssignableFrom(scope)) {
addHost(e.getId());
} else if (Pod.class.isAssignableFrom(scope)) {
addPod(e.getId());
} else if (DataCenter.class.isAssignableFrom(scope)) {
addDataCenter(e.getId());
} else if (Cluster.class.isAssignableFrom(scope)) {
addCluster(e.getId());
} else if (StoragePool.class.isAssignableFrom(scope)) {
addPool(e.getId());
} else {
return false;
}
return true;
}
public boolean add(ResourceUnavailableException e) {
Class<?> scope = e.getScope();
if (scope == null) {
return false;
}
logAvoid(scope, e);
if (Host.class.isAssignableFrom(scope)) {
addHost(e.getResourceId());
} else if (Pod.class.isAssignableFrom(scope)) {
addPod(e.getResourceId());
} else if (DataCenter.class.isAssignableFrom(scope)) {
addDataCenter(e.getResourceId());
} else if (Cluster.class.isAssignableFrom(scope)) {
addCluster(e.getResourceId());
} else if (StoragePool.class.isAssignableFrom(scope)) {
addPool(e.getResourceId());
} else {
return false;
}
return true;
}
public void addPool(long poolId) {
if (_poolIds == null) {
_poolIds = new HashSet<Long>();
}
_poolIds.add(poolId);
}
public void addDataCenter(long dataCenterId) {
if (_dcIds == null) {
_dcIds = new HashSet<Long>();
}
_dcIds.add(dataCenterId);
}
public void addPod(long podId) {
if (_podIds == null) {
_podIds = new HashSet<Long>();
}
_podIds.add(podId);
}
public void addPodList(Collection<Long> podList) {
if (_podIds == null) {
_podIds = new HashSet<Long>();
}
_podIds.addAll(podList);
}
public void addCluster(long clusterId) {
if (_clusterIds == null) {
_clusterIds = new HashSet<Long>();
}
_clusterIds.add(clusterId);
}
public void addClusterList(Collection<Long> clusterList) {
if (_clusterIds == null) {
_clusterIds = new HashSet<Long>();
}
_clusterIds.addAll(clusterList);
}
public void addHost(long hostId) {
if (_hostIds == null) {
_hostIds = new HashSet<Long>();
}
_hostIds.add(hostId);
}
public void addHostList(Collection<Long> hostList) {
if (_hostIds == null) {
_hostIds = new HashSet<Long>();
}
_hostIds.addAll(hostList);
}
public boolean shouldAvoid(Host host) {
if (_dcIds != null && _dcIds.contains(host.getDataCenterId())) {
return true;
}
if (_podIds != null && _podIds.contains(host.getPodId())) {
return true;
}
if (_clusterIds != null && _clusterIds.contains(host.getClusterId())) {
return true;
}
if (_hostIds != null && _hostIds.contains(host.getId())) {
return true;
}
return false;
}
public boolean shouldAvoid(Cluster cluster) {
if (_dcIds != null && _dcIds.contains(cluster.getDataCenterId())) {
return true;
}
if (_podIds != null && _podIds.contains(cluster.getPodId())) {
return true;
}
if (_clusterIds != null && _clusterIds.contains(cluster.getId())) {
return true;
}
return false;
}
public boolean shouldAvoid(Pod pod) {
if (_dcIds != null && _dcIds.contains(pod.getDataCenterId())) {
return true;
}
if (_podIds != null && _podIds.contains(pod.getId())) {
return true;
}
return false;
}
public boolean shouldAvoid(StoragePool pool) {
if (_dcIds != null && _dcIds.contains(pool.getDataCenterId())) {
return true;
}
if (_podIds != null && _podIds.contains(pool.getPodId())) {
return true;
}
if (_clusterIds != null && _clusterIds.contains(pool.getClusterId())) {
return true;
}
if (_poolIds != null && _poolIds.contains(pool.getId())) {
return true;
}
return false;
}
public boolean shouldAvoid(DataCenter dc) {
if (_dcIds != null && _dcIds.contains(dc.getId())) {
return true;
}
return false;
}
public Set<Long> getDataCentersToAvoid() {
return _dcIds;
}
public Set<Long> getPodsToAvoid() {
return _podIds;
}
public Set<Long> getClustersToAvoid() {
return _clusterIds;
}
public Set<Long> getHostsToAvoid() {
return _hostIds;
}
public Set<Long> getPoolsToAvoid() {
return _poolIds;
}
}
}