-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDispatchPolicyStrategy.py
More file actions
603 lines (496 loc) · 20.9 KB
/
DispatchPolicyStrategy.py
File metadata and controls
603 lines (496 loc) · 20.9 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import abc # Python's built-in abstract class library
import numpy as np
import math
########################################################################################################################
# INTERFACE
########################################################################################################################
class DispatchPolicyStrategyAbstract:
"""An abstract class from which all dispatching policies need to inherit."""
# This is how you define abstract classes in Python.
__metaclass__ = abc.ABCMeta
##
# Upon an arrival gets a queue network instance and outputs which queues receive it and what's the workload each
# one of them adds to itself.
##
@abc.abstractmethod
def getDispatch(self, network):
"""Required Method"""
##
# Get effective service rate of policy.
##
@abc.abstractmethod
def getEffectiveServiceRate(self, network):
"""Required Method"""
##
# Get name of policy.
##
@abc.abstractmethod
def getName(self):
"""Required Method"""
##
# Get name of policy.
##
@abc.abstractmethod
def getOneQueueMu(self):
"""Required Method"""
##
# Get level of redundancy for the policy.
##
def getRedundancy(self):
return 1
##
# Get policy params string for logging.
##
def getParamStr(self):
return ""
########################################################################################################################
# IMPLEMENTATIONS
########################################################################################################################
class FixedSubsetsStrategy(DispatchPolicyStrategyAbstract):
"""A fixed subsets dispatching policy."""
def __init__(self, redundancy, alpha, beta, p):
self.redundancy = redundancy
self.alpha = alpha
self.beta = beta
self.p = p
##
# Randomly choose one of the fixed subsets and determine the workload each one of the queues in it will get.
# Assumption: network.size % redundancy == 0
##
def getDispatch(self, network):
n = network.getSize()
# Choose the subset.
numOfSubsets = int(math.ceil(n / float(self.redundancy)))
chosenSubset = np.random.randint(numOfSubsets)
queuesChosen = filter(lambda x: x < n,
range(chosenSubset * self.redundancy, (chosenSubset + 1) * self.redundancy))
# Randomize the incoming job's workload for each queue chosen.
randWorkload = np.random.choice([self.alpha, self.beta], self.redundancy, p=[self.p, 1.0 - self.p])
# Determine the total increment of workload in every queue chosen.
currWorkload = [network.getWorkloads()[q] for q in queuesChosen]
min_i = 0
for i in range(len(currWorkload)):
if currWorkload[i] + randWorkload[i] < currWorkload[min_i] + randWorkload[min_i]:
min_i = i
maxTotalWorkloadInQueue = currWorkload[min_i] + randWorkload[min_i]
addedWorkload = [np.max(maxTotalWorkloadInQueue - currWorkload[i], 0) for i in range(len(currWorkload))]
return queuesChosen, addedWorkload
def getName(self):
return "fixed subsets"
def getEffectiveServiceRate(self, network):
muEffective = 1.0 / (float(self.alpha) * (1.0 - (1.0 - float(self.p))**self.redundancy) +
float(self.beta) * (1.0 - self.p)**self.redundancy)
return muEffective * (float(network.getSize()) / float(self.redundancy))
def getOneQueueMu(self):
return 1.0 / (self.alpha*self.p + self.beta*(1.0 - self.p))
def getRedundancy(self):
return self.redundancy
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta) + \
", d = " + str(self.redundancy)
class RandomQueueStrategy(DispatchPolicyStrategyAbstract):
"""A random dispatching policy. A random queue will get the job."""
def __init__(self, alpha, beta, p):
self.alpha = alpha
self.beta = beta
self.p = p
self.mu = 1.0 / (float(alpha) * float(p) + float(beta) * (1.0 - p))
##
# Randomly choose a queue and determine the workload it will get.
##
def getDispatch(self, network):
return [np.random.choice(range(network.getSize()))], [np.random.choice([self.alpha, self.beta],
p=[self.p, 1.0 - self.p])]
def getName(self):
return "random queue"
def getEffectiveServiceRate(self, network):
return self.mu * network.getSize()
def getOneQueueMu(self):
return self.mu
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta)
class OneQueueFixedServiceRateStrategy(DispatchPolicyStrategyAbstract):
"""A fixed service rate dispatching policy."""
##
# Initialize policy with @alpha being small workload for a job, @mu being the total service rate and @p being the
# probability to choose alpha.
##
def __init__(self, alpha, mu, p):
if 1.0 / mu <= float(alpha):
raise Exception("Error: must be [ (1.0 / mu) > alpha ] in order to dispatch correctly.")
self.alpha = alpha
self.beta = ((1.0/mu) - float(alpha)*p) / (1.0 - p)
self.p = p
self.mu = mu
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
workload = self.alpha
if np.random.binomial(1, self.p) == 0:
workload = self.beta
return [0], [workload]
# return [0], [np.random.choice([self.alpha, self.beta], p=[self.p, 1.0 - self.p])]
def getName(self):
return "one queue fixed service rate"
def getEffectiveServiceRate(self, network):
return self.mu
def setP(self, p):
if p < 0 or p > 1:
raise Exception("Invalid probability given")
self.p = p
self.beta = ((1.0/self.mu) - float(self.alpha)*p) / (1.0 - p)
def setBeta(self, beta):
if beta <= self.alpha:
raise Exception("Invalid beta, should be greater than alpha")
self.p = (beta - (1.0/self.mu)) / (beta - self.alpha)
self.beta = beta
def getOneQueueMu(self):
return self.mu
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta) + \
", mu = " + str(self.mu)
class OnlyFirstQueueGetsJobsStrategy(DispatchPolicyStrategyAbstract):
##
# Initialize policy with @alpha being small workload for a job, @mu being the total service rate and @p being the
# probability to choose alpha.
##
def __init__(self, alpha, beta, p, n):
self.alpha = alpha
self.beta = beta
self.p = p
self.mu = 1.0 / (float(alpha)*float(p) + float(beta)*(1.0 - p))
self.n = n
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
workload = self.alpha
if np.random.binomial(1, self.p) == 0:
workload = self.beta
return [0], [workload]
# return [0], [np.random.choice([self.alpha, self.beta], p=[self.p, 1.0 - self.p])]
def getName(self):
return "only first queue gets jobs"
def getEffectiveServiceRate(self, network):
return self.mu * self.n
def getOneQueueMu(self):
return self.mu
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta) + \
", n = " + str(self.n)
class JoinShortestWorkloadStrategy(DispatchPolicyStrategyAbstract):
"""A dispatching policy that gives the job to the queue with the shortest workload."""
##
# Initialize policy with @alpha being small workload for a job, @beta being unusual workload for a job and @p being
# the probability to choose @alpha.
##
def __init__(self, alpha, beta, p):
self.alpha = alpha
self.mu = 1.0 / (float(alpha)*p + float(beta)*(1.0 - p))
if 1.0 / self.mu <= float(alpha):
raise Exception("Error: must be [ (1.0 / mu) > alpha ] in order to dispatch correctly.")
self.beta = beta
self.p = p
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
workload = self.alpha
if np.random.binomial(1, self.p) == 0:
workload = self.beta
return [np.argmin(network.getWorkloads())], [workload]
def getName(self):
return "join shortest workload"
def getEffectiveServiceRate(self, network):
return self.mu * network.getSize()
def getOneQueueMu(self):
return self.mu
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta)
class RouteToAllStrategy(DispatchPolicyStrategyAbstract):
"""A dispatching policy that gives the job to all the queues."""
##
# Initialize policy with @alpha being small workload for a job, @beta being unusual workload for a job and @p being
# the probability to choose @alpha.
##
def __init__(self, alpha, beta, p):
self.alpha = alpha
self.mu = 1.0 / (float(alpha)*p + float(beta)*(1.0 - p))
if 1.0 / self.mu <= float(alpha):
raise Exception("Error: must be [ (1.0 / mu) > alpha ] in order to dispatch correctly.")
self.beta = beta
self.p = p
self.n = -1
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
self.n = network.getSize()
workload = np.min(np.random.choice([self.alpha, self.beta], network.getSize(), p=[self.p, 1.0 - self.p]))
return range(network.getSize()), [workload for i in range(network.getSize())]
def getName(self):
return "route to all"
def getEffectiveServiceRate(self, network):
self.n = network.getSize()
return 1.0 / (float(self.beta)*(1.0-self.p)**network.getSize() +
float(self.alpha)*(1.0-(1.0-self.p)**network.getSize()))
def getOneQueueMu(self):
return self.mu
def getRedundancy(self):
return self.n
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta)
class VolunteerOrTeamworkStrategy(DispatchPolicyStrategyAbstract):
"""A dispatching policy that gives the job to all queues with probability @q or to 1 random queue with probability
1-@q."""
##
# Initialize policy with @alpha being small workload for a job, @beta being unusual workload for a job and @p being
# the probability to choose @alpha. @q is the probability to route to all.
##
def __init__(self, alpha, beta, p, q):
self.routeToAll = RouteToAllStrategy(alpha, beta, p)
self.q = q
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
# FIXME: wrong assumption in route-to-all. Not all queues have the same workload!
if np.random.binomial(1, self.q) == 0:
return self.routeToAll.getDispatch(network)
return [np.random.choice(range(network.getSize()))], \
[np.random.choice([self.routeToAll.alpha, self.routeToAll.beta],
p=[self.routeToAll.p, 1.0 - self.routeToAll.p])]
def getName(self):
return "volunteer or teamwork"
##
# Capacity region unknown so why not give it extra 20% :)?
##
def getEffectiveServiceRate(self, network):
return 1.2 * self.routeToAll.mu * network.getSize()
def getOneQueueMu(self):
return self.routeToAll.getOneQueueMu()
def getRedundancy(self):
return [1, self.routeToAll.getRedundancy()]
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.routeToAll.p) + ", alpha = " + str(self.routeToAll.alpha) + \
", beta = " + str(self.routeToAll.beta) + ", q = " + str(self.q)
class RandomDStrategy(DispatchPolicyStrategyAbstract):
"""A dispatching policy that chooses d queues at random and dispatches the job to them."""
##
# Initialize policy with @alpha being small workload for a job, @beta being unusual workload for a job and @p being
# the probability to choose @alpha. @d is the redundancy level.
##
def __init__(self, alpha, beta, p, d, bias=0.0):
self.alpha = int(alpha)
self.mu = 1.0 / (float(alpha) * p + float(beta) * (1.0 - p))
if 1.0 / self.mu <= float(alpha):
raise Exception("Error: must be [ (1.0 / mu) > alpha ] in order to dispatch correctly.")
self.beta = int(beta)
self.p = float(p)
self.d = int(d)
self.bias = bias
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
# get network state.
currWlds = network.getWorkloads()
n = network.getSize()
# choose queues to receive the job.
chosenQueues = np.random.choice(range(n), self.d, replace=False, p=[1.0/n]*n)
# randomize incoming workload for each queue.
incomingWlds = np.random.choice([self.alpha, self.beta], self.d, p=[self.p, 1.0 - self.p])
speculation = [currWlds[chosenQueues[i]] + incomingWlds[i] for i in range(self.d)]
min_i = int(np.argmin(speculation))
added = [np.max([speculation[min_i] - currWlds[chosenQueues[i]], 0]) for i in range(self.d)]
return chosenQueues, added
def getName(self):
return "random-d out of n"
##
# Capacity region unknown. Returns cap. region of d=1 with a factor of the bias.
##
def getEffectiveServiceRate(self, network):
return self.mu * network.getSize() * (1.0 + self.bias)
def getOneQueueMu(self):
return self.mu
def getRedundancy(self):
return self.d
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta) + \
", d = " + str(self.d)
class GeometricDeltaRandomDStrategy(DispatchPolicyStrategyAbstract):
"""A dispatching policy that chooses d queues at random and dispatches the job to them. Job size is determined as
a + geometric(p)"""
##
# Initialize policy with @alpha being constant workload for a job, @p being a geometric distribution parameter from
# which a job's random workload part will be drawn, @n being the number of servers in the system. @d is the
# redundancy level.
##
def __init__(self, alpha, p, d, n):
if p >= 1.0 / 2.0*int(n):
raise Exception("Error: must be [ (1.0 / 2*n) > p ] in order to dispatch correctly.")
self.p = float(p)
self.n = int(n)
self.alpha = int(alpha)
self.delta = np.random.RandomState()
self.mu = 1.0 / (int(alpha) + (1.0 / float(p)))
if 1.0 / self.mu <= float(alpha):
raise Exception("Error: must be [ (1.0 / mu) > alpha ] in order to dispatch correctly.")
self.d = int(d)
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
# get network state.
currWlds = network.getWorkloads()
n = network.getSize()
# choose queues to receive the job.
chosenQueues = np.random.choice(range(n), self.d, replace=False, p=[1.0/n]*n)
# randomize incoming workload for each queue.
incomingWlds = self.alpha + self.delta.geometric(p=self.p, size=self.d)
speculation = [currWlds[chosenQueues[i]] + incomingWlds[i] for i in range(self.d)]
min_i = int(np.argmin(speculation))
added = [np.max([speculation[min_i] - currWlds[chosenQueues[i]], 0]) for i in range(self.d)]
return chosenQueues, added
def getName(self):
return "geometric delta random-d"
##
# Capacity region unknown. Returns cap. region of d=1.
##
def getEffectiveServiceRate(self, network):
return self.mu * network.getSize()
def getOneQueueMu(self):
return self.mu
def getRedundancy(self):
return self.d
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", d = " + str(self.d)
class RouteToIdleQueuesStrategy(DispatchPolicyStrategyAbstract):
"""A dispatching policy that chooses all idle queues and dispatches the job to them. If none are idle, a queue is
chosen at random."""
##
# Initialize policy with @alpha being small workload for a job, @beta being unusual workload for a job and @p being
# the probability to choose @alpha.
##
def __init__(self, alpha, beta, p, bias=0.0):
self.alpha = int(alpha)
self.mu = 1.0 / (float(alpha) * p + float(beta) * (1.0 - p))
if 1.0 / self.mu <= float(alpha):
raise Exception("Error: must be [ (1.0 / mu) > alpha ] in order to dispatch correctly.")
self.beta = int(beta)
self.p = float(p)
self.bias = bias
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
# get network state.
currWlds = network.getWorkloads()
n = network.getSize()
# choose queues to receive the job.
chosenQueues = []
for i in np.argsort(currWlds):
if currWlds[i] > 0:
break
chosenQueues.append(i)
if not chosenQueues:
chosenQueues = [np.random.choice(range(n))]
# randomize incoming workload for each queue.
added = [np.min(np.random.choice([self.alpha, self.beta], len(chosenQueues), p=[self.p, 1.0 - self.p]))] * \
len(chosenQueues)
return chosenQueues, added
def getName(self):
return "route to idle queues"
##
# Capacity region unknown. Returns cap. region of d=1 with a factor of the bias.
##
def getEffectiveServiceRate(self, network):
return self.mu * network.getSize() * (1.0 + self.bias)
def getOneQueueMu(self):
return self.mu
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta)
import itertools
class RoundRobinRedundancyDStrategy(DispatchPolicyStrategyAbstract):
"""A dispatching policy that routes to a subset of d queues from a random permutation of all possible d-sized
subsets of {0, 1, ... , n-1}. Once the permutation is decided, the policy will do a round robin on them."""
##
# Initialize policy with @alpha being small workload for a job, @beta being unusual workload for a job and @p being
# the probability to choose @alpha. @d is the redundancy level. @n is the number of queues.
##
def __init__(self, alpha, beta, p, d, n, bias=0.0):
self.alpha = int(alpha)
self.mu = 1.0 / (float(alpha) * p + float(beta) * (1.0 - p))
if 1.0 / self.mu <= float(alpha):
raise Exception("Error: must be [ (1.0 / mu) > alpha ] in order to dispatch correctly.")
self.beta = int(beta)
self.p = float(p)
self.d = int(d)
self.n = n
self.bias = bias
self.order = [list(i) for i in itertools.combinations(range(n), d)]
np.random.shuffle(self.order)
self.round = 0
##
# Randomize arriving job's workload.
##
def getDispatch(self, network):
# get network state.
currWlds = network.getWorkloads()
# choose queues to receive the job.
chosenQueues = self.order[self.round]
self.round = (self.round + 1) % self.n
# randomize incoming workload for each queue.
incomingWlds = np.random.choice([self.alpha, self.beta], self.d, p=[self.p, 1.0 - self.p])
speculation = [currWlds[chosenQueues[i]] + incomingWlds[i] for i in range(self.d)]
min_i = int(np.argmin(speculation))
added = [np.max([speculation[min_i] - currWlds[chosenQueues[i]], 0]) for i in range(self.d)]
return chosenQueues, added
def getName(self):
return "round robin redundancy-d"
##
# Capacity region unknown. Returns cap. region of d=1 with a factor of the bias.
##
def getEffectiveServiceRate(self, network):
return self.mu * network.getSize() * (1.0 + self.bias)
def getOneQueueMu(self):
return self.mu
def getRedundancy(self):
return self.d
##
# Get policy params string for logging.
##
def getParamStr(self):
return "p = " + str(self.p) + ", alpha = " + str(self.alpha) + ", beta = " + str(self.beta) + \
", d = " + str(self.d)