/
IsolationScheduler.java
419 lines (371 loc) · 18.5 KB
/
IsolationScheduler.java
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* 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.storm.scheduler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.lang.Validate;
import org.apache.storm.DaemonConfig;
import org.apache.storm.metric.StormMetricsRegistry;
import org.apache.storm.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// for each isolated topology:
// compute even distribution of executors -> workers on the number of workers specified for the topology
// compute distribution of workers to machines
// determine host -> list of [slot, topology id, executors]
// iterate through hosts and: a machine is good if:
// 1. only running workers from one isolated topology
// 2. all workers running on it match one of the distributions of executors for that topology
// 3. matches one of the # of workers
// blacklist the good hosts and remove those workers from the list of need to be assigned workers
// otherwise unassign all other workers for isolated topologies if assigned
public class IsolationScheduler implements IScheduler {
private static final Logger LOG = LoggerFactory.getLogger(IsolationScheduler.class);
private Map<String, Number> isoMachines;
@Override
public void prepare(Map<String, Object> conf, StormMetricsRegistry metricsRegistry) {
this.isoMachines = (Map<String, Number>) conf.get(DaemonConfig.ISOLATION_SCHEDULER_MACHINES);
Validate.notEmpty(isoMachines);
}
@Override
public Map<String, Map<String, Double>> config() {
return new HashMap<>();
}
// get host -> all assignable worker slots for non-blacklisted machines (assigned or not assigned)
// will then have a list of machines that need to be assigned (machine -> [topology, list of list of executors])
// match each spec to a machine (who has the right number of workers), free everything else on that machine and assign those slots
// (do one topology at a time)
// blacklist all machines who had production slots defined
// log isolated topologies who weren't able to get enough slots / machines
// run default scheduler on isolated topologies that didn't have enough slots + non-isolated topologies on remaining machines
// set blacklist to what it was initially
@Override
public void schedule(Topologies topologies, Cluster cluster) {
List<TopologyDetails> isoTopologies = isolatedTopologies(topologies.getTopologies());
Set<String> isoIds = extractTopologyIds(isoTopologies);
Map<String, Set<Set<ExecutorDetails>>> topologyWorkerSpecs = topologyWorkerSpecs(isoTopologies);
Map<String, Map<Integer, Integer>> topologyMachineDistributions = topologyMachineDistributions(isoTopologies);
Map<String, List<AssignmentInfo>> hostAssignments = hostAssignments(cluster);
for (Map.Entry<String, List<AssignmentInfo>> entry : hostAssignments.entrySet()) {
List<AssignmentInfo> assignments = entry.getValue();
String topologyId = assignments.get(0).getTopologyId();
Map<Integer, Integer> distribution = topologyMachineDistributions.get(topologyId);
Set<Set<ExecutorDetails>> workerSpecs = topologyWorkerSpecs.get(topologyId);
int numWorkers = assignments.size();
if (isoIds.contains(topologyId)
&& checkAssignmentTopology(assignments, topologyId)
&& distribution.containsKey(numWorkers)
&& checkAssignmentWorkerSpecs(assignments, workerSpecs)) {
decrementDistribution(distribution, numWorkers);
for (AssignmentInfo ass : assignments) {
workerSpecs.remove(ass.getExecutors());
}
cluster.blacklistHost(entry.getKey());
} else {
for (AssignmentInfo ass : assignments) {
if (isoIds.contains(ass.getTopologyId())) {
cluster.freeSlot(ass.getWorkerSlot());
}
}
}
}
Map<String, Set<WorkerSlot>> hostUsedSlots = hostToUsedSlots(cluster);
LinkedList<HostAssignableSlots> hss = hostAssignableSlots(cluster);
for (Map.Entry<String, Set<Set<ExecutorDetails>>> entry : topologyWorkerSpecs.entrySet()) {
String topologyId = entry.getKey();
Set<Set<ExecutorDetails>> executorSet = entry.getValue();
List<Integer> workerNum = distributionToSortedAmounts(topologyMachineDistributions.get(topologyId));
for (Integer num : workerNum) {
HostAssignableSlots hostSlots = hss.peek();
List<WorkerSlot> slot = hostSlots != null ? hostSlots.getWorkerSlots() : null;
if (slot != null && slot.size() >= num) {
hss.poll();
cluster.freeSlots(hostUsedSlots.get(hostSlots.getHostName()));
for (WorkerSlot tmpSlot : slot.subList(0, num)) {
Set<ExecutorDetails> executor = removeElemFromExecutorsSet(executorSet);
cluster.assign(tmpSlot, topologyId, executor);
}
cluster.blacklistHost(hostSlots.getHostName());
}
}
}
List<String> failedTopologyIds = extractFailedTopologyIds(topologyWorkerSpecs);
if (failedTopologyIds.size() > 0) {
LOG.warn("Unable to isolate topologies " + failedTopologyIds
+ ". No machine had enough worker slots to run the remaining workers for these topologies. "
+ "Clearing all other resources and will wait for enough resources for "
+ "isolated topologies before allocating any other resources.");
// clear workers off all hosts that are not blacklisted
Map<String, Set<WorkerSlot>> usedSlots = hostToUsedSlots(cluster);
Set<Map.Entry<String, Set<WorkerSlot>>> entries = usedSlots.entrySet();
for (Map.Entry<String, Set<WorkerSlot>> entry : entries) {
if (!cluster.isBlacklistedHost(entry.getKey())) {
cluster.freeSlots(entry.getValue());
}
}
} else {
// run default scheduler on non-isolated topologies
Set<String> allocatedTopologies = allocatedTopologies(topologyWorkerSpecs);
Topologies leftOverTopologies = leftoverTopologies(topologies, allocatedTopologies);
DefaultScheduler.defaultSchedule(leftOverTopologies, cluster);
}
Set<String> origBlacklist = cluster.getBlacklistedHosts();
cluster.setBlacklistedHosts(origBlacklist);
}
private Set<ExecutorDetails> removeElemFromExecutorsSet(Set<Set<ExecutorDetails>> executorsSets) {
Set<ExecutorDetails> elem = executorsSets.iterator().next();
executorsSets.remove(elem);
return elem;
}
private List<TopologyDetails> isolatedTopologies(Collection<TopologyDetails> topologies) {
Set<String> topologyNames = isoMachines.keySet();
List<TopologyDetails> isoTopologies = new ArrayList<TopologyDetails>();
for (TopologyDetails topo : topologies) {
if (topologyNames.contains(topo.getName())) {
isoTopologies.add(topo);
}
}
return isoTopologies;
}
private Set<String> extractTopologyIds(List<TopologyDetails> topologies) {
Set<String> ids = new HashSet<String>();
if (topologies != null && topologies.size() > 0) {
for (TopologyDetails topology : topologies) {
ids.add(topology.getId());
}
}
return ids;
}
private List<String> extractFailedTopologyIds(Map<String, Set<Set<ExecutorDetails>>> isoTopologyWorkerSpecs) {
List<String> failedTopologyIds = new ArrayList<String>();
for (Map.Entry<String, Set<Set<ExecutorDetails>>> topoWorkerSpecsEntry : isoTopologyWorkerSpecs.entrySet()) {
Set<Set<ExecutorDetails>> workerSpecs = topoWorkerSpecsEntry.getValue();
if (workerSpecs != null && !workerSpecs.isEmpty()) {
failedTopologyIds.add(topoWorkerSpecsEntry.getKey());
}
}
return failedTopologyIds;
}
// map from topology id -> set of sets of executors
private Map<String, Set<Set<ExecutorDetails>>> topologyWorkerSpecs(List<TopologyDetails> topologies) {
Map<String, Set<Set<ExecutorDetails>>> workerSpecs = new HashMap<String, Set<Set<ExecutorDetails>>>();
for (TopologyDetails topology : topologies) {
workerSpecs.put(topology.getId(), computeWorkerSpecs(topology));
}
return workerSpecs;
}
private Map<String, List<AssignmentInfo>> hostAssignments(Cluster cluster) {
Collection<SchedulerAssignment> assignmentValues = cluster.getAssignments().values();
Map<String, List<AssignmentInfo>> hostAssignments = new HashMap<String, List<AssignmentInfo>>();
for (SchedulerAssignment sa : assignmentValues) {
Map<WorkerSlot, List<ExecutorDetails>> slotExecutors = Utils.reverseMap(sa.getExecutorToSlot());
Set<Map.Entry<WorkerSlot, List<ExecutorDetails>>> entries = slotExecutors.entrySet();
for (Map.Entry<WorkerSlot, List<ExecutorDetails>> entry : entries) {
WorkerSlot slot = entry.getKey();
List<ExecutorDetails> executors = entry.getValue();
String host = cluster.getHost(slot.getNodeId());
AssignmentInfo ass = new AssignmentInfo(slot, sa.getTopologyId(), new HashSet<ExecutorDetails>(executors));
List<AssignmentInfo> executorList = hostAssignments.get(host);
if (executorList == null) {
executorList = new ArrayList<AssignmentInfo>();
hostAssignments.put(host, executorList);
}
executorList.add(ass);
}
}
return hostAssignments;
}
private Set<Set<ExecutorDetails>> computeWorkerSpecs(TopologyDetails topology) {
Map<String, List<ExecutorDetails>> compExecutors = Utils.reverseMap(topology.getExecutorToComponent());
List<ExecutorDetails> allExecutors = new ArrayList<ExecutorDetails>();
Collection<List<ExecutorDetails>> values = compExecutors.values();
for (List<ExecutorDetails> value : values) {
allExecutors.addAll(value);
}
int numWorkers = topology.getNumWorkers();
int bucketIndex = 0;
Map<Integer, Set<ExecutorDetails>> bucketExecutors = new HashMap<Integer, Set<ExecutorDetails>>(numWorkers);
for (ExecutorDetails executor : allExecutors) {
Set<ExecutorDetails> executors = bucketExecutors.get(bucketIndex);
if (executors == null) {
executors = new HashSet<ExecutorDetails>();
bucketExecutors.put(bucketIndex, executors);
}
executors.add(executor);
bucketIndex = (bucketIndex + 1) % numWorkers;
}
return new HashSet<Set<ExecutorDetails>>(bucketExecutors.values());
}
private Map<String, Map<Integer, Integer>> topologyMachineDistributions(List<TopologyDetails> isoTopologies) {
Map<String, Map<Integer, Integer>> machineDistributions = new HashMap<String, Map<Integer, Integer>>();
for (TopologyDetails topology : isoTopologies) {
machineDistributions.put(topology.getId(), machineDistribution(topology));
}
return machineDistributions;
}
private Map<Integer, Integer> machineDistribution(TopologyDetails topology) {
int machineNum = isoMachines.get(topology.getName()).intValue();
int workerNum = topology.getNumWorkers();
TreeMap<Integer, Integer> distribution = Utils.integerDivided(workerNum, machineNum);
if (distribution.containsKey(0)) {
distribution.remove(0);
}
return distribution;
}
private boolean checkAssignmentTopology(List<AssignmentInfo> assignments, String topologyId) {
for (AssignmentInfo ass : assignments) {
if (!topologyId.equals(ass.getTopologyId())) {
return false;
}
}
return true;
}
private boolean checkAssignmentWorkerSpecs(List<AssignmentInfo> assigments, Set<Set<ExecutorDetails>> workerSpecs) {
for (AssignmentInfo ass : assigments) {
if (!workerSpecs.contains(ass.getExecutors())) {
return false;
}
}
return true;
}
private void decrementDistribution(Map<Integer, Integer> distribution, int value) {
Integer distValue = distribution.get(value);
if (distValue != null) {
int newValue = distValue - 1;
if (newValue == 0) {
distribution.remove(value);
} else {
distribution.put(value, newValue);
}
}
}
private Map<String, Set<WorkerSlot>> hostToUsedSlots(Cluster cluster) {
Collection<WorkerSlot> usedSlots = cluster.getUsedSlots();
Map<String, Set<WorkerSlot>> hostUsedSlots = new HashMap<String, Set<WorkerSlot>>();
for (WorkerSlot slot : usedSlots) {
String host = cluster.getHost(slot.getNodeId());
Set<WorkerSlot> slots = hostUsedSlots.get(host);
if (slots == null) {
slots = new HashSet<WorkerSlot>();
hostUsedSlots.put(host, slots);
}
slots.add(slot);
}
return hostUsedSlots;
}
// returns list of list of slots, reverse sorted by number of slots
private LinkedList<HostAssignableSlots> hostAssignableSlots(Cluster cluster) {
List<WorkerSlot> assignableSlots = cluster.getAssignableSlots();
Map<String, List<WorkerSlot>> hostAssignableSlots = new HashMap<String, List<WorkerSlot>>();
for (WorkerSlot slot : assignableSlots) {
String host = cluster.getHost(slot.getNodeId());
List<WorkerSlot> slots = hostAssignableSlots.get(host);
if (slots == null) {
slots = new ArrayList<WorkerSlot>();
hostAssignableSlots.put(host, slots);
}
slots.add(slot);
}
List<HostAssignableSlots> sortHostAssignSlots = new ArrayList<HostAssignableSlots>();
for (Map.Entry<String, List<WorkerSlot>> entry : hostAssignableSlots.entrySet()) {
sortHostAssignSlots.add(new HostAssignableSlots(entry.getKey(), entry.getValue()));
}
Collections.sort(sortHostAssignSlots, new Comparator<HostAssignableSlots>() {
@Override
public int compare(HostAssignableSlots o1, HostAssignableSlots o2) {
return o2.getWorkerSlots().size() - o1.getWorkerSlots().size();
}
});
Collections.shuffle(sortHostAssignSlots);
return new LinkedList<HostAssignableSlots>(sortHostAssignSlots);
}
private List<Integer> distributionToSortedAmounts(Map<Integer, Integer> distributions) {
List<Integer> sorts = new ArrayList<Integer>();
for (Map.Entry<Integer, Integer> entry : distributions.entrySet()) {
int workers = entry.getKey();
int machines = entry.getValue();
for (int i = 0; i < machines; i++) {
sorts.add(workers);
}
}
Collections.sort(sorts, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.intValue() - o1.intValue();
}
});
return sorts;
}
private Set<String> allocatedTopologies(Map<String, Set<Set<ExecutorDetails>>> topologyToWorkerSpecs) {
Set<String> allocatedTopologies = new HashSet<String>();
Set<Map.Entry<String, Set<Set<ExecutorDetails>>>> entries = topologyToWorkerSpecs.entrySet();
for (Map.Entry<String, Set<Set<ExecutorDetails>>> entry : entries) {
if (entry.getValue().isEmpty()) {
allocatedTopologies.add(entry.getKey());
}
}
return allocatedTopologies;
}
private Topologies leftoverTopologies(Topologies topologies, Set<String> filterIds) {
Collection<TopologyDetails> topos = topologies.getTopologies();
Map<String, TopologyDetails> leftoverTopologies = new HashMap<String, TopologyDetails>();
for (TopologyDetails topo : topos) {
String id = topo.getId();
if (!filterIds.contains(id)) {
leftoverTopologies.put(id, topo);
}
}
return new Topologies(leftoverTopologies);
}
class AssignmentInfo {
private WorkerSlot workerSlot;
private String topologyId;
private Set<ExecutorDetails> executors;
AssignmentInfo(WorkerSlot workerSlot, String topologyId, Set<ExecutorDetails> executors) {
this.workerSlot = workerSlot;
this.topologyId = topologyId;
this.executors = executors;
}
public WorkerSlot getWorkerSlot() {
return workerSlot;
}
public String getTopologyId() {
return topologyId;
}
public Set<ExecutorDetails> getExecutors() {
return executors;
}
}
class HostAssignableSlots {
private String hostName;
private List<WorkerSlot> workerSlots;
HostAssignableSlots(String hostName, List<WorkerSlot> workerSlots) {
this.hostName = hostName;
this.workerSlots = workerSlots;
}
public String getHostName() {
return hostName;
}
public List<WorkerSlot> getWorkerSlots() {
return workerSlots;
}
}
}