@@ -425,45 +425,81 @@ func TestQueryshardingCorrectness(t *testing.T) {
425425
426426func TestShardSplitting (t * testing.T ) {
427427
428- req := & PrometheusRequest {
429- Path : "/query_range" ,
430- Start : util .TimeToMillis (start ),
431- End : util .TimeToMillis (end ),
432- Step : int64 (step ) / int64 (time .Second ),
433- Query : "sum(rate(bar1[1m]))" ,
434- }
435-
436- shardingware := NewQueryShardMiddleware (
437- log .NewNopLogger (),
438- engine ,
439- // ensure that all requests are shard compatbile
440- ShardingConfigs {
441- chunk.PeriodConfig {
442- Schema : "v10" ,
443- RowShards : uint32 (2 ),
444- },
428+ for _ , tc := range []struct {
429+ desc string
430+ lookback time.Duration
431+ shouldShard bool
432+ }{
433+ {
434+ desc : "older than lookback" ,
435+ lookback : - 1 , // a negative lookback will ensure the entire query doesn't cross the sharding boundary & can safely be sharded.
436+ shouldShard : true ,
445437 },
446- PrometheusCodec ,
447- end .Sub (start )/ 2 , // shard 1/2 of the req
448- nil ,
449- nil ,
450- )
438+ {
439+ desc : "overlaps lookback" ,
440+ lookback : end .Sub (start ) / 2 , // intersect the request causing it to avoid sharding
441+ shouldShard : false ,
442+ },
443+ {
444+ desc : "newer than lookback" ,
445+ lookback : end .Sub (start ) + 1 ,
446+ shouldShard : false ,
447+ },
448+ } {
449+ t .Run (tc .desc , func (t * testing.T ) {
450+ req := & PrometheusRequest {
451+ Path : "/query_range" ,
452+ Start : util .TimeToMillis (start ),
453+ End : util .TimeToMillis (end ),
454+ Step : int64 (step ) / int64 (time .Second ),
455+ Query : "sum(rate(bar1[1m]))" ,
456+ }
451457
452- downstream := & downstreamHandler {
453- engine : engine ,
454- queryable : shardAwareQueryable ,
455- }
458+ shardingware := NewQueryShardMiddleware (
459+ log .NewNopLogger (),
460+ engine ,
461+ // ensure that all requests are shard compatbile
462+ ShardingConfigs {
463+ chunk.PeriodConfig {
464+ Schema : "v10" ,
465+ RowShards : uint32 (2 ),
466+ },
467+ },
468+ PrometheusCodec ,
469+ tc .lookback ,
470+ nil ,
471+ nil ,
472+ )
456473
457- handler := shardingware .Wrap (downstream ).(* shardSplitter )
458- handler .now = func () time.Time { return end } // make the split cut the request in half (don't use time.Now)
474+ downstream := & downstreamHandler {
475+ engine : engine ,
476+ queryable : shardAwareQueryable ,
477+ }
459478
460- resp , err := handler . Do ( context . Background (), req )
461- require . Nil ( t , err )
479+ handler := shardingware . Wrap ( downstream ).( * shardSplitter )
480+ handler . now = func () time. Time { return end } // make the split cut the request in half (don't use time.Now )
462481
463- unaltered , err := downstream .Do (context .Background (), req )
464- require .Nil (t , err )
482+ var didShard bool
483+
484+ old := handler .shardingware
485+ handler .shardingware = HandlerFunc (func (ctx context.Context , req Request ) (Response , error ) {
486+ didShard = true
487+ return old .Do (ctx , req )
488+ })
489+
490+ resp , err := handler .Do (context .Background (), req )
491+ require .Nil (t , err )
492+
493+ require .Equal (t , tc .shouldShard , didShard )
494+
495+ unaltered , err := downstream .Do (context .Background (), req )
496+ require .Nil (t , err )
497+
498+ approximatelyEquals (t , unaltered .(* PrometheusResponse ), resp .(* PrometheusResponse ))
499+
500+ })
501+ }
465502
466- approximatelyEquals (t , unaltered .(* PrometheusResponse ), resp .(* PrometheusResponse ))
467503}
468504
469505func BenchmarkQuerySharding (b * testing.B ) {
0 commit comments