2019-02-19 23:47:19 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
2019-05-16 15:30:36 +00:00
|
|
|
"time"
|
2019-02-19 23:47:19 +00:00
|
|
|
|
2019-12-06 03:55:26 +00:00
|
|
|
"github.com/benbjohnson/clock"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/rand"
|
|
|
|
"github.com/influxdata/influxdb/v2/resource"
|
|
|
|
"github.com/influxdata/influxdb/v2/resource/noop"
|
|
|
|
"github.com/influxdata/influxdb/v2/snowflake"
|
2019-12-06 03:55:26 +00:00
|
|
|
"go.uber.org/zap"
|
2019-02-19 23:47:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ influxdb.UserService = (*Service)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpPrefix is the prefix for kv errors.
|
|
|
|
const OpPrefix = "kv/"
|
|
|
|
|
|
|
|
// Service is the struct that influxdb services are implemented on.
|
|
|
|
type Service struct {
|
2020-02-07 16:01:37 +00:00
|
|
|
kv Store
|
|
|
|
log *zap.Logger
|
|
|
|
clock clock.Clock
|
|
|
|
Config ServiceConfig
|
|
|
|
audit resource.Logger
|
2019-09-07 23:00:26 +00:00
|
|
|
IDGenerator influxdb.IDGenerator
|
|
|
|
|
2020-03-05 20:36:58 +00:00
|
|
|
// FluxLanguageService is used for parsing flux.
|
|
|
|
// If this is unset, operations that require parsing flux
|
|
|
|
// will fail.
|
|
|
|
FluxLanguageService influxdb.FluxLanguageService
|
|
|
|
|
2019-09-07 23:00:26 +00:00
|
|
|
// special ID generator that never returns bytes with backslash,
|
|
|
|
// comma, or space. Used to support very specific encoding of org &
|
|
|
|
// bucket into the old measurement in storage.
|
2020-08-11 14:56:42 +00:00
|
|
|
OrgIDs influxdb.IDGenerator
|
|
|
|
BucketIDs influxdb.IDGenerator
|
2019-09-07 23:00:26 +00:00
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
TokenGenerator influxdb.TokenGenerator
|
2019-11-07 14:46:30 +00:00
|
|
|
// TODO(desa:ariel): this should not be embedded
|
2019-04-19 19:46:58 +00:00
|
|
|
influxdb.TimeGenerator
|
|
|
|
Hash Crypt
|
2019-12-26 04:55:04 +00:00
|
|
|
|
2019-12-27 02:15:14 +00:00
|
|
|
variableStore *IndexStore
|
2020-03-18 12:23:51 +00:00
|
|
|
|
2020-03-20 11:03:00 +00:00
|
|
|
urmByUserIndex *Index
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns an instance of a Service.
|
2019-12-04 23:10:23 +00:00
|
|
|
func NewService(log *zap.Logger, kv Store, configs ...ServiceConfig) *Service {
|
2019-05-15 17:16:47 +00:00
|
|
|
s := &Service{
|
2019-12-04 23:10:23 +00:00
|
|
|
log: log,
|
2019-09-07 23:00:26 +00:00
|
|
|
IDGenerator: snowflake.NewIDGenerator(),
|
|
|
|
// Seed the random number generator with the current time
|
2020-08-11 14:56:42 +00:00
|
|
|
OrgIDs: rand.NewOrgBucketID(time.Now().UnixNano()),
|
|
|
|
BucketIDs: rand.NewOrgBucketID(time.Now().UnixNano()),
|
2019-02-19 23:47:19 +00:00
|
|
|
TokenGenerator: rand.NewTokenGenerator(64),
|
|
|
|
Hash: &Bcrypt{},
|
|
|
|
kv: kv,
|
2020-01-13 14:22:52 +00:00
|
|
|
audit: noop.ResourceLogger{},
|
2019-04-19 19:46:58 +00:00
|
|
|
TimeGenerator: influxdb.RealTimeGenerator{},
|
2019-12-31 03:11:53 +00:00
|
|
|
variableStore: newVariableStore(),
|
2020-07-01 11:08:20 +00:00
|
|
|
urmByUserIndex: NewIndex(URMByUserIndexMapping, WithIndexReadPathEnabled),
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
2019-05-15 17:16:47 +00:00
|
|
|
|
|
|
|
if len(configs) > 0 {
|
|
|
|
s.Config = configs[0]
|
2020-03-09 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s.Config.SessionLength == 0 {
|
2019-05-15 17:16:47 +00:00
|
|
|
s.Config.SessionLength = influxdb.DefaultSessionLength
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:55:26 +00:00
|
|
|
s.clock = s.Config.Clock
|
|
|
|
if s.clock == nil {
|
|
|
|
s.clock = clock.New()
|
|
|
|
}
|
2020-03-05 20:36:58 +00:00
|
|
|
s.FluxLanguageService = s.Config.FluxLanguageService
|
2019-12-06 03:55:26 +00:00
|
|
|
|
2019-05-15 17:16:47 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceConfig allows us to configure Services
|
|
|
|
type ServiceConfig struct {
|
2020-07-01 11:08:20 +00:00
|
|
|
SessionLength time.Duration
|
|
|
|
Clock clock.Clock
|
|
|
|
FluxLanguageService influxdb.FluxLanguageService
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 14:22:52 +00:00
|
|
|
// WithResourceLogger sets the resource audit logger for the service.
|
|
|
|
func (s *Service) WithResourceLogger(audit resource.Logger) {
|
|
|
|
s.audit = audit
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
// WithStore sets kv store for the service.
|
|
|
|
// Should only be used in tests for mocking.
|
|
|
|
func (s *Service) WithStore(store Store) {
|
|
|
|
s.kv = store
|
|
|
|
}
|