@@ -386,52 +386,60 @@ func TestShouldCache(t *testing.T) {
386
386
}
387
387
388
388
func TestPartition (t * testing.T ) {
389
- for i , tc := range []struct {
390
- input Request
391
- prevCachedResponse []Extent
392
- expectedRequests []Request
393
- expectedCachedResponse []Response
389
+ for _ , tc := range []struct {
390
+ name string
391
+ input Request
392
+ prevCachedResponse []Extent
393
+ expectedUsedCachedEntry []Extent
394
+ expectedRequests []Request
395
+ expectedCachedResponse []Response
394
396
}{
395
- // 1. Test a complete hit.
396
397
{
398
+ name : "Test a complete hit." ,
397
399
input : & PrometheusRequest {
398
400
Start : 0 ,
399
401
End : 100 ,
400
402
},
401
403
prevCachedResponse : []Extent {
402
404
mkExtent (0 , 100 ),
403
405
},
406
+ expectedUsedCachedEntry : []Extent {
407
+ mkExtent (0 , 100 ),
408
+ },
404
409
expectedCachedResponse : []Response {
405
410
mkAPIResponse (0 , 100 , 10 ),
406
411
},
407
412
},
408
413
409
- // Test with a complete miss.
410
414
{
415
+ name : "Test with a complete miss." ,
411
416
input : & PrometheusRequest {
412
417
Start : 0 ,
413
418
End : 100 ,
414
419
},
415
420
prevCachedResponse : []Extent {
416
421
mkExtent (110 , 210 ),
417
422
},
423
+ expectedUsedCachedEntry : nil ,
418
424
expectedRequests : []Request {
419
425
& PrometheusRequest {
420
426
Start : 0 ,
421
427
End : 100 ,
422
428
}},
423
429
expectedCachedResponse : nil ,
424
430
},
425
-
426
- // Test a partial hit.
427
431
{
432
+ name : "Test a partial hit." ,
428
433
input : & PrometheusRequest {
429
434
Start : 0 ,
430
435
End : 100 ,
431
436
},
432
437
prevCachedResponse : []Extent {
433
438
mkExtent (50 , 100 ),
434
439
},
440
+ expectedUsedCachedEntry : []Extent {
441
+ mkExtent (50 , 100 ),
442
+ },
435
443
expectedRequests : []Request {
436
444
& PrometheusRequest {
437
445
Start : 0 ,
@@ -442,9 +450,8 @@ func TestPartition(t *testing.T) {
442
450
mkAPIResponse (50 , 100 , 10 ),
443
451
},
444
452
},
445
-
446
- // Test multiple partial hits.
447
453
{
454
+ name : "Test multiple partial hits." ,
448
455
input : & PrometheusRequest {
449
456
Start : 100 ,
450
457
End : 200 ,
@@ -453,6 +460,10 @@ func TestPartition(t *testing.T) {
453
460
mkExtent (50 , 120 ),
454
461
mkExtent (160 , 250 ),
455
462
},
463
+ expectedUsedCachedEntry : []Extent {
464
+ mkExtent (50 , 120 ),
465
+ mkExtent (160 , 250 ),
466
+ },
456
467
expectedRequests : []Request {
457
468
& PrometheusRequest {
458
469
Start : 120 ,
@@ -464,9 +475,8 @@ func TestPartition(t *testing.T) {
464
475
mkAPIResponse (160 , 200 , 10 ),
465
476
},
466
477
},
467
-
468
- // Partial hits with tiny gap.
469
478
{
479
+ name : "Partial hits with tiny gap." ,
470
480
input : & PrometheusRequest {
471
481
Start : 100 ,
472
482
End : 160 ,
@@ -475,6 +485,9 @@ func TestPartition(t *testing.T) {
475
485
mkExtent (50 , 120 ),
476
486
mkExtent (122 , 130 ),
477
487
},
488
+ expectedUsedCachedEntry : []Extent {
489
+ mkExtent (50 , 120 ),
490
+ },
478
491
expectedRequests : []Request {
479
492
& PrometheusRequest {
480
493
Start : 120 ,
@@ -485,24 +498,25 @@ func TestPartition(t *testing.T) {
485
498
mkAPIResponse (100 , 120 , 10 ),
486
499
},
487
500
},
488
- // Extent is outside the range and the request has a single step (same start and end).
489
501
{
502
+ name : "Extent is outside the range and the request has a single step (same start and end)." ,
490
503
input : & PrometheusRequest {
491
504
Start : 100 ,
492
505
End : 100 ,
493
506
},
494
507
prevCachedResponse : []Extent {
495
508
mkExtent (50 , 90 ),
496
509
},
510
+ expectedUsedCachedEntry : nil ,
497
511
expectedRequests : []Request {
498
512
& PrometheusRequest {
499
513
Start : 100 ,
500
514
End : 100 ,
501
515
},
502
516
},
503
517
},
504
- // Test when hit has a large step and only a single sample extent.
505
518
{
519
+ name : "Test when hit has a large step and only a single sample extent." ,
506
520
// If there is a only a single sample in the split interval, start and end will be the same.
507
521
input : & PrometheusRequest {
508
522
Start : 100 ,
@@ -511,24 +525,67 @@ func TestPartition(t *testing.T) {
511
525
prevCachedResponse : []Extent {
512
526
mkExtent (100 , 100 ),
513
527
},
528
+ expectedUsedCachedEntry : []Extent {
529
+ mkExtent (100 , 100 ),
530
+ },
514
531
expectedCachedResponse : []Response {
515
532
mkAPIResponse (100 , 105 , 10 ),
516
533
},
517
534
},
518
535
} {
519
- t .Run (strconv . Itoa ( i ) , func (t * testing.T ) {
536
+ t .Run (tc . name , func (t * testing.T ) {
520
537
s := resultsCache {
521
538
extractor : PrometheusResponseExtractor {},
522
539
minCacheExtent : 10 ,
523
540
}
524
- reqs , resps , err := s .partition (tc .input , tc .prevCachedResponse )
541
+ reqs , resps , usedCachedEntry , err := s .partition (tc .input , tc .prevCachedResponse )
525
542
require .Nil (t , err )
526
543
require .Equal (t , tc .expectedRequests , reqs )
527
544
require .Equal (t , tc .expectedCachedResponse , resps )
545
+ require .Equal (t , tc .expectedUsedCachedEntry , usedCachedEntry )
528
546
})
529
547
}
530
548
}
531
549
550
+ func TestCacheHitShouldNotDuplicateSamplesForTinyRequest (t * testing.T ) {
551
+ s := resultsCache {
552
+ extractor : PrometheusResponseExtractor {},
553
+ minCacheExtent : 10 ,
554
+ limits : mockLimits {},
555
+ merger : PrometheusCodec ,
556
+ next : HandlerFunc (func (_ context.Context , req Request ) (Response , error ) {
557
+ return parsedResponse , nil
558
+ }),
559
+ }
560
+
561
+ request := & PrometheusRequest {
562
+ Start : 0 ,
563
+ End : 5 ,
564
+ }
565
+
566
+ ctx := user .InjectOrgID (context .Background (), "1" )
567
+
568
+ parsedResponseAsAny , err := types .MarshalAny (parsedResponse )
569
+ require .NoError (t , err )
570
+
571
+ // current cached extents is smaller than minCacheExtent, so it should not be used and should
572
+ // be dropped from the cache.
573
+ currentlyCachedExtents := []Extent {{
574
+ Start : request .Start ,
575
+ End : request .End + 1 ,
576
+ Response : parsedResponseAsAny ,
577
+ }}
578
+ _ , updatedExtents , err := s .handleHit (ctx , request , currentlyCachedExtents , 0 )
579
+ require .NoError (t , err )
580
+
581
+ expectedExtents := []Extent {{
582
+ Start : request .Start ,
583
+ End : request .End ,
584
+ Response : parsedResponseAsAny ,
585
+ }}
586
+ require .Equal (t , expectedExtents , updatedExtents )
587
+ }
588
+
532
589
func TestResultsCache (t * testing.T ) {
533
590
calls := 0
534
591
cfg := ResultsCacheConfig {
0 commit comments