From eebd344540202fc63ae35a4adf1a5c91a2bca3e2 Mon Sep 17 00:00:00 2001 From: ystaticy Date: Wed, 25 Dec 2024 15:19:11 +0800 Subject: [PATCH 1/6] new store by ks meta Signed-off-by: ystaticy --- internal/locate/pd_codec.go | 9 +++++++++ tikv/region.go | 3 +++ 2 files changed, 12 insertions(+) diff --git a/internal/locate/pd_codec.go b/internal/locate/pd_codec.go index 88e187e07c..110ea4ca28 100644 --- a/internal/locate/pd_codec.go +++ b/internal/locate/pd_codec.go @@ -72,6 +72,15 @@ func NewCodecPDClientWithKeyspace(mode apicodec.Mode, client pd.Client, keyspace return &CodecPDClient{client, codec}, nil } +// NewCodecPDClientWithKeyspaceMeta creates a CodecPDClient in API v2 with keyspace name. +func NewCodecPDClientWithKeyspaceMeta(mode apicodec.Mode, client pd.Client, keyspaceMeta *keyspacepb.KeyspaceMeta) (*CodecPDClient, error) { + codec, err := apicodec.NewCodecV2(mode, keyspaceMeta) + if err != nil { + return nil, err + } + return &CodecPDClient{client, codec}, nil +} + // GetKeyspaceID attempts to retrieve keyspace ID corresponding to the given keyspace name from PD. func GetKeyspaceID(client pd.Client, name string) (uint32, error) { meta, err := client.LoadKeyspace(context.Background(), apicodec.BuildKeyspaceName(name)) diff --git a/tikv/region.go b/tikv/region.go index 6b5e4874d5..fea4638653 100644 --- a/tikv/region.go +++ b/tikv/region.go @@ -98,6 +98,9 @@ var NewCodecPDClient = locate.NewCodecPDClient // NewCodecPDClientWithKeyspace creates a CodecPDClient in API v2 with keyspace name. var NewCodecPDClientWithKeyspace = locate.NewCodecPDClientWithKeyspace +// NewCodecPDClientWithKeyspaceMeta creates a CodecPDClient in API v2 with keyspace meta. +var NewCodecPDClientWithKeyspaceMeta = locate.NewCodecPDClientWithKeyspaceMeta + // NewCodecV1 is a constructor for v1 Codec. var NewCodecV1 = apicodec.NewCodecV1 From 7da9818dd6bd68f33d1b53445495db152403c841 Mon Sep 17 00:00:00 2001 From: ystaticy Date: Wed, 25 Dec 2024 16:20:03 +0800 Subject: [PATCH 2/6] fix if ks meta is nil Signed-off-by: ystaticy --- internal/locate/pd_codec.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/locate/pd_codec.go b/internal/locate/pd_codec.go index 110ea4ca28..f5aa9c18ce 100644 --- a/internal/locate/pd_codec.go +++ b/internal/locate/pd_codec.go @@ -74,6 +74,10 @@ func NewCodecPDClientWithKeyspace(mode apicodec.Mode, client pd.Client, keyspace // NewCodecPDClientWithKeyspaceMeta creates a CodecPDClient in API v2 with keyspace name. func NewCodecPDClientWithKeyspaceMeta(mode apicodec.Mode, client pd.Client, keyspaceMeta *keyspacepb.KeyspaceMeta) (*CodecPDClient, error) { + if keyspaceMeta == nil { + return NewCodecPDClient(mode, client), nil + } + codec, err := apicodec.NewCodecV2(mode, keyspaceMeta) if err != nil { return nil, err From 93f29baba2a68d0cbe58e1f75b75f61605e4fce7 Mon Sep 17 00:00:00 2001 From: ystaticy Date: Wed, 25 Dec 2024 16:23:40 +0800 Subject: [PATCH 3/6] fix code comments Signed-off-by: ystaticy --- internal/locate/pd_codec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/locate/pd_codec.go b/internal/locate/pd_codec.go index f5aa9c18ce..d155193fcd 100644 --- a/internal/locate/pd_codec.go +++ b/internal/locate/pd_codec.go @@ -72,7 +72,7 @@ func NewCodecPDClientWithKeyspace(mode apicodec.Mode, client pd.Client, keyspace return &CodecPDClient{client, codec}, nil } -// NewCodecPDClientWithKeyspaceMeta creates a CodecPDClient in API v2 with keyspace name. +// NewCodecPDClientWithKeyspaceMeta creates a CodecPDClient in API v2 with keyspace meta. func NewCodecPDClientWithKeyspaceMeta(mode apicodec.Mode, client pd.Client, keyspaceMeta *keyspacepb.KeyspaceMeta) (*CodecPDClient, error) { if keyspaceMeta == nil { return NewCodecPDClient(mode, client), nil From 71a902f1aebc2a4f3c7f8fcb5a85c3342436381c Mon Sep 17 00:00:00 2001 From: ystaticy Date: Wed, 25 Dec 2024 16:37:36 +0800 Subject: [PATCH 4/6] remove var NewCodecPDClientWithKeyspace Signed-off-by: ystaticy --- tikv/region.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/tikv/region.go b/tikv/region.go index fea4638653..6b33ae5e10 100644 --- a/tikv/region.go +++ b/tikv/region.go @@ -95,9 +95,6 @@ type CodecPDClient = locate.CodecPDClient // NewCodecPDClient is a constructor for CodecPDClient var NewCodecPDClient = locate.NewCodecPDClient -// NewCodecPDClientWithKeyspace creates a CodecPDClient in API v2 with keyspace name. -var NewCodecPDClientWithKeyspace = locate.NewCodecPDClientWithKeyspace - // NewCodecPDClientWithKeyspaceMeta creates a CodecPDClient in API v2 with keyspace meta. var NewCodecPDClientWithKeyspaceMeta = locate.NewCodecPDClientWithKeyspaceMeta From 8c23c81b0618efc4a03718842fbf2f7d057b6c28 Mon Sep 17 00:00:00 2001 From: ystaticy Date: Wed, 25 Dec 2024 17:07:34 +0800 Subject: [PATCH 5/6] remove var NewCodecPDClientWithKeyspace Signed-off-by: ystaticy --- integration_tests/raw/api_test.go | 4 +++- integration_tests/util_test.go | 4 +++- rawkv/rawkv.go | 6 +++++- txnkv/client.go | 6 +++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/integration_tests/raw/api_test.go b/integration_tests/raw/api_test.go index 58da430b7d..ec9295e5d2 100644 --- a/integration_tests/raw/api_test.go +++ b/integration_tests/raw/api_test.go @@ -77,7 +77,9 @@ func (s *apiTestSuite) newRawKVClient(pdCli pd.Client, addrs []string) *rawkv.Cl func (s *apiTestSuite) wrapPDClient(pdCli pd.Client, addrs []string) pd.Client { var err error if s.getApiVersion(pdCli) == kvrpcpb.APIVersion_V2 { - pdCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeRaw, pdCli, tikv.DefaultKeyspaceName) + keyspaceMeta, err := pdCli.LoadKeyspace(context.Background(), tikv.DefaultKeyspaceName) + s.Nil(err) + pdCli, err = tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeRaw, pdCli, keyspaceMeta) } s.Nil(err) return pdCli diff --git a/integration_tests/util_test.go b/integration_tests/util_test.go index 51452a05e7..de1501b729 100644 --- a/integration_tests/util_test.go +++ b/integration_tests/util_test.go @@ -109,7 +109,9 @@ func newTiKVStore(t *testing.T) *tikv.KVStore { pdClient = tikv.NewCodecPDClient(tikv.ModeTxn, pdClient) opt = tikv.WithCodec(tikv.NewCodecV1(tikv.ModeTxn)) case kvrpcpb.APIVersion_V2: - codecCli, err := tikv.NewCodecPDClientWithKeyspace(tikv.ModeTxn, pdClient, tikv.DefaultKeyspaceName) + keyspaceMeta, err := pdClient.LoadKeyspace(context.Background(), tikv.DefaultKeyspaceName) + re.Nil(err) + codecCli, err := tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeTxn, pdClient, keyspaceMeta) pdClient = codecCli re.Nil(err) opt = tikv.WithCodec(codecCli.GetCodec()) diff --git a/rawkv/rawkv.go b/rawkv/rawkv.go index f851a3ed4a..4f13763201 100644 --- a/rawkv/rawkv.go +++ b/rawkv/rawkv.go @@ -218,7 +218,11 @@ func NewClientWithOpts(ctx context.Context, pdAddrs []string, opts ...ClientOpt) if err != nil { return nil, errors.WithStack(err) } - codecCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeRaw, pdClient, opt.keyspace) + keyspaceMeta, err := pdClient.LoadKeyspace(context.Background(), opt.keyspace) + if err != nil { + return nil, err + } + codecCli, err = tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeRaw, pdClient, keyspaceMeta) if err != nil { return nil, err } diff --git a/txnkv/client.go b/txnkv/client.go index 5fc98c2c7d..9ea0eda57a 100644 --- a/txnkv/client.go +++ b/txnkv/client.go @@ -97,7 +97,11 @@ func NewClient(pdAddrs []string, opts ...ClientOpt) (*Client, error) { case kvrpcpb.APIVersion_V1: codecCli = tikv.NewCodecPDClient(tikv.ModeTxn, pdClient) case kvrpcpb.APIVersion_V2: - codecCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeTxn, pdClient, opt.keyspaceName) + keyspaceMeta, err := pdClient.LoadKeyspace(context.Background(), opt.keyspaceName) + if err != nil { + return nil, err + } + codecCli, err = tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeTxn, pdClient, keyspaceMeta) if err != nil { return nil, err } From 016657c49ded5139792795c83e1d08a67c54b3eb Mon Sep 17 00:00:00 2001 From: ystaticy Date: Wed, 25 Dec 2024 17:23:59 +0800 Subject: [PATCH 6/6] don't remove NewCodecPDClientWithKeyspace Signed-off-by: ystaticy --- integration_tests/raw/api_test.go | 4 +--- integration_tests/util_test.go | 4 +--- rawkv/rawkv.go | 6 +----- tikv/region.go | 3 +++ txnkv/client.go | 6 +----- 5 files changed, 7 insertions(+), 16 deletions(-) diff --git a/integration_tests/raw/api_test.go b/integration_tests/raw/api_test.go index ec9295e5d2..58da430b7d 100644 --- a/integration_tests/raw/api_test.go +++ b/integration_tests/raw/api_test.go @@ -77,9 +77,7 @@ func (s *apiTestSuite) newRawKVClient(pdCli pd.Client, addrs []string) *rawkv.Cl func (s *apiTestSuite) wrapPDClient(pdCli pd.Client, addrs []string) pd.Client { var err error if s.getApiVersion(pdCli) == kvrpcpb.APIVersion_V2 { - keyspaceMeta, err := pdCli.LoadKeyspace(context.Background(), tikv.DefaultKeyspaceName) - s.Nil(err) - pdCli, err = tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeRaw, pdCli, keyspaceMeta) + pdCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeRaw, pdCli, tikv.DefaultKeyspaceName) } s.Nil(err) return pdCli diff --git a/integration_tests/util_test.go b/integration_tests/util_test.go index de1501b729..51452a05e7 100644 --- a/integration_tests/util_test.go +++ b/integration_tests/util_test.go @@ -109,9 +109,7 @@ func newTiKVStore(t *testing.T) *tikv.KVStore { pdClient = tikv.NewCodecPDClient(tikv.ModeTxn, pdClient) opt = tikv.WithCodec(tikv.NewCodecV1(tikv.ModeTxn)) case kvrpcpb.APIVersion_V2: - keyspaceMeta, err := pdClient.LoadKeyspace(context.Background(), tikv.DefaultKeyspaceName) - re.Nil(err) - codecCli, err := tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeTxn, pdClient, keyspaceMeta) + codecCli, err := tikv.NewCodecPDClientWithKeyspace(tikv.ModeTxn, pdClient, tikv.DefaultKeyspaceName) pdClient = codecCli re.Nil(err) opt = tikv.WithCodec(codecCli.GetCodec()) diff --git a/rawkv/rawkv.go b/rawkv/rawkv.go index 4f13763201..f851a3ed4a 100644 --- a/rawkv/rawkv.go +++ b/rawkv/rawkv.go @@ -218,11 +218,7 @@ func NewClientWithOpts(ctx context.Context, pdAddrs []string, opts ...ClientOpt) if err != nil { return nil, errors.WithStack(err) } - keyspaceMeta, err := pdClient.LoadKeyspace(context.Background(), opt.keyspace) - if err != nil { - return nil, err - } - codecCli, err = tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeRaw, pdClient, keyspaceMeta) + codecCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeRaw, pdClient, opt.keyspace) if err != nil { return nil, err } diff --git a/tikv/region.go b/tikv/region.go index 6b33ae5e10..fea4638653 100644 --- a/tikv/region.go +++ b/tikv/region.go @@ -95,6 +95,9 @@ type CodecPDClient = locate.CodecPDClient // NewCodecPDClient is a constructor for CodecPDClient var NewCodecPDClient = locate.NewCodecPDClient +// NewCodecPDClientWithKeyspace creates a CodecPDClient in API v2 with keyspace name. +var NewCodecPDClientWithKeyspace = locate.NewCodecPDClientWithKeyspace + // NewCodecPDClientWithKeyspaceMeta creates a CodecPDClient in API v2 with keyspace meta. var NewCodecPDClientWithKeyspaceMeta = locate.NewCodecPDClientWithKeyspaceMeta diff --git a/txnkv/client.go b/txnkv/client.go index 9ea0eda57a..5fc98c2c7d 100644 --- a/txnkv/client.go +++ b/txnkv/client.go @@ -97,11 +97,7 @@ func NewClient(pdAddrs []string, opts ...ClientOpt) (*Client, error) { case kvrpcpb.APIVersion_V1: codecCli = tikv.NewCodecPDClient(tikv.ModeTxn, pdClient) case kvrpcpb.APIVersion_V2: - keyspaceMeta, err := pdClient.LoadKeyspace(context.Background(), opt.keyspaceName) - if err != nil { - return nil, err - } - codecCli, err = tikv.NewCodecPDClientWithKeyspaceMeta(tikv.ModeTxn, pdClient, keyspaceMeta) + codecCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeTxn, pdClient, opt.keyspaceName) if err != nil { return nil, err }