@@ -392,10 +392,17 @@ NAMES:
392
392
return defs
393
393
}
394
394
395
- func (c * CasIdx ) ArchiveDefs (defs []schema.MetricDefinition ) error {
395
+ // ArchiveDefs writes each of the provided defs to the archive table and
396
+ // then deletes the defs from the metric_idx table.
397
+ func (c * CasIdx ) ArchiveDefs (defs []schema.MetricDefinition ) (int , error ) {
396
398
defChan := make (chan * schema.MetricDefinition , c .cfg .numConns )
397
399
g , ctx := errgroup .WithContext (context .Background ())
400
+
401
+ // keep track of how many defs were successfully archived.
402
+ success := make ([]int , c .cfg .numConns )
403
+
398
404
for i := 0 ; i < c .cfg .numConns ; i ++ {
405
+ i := i
399
406
g .Go (func () error {
400
407
for {
401
408
select {
@@ -405,13 +412,25 @@ func (c *CasIdx) ArchiveDefs(defs []schema.MetricDefinition) error {
405
412
}
406
413
err := c .addDefToArchive (* def )
407
414
if err != nil {
408
- return err
415
+ // If we failed to add the def to the archive table then just continue on to the next def.
416
+ // As we havnet yet removed the this def from the metric_idx table yet, the next time archiving
417
+ // is performed the this def will be processed again. As no action is needed by an operator, we
418
+ // just log this as a warning.
419
+ log .Warnf ("cassandra-idx: Failed add def to archive table. error=%s. def=%+v" , err , * def )
420
+ continue
409
421
}
410
422
411
423
err = c .deleteDef (def .Id , def .Partition )
412
424
if err != nil {
413
- return err
425
+ // The next time archiving is performed this def will be processed again. Re-adding the def to the archive
426
+ // table will just be treated like an update with only the archived_at field changing. As no action is needed
427
+ // by an operator, we just log this as a warning.
428
+ log .Warnf ("cassandra-idx: Failed to remove archived def from metric_idx table. error=%s. def=%+v" , err , * def )
429
+ continue
414
430
}
431
+
432
+ // increment counter of defs successfully archived
433
+ success [i ] = success [i ] + 1
415
434
case <- ctx .Done ():
416
435
return ctx .Err ()
417
436
}
@@ -422,11 +441,17 @@ func (c *CasIdx) ArchiveDefs(defs []schema.MetricDefinition) error {
422
441
defChan <- & defs [i ]
423
442
}
424
443
close (defChan )
425
- if err := g .Wait (); err != nil {
426
- return err
444
+
445
+ // wait for all goroutines to complete.
446
+ err := g .Wait ()
447
+
448
+ // get the count of defs successfully archived.
449
+ total := 0
450
+ for _ , count := range success {
451
+ total = total + count
427
452
}
428
453
429
- return nil
454
+ return total , err
430
455
}
431
456
432
457
func (c * CasIdx ) processWriteQueue () {
@@ -513,9 +538,9 @@ func (c *CasIdx) addDefToArchive(def schema.MetricDefinition) error {
513
538
return nil
514
539
}
515
540
516
- // log first failure and every 20th after that .
517
- if ( attempts % 20 ) == 0 {
518
- log .Warnf ("cassandra-idx: Failed to write def to cassandra. it will be retried. %s. the value was: %+v" , err , def )
541
+ // log first failure as a warning. If we reach max attempts, the error will bubble up to the caller .
542
+ if attempts == 0 {
543
+ log .Warnf ("cassandra-idx: Failed to write def to cassandra. it will be retried. error= %s. def= %+v" , err , def )
519
544
}
520
545
}
521
546
0 commit comments