2015-07-22 14:53:20 +00:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2016-09-13 12:04:18 +00:00
|
|
|
"regexp"
|
2015-08-16 19:45:09 +00:00
|
|
|
"sort"
|
2015-07-22 14:53:20 +00:00
|
|
|
"time"
|
|
|
|
|
2016-02-10 17:26:18 +00:00
|
|
|
"github.com/influxdata/influxdb/influxql"
|
|
|
|
"github.com/influxdata/influxdb/models"
|
2016-09-21 15:04:37 +00:00
|
|
|
"github.com/influxdata/influxdb/pkg/estimator"
|
2017-02-17 23:17:22 +00:00
|
|
|
"github.com/uber-go/zap"
|
2015-07-22 14:53:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrFormatNotFound is returned when no format can be determined from a path.
|
|
|
|
ErrFormatNotFound = errors.New("format not found")
|
2016-02-24 13:33:07 +00:00
|
|
|
|
|
|
|
// ErrUnknownEngineFormat is returned when the engine format is
|
|
|
|
// unknown. ErrUnknownEngineFormat is currently returned if a format
|
|
|
|
// other than tsm1 is encountered.
|
|
|
|
ErrUnknownEngineFormat = errors.New("unknown engine format")
|
2015-07-22 14:53:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Engine represents a swappable storage engine for the shard.
|
|
|
|
type Engine interface {
|
|
|
|
Open() error
|
|
|
|
Close() error
|
2016-09-14 13:55:44 +00:00
|
|
|
SetEnabled(enabled bool)
|
2016-12-01 18:26:23 +00:00
|
|
|
WithLogger(zap.Logger)
|
2016-09-14 13:55:44 +00:00
|
|
|
|
2016-10-04 18:45:09 +00:00
|
|
|
LoadMetadataIndex(shardID uint64, index Index) error
|
2015-07-22 14:53:20 +00:00
|
|
|
|
2016-09-14 13:55:44 +00:00
|
|
|
CreateSnapshot() (string, error)
|
2016-04-29 00:29:09 +00:00
|
|
|
Backup(w io.Writer, basePath string, since time.Time) error
|
|
|
|
Restore(r io.Reader, basePath string) error
|
2017-04-26 23:16:59 +00:00
|
|
|
Import(r io.Reader, basePath string) error
|
2016-04-29 00:29:09 +00:00
|
|
|
|
2016-11-23 20:32:42 +00:00
|
|
|
CreateIterator(measurement string, opt influxql.IteratorOptions) (influxql.Iterator, error)
|
2016-04-01 19:30:09 +00:00
|
|
|
WritePoints(points []models.Point) error
|
2016-09-13 12:04:18 +00:00
|
|
|
|
2016-11-21 15:21:58 +00:00
|
|
|
CreateSeriesIfNotExists(key, name []byte, tags models.Tags) error
|
2016-12-22 18:50:56 +00:00
|
|
|
CreateSeriesListIfNotExists(keys, names [][]byte, tags []models.Tags) error
|
2016-09-29 09:39:13 +00:00
|
|
|
DeleteSeriesRange(keys [][]byte, min, max int64) error
|
2016-09-21 15:04:37 +00:00
|
|
|
|
2016-09-23 13:33:47 +00:00
|
|
|
SeriesSketches() (estimator.Sketch, estimator.Sketch, error)
|
|
|
|
MeasurementsSketches() (estimator.Sketch, estimator.Sketch, error)
|
2016-11-29 12:26:52 +00:00
|
|
|
SeriesN() int64
|
2016-09-13 12:04:18 +00:00
|
|
|
|
2017-01-02 16:29:18 +00:00
|
|
|
MeasurementExists(name []byte) (bool, error)
|
2016-12-05 17:51:06 +00:00
|
|
|
MeasurementNamesByExpr(expr influxql.Expr) ([][]byte, error)
|
2016-11-11 16:25:53 +00:00
|
|
|
MeasurementNamesByRegex(re *regexp.Regexp) ([][]byte, error)
|
2016-04-01 19:30:09 +00:00
|
|
|
MeasurementFields(measurement string) *MeasurementFields
|
2016-12-15 15:31:18 +00:00
|
|
|
ForEachMeasurementName(fn func(name []byte) error) error
|
2016-12-05 17:51:06 +00:00
|
|
|
DeleteMeasurement(name []byte) error
|
|
|
|
|
|
|
|
// TagKeys(name []byte) ([][]byte, error)
|
2017-01-24 16:27:47 +00:00
|
|
|
HasTagKey(name, key []byte) (bool, error)
|
2016-12-06 17:30:41 +00:00
|
|
|
MeasurementTagKeysByExpr(name []byte, expr influxql.Expr) (map[string]struct{}, error)
|
2016-12-05 17:51:06 +00:00
|
|
|
ForEachMeasurementTagKey(name []byte, fn func(key []byte) error) error
|
2017-03-24 15:48:10 +00:00
|
|
|
TagKeyCardinality(name, key []byte) int
|
2015-09-29 02:50:00 +00:00
|
|
|
|
2016-11-30 19:45:14 +00:00
|
|
|
// InfluxQL iterators
|
|
|
|
MeasurementSeriesKeysByExpr(name []byte, condition influxql.Expr) ([][]byte, error)
|
2016-12-05 17:51:06 +00:00
|
|
|
ForEachMeasurementSeriesByExpr(name []byte, expr influxql.Expr, fn func(tags models.Tags) error) error
|
2016-11-28 16:59:36 +00:00
|
|
|
SeriesPointIterator(opt influxql.IteratorOptions) (influxql.Iterator, error)
|
|
|
|
|
2016-07-07 16:13:56 +00:00
|
|
|
// Statistics will return statistics relevant to this engine.
|
|
|
|
Statistics(tags map[string]string) []models.Statistic
|
2016-11-21 22:40:00 +00:00
|
|
|
LastModified() time.Time
|
2016-07-07 16:13:56 +00:00
|
|
|
|
2015-09-03 16:48:37 +00:00
|
|
|
io.WriterTo
|
2015-07-22 14:53:20 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 20:04:18 +00:00
|
|
|
// EngineFormat represents the format for an engine.
|
2015-09-29 02:50:00 +00:00
|
|
|
type EngineFormat int
|
|
|
|
|
|
|
|
const (
|
2016-02-10 20:04:18 +00:00
|
|
|
// TSM1Format is the format used by the tsm1 engine.
|
2015-11-04 21:06:06 +00:00
|
|
|
TSM1Format EngineFormat = 2
|
2015-09-29 02:50:00 +00:00
|
|
|
)
|
|
|
|
|
2015-07-22 14:53:20 +00:00
|
|
|
// NewEngineFunc creates a new engine.
|
2016-11-15 16:20:00 +00:00
|
|
|
type NewEngineFunc func(id uint64, i Index, path string, walPath string, options EngineOptions) Engine
|
2015-07-22 14:53:20 +00:00
|
|
|
|
|
|
|
// newEngineFuncs is a lookup of engine constructors by name.
|
|
|
|
var newEngineFuncs = make(map[string]NewEngineFunc)
|
|
|
|
|
|
|
|
// RegisterEngine registers a storage engine initializer by name.
|
|
|
|
func RegisterEngine(name string, fn NewEngineFunc) {
|
|
|
|
if _, ok := newEngineFuncs[name]; ok {
|
|
|
|
panic("engine already registered: " + name)
|
|
|
|
}
|
|
|
|
newEngineFuncs[name] = fn
|
|
|
|
}
|
|
|
|
|
2015-10-27 18:57:21 +00:00
|
|
|
// RegisteredEngines returns the slice of currently registered engines.
|
|
|
|
func RegisteredEngines() []string {
|
|
|
|
a := make([]string, 0, len(newEngineFuncs))
|
2016-02-10 20:04:18 +00:00
|
|
|
for k := range newEngineFuncs {
|
2015-10-27 18:57:21 +00:00
|
|
|
a = append(a, k)
|
|
|
|
}
|
|
|
|
sort.Strings(a)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2015-07-22 14:53:20 +00:00
|
|
|
// NewEngine returns an instance of an engine based on its format.
|
|
|
|
// If the path does not exist then the DefaultFormat is used.
|
2016-11-15 16:20:00 +00:00
|
|
|
func NewEngine(id uint64, i Index, path string, walPath string, options EngineOptions) (Engine, error) {
|
2015-07-22 14:53:20 +00:00
|
|
|
// Create a new engine
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
2016-11-15 16:20:00 +00:00
|
|
|
return newEngineFuncs[options.EngineVersion](id, i, path, walPath, options), nil
|
2015-07-22 14:53:20 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 21:06:06 +00:00
|
|
|
// If it's a dir then it's a tsm1 engine
|
2016-06-21 03:41:07 +00:00
|
|
|
format := DefaultEngine
|
2015-11-04 21:06:06 +00:00
|
|
|
if fi, err := os.Stat(path); err != nil {
|
2015-07-22 14:53:20 +00:00
|
|
|
return nil, err
|
2015-11-04 21:06:06 +00:00
|
|
|
} else if !fi.Mode().IsDir() {
|
2016-02-24 13:33:07 +00:00
|
|
|
return nil, ErrUnknownEngineFormat
|
2015-11-04 21:06:06 +00:00
|
|
|
} else {
|
|
|
|
format = "tsm1"
|
2015-07-22 14:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup engine by format.
|
|
|
|
fn := newEngineFuncs[format]
|
|
|
|
if fn == nil {
|
|
|
|
return nil, fmt.Errorf("invalid engine format: %q", format)
|
|
|
|
}
|
|
|
|
|
2016-11-15 16:20:00 +00:00
|
|
|
return fn(id, i, path, walPath, options), nil
|
2015-07-22 14:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EngineOptions represents the options used to initialize the engine.
|
|
|
|
type EngineOptions struct {
|
2016-03-20 19:33:45 +00:00
|
|
|
EngineVersion string
|
2016-11-16 18:57:55 +00:00
|
|
|
IndexVersion string
|
2016-10-20 19:15:31 +00:00
|
|
|
ShardID uint64
|
2016-11-16 18:57:55 +00:00
|
|
|
InmemIndex interface{} // shared in-memory index
|
2015-08-18 20:59:54 +00:00
|
|
|
|
|
|
|
Config Config
|
2015-07-22 14:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEngineOptions returns the default options.
|
|
|
|
func NewEngineOptions() EngineOptions {
|
|
|
|
return EngineOptions{
|
2016-03-20 19:33:45 +00:00
|
|
|
EngineVersion: DefaultEngine,
|
2016-11-16 18:57:55 +00:00
|
|
|
IndexVersion: DefaultIndex,
|
2016-03-20 19:33:45 +00:00
|
|
|
Config: NewConfig(),
|
2015-07-22 14:53:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-19 16:57:05 +00:00
|
|
|
|
|
|
|
// NewInmemIndex returns a new "inmem" index type.
|
|
|
|
var NewInmemIndex func(name string) (interface{}, error)
|