forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: introduce plugin framework (pingcap#8788) (pingcap#9880)
- Loading branch information
Showing
12 changed files
with
963 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright 2018 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
var ( | ||
pkgDir string | ||
outDir string | ||
) | ||
|
||
const codeTemplate = ` | ||
package main | ||
import ( | ||
"github.com/pingcap/tidb/plugin" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
) | ||
func PluginManifest() *plugin.Manifest { | ||
return plugin.ExportManifest(&plugin.{{.kind}}Manifest{ | ||
Manifest: plugin.Manifest{ | ||
Kind: plugin.{{.kind}}, | ||
Name: "{{.name}}", | ||
Description: "{{.description}}", | ||
Version: {{.version}}, | ||
RequireVersion: map[string]uint16{}, | ||
License: "{{.license}}", | ||
BuildTime: "{{.buildTime}}", | ||
SysVars: map[string]*variable.SysVar{ | ||
{{range .sysVars}} | ||
"{{.name}}": { | ||
Scope: variable.Scope{{.scope}}, | ||
Name: "{{.name}}", | ||
Value: "{{.value}}", | ||
}, | ||
{{end}} | ||
}, | ||
Validate: {{.validate}}, | ||
OnInit: {{.onInit}}, | ||
OnShutdown: {{.onShutdown}}, | ||
}, | ||
{{range .export}} | ||
{{.extPoint}}: {{.impl}}, | ||
{{end}} | ||
}) | ||
} | ||
` | ||
|
||
func init() { | ||
flag.StringVar(&pkgDir, "pkg-dir", "", "plugin package folder path") | ||
flag.StringVar(&outDir, "out-dir", "", "plugin packaged folder path") | ||
flag.Usage = usage | ||
} | ||
|
||
func usage() { | ||
log.Printf("Usage: %s --pkg-dir [plugin source pkg folder] --outDir-dir [outDir-dir]\n", path.Base(os.Args[0])) | ||
flag.PrintDefaults() | ||
os.Exit(1) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
if pkgDir == "" || outDir == "" { | ||
flag.Usage() | ||
} | ||
var manifest map[string]interface{} | ||
_, err := toml.DecodeFile(filepath.Join(pkgDir, "manifest.toml"), &manifest) | ||
if err != nil { | ||
log.Printf("read pkg %s's manifest failure, %+v\n", pkgDir, err) | ||
os.Exit(1) | ||
} | ||
manifest["buildTime"] = time.Now().String() | ||
|
||
pluginName := manifest["name"].(string) | ||
if strings.Contains(pluginName, "-") { | ||
log.Printf("plugin name should not contain '-'\n") | ||
os.Exit(1) | ||
} | ||
if pluginName != filepath.Base(pkgDir) { | ||
log.Printf("plugin package must be same with plugin name in manifest file\n") | ||
os.Exit(1) | ||
} | ||
|
||
version := manifest["version"].(string) | ||
tmpl, err := template.New("gen-plugin").Parse(codeTemplate) | ||
if err != nil { | ||
log.Printf("generate code failure during parse template, %+v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
genFileName := filepath.Join(pkgDir, filepath.Base(pkgDir)+".gen.go") | ||
genFile, err := os.OpenFile(genFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) | ||
if err != nil { | ||
log.Printf("generate code failure during prepare output file, %+v\n", err) | ||
os.Exit(1) | ||
} | ||
defer func() { | ||
err1 := os.Remove(genFileName) | ||
if err1 != nil { | ||
log.Printf("remove tmp file %s failure, please clean up manually at %v", genFileName, err1) | ||
} | ||
}() | ||
|
||
err = tmpl.Execute(genFile, manifest) | ||
if err != nil { | ||
log.Printf("generate code failure during generating code, %+v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
outputFile := filepath.Join(outDir, pluginName+"-"+version+".so") | ||
pluginPath := `-pluginpath=` + pluginName + "-" + version | ||
ctx := context.Background() | ||
buildCmd := exec.CommandContext(ctx, "go", "build", | ||
"-ldflags", pluginPath, | ||
"-buildmode=plugin", | ||
"-o", outputFile, pkgDir) | ||
buildCmd.Stderr = os.Stderr | ||
buildCmd.Stdout = os.Stdout | ||
buildCmd.Env = append(os.Environ(), "GO111MODULE=on") | ||
err = buildCmd.Run() | ||
if err != nil { | ||
log.Printf("compile plugin source code failure, %+v\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Printf(`Package "%s" as plugin "%s" success.`+"\nManifest:\n", pkgDir, outputFile) | ||
encoder := json.NewEncoder(os.Stdout) | ||
encoder.SetIndent(" ", "\t") | ||
err = encoder.Encode(manifest) | ||
if err != nil { | ||
log.Printf("print manifest detail failure, err: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2019 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pingcap/tidb/plugin" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
) | ||
|
||
// Validate implements TiDB plugin's Validate SPI. | ||
func Validate(ctx context.Context, m *plugin.Manifest) error { | ||
fmt.Println("conn_ip_example validate called") | ||
return nil | ||
} | ||
|
||
// OnInit implements TiDB plugin's OnInit SPI. | ||
func OnInit(ctx context.Context, manifest *plugin.Manifest) error { | ||
fmt.Println("conn_ip_example init called") | ||
fmt.Println("read cfg in init", manifest.SysVars["conn_ip_example_test_variable"].Value) | ||
return nil | ||
} | ||
|
||
// OnShutdown implements TiDB plugin's OnShutdown SPI. | ||
func OnShutdown(ctx context.Context, manifest *plugin.Manifest) error { | ||
fmt.Println("conn_ip_examples hutdown called") | ||
return nil | ||
} | ||
|
||
// NotifyEvent implements TiDB Audit plugin's NotifyEvent SPI. | ||
func NotifyEvent(ctx context.Context) error { | ||
fmt.Println("conn_ip_example notifiy called") | ||
fmt.Println("variable test: ", variable.GetSysVar("conn_ip_example_test_variable").Value) | ||
fmt.Printf("new connection by %s\n", ctx.Value("ip")) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2019 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main_test | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/pingcap/tidb/plugin" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
) | ||
|
||
func Example_LoadRunShutdownPlugin() { | ||
ctx := context.Background() | ||
var pluginVarNames []string | ||
cfg := plugin.Config{ | ||
Plugins: []string{"conn_ip_example-1"}, | ||
PluginDir: "/home/robi/Code/go/src/github.com/pingcap/tidb/plugin/conn_ip_example", | ||
GlobalSysVar: &variable.SysVars, | ||
PluginVarNames: &pluginVarNames, | ||
} | ||
|
||
err := plugin.Init(ctx, cfg) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ps := plugin.GetByKind(plugin.Audit) | ||
for _, auditPlugin := range ps { | ||
if auditPlugin.State != plugin.Ready { | ||
continue | ||
} | ||
plugin.DeclareAuditManifest(auditPlugin.Manifest).NotifyEvent(context.Background(), nil) | ||
} | ||
|
||
err = plugin.Reload(ctx, cfg, plugin.ID("conn_ip_example-2")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, auditPlugin := range plugin.GetByKind(plugin.Audit) { | ||
if auditPlugin.State != plugin.Ready { | ||
continue | ||
} | ||
plugin.DeclareAuditManifest(auditPlugin.Manifest).NotifyEvent( | ||
context.WithValue(context.Background(), "ip", "1.1.1.2"), nil, | ||
) | ||
} | ||
|
||
plugin.Shutdown(context.Background()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name = "conn_ip_example" | ||
kind = "Audit" | ||
description = "just a test" | ||
version = "1" | ||
license = "" | ||
sysVars = [ | ||
{name="conn_ip_example_test_variable", scope="Global", value="2"}, | ||
{name="conn_ip_example_test_variable2", scope="Session", value="2"}, | ||
] | ||
validate = "Validate" | ||
onInit = "OnInit" | ||
onShutdown = "OnShutdown" | ||
export = [ | ||
{extPoint="NotifyEvent", impl="NotifyEvent"} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2019 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package plugin | ||
|
||
// Kind presents the kind of plugin. | ||
type Kind uint8 | ||
|
||
const ( | ||
// Audit indicates it is a Audit plugin. | ||
Audit Kind = 1 + iota | ||
// Authentication indicate it is a Authentication plugin. | ||
Authentication | ||
// Schema indicate a plugin that can change TiDB schema. | ||
Schema | ||
// Daemon indicate a plugin that can run as daemon task. | ||
Daemon | ||
) | ||
|
||
func (k Kind) String() (str string) { | ||
switch k { | ||
case Audit: | ||
str = "Audit" | ||
case Authentication: | ||
str = "Authentication" | ||
case Schema: | ||
str = "Schema" | ||
case Daemon: | ||
str = "Daemon" | ||
} | ||
return | ||
} | ||
|
||
// State present the state of plugin. | ||
type State uint8 | ||
|
||
const ( | ||
// Uninitialized indicates plugin is uninitialized. | ||
Uninitialized State = iota | ||
// Ready indicates plugin is ready to work. | ||
Ready | ||
// Dying indicates plugin will be close soon. | ||
Dying | ||
// Disable indicate plugin is disabled. | ||
Disable | ||
) | ||
|
||
func (s State) String() (str string) { | ||
switch s { | ||
case Uninitialized: | ||
str = "Uninitialized" | ||
case Ready: | ||
str = "Ready" | ||
case Dying: | ||
str = "Dying" | ||
case Disable: | ||
str = "Disable" | ||
} | ||
return | ||
} |
Oops, something went wrong.