-
Notifications
You must be signed in to change notification settings - Fork 453
/
storage_policy.go
285 lines (254 loc) · 8.62 KB
/
storage_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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) 2016 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"
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/m3db/m3/src/metrics/generated/proto/policypb"
xtime "github.com/m3db/m3/src/x/time"
)
const (
resolutionRetentionSeparator = ":"
)
var (
// EmptyStoragePolicy represents an empty storage policy.
EmptyStoragePolicy StoragePolicy
errNilStoragePolicyProto = errors.New("nil storage policy proto")
errInvalidStoragePolicyString = errors.New("invalid storage policy string")
)
// StoragePolicy represents the resolution and retention period metric datapoints
// are stored at.
type StoragePolicy struct {
resolution Resolution
retention Retention
}
// NewStoragePolicy creates a new storage policy given a resolution and a retention.
func NewStoragePolicy(window time.Duration, precision xtime.Unit, retention time.Duration) StoragePolicy {
return StoragePolicy{
resolution: Resolution{
Window: window,
Precision: precision,
},
retention: Retention(retention),
}
}
// NewStoragePolicyFromProto creates a new storage policy from a storage policy protobuf message.
func NewStoragePolicyFromProto(pb *policypb.StoragePolicy) (StoragePolicy, error) {
if pb == nil {
return EmptyStoragePolicy, errNilStoragePolicyProto
}
var sp StoragePolicy
if err := sp.FromProto(*pb); err != nil {
return EmptyStoragePolicy, err
}
return sp, nil
}
// String is the string representation of a storage policy.
func (p StoragePolicy) String() string {
return fmt.Sprintf("%s%s%s", p.resolution.String(), resolutionRetentionSeparator, p.retention.String())
}
// Resolution returns the resolution of the storage policy.
func (p StoragePolicy) Resolution() Resolution {
return p.resolution
}
// Retention return the retention of the storage policy.
func (p StoragePolicy) Retention() Retention {
return p.retention
}
// Proto returns the proto message for the storage policy.
func (p StoragePolicy) Proto() (*policypb.StoragePolicy, error) {
var pb policypb.StoragePolicy
if err := p.ToProto(&pb); err != nil {
return nil, err
}
return &pb, nil
}
// ToProto converts the storage policy to a protobuf message in place.
func (p StoragePolicy) ToProto(pb *policypb.StoragePolicy) error {
if pb.Resolution == nil {
pb.Resolution = &policypb.Resolution{}
}
if err := p.resolution.ToProto(pb.Resolution); err != nil {
return err
}
if pb.Retention == nil {
pb.Retention = &policypb.Retention{}
}
p.retention.ToProto(pb.Retention)
return nil
}
// FromProto converts the protobuf message to a storage policy in place.
func (p *StoragePolicy) FromProto(pb policypb.StoragePolicy) error {
if err := p.resolution.FromProto(pb.Resolution); err != nil {
return err
}
if err := p.retention.FromProto(pb.Retention); err != nil {
return err
}
return nil
}
// MarshalJSON returns the JSON encoding of a storage policy.
func (p StoragePolicy) MarshalJSON() ([]byte, error) {
marshalled := strconv.Quote(p.String())
return []byte(marshalled), nil
}
// UnmarshalJSON unmarshals JSON-encoded data into a storage policy.
func (p *StoragePolicy) UnmarshalJSON(data []byte) error {
str := string(data)
unquoted, err := strconv.Unquote(str)
if err != nil {
return err
}
parsed, err := ParseStoragePolicy(unquoted)
if err != nil {
return err
}
*p = parsed
return nil
}
// UnmarshalYAML unmarshals a storage policy value from a string.
func (p *StoragePolicy) UnmarshalYAML(unmarshal func(interface{}) error) error {
var str string
if err := unmarshal(&str); err != nil {
return err
}
parsed, err := ParseStoragePolicy(str)
if err != nil {
return err
}
*p = parsed
return nil
}
// ParseStoragePolicy parses a storage policy in the form of resolution:retention.
func ParseStoragePolicy(str string) (StoragePolicy, error) {
parts := strings.Split(str, resolutionRetentionSeparator)
if len(parts) != 2 {
return EmptyStoragePolicy, errInvalidStoragePolicyString
}
resolution, err := ParseResolution(parts[0])
if err != nil {
return EmptyStoragePolicy, err
}
retention, err := ParseRetention(parts[1])
if err != nil {
return EmptyStoragePolicy, err
}
return StoragePolicy{resolution: resolution, retention: retention}, nil
}
// MustParseStoragePolicy parses a storage policy in the form of resolution:retention,
// and panics if the input string is invalid.
func MustParseStoragePolicy(str string) StoragePolicy {
sp, err := ParseStoragePolicy(str)
if err != nil {
panic(fmt.Errorf("invalid storage policy string %s: %v", str, err))
}
return sp
}
// StoragePolicies is a list of storage policies.
type StoragePolicies []StoragePolicy
// NewStoragePoliciesFromProto creates a list of storage policies from given storage policies proto.
func NewStoragePoliciesFromProto(
storagePolicies []*policypb.StoragePolicy,
) (StoragePolicies, error) {
res := make(StoragePolicies, 0, len(storagePolicies))
for _, sp := range storagePolicies {
storagePolicy, err := NewStoragePolicyFromProto(sp)
if err != nil {
return nil, err
}
res = append(res, storagePolicy)
}
return res, nil
}
// Equal returns true if two lists of storage policies are considered equal.
func (sp StoragePolicies) Equal(other StoragePolicies) bool {
if len(sp) != len(other) {
return false
}
sp1 := sp.Clone()
sp2 := other.Clone()
sort.Sort(ByResolutionAscRetentionDesc(sp1))
sort.Sort(ByResolutionAscRetentionDesc(sp2))
for i := 0; i < len(sp1); i++ {
if sp1[i] != sp2[i] {
return false
}
}
return true
}
// Proto returns the proto message for the given list of storage policies.
func (sp StoragePolicies) Proto() ([]*policypb.StoragePolicy, error) {
pbStoragePolicies := make([]*policypb.StoragePolicy, 0, len(sp))
for _, storagePolicy := range sp {
pbStoragePolicy, err := storagePolicy.Proto()
if err != nil {
return nil, err
}
pbStoragePolicies = append(pbStoragePolicies, pbStoragePolicy)
}
return pbStoragePolicies, nil
}
// Clone clones the list of storage policies.
func (sp StoragePolicies) Clone() StoragePolicies {
cloned := make(StoragePolicies, len(sp))
copy(cloned, sp)
return cloned
}
// IsDefault returns whether a list of storage policies are considered
// as default storage policies.
func (sp StoragePolicies) IsDefault() bool { return len(sp) == 0 }
// ByResolutionAscRetentionDesc implements the sort.Sort interface that enables sorting
// storage policies by resolution in ascending order and then by retention in descending
// order.
type ByResolutionAscRetentionDesc StoragePolicies
func (sp ByResolutionAscRetentionDesc) Len() int { return len(sp) }
func (sp ByResolutionAscRetentionDesc) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
func (sp ByResolutionAscRetentionDesc) Less(i, j int) bool {
rw1, rw2 := sp[i].Resolution().Window, sp[j].Resolution().Window
if rw1 != rw2 {
return rw1 < rw2
}
rt1, rt2 := sp[i].Retention(), sp[j].Retention()
if rt1 != rt2 {
return rt1 > rt2
}
return sp[i].Resolution().Precision < sp[j].Resolution().Precision
}
// ByRetentionAscResolutionAsc implements the sort.Sort interface that enables sorting
// storage policies by retention in ascending order and then by resolution in ascending
// order.
type ByRetentionAscResolutionAsc StoragePolicies
func (sp ByRetentionAscResolutionAsc) Len() int { return len(sp) }
func (sp ByRetentionAscResolutionAsc) Swap(i, j int) { sp[i], sp[j] = sp[j], sp[i] }
func (sp ByRetentionAscResolutionAsc) Less(i, j int) bool {
rt1, rt2 := sp[i].Retention(), sp[j].Retention()
if rt1 != rt2 {
return rt1 < rt2
}
rw1, rw2 := sp[i].Resolution().Window, sp[j].Resolution().Window
if rw1 != rw2 {
return rw1 < rw2
}
return sp[i].Resolution().Precision < sp[j].Resolution().Precision
}