-
Notifications
You must be signed in to change notification settings - Fork 784
/
factory.go
318 lines (280 loc) · 12.1 KB
/
factory.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
package storage
import (
"context"
"flag"
"fmt"
"strings"
"time"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/aws"
"github.com/cortexproject/cortex/pkg/chunk/azure"
"github.com/cortexproject/cortex/pkg/chunk/cache"
"github.com/cortexproject/cortex/pkg/chunk/cassandra"
"github.com/cortexproject/cortex/pkg/chunk/gcp"
"github.com/cortexproject/cortex/pkg/chunk/local"
"github.com/cortexproject/cortex/pkg/chunk/objectclient"
"github.com/cortexproject/cortex/pkg/chunk/openstack"
"github.com/cortexproject/cortex/pkg/chunk/purger"
"github.com/cortexproject/cortex/pkg/util"
)
// Supported storage engines
const (
StorageEngineChunks = "chunks"
StorageEngineTSDB = "tsdb"
)
type indexStoreFactories struct {
indexClientFactoryFunc IndexClientFactoryFunc
tableClientFactoryFunc TableClientFactoryFunc
}
// IndexClientFactoryFunc defines signature of function which creates chunk.IndexClient for managing index in index store
type IndexClientFactoryFunc func() (chunk.IndexClient, error)
// TableClientFactoryFunc defines signature of function which creates chunk.TableClient for managing tables in index store
type TableClientFactoryFunc func() (chunk.TableClient, error)
var customIndexStores = map[string]indexStoreFactories{}
// RegisterIndexStore is used for registering a custom index type.
// When an index type is registered here with same name as existing types, the registered one takes the precedence.
func RegisterIndexStore(name string, indexClientFactory IndexClientFactoryFunc, tableClientFactory TableClientFactoryFunc) {
customIndexStores[name] = indexStoreFactories{indexClientFactory, tableClientFactory}
}
// StoreLimits helps get Limits specific to Queries for Stores
type StoreLimits interface {
CardinalityLimit(userID string) int
MaxChunksPerQuery(userID string) int
MaxQueryLength(userID string) time.Duration
}
// Config chooses which storage client to use.
type Config struct {
Engine string `yaml:"engine"`
AWSStorageConfig aws.StorageConfig `yaml:"aws"`
AzureStorageConfig azure.BlobStorageConfig `yaml:"azure"`
GCPStorageConfig gcp.Config `yaml:"bigtable"`
GCSConfig gcp.GCSConfig `yaml:"gcs"`
CassandraStorageConfig cassandra.Config `yaml:"cassandra"`
BoltDBConfig local.BoltDBConfig `yaml:"boltdb"`
FSConfig local.FSConfig `yaml:"filesystem"`
Swift openstack.SwiftConfig `yaml:"swift"`
IndexCacheValidity time.Duration `yaml:"index_cache_validity"`
IndexQueriesCacheConfig cache.Config `yaml:"index_queries_cache_config"`
DeleteStoreConfig purger.DeleteStoreConfig `yaml:"delete_store"`
}
// RegisterFlags adds the flags required to configure this flag set.
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
cfg.AWSStorageConfig.RegisterFlags(f)
cfg.AzureStorageConfig.RegisterFlags(f)
cfg.GCPStorageConfig.RegisterFlags(f)
cfg.GCSConfig.RegisterFlags(f)
cfg.CassandraStorageConfig.RegisterFlags(f)
cfg.BoltDBConfig.RegisterFlags(f)
cfg.FSConfig.RegisterFlags(f)
cfg.DeleteStoreConfig.RegisterFlags(f)
cfg.Swift.RegisterFlags(f)
f.StringVar(&cfg.Engine, "store.engine", "chunks", "The storage engine to use: chunks or tsdb. Be aware tsdb is experimental and shouldn't be used in production.")
cfg.IndexQueriesCacheConfig.RegisterFlagsWithPrefix("store.index-cache-read.", "Cache config for index entry reading. ", f)
f.DurationVar(&cfg.IndexCacheValidity, "store.index-cache-validity", 5*time.Minute, "Cache validity for active index entries. Should be no higher than -ingester.max-chunk-idle.")
}
// Validate config and returns error on failure
func (cfg *Config) Validate() error {
if cfg.Engine != StorageEngineChunks && cfg.Engine != StorageEngineTSDB {
return errors.New("unsupported storage engine")
}
if err := cfg.CassandraStorageConfig.Validate(); err != nil {
return errors.Wrap(err, "invalid Cassandra Storage config")
}
if err := cfg.Swift.Validate(); err != nil {
return errors.Wrap(err, "invalid Swift Storage config")
}
if err := cfg.IndexQueriesCacheConfig.Validate(); err != nil {
return errors.Wrap(err, "invalid Index Queries Cache config")
}
return nil
}
// NewStore makes the storage clients based on the configuration.
func NewStore(cfg Config, storeCfg chunk.StoreConfig, schemaCfg chunk.SchemaConfig, limits StoreLimits, reg prometheus.Registerer, cacheGenNumLoader chunk.CacheGenNumLoader) (chunk.Store, error) {
chunkMetrics := newChunkClientMetrics(reg)
indexReadCache, err := cache.New(cfg.IndexQueriesCacheConfig)
if err != nil {
return nil, err
}
writeDedupeCache, err := cache.New(storeCfg.WriteDedupeCacheConfig)
if err != nil {
return nil, err
}
chunkCacheCfg := storeCfg.ChunkCacheConfig
chunkCacheCfg.Prefix = "chunks"
chunksCache, err := cache.New(chunkCacheCfg)
if err != nil {
return nil, err
}
// Cache is shared by multiple stores, which means they will try and Stop
// it more than once. Wrap in a StopOnce to prevent this.
indexReadCache = cache.StopOnce(indexReadCache)
chunksCache = cache.StopOnce(chunksCache)
writeDedupeCache = cache.StopOnce(writeDedupeCache)
// lets wrap all caches with CacheGenMiddleware to facilitate cache invalidation using cache generation numbers
indexReadCache = cache.NewCacheGenNumMiddleware(indexReadCache)
chunksCache = cache.NewCacheGenNumMiddleware(chunksCache)
writeDedupeCache = cache.NewCacheGenNumMiddleware(writeDedupeCache)
err = schemaCfg.Load()
if err != nil {
return nil, errors.Wrap(err, "error loading schema config")
}
stores := chunk.NewCompositeStore(cacheGenNumLoader)
for _, s := range schemaCfg.Configs {
index, err := NewIndexClient(s.IndexType, cfg, schemaCfg)
if err != nil {
return nil, errors.Wrap(err, "error creating index client")
}
index = newCachingIndexClient(index, indexReadCache, cfg.IndexCacheValidity, limits)
objectStoreType := s.ObjectType
if objectStoreType == "" {
objectStoreType = s.IndexType
}
chunks, err := NewChunkClient(objectStoreType, cfg, schemaCfg)
if err != nil {
return nil, errors.Wrap(err, "error creating object client")
}
chunks = newMetricsChunkClient(chunks, chunkMetrics)
err = stores.AddPeriod(storeCfg, s, index, chunks, limits, chunksCache, writeDedupeCache)
if err != nil {
return nil, err
}
}
return stores, nil
}
// NewIndexClient makes a new index client of the desired type.
func NewIndexClient(name string, cfg Config, schemaCfg chunk.SchemaConfig) (chunk.IndexClient, error) {
if indexClientFactory, ok := customIndexStores[name]; ok {
if indexClientFactory.indexClientFactoryFunc != nil {
return indexClientFactory.indexClientFactoryFunc()
}
}
switch name {
case "inmemory":
store := chunk.NewMockStorage()
return store, nil
case "aws", "aws-dynamo":
if cfg.AWSStorageConfig.DynamoDB.URL == nil {
return nil, fmt.Errorf("Must set -dynamodb.url in aws mode")
}
path := strings.TrimPrefix(cfg.AWSStorageConfig.DynamoDB.URL.Path, "/")
if len(path) > 0 {
level.Warn(util.Logger).Log("msg", "ignoring DynamoDB URL path", "path", path)
}
return aws.NewDynamoDBIndexClient(cfg.AWSStorageConfig.DynamoDBConfig, schemaCfg)
case "gcp":
return gcp.NewStorageClientV1(context.Background(), cfg.GCPStorageConfig, schemaCfg)
case "gcp-columnkey", "bigtable":
return gcp.NewStorageClientColumnKey(context.Background(), cfg.GCPStorageConfig, schemaCfg)
case "bigtable-hashed":
cfg.GCPStorageConfig.DistributeKeys = true
return gcp.NewStorageClientColumnKey(context.Background(), cfg.GCPStorageConfig, schemaCfg)
case "cassandra":
return cassandra.NewStorageClient(cfg.CassandraStorageConfig, schemaCfg)
case "boltdb":
return local.NewBoltDBIndexClient(cfg.BoltDBConfig)
default:
return nil, fmt.Errorf("Unrecognized storage client %v, choose one of: aws, cassandra, inmemory, gcp, bigtable, bigtable-hashed", name)
}
}
// NewChunkClient makes a new chunk.Client of the desired types.
func NewChunkClient(name string, cfg Config, schemaCfg chunk.SchemaConfig) (chunk.Client, error) {
switch name {
case "inmemory":
return chunk.NewMockStorage(), nil
case "aws", "s3":
return newChunkClientFromStore(aws.NewS3ObjectClient(cfg.AWSStorageConfig.S3Config, chunk.DirDelim))
case "aws-dynamo":
if cfg.AWSStorageConfig.DynamoDB.URL == nil {
return nil, fmt.Errorf("Must set -dynamodb.url in aws mode")
}
path := strings.TrimPrefix(cfg.AWSStorageConfig.DynamoDB.URL.Path, "/")
if len(path) > 0 {
level.Warn(util.Logger).Log("msg", "ignoring DynamoDB URL path", "path", path)
}
return aws.NewDynamoDBChunkClient(cfg.AWSStorageConfig.DynamoDBConfig, schemaCfg)
case "azure":
return newChunkClientFromStore(azure.NewBlobStorage(&cfg.AzureStorageConfig, chunk.DirDelim))
case "gcp":
return gcp.NewBigtableObjectClient(context.Background(), cfg.GCPStorageConfig, schemaCfg)
case "gcp-columnkey", "bigtable", "bigtable-hashed":
return gcp.NewBigtableObjectClient(context.Background(), cfg.GCPStorageConfig, schemaCfg)
case "gcs":
return newChunkClientFromStore(gcp.NewGCSObjectClient(context.Background(), cfg.GCSConfig, chunk.DirDelim))
case "swift":
return newChunkClientFromStore(openstack.NewSwiftObjectClient(cfg.Swift, chunk.DirDelim))
case "cassandra":
return cassandra.NewStorageClient(cfg.CassandraStorageConfig, schemaCfg)
case "filesystem":
store, err := local.NewFSObjectClient(cfg.FSConfig)
if err != nil {
return nil, err
}
return objectclient.NewClient(store, objectclient.Base64Encoder), nil
default:
return nil, fmt.Errorf("Unrecognized storage client %v, choose one of: aws, azure, cassandra, inmemory, gcp, bigtable, bigtable-hashed", name)
}
}
func newChunkClientFromStore(store chunk.ObjectClient, err error) (chunk.Client, error) {
if err != nil {
return nil, err
}
return objectclient.NewClient(store, nil), nil
}
// NewTableClient makes a new table client based on the configuration.
func NewTableClient(name string, cfg Config) (chunk.TableClient, error) {
if indexClientFactory, ok := customIndexStores[name]; ok {
if indexClientFactory.tableClientFactoryFunc != nil {
return indexClientFactory.tableClientFactoryFunc()
}
}
switch name {
case "inmemory":
return chunk.NewMockStorage(), nil
case "aws", "aws-dynamo":
if cfg.AWSStorageConfig.DynamoDB.URL == nil {
return nil, fmt.Errorf("Must set -dynamodb.url in aws mode")
}
path := strings.TrimPrefix(cfg.AWSStorageConfig.DynamoDB.URL.Path, "/")
if len(path) > 0 {
level.Warn(util.Logger).Log("msg", "ignoring DynamoDB URL path", "path", path)
}
return aws.NewDynamoDBTableClient(cfg.AWSStorageConfig.DynamoDBConfig)
case "gcp", "gcp-columnkey", "bigtable", "bigtable-hashed":
return gcp.NewTableClient(context.Background(), cfg.GCPStorageConfig)
case "cassandra":
return cassandra.NewTableClient(context.Background(), cfg.CassandraStorageConfig)
case "boltdb":
return local.NewTableClient(cfg.BoltDBConfig.Directory)
default:
return nil, fmt.Errorf("Unrecognized storage client %v, choose one of: aws, cassandra, inmemory, gcp, bigtable, bigtable-hashed", name)
}
}
// NewBucketClient makes a new bucket client based on the configuration.
func NewBucketClient(storageConfig Config) (chunk.BucketClient, error) {
if storageConfig.FSConfig.Directory != "" {
return local.NewFSObjectClient(storageConfig.FSConfig)
}
return nil, nil
}
// NewObjectClient makes a new StorageClient of the desired types.
func NewObjectClient(name string, cfg Config) (chunk.ObjectClient, error) {
switch name {
case "aws", "s3":
return aws.NewS3ObjectClient(cfg.AWSStorageConfig.S3Config, chunk.DirDelim)
case "gcs":
return gcp.NewGCSObjectClient(context.Background(), cfg.GCSConfig, chunk.DirDelim)
case "azure":
return azure.NewBlobStorage(&cfg.AzureStorageConfig, chunk.DirDelim)
case "swift":
return openstack.NewSwiftObjectClient(cfg.Swift, chunk.DirDelim)
case "inmemory":
return chunk.NewMockStorage(), nil
case "filesystem":
return local.NewFSObjectClient(cfg.FSConfig)
default:
return nil, fmt.Errorf("Unrecognized storage client %v, choose one of: aws, s3, gcs, azure, filesystem", name)
}
}