-
Notifications
You must be signed in to change notification settings - Fork 450
/
policy.go
238 lines (201 loc) · 5.99 KB
/
policy.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
// 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 policy
import (
"errors"
"strconv"
"strings"
"github.com/m3db/m3/src/metrics/aggregation"
"github.com/m3db/m3/src/metrics/generated/proto/policypb"
)
const (
policyAggregationTypeSeparator = "|"
)
var (
// DefaultPolicy represents a default policy.
DefaultPolicy Policy
errNilPolicyProto = errors.New("nil policy proto")
errInvalidPolicyString = errors.New("invalid policy string")
errInvalidDropPolicyString = errors.New("invalid drop policy string")
)
// Policy contains a storage policy and a list of custom aggregation types.
type Policy struct {
StoragePolicy
AggregationID aggregation.ID
}
// NewPolicy creates a policy.
func NewPolicy(sp StoragePolicy, aggID aggregation.ID) Policy {
return Policy{StoragePolicy: sp, AggregationID: aggID}
}
// NewPolicyFromProto creates a new policy from a proto policy.
func NewPolicyFromProto(p *policypb.Policy) (Policy, error) {
if p == nil {
return DefaultPolicy, errNilPolicyProto
}
policy, err := NewStoragePolicyFromProto(p.StoragePolicy)
if err != nil {
return DefaultPolicy, err
}
aggID, err := aggregation.NewIDFromProto(p.AggregationTypes)
if err != nil {
return DefaultPolicy, err
}
return NewPolicy(policy, aggID), nil
}
// Proto returns the proto of the policy.
func (p Policy) Proto() (*policypb.Policy, error) {
var storagePolicyProto policypb.StoragePolicy
err := p.StoragePolicy.ToProto(&storagePolicyProto)
if err != nil {
return nil, err
}
aggTypes, err := aggregation.NewIDDecompressor().Decompress(p.AggregationID)
if err != nil {
return nil, err
}
protoAggTypes, err := aggTypes.Proto()
if err != nil {
return nil, err
}
return &policypb.Policy{
StoragePolicy: &storagePolicyProto,
AggregationTypes: protoAggTypes,
}, nil
}
// String is the string representation of a policy.
func (p Policy) String() string {
if p.AggregationID.IsDefault() {
return p.StoragePolicy.String()
}
return p.StoragePolicy.String() + policyAggregationTypeSeparator + p.AggregationID.String()
}
// MarshalJSON returns the JSON encoding of a policy.
func (p Policy) MarshalJSON() ([]byte, error) {
marshalled := strconv.Quote(p.String())
return []byte(marshalled), nil
}
// UnmarshalJSON unmarshals JSON-encoded data into a policy.
func (p *Policy) UnmarshalJSON(data []byte) error {
str := string(data)
unquoted, err := strconv.Unquote(str)
if err != nil {
return err
}
parsed, err := ParsePolicy(unquoted)
if err != nil {
return err
}
*p = parsed
return nil
}
// UnmarshalYAML unmarshals a policy value from a string.
func (p *Policy) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}
parsed, err := ParsePolicy(str)
if err != nil {
return err
}
*p = parsed
return nil
}
// ParsePolicy parses a policy in the form of resolution:retention|aggregationTypes.
func ParsePolicy(str string) (Policy, error) {
parts := strings.Split(str, policyAggregationTypeSeparator)
l := len(parts)
if l > 2 {
return DefaultPolicy, errInvalidPolicyString
}
sp, err := ParseStoragePolicy(parts[0])
if err != nil {
return DefaultPolicy, err
}
var aggID = aggregation.DefaultID
if l == 2 {
aggTypes, err := aggregation.ParseTypes(parts[1])
if err != nil {
return DefaultPolicy, err
}
aggID, err = aggregation.NewIDCompressor().Compress(aggTypes)
if err != nil {
return DefaultPolicy, err
}
}
return NewPolicy(sp, aggID), nil
}
// NewPoliciesFromProto creates multiple new policies from given proto policies.
func NewPoliciesFromProto(policies []*policypb.Policy) ([]Policy, error) {
res := make([]Policy, 0, len(policies))
for _, p := range policies {
policy, err := NewPolicyFromProto(p)
if err != nil {
return nil, err
}
res = append(res, policy)
}
return res, nil
}
// IsDefaultPolicies checks if the policies are the default policies.
func IsDefaultPolicies(ps []Policy) bool {
return len(ps) == 0
}
// Policies is a list of policies. Used to check ploicy list equivalence.
type Policies []Policy
// Equals takes a list of policies and checks equivalence.
func (p Policies) Equals(other Policies) bool {
if len(p) != len(other) {
return false
}
for i := 0; i < len(p); i++ {
if p[i] != other[i] {
return false
}
}
return true
}
// UnmarshalYAML unmarshals a drop policy value from a string.
func (p *DropPolicy) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}
// Allow default string value (not specified) to mean default
if str == "" {
*p = DefaultDropPolicy
return nil
}
parsed, err := ParseDropPolicy(str)
if err != nil {
return err
}
*p = parsed
return nil
}
// ParseDropPolicy parses a drop policy.
func ParseDropPolicy(str string) (DropPolicy, error) {
for _, valid := range validDropPolicies {
if valid.String() == str {
return valid, nil
}
}
return DefaultDropPolicy, errInvalidDropPolicyString
}