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: init tidb new extension framework #38497

Merged
merged 17 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions br/pkg/glue/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//br/pkg/logutil",
"//ddl",
"//domain",
"//kv",
"//parser/model",
Expand Down
2 changes: 2 additions & 0 deletions br/pkg/restore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"//br/pkg/metautil",
"//br/pkg/pdutil",
"//br/pkg/redact",
"//br/pkg/restore/prealloc_table_id",
"//br/pkg/restore/split",
"//br/pkg/restore/tiflashrec",
"//br/pkg/rtree",
Expand All @@ -41,6 +42,7 @@ go_library(
"//br/pkg/summary",
"//br/pkg/utils",
"//config",
"//ddl",
"//ddl/util",
"//domain",
"//kv",
Expand Down
20 changes: 20 additions & 0 deletions br/pkg/restore/prealloc_table_id/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "prealloc_table_id",
srcs = ["alloc.go"],
importpath = "github.com/pingcap/tidb/br/pkg/restore/prealloc_table_id",
visibility = ["//visibility:public"],
deps = ["//br/pkg/metautil"],
)

go_test(
name = "prealloc_table_id_test",
srcs = ["alloc_test.go"],
deps = [
":prealloc_table_id",
"//br/pkg/metautil",
"//parser/model",
"@com_github_stretchr_testify//require",
],
)
12 changes: 8 additions & 4 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@
"server/conn_stmt.go": "server/conn_stmt.go",
"server/conn_test.go": "server/conn_test.go",
"planner/core/plan.go": "planner/core/plan.go",
"errno/": "only table code"
"errno/": "only table code",
"extension/": "extension code"
}
},
"gofmt": {
Expand Down Expand Up @@ -332,7 +333,8 @@
"kv/": "kv code",
"util/memory": "util/memory",
"ddl/": "ddl",
"planner/": "planner"
"planner/": "planner",
"extension/": "extension code"
}
},
"pkgfact": {
Expand Down Expand Up @@ -399,7 +401,8 @@
"planner/core/util.go": "planner/core/util.go",
"util/": "util code",
"parser/": "parser code",
"meta/": "parser code"
"meta/": "parser code",
"extension/": "extension code"
}
},
"shift": {
Expand Down Expand Up @@ -757,7 +760,8 @@
"planner/core/plan.go": "planner/core/plan.go",
"server/conn.go": "server/conn.go",
"server/conn_stmt.go": "server/conn_stmt.go",
"server/conn_test.go": "server/conn_test.go"
"server/conn_test.go": "server/conn_test.go",
"extension/": "extension code"
}
},
"SA2000": {
Expand Down
29 changes: 29 additions & 0 deletions extension/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "extension",
srcs = [
"extensions.go",
"manifest.go",
"registry.go",
"util.go",
],
importpath = "github.com/pingcap/tidb/extension",
visibility = ["//visibility:public"],
deps = [
"//sessionctx/variable",
"@com_github_pingcap_errors//:errors",
],
)

go_test(
name = "extension_test",
srcs = ["registry_test.go"],
deps = [
":extension",
"//privilege/privileges",
"//sessionctx/variable",
"@com_github_pingcap_errors//:errors",
"@com_github_stretchr_testify//require",
],
)
30 changes: 30 additions & 0 deletions extension/extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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

// Extensions contains all extensions that have already setup
type Extensions struct {
manifests []*Manifest
}

// Manifests returns a extension manifests
func (es *Extensions) Manifests() []*Manifest {
if es == nil {
return nil
}
manifests := make([]*Manifest, len(es.manifests))
copy(manifests, es.manifests)
return manifests
}
136 changes: 136 additions & 0 deletions extension/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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 (
"github.com/pingcap/errors"
"github.com/pingcap/tidb/sessionctx/variable"
)

// Option represents an option to initialize an extension
type Option func(m *Manifest)

// WithCustomSysVariables specifies custom variables of an extension
func WithCustomSysVariables(vars []*variable.SysVar) Option {
return func(m *Manifest) {
m.sysVariables = vars
}
}

// WithCustomDynPrivs specifies dynamic privileges of an extension
func WithCustomDynPrivs(privs []string) Option {
return func(m *Manifest) {
m.dynPrivs = privs
}
}

// WithClose specifies the close function of an extension.
// It will be invoked when `extension.Reset` is called
func WithClose(fn func()) Option {
return func(m *Manifest) {
m.close = fn
}
}

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

// Name returns the extension's name
func (m *Manifest) Name() string {
return m.name
}

func newManifestWithSetup(name string, factory func() ([]Option, error)) (_ *Manifest, _ func(), err error) {
clearBuilder := &clearFuncBuilder{}
defer func() {
if err != nil {
clearBuilder.Build()()
}
}()

// new manifest with factory
m := &Manifest{name: name}
err = clearBuilder.DoWithCollectClear(func() (func(), error) {
options, err := factory()
if err != nil {
return nil, err
}

for _, opt := range options {
opt(m)
}

return m.close, nil
})

if err != nil {
return nil, nil, err
}

// setup dynamic privileges
for i := range m.dynPrivs {
priv := m.dynPrivs[i]
err = clearBuilder.DoWithCollectClear(func() (func(), error) {
if err = RegisterDynamicPrivilege(priv); err != nil {
return nil, err
}
return func() {
RemoveDynamicPrivilege(priv)
}, nil
})
if err != nil {
return nil, nil, err
}
}

// setup sys vars
for i := range m.sysVariables {
sysVar := m.sysVariables[i]
err = clearBuilder.DoWithCollectClear(func() (func(), error) {
if sysVar == nil {
return nil, errors.New("system var should not be nil")
}

if sysVar.Name == "" {
return nil, errors.New("system var name should not be empty")
}

if variable.GetSysVar(sysVar.Name) != nil {
return nil, errors.Errorf("system var '%s' has already registered", sysVar.Name)
}

variable.RegisterSysVar(sysVar)
return func() {
variable.UnregisterSysVar(sysVar.Name)
}, nil
})

if err != nil {
return nil, nil, err
}
}
return m, clearBuilder.Build(), nil
}

// RegisterDynamicPrivilege is used to resolve dependency cycle
var RegisterDynamicPrivilege func(string) error

// RemoveDynamicPrivilege is used to resolve dependency cycle
var RemoveDynamicPrivilege func(string) bool
Loading