-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathvalidator.go
152 lines (135 loc) · 4.05 KB
/
validator.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
// 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 kv
import (
"errors"
"fmt"
"sync"
"time"
"github.com/m3db/m3/src/cluster/kv"
kvutil "github.com/m3db/m3/src/cluster/kv/util"
"github.com/m3db/m3/src/metrics/rules/validator/namespace"
"github.com/m3db/m3x/log"
)
var (
errValidatorClosed = errors.New("validator is closed")
)
type validator struct {
sync.RWMutex
opts NamespaceValidatorOptions
logger log.Logger
kvStore kv.Store
validNamespacesKey string
initWatchTimeout time.Duration
closed bool
doneCh chan struct{}
wg sync.WaitGroup
validNamespaces map[string]struct{}
}
// NewNamespaceValidator creates a new namespace validator.
func NewNamespaceValidator(opts NamespaceValidatorOptions) (namespace.Validator, error) {
v := &validator{
opts: opts,
logger: opts.InstrumentOptions().Logger(),
kvStore: opts.KVStore(),
validNamespacesKey: opts.ValidNamespacesKey(),
initWatchTimeout: opts.InitWatchTimeout(),
doneCh: make(chan struct{}),
validNamespaces: toStringSet(opts.DefaultValidNamespaces()),
}
if err := v.watchRuntimeConfig(); err != nil {
return nil, err
}
return v, nil
}
// Validate validates whether a given namespace is valid.
func (v *validator) Validate(ns string) error {
v.RLock()
defer v.RUnlock()
if v.closed {
return errValidatorClosed
}
if _, exists := v.validNamespaces[ns]; !exists {
return fmt.Errorf("%s is not a valid namespace", ns)
}
return nil
}
func (v *validator) Close() {
v.Lock()
if v.closed {
v.Unlock()
return
}
v.closed = true
close(v.doneCh)
v.Unlock()
// NB: Must wait outside the lock to avoid a circular dependency
// between the goroutine closing the validator and the goroutine
// processing updates.
v.wg.Wait()
}
func (v *validator) watchRuntimeConfig() error {
watch, err := v.kvStore.Watch(v.validNamespacesKey)
if err != nil {
return err
}
kvOpts := kvutil.NewOptions().SetLogger(v.logger)
select {
case <-watch.C():
v.processUpdate(watch.Get(), kvOpts)
case <-time.After(v.initWatchTimeout):
v.logger.WithFields(
log.NewField("key", v.validNamespacesKey),
log.NewField("timeout", v.initWatchTimeout),
log.NewField("default", v.opts.DefaultValidNamespaces()),
).Warnf("timed out waiting for initial valid namespaces")
}
v.wg.Add(1)
go func() {
defer v.wg.Done()
for {
select {
case <-watch.C():
v.processUpdate(watch.Get(), kvOpts)
case <-v.doneCh:
return
}
}
}()
return nil
}
func (v *validator) processUpdate(value kv.Value, opts kvutil.Options) {
strs, err := kvutil.StringArrayFromValue(value, v.validNamespacesKey, v.opts.DefaultValidNamespaces(), opts)
if err != nil {
// kvutil already logged the error.
return
}
m := toStringSet(strs)
v.Lock()
v.validNamespaces = m
v.Unlock()
}
func toStringSet(strs []string) map[string]struct{} {
m := make(map[string]struct{}, len(strs))
for _, s := range strs {
m[s] = struct{}{}
}
return m
}