forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gateway_client.go
435 lines (374 loc) · 14.2 KB
/
gateway_client.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package gatewayclient
import (
"context"
"flag"
"fmt"
"io"
"math/rand"
"sync"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/grpcclient"
"github.com/grafana/dskit/ring"
ring_client "github.com/grafana/dskit/ring/client"
"github.com/grafana/dskit/tenant"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/instrument"
"google.golang.org/grpc"
"github.com/grafana/loki/pkg/distributor/clientpool"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/storage/stores/series/index"
"github.com/grafana/loki/pkg/storage/stores/shipper/indexgateway"
shipper_util "github.com/grafana/loki/pkg/storage/stores/shipper/util"
"github.com/grafana/loki/pkg/util"
util_log "github.com/grafana/loki/pkg/util/log"
util_math "github.com/grafana/loki/pkg/util/math"
)
const (
maxQueriesPerGrpc = 100
maxConcurrentGrpcCalls = 10
)
// IndexGatewayClientConfig configures the Index Gateway client used to
// communicate with the Index Gateway server.
type IndexGatewayClientConfig struct {
// Mode sets in which mode the client will operate. It is actually defined at the
// index_gateway YAML section and reused here.
Mode indexgateway.Mode `yaml:"-"`
// PoolConfig defines the behavior of the gRPC connection pool used to communicate
// with the Index Gateway.
//
// Only relevant for the ring mode.
// It is defined at the distributors YAML section and reused here.
PoolConfig clientpool.PoolConfig `yaml:"-"`
// Ring is the Index Gateway ring used to find the appropriate Index Gateway instance
// this client should talk to.
//
// Only relevant for the ring mode.
Ring ring.ReadRing `yaml:"-"`
// GRPCClientConfig configures the gRPC connection between the Index Gateway client and the server.
//
// Used by both, ring and simple mode.
GRPCClientConfig grpcclient.Config `yaml:"grpc_client_config"`
// Address of the Index Gateway instance responsible for retaining the index for all tenants.
//
// Only relevant for the simple mode.
Address string `yaml:"server_address,omitempty"`
// Forcefully disable the use of the index gateway client for the storage.
// This is mainly useful for the index-gateway component which should always use the storage.
Disabled bool `yaml:"-"`
// LogGatewayRequests configures if requests sent to the gateway should be logged or not.
// The log messages are of type debug and contain the address of the gateway and the relevant tenant.
LogGatewayRequests bool `yaml:"log_gateway_requests"`
}
// RegisterFlagsWithPrefix register client-specific flags with the given prefix.
//
// Flags that are used by both, client and server, are defined in the indexgateway package.
func (i *IndexGatewayClientConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
i.GRPCClientConfig.RegisterFlagsWithPrefix(prefix+".grpc", f)
f.StringVar(&i.Address, prefix+".server-address", "", "Hostname or IP of the Index Gateway gRPC server running in simple mode.")
f.BoolVar(&i.LogGatewayRequests, prefix+".log-gateway-requests", false, "Whether requests sent to the gateway should be logged or not.")
}
func (i *IndexGatewayClientConfig) RegisterFlags(f *flag.FlagSet) {
i.RegisterFlagsWithPrefix("index-gateway-client", f)
}
type GatewayClient struct {
cfg IndexGatewayClientConfig
storeGatewayClientRequestDuration *prometheus.HistogramVec
conn *grpc.ClientConn
grpcClient logproto.IndexGatewayClient
pool *ring_client.Pool
ring ring.ReadRing
stringBufPool *sync.Pool
instanceBufPool *sync.Pool
}
// NewGatewayClient instantiates a new client used to communicate with an Index Gateway instance.
//
// If it is configured to be in ring mode, a pool of GRPC connections to all Index Gateway instances is created.
// Otherwise, it creates a single GRPC connection to an Index Gateway instance running in simple mode.
func NewGatewayClient(cfg IndexGatewayClientConfig, r prometheus.Registerer, logger log.Logger) (*GatewayClient, error) {
latency := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "loki",
Name: "index_gateway_request_duration_seconds",
Help: "Time (in seconds) spent serving requests when using the index gateway",
Buckets: instrument.DefBuckets,
}, []string{"operation", "status_code"})
if r != nil {
err := r.Register(latency)
if err != nil {
alreadyErr, ok := err.(prometheus.AlreadyRegisteredError)
if !ok {
return nil, err
}
latency = alreadyErr.ExistingCollector.(*prometheus.HistogramVec)
}
}
sgClient := &GatewayClient{
cfg: cfg,
storeGatewayClientRequestDuration: latency,
ring: cfg.Ring,
}
dialOpts, err := cfg.GRPCClientConfig.DialOption(grpcclient.Instrument(sgClient.storeGatewayClientRequestDuration))
if err != nil {
return nil, errors.Wrap(err, "index gateway grpc dial option")
}
if sgClient.cfg.Mode == indexgateway.RingMode {
factory := func(addr string) (ring_client.PoolClient, error) {
igPool, err := NewIndexGatewayGRPCPool(addr, dialOpts)
if err != nil {
return nil, errors.Wrap(err, "new index gateway grpc pool")
}
return igPool, nil
}
// Replication factor plus additional room for JOINING/LEAVING instances
// See also ring.GetBufferSize
bufSize := cfg.Ring.ReplicationFactor() * 3 / 2
sgClient.stringBufPool = &sync.Pool{
New: func() any {
return make([]string, 0, bufSize)
},
}
sgClient.instanceBufPool = &sync.Pool{
New: func() any {
return make([]ring.InstanceDesc, 0, bufSize)
},
}
sgClient.pool = clientpool.NewPool(cfg.PoolConfig, sgClient.ring, factory, logger)
} else {
sgClient.conn, err = grpc.Dial(cfg.Address, dialOpts...)
if err != nil {
return nil, errors.Wrap(err, "index gateway grpc dial")
}
sgClient.grpcClient = logproto.NewIndexGatewayClient(sgClient.conn)
}
return sgClient, nil
}
// Stop stops the execution of this gateway client.
//
// If it is in simple mode, the single GRPC connection is closed. Otherwise, nothing happens.
func (s *GatewayClient) Stop() {
if s.cfg.Mode == indexgateway.SimpleMode {
s.conn.Close()
}
}
func (s *GatewayClient) QueryPages(ctx context.Context, queries []index.Query, callback index.QueryPagesCallback) error {
if len(queries) <= maxQueriesPerGrpc {
return s.doQueries(ctx, queries, callback)
}
jobsCount := len(queries) / maxQueriesPerGrpc
if len(queries)%maxQueriesPerGrpc != 0 {
jobsCount++
}
return concurrency.ForEachJob(ctx, jobsCount, maxConcurrentGrpcCalls, func(ctx context.Context, idx int) error {
return s.doQueries(ctx, queries[idx*maxQueriesPerGrpc:util_math.Min((idx+1)*maxQueriesPerGrpc, len(queries))], callback)
})
}
func (s *GatewayClient) QueryIndex(_ context.Context, _ *logproto.QueryIndexRequest, _ ...grpc.CallOption) (logproto.IndexGateway_QueryIndexClient, error) {
panic("not implemented")
}
func (s *GatewayClient) GetChunkRef(ctx context.Context, in *logproto.GetChunkRefRequest, opts ...grpc.CallOption) (*logproto.GetChunkRefResponse, error) {
if s.cfg.Mode == indexgateway.RingMode {
var (
resp *logproto.GetChunkRefResponse
err error
)
err = s.ringModeDo(ctx, func(client logproto.IndexGatewayClient) error {
resp, err = client.GetChunkRef(ctx, in, opts...)
return err
})
return resp, err
}
return s.grpcClient.GetChunkRef(ctx, in, opts...)
}
func (s *GatewayClient) GetSeries(ctx context.Context, in *logproto.GetSeriesRequest, opts ...grpc.CallOption) (*logproto.GetSeriesResponse, error) {
if s.cfg.Mode == indexgateway.RingMode {
var (
resp *logproto.GetSeriesResponse
err error
)
err = s.ringModeDo(ctx, func(client logproto.IndexGatewayClient) error {
resp, err = client.GetSeries(ctx, in, opts...)
return err
})
return resp, err
}
return s.grpcClient.GetSeries(ctx, in, opts...)
}
func (s *GatewayClient) LabelNamesForMetricName(ctx context.Context, in *logproto.LabelNamesForMetricNameRequest, opts ...grpc.CallOption) (*logproto.LabelResponse, error) {
if s.cfg.Mode == indexgateway.RingMode {
var (
resp *logproto.LabelResponse
err error
)
err = s.ringModeDo(ctx, func(client logproto.IndexGatewayClient) error {
resp, err = client.LabelNamesForMetricName(ctx, in, opts...)
return err
})
return resp, err
}
return s.grpcClient.LabelNamesForMetricName(ctx, in, opts...)
}
func (s *GatewayClient) LabelValuesForMetricName(ctx context.Context, in *logproto.LabelValuesForMetricNameRequest, opts ...grpc.CallOption) (*logproto.LabelResponse, error) {
if s.cfg.Mode == indexgateway.RingMode {
var (
resp *logproto.LabelResponse
err error
)
err = s.ringModeDo(ctx, func(client logproto.IndexGatewayClient) error {
resp, err = client.LabelValuesForMetricName(ctx, in, opts...)
return err
})
return resp, err
}
return s.grpcClient.LabelValuesForMetricName(ctx, in, opts...)
}
func (s *GatewayClient) GetStats(ctx context.Context, in *logproto.IndexStatsRequest, opts ...grpc.CallOption) (*logproto.IndexStatsResponse, error) {
if s.cfg.Mode == indexgateway.RingMode {
var (
resp *logproto.IndexStatsResponse
err error
)
err = s.ringModeDo(ctx, func(client logproto.IndexGatewayClient) error {
resp, err = client.GetStats(ctx, in, opts...)
return err
})
return resp, err
}
return s.grpcClient.GetStats(ctx, in, opts...)
}
func (s *GatewayClient) GetLabelVolume(ctx context.Context, in *logproto.LabelVolumeRequest, opts ...grpc.CallOption) (*logproto.LabelVolumeResponse, error) {
if s.cfg.Mode == indexgateway.RingMode {
var (
resp *logproto.LabelVolumeResponse
err error
)
err = s.ringModeDo(ctx, func(client logproto.IndexGatewayClient) error {
resp, err = client.GetLabelVolume(ctx, in, opts...)
return err
})
return resp, err
}
return s.grpcClient.GetLabelVolume(ctx, in, opts...)
}
func (s *GatewayClient) doQueries(ctx context.Context, queries []index.Query, callback index.QueryPagesCallback) error {
queryKeyQueryMap := make(map[string]index.Query, len(queries))
gatewayQueries := make([]*logproto.IndexQuery, 0, len(queries))
for _, query := range queries {
queryKeyQueryMap[shipper_util.QueryKey(query)] = query
gatewayQueries = append(gatewayQueries, &logproto.IndexQuery{
TableName: query.TableName,
HashValue: query.HashValue,
RangeValuePrefix: query.RangeValuePrefix,
RangeValueStart: query.RangeValueStart,
ValueEqual: query.ValueEqual,
})
}
if s.cfg.Mode == indexgateway.RingMode {
return s.ringModeDo(ctx, func(client logproto.IndexGatewayClient) error {
return s.clientDoQueries(ctx, gatewayQueries, queryKeyQueryMap, callback, client)
})
}
return s.clientDoQueries(ctx, gatewayQueries, queryKeyQueryMap, callback, s.grpcClient)
}
// clientDoQueries send a query request to an Index Gateway instance using the given gRPC client.
//
// It is used by both, simple and ring mode.
func (s *GatewayClient) clientDoQueries(ctx context.Context, gatewayQueries []*logproto.IndexQuery,
queryKeyQueryMap map[string]index.Query, callback index.QueryPagesCallback, client logproto.IndexGatewayClient,
) error {
streamer, err := client.QueryIndex(ctx, &logproto.QueryIndexRequest{Queries: gatewayQueries})
if err != nil {
return errors.Wrap(err, "query index")
}
for {
resp, err := streamer.Recv()
if err == io.EOF {
break
}
if err != nil {
return errors.WithStack(err)
}
query, ok := queryKeyQueryMap[resp.QueryKey]
if !ok {
level.Error(util_log.Logger).Log("msg", fmt.Sprintf("unexpected %s QueryKey received, expected queries %s", resp.QueryKey, fmt.Sprint(queryKeyQueryMap)))
return fmt.Errorf("unexpected %s QueryKey received", resp.QueryKey)
}
if !callback(query, &readBatch{resp}) {
return nil
}
}
return nil
}
// ringModeDo executes the given function for each Index Gateway instance in the ring mapping to the correct tenant in the index.
// In case of callback failure, we'll try another member of the ring for that tenant ID.
func (s *GatewayClient) ringModeDo(ctx context.Context, callback func(client logproto.IndexGatewayClient) error) error {
userID, err := tenant.TenantID(ctx)
if err != nil {
return errors.Wrap(err, "index gateway client get tenant ID")
}
bufDescs := s.instanceBufPool.Get().([]ring.InstanceDesc)
defer s.instanceBufPool.Put(bufDescs) //nolint:staticcheck
bufHosts := s.stringBufPool.Get().([]string)
defer s.stringBufPool.Put(bufHosts) //nolint:staticcheck
bufZones := s.stringBufPool.Get().([]string)
defer s.stringBufPool.Put(bufZones) //nolint:staticcheck
key := util.TokenFor(userID, "" /* labels */)
rs, err := s.ring.Get(key, ring.WriteNoExtend, bufDescs[:0], bufHosts[:0], bufZones[:0])
if err != nil {
return errors.Wrap(err, "index gateway get ring")
}
addrs := rs.GetAddresses()
// shuffle addresses to make sure we don't always access the same Index Gateway instances in sequence for same tenant.
rand.Shuffle(len(addrs), func(i, j int) {
addrs[i], addrs[j] = addrs[j], addrs[i]
})
var lastErr error
for _, addr := range addrs {
if s.cfg.LogGatewayRequests {
level.Debug(util_log.Logger).Log("msg", "sending request to gateway", "gateway", addr, "tenant", userID)
}
genericClient, err := s.pool.GetClientFor(addr)
if err != nil {
level.Error(util_log.Logger).Log("msg", fmt.Sprintf("failed to get client for instance %s", addr), "err", err)
continue
}
client := (genericClient.(logproto.IndexGatewayClient))
if err := callback(client); err != nil {
lastErr = err
level.Error(util_log.Logger).Log("msg", fmt.Sprintf("client do failed for instance %s", addr), "err", err)
continue
}
return nil
}
return lastErr
}
func (s *GatewayClient) NewWriteBatch() index.WriteBatch {
panic("unsupported")
}
func (s *GatewayClient) BatchWrite(_ context.Context, _ index.WriteBatch) error {
panic("unsupported")
}
type readBatch struct {
*logproto.QueryIndexResponse
}
func (r *readBatch) Iterator() index.ReadBatchIterator {
return &grpcIter{
i: -1,
QueryIndexResponse: r.QueryIndexResponse,
}
}
type grpcIter struct {
i int
*logproto.QueryIndexResponse
}
func (b *grpcIter) Next() bool {
b.i++
return b.i < len(b.Rows)
}
func (b *grpcIter) RangeValue() []byte {
return b.Rows[b.i].RangeValue
}
func (b *grpcIter) Value() []byte {
return b.Rows[b.i].Value
}