-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppSchedulable.py
302 lines (228 loc) · 12.1 KB
/
AppSchedulable.py
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
'''
Created on Jan 8, 2015
@author: niuzhaojie
'''
from Schedulable import Schedulable
from Resources import Resources
from RMContainerInfo import RMContainerInfo
from SchedulableStatus import SchedulableStatus
from SimilarityType import SimilarityType
from Utility import Utility
import Configuration
class AppSchedulable(Schedulable):
'''
classdocs
'''
def __init__(self, scheduler, app, queue):
'''
Constructor
'''
super(AppSchedulable, self).__init__()
self._scheduler = scheduler
self._app = app
self._queue = queue
self._startTime = scheduler.getCurrentTime()
self._demand = Resources.createResource(0, 0, 0, 0)
self._multipleResourceFitness = -1.0
self._resourceEntropy = -1.0
self._blockCount = 0
def getBlockCount(self):
return self._blockCount
def setBlockCount(self, blockCount):
self._blockCount = blockCount
def getApp(self):
return self._app
def getName(self):
return self._app.getApplicationID()
def updateDemand(self):
self._demand = Resources.createResource(0, 0, 0, 0)
Resources.addTo(self._demand, self._app.getCurrentConsumption())
for p in self._app.getPriorities():
for task in self._app.getResourceRequests(p):
if task.getStatus() == SchedulableStatus.RUNNABLE:
resource = task.getResource()
Resources.addTo(self._demand, resource)
def getDemand(self):
return self._demand
def getStartTime(self):
return self._startTime
def getResourceUsage(self):
return self._app.getCurrentConsumption()
def getAnyResourceRequest(self):
return self._app.getAnyResourceRequest()
def createContainer(self, node, task):
containerID = self._app.getNewContainerID()
return RMContainerInfo(containerID, self._app, node, task, self._scheduler.getCurrentTime())
def hasContainerForNode(self, priority, node):
requests = self._app.getResourceRequests(priority)
if len(requests) == 0:
return False
else:
return True
'''for task in requests:
if Resources.allFitIn(task.getResource(), node.getCapacity()):
return True
return False'''
def taskFitsInNode(self, task, node):
if not self._scheduler.consideringIO():
return Resources.fitsIn(task.getResource(), node.getAvailableResource())
else:
if task.getExpectedNode() == None:
#print("any" + ", request: " + str(task.getResource()) + ", node: " + str(node) + "," + str(node.getAvailableResource()))
return Resources.allFitIn(task.getResource(), node.getAvailableResource())
else:
if task.getExpectedNode() == node:
#print("local" + ", request: " + str(task.getResource()) + ", node: " + str(node) + "," + str(node.getAvailableResource()))
return Resources.localFitIn(task.getResource(), node.getAvailableResource())
else:
#print("remote" + ", request: " + str(task.getResource()) + ", node: " + str(node) + "," + str(node.getAvailableResource()) + ", expected: " + str(task.getExpectedNode()) + "," + str(task.getExpectedNode().getAvailableResource()))
return Resources.remoteFitIn(task.getResource(), node.getAvailableResource(), task.getExpectedNode().getAvailableResource())
def assignContainerToTask(self, node, priority, task, reserved):
container = None
if reserved:
print("reserved")
container = node.getReservedContainer()
else:
container = self.createContainer(node, task)
#debug
'''expectedNode = "none"
if task.getExpectedNode() != None:
expectedNode = task.getExpectedNode().getNodeID()
print("expected node: " + expectedNode)'''
if self.taskFitsInNode(task, node):
self._app.allocate(priority, container)
if reserved:
# unserve
print("unserve")
# inform the node
node.allocateContainer(container)
# add to new launch container list of the scheduler
self._scheduler._newLaunchContainerList.append(
{"containerID": container.getContainerID(),
"appID": self._app.getApplicationID(),
"node": node.getNodeID(),
"priority": priority,
"task": task.getTaskID(),
"reserved": reserved})
self._scheduler._launchedContainerDict[container.getContainerID()] = container
return task.getResource()
else:
#print(Resources.notFit())
return Resources.notFit()
def assignContainerByPriority(self, node, reserved):
if reserved:
container = node.getReservedContainer()
priority = container.getTask().getPriority()
if len(self._app.getResourceRequests(priority)) == 0:
# unreserve the previous reserved container
return Resources.none()
prioritiesToTry = []
if reserved:
prioritiesToTry.append(node.getReservedContainer().getTask().getPriority())
else:
prioritiesToTry = self._app.getPriorities()
for priority in prioritiesToTry:
if not self.hasContainerForNode(priority, node):
continue
tasks = self._app.getResourceRequests(priority)
#first, assign contaienr to any request
anyRequests = [task for task in tasks if task.getExpectedNode() == None]
if len(anyRequests) > 0:
return self.assignContainerToTask(node, priority, anyRequests[0], reserved)
#second, assign container to local request
localRequests = [task for task in tasks if task.getExpectedNode() == node]
if len(localRequests) > 0:
return self.assignContainerToTask(node, priority, localRequests[0], reserved)
#Lastly, assign container to other request
otherRequests = [task for task in tasks if task not in localRequests and task not in anyRequests]
if len(otherRequests) > 0:
return self.assignContainerToTask(node, priority, otherRequests[0], reserved)
return Resources.none()
def assignContainer(self, node):
return self.assignContainerByPriority(node, False)
def getMultiResFitness(self):
return self._multipleResourceFitness
def getResEntropy(self):
return self._resourceEntropy
def calResEntropy(self, node):
currentRunningContainer = node.getRunningContainers()
runningDominantResList = []
for container in currentRunningContainer:
runningDominantResList.append(container.getTask().getResource().getDominantResource())
entropy = -1.0
priorities = self._app.getPriorities()
for priority in priorities:
if not self.hasContainerForNode(priority, node):
continue
tasks = self._app.getResourceRequests(priority)
#if len(tasks) > 0:
task = tasks[0]
if Resources.allFitIn(task.getResource(), node.getAvailableResource()):
dominantResList = runningDominantResList[:]
dominantResList.append(task.getResource().getDominantResource())
entropy = Utility.calEntropyOfVectorList(dominantResList)
break
self._resourceEntropy = entropy
def calMultiResFitness(self, node, similarityType = SimilarityType.PRODUCT):
fitness = -1.0
priorities = self._app.getPriorities()
for priority in priorities:
if not self.hasContainerForNode(priority, node):
continue
tasks = self._app.getResourceRequests(priority)
#first, assign contaienr to any request
anyRequests = [task for task in tasks if task.getExpectedNode() == None]
if len(anyRequests) > 0:
task = anyRequests[0]
if self.taskFitsInNode(task, node):
if similarityType == SimilarityType.PRODUCT:
fitness = Resources.normalizedDotProduct(task.getResource(), node.getAvailableResource(), node.getCapacity(), self._scheduler.consideringIO())
elif similarityType == SimilarityType.COSINE:
fitness = Resources.cosineSimilarity(task.getResource(), node.getAvailableResource(), self._scheduler.consideringIO())
else:
fitness = 0.0
else:
fitness = 0.0
break
#second, assign container to local request
localRequests = [task for task in tasks if task.getExpectedNode() == node]
if len(localRequests) > 0:
task = localRequests[0]
if self.taskFitsInNode(task, node):
localResource = Resources.createResource(task.getResource().getMemory(), task.getResource().getCPU(),
task.getResource().getDisk(), 0)
#localResource = task.getResource()
if similarityType == SimilarityType.PRODUCT:
fitness = Resources.normalizedDotProduct(localResource, node.getAvailableResource(), node.getCapacity(), self._scheduler.consideringIO())
elif similarityType == SimilarityType.COSINE:
fitness = Resources.cosineSimilarity(localResource, node.getAvailableResource(), self._scheduler.consideringIO())
else:
fitness = 0.0
else:
fitness = 0.0
break
#Lastly, assign container to other request
otherRequests = [task for task in tasks if task not in localRequests and task not in anyRequests]
if len(otherRequests) > 0:
task = otherRequests[0]
if self.taskFitsInNode(task, node):
localResource = Resources.createResource(task.getResource().getMemory(), task.getResource().getCPU(),
task.getResource().getDisk(), 0)
if similarityType == SimilarityType.PRODUCT:
fitness = Resources.normalizedDotProduct(localResource, node.getAvailableResource(), node.getCapacity(), self._scheduler.consideringIO())
elif similarityType == SimilarityType.COSINE:
fitness = Resources.cosineSimilarity(localResource, node.getAvailableResource(), self._scheduler.consideringIO())
else:
fitness = 0.0
fitness = fitness * (1 - Configuration.REMOTE_PENALTY)
else:
fitness = 0.0
break
self._multipleResourceFitness = fitness
def getCurrentResourceDemand(self):
prioritiesToTry = self._app.getPriorities()
for priority in prioritiesToTry:
tasks = self._app.getResourceRequests(priority)
if (len(tasks) > 0):
return tasks[0].getResource()
return Resources.none()