2016-12-31 01:17:59 +00:00
|
|
|
// Package precreator provides the shard precreation service.
|
2016-02-10 18:30:52 +00:00
|
|
|
package precreator // import "github.com/influxdata/influxdb/services/precreator"
|
2015-06-10 18:12:25 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-01 18:26:23 +00:00
|
|
|
"fmt"
|
2015-06-10 18:12:25 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-12-01 18:26:23 +00:00
|
|
|
|
2016-12-15 14:54:14 +00:00
|
|
|
"go.uber.org/zap"
|
2015-06-10 18:12:25 +00:00
|
|
|
)
|
|
|
|
|
2015-11-18 09:32:39 +00:00
|
|
|
// Service manages the shard precreation service.
|
2015-06-10 18:12:25 +00:00
|
|
|
type Service struct {
|
|
|
|
checkInterval time.Duration
|
|
|
|
advancePeriod time.Duration
|
|
|
|
|
2016-12-01 18:26:23 +00:00
|
|
|
Logger zap.Logger
|
2015-06-10 18:12:25 +00:00
|
|
|
|
|
|
|
done chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2015-12-23 15:48:25 +00:00
|
|
|
MetaClient interface {
|
2015-09-01 18:51:26 +00:00
|
|
|
PrecreateShardGroups(now, cutoff time.Time) error
|
2015-06-10 18:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 22:26:24 +00:00
|
|
|
// NewService returns an instance of the precreation service.
|
2015-06-10 18:12:25 +00:00
|
|
|
func NewService(c Config) (*Service, error) {
|
|
|
|
s := Service{
|
|
|
|
checkInterval: time.Duration(c.CheckInterval),
|
|
|
|
advancePeriod: time.Duration(c.AdvancePeriod),
|
2016-12-01 18:26:23 +00:00
|
|
|
Logger: zap.New(zap.NullEncoder()),
|
2015-06-10 18:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &s, nil
|
|
|
|
}
|
|
|
|
|
2016-12-31 01:17:59 +00:00
|
|
|
// WithLogger sets the logger for the service.
|
2016-12-01 18:26:23 +00:00
|
|
|
func (s *Service) WithLogger(log zap.Logger) {
|
|
|
|
s.Logger = log.With(zap.String("service", "shard-precreation"))
|
2015-06-10 18:12:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 22:26:24 +00:00
|
|
|
// Open starts the precreation service.
|
2015-06-10 18:12:25 +00:00
|
|
|
func (s *Service) Open() error {
|
|
|
|
if s.done != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:26:23 +00:00
|
|
|
s.Logger.Info(fmt.Sprintf("Starting precreation service with check interval of %s, advance period of %s",
|
|
|
|
s.checkInterval, s.advancePeriod))
|
2015-08-12 22:07:12 +00:00
|
|
|
|
2015-06-10 18:12:25 +00:00
|
|
|
s.done = make(chan struct{})
|
|
|
|
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.runPrecreation()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-10 22:26:24 +00:00
|
|
|
// Close stops the precreation service.
|
2015-06-10 18:12:25 +00:00
|
|
|
func (s *Service) Close() error {
|
|
|
|
if s.done == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
close(s.done)
|
|
|
|
s.wg.Wait()
|
|
|
|
s.done = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-10 22:26:24 +00:00
|
|
|
// runPrecreation continually checks if resources need precreation.
|
2015-06-10 18:12:25 +00:00
|
|
|
func (s *Service) runPrecreation() {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(s.checkInterval):
|
2015-06-10 22:26:24 +00:00
|
|
|
if err := s.precreate(time.Now().UTC()); err != nil {
|
2016-12-01 18:26:23 +00:00
|
|
|
s.Logger.Info(fmt.Sprintf("failed to precreate shards: %s", err.Error()))
|
2015-06-10 18:12:25 +00:00
|
|
|
}
|
|
|
|
case <-s.done:
|
2016-12-01 18:26:23 +00:00
|
|
|
s.Logger.Info("Precreation service terminating")
|
2015-06-10 18:12:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 22:26:24 +00:00
|
|
|
// precreate performs actual resource precreation.
|
2015-09-01 18:51:26 +00:00
|
|
|
func (s *Service) precreate(now time.Time) error {
|
|
|
|
cutoff := now.Add(s.advancePeriod).UTC()
|
2015-12-23 15:48:25 +00:00
|
|
|
if err := s.MetaClient.PrecreateShardGroups(now, cutoff); err != nil {
|
2015-06-10 22:26:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-10 18:12:25 +00:00
|
|
|
}
|