forked from thanos-io/thanos
/
storeset.go
399 lines (334 loc) · 11.5 KB
/
storeset.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
package query
import (
"context"
"fmt"
"sort"
"sync"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/improbable-eng/thanos/pkg/component"
"github.com/improbable-eng/thanos/pkg/runutil"
"github.com/improbable-eng/thanos/pkg/store"
"github.com/improbable-eng/thanos/pkg/store/storepb"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/tsdb/labels"
"google.golang.org/grpc"
)
const (
unhealthyStoreMessage = "removing store because it's unhealthy or does not exist"
droppingStoreMessage = "dropping store, external labels are not unique"
)
type StoreSpec interface {
// Addr returns StoreAPI Address for the store spec. It is used as ID for store.
Addr() string
// Metadata returns current labels, store type and min, max ranges for store.
// It can change for every call for this method.
// If metadata call fails we assume that store is no longer accessible and we should not use it.
// NOTE: It is implementation responsibility to retry until context timeout, but a caller responsibility to manage
// given store connection.
Metadata(ctx context.Context, client storepb.StoreClient) (labels []storepb.Label, mint int64, maxt int64, err error)
}
type StoreStatus struct {
Name string
LastCheck time.Time
LastError error
Labels []storepb.Label
StoreType component.StoreAPI
MinTime int64
MaxTime int64
}
type grpcStoreSpec struct {
addr string
}
// NewGRPCStoreSpec creates store pure gRPC spec.
// It uses Info gRPC call to get Metadata.
func NewGRPCStoreSpec(addr string) StoreSpec {
return &grpcStoreSpec{addr: addr}
}
func (s *grpcStoreSpec) Addr() string {
// API addr should not change between state changes.
return s.addr
}
// Metadata method for gRPC store API tries to reach host Info method until context timeout. If we are unable to get metadata after
// that time, we assume that the host is unhealthy and return error.
func (s *grpcStoreSpec) Metadata(ctx context.Context, client storepb.StoreClient) (labels []storepb.Label, mint int64, maxt int64, err error) {
resp, err := client.Info(ctx, &storepb.InfoRequest{}, grpc.FailFast(false))
if err != nil {
return nil, 0, 0, errors.Wrapf(err, "fetching store info from %s", s.addr)
}
return resp.Labels, resp.MinTime, resp.MaxTime, nil
}
// StoreSet maintains a set of active stores. It is backed up by Store Specifications that are dynamically fetched on
// every Update() call.
type StoreSet struct {
logger log.Logger
// Store specifications can change dynamically. If some store is missing from the list, we assuming it is no longer
// accessible and we close gRPC client for it.
storeSpecs func() []StoreSpec
dialOpts []grpc.DialOption
gRPCInfoCallTimeout time.Duration
mtx sync.RWMutex
storesStatusesMtx sync.RWMutex
stores map[string]*storeRef
storeNodeConnections prometheus.Gauge
externalLabelStores map[string]int
storeStatuses map[string]*StoreStatus
}
type storeSetNodeCollector struct {
externalLabelOccurrences func() map[string]int
}
var (
nodeInfoDesc = prometheus.NewDesc(
"thanos_store_node_info",
"Number of nodes with the same external labels identified by their hash. If any time-series is larger than 1, external label uniqueness is not true",
[]string{"external_labels"}, nil,
)
)
func (c *storeSetNodeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- nodeInfoDesc
}
func (c *storeSetNodeCollector) Collect(ch chan<- prometheus.Metric) {
externalLabelOccurrences := c.externalLabelOccurrences()
for externalLabels, occurrences := range externalLabelOccurrences {
ch <- prometheus.MustNewConstMetric(nodeInfoDesc, prometheus.GaugeValue, float64(occurrences), externalLabels)
}
}
// NewStoreSet returns a new set of stores from cluster peers and statically configured ones.
func NewStoreSet(
logger log.Logger,
reg *prometheus.Registry,
storeSpecs func() []StoreSpec,
dialOpts []grpc.DialOption,
) *StoreSet {
storeNodeConnections := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "thanos_store_nodes_grpc_connections",
Help: "Number indicating current number of gRPC connection to store nodes. This indicates also to how many stores query node have access to.",
})
if logger == nil {
logger = log.NewNopLogger()
}
if reg != nil {
reg.MustRegister(storeNodeConnections)
}
if storeSpecs == nil {
storeSpecs = func() []StoreSpec { return nil }
}
ss := &StoreSet{
logger: log.With(logger, "component", "storeset"),
storeSpecs: storeSpecs,
dialOpts: dialOpts,
storeNodeConnections: storeNodeConnections,
gRPCInfoCallTimeout: 10 * time.Second,
externalLabelStores: map[string]int{},
stores: make(map[string]*storeRef),
storeStatuses: make(map[string]*StoreStatus),
}
storeNodeCollector := &storeSetNodeCollector{externalLabelOccurrences: ss.externalLabelOccurrences}
if reg != nil {
reg.MustRegister(storeNodeCollector)
}
return ss
}
type storeRef struct {
storepb.StoreClient
mtx sync.RWMutex
cc *grpc.ClientConn
addr string
// Meta (can change during runtime).
labels []storepb.Label
storeType component.StoreAPI
minTime int64
maxTime int64
logger log.Logger
}
func (s *storeRef) Update(labels []storepb.Label, minTime int64, maxTime int64) {
s.mtx.RLock()
defer s.mtx.RUnlock()
s.labels = labels
s.minTime = minTime
s.maxTime = maxTime
}
func (s *storeRef) Labels() []storepb.Label {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.labels
}
func (s *storeRef) TimeRange() (int64, int64) {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.minTime, s.maxTime
}
func (s *storeRef) String() string {
mint, maxt := s.TimeRange()
return fmt.Sprintf("Addr: %s Labels: %v Mint: %d Maxt: %d", s.addr, s.Labels(), mint, maxt)
}
func (s *storeRef) close() {
runutil.CloseWithLogOnErr(s.logger, s.cc, fmt.Sprintf("store %v connection close", s.addr))
}
// Update updates the store set. It fetches current list of store specs from function and updates the fresh metadata
// from all stores.
func (s *StoreSet) Update(ctx context.Context) {
healthyStores := s.getHealthyStores(ctx)
// Record the number of occurrences of external label combinations for current store slice.
externalLabelStores := map[string]int{}
for _, st := range healthyStores {
externalLabelStores[externalLabelsFromStore(st)]++
}
s.mtx.Lock()
defer s.mtx.Unlock()
// Close stores that where not healthy this time (are not in healthy stores map).
for addr, store := range s.stores {
if _, ok := healthyStores[addr]; ok {
continue
}
// Peer does not exists anymore.
store.close()
delete(s.stores, addr)
s.updateStoreStatus(store, errors.New(unhealthyStoreMessage))
level.Info(s.logger).Log("msg", unhealthyStoreMessage, "address", addr)
}
// Add stores that are not yet in s.stores.
for addr, store := range healthyStores {
if _, ok := s.stores[addr]; ok {
s.updateStoreStatus(store, nil)
continue
}
// Check if it has some external labels specified.
// No external labels means strictly store gateway or ruler and it is fine to have access to multiple instances of them.
//
// Sidecar will error out if it will be configured with empty external labels.
if len(store.Labels()) > 0 && externalLabelStores[externalLabelsFromStore(store)] != 1 {
store.close()
s.updateStoreStatus(store, errors.New(droppingStoreMessage))
level.Warn(s.logger).Log("msg", droppingStoreMessage, "address", addr)
continue
}
s.stores[addr] = store
s.updateStoreStatus(store, nil)
level.Info(s.logger).Log("msg", "adding new store to query storeset", "address", addr)
}
s.externalLabelStores = externalLabelStores
s.storeNodeConnections.Set(float64(len(s.stores)))
}
func (s *StoreSet) getHealthyStores(ctx context.Context) map[string]*storeRef {
var (
unique = make(map[string]struct{})
healthyStores = make(map[string]*storeRef, len(s.stores))
mtx sync.Mutex
wg sync.WaitGroup
)
// Gather healthy stores map concurrently. Build new store if does not exist already.
for _, storeSpec := range s.storeSpecs() {
if _, ok := unique[storeSpec.Addr()]; ok {
level.Warn(s.logger).Log("msg", "duplicated address in gossip or static store nodes", "address", storeSpec.Addr())
continue
}
unique[storeSpec.Addr()] = struct{}{}
wg.Add(1)
go func(spec StoreSpec) {
defer wg.Done()
addr := spec.Addr()
ctx, cancel := context.WithTimeout(ctx, s.gRPCInfoCallTimeout)
defer cancel()
store, ok := s.stores[addr]
if ok {
// Check existing store. Is it healthy? What are current metadata?
labels, minTime, maxTime, err := spec.Metadata(ctx, store.StoreClient)
if err != nil {
// Peer unhealthy. Do not include in healthy stores.
s.updateStoreStatus(store, err)
level.Warn(s.logger).Log("msg", "update of store node failed", "err", err, "address", addr)
return
}
store.Update(labels, minTime, maxTime)
} else {
// New store or was unhealthy and was removed in the past - create new one.
conn, err := grpc.DialContext(ctx, addr, s.dialOpts...)
if err != nil {
s.updateStoreStatus(&storeRef{addr: addr}, err)
level.Warn(s.logger).Log("msg", "update of store node failed", "err", errors.Wrap(err, "dialing connection"), "address", addr)
return
}
store = &storeRef{StoreClient: storepb.NewStoreClient(conn), cc: conn, addr: addr, logger: s.logger}
// Initial info call for all types of stores (gossip + static) to check gRPC StoreAPI.
resp, err := store.StoreClient.Info(ctx, &storepb.InfoRequest{}, grpc.FailFast(false))
if err != nil {
store.close()
s.updateStoreStatus(store, err)
level.Warn(s.logger).Log("msg", "update of store node failed", "err", errors.Wrap(err, "initial store client info fetch"), "address", addr)
return
}
store.storeType = component.FromProto(resp.StoreType)
store.Update(resp.Labels, resp.MinTime, resp.MaxTime)
}
mtx.Lock()
defer mtx.Unlock()
healthyStores[addr] = store
}(storeSpec)
}
wg.Wait()
return healthyStores
}
func externalLabelsFromStore(store *storeRef) string {
tsdbLabels := labels.Labels{}
for _, l := range store.labels {
tsdbLabels = append(tsdbLabels, labels.Label{
Name: l.Name,
Value: l.Value,
})
}
sort.Sort(tsdbLabels)
return tsdbLabels.String()
}
func (s *StoreSet) updateStoreStatus(store *storeRef, err error) {
s.storesStatusesMtx.Lock()
defer s.storesStatusesMtx.Unlock()
now := time.Now()
s.storeStatuses[store.addr] = &StoreStatus{
Name: store.addr,
LastError: err,
LastCheck: now,
Labels: store.labels,
StoreType: store.storeType,
MinTime: store.minTime,
MaxTime: store.maxTime,
}
}
func (s *StoreSet) GetStoreStatus() []StoreStatus {
s.storesStatusesMtx.RLock()
defer s.storesStatusesMtx.RUnlock()
statuses := make([]StoreStatus, 0, len(s.storeStatuses))
for _, v := range s.storeStatuses {
statuses = append(statuses, *v)
}
sort.Slice(statuses, func(i, j int) bool {
return statuses[i].Name < statuses[j].Name
})
return statuses
}
func (s *StoreSet) externalLabelOccurrences() map[string]int {
s.mtx.RLock()
defer s.mtx.RUnlock()
r := make(map[string]int, len(s.externalLabelStores))
for k, v := range s.externalLabelStores {
r[k] = v
}
return r
}
// Get returns a list of all active stores.
func (s *StoreSet) Get() []store.Client {
s.mtx.RLock()
defer s.mtx.RUnlock()
stores := make([]store.Client, 0, len(s.stores))
for _, st := range s.stores {
stores = append(stores, st)
}
return stores
}
func (s *StoreSet) Close() {
for _, st := range s.stores {
st.close()
}
}