@@ -22,10 +22,11 @@ import (
22
22
// testLoadSplitConfig implements the LoadSplitConfig interface and may be used
23
23
// in testing.
24
24
type testLoadSplitConfig struct {
25
- randSource RandSource
26
- useWeighted bool
27
- statRetention time.Duration
28
- statThreshold float64
25
+ randSource RandSource
26
+ useWeighted bool
27
+ statRetention time.Duration
28
+ statThreshold float64
29
+ sampleResetDuration time.Duration
29
30
}
30
31
31
32
// NewLoadBasedSplitter returns a new LoadBasedSplitter that may be used to
@@ -50,6 +51,12 @@ func (t *testLoadSplitConfig) StatThreshold(_ SplitObjective) float64 {
50
51
return t .statThreshold
51
52
}
52
53
54
+ // SampleResetDuration returns the duration that any sampling structure should
55
+ // retain data for before resetting.
56
+ func (t * testLoadSplitConfig ) SampleResetDuration () time.Duration {
57
+ return t .sampleResetDuration
58
+ }
59
+
53
60
func ld (n int ) func (SplitObjective ) int {
54
61
return func (_ SplitObjective ) int {
55
62
return n
@@ -448,3 +455,69 @@ func TestDeciderMetrics(t *testing.T) {
448
455
assert .Equal (t , dAllInsufficientCounters .loadSplitterMetrics .PopularKeyCount .Count (), int64 (0 ))
449
456
assert .Equal (t , dAllInsufficientCounters .loadSplitterMetrics .NoSplitKeyCount .Count (), int64 (0 ))
450
457
}
458
+
459
+ // TestDeciderSampleReset tests the sample reset functionality of the decider,
460
+ // when the sample reset duration is non-zero, the split finder should be reset
461
+ // after the given duration. When the sample reset duration is zero, the split
462
+ // finder should not be reset.
463
+ func TestDeciderSampleReset (t * testing.T ) {
464
+ defer leaktest .AfterTest (t )()
465
+
466
+ rng := rand .New (rand .NewSource (12 ))
467
+ loadSplitConfig := testLoadSplitConfig {
468
+ randSource : rng ,
469
+ useWeighted : false ,
470
+ statRetention : 2 * time .Second ,
471
+ statThreshold : 1 ,
472
+ sampleResetDuration : 10 * time .Second ,
473
+ }
474
+ ctx := context .Background ()
475
+ tick := 0
476
+
477
+ var d Decider
478
+ Init (& d , & loadSplitConfig , & LoadSplitterMetrics {
479
+ PopularKeyCount : metric .NewCounter (metric.Metadata {}),
480
+ NoSplitKeyCount : metric .NewCounter (metric.Metadata {}),
481
+ }, SplitQPS )
482
+
483
+ require .Nil (t , d .mu .splitFinder )
484
+ d .Record (ctx , ms (tick ), ld (100 ), func () roachpb.Span {
485
+ return roachpb.Span {Key : keys .SystemSQLCodec .TablePrefix (uint32 (0 ))}
486
+ })
487
+ // The split finder should be created as the second sample is recorded and
488
+ // the stat remains above the threshold (1) each tick.
489
+ for i := 0 ; i < 10 ; i ++ {
490
+ tick += 1000
491
+ d .Record (ctx , ms (tick ), ld (100 ), func () roachpb.Span {
492
+ return roachpb.Span {Key : keys .SystemSQLCodec .TablePrefix (uint32 (0 ))}
493
+ })
494
+ require .NotNil (t , d .mu .splitFinder , (* lockedDecider )(& d ))
495
+ }
496
+
497
+ // Tick one more time, now the sample reset duration (10s) has passed and the
498
+ // split finder should be reset.
499
+ tick += 1000
500
+ d .Record (ctx , ms (tick ), ld (100 ), func () roachpb.Span {
501
+ return roachpb.Span {Key : keys .SystemSQLCodec .TablePrefix (uint32 (0 ))}
502
+ })
503
+ require .Nil (t , d .mu .splitFinder , (* lockedDecider )(& d ))
504
+
505
+ // Immediately following the last tick where the splitFinder was reset, it
506
+ // should be recreated as the stat is still above the threshold.
507
+ for i := 0 ; i < 10 ; i ++ {
508
+ tick += 1000
509
+ d .Record (ctx , ms (tick ), ld (100 ), func () roachpb.Span {
510
+ return roachpb.Span {Key : keys .SystemSQLCodec .TablePrefix (uint32 (0 ))}
511
+ })
512
+ require .NotNil (t , d .mu .splitFinder , (* lockedDecider )(& d ))
513
+ }
514
+ // Set the sample reset duration to 0, which should cause the split finder to
515
+ // not be reset in the next tick, unlike before when the sample reset
516
+ // duration was 10s.
517
+ loadSplitConfig .sampleResetDuration = 0
518
+ tick += 1000
519
+ d .Record (ctx , ms (tick ), ld (100 ), func () roachpb.Span {
520
+ return roachpb.Span {Key : keys .SystemSQLCodec .TablePrefix (uint32 (0 ))}
521
+ })
522
+ require .NotNil (t , d .mu .splitFinder , (* lockedDecider )(& d ))
523
+ }
0 commit comments