-
Notifications
You must be signed in to change notification settings - Fork 451
/
series_load.go
285 lines (242 loc) · 7.43 KB
/
series_load.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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package parser
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"sync"
"time"
"github.com/m3db/m3/src/dbnode/encoding"
"github.com/m3db/m3/src/dbnode/ts"
"github.com/m3db/m3/src/dbnode/x/xio"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/x/ident"
"github.com/m3db/m3/src/x/instrument"
xtime "github.com/m3db/m3/src/x/time"
"go.uber.org/zap"
)
// Options are options for series parsing.
type Options struct {
EncoderPool encoding.EncoderPool
IteratorPools encoding.IteratorPools
TagOptions models.TagOptions
InstrumentOptions instrument.Options
Size int
}
type nameIDSeriesMap map[string]idSeriesMap
type idSeriesMap struct {
start time.Time
end time.Time
series map[string][]Series
}
type seriesReader struct {
iterOpts Options
nameIDSeriesMap nameIDSeriesMap
sync.RWMutex
}
// SeriesReader reads SeriesIterators from a generic io.Reader.
type SeriesReader interface {
SeriesIterators(name string) (encoding.SeriesIterators, error)
Load(reader io.Reader) error
Clear()
}
// NewSeriesReader creates a new SeriesReader that reads entries as
// a slice of Series.
func NewSeriesReader(opts Options) SeriesReader {
size := 10
if opts.Size != 0 {
size = opts.Size
}
return &seriesReader{
iterOpts: opts,
nameIDSeriesMap: make(nameIDSeriesMap, size),
}
}
func (l *seriesReader) SeriesIterators(name string) (encoding.SeriesIterators, error) {
l.RLock()
defer l.RUnlock()
var seriesMaps []idSeriesMap
logger := l.iterOpts.InstrumentOptions.Logger()
if name == "" {
// return all preloaded data
seriesMaps = make([]idSeriesMap, 0, len(l.nameIDSeriesMap))
for _, series := range l.nameIDSeriesMap {
seriesMaps = append(seriesMaps, series)
}
} else {
seriesMap, found := l.nameIDSeriesMap[name]
if !found || len(seriesMap.series) == 0 {
return nil, nil
}
seriesMaps = append(seriesMaps, seriesMap)
}
iters := make([]encoding.SeriesIterator, 0, len(seriesMaps))
for _, seriesMap := range seriesMaps {
for _, seriesPerID := range seriesMap.series {
for _, series := range seriesPerID {
encoder := l.iterOpts.EncoderPool.Get()
dps := series.Datapoints
startTime := time.Time{}
if len(dps) > 0 {
startTime = dps[0].Timestamp.Truncate(time.Hour)
}
encoder.Reset(startTime, len(dps), nil)
for _, dp := range dps {
err := encoder.Encode(ts.Datapoint{
Timestamp: dp.Timestamp,
Value: float64(dp.Value),
TimestampNanos: xtime.ToUnixNano(dp.Timestamp),
}, xtime.Nanosecond, nil)
if err != nil {
encoder.Close()
logger.Error("error encoding datapoints", zap.Error(err))
return nil, err
}
}
readers := [][]xio.BlockReader{{{
SegmentReader: xio.NewSegmentReader(encoder.Discard()),
Start: series.Start,
BlockSize: series.End.Sub(series.Start),
}}}
multiReader := encoding.NewMultiReaderIterator(
iterAlloc,
l.iterOpts.IteratorPools.MultiReaderIterator(),
)
sliceOfSlicesIter := xio.NewReaderSliceOfSlicesFromBlockReadersIterator(readers)
multiReader.ResetSliceOfSlices(sliceOfSlicesIter, nil)
tagIter, id := buildTagIteratorAndID(series.Tags, l.iterOpts.TagOptions)
iter := encoding.NewSeriesIterator(
encoding.SeriesIteratorOptions{
ID: id,
Namespace: ident.StringID("ns"),
Tags: tagIter,
StartInclusive: xtime.ToUnixNano(series.Start),
EndExclusive: xtime.ToUnixNano(series.End),
Replicas: []encoding.MultiReaderIterator{
multiReader,
},
}, nil)
iters = append(iters, iter)
}
}
}
return encoding.NewSeriesIterators(
iters,
l.iterOpts.IteratorPools.MutableSeriesIterators(),
), nil
}
func calculateSeriesRange(seriesList []Series) (time.Time, time.Time) {
// NB: keep consistent start/end for the entire ingested set.
//
// Try taking from set start/end; infer from first/last endpoint otherwise.
start, end := time.Time{}, time.Time{}
for _, series := range seriesList {
if start.IsZero() || series.Start.Before(start) {
start = series.Start
}
if end.IsZero() || series.End.Before(start) {
end = series.End
}
}
if !start.IsZero() && !end.IsZero() {
return start, end
}
for _, series := range seriesList {
dps := series.Datapoints
if len(dps) == 0 {
continue
}
first, last := dps[0].Timestamp, dps[len(dps)-1].Timestamp
if start.IsZero() || first.Before(start) {
start = first
}
if end.IsZero() || last.Before(start) {
end = last
}
}
return start, end
}
func (l *seriesReader) Load(reader io.Reader) error {
l.Lock()
defer l.Unlock()
buf, err := ioutil.ReadAll(reader)
logger := l.iterOpts.InstrumentOptions.Logger()
if err != nil {
logger.Error("could not read body", zap.Error(err))
return err
}
seriesList := make([]Series, 0, 10)
if err := json.Unmarshal(buf, &seriesList); err != nil {
logger.Error("could not unmarshal queries", zap.Error(err))
return err
}
// NB: keep consistent start/end for the entire ingested set.
start, end := calculateSeriesRange(seriesList)
nameKey := string(l.iterOpts.TagOptions.MetricName())
nameMap := make(nameIDSeriesMap, len(seriesList))
for _, series := range seriesList {
names := series.Tags.Get(nameKey)
if len(names) != 1 || len(series.Datapoints) == 0 {
if len(names) > 1 {
err := fmt.Errorf("series has duplicate __name__ tags: %v", names)
logger.Error("bad __name__ variable", zap.Error(err))
return err
}
continue
}
name := names[0]
seriesMap, found := nameMap[name]
if !found {
seriesMap = idSeriesMap{
series: make(map[string][]Series, len(seriesList)),
}
}
id := series.IDOrGenID()
seriesList, found := seriesMap.series[id]
if !found {
seriesList = make([]Series, 0, 1)
} else {
logger.Info("duplicate tag set in loaded series",
zap.Int("count", len(seriesList)),
zap.String("id", id))
}
seriesList = append(seriesList, series)
seriesMap.series[id] = seriesList
logger.Info("setting series",
zap.String("name", name), zap.String("id", id))
series.Start = start
series.End = end
nameMap[name] = seriesMap
}
for k, v := range nameMap {
// NB: overwrite existing series.
l.nameIDSeriesMap[k] = v
}
return nil
}
func (l *seriesReader) Clear() {
l.Lock()
for k := range l.nameIDSeriesMap {
delete(l.nameIDSeriesMap, k)
}
l.Unlock()
}