Skip to content

Commit

Permalink
fix: use fluxinit package to init flux library instead of builtin (#1…
Browse files Browse the repository at this point in the history
…9887)

* fix: use fluxinit package to init flux library instead of builtin

The builtin package performs costly initialization work in the go init()
function, which causes the work to be performed for all paths of the main
executables that need it, including help screens. This patch uses the fluxinit
package, which requires a call to do the costly work. It allows help screens
and other code paths to execute quickly.

* fix: need to draw in the influxdb-specific standard library in fluxinit
  • Loading branch information
adrian-thurston authored Nov 3, 2020
1 parent 428a46b commit 37ef9d2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmd/influxd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/influxdata/influxdb/v2/chronograf/server"
"github.com/influxdata/influxdb/v2/cmd/influxd/inspect"
"github.com/influxdata/influxdb/v2/dbrp"
"github.com/influxdata/influxdb/v2/fluxinit"
"github.com/influxdata/influxdb/v2/gather"
"github.com/influxdata/influxdb/v2/http"
iqlcontrol "github.com/influxdata/influxdb/v2/influxql/control"
Expand Down Expand Up @@ -136,6 +137,8 @@ func NewInfluxdCommand(ctx context.Context, subCommands ...*cobra.Command) *cobr

func cmdRunE(ctx context.Context, l *Launcher) func() error {
return func() error {
fluxinit.FluxInit()

// exit with SIGINT and SIGTERM
ctx = signals.WithStandardSignals(ctx)

Expand Down
1 change: 0 additions & 1 deletion cmd/influxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/cmd/influxd/launcher"
"github.com/influxdata/influxdb/v2/cmd/influxd/upgrade"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/tsdb/engine/tsm1"
_ "github.com/influxdata/influxdb/v2/tsdb/index/tsi1"
"github.com/spf13/cobra"
Expand Down
9 changes: 9 additions & 0 deletions cmd/influxd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/bolt"
"github.com/influxdata/influxdb/v2/dbrp"
"github.com/influxdata/influxdb/v2/fluxinit"
"github.com/influxdata/influxdb/v2/internal/fs"
"github.com/influxdata/influxdb/v2/kit/cli"
"github.com/influxdata/influxdb/v2/kit/metric"
Expand Down Expand Up @@ -278,7 +279,15 @@ func (i *influxDBv2) close() error {
return nil
}

var fluxInitialized bool

func runUpgradeE(*cobra.Command, []string) error {
// This command is executed multiple times by test code. Initialization can happen only once.
if !fluxInitialized {
fluxinit.FluxInit()
fluxInitialized = true
}

ctx := context.Background()
config := zap.NewProductionConfig()
config.OutputPaths = append(config.OutputPaths, options.logPath)
Expand Down
2 changes: 2 additions & 0 deletions cmd/influxd/upgrade/v1_dump_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"path/filepath"
"text/tabwriter"

"github.com/influxdata/influxdb/v2/fluxinit"
"github.com/spf13/cobra"
)

var v1DumpMetaCommand = &cobra.Command{
Use: "v1-dump-meta",
Short: "Dump InfluxDB 1.x meta.db",
RunE: func(cmd *cobra.Command, args []string) error {
fluxinit.FluxInit()
svc, err := newInfluxDBv1(&v1DumpMetaOptions)
if err != nil {
return fmt.Errorf("error opening 1.x meta.db: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/influxd/upgrade/v2_dump_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/tabwriter"

"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/fluxinit"
"github.com/influxdata/influxdb/v2/internal/fs"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand All @@ -17,6 +18,7 @@ var v2DumpMetaCommand = &cobra.Command{
Use: "v2-dump-meta",
Short: "Dump InfluxDB 2.x influxd.bolt",
RunE: func(cmd *cobra.Command, args []string) error {
fluxinit.FluxInit()
ctx := context.Background()
svc, err := newInfluxDBv2(ctx, &v2DumpMetaOptions, zap.NewNop())
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions fluxinit/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fluxinit

import (
"github.com/influxdata/flux/runtime"

_ "github.com/influxdata/flux/stdlib"
_ "github.com/influxdata/influxdb/v2/query/stdlib" // Import the stdlib
)

// FluxInit() prepares the runtime for compilation and execution of flux. This
// is a costly step and should only be performed if the intention is to compile
// and execute flux code.
//
// Importing this package and calling FluxInit is equivalent to importing the
// "builtin" package. It draws in the standard library functions, which
// register themselves in init() functions, then performs the final steps
// necessary to prepare for executing flux.
func FluxInit() {
runtime.FinalizeBuiltIns()
}

0 comments on commit 37ef9d2

Please sign in to comment.