Skip to content

Commit

Permalink
update pd client
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Feb 2, 2023
1 parent f0575a8 commit 1d45ae8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 34 deletions.
5 changes: 1 addition & 4 deletions domain/globalconfigsync/globalconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
"go.uber.org/zap"
)

// GlobalConfigPath as Etcd prefix
const GlobalConfigPath = "/global/config/"

// GlobalConfigSyncer is used to sync pd global config.
type GlobalConfigSyncer struct {
pd pd.Client
Expand All @@ -44,7 +41,7 @@ func (s *GlobalConfigSyncer) StoreGlobalConfig(ctx context.Context, item pd.Glob
if s.pd == nil {
return nil
}
err := s.pd.StoreGlobalConfig(ctx, GlobalConfigPath, []pd.GlobalConfigItem{item})
err := s.pd.StoreGlobalConfig(ctx, "", []pd.GlobalConfigItem{item})
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions domain/globalconfigsync/globalconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package globalconfigsync_test

import (
"context"
"path"
"runtime"
"testing"
"time"
Expand Down Expand Up @@ -59,10 +58,10 @@ func TestGlobalConfigSyncer(t *testing.T) {
syncer.Notify(pd.GlobalConfigItem{Name: "a", Value: "b"})
err = syncer.StoreGlobalConfig(context.Background(), <-syncer.NotifyCh)
require.NoError(t, err)
items, revision, err := client.LoadGlobalConfig(context.Background(), globalconfigsync.GlobalConfigPath)
items, revision, err := client.LoadGlobalConfig(context.Background(), []string{"a"}, "")
require.NoError(t, err)
require.Equal(t, 1, len(items))
require.Equal(t, path.Join(globalconfigsync.GlobalConfigPath, "a"), items[0].Name)
require.Equal(t, "/global/config/a", items[0].Name)
require.Equal(t, int64(0), revision)
require.Equal(t, "b", items[0].Value)
}
Expand Down Expand Up @@ -97,15 +96,15 @@ func TestStoreGlobalConfig(t *testing.T) {
client :=
store.(kv.StorageWithPD).GetPDClient()
// enable top sql will be translated to enable_resource_metering
items, _, err := client.LoadGlobalConfig(context.Background(), globalconfigsync.GlobalConfigPath)
items, _, err := client.LoadGlobalConfig(context.Background(), []string{"enable_resource_metering", "source_id"}, "")
require.NoError(t, err)
if len(items) == 2 && items[0].Value == "" {
continue
}
require.Len(t, items, 2)
require.Equal(t, items[0].Name, path.Join(globalconfigsync.GlobalConfigPath, "enable_resource_metering"))
require.Equal(t, items[0].Name, "/global/config/enable_resource_metering")
require.Equal(t, items[0].Value, "true")
require.Equal(t, items[1].Name, path.Join(globalconfigsync.GlobalConfigPath, "source_id"))
require.Equal(t, items[1].Name, "/global/config/source_id")
require.Equal(t, items[1].Value, "2")
return
}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ require (
github.com/pingcap/errors v0.11.5-0.20221009092201-b66cddb77c32
github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3
github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059
github.com/pingcap/kvproto v0.0.0-20230131104319-a7c51106dfe7
github.com/pingcap/kvproto v0.0.0-20230201112839-2b853bed8125
github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c
github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4
github.com/pingcap/tidb/parser v0.0.0-20211011031125-9b13dc409c5e
Expand Down Expand Up @@ -276,5 +276,8 @@ replace (
// fix potential security issue(CVE-2020-26160) introduced by indirect dependency.
github.com/dgrijalva/jwt-go => github.com/form3tech-oss/jwt-go v3.2.6-0.20210809144907-32ab6a8243d7+incompatible
github.com/pingcap/tidb/parser => ./parser
github.com/tikv/client-go/v2 => github.com/HuSharp/client-go/v2 v2.0.0-20230202021713-c9a6ecd403ab
github.com/tikv/pd => github.com/HuSharp/pd v1.1.0-beta.0.20230202034438-68b5d80b943d
github.com/tikv/pd/client => github.com/HuSharp/pd/client v0.0.0-20230202034438-68b5d80b943d
go.opencensus.io => go.opencensus.io v0.23.1-0.20220331163232-052120675fac
)
17 changes: 8 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/HdrHistogram/hdrhistogram-go v1.1.2 h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM=
github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo=
github.com/HuSharp/client-go/v2 v2.0.0-20230202021713-c9a6ecd403ab h1:uKRGfQyIWCbLomNy6eLPjrVeqj0OSvrb5vYlysuDPJA=
github.com/HuSharp/client-go/v2 v2.0.0-20230202021713-c9a6ecd403ab/go.mod h1:exX/L++dG1l7GZ5h0MXZQb6UEzbjZphDcsTQerGafw4=
github.com/HuSharp/pd v1.1.0-beta.0.20230202034438-68b5d80b943d h1:5TdzvzFzTh3Z4dUDayp2Ga5B6St8okqb5LSP55SLzTE=
github.com/HuSharp/pd v1.1.0-beta.0.20230202034438-68b5d80b943d/go.mod h1:jb9oq6rN4U0U3FZdvqWlpi9rZzFJxiOlvZ3aj5BTpg8=
github.com/HuSharp/pd/client v0.0.0-20230202034438-68b5d80b943d h1:GXXvn8QekeU0hr9tj7M/eP+lpzACMR5nCENwp8PDupo=
github.com/HuSharp/pd/client v0.0.0-20230202034438-68b5d80b943d/go.mod h1:0fdKmj9cafPLsHAeVAcgB50Uj018WGfMC1tgvzWXXQQ=
github.com/Jeffail/gabs/v2 v2.5.1 h1:ANfZYjpMlfTTKebycu4X1AgkVWumFVDYQl7JwOr4mDk=
github.com/Jeffail/gabs/v2 v2.5.1/go.mod h1:xCn81vdHKxFUuWWAaD5jCTQDNPBMh5pPs9IJ+NcziBI=
github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY=
Expand Down Expand Up @@ -1143,9 +1149,8 @@ github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059/go.mod h1:fMRU1BA1y+r89
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E=
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw=
github.com/pingcap/kvproto v0.0.0-20191211054548-3c6b38ea5107/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
github.com/pingcap/kvproto v0.0.0-20230119031034-25f1909b7934/go.mod h1:+on3Lfk/fb1lXkud3XvskJumhSIEEgN2TTbMObUlrxE=
github.com/pingcap/kvproto v0.0.0-20230131104319-a7c51106dfe7 h1:oYUK4V5PMlyIooU/+pPkKrJ3vELwcuuCNyKKlqSQa5c=
github.com/pingcap/kvproto v0.0.0-20230131104319-a7c51106dfe7/go.mod h1:+on3Lfk/fb1lXkud3XvskJumhSIEEgN2TTbMObUlrxE=
github.com/pingcap/kvproto v0.0.0-20230201112839-2b853bed8125 h1:ZiCJcEzmmF5xNgt8GIXekd3WQXI/22kzYQnrHi3Fc/4=
github.com/pingcap/kvproto v0.0.0-20230201112839-2b853bed8125/go.mod h1:+on3Lfk/fb1lXkud3XvskJumhSIEEgN2TTbMObUlrxE=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
Expand Down Expand Up @@ -1308,12 +1313,6 @@ github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJf
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4=
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a/go.mod h1:h4xBhSNtOeEosLJ4P7JyKXX7Cabg7AVkWCK5gV2vOrM=
github.com/tikv/client-go/v2 v2.0.5-0.20230120021435-f89383775234 h1:2BmijiUk1Hcv0z58DVk4ypwaNmgutzLc2YJm0SHPEWE=
github.com/tikv/client-go/v2 v2.0.5-0.20230120021435-f89383775234/go.mod h1:jc7J2EbNeVvU6eXmB50wAGrTPyJwdi+0ENccMXMFSpw=
github.com/tikv/pd v1.1.0-beta.0.20230201064005-6ca9a3398f15 h1:ef+kODGby/rmF9fabJzqRM15NcGufkTRftROdy7jvAk=
github.com/tikv/pd v1.1.0-beta.0.20230201064005-6ca9a3398f15/go.mod h1:UnfZJ+ykNsVbQgo/CZ3NFyHnLJ/pQGlBD0M/WgxFHMQ=
github.com/tikv/pd/client v0.0.0-20230119115149-5c518d079b93 h1:KK5bx0KLcpYUCnuQ06THPYT6QdAMfvwAtRQ0saVGD7k=
github.com/tikv/pd/client v0.0.0-20230119115149-5c518d079b93/go.mod h1:NrbwVp9afaCmJjJEwFNtEQWfCChAW1ndnwjteHHS+d0=
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro=
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144/go.mod h1:Qimiffbc6q9tBWlVV6x0P9sat/ao1xEkREYPPj9hphk=
github.com/tklauser/go-sysconf v0.3.9/go.mod h1:11DU/5sG7UexIrp/O6g35hrWzu0JxlwQ3LSFUzyeuhs=
Expand Down
16 changes: 8 additions & 8 deletions store/mockstore/unistore/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"context"
"errors"
"math"
"path"
"strings"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -50,19 +48,21 @@ func newPDClient(pd *us.MockPD) *pdClient {
}
}

func (c *pdClient) LoadGlobalConfig(ctx context.Context, configPath string) ([]pd.GlobalConfigItem, int64, error) {
ret := make([]pd.GlobalConfigItem, 0)
for k, v := range c.globalConfig {
if strings.HasPrefix(k, configPath) {
ret = append(ret, pd.GlobalConfigItem{Name: k, Value: v})
func (c *pdClient) LoadGlobalConfig(ctx context.Context, names []string, configPath string) ([]pd.GlobalConfigItem, int64, error) {
ret := make([]pd.GlobalConfigItem, len(names))
for i, name := range names {
if r, ok := c.globalConfig["/global/config/"+name]; ok {
ret[i] = pd.GlobalConfigItem{Name: "/global/config/" + name, Value: r, EventType: pdpb.EventType_PUT}
} else {
ret[i] = pd.GlobalConfigItem{Name: "/global/config/" + name, Value: ""}
}
}
return ret, 0, nil
}

func (c *pdClient) StoreGlobalConfig(ctx context.Context, configPath string, items []pd.GlobalConfigItem) error {
for _, item := range items {
c.globalConfig[path.Join(configPath, item.Name)] = item.Value
c.globalConfig["/global/config/"+item.Name] = item.Value
}
return nil
}
Expand Down
36 changes: 30 additions & 6 deletions store/mockstore/unistore/pd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,39 @@ func SetUpSuite() *GlobalConfigTestSuite {
return s
}

func TestLoadAndStore(t *testing.T) {
func TestLoad(t *testing.T) {
s := SetUpSuite()

err := s.client.StoreGlobalConfig(context.Background(), "/global/config", []pd.GlobalConfigItem{{Name: "NewObject", Value: "ok"}})
err := s.client.StoreGlobalConfig(context.Background(), "", []pd.GlobalConfigItem{{Name: "LoadOkGlobalConfig", Value: "ok"}})
require.Equal(t, nil, err)
res, _, err := s.client.LoadGlobalConfig(context.Background(), []string{"LoadOkGlobalConfig", "LoadErrGlobalConfig"}, "")
require.Equal(t, err, nil)
for _, j := range res {
println(j.Name)
switch j.Name {
case "/global/config/LoadOkGlobalConfig":
require.Equal(t, "ok", j.Value)
case "/global/config/LoadErrGlobalConfig":
require.Equal(t, "", j.Value)
default:
require.Equal(t, true, false)
}
}
s.TearDownSuite()
}

res, _, err := s.client.LoadGlobalConfig(context.Background(), "/global/config")
require.Equal(t, nil, err)
require.Equal(t, 1, len(res))
func TestStore(t *testing.T) {
s := SetUpSuite()

res, _, err := s.client.LoadGlobalConfig(context.Background(), []string{"NewObject"}, "")
require.Equal(t, err, nil)
require.Equal(t, res[0].Value, "")

err = s.client.StoreGlobalConfig(context.Background(), "", []pd.GlobalConfigItem{{Name: "NewObject", Value: "ok"}})
require.Equal(t, err, nil)

res, _, err = s.client.LoadGlobalConfig(context.Background(), []string{"NewObject"}, "")
require.Equal(t, err, nil)
require.Equal(t, res[0].Value, "ok")

s.TearDownSuite()
}
Expand Down

0 comments on commit 1d45ae8

Please sign in to comment.