refactor: replace builtin package with fluxinit/static (#3304) (#19909)

A static initialization is not desirable in the main binaries, as it forces all
paths of code to init, but it is still useful in tests. It allows static
intialization to be performed once for all tests and eliminates the need to
always add the FluxInit call. Added a fluxinit/static package that calls
fluxinit.FluxInit() to replace the builtin package. This hides the nature of
the initialization and makes it clear that it is mandatory initialization code
getting called.
pull/19918/head
Adrian Thurston 2020-11-05 16:54:28 -08:00 committed by GitHub
parent a849bfdef3
commit 30ad7281aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 37 additions and 40 deletions

View File

@ -13,7 +13,7 @@ import (
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/kv/migration/all"
"github.com/influxdata/influxdb/v2/mock"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/pkg/errors"
"go.uber.org/zap/zaptest"
)

View File

@ -8,7 +8,7 @@ import (
"github.com/influxdata/influxdb/v2/inmem"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/kv/migration/all"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"go.uber.org/zap/zaptest"
)

View File

@ -10,7 +10,7 @@ import (
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/cmd/influxd/launcher"
"github.com/influxdata/influxdb/v2/http"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
)
// Default context.

View File

@ -1,3 +1,9 @@
// This package imports all the influxdb-specific query builtins. From influxdb
// we must use this package and not the init package provided by flux.
//
// This package is used for initializing with a function call. As a
// convenience, the fluxinit/static package can be imported for use cases where
// static initialization is okay, such as tests.
package fluxinit
import (
@ -7,14 +13,9 @@ import (
_ "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.
// The FluxInit() function 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.
func FluxInit() {
runtime.FinalizeBuiltIns()
}

11
fluxinit/static/static.go Normal file
View File

@ -0,0 +1,11 @@
// The fluxinit/static package can be imported in test cases and other uses
// cases where it is okay to always initialize flux.
package static
import (
"github.com/influxdata/influxdb/v2/fluxinit"
)
func init() {
fluxinit.FluxInit()
}

View File

@ -20,7 +20,7 @@ import (
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/query"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
)
var cmpOptions = cmp.Options{

View File

@ -21,7 +21,7 @@ import (
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/label"
"github.com/influxdata/influxdb/v2/mock"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/tenant"
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
"go.uber.org/zap"

View File

@ -15,7 +15,7 @@ import (
"github.com/influxdata/influxdb/v2/kit/feature"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/mock"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"github.com/influxdata/influxdb/v2/task/options"
"github.com/influxdata/influxdb/v2/task/servicetest"

View File

@ -14,7 +14,7 @@ import (
"github.com/influxdata/influxdb/v2/kv/migration/all"
"github.com/influxdata/influxdb/v2/mock"
endpointservice "github.com/influxdata/influxdb/v2/notification/endpoint/service"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"github.com/influxdata/influxdb/v2/tenant"
"go.uber.org/zap/zaptest"

View File

@ -1,15 +0,0 @@
// Package builtin ensures all packages related to Flux built-ins are imported and initialized.
// This should only be imported from main or test packages.
// It is a mistake to import it from any other package.
package builtin
import (
"github.com/influxdata/flux/runtime"
_ "github.com/influxdata/flux/stdlib" // Import the stdlib
_ "github.com/influxdata/influxdb/v2/query/stdlib" // Import the stdlib
)
func init() {
runtime.FinalizeBuiltIns()
}

View File

@ -23,7 +23,7 @@ import (
"github.com/influxdata/influxdb/v2/kit/feature"
pmock "github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/query"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/control"
"github.com/influxdata/influxdb/v2/query/stdlib/influxdata/influxdb"
"github.com/opentracing/opentracing-go"

View File

@ -19,7 +19,7 @@ import (
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/query"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/influxql"
"github.com/influxdata/influxdb/v2/query/querytest"
platformtesting "github.com/influxdata/influxdb/v2/testing"

View File

@ -3,7 +3,7 @@ package promqltests
import (
"testing"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
)
func TestPromQLEndToEnd(t *testing.T) {

View File

@ -15,7 +15,7 @@ import (
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/models"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/stdlib/experimental"
"github.com/influxdata/influxdb/v2/query/stdlib/influxdata/influxdb"
)

View File

@ -14,7 +14,7 @@ import (
"github.com/influxdata/flux/values/valuestest"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/models"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/stdlib/influxdata/influxdb"
)

View File

@ -17,7 +17,7 @@ import (
"github.com/influxdata/influxdb/v2/kv/migration/all"
"github.com/influxdata/influxdb/v2/mock"
"github.com/influxdata/influxdb/v2/query"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/control"
"github.com/influxdata/influxdb/v2/query/fluxlang"
stdlib "github.com/influxdata/influxdb/v2/query/stdlib/influxdata/influxdb"

View File

@ -17,7 +17,7 @@ import (
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kv"
"github.com/influxdata/influxdb/v2/query"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
)
type fakeQueryService struct {

View File

@ -9,7 +9,7 @@ import (
"github.com/influxdata/influxdb/v2"
pmock "github.com/influxdata/influxdb/v2/mock"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/snowflake"
"github.com/influxdata/influxdb/v2/task/backend"
"github.com/influxdata/influxdb/v2/task/backend/coordinator"

View File

@ -11,7 +11,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/influxdata/flux/ast"
"github.com/influxdata/influxdb/v2/pkg/pointer"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"github.com/influxdata/influxdb/v2/task/options"
)

View File

@ -9,7 +9,7 @@ import (
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/feature"
"github.com/influxdata/influxdb/v2/mock"
_ "github.com/influxdata/influxdb/v2/query/builtin"
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
"github.com/influxdata/influxdb/v2/query/fluxlang"
"github.com/influxdata/influxdb/v2/task/options"
)