7
7
"log/slog"
8
8
"time"
9
9
10
+ // "github.com/k0kubun/pp/v3"
10
11
"github.com/pkg/errors"
11
12
12
13
"github.com/streemtech/divider"
@@ -80,18 +81,6 @@ func (d *dividerWorker) StartWorker(ctx context.Context) {
80
81
D : d .conf .compareKeys ,
81
82
F : d .workerRectifyAssignedWorkFunc ,
82
83
}
83
- d .masterPing = ticker.TickerFunc {
84
- C : d .ctx ,
85
- Logger : logger ,
86
- D : d .conf .masterPing ,
87
- F : d .masterPingFunc ,
88
- }
89
- d .workerPing = ticker.TickerFunc {
90
- C : d .ctx ,
91
- Logger : logger ,
92
- D : d .conf .workerPing ,
93
- F : d .workerPingFunc ,
94
- }
95
84
96
85
InitMetrics (d .conf .metricsName )
97
86
@@ -103,7 +92,7 @@ func (d *dividerWorker) StartWorker(ctx context.Context) {
103
92
//add workers to work holder
104
93
err := d .storage .UpdateTimeoutForWorkers (ctx , d .getWorkerNodeKeys ())
105
94
if err != nil {
106
- d .conf .logger (ctx ).Panic ("failed to update timeout for workers" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
95
+ d .conf .logger (ctx ).Panic ("failed to update timeout for workers" , err , slog .String ("divider.id" , d .conf .instanceID ))
107
96
}
108
97
109
98
//start all listeners and tickers.
@@ -114,23 +103,13 @@ func (d *dividerWorker) StartWorker(ctx context.Context) {
114
103
115
104
err = d .masterUpdateRequiredWork .Do ()
116
105
if err != nil {
117
- d .conf .logger (ctx ).Panic ("failed to start ticker" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
106
+ d .conf .logger (ctx ).Panic ("failed to start ticker" , err , slog .String ("divider.id" , d .conf .instanceID ))
118
107
}
119
108
err = d .workerRectifyAssignedWork .Do ()
120
109
if err != nil {
121
- d .conf .logger (ctx ).Panic ("failed to start ticker" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
122
- }
123
- err = d .masterPing .Do ()
124
- if err != nil {
125
- d .conf .logger (ctx ).Panic ("failed to start ticker" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
126
- }
127
- err = d .workerPing .Do ()
128
- if err != nil {
129
- d .conf .logger (ctx ).Panic ("failed to start ticker" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
110
+ d .conf .logger (ctx ).Panic ("failed to start ticker" , err , slog .String ("divider.id" , d .conf .instanceID ))
130
111
}
131
112
132
- d .masterPing .F ()
133
- d .workerPing .F ()
134
113
d .masterUpdateRequiredWork .F ()
135
114
d .workerRectifyAssignedWork .F ()
136
115
@@ -309,52 +288,53 @@ func (d *dividerWorker) masterUpdateRequiredWorkFunc() {
309
288
}()
310
289
311
290
d .conf .logger (d .ctx ).Debug ("masterUpdateRequiredWorkFunc triggered" , slog .String ("divider.id" , d .conf .instanceID ))
312
- masterKey := fmt .Sprintf ("%s:%s" , d .conf .rootKey , "master" )
313
291
314
- //check the master key.
315
- master , err := d .client .Get (d .ctx , masterKey ).Result ()
316
- if err != nil {
317
- d .conf .logger (d .ctx ).Panic ("Error getting current master" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
318
- return
319
- }
320
292
//if not master, dont do the work fetcher.
321
- if master != d . conf . instanceID {
293
+ if ! d . materUpdate ( d . ctx ) {
322
294
return
323
295
}
324
296
325
297
//Get all the newWork that needs to be assigned.
326
298
newWork , err := d .conf .workFetcher (d .ctx )
327
299
if err != nil {
328
- d .conf .logger (d .ctx ).Panic ("failed to execute work fetcher" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
300
+ d .conf .logger (d .ctx ).Panic ("failed to execute work fetcher" , err , slog .String ("divider.id" , d .conf .instanceID ))
329
301
return
330
302
}
331
303
332
304
//Add the work to the list of work in the system.
333
305
err = d .storage .AddWorkToDividedWork (d .ctx , newWork )
334
306
if err != nil {
335
- d .conf .logger (d .ctx ).Panic ("failed to add work to divided work" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
307
+ d .conf .logger (d .ctx ).Panic ("failed to add work to divided work" , err , slog .String ("divider.id" , d .conf .instanceID ))
336
308
return
337
309
}
338
310
339
311
//get existing work
340
312
existingWork , err := d .storage .GetAllWork (d .ctx )
341
313
if err != nil {
342
- d .conf .logger (d .ctx ).Panic ("failed to get list of all work" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
314
+ d .conf .logger (d .ctx ).Panic ("failed to get list of all work" , err , slog .String ("divider.id" , d .conf .instanceID ))
343
315
return
344
316
}
345
317
346
318
//determine what work to remove
347
319
_ , remove := getToRemoveToKeep (existingWork , newWork )
348
320
321
+ // pp.Println(d.conf.metricsName, existingWork, newWork, remove)
349
322
//remove all that work
350
323
err = d .storage .RemoveWorkFromDividedWork (d .ctx , remove )
351
324
if err != nil {
352
- d .conf .logger (d .ctx ).Panic ("failed to remove the old work" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
325
+ d .conf .logger (d .ctx ).Panic ("failed to remove the old work" , err , slog .String ("divider.id" , d .conf .instanceID ))
353
326
return
354
327
}
355
328
329
+ for _ , v := range remove {
330
+ err = d .removeWork .Publish (d .ctx , v )
331
+ if err != nil {
332
+ d .conf .logger (d .ctx ).Panic ("failed to publish old work removal" , err , slog .String ("divider.id" , d .conf .instanceID ))
333
+ }
334
+ }
356
335
//note: Not triggering the add work event as that event should be triggered manually by the add work call, not by this.
357
336
//The work will be picked up by the rectify call later.
337
+ //Am manually calling the remove work as that should happen as soon as rectified if needed. (This can result in a double call, but thats ok in this case.)
358
338
}
359
339
360
340
// get work assigned to this node, compare with known work, and start/stop all work as needed.
@@ -369,56 +349,56 @@ func (d *dividerWorker) workerRectifyAssignedWorkFunc() {
369
349
if err != nil {
370
350
d .conf .logger (d .ctx ).Error ("failed to rectify work" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
371
351
}
352
+
353
+ d .workerPing (d .ctx )
372
354
}
373
355
374
356
// set master as still attached
375
- func (d * dividerWorker ) masterPingFunc () {
376
- start := time .Now ()
377
- defer func () {
378
- ObserveDuration (MasterPingTime , d .conf .metricsName , time .Since (start ))
379
- }()
357
+ func (d * dividerWorker ) materUpdate (ctx context.Context ) (isMaster bool ) {
380
358
381
359
// d.conf.logger(d.ctx).Debug("masterPingFunc triggered")
382
360
masterKey := fmt .Sprintf ("%s:%s" , d .conf .rootKey , "master" )
383
361
//set the master key to this value if it does not exist.
384
- set , err := d .client .SetNX (d . ctx , masterKey , d .conf .instanceID , d .conf .masterTimeout ).Result ()
362
+ set , err := d .client .SetNX (ctx , masterKey , d .conf .instanceID , d .conf .masterTimeout ).Result ()
385
363
if err != nil {
386
- d .conf .logger (d . ctx ).Panic ("Error updating node master" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
364
+ d .conf .logger (ctx ).Panic ("Error updating node master" , err , slog .String ("divider.id" , d .conf .instanceID ))
387
365
return
388
366
}
389
367
390
368
if set {
391
- d .conf .logger (d . ctx ).Info ("Master set to this node" , slog .String ("divider.id" , d .conf .instanceID ))
369
+ d .conf .logger (ctx ).Info ("Master set to this node" , slog .String ("divider.id" , d .conf .instanceID ))
392
370
}
393
371
394
372
//check the master key.
395
- master , err := d .client .Get (d . ctx , masterKey ).Result ()
373
+ master , err := d .client .Get (ctx , masterKey ).Result ()
396
374
if err != nil {
397
- d .conf .logger (d . ctx ).Panic ("Error getting current master" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
375
+ d .conf .logger (ctx ).Panic ("Error getting current master" , err , slog .String ("divider.id" , d .conf .instanceID ))
398
376
return
399
377
}
400
378
401
379
//if this is the master, run the update to keep the master inline.
402
380
if master == d .conf .instanceID {
403
- _ , err = d .client .Set (d . ctx , masterKey , d .conf .instanceID , d .conf .masterTimeout ).Result ()
381
+ _ , err = d .client .Set (ctx , masterKey , d .conf .instanceID , d .conf .masterTimeout ).Result ()
404
382
if err != nil {
405
- d .conf .logger (d . ctx ).Panic ("Error updating master timeout" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
383
+ d .conf .logger (ctx ).Panic ("Error updating master timeout" , err , slog .String ("divider.id" , d .conf .instanceID ))
406
384
return
407
385
}
386
+ return true
408
387
}
388
+ return false
409
389
}
410
390
411
391
// update nodes in storage as still attached.
412
- func (d * dividerWorker ) workerPingFunc ( ) {
392
+ func (d * dividerWorker ) workerPing ( ctx context. Context ) {
413
393
start := time .Now ()
414
394
defer func () {
415
395
ObserveDuration (WorkerPingTime , d .conf .metricsName , time .Since (start ))
416
396
}()
417
397
// d.conf.logger(d.ctx).Debug("workerPingFunc triggered")
418
398
//add workers to work holder
419
- err := d .storage .UpdateTimeoutForWorkers (d . ctx , d .getWorkerNodeKeys ())
399
+ err := d .storage .UpdateTimeoutForWorkers (ctx , d .getWorkerNodeKeys ())
420
400
if err != nil {
421
- d .conf .logger (d .ctx ).Panic ("failed to update timeout for workers" , slog . String ( " err.error" , err . Error ()) , slog .String ("divider.id" , d .conf .instanceID ))
401
+ d .conf .logger (d .ctx ).Panic ("failed to update timeout for workers" , err , slog .String ("divider.id" , d .conf .instanceID ))
422
402
}
423
403
}
424
404
@@ -474,19 +454,23 @@ func (d *dividerWorker) rectifyWork(ctx context.Context) (err error) {
474
454
475
455
// pp.Println(d.conf.instanceID, "toRemove", toRemove.Array())
476
456
for key := range toRemove .Iterator () {
477
- err = d .conf .stopper (ctx , key )
457
+ ctx2 , can := context .WithTimeout (ctx , time .Second * 5 )
458
+ defer can ()
459
+ err = d .conf .stopper (ctx2 , key )
478
460
if err != nil {
479
- d .conf .logger (ctx ).Error ("failed to execute stopper, not removing from known work" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
461
+ d .conf .logger (ctx2 ).Error ("failed to execute stopper, not removing from known work" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
480
462
continue
481
463
}
482
464
d .knownWork .Remove (key )
483
465
}
484
466
485
467
// pp.Println(d.conf.instanceID, "toAdd", toAdd.Array())
486
468
for key := range toAdd .Iterator () {
487
- err = d .conf .starter (ctx , key )
469
+ ctx2 , can := context .WithTimeout (ctx , time .Second * 5 )
470
+ defer can ()
471
+ err = d .conf .starter (ctx2 , key )
488
472
if err != nil {
489
- d .conf .logger (ctx ).Error ("failed to execute starter, not adding to known work" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
473
+ d .conf .logger (ctx2 ).Error ("failed to execute starter, not adding to known work" , slog .String ("err.error" , err .Error ()), slog .String ("divider.id" , d .conf .instanceID ))
490
474
continue
491
475
}
492
476
d .knownWork .Add (key )
0 commit comments