-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
infosync, router: register TiProxy to ETCD (#309)
- Loading branch information
Showing
15 changed files
with
562 additions
and
59 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
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
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,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++ | ||
}) | ||
} |
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,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 "" | ||
} |
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,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") | ||
} |
Oops, something went wrong.