-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtsdb.go
265 lines (225 loc) · 7.64 KB
/
tsdb.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package store
import (
"context"
"io"
"math"
"sort"
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/thanos-io/thanos/pkg/component"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/pkg/store/storepb"
)
const RemoteReadFrameLimit = 1048576
type TSDBReader interface {
storage.ChunkQueryable
StartTime() (int64, error)
}
// TSDBStore implements the store API against a local TSDB instance.
// It attaches the provided external labels to all results. It only responds with raw data
// and does not support downsampling.
type TSDBStore struct {
logger log.Logger
db TSDBReader
component component.StoreAPI
extLset labels.Labels
maxBytesPerFrame int
}
func RegisterWritableStoreServer(storeSrv storepb.WriteableStoreServer) func(*grpc.Server) {
return func(s *grpc.Server) {
storepb.RegisterWriteableStoreServer(s, storeSrv)
}
}
// ReadWriteTSDBStore is a TSDBStore that can also be written to.
type ReadWriteTSDBStore struct {
storepb.StoreServer
storepb.WriteableStoreServer
}
// NewTSDBStore creates a new TSDBStore.
// NOTE: Given lset has to be sorted.
func NewTSDBStore(logger log.Logger, db TSDBReader, component component.StoreAPI, extLset labels.Labels) *TSDBStore {
if logger == nil {
logger = log.NewNopLogger()
}
return &TSDBStore{
logger: logger,
db: db,
component: component,
extLset: extLset,
maxBytesPerFrame: RemoteReadFrameLimit,
}
}
// Info returns store information about the Prometheus instance.
func (s *TSDBStore) Info(_ context.Context, _ *storepb.InfoRequest) (*storepb.InfoResponse, error) {
minTime, err := s.db.StartTime()
if err != nil {
return nil, errors.Wrap(err, "TSDB min Time")
}
res := &storepb.InfoResponse{
Labels: labelpb.ZLabelsFromPromLabels(s.extLset),
StoreType: s.component.ToProto(),
MinTime: minTime,
MaxTime: math.MaxInt64,
}
// Until we deprecate the single labels in the reply, we just duplicate
// them here for migration/compatibility purposes.
res.LabelSets = []labelpb.ZLabelSet{}
if len(res.Labels) > 0 {
res.LabelSets = append(res.LabelSets, labelpb.ZLabelSet{
Labels: res.Labels,
})
}
return res, nil
}
// CloseDelegator allows to delegate close (releasing resources used by request to the server).
// This is useful when we invoke StoreAPI within another StoreAPI and results are ephemeral until copied.
type CloseDelegator interface {
Delegate(io.Closer)
}
// Series returns all series for a requested time range and label matcher. The returned data may
// exceed the requested time bounds.
func (s *TSDBStore) Series(r *storepb.SeriesRequest, srv storepb.Store_SeriesServer) error {
match, matchers, err := matchesExternalLabels(r.Matchers, s.extLset)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
if !match {
return nil
}
if len(matchers) == 0 {
return status.Error(codes.InvalidArgument, errors.New("no matchers specified (excluding external labels)").Error())
}
q, err := s.db.ChunkQuerier(context.Background(), r.MinTime, r.MaxTime)
if err != nil {
return status.Error(codes.Internal, err.Error())
}
if cd, ok := srv.(CloseDelegator); ok {
cd.Delegate(q)
} else {
defer runutil.CloseWithLogOnErr(s.logger, q, "close tsdb chunk querier series")
}
set := q.Select(false, nil, matchers...)
// Stream at most one series per frame; series may be split over multiple frames according to maxBytesInFrame.
for set.Next() {
series := set.At()
storeSeries := storepb.Series{Labels: labelpb.ZLabelsFromPromLabels(labelpb.ExtendSortedLabels(series.Labels(), s.extLset))}
if r.SkipChunks {
if err := srv.Send(storepb.NewSeriesResponse(&storeSeries)); err != nil {
return status.Error(codes.Aborted, err.Error())
}
continue
}
bytesLeftForChunks := s.maxBytesPerFrame
for _, lbl := range storeSeries.Labels {
bytesLeftForChunks -= lbl.Size()
}
frameBytesLeft := bytesLeftForChunks
seriesChunks := []storepb.AggrChunk{}
chIter := series.Iterator()
isNext := chIter.Next()
for isNext {
chk := chIter.At()
if chk.Chunk == nil {
return status.Errorf(codes.Internal, "TSDBStore: found not populated chunk returned by SeriesSet at ref: %v", chk.Ref)
}
c := storepb.AggrChunk{
MinTime: chk.MinTime,
MaxTime: chk.MaxTime,
Raw: &storepb.Chunk{
Type: storepb.Chunk_Encoding(chk.Chunk.Encoding() - 1), // Proto chunk encoding is one off to TSDB one.
Data: chk.Chunk.Bytes(),
},
}
frameBytesLeft -= c.Size()
seriesChunks = append(seriesChunks, c)
// We are fine with minor inaccuracy of max bytes per frame. The inaccuracy will be max of full chunk size.
isNext = chIter.Next()
if frameBytesLeft > 0 && isNext {
continue
}
if err := srv.Send(storepb.NewSeriesResponse(&storepb.Series{Labels: storeSeries.Labels, Chunks: seriesChunks})); err != nil {
return status.Error(codes.Aborted, err.Error())
}
if isNext {
frameBytesLeft = bytesLeftForChunks
seriesChunks = make([]storepb.AggrChunk, 0, len(seriesChunks))
}
}
if err := chIter.Err(); err != nil {
return status.Error(codes.Internal, errors.Wrap(err, "chunk iter").Error())
}
}
if err := set.Err(); err != nil {
return status.Error(codes.Internal, err.Error())
}
for _, w := range set.Warnings() {
if err := srv.Send(storepb.NewWarnSeriesResponse(w)); err != nil {
return status.Error(codes.Aborted, err.Error())
}
}
return nil
}
// LabelNames returns all known label names constrained with the given matchers.
func (s *TSDBStore) LabelNames(ctx context.Context, r *storepb.LabelNamesRequest) (
*storepb.LabelNamesResponse, error,
) {
match, matchers, err := matchesExternalLabels(r.Matchers, s.extLset)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
if !match {
return &storepb.LabelNamesResponse{Names: nil}, nil
}
q, err := s.db.ChunkQuerier(ctx, r.Start, r.End)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
defer runutil.CloseWithLogOnErr(s.logger, q, "close tsdb querier label names")
res, _, err := q.LabelNames(matchers...)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if len(res) > 0 {
for _, lbl := range s.extLset {
res = append(res, lbl.Name)
}
sort.Strings(res)
}
return &storepb.LabelNamesResponse{Names: res}, nil
}
// LabelValues returns all known label values for a given label name.
func (s *TSDBStore) LabelValues(ctx context.Context, r *storepb.LabelValuesRequest) (
*storepb.LabelValuesResponse, error,
) {
if r.Label == "" {
return nil, status.Error(codes.InvalidArgument, "label name parameter cannot be empty")
}
match, matchers, err := matchesExternalLabels(r.Matchers, s.extLset)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
if !match {
return &storepb.LabelValuesResponse{Values: nil}, nil
}
if v := s.extLset.Get(r.Label); v != "" {
return &storepb.LabelValuesResponse{Values: []string{v}}, nil
}
q, err := s.db.ChunkQuerier(ctx, r.Start, r.End)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
defer runutil.CloseWithLogOnErr(s.logger, q, "close tsdb querier label values")
res, _, err := q.LabelValues(r.Label, matchers...)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &storepb.LabelValuesResponse{Values: res}, nil
}