Skip to content

Commit

Permalink
etcdutil: revert etcd client in pd legacy server (tikv#6182) (tikv#6186)
Browse files Browse the repository at this point in the history
close tikv#6124, ref tikv#6182

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: lhy1024 <[email protected]>
Signed-off-by: Cabinfever_B <[email protected]>
  • Loading branch information
2 people authored and CabinfeverB committed Mar 22, 2023
1 parent 0eb5850 commit b487635
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pd-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ on:
- release-4.0
- release-5.*
- release-6.*
- release-7.*
pull_request:
branches:
- master
- release-4.0
- release-5.*
- release-6.*
- release-7.*
concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tso-function-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ on:
- master
- release-5.*
- release-6.*
- release-7.*
pull_request:
branches:
- master
- release-5.*
- release-6.*
- release-7.*
concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true
Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/resource_manager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (s *Server) initClient() error {
if err != nil {
return err
}
s.etcdClient, s.httpClient, err = etcdutil.CreateClients(tlsConfig, []url.URL(u))
s.etcdClient, s.httpClient, err = etcdutil.CreateClientsWithMultiEndpoint(tlsConfig, []url.URL(u))
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/mcs/tso/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (s *Server) initClient() error {
if err != nil {
return err
}
s.etcdClient, s.httpClient, err = etcdutil.CreateClients(tlsConfig, s.backendUrls)
s.etcdClient, s.httpClient, err = etcdutil.CreateClientsWithMultiEndpoint(tlsConfig, s.backendUrls)
return err
}

Expand Down
50 changes: 42 additions & 8 deletions pkg/utils/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,30 @@ func EtcdKVPutWithTTL(ctx context.Context, c *clientv3.Client, key string, value
return kv.Put(ctx, key, value, clientv3.WithLease(grantResp.ID))
}

// CreateClientsWithMultiEndpoint creates etcd v3 client and http client.
func CreateClientsWithMultiEndpoint(tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client, *http.Client, error) {
client, err := createEtcdClientWithMultiEndpoint(tlsConfig, acUrls)
if err != nil {
return nil, nil, errs.ErrNewEtcdClient.Wrap(err).GenWithStackByCause()
}
httpClient := createHTTPClient(tlsConfig)
return client, httpClient, nil
}

// CreateClients creates etcd v3 client and http client.
func CreateClients(tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client, *http.Client, error) {
func CreateClients(tlsConfig *tls.Config, acUrls url.URL) (*clientv3.Client, *http.Client, error) {
client, err := createEtcdClient(tlsConfig, acUrls)
if err != nil {
return nil, nil, errs.ErrNewEtcdClient.Wrap(err).GenWithStackByCause()
}
httpClient := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
httpClient := createHTTPClient(tlsConfig)
return client, httpClient, nil
}

func createEtcdClient(tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client, error) {
// createEtcdClientWithMultiEndpoint creates etcd v3 client.
// Note: it will be used by micro service server and support multi etcd endpoints.
// FIXME: But it cannot switch etcd endpoints as soon as possible when one of endpoints is with io hang.
func createEtcdClientWithMultiEndpoint(tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client, error) {
if len(acUrls) == 0 {
return nil, errs.ErrNewEtcdClient.FastGenByArgs("no available etcd address")
}
Expand Down Expand Up @@ -244,6 +252,32 @@ func createEtcdClient(tlsConfig *tls.Config, acUrls []url.URL) (*clientv3.Client
return client, err
}

// createEtcdClient creates etcd v3 client.
// Note: it will be used by legacy pd-server, and only connect to leader only.
func createEtcdClient(tlsConfig *tls.Config, acURL url.URL) (*clientv3.Client, error) {
lgc := zap.NewProductionConfig()
lgc.Encoding = log.ZapEncodingName
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{acURL.String()},
DialTimeout: defaultEtcdClientTimeout,
TLS: tlsConfig,
LogConfig: &lgc,
})
if err == nil {
log.Info("create etcd v3 client", zap.String("endpoints", acURL.String()))
}
return client, err
}

func createHTTPClient(tlsConfig *tls.Config) *http.Client {
return &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
TLSClientConfig: tlsConfig,
},
}
}

// InitClusterID creates a cluster ID for the given key if it hasn't existed.
// This function assumes the cluster ID has already existed and always use a
// cheaper read to retrieve it; if it doesn't exist, invoke the more expensive
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/etcdutil/etcdutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestEtcdClientSync(t *testing.T) {
ep1 := cfg1.LCUrls[0].String()
urls, err := types.NewURLs([]string{ep1})
re.NoError(err)
client1, err := createEtcdClient(nil, urls)
client1, err := createEtcdClientWithMultiEndpoint(nil, urls)
re.NoError(err)
<-etcd1.Server.ReadyNotify()

Expand Down Expand Up @@ -277,7 +277,7 @@ func checkEtcdWithHangLeader(t *testing.T) error {
// Create a etcd client with etcd1 as endpoint.
urls, err := types.NewURLs([]string{proxyAddr})
re.NoError(err)
client1, err := createEtcdClient(nil, urls)
client1, err := createEtcdClientWithMultiEndpoint(nil, urls)
re.NoError(err)

// Add a new member and set the client endpoints to etcd1 and etcd2.
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func startClient(cfg *config.Config) (*clientv3.Client, *http.Client, error) {
if err != nil {
return nil, nil, err
}
return etcdutil.CreateClients(tlsConfig, etcdCfg.ACUrls)
return etcdutil.CreateClients(tlsConfig, etcdCfg.ACUrls[0])
}

// AddStartCallback adds a callback in the startServer phase.
Expand Down

0 comments on commit b487635

Please sign in to comment.