/
ruleset.go
218 lines (193 loc) · 5.93 KB
/
ruleset.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
// Copyright (c) 2017 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 matcher
import (
"sync"
"time"
"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cluster/kv/util/runtime"
"github.com/m3db/m3/src/metrics/aggregation"
"github.com/m3db/m3/src/metrics/generated/proto/rulepb"
"github.com/m3db/m3/src/metrics/metric"
"github.com/m3db/m3/src/metrics/rules"
"github.com/m3db/m3/src/x/clock"
"github.com/m3db/m3/src/x/instrument"
"github.com/uber-go/tally"
)
// RuleSet manages runtime updates to registered rules and provides
// API to match metic ids against rules in the corresponding ruleset.
type RuleSet interface {
runtime.Value
rules.Matcher
// Namespace returns the namespace of the ruleset.
Namespace() []byte
// Version returns the current version of the ruleset.
Version() int
// CutoverNanos returns the cutover time of the ruleset.
CutoverNanos() int64
// Tombstoned returns whether the ruleset is tombstoned.
Tombstoned() bool
}
type ruleSetMetrics struct {
match instrument.MethodMetrics
nilMatcher tally.Counter
updated tally.Counter
}
func newRuleSetMetrics(scope tally.Scope, opts instrument.TimerOptions) ruleSetMetrics {
return ruleSetMetrics{
match: instrument.NewMethodMetrics(scope, "match", opts),
nilMatcher: scope.Counter("nil-matcher"),
updated: scope.Counter("updated"),
}
}
// ruleSet contains the list of rules for a namespace.
type ruleSet struct {
sync.RWMutex
runtime.Value
namespace []byte
key string
store kv.Store
opts Options
nowFn clock.NowFn
matchRangePast time.Duration
ruleSetOpts rules.Options
onRuleSetUpdatedFn OnRuleSetUpdatedFn
proto *rulepb.RuleSet
version int
cutoverNanos int64
tombstoned bool
matcher rules.Matcher
metrics ruleSetMetrics
}
func newRuleSet(
namespace []byte,
key string,
opts Options,
) RuleSet {
instrumentOpts := opts.InstrumentOptions()
r := &ruleSet{
namespace: namespace,
key: key,
opts: opts,
store: opts.KVStore(),
nowFn: opts.ClockOptions().NowFn(),
matchRangePast: opts.MatchRangePast(),
ruleSetOpts: opts.RuleSetOptions(),
onRuleSetUpdatedFn: opts.OnRuleSetUpdatedFn(),
proto: &rulepb.RuleSet{},
version: kv.UninitializedVersion,
metrics: newRuleSetMetrics(instrumentOpts.MetricsScope(),
instrumentOpts.TimerOptions()),
}
valueOpts := runtime.NewOptions().
SetInstrumentOptions(opts.InstrumentOptions()).
SetInitWatchTimeout(opts.InitWatchTimeout()).
SetKVStore(r.store).
SetUnmarshalFn(r.toRuleSet).
SetProcessFn(r.process)
r.Value = runtime.NewValue(key, valueOpts)
return r
}
func (r *ruleSet) Namespace() []byte {
r.RLock()
namespace := r.namespace
r.RUnlock()
return namespace
}
func (r *ruleSet) Version() int {
r.RLock()
version := r.version
r.RUnlock()
return version
}
func (r *ruleSet) CutoverNanos() int64 {
r.RLock()
cutoverNanos := r.cutoverNanos
r.RUnlock()
return cutoverNanos
}
func (r *ruleSet) Tombstoned() bool {
r.RLock()
tombstoned := r.tombstoned
r.RUnlock()
return tombstoned
}
func (r *ruleSet) ForwardMatch(id []byte, fromNanos, toNanos int64) rules.MatchResult {
callStart := r.nowFn()
r.RLock()
if r.matcher == nil {
r.RUnlock()
r.metrics.nilMatcher.Inc(1)
return rules.EmptyMatchResult
}
res := r.matcher.ForwardMatch(id, fromNanos, toNanos)
r.RUnlock()
r.metrics.match.ReportSuccess(r.nowFn().Sub(callStart))
return res
}
func (r *ruleSet) ReverseMatch(
id []byte,
fromNanos, toNanos int64,
mt metric.Type,
at aggregation.Type,
isMultiAggregationTypesAllowed bool,
aggTypesOpts aggregation.TypesOptions,
) rules.MatchResult {
callStart := r.nowFn()
r.RLock()
if r.matcher == nil {
r.RUnlock()
r.metrics.nilMatcher.Inc(1)
return rules.EmptyMatchResult
}
res := r.matcher.ReverseMatch(id, fromNanos, toNanos, mt, at, isMultiAggregationTypesAllowed, aggTypesOpts)
r.RUnlock()
r.metrics.match.ReportSuccess(r.nowFn().Sub(callStart))
return res
}
func (r *ruleSet) toRuleSet(value kv.Value) (interface{}, error) {
r.Lock()
defer r.Unlock()
if value == nil {
return nil, errNilValue
}
r.proto.Reset()
if err := value.Unmarshal(r.proto); err != nil {
return nil, err
}
return rules.NewRuleSetFromProto(value.Version(), r.proto, r.ruleSetOpts)
}
// process processes an ruleset update.
func (r *ruleSet) process(value interface{}) error {
r.Lock()
ruleSet := value.(rules.RuleSet)
r.version = ruleSet.Version()
r.cutoverNanos = ruleSet.CutoverNanos()
r.tombstoned = ruleSet.Tombstoned()
r.matcher = ruleSet.ActiveSet(r.nowFn().Add(-r.matchRangePast).UnixNano())
r.Unlock()
// NB: calling the update callback outside the ruleset lock to avoid circular
// lock dependency causing a deadlock.
if r.onRuleSetUpdatedFn != nil {
r.onRuleSetUpdatedFn(r.namespace, r)
}
r.metrics.updated.Inc(1)
return nil
}