Skip to content

Commit

Permalink
Use a new way to create client for v2 (tikv#806)
Browse files Browse the repository at this point in the history
Signed-off-by: iosmanthus <[email protected]>
  • Loading branch information
rleungx authored and zeminzhou committed Feb 28, 2024
1 parent f778441 commit 49b9387
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
39 changes: 20 additions & 19 deletions rawkv/rawkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,32 +200,33 @@ func NewClientWithOpts(ctx context.Context, pdAddrs []string, opts ...ClientOpt)
o(opt)
}

// Use an unwrapped PDClient to obtain keyspace meta.
pdCli, err := pd.NewClientWithContext(ctx, pdAddrs, pd.SecurityOption{
CAPath: opt.security.ClusterSSLCA,
CertPath: opt.security.ClusterSSLCert,
KeyPath: opt.security.ClusterSSLKey,
}, opt.pdOptions...)
if err != nil {
return nil, errors.WithStack(err)
}

// Build a CodecPDClient
var codecCli *tikv.CodecPDClient
var (
pdClient pd.Client
codecCli *tikv.CodecPDClient
err error
)

switch opt.apiVersion {
case kvrpcpb.APIVersion_V1, kvrpcpb.APIVersion_V1TTL:
codecCli = locate.NewCodecPDClient(tikv.ModeRaw, pdCli)
pdClient, err = tikv.NewPDClientWithAPIContext(pdAddrs, pd.NewAPIContextV1())
if err != nil {
return nil, errors.WithStack(err)
}
codecCli = locate.NewCodecPDClient(tikv.ModeRaw, pdClient)
case kvrpcpb.APIVersion_V2:
codecCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeRaw, pdCli, opt.keyspace)
pdClient, err = tikv.NewPDClientWithAPIContext(pdAddrs, pd.NewAPIContextV2(opt.keyspace))
if err != nil {
return nil, errors.WithStack(err)
}
codecCli, err = tikv.NewCodecPDClientWithKeyspace(tikv.ModeRaw, pdClient, opt.keyspace)
if err != nil {
return nil, err
}
default:
return nil, errors.Errorf("unknown api version: %d", opt.apiVersion)
return nil, errors.Errorf("unknown API version: %d", opt.apiVersion)
}

pdCli = codecCli
pdClient = codecCli

rpcCli := client.NewRPCClient(
client.WithSecurity(opt.security),
Expand All @@ -235,9 +236,9 @@ func NewClientWithOpts(ctx context.Context, pdAddrs []string, opts ...ClientOpt)

return &Client{
apiVersion: opt.apiVersion,
clusterID: pdCli.GetClusterID(ctx),
regionCache: locate.NewRegionCache(pdCli),
pdClient: pdCli,
clusterID: pdClient.GetClusterID(ctx),
regionCache: locate.NewRegionCache(pdClient),
pdClient: pdClient,
rpcClient: rpcCli,
}, nil
}
Expand Down
8 changes: 7 additions & 1 deletion tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,15 @@ func NewKVStore(uuid string, pdClient pd.Client, spkv SafePointKV, tikvclient Cl

// NewPDClient returns an unwrapped pd client.
func NewPDClient(pdAddrs []string) (pd.Client, error) {
return NewPDClientWithAPIContext(pdAddrs, pd.NewAPIContextV1())
}

// NewPDClientWithAPIContext returns an unwrapped pd client with API context.
func NewPDClientWithAPIContext(pdAddrs []string, apiContext pd.APIContext) (pd.Client, error) {
cfg := config.GetGlobalConfig()
// init pd-client
pdCli, err := pd.NewClient(
pdCli, err := pd.NewClientWithAPIContext(
context.Background(), apiContext,
pdAddrs, pd.SecurityOption{
CAPath: cfg.Security.ClusterSSLCA,
CertPath: cfg.Security.ClusterSSLCert,
Expand Down
21 changes: 17 additions & 4 deletions txnkv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/txnkv/transaction"
"github.com/tikv/client-go/v2/util"
pd "github.com/tikv/pd/client"
)

// Client is a txn client.
Expand Down Expand Up @@ -70,12 +71,24 @@ func NewClient(pdAddrs []string, opts ...ClientOpt) (*Client, error) {
for _, o := range opts {
o(opt)
}
// Use an unwrapped PDClient to obtain keyspace meta.
pdClient, err := tikv.NewPDClient(pdAddrs)

var (
pdClient pd.Client
apiContext pd.APIContext
err error
)
switch opt.apiVersion {
case kvrpcpb.APIVersion_V1:
apiContext = pd.NewAPIContextV1()
case kvrpcpb.APIVersion_V2:
apiContext = pd.NewAPIContextV2(opt.keyspaceName)
default:
return nil, errors.Errorf("unknown API version: %d", opt.apiVersion)
}
pdClient, err = tikv.NewPDClientWithAPIContext(pdAddrs, apiContext)
if err != nil {
return nil, errors.WithStack(err)
}

pdClient = util.InterceptedPDClient{Client: pdClient}

// Construct codec from options.
Expand All @@ -89,7 +102,7 @@ func NewClient(pdAddrs []string, opts ...ClientOpt) (*Client, error) {
return nil, err
}
default:
return nil, errors.Errorf("unknown api version: %d", opt.apiVersion)
return nil, errors.Errorf("unknown API version: %d", opt.apiVersion)
}

pdClient = codecCli
Expand Down

0 comments on commit 49b9387

Please sign in to comment.