Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrated sqlite db for metadata #21543

Merged
merged 7 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
## unreleased

### SQLite Metadata Store

This release adds an embedded SQLite database for storing metadata required by the latest UI features like Notebooks and Annotations.

### Features

1. [19811](https://github.com/influxdata/influxdb/pull/19811): Add Geo graph type to be able to store in Dashboard cells.
1. [21218](https://github.com/influxdata/influxdb/pull/21218): Add the properties of a static legend for line graphs and band plots.
1. [21367](https://github.com/influxdata/influxdb/pull/21367): List users via the API now supports pagination
1. [21531](https://github.com/influxdata/influxdb/pull/21531): Remove feature flags for permanent UI features
1. [21543](https://github.com/influxdata/influxdb/pull/21543): Added `influxd` configuration flag `--sqlite-path` for specifying a user-defined path to the SQLite database file
1. [21543](https://github.com/influxdata/influxdb/pull/21543): Updated `influxd` configuration flag `--store` to work with string values `disk` or `memory`. Memory continues to store metadata in-memory for testing; `disk` will persist metadata to disk via bolt and SQLite

### Bug Fixes

Expand Down
12 changes: 10 additions & 2 deletions cmd/influxd/launcher/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
influxlogger "github.com/influxdata/influxdb/v2/logger"
"github.com/influxdata/influxdb/v2/nats"
"github.com/influxdata/influxdb/v2/pprof"
"github.com/influxdata/influxdb/v2/sqlite"
"github.com/influxdata/influxdb/v2/storage"
"github.com/influxdata/influxdb/v2/v1/coordinator"
"github.com/influxdata/influxdb/v2/vault"
Expand Down Expand Up @@ -127,6 +128,7 @@ type InfluxdOpts struct {

AssetsPath string
BoltPath string
SqLitePath string
EnginePath string

StoreType string
Expand Down Expand Up @@ -184,6 +186,7 @@ func NewOpts(viper *viper.Viper) *InfluxdOpts {
ReportingDisabled: false,

BoltPath: filepath.Join(dir, bolt.DefaultFilename),
SqLitePath: filepath.Join(dir, sqlite.DefaultFilename),
EnginePath: filepath.Join(dir, "engine"),

HttpBindAddress: ":8086",
Expand All @@ -197,7 +200,7 @@ func NewOpts(viper *viper.Viper) *InfluxdOpts {
ProfilingDisabled: false,
MetricsDisabled: false,

StoreType: BoltStore,
StoreType: DiskStore,
SecretStore: BoltStore,

NatsPort: nats.RandomPort,
Expand Down Expand Up @@ -237,6 +240,11 @@ func (o *InfluxdOpts) BindCliOpts() []cli.Opt {
Default: o.BoltPath,
Desc: "path to boltdb database",
},
{
DestP: &o.SqLitePath,
Flag: "sqlite-path",
Desc: fmt.Sprintf("path to sqlite database. if not set, sqlite database will be stored in the bolt-path directory as %q.", sqlite.DefaultFilename),
},
{
DestP: &o.AssetsPath,
Flag: "assets-path",
Expand All @@ -246,7 +254,7 @@ func (o *InfluxdOpts) BindCliOpts() []cli.Opt {
DestP: &o.StoreType,
Flag: "store",
Default: o.StoreType,
Desc: "backing store for REST resources (bolt or memory)",
Desc: "backing store for REST resources (disk or memory)",
},
{
DestP: &o.Testing,
Expand Down
72 changes: 57 additions & 15 deletions cmd/influxd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import (
"github.com/influxdata/influxdb/v2/session"
"github.com/influxdata/influxdb/v2/snowflake"
"github.com/influxdata/influxdb/v2/source"
"github.com/influxdata/influxdb/v2/sqlite"
sqliteMigrations "github.com/influxdata/influxdb/v2/sqlite/migrations"
"github.com/influxdata/influxdb/v2/storage"
storageflux "github.com/influxdata/influxdb/v2/storage/flux"
"github.com/influxdata/influxdb/v2/storage/readservice"
Expand Down Expand Up @@ -88,7 +90,9 @@ import (
)

const (
// BoltStore stores all REST resources in boltdb.
// DiskStore stores all REST resources to disk in boltdb and sqlite.
DiskStore = "disk"
// BoltStore also stores all REST resources to disk in boltdb and sqlite. Kept for backwards-compatibility.
BoltStore = "bolt"
// MemoryStore stores all REST resources in memory (useful for testing).
MemoryStore = "memory"
Expand All @@ -110,6 +114,7 @@ type Launcher struct {
boltClient *bolt.Client
kvStore kv.SchemaStore
kvService *kv.Service
sqlStore *sqlite.SqlStore

// storage engine
engine Engine
Expand Down Expand Up @@ -185,6 +190,12 @@ func (m *Launcher) Shutdown(ctx context.Context) error {
errs = append(errs, err.Error())
}

m.log.Info("Stopping", zap.String("service", "sqlite"))
if err := m.sqlStore.Close(); err != nil {
m.log.Error("Failed closing sqlite", zap.Error(err))
errs = append(errs, err.Error())
}

m.log.Info("Stopping", zap.String("service", "query"))
if err := m.queryController.Shutdown(ctx); err != nil && err != context.Canceled {
m.log.Error("Failed closing query service", zap.Error(err))
Expand Down Expand Up @@ -270,28 +281,51 @@ func (m *Launcher) run(ctx context.Context, opts *InfluxdOpts) (err error) {
var flushers flushers
switch opts.StoreType {
case BoltStore:
store := bolt.NewKVStore(m.log.With(zap.String("service", "kvstore-bolt")), opts.BoltPath)
store.WithDB(m.boltClient.DB())
m.kvStore = store
m.log.Warn("Using --store=bolt is deprecated. Use --store=disk instead.")
fallthrough
case DiskStore:
kvStore := bolt.NewKVStore(m.log.With(zap.String("service", "kvstore-bolt")), opts.BoltPath)
kvStore.WithDB(m.boltClient.DB())
m.kvStore = kvStore

// If a sqlite-path is not specified, store sqlite db in the same directory as bolt with the default filename.
if opts.SqLitePath == "" {
opts.SqLitePath = filepath.Dir(opts.BoltPath) + "/" + sqlite.DefaultFilename
danxmoran marked this conversation as resolved.
Show resolved Hide resolved
}
sqlStore, err := sqlite.NewSqlStore(opts.SqLitePath, m.log.With(zap.String("service", "sqlite")))
if err != nil {
m.log.Error("Failed opening sqlite store", zap.Error(err))
return err
}
m.sqlStore = sqlStore

if opts.Testing {
flushers = append(flushers, store)
flushers = append(flushers, kvStore, sqlStore)
}

case MemoryStore:
store := inmem.NewKVStore()
m.kvStore = store
kvStore := inmem.NewKVStore()
m.kvStore = kvStore

sqlStore, err := sqlite.NewSqlStore(sqlite.InmemPath, m.log.With(zap.String("service", "sqlite")))
if err != nil {
m.log.Error("Failed opening sqlite store", zap.Error(err))
return err
}
m.sqlStore = sqlStore

if opts.Testing {
flushers = append(flushers, store)
flushers = append(flushers, kvStore, sqlStore)
}

default:
err := fmt.Errorf("unknown store type %s; expected bolt or memory", opts.StoreType)
m.log.Error("Failed opening bolt", zap.Error(err))
err := fmt.Errorf("unknown store type %s; expected disk or memory", opts.StoreType)
m.log.Error("Failed opening metadata store", zap.Error(err))
return err
}

migrator, err := migration.NewMigrator(
m.log.With(zap.String("service", "migrations")),
boltMigrator, err := migration.NewMigrator(
m.log.With(zap.String("service", "bolt migrations")),
m.kvStore,
all.Migrations[:]...,
)
Expand All @@ -300,9 +334,17 @@ func (m *Launcher) run(ctx context.Context, opts *InfluxdOpts) (err error) {
return err
}

// apply migrations to metadata store
if err := migrator.Up(ctx); err != nil {
m.log.Error("Failed to apply migrations", zap.Error(err))
// apply migrations to the bolt metadata store
if err := boltMigrator.Up(ctx); err != nil {
m.log.Error("Failed to apply bolt migrations", zap.Error(err))
return err
}

sqliteMigrator := sqlite.NewMigrator(m.sqlStore, m.log.With(zap.String("service", "sqlite migrations")))

// apply migrations to the sqlite metadata store
if err := sqliteMigrator.Up(ctx, &sqliteMigrations.All{}); err != nil {
m.log.Error("Failed to apply sqlite migrations", zap.Error(err))
return err
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/influxd/launcher/launcher_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/influxdata/influxdb/v2/pkger"
"github.com/influxdata/influxdb/v2/query"
"github.com/influxdata/influxdb/v2/restore"
"github.com/influxdata/influxdb/v2/sqlite"
"github.com/influxdata/influxdb/v2/task/taskmodel"
"github.com/influxdata/influxdb/v2/tenant"
dto "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -125,6 +126,7 @@ func (tl *TestLauncher) Run(tb zaptest.TestingT, ctx context.Context, setters ..
}
opts.TestingAlwaysAllowSetup = true
opts.BoltPath = filepath.Join(tl.Path, bolt.DefaultFilename)
opts.SqLitePath = filepath.Join(tl.Path, sqlite.DefaultFilename)
opts.EnginePath = filepath.Join(tl.Path, "engine")
opts.HttpBindAddress = "127.0.0.1:0"
opts.LogLevel = zap.DebugLevel
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
github.com/kevinburke/go-bindata v3.11.0+incompatible
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/mattn/go-sqlite3 v1.14.7
github.com/matttproud/golang_protobuf_extensions v1.0.1
github.com/mileusna/useragent v0.0.0-20190129205925-3e331f0949a5
github.com/mna/pigeon v1.0.1-0.20180808201053-bb0192cfc2ae
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,9 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q=
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104 h1:d8RFOZ2IiFtFWBcKEHAFYJcPTf0wY5q0exFNJZVWa1U=
github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
Expand Down
13 changes: 13 additions & 0 deletions sqlite/migrations/0001_create_notebooks_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- The user_version should match the "000X" from the file name
-- Ex: 0001_create_notebooks_table should have a user_verison of 1
PRAGMA user_version=1;

-- Create the initial table to store notebooks
CREATE TABLE notebooks (
id TEXT NOT NULL PRIMARY KEY,
org_id TEXT NOT NULL,
name TEXT NOT NULL,
spec TEXT NOT NULL,
created_at TIMESTAMP,
updated_at TIMESTAMP
);
13 changes: 13 additions & 0 deletions sqlite/migrations/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:generate env GO111MODULE=on go run github.com/kevinburke/go-bindata/go-bindata -prefix "migrations/" -o ./migrations_gen.go -ignore README|go|conf -pkg migrations .
danxmoran marked this conversation as resolved.
Show resolved Hide resolved

package migrations

type All struct{}

func (s *All) ListNames() []string {
return AssetNames()
}

func (s *All) MustAssetString(n string) string {
return MustAssetString(n)
}
Loading