Skip to content

Commit

Permalink
Merge branch 'main' into nbio_2
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Jun 30, 2023
2 parents 76e8cde + 4035b4b commit bb924f4
Show file tree
Hide file tree
Showing 20 changed files with 1,128 additions and 565 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,7 +5,7 @@ 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
Expand All @@ -19,12 +19,12 @@ 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
go.uber.org/ratelimit v0.2.0
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20221023144134-a1e5550cf13e
google.golang.org/grpc v1.51.0
)

Expand Down Expand Up @@ -113,7 +113,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 All @@ -128,6 +127,7 @@ require (
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/exp v0.0.0-20221023144134-a1e5550cf13e // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.3.0 // 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
26 changes: 10 additions & 16 deletions lib/config/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,27 @@ const (
healthCheckMaxRetries = 3
healthCheckRetryInterval = 1 * time.Second
healthCheckTimeout = 2 * time.Second
tombstoneThreshold = 5 * time.Minute
)

// HealthCheck contains some configurations for health check.
// Some general configurations of them may be exposed to users in the future.
// We can use shorter durations to speed up unit tests.
type HealthCheck struct {
Enable bool `yaml:"enable" json:"enable" toml:"enable"`
Interval time.Duration `yaml:"interval" json:"interval" toml:"interval"`
MaxRetries int `yaml:"max-retries" json:"max-retries" toml:"max-retries"`
RetryInterval time.Duration `yaml:"retry-interval" json:"retry-interval" toml:"retry-interval"`
DialTimeout time.Duration `yaml:"dial-timeout" json:"dial-timeout" toml:"dial-timeout"`
TombstoneThreshold time.Duration `yaml:"tombstone-threshold" json:"tombstone-threshold" toml:"tombstone-threshold"`
Enable bool `yaml:"enable" json:"enable" toml:"enable"`
Interval time.Duration `yaml:"interval" json:"interval" toml:"interval"`
MaxRetries int `yaml:"max-retries" json:"max-retries" toml:"max-retries"`
RetryInterval time.Duration `yaml:"retry-interval" json:"retry-interval" toml:"retry-interval"`
DialTimeout time.Duration `yaml:"dial-timeout" json:"dial-timeout" toml:"dial-timeout"`
}

// NewDefaultHealthCheckConfig creates a default HealthCheck.
func NewDefaultHealthCheckConfig() *HealthCheck {
return &HealthCheck{
Enable: true,
Interval: healthCheckInterval,
MaxRetries: healthCheckMaxRetries,
RetryInterval: healthCheckRetryInterval,
DialTimeout: healthCheckTimeout,
TombstoneThreshold: tombstoneThreshold,
Enable: true,
Interval: healthCheckInterval,
MaxRetries: healthCheckMaxRetries,
RetryInterval: healthCheckRetryInterval,
DialTimeout: healthCheckTimeout,
}
}

Expand All @@ -73,9 +70,6 @@ func (hc *HealthCheck) Check() {
if hc.DialTimeout == 0 {
hc.DialTimeout = healthCheckTimeout
}
if hc.TombstoneThreshold == 0 {
hc.TombstoneThreshold = tombstoneThreshold
}
}

func NewNamespace(data []byte) (*Namespace, error) {
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 connects to PD ETCD server.
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 bb924f4

Please sign in to comment.