/
intervals.go
673 lines (596 loc) · 15.1 KB
/
intervals.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
Copyright © 2019 Portworx
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sched
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
)
const (
scheduleSeparator = ";"
nonYamlTypeSeparator = "="
DailyType = "daily"
MonthlyType = "monthly"
WeeklyType = "weekly"
PeriodicType = "periodic"
retainSeparator = ","
MonthlyRetain = 12
WeeklyRetain = 5
DailyRetain = 7
)
var (
// speedUp advances the clock faster for tests.
speedUp = false
)
// SpeedUp advances teh clock faster for tests.
func SpeedUp() {
speedUp = true
}
func inSpeedUp() bool {
return speedUp
}
type IntervalSpec struct {
Freq string
Period uint64 `yaml:"period,omitempty"`
Month int `yaml:"month,omitempty"`
Weekday int `yaml:"weekday,omitempty"`
Day int `yaml:"day,omitempty"`
Hour int `yaml:"hour,omitempty"`
Minute int `yaml:"minute,omitempty"`
}
type Interval interface {
nextAfter(t time.Time) time.Time
String() string
IntervalType() string
Spec() IntervalSpec
}
type periodic struct {
delta time.Duration
}
func (p periodic) nextAfter(t time.Time) time.Time {
return t.Add(p.delta)
}
func (p periodic) String() string {
return fmt.Sprintf("%v %v", PeriodicType, p.delta)
}
func (p periodic) IntervalType() string {
return PeriodicType
}
func (p periodic) Spec() IntervalSpec {
return IntervalSpec{Freq: PeriodicType, Period: uint64(p.delta)}
}
func Periodic(period time.Duration) Interval {
return periodic{period}
}
type daily struct {
hour int
minute int
}
func (d daily) nextAfter(t time.Time) time.Time {
h, m := t.Hour(), t.Minute()
if h < d.hour {
t = t.Add(time.Duration(d.hour-h) * time.Hour)
} else if h > d.hour || m >= d.minute {
t = t.Add(time.Duration(24-h+d.hour) * time.Hour)
}
return t.Add(time.Duration(d.minute-m) * time.Minute)
}
func (d daily) String() string {
return fmt.Sprintf("%s @%02d:%02d", DailyType, d.hour, d.minute)
}
func (p daily) IntervalType() string {
return DailyType
}
func (d daily) Spec() IntervalSpec {
return IntervalSpec{Freq: DailyType, Hour: d.hour, Minute: d.minute}
}
func Daily(hour int, minute int) Interval {
return daily{hour, minute}
}
type weekly struct {
day time.Weekday
tod daily
}
func (w weekly) nextAfter(t time.Time) time.Time {
t = w.tod.nextAfter(t)
d := t.Weekday()
delta := time.Duration(w.day - d)
if w.day < d {
delta += 7
}
return t.Add(delta * 24 * time.Hour)
}
func (w weekly) String() string {
return fmt.Sprintf("%s %s@%02d:%02d", WeeklyType, w.day.String(), w.tod.hour,
w.tod.minute)
}
func (p weekly) IntervalType() string {
return WeeklyType
}
func (w weekly) Spec() IntervalSpec {
return IntervalSpec{Freq: WeeklyType,
Weekday: int(w.day), Hour: w.tod.hour, Minute: w.tod.minute}
}
func Weekly(dow time.Weekday, hour int, minute int) Interval {
return weekly{dow, daily{hour, minute}}
}
type monthly struct {
day int
tod daily
}
func (m monthly) nextAfter(t time.Time) time.Time {
t = m.tod.nextAfter(t)
y, month, d := t.Date()
if d > m.day {
daysIn := time.Date(y, month+1, 0, 0, 0, 0, 0, time.UTC).Day()
t = t.Add(time.Duration(daysIn*24) * time.Hour)
}
return t
}
func (m monthly) String() string {
return fmt.Sprintf("%s %d@%02d:%02d", MonthlyType, m.day, m.tod.hour,
m.tod.minute)
}
func (p monthly) IntervalType() string {
return MonthlyType
}
func (m monthly) Spec() IntervalSpec {
return IntervalSpec{Freq: MonthlyType,
Day: m.day, Hour: m.tod.hour, Minute: m.tod.minute}
}
func Monthly(day int, hour int, minute int) Interval {
return monthly{day, daily{hour, minute}}
}
func parseSpec(spec *IntervalSpec) (Interval, error) {
switch spec.Freq {
case PeriodicType:
return Periodic(time.Duration(spec.Period)), nil
case DailyType:
return Daily(spec.Hour, spec.Minute), nil
case WeeklyType:
dow := time.Weekday(spec.Weekday)
return Weekly(dow, spec.Hour, spec.Minute), nil
case MonthlyType:
if spec.Day == 0 {
spec.Day = 1
}
return Monthly(spec.Day, spec.Hour, spec.Minute), nil
}
return nil, fmt.Errorf("Invalid schedule spec")
}
func parseRetainSpec(spec *RetainIntervalSpec) (RetainInterval, error) {
s, err := parseSpec(&spec.IntervalSpec)
if err != nil {
return nil, err
}
return &RetainIntervalImpl{iv: s, retain: spec.Retain}, nil
}
func parseNonYamlSchedule(schedule string) (RetainIntervalSpec, error) {
parts := strings.Split(schedule, nonYamlTypeSeparator)
if len(parts) != 2 || !IsIntervalType(parts[0]) {
return RetainIntervalSpec{},
fmt.Errorf("Invalid schedule specification: %s", schedule)
}
if fn, ok := ParseCLI[parts[0]]; ok {
return fn(parts[1])
}
return ParsePeriodic(parts[1])
}
func ParseSchedule(schedule string) ([]RetainInterval, error) {
var schedInts []RetainInterval
if schedule == "" {
return schedInts, nil
}
var sspec []RetainIntervalSpec
err := yaml.Unmarshal([]byte(schedule), &sspec)
if err != nil {
intv, err := parseNonYamlSchedule(schedule)
if err != nil {
return nil, err
}
sspec = []RetainIntervalSpec{intv}
}
for _, s := range sspec {
interval, err := parseRetainSpec(&s)
if err != nil {
return nil, err
}
schedInts = append(schedInts, interval)
}
return schedInts, nil
}
func ParseScheduleAndPolicies(scheduleString string) (
[]RetainInterval,
*PolicyTags,
error,
) {
schedules := strings.Split(scheduleString, scheduleSeparator)
policies := &PolicyTags{}
var schedInts []RetainInterval
for _, schedule := range schedules {
if strings.HasPrefix(schedule, policyTag) {
if policy, err := ParsePolicyTags(schedule); err == nil {
policies.Names = append(policies.Names, policy.Names...)
} else {
return nil, nil, err
}
} else {
if scheds, err := ParseSchedule(schedule); err == nil {
if len(scheds) > 0 {
schedInts = append(schedInts, scheds...)
}
} else {
return nil, nil, err
}
}
}
if len(policies.Names) == 0 {
return schedInts, nil, nil
}
return schedInts, policies, nil
}
func ScheduleStringRetainInv(
intvs []RetainInterval,
p *PolicyTags,
) (string, error) {
intvSpecs := make([]RetainIntervalSpec, 0)
for _, intv := range intvs {
intvSpecs = append(intvSpecs, intv.RetainIntervalSpec())
}
return ScheduleString(intvSpecs, p)
}
func ScheduleString(items []RetainIntervalSpec, p *PolicyTags) (string, error) {
schedString := ""
if len(items) != 0 {
s, err := yaml.Marshal(items)
if err != nil {
return "", err
}
schedString = string(s)
}
if p != nil {
pString := p.String()
if pString != "" {
if schedString != "" {
schedString = schedString + scheduleSeparator
}
schedString = schedString + pString
}
}
return schedString, nil
}
func ScheduleIntervalSummary(items []Interval, policyTags *PolicyTags) string {
if len(items) == 0 {
return ""
}
summary := ""
for i, iv := range items {
if i != 0 {
summary += ", "
}
summary += iv.String()
}
return summary
}
func ScheduleSummary(items []RetainInterval, policyTags *PolicyTags) string {
summary := ""
if policyTags != nil {
summary = policyTags.String()
}
if len(items) == 0 {
return summary
}
if len(summary) > 0 {
summary = summary + scheduleSeparator
}
for i, iv := range items {
if i != 0 {
summary += ", "
}
summary += iv.String()
}
return summary
}
func timeOfDay(hhmm string) (int, int, error) {
if hhmm == "" {
return 0, 0, nil
}
t := strings.Split(hhmm, ":")
if len(t) == 1 {
t = append(t, "0")
}
ok := len(t) == 2
var h, m int
if ok {
var e1, e2 error
h, e1 = strconv.Atoi(t[0])
m, e2 = strconv.Atoi(t[1])
ok = e1 == nil && e2 == nil && 0 <= h && h < 24 && 0 <= m && m < 60
}
if !ok {
return 0, 0, fmt.Errorf("invalid start time %v", hhmm)
}
return h, m, nil
}
func dayOfWeek(wd string) (time.Weekday, error) {
day := strings.Title(wd)
if day == "" {
day = "Sunday"
}
d := time.Sunday
for d <= time.Saturday && d.String() != day {
d++
}
if d > time.Saturday {
return time.Sunday, fmt.Errorf("invalid weekday %v", day)
}
return d, nil
}
func parseRetainNumber(input string) (RetainIntervalSpec, string, error) {
r := RetainIntervalSpec{}
parts := strings.Split(input, retainSeparator)
if len(parts) > 1 {
if retain, err := strconv.Atoi(parts[1]); err != nil {
return r, "", fmt.Errorf("Invalid number: %s", parts[1])
} else if retain <= 0 {
return r, "", fmt.Errorf("Keep number should be greater than 0")
} else {
r.Retain = uint32(retain)
}
}
return r, parts[0], nil
}
func ParsePeriodic(input string) (RetainIntervalSpec, error) {
r, intvl, err := parseRetainNumber(input)
if err != nil {
return r, err
}
if intvl == "" {
return r, fmt.Errorf("Interval is missing")
}
delta, err := strconv.ParseUint(intvl, 10, 64)
if err != nil {
return r, fmt.Errorf("Invalid interval %s", intvl)
}
dt := time.Duration(delta) * time.Minute
r.IntervalSpec = Periodic(dt).Spec()
return r, nil
}
// parseDaily item [@]hh:mm,r
func parseDaily(dailyStr string) (RetainIntervalSpec, error) {
r, daily, err := parseRetainNumber(dailyStr)
if err != nil {
return r, err
}
if daily == "" {
return r, fmt.Errorf("Daily schedule is missing")
}
dt := strings.Split(daily, "@")
h, m, err := timeOfDay(dt[len(dt)-1])
if err != nil {
return RetainIntervalSpec{}, err
}
r.IntervalSpec = Daily(h, m).Spec()
return r, nil
}
// parseWeekly item weekday@hh:mm,r
func parseWeekly(weeklyStr string) (RetainIntervalSpec, error) {
r, weekly, err := parseRetainNumber(weeklyStr)
if err != nil {
return r, err
}
if weekly == "" {
return r, fmt.Errorf("Weekly schedule is missing")
}
dt := strings.Split(weekly, "@")
if len(dt) == 1 {
dt = append(dt, "0:0")
}
if len(dt) != 2 {
return RetainIntervalSpec{},
fmt.Errorf("Invalid weekly spec %v", weeklyStr)
}
d, err := dayOfWeek(dt[0])
if err != nil {
return RetainIntervalSpec{}, err
}
h, m, err := timeOfDay(dt[1])
r.IntervalSpec = Weekly(d, h, m).Spec()
return r, err
}
// parseMonthly item day@hh:mm,r
func parseMonthly(monthlyStr string) (RetainIntervalSpec, error) {
r, monthly, err := parseRetainNumber(monthlyStr)
if err != nil {
return r, err
}
if monthly == "" {
return r, fmt.Errorf("Monthly schedule is missing")
}
dt := strings.Split(monthly, "@")
if len(dt) == 1 {
dt = append(dt, "0:0")
}
if len(dt) != 2 {
return RetainIntervalSpec{},
fmt.Errorf("Invalid monthly spec %v", monthlyStr)
}
d, err := strconv.Atoi(dt[0])
if err != nil || d < 0 || d > 31 {
return RetainIntervalSpec{},
fmt.Errorf("Invalid day of month %v", dt[0])
}
h, m, err := timeOfDay(dt[1])
if err != nil {
return RetainIntervalSpec{}, err
}
r.IntervalSpec = Monthly(d, h, m).Spec()
return r, nil
}
var ParseCLI = map[string]func(string) (RetainIntervalSpec, error){
DailyType: parseDaily,
WeeklyType: parseWeekly,
MonthlyType: parseMonthly,
}
func IntervalType(interval Interval) string {
return strings.Split(interval.String(), " ")[0]
}
func IsIntervalType(t string) bool {
knownTypes := []string{PeriodicType, DailyType, WeeklyType, MonthlyType}
for _, p := range knownTypes {
if p == t {
return true
}
}
return false
}
// RetainIntervalSpec is the serialized form of retain interval
type RetainIntervalSpec struct {
IntervalSpec `yaml:",inline"`
Retain uint32 `yaml:"retain,omitempty"`
}
// RetainInterval is a schedule interval with a number of instances to retain.
type RetainInterval interface {
Interval
// RetainNumber is the number of instances to retain
RetainNumber() uint32
// RetainIntervalSpec returns RetainIntervalSpec
RetainIntervalSpec() RetainIntervalSpec
}
// RetainIntervalImpl implements the RetainInterval interface
type RetainIntervalImpl struct {
iv Interval
retain uint32
}
func NewRetainInterval(iv Interval) RetainInterval {
return RetainIntervalImpl{iv: iv}
}
func (p RetainIntervalImpl) nextAfter(t time.Time) time.Time {
newTime := p.iv.nextAfter(t)
if inSpeedUp() {
return t.Add(time.Minute)
}
return newTime
}
func (p RetainIntervalImpl) String() string {
str := p.iv.String()
if p.RetainNumber() > 0 {
return fmt.Sprintf("%s,keep last %d", str, p.RetainNumber())
}
return str
}
func (p RetainIntervalImpl) IntervalType() string {
return p.iv.IntervalType()
}
func (p RetainIntervalImpl) Spec() IntervalSpec {
return p.iv.Spec()
}
func (p RetainIntervalImpl) RetainNumber() uint32 {
return p.retain
}
func SetupIntvWithDefaults(intvs []RetainInterval) []RetainInterval {
retIntvs := make([]RetainInterval, 0)
for _, intv := range intvs {
p := &RetainIntervalImpl{iv: intv}
if intv.RetainNumber() == 0 {
switch intv.IntervalType() {
case DailyType:
p.retain = DailyRetain
case WeeklyType, PeriodicType:
p.retain = WeeklyRetain
case MonthlyType:
p.retain = MonthlyRetain
}
} else {
p.retain = intv.RetainNumber()
}
retIntvs = append(retIntvs, p)
}
return retIntvs
}
func (p RetainIntervalImpl) RetainIntervalSpec() RetainIntervalSpec {
return RetainIntervalSpec{IntervalSpec: p.Spec(), Retain: p.RetainNumber()}
}
const (
policyTag = "policy"
policyStrSeparator = "="
policyNameSeparator = ","
)
// PolicyTags groups one or more policies together.
type PolicyTags struct {
// Names is the list of policy names
Names []string
}
var policyNameRegex = regexp.MustCompile("[A-Za-z0-9_]+")
func (p *PolicyTags) verifyPolicyTags() error {
for _, name := range p.Names {
if !policyNameRegex.MatchString(name) {
return fmt.Errorf("Invalid policy name '%s'", name)
}
}
return nil
}
// NewPolicyTagsFromSlice returns a new object from a string slice of names
func NewPolicyTagsFromSlice(policies []string) (*PolicyTags, error) {
p := &PolicyTags{Names: policies}
return p, p.verifyPolicyTags()
}
func NewPolicyTags(policies string) (*PolicyTags, error) {
if policies == "" {
return nil, nil
}
p := &PolicyTags{Names: strings.Split(policies, policyNameSeparator)}
return p, p.verifyPolicyTags()
}
func (p *PolicyTags) Summary() string {
if len(p.Names) == 0 {
return ""
}
return fmt.Sprintf("%s=%s", policyTag,
strings.Join(p.Names, policyNameSeparator))
}
func (p *PolicyTags) String() string {
return p.Summary()
}
func ParsePolicyTags(policyTagsStr string) (*PolicyTags, error) {
if policyTagsStr == "" {
return nil, nil
}
names := strings.Split(policyTagsStr, policyStrSeparator)
if len(names) != 2 || names[0] != policyTag {
return nil, fmt.Errorf("Invalid policy string %s", policyTagsStr)
}
return NewPolicyTags(names[1])
}
func SamePolicyTags(p1, p2 *PolicyTags) bool {
if p1 == p2 {
return true
}
if p1 == nil && p2 != nil || p2 == nil && p1 != nil ||
len(p1.Names) != len(p2.Names) {
return false
}
next:
for _, name := range p1.Names {
for _, othername := range p2.Names {
if name == othername {
continue next
}
}
return false
}
return true
}