github.com/thanos-io/thanos@v0.32.5/pkg/queryfrontend/cache_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package queryfrontend 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/prometheus/prometheus/model/labels" 11 12 "github.com/thanos-io/thanos/internal/cortex/querier/queryrange" 13 14 "github.com/efficientgo/core/testutil" 15 ) 16 17 func TestGenerateCacheKey(t *testing.T) { 18 intervalFn := func(r queryrange.Request) time.Duration { return hour } 19 splitter := newThanosCacheKeyGenerator(intervalFn) 20 21 for _, tc := range []struct { 22 name string 23 req queryrange.Request 24 expected string 25 }{ 26 { 27 name: "non thanos req", 28 req: &queryrange.PrometheusRequest{ 29 Query: "up", 30 Start: 0, 31 Step: 60 * seconds, 32 }, 33 expected: "fe::up:60000:0", 34 }, 35 { 36 name: "non downsampling resolution specified", 37 req: &ThanosQueryRangeRequest{ 38 Query: "up", 39 Start: 0, 40 Step: 60 * seconds, 41 }, 42 expected: "fe::up:60000:0:2:-:0:", 43 }, 44 { 45 name: "10s step", 46 req: &ThanosQueryRangeRequest{ 47 Query: "up", 48 Start: 0, 49 Step: 10 * seconds, 50 }, 51 expected: "fe::up:10000:0:2:-:0:", 52 }, 53 { 54 name: "1m downsampling resolution", 55 req: &ThanosQueryRangeRequest{ 56 Query: "up", 57 Start: 0, 58 Step: 10 * seconds, 59 MaxSourceResolution: 60 * seconds, 60 }, 61 expected: "fe::up:10000:0:2:-:0:", 62 }, 63 { 64 name: "5m downsampling resolution, different cache key", 65 req: &ThanosQueryRangeRequest{ 66 Query: "up", 67 Start: 0, 68 Step: 10 * seconds, 69 MaxSourceResolution: 300 * seconds, 70 }, 71 expected: "fe::up:10000:0:1:-:0:", 72 }, 73 { 74 name: "1h downsampling resolution, different cache key", 75 req: &ThanosQueryRangeRequest{ 76 Query: "up", 77 Start: 0, 78 Step: 10 * seconds, 79 MaxSourceResolution: hour, 80 }, 81 expected: "fe::up:10000:0:0:-:0:", 82 }, 83 { 84 name: "1h downsampling resolution with lookback delta", 85 req: &ThanosQueryRangeRequest{ 86 Query: "up", 87 Start: 0, 88 Step: 10 * seconds, 89 MaxSourceResolution: hour, 90 LookbackDelta: 1000, 91 }, 92 expected: "fe::up:10000:0:0:-:1000:", 93 }, 94 { 95 name: "label names, no matcher", 96 req: &ThanosLabelsRequest{ 97 Start: 0, 98 }, 99 expected: "fe:::[]:0", 100 }, 101 { 102 name: "label names, single matcher", 103 req: &ThanosLabelsRequest{ 104 Start: 0, 105 Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}}, 106 }, 107 expected: `fe:::[[foo="bar"]]:0`, 108 }, 109 { 110 name: "label names, multiple matchers", 111 req: &ThanosLabelsRequest{ 112 Start: 0, 113 Matchers: [][]*labels.Matcher{ 114 {labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}, 115 {labels.MustNewMatcher(labels.MatchEqual, "baz", "qux")}, 116 }, 117 }, 118 expected: `fe:::[[foo="bar"] [baz="qux"]]:0`, 119 }, 120 { 121 name: "label values, no matcher", 122 req: &ThanosLabelsRequest{ 123 Start: 0, 124 Label: "up", 125 }, 126 expected: "fe::up:[]:0", 127 }, 128 { 129 name: "label values, single matcher", 130 req: &ThanosLabelsRequest{ 131 Start: 0, 132 Label: "up", 133 Matchers: [][]*labels.Matcher{{labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}}, 134 }, 135 expected: `fe::up:[[foo="bar"]]:0`, 136 }, 137 { 138 name: "label values, multiple matchers", 139 req: &ThanosLabelsRequest{ 140 Start: 0, 141 Label: "up", 142 Matchers: [][]*labels.Matcher{ 143 {labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")}, 144 {labels.MustNewMatcher(labels.MatchEqual, "baz", "qux")}, 145 }, 146 }, 147 expected: `fe::up:[[foo="bar"] [baz="qux"]]:0`, 148 }, 149 } { 150 t.Run(tc.name, func(t *testing.T) { 151 key := splitter.GenerateCacheKey("", tc.req) 152 testutil.Equals(t, tc.expected, key) 153 }) 154 } 155 }