-
Notifications
You must be signed in to change notification settings - Fork 455
/
flush_handler.go
209 lines (182 loc) · 6.38 KB
/
flush_handler.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
// Copyright (c) 2018 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 downsample
import (
"bytes"
"context"
"sync"
"time"
"github.com/m3db/m3/src/aggregator/aggregator/handler"
"github.com/m3db/m3/src/aggregator/aggregator/handler/writer"
"github.com/m3db/m3/src/metrics/metric/aggregated"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/storage"
"github.com/m3db/m3/src/query/ts"
"github.com/m3db/m3/src/x/convert"
"github.com/m3db/m3/src/x/instrument"
"github.com/m3db/m3/src/x/serialize"
xsync "github.com/m3db/m3/src/x/sync"
"github.com/uber-go/tally"
"go.uber.org/zap"
)
var (
// MetricsOptionIDSchemeTagName is a meta tag
// that describes the ID should use a specific ID scheme.
MetricsOptionIDSchemeTagName = []byte("__option_id_scheme__")
// GraphiteIDSchemeTagValue specifies that the graphite ID
// scheme should be used for a metric.
GraphiteIDSchemeTagValue = []byte("graphite")
)
var (
aggregationSuffixTag = []byte("agg")
)
type downsamplerFlushHandler struct {
sync.RWMutex
storage storage.Storage
metricTagsIteratorPool serialize.MetricTagsIteratorPool
workerPool xsync.WorkerPool
instrumentOpts instrument.Options
metrics downsamplerFlushHandlerMetrics
tagOptions models.TagOptions
}
type downsamplerFlushHandlerMetrics struct {
flushSuccess tally.Counter
flushErrors tally.Counter
}
func newDownsamplerFlushHandlerMetrics(
scope tally.Scope,
) downsamplerFlushHandlerMetrics {
return downsamplerFlushHandlerMetrics{
flushSuccess: scope.Counter("flush-success"),
flushErrors: scope.Counter("flush-errors"),
}
}
func newDownsamplerFlushHandler(
storage storage.Storage,
metricTagsIteratorPool serialize.MetricTagsIteratorPool,
workerPool xsync.WorkerPool,
tagOptions models.TagOptions,
instrumentOpts instrument.Options,
) handler.Handler {
scope := instrumentOpts.MetricsScope().SubScope("downsampler-flush-handler")
return &downsamplerFlushHandler{
storage: storage,
metricTagsIteratorPool: metricTagsIteratorPool,
workerPool: workerPool,
instrumentOpts: instrumentOpts,
metrics: newDownsamplerFlushHandlerMetrics(scope),
tagOptions: tagOptions,
}
}
func (h *downsamplerFlushHandler) NewWriter(
scope tally.Scope,
) (writer.Writer, error) {
return &downsamplerFlushHandlerWriter{
tagOptions: h.tagOptions,
ctx: context.Background(),
handler: h,
}, nil
}
func (h *downsamplerFlushHandler) Close() {
}
type downsamplerFlushHandlerWriter struct {
tagOptions models.TagOptions
wg sync.WaitGroup
ctx context.Context
handler *downsamplerFlushHandler
}
func (w *downsamplerFlushHandlerWriter) Write(
mp aggregated.ChunkedMetricWithStoragePolicy,
) error {
w.wg.Add(1)
w.handler.workerPool.Go(func() {
defer w.wg.Done()
logger := w.handler.instrumentOpts.Logger()
iter := w.handler.metricTagsIteratorPool.Get()
iter.Reset(mp.ChunkedID.Data)
expected := iter.NumTags()
chunkSuffix := mp.ChunkedID.Suffix
if len(chunkSuffix) != 0 {
expected++
}
tags := models.NewTags(expected, w.tagOptions)
for iter.Next() {
name, value := iter.Current()
// NB(r): Quite gross, need to actually make it possible to plumb this
// through for each metric.
// TODO_FIX_GRAPHITE_TAGGING: Using this string constant to track
// all places worth fixing this hack. There is at least one
// other path where flows back to the coordinator from the aggregator
// and this tag is interpreted, eventually need to handle more cleanly.
if bytes.Equal(name, MetricsOptionIDSchemeTagName) {
if bytes.Equal(value, GraphiteIDSchemeTagValue) &&
tags.Opts.IDSchemeType() != models.TypeGraphite {
iter.Reset(mp.ChunkedID.Data)
tags.Opts = w.tagOptions.SetIDSchemeType(models.TypeGraphite)
tags.Tags = tags.Tags[:0]
}
// Continue, whether we updated and need to restart iteration,
// or if passing for the second time
continue
}
tags = tags.AddTag(models.Tag{Name: name, Value: value}.Clone())
}
if len(chunkSuffix) != 0 {
tags = tags.AddTag(models.Tag{Name: aggregationSuffixTag, Value: chunkSuffix}.Clone())
}
err := iter.Err()
iter.Close()
if err != nil {
logger.Error("downsampler flush error preparing write", zap.Error(err))
w.handler.metrics.flushErrors.Inc(1)
return
}
err = w.handler.storage.Write(w.ctx, &storage.WriteQuery{
Tags: tags,
Datapoints: ts.Datapoints{ts.Datapoint{
Timestamp: time.Unix(0, mp.TimeNanos),
Value: mp.Value,
}},
Unit: convert.UnitForM3DB(mp.StoragePolicy.Resolution().Precision),
Attributes: storage.Attributes{
MetricsType: storage.AggregatedMetricsType,
Retention: mp.StoragePolicy.Retention().Duration(),
Resolution: mp.StoragePolicy.Resolution().Window,
},
})
if err != nil {
logger.Error("downsampler flush error failed write", zap.Error(err))
w.handler.metrics.flushErrors.Inc(1)
return
}
w.handler.metrics.flushSuccess.Inc(1)
})
return nil
}
func (w *downsamplerFlushHandlerWriter) Flush() error {
// NB(r): This is just simply waiting for inflight requests
// to complete since this flush handler isn't connection based.
w.wg.Wait()
return nil
}
func (w *downsamplerFlushHandlerWriter) Close() error {
// NB(r): This is a no-op since this flush handler isn't connection based.
return nil
}