/
service.go
93 lines (75 loc) · 2.09 KB
/
service.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
// Package precreator provides the shard precreation service.
package precreator // import "github.com/influxdata/influxdb/v2/v1/services/precreator"
import (
"context"
"sync"
"time"
"github.com/influxdata/influxdb/v2/logger"
"go.uber.org/zap"
)
// Service manages the shard precreation service.
type Service struct {
checkInterval time.Duration
advancePeriod time.Duration
Logger *zap.Logger
cancel context.CancelFunc
wg sync.WaitGroup
MetaClient interface {
PrecreateShardGroups(now, cutoff time.Time) error
}
}
// NewService returns an instance of the precreation service.
func NewService(c Config) *Service {
return &Service{
checkInterval: time.Duration(c.CheckInterval),
advancePeriod: time.Duration(c.AdvancePeriod),
Logger: zap.NewNop(),
}
}
// WithLogger sets the logger for the service.
func (s *Service) WithLogger(log *zap.Logger) {
s.Logger = log.With(zap.String("service", "shard-precreation"))
}
// Open starts the precreation service.
func (s *Service) Open(ctx context.Context) error {
if s.cancel != nil {
return nil
}
s.Logger.Info("Starting precreation service",
logger.DurationLiteral("check_interval", s.checkInterval),
logger.DurationLiteral("advance_period", s.advancePeriod))
ctx, s.cancel = context.WithCancel(ctx)
s.wg.Add(1)
go s.runPrecreation(ctx)
return nil
}
// Close stops the precreation service.
func (s *Service) Close() error {
if s.cancel == nil {
return nil
}
s.cancel()
s.wg.Wait()
s.cancel = nil
return nil
}
// runPrecreation continually checks if resources need precreation.
func (s *Service) runPrecreation(ctx context.Context) {
defer s.wg.Done()
for {
select {
case <-time.After(s.checkInterval):
if err := s.precreate(time.Now().UTC()); err != nil {
s.Logger.Info("Failed to precreate shards", zap.Error(err))
}
case <-ctx.Done():
s.Logger.Info("Terminating precreation service")
return
}
}
}
// precreate performs actual resource precreation.
func (s *Service) precreate(now time.Time) error {
cutoff := now.Add(s.advancePeriod).UTC()
return s.MetaClient.PrecreateShardGroups(now, cutoff)
}