/
query_runner.go
287 lines (246 loc) · 7.81 KB
/
query_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2019 grafana
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bench
import (
"context"
"flag"
"math/rand"
"net/http"
"sync"
"time"
"github.com/cortexproject/cortex/pkg/util/spanlogger"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
config_util "github.com/prometheus/common/config"
"github.com/thanos-io/thanos/pkg/discovery/dns"
"github.com/thanos-io/thanos/pkg/extprom"
)
type QueryConfig struct {
Enabled bool `yaml:"enabled"`
Endpoint string `yaml:"endpoint"`
BasicAuthUsername string `yaml:"basic_auth_username"`
BasicAuthPasword string `yaml:"basic_auth_password"`
}
func (cfg *QueryConfig) RegisterFlags(f *flag.FlagSet) {
f.BoolVar(&cfg.Enabled, "bench.query.enabled", false, "enable query benchmarking")
f.StringVar(&cfg.Endpoint, "bench.query.endpoint", "", "Remote query endpoint.")
f.StringVar(&cfg.BasicAuthUsername, "bench.query.basic-auth-username", "", "Set the basic auth username on remote query requests.")
f.StringVar(&cfg.BasicAuthPasword, "bench.query.basic-auth-password", "", "Set the basic auth password on remote query requests.")
}
type QueryRunner struct {
id string
tenantName string
cfg QueryConfig
// Do DNS client side load balancing if configured
dnsProvider *dns.Provider
addressMtx sync.Mutex
addresses []string
clientPool map[string]v1.API
workload *QueryWorkload
reg prometheus.Registerer
logger log.Logger
requestDuration *prometheus.HistogramVec
}
func NewQueryRunner(id string, tenantName string, cfg QueryConfig, workload *QueryWorkload, logger log.Logger, reg prometheus.Registerer) (*QueryRunner, error) {
runner := &QueryRunner{
id: id,
tenantName: tenantName,
cfg: cfg,
workload: workload,
clientPool: map[string]v1.API{},
dnsProvider: dns.NewProvider(
logger,
extprom.WrapRegistererWithPrefix("benchtool_query_", reg),
dns.GolangResolverType,
),
logger: logger,
reg: reg,
requestDuration: promauto.With(reg).NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "benchtool",
Name: "query_request_duration_seconds",
Buckets: []float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 200},
},
[]string{"code", "type"},
),
}
return runner, nil
}
// JitterUp adds random jitter to the duration.
//
// This adds or subtracts time from the duration within a given jitter fraction.
// For example for 10s and jitter 0.1, it will return a time within [9s, 11s])
//
// Reference: https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils
func JitterUp(duration time.Duration, jitter float64) time.Duration {
multiplier := jitter * (rand.Float64()*2 - 1)
return time.Duration(float64(duration) * (1 + multiplier))
}
func (q *QueryRunner) Run(ctx context.Context) error {
go q.ResolveAddrsLoop(ctx)
queryChan := make(chan Query, 1000)
for i := 0; i < 100; i++ {
go q.QueryWorker(queryChan)
}
for _, queryReq := range q.workload.Queries {
// every query has a ticker and a Go loop...
// not sure if this is a good idea but it should be fine
go func(req Query) {
// issue the initial query with a jitter
firstWait := JitterUp(req.Interval, 0.4)
time.Sleep(firstWait)
queryChan <- req
ticker := time.NewTicker(req.Interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
queryChan <- req
case <-ctx.Done():
return
}
}
}(queryReq)
}
for {
<-ctx.Done()
close(queryChan)
return nil
}
}
func (q *QueryRunner) QueryWorker(queryChan chan Query) {
for queryReq := range queryChan {
err := q.ExecuteQuery(context.Background(), queryReq)
if err != nil {
level.Warn(q.logger).Log("msg", "unable to execute query", "err", err)
}
}
}
type TenantIDRoundTripper struct {
TenantName string
Next http.RoundTripper
}
func (r *TenantIDRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if r.TenantName != "" {
req.Header.Set("X-Scope-OrgID", r.TenantName)
}
return r.Next.RoundTrip(req)
}
func NewQueryClient(url, tenantName, username, password string) (v1.API, error) {
apiClient, err := api.NewClient(api.Config{
Address: url,
RoundTripper: &TenantIDRoundTripper{
TenantName: tenantName,
Next: config_util.NewBasicAuthRoundTripper(username, config_util.Secret(password), "", api.DefaultRoundTripper),
},
})
if err != nil {
return nil, err
}
return v1.NewAPI(apiClient), nil
}
func (q *QueryRunner) GetRandomAPIClient() (v1.API, error) {
q.addressMtx.Lock()
defer q.addressMtx.Unlock()
if len(q.addresses) == 0 {
return nil, errors.New("no addresses found")
}
randomIndex := rand.Intn(len(q.addresses))
pick := q.addresses[randomIndex]
var cli v1.API
var exists bool
var err error
if cli, exists = q.clientPool[pick]; !exists {
cli, err = NewQueryClient("http://"+pick+"/prometheus", q.tenantName, q.cfg.BasicAuthUsername, q.cfg.BasicAuthPasword)
if err != nil {
return nil, err
}
q.clientPool[pick] = cli
}
return cli, nil
}
func (q *QueryRunner) ExecuteQuery(ctx context.Context, queryReq Query) error {
spanLog, ctx := spanlogger.New(ctx, "queryRunner.executeQuery")
defer spanLog.Span.Finish()
apiClient, err := q.GetRandomAPIClient()
if err != nil {
return err
}
// Create a timestamp for use when creating the requests and observing latency
now := time.Now()
var (
queryType = "instant"
status = "success"
)
if queryReq.TimeRange > 0 {
queryType = "range"
level.Debug(q.logger).Log("msg", "sending range query", "expr", queryReq.Expr, "range", queryReq.TimeRange)
r := v1.Range{
Start: now.Add(-queryReq.TimeRange),
End: now,
Step: time.Minute,
}
_, _, err = apiClient.QueryRange(ctx, queryReq.Expr, r)
} else {
level.Debug(q.logger).Log("msg", "sending instant query", "expr", queryReq.Expr)
_, _, err = apiClient.Query(ctx, queryReq.Expr, now)
}
if err != nil {
status = "failure"
}
q.requestDuration.WithLabelValues(status, queryType).Observe(time.Since(now).Seconds())
return err
}
func (q *QueryRunner) ResolveAddrsLoop(ctx context.Context) {
err := q.ResolveAddrs()
if err != nil {
level.Warn(q.logger).Log("msg", "failed update remote write servers list", "err", err)
}
ticker := time.NewTicker(time.Minute * 5)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := q.ResolveAddrs()
if err != nil {
level.Warn(q.logger).Log("msg", "failed update remote write servers list", "err", err)
}
case <-ctx.Done():
return
}
}
}
func (q *QueryRunner) ResolveAddrs() error {
// Resolve configured addresses with a reasonable timeout.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// If some of the dns resolution fails, log the error.
if err := q.dnsProvider.Resolve(ctx, []string{q.cfg.Endpoint}); err != nil {
level.Error(q.logger).Log("msg", "failed to resolve addresses", "err", err)
}
// Fail in case no server address is resolved.
servers := q.dnsProvider.Addresses()
if len(servers) == 0 {
return errors.New("no server address resolved")
}
q.addressMtx.Lock()
q.addresses = servers
q.addressMtx.Unlock()
return nil
}