-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharrbeh.go
288 lines (256 loc) · 9.07 KB
/
arrbeh.go
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
package qsim
import (
"math/rand"
"sort"
)
// An ArrBeh ("arrival behavior") assigns new jobs to queues or processors.
type ArrBeh interface {
Assign(j *Job) Assignment
BeforeAssign(f func(ab ArrBeh, j *Job) *Assignment)
AfterAssign(f func(ab ArrBeh, j *Job, ass Assignment))
}
// ShortestQueueArrBeh implements the ArrBeh interface, assigning new
// Jobs by the following algorithm:
//
// – If there is at least one idle Processor, pick an idle Processor at
// random and start the Job on it.
// – Otherwise, append the Job to the shortest Queue available. If the
// shortest queue length is shared by more than one Queue, the Job is
// appended to one of those Queues at random.
//
// This behavior is like that of a supermarket checkout line: if there's
// an empty aisle you go straight there; otherwise you find the shortest
// queue and join it.
type ShortestQueueArrBeh struct {
// Queues contains all the queues known to us.
Queues []*Queue
// IdleProcessors keeps track of which Processors are idle. A Processor
// is a key in this map iff it is idle.
IdleProcessors map[*Processor]bool
// Callback lists
cbBeforeAssign []func(ab ArrBeh, j *Job) *Assignment
cbAfterAssign []func(ab ArrBeh, j *Job, ass Assignment)
}
// Assign takes the given Job and assigns it to a queue or a processor.
// The documentation for ShortestQueueArrBeh describes the logic used in
// this implementation.
func (ab *ShortestQueueArrBeh) Assign(j *Job) Assignment {
var proc *Processor
var procs []*Processor
var q *Queue
var shortQueues []*Queue
var i, smallestLength int
var ass Assignment
var assPtr *Assignment
// Allow beforeAssign callback to override the assignment logic
assPtr = ab.beforeAssign(j)
if assPtr != nil {
ab.assign(j, *assPtr)
ab.afterAssign(j, *assPtr)
return *assPtr
}
// Assign to an idle processor if there is at least one
if len(ab.IdleProcessors) >= 1 {
procs = make([]*Processor, 0)
for proc, _ = range ab.IdleProcessors {
procs = append(procs, proc)
}
i = rand.Intn(len(procs))
ass = Assignment{Type: "Processor", Processor: procs[i]}
ab.assign(j, ass)
ab.afterAssign(j, ass)
return ass
}
// If we've arrived here, then there are no idle Processors.
sort.Sort(ByQueueLength(ab.Queues))
smallestLength = ab.Queues[0].Length()
shortQueues = make([]*Queue, 0, len(ab.Queues))
for i = 0; i < len(ab.Queues) && ab.Queues[i].Length() == smallestLength; i++ {
shortQueues = append(shortQueues, ab.Queues[i])
}
// Pick a random element from the list of queues that have the shortest length.
i = rand.Intn(len(shortQueues))
q = shortQueues[i]
ab.beforeAssign(j)
ass = Assignment{Type: "Queue", Queue: q}
ab.assign(j, ass)
ab.afterAssign(j, ass)
return ass
}
// assign does the appropriate thing with the Job given an Assignment.
func (ab *ShortestQueueArrBeh) assign(j *Job, ass Assignment) {
switch ass.Type {
case "Processor":
ass.Processor.Start(j)
D("Job", j.JobId, "arrived and was assigned to Processor", ass.Processor)
case "Queue":
ass.Queue.Append(j)
D("Job", j.JobId, "arrived and was assigned to Queue", ass.Queue)
default:
panic("Tried to process Assignment with unknown Type '" + ass.Type + "'")
}
}
// BeforeAssign adds a callback to run immediately before the Arrival Behavior
// assigns a job to a Queue or Processor. This callback is passed the ArrBeh
// itself as well as the Job that's about to be assigned.
//
// The callback may return an Assignment pointer. If it does so, this Assignment
// will override the ArrBeh's assignment logic. Otherwise, if the callback
// returns <nil>, the assignment will proceed normally.
//
// If there are multiple BeforeAssign callbacks that return non-nil Assignment
// pointers, the callback most recently created wins.
func (ab *ShortestQueueArrBeh) BeforeAssign(f func(ArrBeh, *Job) *Assignment) {
ab.cbBeforeAssign = append(ab.cbBeforeAssign, f)
}
func (ab *ShortestQueueArrBeh) beforeAssign(j *Job) *Assignment {
var assPtr, newAssPtr *Assignment
for _, cb := range ab.cbBeforeAssign {
newAssPtr = cb(ab, j)
if newAssPtr != nil {
assPtr = newAssPtr
}
}
return assPtr
}
// BeforeAssign adds a callback to run immediately after the Arrival Behavior
// assigns a job to a Queue or Processor. This callback is passed the ArrBeh
// itself, the Job that's about to be assigned, and an Assignment struct
// indicating where the Job was placed.
func (ab *ShortestQueueArrBeh) AfterAssign(f func(ArrBeh, *Job, Assignment)) {
ab.cbAfterAssign = append(ab.cbAfterAssign, f)
}
func (ab *ShortestQueueArrBeh) afterAssign(j *Job, ass Assignment) {
for _, cb := range ab.cbAfterAssign {
cb(ab, j, ass)
}
}
// NewShortestQueueArrBeh initializes a ShortestQueueArrBeh with the given Queues &
// Processors.
func NewShortestQueueArrBeh(queues []*Queue, procs []*Processor, ap ArrProc) ArrBeh {
var ab *ShortestQueueArrBeh
var p *Processor
ab = new(ShortestQueueArrBeh)
ab.Queues = queues
ab.IdleProcessors = make(map[*Processor]bool)
for _, p = range procs {
if p.IsIdle() {
ab.IdleProcessors[p] = true
}
}
// These callbacks keep ab.IdleProcessors up to date.
afterStart := func(p *Processor, j *Job, procTime int) {
delete(ab.IdleProcessors, p)
}
afterFinish := func(p *Processor, j *Job) {
ab.IdleProcessors[p] = true
}
for _, p = range procs {
p.AfterStart(afterStart)
p.AfterFinish(afterFinish)
}
// Make sure that newly arriving Jobs get assigned.
ap.AfterArrive(func(cbArrProc ArrProc, cbJobs []*Job, cbInterval int) {
for _, j := range cbJobs {
ab.Assign(j)
}
})
return ab
}
// AlwaysQueueArrBeh always puts incoming jobs in the given queue. Processors
// don't even enter into it.
type AlwaysQueueArrBeh struct {
Q *Queue
// Callback lists
cbBeforeAssign []func(ab ArrBeh, j *Job) *Assignment
cbAfterAssign []func(ab ArrBeh, j *Job, ass Assignment)
}
// Assign takes the given Job and assigns it to the queue.
func (ab *AlwaysQueueArrBeh) Assign(j *Job) Assignment {
// Allow beforeAssign callback to override the assignment logic
assPtr := ab.beforeAssign(j)
if assPtr != nil {
ab.assign(j, *assPtr)
ab.afterAssign(j, *assPtr)
return *assPtr
}
ass := Assignment{Type: "Queue", Queue: ab.Q}
ab.assign(j, ass)
ab.afterAssign(j, ass)
return ass
}
// assign does the appropriate thing with the Job given an Assignment.
func (ab *AlwaysQueueArrBeh) assign(j *Job, ass Assignment) {
switch ass.Type {
case "Processor":
panic("AlwaysQueueArrBeh does not support assignment to Processors")
case "Queue":
ass.Queue.Append(j)
D("Job", j.JobId, "arrived and was assigned to Queue", ass.Queue)
default:
panic("Tried to process Assignment with unknown Type '" + ass.Type + "'")
}
}
// BeforeAssign adds a callback to run immediately before the Arrival Behavior
// assigns a job to a Queue or Processor. This callback is passed the ArrBeh
// itself as well as the Job that's about to be assigned.
//
// The callback may return an Assignment pointer. If it does so, this Assignment
// will override the ArrBeh's assignment logic. Otherwise, if the callback
// returns <nil>, the assignment will proceed normally.
//
// If there are multiple BeforeAssign callbacks that return non-nil Assignment
// pointers, the callback most recently created wins.
func (ab *AlwaysQueueArrBeh) BeforeAssign(f func(ArrBeh, *Job) *Assignment) {
ab.cbBeforeAssign = append(ab.cbBeforeAssign, f)
}
func (ab *AlwaysQueueArrBeh) beforeAssign(j *Job) *Assignment {
var assPtr, newAssPtr *Assignment
for _, cb := range ab.cbBeforeAssign {
newAssPtr = cb(ab, j)
if newAssPtr != nil {
assPtr = newAssPtr
}
}
return assPtr
}
// AfterAssign adds a callback to run immediately after the Arrival Behavior
// assigns a job to a Queue or Processor. This callback is passed the ArrBeh
// itself, the Job that's about to be assigned, and an Assignment struct
// indicating where the Job was placed.
func (ab *AlwaysQueueArrBeh) AfterAssign(f func(ArrBeh, *Job, Assignment)) {
ab.cbAfterAssign = append(ab.cbAfterAssign, f)
}
func (ab *AlwaysQueueArrBeh) afterAssign(j *Job, ass Assignment) {
for _, cb := range ab.cbAfterAssign {
cb(ab, j, ass)
}
}
// NewAlwaysQueueArrBeh initializes a AlwaysQueueArrBeh with the given Queue.
func NewAlwaysQueueArrBeh(q *Queue, ap ArrProc) ArrBeh {
var ab *AlwaysQueueArrBeh
ab = new(AlwaysQueueArrBeh)
ab.Q = q
// Make sure that newly arriving Jobs get assigned.
ap.AfterArrive(func(cbArrProc ArrProc, cbJobs []*Job, cbInterval int) {
for _, j := range cbJobs {
ab.Assign(j)
}
})
return ab
}
// An Assignment indicates where a Job has been assigned by an Arrival Behavior.
//
// The string Type will be either "Processor" or "Queue", and the corresponding
// attribute (either Processor or Queue) will contain the entity to which the
// Job was assigned. The other attribute will be nil.
type Assignment struct {
Type string
Processor *Processor
Queue *Queue
}
// ByQueueLength implements sort.Interface for []*Queue based on Length.
type ByQueueLength []*Queue
func (a ByQueueLength) Len() int { return len(a) }
func (a ByQueueLength) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByQueueLength) Less(i, j int) bool { return a[i].Length() < a[j].Length() }