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

extension: support bootstrap for extension #38589

Merged
merged 8 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 10 additions & 2 deletions extension/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//sessionctx/variable",
"//util/chunk",
"@com_github_pingcap_errors//:errors",
],
)

go_test(
name = "extension_test",
srcs = ["registry_test.go"],
srcs = [
"bootstrap_test.go",
"main_test.go",
"registry_test.go",
],
embed = [":extension"],
deps = [
":extension",
"//privilege/privileges",
"//sessionctx/variable",
"//testkit",
"//testkit/testsetup",
"@com_github_pingcap_errors//:errors",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
],
)
48 changes: 48 additions & 0 deletions extension/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package extension_test

import (
"testing"

"github.com/pingcap/tidb/extension"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)

func TestBootstrap(t *testing.T) {
defer func() {
extension.Reset()
}()

extension.Reset()
require.NoError(t, extension.Register("test1", extension.WithBootstrapSQL("create table test.t1 (a int)")))
require.NoError(t, extension.Register("test2", extension.WithBootstrap(func(ctx extension.BootstrapContext) error {
_, err := ctx.ExecuteSQL(ctx, "insert into test.t1 values(1)")
require.NoError(t, err)

rows, err := ctx.ExecuteSQL(ctx, "select * from test.t1 where a=1")
require.NoError(t, err)

require.Equal(t, 1, len(rows))
require.Equal(t, int64(1), rows[0].GetInt64(0))
return nil
})))
require.NoError(t, extension.Setup())

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustQuery("select * from test.t1").Check(testkit.Rows("1"))
}
15 changes: 15 additions & 0 deletions extension/extensionimpl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "extensionimpl",
srcs = ["bootstrap.go"],
importpath = "github.com/pingcap/tidb/extension/extensionimpl",
visibility = ["//visibility:public"],
deps = [
"//domain",
"//extension",
"//kv",
"//util/chunk",
"//util/sqlexec",
],
)
78 changes: 78 additions & 0 deletions extension/extensionimpl/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package extensionimpl

import (
"context"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/extension"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/sqlexec"
)

type bootstrapContext struct {
context.Context
sqlExecutor sqlexec.SQLExecutor
}

func (c *bootstrapContext) ExecuteSQL(ctx context.Context, sql string) (rows []chunk.Row, err error) {
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnBootstrap)
rs, err := c.sqlExecutor.ExecuteInternal(ctx, sql)
if err != nil {
return nil, err
}

if rs == nil {
return nil, nil
}

defer func() {
closeErr := rs.Close()
if err == nil {
err = closeErr
}
}()

return sqlexec.DrainRecordSet(ctx, rs, 8)
}

// Bootstrap bootstrap all extensions
func Bootstrap(ctx context.Context, do *domain.Domain) error {
extensions, err := extension.GetExtensions()
if err != nil {
return err
}

if extensions == nil {
return nil
}

pool := do.SysSessionPool()
sctx, err := pool.Get()
if err != nil {
return err
}
defer pool.Put(sctx)

executor, ok := sctx.(sqlexec.SQLExecutor)
if !ok {
return errors.Errorf("type '%T' cannot be casted to 'sqlexec.SQLExecutor'", sctx)
}

return extensions.Bootstrap(&bootstrapContext{ctx, executor})
}
16 changes: 16 additions & 0 deletions extension/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ func (es *Extensions) Manifests() []*Manifest {
copy(manifests, es.manifests)
return manifests
}

// Bootstrap bootstrap all extensions
func (es *Extensions) Bootstrap(ctx BootstrapContext) error {
if es == nil {
return nil
}

for _, m := range es.manifests {
if m.bootstrap != nil {
if err := m.bootstrap(ctx); err != nil {
return err
}
}
}
return nil
}
31 changes: 31 additions & 0 deletions extension/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package extension

import (
"testing"

"github.com/pingcap/tidb/testkit/testsetup"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testsetup.SetupForCommonTest()
opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
}
goleak.VerifyTestMain(m, opts...)
}
30 changes: 30 additions & 0 deletions extension/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package extension

import (
"context"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
)

// Option represents an option to initialize an extension
Expand Down Expand Up @@ -44,11 +47,38 @@ func WithClose(fn func()) Option {
}
}

// BootstrapContext is the context used by extension in bootstrap
type BootstrapContext interface {
context.Context
// ExecuteSQL is used to execute a sql
ExecuteSQL(ctx context.Context, sql string) ([]chunk.Row, error)
}

// WithBootstrap specifies the bootstrap func of an extension
func WithBootstrap(fn func(BootstrapContext) error) Option {
return func(m *Manifest) {
m.bootstrap = fn
}
}

// WithBootstrapSQL the bootstrap SQL list
func WithBootstrapSQL(sqlList ...string) Option {
return WithBootstrap(func(ctx BootstrapContext) error {
for _, sql := range sqlList {
if _, err := ctx.ExecuteSQL(ctx, sql); err != nil {
return err
}
}
return nil
})
}

// Manifest is an extension's manifest
type Manifest struct {
name string
sysVariables []*variable.SysVar
dynPrivs []string
bootstrap func(BootstrapContext) error
close func()
}

Expand Down
36 changes: 25 additions & 11 deletions extension/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,30 @@ type registry struct {
close func()
}

// Setup sets up the extensions
func (r *registry) Setup() error {
r.Lock()
defer r.Unlock()

if _, err := r.doSetup(); err != nil {
return err
}
return nil
}

// Extensions returns the extensions after setup
func (r *registry) Extensions() (*Extensions, error) {
r.RLock()
defer r.RUnlock()
if !r.setup {
return nil, errors.New("The extensions has not been setup")
if r.setup {
extensions := r.extensions
r.RUnlock()
return extensions, nil
}
return r.extensions, nil
r.RUnlock()

r.Lock()
defer r.Unlock()
return r.doSetup()
}

// RegisterFactory registers a new extension with a factory
Expand Down Expand Up @@ -68,17 +84,15 @@ func (r *registry) RegisterFactory(name string, factory func() ([]Option, error)
}

// Setup setups all extensions
func (r *registry) Setup() (err error) {
r.Lock()
defer r.Unlock()
func (r *registry) doSetup() (_ *Extensions, err error) {
if r.setup {
return nil
return r.extensions, nil
}

if len(r.factories) == 0 {
r.extensions = nil
r.setup = true
return nil
return nil, nil
}

clearBuilder := &clearFuncBuilder{}
Expand All @@ -102,13 +116,13 @@ func (r *registry) Setup() (err error) {
})

if err != nil {
return err
return nil, err
}
}
r.extensions = &Extensions{manifests: manifests}
r.setup = true
r.close = clearBuilder.Build()
return nil
return r.extensions, nil
}

// Reset resets the registry. It is only used by test
Expand Down
1 change: 1 addition & 0 deletions session/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
"//errno",
"//executor",
"//expression",
"//extension/extensionimpl",
"//infoschema",
"//kv",
"//meta",
Expand Down
5 changes: 5 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/extension/extensionimpl"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
Expand Down Expand Up @@ -2888,6 +2889,10 @@ func BootstrapSession(store kv.Storage) (*domain.Domain, error) {
return nil, err
}

if err = extensionimpl.Bootstrap(context.Background(), dom); err != nil {
return nil, err
}

if len(cfg.Instance.PluginLoad) > 0 {
err := plugin.Init(context.Background(), plugin.Config{EtcdClient: dom.GetEtcdClient()})
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion tidb-server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ go_library(
"//util/deadlockhistory",
"//util/disk",
"//util/domainutil",
"//util/gctuner",
"//util/kvcache",
"//util/logutil",
"//util/memory",
Expand Down