Skip to content

Commit

Permalink
*: another operator adapt PR (pingcap#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox committed Mar 7, 2023
1 parent b321bc1 commit ec41f4b
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 59 deletions.
7 changes: 4 additions & 3 deletions cmd/weirproxy/main.go → cmd/tiproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ package main

import (
"io/ioutil"
"os"

"github.com/pingcap/TiProxy/lib/config"
"github.com/pingcap/TiProxy/pkg/server"
"github.com/pingcap/TiProxy/lib/util/cmd"
"github.com/pingcap/TiProxy/lib/util/errors"
"github.com/pingcap/TiProxy/lib/util/waitgroup"
"github.com/pingcap/TiProxy/pkg/server"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

func main() {
rootCmd := &cobra.Command{
Use: "weirproxy",
Use: os.Args[0],
Short: "start the proxy server",
}

configFile := rootCmd.PersistentFlags().String("config", "conf/weirproxy.yaml", "weir proxy config file path")
configFile := rootCmd.PersistentFlags().String("config", "conf/proxy.yaml", "proxy config file path")
pubAddr := rootCmd.PersistentFlags().String("pub_addr", "127.0.0.1", "IP or domain, will be used as the accessible addr for other clients")
logEncoder := rootCmd.PersistentFlags().String("log_encoder", "", "log in format of tidb, console, or json")
logLevel := rootCmd.PersistentFlags().String("log_level", "", "log level")
Expand Down
4 changes: 4 additions & 0 deletions cmd/weirctl/main.go → cmd/tiproxyctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
package main

import (
"os"
"strings"

"github.com/pingcap/TiProxy/lib/cli"
"github.com/pingcap/TiProxy/lib/util/cmd"
)

func main() {
rootCmd := cli.GetRootCmd()
rootCmd.Use = strings.Replace(rootCmd.Use, "tiproxyctl", os.Args[0], 1)
cmd.RunRootCommand(rootCmd)
}
6 changes: 3 additions & 3 deletions conf/weirproxy.yaml → conf/proxy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ security:
# 2. useless/forbid: skip-ca
cluster-tls: # client object
# access to other components like TiDB or PD, will use this
skip-ca: true
# skip-ca: true
sql-tls: # client object
# access to TiDB sql port, it has a standalone TLS configuration
skip-ca: true
server-tls: # server object
# proxy SQL or HTTP port will use this
auto-certs: true
# auto-certs: true
peer-tls: # peer object
# internal communication between proxies
auto-certs: true
# auto-certs: true
advance:
# ignore-wrong-namespace: true
# peer-port: "3081"
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ ARG BUILDFLAGS
ARG GOPROXY
RUN export BUILDFLAGS=${BUILDFLAGS} && export GOPROXY=${GOPROXY} && cd proxy && make cmd && cp bin/* /bin/ && cp -a conf /etc/proxy && cd .. && rm -rf proxy
RUN rm -rf $(go env GOMODCACHE GOCACHE) && apk del git make go
ENTRYPOINT ["/bin/weirproxy", "-conf", "/etc/proxy/weirproxy.yaml"]
ENTRYPOINT ["/bin/tiproxy", "-conf", "/etc/proxy/proxy.yaml"]
43 changes: 34 additions & 9 deletions lib/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ const (
configPrefix = "/api/admin/config"
)

func GetConfigCmd(ctx *Context) *cobra.Command {
func GetConfigProxyCmd(ctx *Context) *cobra.Command {
rootCmd := &cobra.Command{
Use: "config",
Short: "",
Use: "proxy",
}

// config proxy
// set config proxy
{
configProxy := &cobra.Command{
Use: "proxy",
setProxy := &cobra.Command{
Use: "set",
}
input := configProxy.Flags().String("input", "", "specify the input json file for proxy config")
configProxy.RunE = func(cmd *cobra.Command, args []string) error {
input := setProxy.Flags().String("input", "", "specify the input json file for proxy config")
setProxy.RunE = func(cmd *cobra.Command, args []string) error {
var b io.Reader
if *input != "" {
f, err := os.Open(*input)
Expand All @@ -59,8 +58,34 @@ func GetConfigCmd(ctx *Context) *cobra.Command {
cmd.Println(resp)
return nil
}
rootCmd.AddCommand(configProxy)
rootCmd.AddCommand(setProxy)
}

// get config proxy
{
getProxy := &cobra.Command{
Use: "get",
}
getProxy.RunE = func(cmd *cobra.Command, args []string) error {
resp, err := doRequest(cmd.Context(), ctx, http.MethodGet, fmt.Sprintf("%s/proxy", configPrefix), nil)
if err != nil {
return err
}

cmd.Println(resp)
return nil
}
rootCmd.AddCommand(getProxy)
}

return rootCmd
}

func GetConfigCmd(ctx *Context) *cobra.Command {
rootCmd := &cobra.Command{
Use: "config",
Short: "",
}
rootCmd.AddCommand(GetConfigProxyCmd(ctx))
return rootCmd
}
2 changes: 1 addition & 1 deletion lib/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

func GetRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "weirctl",
Use: "tiproxyctl",
Short: "cli",
}

Expand Down
9 changes: 1 addition & 8 deletions lib/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/pingcap/TiProxy/lib

go 1.19
go 1.16

require (
github.com/pingcap/log v1.1.0
Expand All @@ -13,16 +13,9 @@ require (
)

require (
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pingcap/errors v0.11.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.7.0 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
)
18 changes: 18 additions & 0 deletions lib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/etcd/client/pkg/v3 v3.5.4 h1:lrneYvz923dvC14R54XcA7FXoZ3mlGZAgmwhfm7HqOg=
go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
Expand All @@ -56,18 +58,34 @@ go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
2 changes: 1 addition & 1 deletion lib/util/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (e *Error) Is(target error) bool {
return errors.Is(e.err, target)
}

func (e *Error) As(target any) bool {
func (e *Error) As(target interface{}) bool {
return errors.As(e.err, target)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func New(text string) error {
return errors.New(text)
}

func Errorf(format string, args ...any) error {
func Errorf(format string, args ...interface{}) error {
return fmt.Errorf(format, args...)
}

func Is(err, target error) bool {
return errors.Is(err, target)
}

func As(err error, target any) bool {
func As(err error, target interface{}) bool {
return errors.As(err, target)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/util/errors/werror.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Wrap(cerr error, uerr error) error {
}

// Wrapf is like Wrap, with the underlying error being the result of `fmt.Errorf()`
func Wrapf(cerr error, msg string, args ...any) error {
func Wrapf(cerr error, msg string, args ...interface{}) error {
if cerr == nil {
return nil
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/manager/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func NewConfigManager() *ConfigManager {
}
}

func (srv *ConfigManager) Init(ctx context.Context, kv mvcc.WatchableKV, cfg config.Advance, logger *zap.Logger) error {
func (srv *ConfigManager) Init(ctx context.Context, kv mvcc.WatchableKV, cfg *config.Config, logger *zap.Logger) error {
srv.logger = logger
srv.ignoreWrongNamespace = cfg.IgnoreWrongNamespace
srv.ignoreWrongNamespace = cfg.Advance.IgnoreWrongNamespace
// slash appended to distinguish '/dir'(file) and '/dir/'(directory)
srv.basePath = appendSlashToDirPath(DefaultEtcdPath)

Expand All @@ -72,9 +72,7 @@ func (srv *ConfigManager) Init(ctx context.Context, kv mvcc.WatchableKV, cfg con
ctx, cancel := context.WithCancel(ctx)
srv.cancel = cancel

srv.initProxyConfig(ctx)

return nil
return srv.watchCfgProxy(ctx, cfg)
}

func (e *ConfigManager) watch(ctx context.Context, ns, key string, f func(*zap.Logger, mvccpb.Event)) {
Expand All @@ -85,7 +83,7 @@ func (e *ConfigManager) watch(ctx context.Context, ns, key string, f func(*zap.L
wch := e.kv.NewWatchStream()
defer wch.Close()
for {
if _, err := wch.Watch(mvcc.AutoWatchID, wkey, getPrefix(wkey), wch.Rev()-1); err == nil {
if _, err := wch.Watch(mvcc.AutoWatchID, wkey, getPrefix(wkey), wch.Rev()); err == nil {
break
}
if k := retryInterval * 2; k < e.watchInterval {
Expand Down
23 changes: 7 additions & 16 deletions pkg/manager/config/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"go.uber.org/zap"
)

func testConfigManager(t *testing.T, cfg config.Advance) (*ConfigManager, context.Context) {
func testConfigManager(t *testing.T, cfg *config.Config) (*ConfigManager, context.Context) {
addr, err := url.Parse("http://127.0.0.1:0")
require.NoError(t, err)

Expand Down Expand Up @@ -68,9 +68,7 @@ func testConfigManager(t *testing.T, cfg config.Advance) (*ConfigManager, contex
}

func TestBase(t *testing.T) {
cfgmgr, ctx := testConfigManager(t, config.Advance{
IgnoreWrongNamespace: true,
})
cfgmgr, ctx := testConfigManager(t, &config.Config{})

nsNum := 10
valNum := 30
Expand Down Expand Up @@ -130,9 +128,7 @@ func TestBase(t *testing.T) {
}

func TestBaseConcurrency(t *testing.T) {
cfgmgr, ctx := testConfigManager(t, config.Advance{
IgnoreWrongNamespace: true,
})
cfgmgr, ctx := testConfigManager(t, &config.Config{})

var wg waitgroup.WaitGroup
batchNum := 16
Expand Down Expand Up @@ -171,28 +167,23 @@ func TestBaseConcurrency(t *testing.T) {
}

func TestBaseWatch(t *testing.T) {
cfgmgr, ctx := testConfigManager(t, config.Advance{
IgnoreWrongNamespace: true,
})
cfgmgr, ctx := testConfigManager(t, &config.Config{})

ch := make(chan string, 1)
cfgmgr.watch(ctx, "test", "t", func(l *zap.Logger, e mvccpb.Event) {
ch <- string(e.Kv.Value)
})

// clear the channel first
for len(ch) > 0 {
<-ch
}

// set it
require.NoError(t, cfgmgr.set(ctx, "test", "t", "1"))

// now the only way to check watch is to wait
select {
case <-time.After(5 * time.Second):
t.Fatal("timeout waiting chan")
case tg := <-ch:
for len(ch) > 0 {
tg = <-ch
}
require.Equal(t, "1", tg)
}
}
26 changes: 24 additions & 2 deletions pkg/manager/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@ import (
"encoding/json"

"github.com/pingcap/TiProxy/lib/config"
"github.com/pingcap/TiProxy/lib/util/errors"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.uber.org/zap"
)

func (e *ConfigManager) initProxyConfig(ctx context.Context) {
func (e *ConfigManager) watchCfgProxy(ctx context.Context, cfg *config.Config) error {
if _, err := e.GetProxyConfig(ctx); err != nil && errors.Is(err, ErrNoOrMultiResults) {
if err := e.SetProxyConfig(ctx, &config.ProxyServerOnline{
MaxConnections: cfg.Proxy.MaxConnections,
TCPKeepAlive: cfg.Proxy.TCPKeepAlive,
}); err != nil {
return err
}
}
e.watch(ctx, PathPrefixProxy, "config", func(logger *zap.Logger, evt mvccpb.Event) {
var proxy config.ProxyServerOnline
if err := json.Unmarshal(evt.Kv.Value, &proxy); err != nil {
Expand All @@ -32,12 +41,25 @@ func (e *ConfigManager) initProxyConfig(ctx context.Context) {
}
e.chProxy <- &proxy
})
return nil
}

func (e *ConfigManager) GetProxyConfig() <-chan *config.ProxyServerOnline {
func (e *ConfigManager) GetProxyConfigWatch() <-chan *config.ProxyServerOnline {
return e.chProxy
}

func (e *ConfigManager) GetProxyConfig(ctx context.Context) (*config.ProxyServerOnline, error) {
val, err := e.get(ctx, PathPrefixProxy, "config")
if err != nil {
return nil, err
}
ret := &config.ProxyServerOnline{}
if err := json.Unmarshal(val.Value, ret); err != nil {
return nil, err
}
return ret, nil
}

func (e *ConfigManager) SetProxyConfig(ctx context.Context, proxy *config.ProxyServerOnline) error {
value, err := json.Marshal(proxy)
if err != nil {
Expand Down
Loading

0 comments on commit ec41f4b

Please sign in to comment.