forked from m3db/m3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cost_reporters.go
209 lines (180 loc) · 7.22 KB
/
cost_reporters.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) 2019 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 server
// This file contains reporters and setup for our query/cost.ChainedEnforcer
// instances.
import (
"sync"
"github.com/m3db/m3/src/cmd/services/m3query/config"
qcost "github.com/m3db/m3/src/query/cost"
"github.com/m3db/m3/src/x/cost"
"github.com/m3db/m3/src/x/instrument"
"github.com/uber-go/tally"
)
const (
queriesOverLimitMetric = "over_datapoints_limit"
datapointsMetric = "datapoints"
datapointsCounterMetric = "datapoints_counter"
maxDatapointsHistMetric = "max_datapoints_hist"
)
// newConfiguredChainedEnforcer returns a ChainedEnforcer with 3 configured
// levels: global, per-query, per-block. Global and per-query both have limits
// on them (as configured by cfg.Limits); per-block is purely for accounting
// purposes.
// Our enforcers report at least these stats:
// cost.global.datapoints: gauge; the number of datapoints currently in use
// by this instance.
//
// cost.global.datapoints_counter: counter; counter representation of the
// number of datapoints in use by this instance
//
// cost.{per_query,global}.over_datapoints_limit: counter; how many queries are over the
// datapoint limit
//
// cost.per_query.max_datapoints_hist: histogram; represents the
// distribution of the maximum datapoints used at any point in each query.
func newConfiguredChainedEnforcer(cfg *config.Configuration, instrumentOptions instrument.Options) (qcost.ChainedEnforcer, error) {
costScope := instrumentOptions.MetricsScope().SubScope("cost")
costIops := instrumentOptions.SetMetricsScope(costScope)
limitMgr := cost.NewStaticLimitManager(cfg.Limits.Global.AsLimitManagerOptions().SetInstrumentOptions(costIops))
tracker := cost.NewTracker()
globalEnforcer := cost.NewEnforcer(limitMgr, tracker,
cost.NewEnforcerOptions().SetReporter(
newGlobalReporter(costScope.SubScope("global")),
).SetCostExceededMessage("limits.global.maxFetchedDatapoints exceeded"),
)
queryEnforcerOpts := cost.NewEnforcerOptions().SetCostExceededMessage("limits.perQuery.maxFetchedDatapoints exceeded").
SetReporter(newPerQueryReporter(costScope.
SubScope("per_query")))
queryEnforcer := cost.NewEnforcer(
cost.NewStaticLimitManager(cfg.Limits.PerQuery.AsLimitManagerOptions()),
cost.NewTracker(),
queryEnforcerOpts)
blockEnforcer := cost.NewEnforcer(
cost.NewStaticLimitManager(cost.NewLimitManagerOptions().SetDefaultLimit(cost.Limit{Enabled: false})),
cost.NewTracker(),
nil,
)
return qcost.NewChainedEnforcer(qcost.GlobalLevel, []cost.Enforcer{
globalEnforcer,
queryEnforcer,
blockEnforcer,
})
}
// globalReporter records ChainedEnforcer statistics for the global enforcer.
type globalReporter struct {
datapoints tally.Gauge
datapointsCounter tally.Counter
overLimit overLimitReporter
}
// assert we implement the interface
var _ cost.EnforcerReporter = (*globalReporter)(nil)
func newGlobalReporter(s tally.Scope) *globalReporter {
return &globalReporter{
datapoints: s.Gauge(datapointsMetric),
datapointsCounter: s.Counter(datapointsCounterMetric),
overLimit: newOverLimitReporter(s),
}
}
func (gr *globalReporter) ReportCurrent(c cost.Cost) {
gr.datapoints.Update(float64(c))
}
// ReportCost for global reporters sends the new incoming cost to a counter.
// Since counters can only be incremented, it ignores negative values.
func (gr *globalReporter) ReportCost(c cost.Cost) {
if c > 0 {
gr.datapointsCounter.Inc(int64(c))
}
}
// ReportOverLimit delegates to gr.overLimit
func (gr *globalReporter) ReportOverLimit(enabled bool) {
gr.overLimit.ReportOverLimit(enabled)
}
// perQueryReporter records ChainedEnforcer statistics on a per query level.
type perQueryReporter struct {
mu *sync.Mutex
maxDatapoints cost.Cost
queryHisto tally.Histogram
overLimit overLimitReporter
}
// assert we implement the interface
var _ qcost.ChainedReporter = (*perQueryReporter)(nil)
func newPerQueryReporter(scope tally.Scope) *perQueryReporter {
return &perQueryReporter{
mu: &sync.Mutex{},
maxDatapoints: 0,
queryHisto: scope.Histogram(maxDatapointsHistMetric,
tally.MustMakeExponentialValueBuckets(10.0, 10.0, 6)),
overLimit: newOverLimitReporter(scope),
}
}
// ReportCost is a noop for perQueryReporter because it's noisy to report
// the current cost for every query (hard to meaningfully divide out).
// Instead, we report the max datapoints at the end of the query--see on
// release.
func (perQueryReporter) ReportCost(c cost.Cost) {}
// ReportCurrent is a noop for perQueryReporter--see ReportCost for
// explanation.
func (perQueryReporter) ReportCurrent(c cost.Cost) {}
// ReportOverLimit reports when a query is over its per query limit.
func (pr *perQueryReporter) ReportOverLimit(enabled bool) {
pr.overLimit.ReportOverLimit(enabled)
}
// OnChildClose takes the max of the current cost for this query and the
// previously recorded cost. We do this OnChildRelease instead of on
// ReportCurrent to avoid locking every time we add to the Enforcer.
func (pr *perQueryReporter) OnChildClose(curCost cost.Cost) {
pr.mu.Lock()
if curCost > pr.maxDatapoints {
pr.maxDatapoints = curCost
}
pr.mu.Unlock()
}
// OnClose records the maximum cost seen by this reporter.
func (pr *perQueryReporter) OnClose(curCost cost.Cost) {
pr.mu.Lock()
pr.queryHisto.RecordValue(float64(pr.maxDatapoints))
pr.mu.Unlock()
}
// overLimitReporter factors out reporting over limit cases for both global
// and per query enforcer reporters.
type overLimitReporter struct {
queriesOverLimitDisabled tally.Counter
queriesOverLimitEnabled tally.Counter
}
func newOverLimitReporter(scope tally.Scope) overLimitReporter {
return overLimitReporter{
queriesOverLimitDisabled: scope.Tagged(map[string]string{
"enabled": "false",
}).Counter(queriesOverLimitMetric),
queriesOverLimitEnabled: scope.Tagged(map[string]string{
"enabled": "true",
}).Counter(queriesOverLimitMetric),
}
}
// ReportOverLimit increments <prefix>.over_limit, tagged by
// "enabled".
func (olr overLimitReporter) ReportOverLimit(enabled bool) {
if enabled {
olr.queriesOverLimitEnabled.Inc(1)
} else {
olr.queriesOverLimitDisabled.Inc(1)
}
}