Skip to content

Commit

Permalink
infosync, router: register TiProxy to ETCD (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
djshow832 authored Jun 13, 2023
1 parent 66d42d5 commit d418199
Show file tree
Hide file tree
Showing 15 changed files with 562 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DEBUG ?=
DOCKERPREFIX ?=
BUILD_TAGS ?=
LDFLAGS ?=
BUILDFLAGS ?= -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS) -X main.Version=$(VERSION) -X main.Commit=$(COMMIT)' -tags '$(BUILD_TAGS)'
BUILDFLAGS ?= -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS) -X github.com/pingcap/TiProxy/pkg/util/versioninfo.TiProxyVersion=$(VERSION) -X github.com/pingcap/TiProxy/pkg/util/versioninfo.TiProxyGitHash=$(COMMIT)' -tags '$(BUILD_TAGS)'
ifneq ("$(DEBUG)", "")
BUILDFLAGS += -race
endif
Expand Down
8 changes: 2 additions & 6 deletions cmd/tiproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ import (
"github.com/pingcap/TiProxy/pkg/metrics"
"github.com/pingcap/TiProxy/pkg/sctx"
"github.com/pingcap/TiProxy/pkg/server"
"github.com/pingcap/TiProxy/pkg/util/versioninfo"
"github.com/spf13/cobra"
)

var (
Version = "test"
Commit = "test commit"
)

func main() {
rootCmd := &cobra.Command{
Use: os.Args[0],
Short: "start the proxy server",
Version: fmt.Sprintf("%s, commit %s", Version, Commit),
Version: fmt.Sprintf("%s, commit %s", versioninfo.TiProxyVersion, versioninfo.TiProxyGitHash),
}
rootCmd.SetOutput(os.Stdout)
rootCmd.SetErr(os.Stderr)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ go 1.19
require (
github.com/BurntSushi/toml v1.2.1
github.com/bahlo/generic-list-go v0.2.0
github.com/cenkalti/backoff/v4 v4.2.0
github.com/cenkalti/backoff/v4 v4.2.1
github.com/fsnotify/fsnotify v1.6.0
github.com/gin-contrib/pprof v1.4.0
github.com/gin-gonic/gin v1.8.1
github.com/go-mysql-org/go-mysql v1.6.0
github.com/pingcap/TiProxy/lib v0.0.0-00010101000000-000000000000
github.com/pingcap/errors v0.11.5-0.20221009092201-b66cddb77c32
github.com/pingcap/tidb v1.1.0-beta.0.20230103132820-3ccff46aa3bc
github.com/pingcap/tidb/parser v0.0.0-20230103132820-3ccff46aa3bc
github.com/prometheus/client_golang v1.14.0
Expand All @@ -19,6 +20,7 @@ require (
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
github.com/tidwall/btree v1.5.2
go.etcd.io/etcd/client/pkg/v3 v3.5.6
go.etcd.io/etcd/client/v3 v3.5.6
go.etcd.io/etcd/server/v3 v3.5.6
go.uber.org/atomic v1.10.0
Expand Down Expand Up @@ -83,7 +85,6 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pingcap/check v0.0.0-20211026125417-57bd13f7b5f0 // indirect
github.com/pingcap/errors v0.11.5-0.20221009092201-b66cddb77c32 // indirect
github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3 // indirect
github.com/pingcap/kvproto v0.0.0-20221213093948-9ccc6beaf0aa // indirect
github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c // indirect
Expand Down Expand Up @@ -113,7 +114,6 @@ require (
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.etcd.io/etcd/api/v3 v3.5.6 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.6 // indirect
go.etcd.io/etcd/client/v2 v2.305.6 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.6 // indirect
go.etcd.io/etcd/raft/v3 v3.5.6 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
github.com/blacktear23/go-proxyprotocol v1.0.2 h1:zR7PZeoU0wAkElcIXenFiy3R56WB6A+UEVi4c6RH8wo=
github.com/carlmjohnson/flagext v0.21.0 h1:/c4uK3ie786Z7caXLcIMvePNSSiH3bQVGDvmGLMme60=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
Expand Down
1 change: 1 addition & 0 deletions lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/pingcap/TiProxy/lib
go 1.18

require (
github.com/cenkalti/backoff/v4 v4.2.1
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.1
go.uber.org/atomic v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions lib/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
44 changes: 44 additions & 0 deletions lib/util/retry/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2023 PingCAP, Inc.
// SPDX-License-Identifier: Apache-2.0

package retry

import (
"context"
"time"

"github.com/cenkalti/backoff/v4"
)

const (
InfiniteCnt = 0
)

func NewBackOff(ctx context.Context, retryInterval time.Duration, retryCnt uint64) backoff.BackOff {
var bo backoff.BackOff
bo = backoff.NewConstantBackOff(retryInterval)
if ctx != nil {
bo = backoff.WithContext(bo, ctx)
}
if retryCnt != InfiniteCnt {
bo = backoff.WithMaxRetries(bo, retryCnt)
}
return bo
}

func Retry(o backoff.Operation, ctx context.Context, retryInterval time.Duration, retryCnt uint64) error {
bo := NewBackOff(ctx, retryInterval, retryCnt)
return backoff.Retry(o, bo)
}

func RetryNotify(o backoff.Operation, ctx context.Context, retryInterval time.Duration, retryCnt uint64,
notify backoff.Notify, notifyInterval uint64) error {
bo := NewBackOff(ctx, retryInterval, retryCnt)
var cnt uint64
return backoff.RetryNotify(o, bo, func(err error, duration time.Duration) {
if cnt%notifyInterval == 0 {
notify(err, duration)
}
cnt++
})
}
19 changes: 19 additions & 0 deletions lib/util/sys/sys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2023 PingCAP, Inc.
// SPDX-License-Identifier: Apache-2.0

package sys

import "net"

func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err == nil {
for _, address := range addrs {
ipnet, ok := address.(*net.IPNet)
if ok && ipnet.IP.IsGlobalUnicast() {
return ipnet.IP.String()
}
}
}
return ""
}
52 changes: 52 additions & 0 deletions pkg/manager/infosync/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 PingCAP, Inc.
// SPDX-License-Identifier: Apache-2.0

package infosync

import (
"strings"
"time"

"github.com/pingcap/TiProxy/lib/config"
"github.com/pingcap/TiProxy/lib/util/errors"
"github.com/pingcap/TiProxy/pkg/manager/cert"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/keepalive"
)

// InitEtcdClient initializes an etcd client that fetches TiDB instance topology from PD.
func InitEtcdClient(logger *zap.Logger, cfg *config.Config, certMgr *cert.CertManager) (*clientv3.Client, error) {
pdAddr := cfg.Proxy.PDAddrs
if len(pdAddr) == 0 {
// use tidb server addresses directly
return nil, nil
}
pdEndpoints := strings.Split(pdAddr, ",")
logger.Info("connect ETCD servers", zap.Strings("addrs", pdEndpoints))
etcdClient, err := clientv3.New(clientv3.Config{
Endpoints: pdEndpoints,
TLS: certMgr.ClusterTLS(),
Logger: logger.Named("etcdcli"),
AutoSyncInterval: 30 * time.Second,
DialTimeout: 5 * time.Second,
DialOptions: []grpc.DialOption{
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 10 * time.Second,
Timeout: 3 * time.Second,
}),
grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: time.Second,
Multiplier: 1.1,
Jitter: 0.1,
MaxDelay: 3 * time.Second,
},
MinConnectTimeout: 3 * time.Second,
}),
},
})
return etcdClient, errors.Wrapf(err, "init etcd client failed")
}
Loading

0 comments on commit d418199

Please sign in to comment.