/
worker.go
424 lines (391 loc) · 11.3 KB
/
worker.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
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
package worker
import (
"container/heap"
"context"
"log"
"math"
"sync"
"github.com/badochov/distributed-shortest-path/src/libs/db"
"github.com/badochov/distributed-shortest-path/src/services/worker/api"
"github.com/badochov/distributed-shortest-path/src/services/worker/discoverer"
"github.com/badochov/distributed-shortest-path/src/services/worker/link"
"github.com/badochov/distributed-shortest-path/src/services/worker/link/link_server"
"github.com/badochov/distributed-shortest-path/src/services/worker/service"
"golang.org/x/sync/errgroup"
)
type Deps struct {
Db db.DB
Discoverer discoverer.Discoverer
RegionID db.RegionId
Context context.Context
LinkPort int
}
// Worker All methods from link service and worker service should end up calling this interface.
type Worker interface {
service.Worker
link_server.Worker
LoadRegionData(ctx context.Context) error
}
type workerData struct {
vertices []db.VertexId
edges map[db.VertexId][]db.Edge
knownVertices map[db.VertexId]bool
arcFlags []db.ArcFlag
neighbouringVerticesRegions map[db.EdgeId]db.RegionId
}
type executionData struct {
queue PriorityQueue
queueItem map[db.VertexId]*Item
processed map[db.VertexId]bool
through map[db.VertexId]db.VertexId
}
type worker struct {
db db.DB
discoverer discoverer.Discoverer
generation db.Generation
regionId db.RegionId
data workerData
links map[db.RegionId]link.RegionManager
linkPort int
executions map[api.RequestId]*executionData
executionsLock sync.RWMutex
}
func (w *worker) Finish(ctx context.Context, requestId api.RequestId) error {
w.executionsLock.Lock()
defer w.executionsLock.Unlock()
// TODO delete doesn't really free resources in go. Once upon a while we need to copy the map to free.
ex := w.executions[requestId]
*ex = executionData{}
delete(w.executions, requestId)
return nil
}
func (w *worker) CalculateArcFlags(ctx context.Context) error {
//TODO implement me
return nil
}
func (w *worker) Init(ctx context.Context, requestId api.RequestId) error {
w.executionsLock.Lock()
defer w.executionsLock.Unlock()
w.executions[requestId] = &executionData{
queueItem: make(map[db.VertexId]*Item),
processed: make(map[db.VertexId]bool),
through: make(map[db.VertexId]db.VertexId),
}
return nil
}
func (w *worker) Step(ctx context.Context, vertexId db.VertexId, distance float64, through db.VertexId, requestId api.RequestId) (db.VertexId, float64, db.VertexId, error) {
log.Println("[STEP] Vertex", vertexId, "distance", distance, "through", through, "Request", requestId)
w.executionsLock.RLock()
ex := w.executions[requestId]
w.executionsLock.RUnlock()
if w.data.knownVertices[vertexId] {
ex.through[vertexId] = through
ex.processed[vertexId] = true
}
for _, edge := range w.data.edges[vertexId] {
if !ex.processed[edge.To] {
d := distance + edge.Length
item, inQueue := ex.queueItem[edge.To]
if !inQueue {
ex.through[edge.To] = vertexId
newItem := &Item{id: edge.To, distance: d}
ex.queueItem[edge.To] = newItem
heap.Push(&ex.queue, newItem)
} else if d < item.distance {
ex.through[edge.To] = vertexId
ex.queue.update(item, d)
}
}
}
for ex.queue.Len() > 0 {
item := ex.queue[0] // Peak
if !ex.processed[item.id] {
return item.id, item.distance, ex.through[item.id], nil
}
_ = heap.Pop(&ex.queue)
}
return db.VertexId(-1), math.Inf(1), db.VertexId(-1), nil
}
func (w *worker) Reconstruct(ctx context.Context, vertexId db.VertexId, requestId api.RequestId) ([]db.VertexId, error) {
log.Println("[RECONSTRUCT] Vertex", vertexId, "Request", requestId)
w.executionsLock.RLock()
ex := w.executions[requestId]
w.executionsLock.RUnlock()
var path []db.VertexId
vertexId, inRegion := ex.through[vertexId]
for inRegion {
path = append(path, vertexId)
vertexId, inRegion = ex.through[vertexId]
}
return path, nil
}
//// shortestPathFast is more efficient version of ShortestPath. Albeit much more complicated probably completely not worth it.
//func (w *worker) shortestPathFast(ctx context.Context, args service.ShortestPathArgs) (service.ShortestPathResult, error) {
// type newGenData struct {
// distance float64
// vertexId db.VertexId
// }
// type res struct {
// distance float64
// vertexId db.VertexId
// err error
// }
// executionLinks := make([]link.Link, len(w.links))
// for i, regionManager := range w.links {
// _, l, err := regionManager.GetLink()
// if err != nil {
// return service.ShortestPathResult{}, err
// }
// if err := l.Init(ctx, args.RequestId); err != nil {
// return service.ShortestPathResult{}, err
// }
// executionLinks[i] = l
// }
//
// newGenChans := make([]chan newGenData, len(executionLinks))
// resChan := make(chan res)
// ctx, cancel := context.WithCancel(ctx)
// for i, l := range executionLinks {
// newGenChan := make(chan newGenData)
// newGenChans[i] = newGenChan
// go func(l link.Link) {
// for data := range newGenChan {
// linkVertexId, linkDistance, err := l.Step(ctx, data.vertexId, data.distance, args.RequestId)
// if err != nil {
// resChan <- res{
// err: err,
// }
// return
// }
// resChan <- res{
// distance: linkDistance,
// vertexId: linkVertexId,
// }
// }
// }(l)
// }
// // Close new gen chans to stop goroutines.
// defer func() {
// for _, ch := range newGenChans {
// close(ch)
// }
// }()
//
// iters := 0 // For debug purposes.
// data := newGenData{
// distance: 0,
// vertexId: args.From,
// }
// for data.vertexId != args.To {
// // Start Step requests.
// for _, ch := range newGenChans {
// ch <- data
// }
// data.distance = math.Inf(1)
//
// // Get data from links.
// var err error
// for range newGenChans {
// r := <-resChan
// if r.err != nil {
// if err == nil {
// // If it was first error cancel the context.
// cancel()
// }
// err = multierror.Append(err, r.err)
// } else if r.distance < data.distance { // Update next vertex
// data = newGenData{
// distance: r.distance,
// vertexId: r.vertexId,
// }
// }
// }
// // Check error.
// if err != nil {
// // Context was canceled in err handler above.
// return service.ShortestPathResult{}, err
// }
//
// iters++
// if math.IsInf(data.distance, 1) {
// log.Println("ITERS", iters)
// cancel()
// return service.ShortestPathResult{NoPath: true}, nil
// }
// }
// cancel()
// return service.ShortestPathResult{Distance: data.distance}, nil
//}
func (w *worker) ShortestPath(ctx context.Context, args service.ShortestPathArgs) (service.ShortestPathResult, error) {
executionLinks := make([]link.Link, len(w.links))
for i, regionManager := range w.links {
_, l, err := regionManager.GetLink()
if err != nil {
return service.ShortestPathResult{}, err
}
if err := l.Init(ctx, args.RequestId); err != nil {
return service.ShortestPathResult{}, err
}
executionLinks[i] = l
}
vertexId, distance, through := args.From, float64(0), db.VertexId(-1)
iters := 0
var mutex sync.Mutex
for vertexId != args.To {
newVertexId, newDistance, newThrough := db.VertexId(-1), math.Inf(1), db.VertexId(-1)
errGrp, ctx := errgroup.WithContext(ctx)
for _, l := range executionLinks {
l := l
errGrp.Go(func() error {
linkVertexId, linkDistance, linkThrough, err := l.Step(ctx, vertexId, distance, through, args.RequestId)
if err != nil {
return err
}
mutex.Lock() // More go-like would be to send the result via channel and aggregate them in main thread.
defer mutex.Unlock()
if linkDistance < newDistance {
newVertexId, newDistance, newThrough = linkVertexId, linkDistance, linkThrough
}
return nil
})
}
if err := errGrp.Wait(); err != nil {
return service.ShortestPathResult{}, err
}
vertexId, distance, through = newVertexId, newDistance, newThrough
iters++
if math.IsInf(distance, 1) {
log.Println("ITERS", iters)
return service.ShortestPathResult{NoPath: true}, nil
}
}
// Reconstruct the path.
path := []db.VertexId{vertexId}
for vertexId != -1 {
errGrp, ctx := errgroup.WithContext(ctx)
for _, l := range executionLinks {
l := l
errGrp.Go(func() error {
linkPath, err := l.Reconstruct(ctx, vertexId, args.RequestId)
if err != nil {
return err
}
if len(linkPath) > 0 {
mutex.Lock()
path = append(path, linkPath...)
mutex.Unlock()
}
return nil
})
}
if err := errGrp.Wait(); err != nil {
return service.ShortestPathResult{}, err
}
vertexId = path[len(path)-1]
}
path = path[:len(path)-1] // Last element is -1.
// Path is in reverse order.
for i := 0; i < len(path)/2; i++ {
path[i], path[len(path)-1] = path[len(path)-1], path[i]
}
// Free resources.
errGrp, ctx := errgroup.WithContext(ctx)
for _, l := range executionLinks {
l := l
errGrp.Go(func() error {
return l.Finish(ctx, args.RequestId)
})
}
if err := errGrp.Wait(); err != nil {
return service.ShortestPathResult{}, err
}
return service.ShortestPathResult{Distance: distance, Vertices: path}, nil
}
func (w *worker) LoadRegionData(ctx context.Context) (err error) {
w.generation, err = w.db.GetActiveGeneration(ctx)
if err != nil {
return
}
w.data.vertices, err = w.db.GetVertexIds(ctx, w.regionId, w.generation)
if err != nil {
return
}
w.data.edges, err = w.db.GetEdgesFrom(ctx, w.data.vertices, w.generation)
if err != nil {
return
}
w.data.knownVertices = make(map[int64]bool)
for _, edges := range w.data.edges {
for _, e := range edges {
w.data.knownVertices[e.To] = true
w.data.knownVertices[e.From] = true
}
}
//eIds := make([]db.EdgeId, 0, len(w.data.edges))for _, edges := range w.data.edges {
// for _, e := range edges {
// eIds = append(eIds, e.Id)
//
// eIds = append(eIds, e.Id)
// }
//}
// w.data.arcFlags, err = w.db.GetArcFlags(ctx, eIds, w.generation)
// if err != nil {
// return
// }
w.data.neighbouringVerticesRegions, err = w.db.GetEdgeToRegionMapping(ctx, w.regionId, w.generation)
if err != nil {
return
}
return
}
func (w *worker) initDiscoverer(ctx context.Context) error {
if err := w.discoverer.Run(ctx); err != nil {
return err
}
go func() {
for {
select {
// some worker changed its status, e.g. failed (or went up)
case status := <-w.discoverer.InstanceStatuses():
w.handleInstanceStatus(status)
// set of workers in region has changed
case data := <-w.discoverer.RegionDataChan():
w.handleRegionData(ctx, data)
case <-ctx.Done():
return
}
}
}()
return nil
}
func (w *worker) handleInstanceStatus(status discoverer.WorkerInstanceStatus) {
// TODO [wprzytula]
}
func (w *worker) handleRegionData(ctx context.Context, data discoverer.RegionData) {
log.Println("Handling region data change", data)
l, ok := w.links[data.RegionId]
if !ok {
l = link.NewRegionDialer(w.linkPort)
}
err := l.UpdateInstances(ctx, data.Instances)
if err != nil {
log.Println(err)
}
if !ok {
w.links[data.RegionId] = l
}
}
func New(deps Deps) (Worker, error) {
w := &worker{
db: deps.Db,
discoverer: deps.Discoverer,
regionId: deps.RegionID,
linkPort: deps.LinkPort,
links: make(map[db.RegionId]link.RegionManager),
executions: make(map[api.RequestId]*executionData),
}
if err := w.initDiscoverer(deps.Context); err != nil {
return nil, err
}
return w, nil
}