move tikv.NewTxnClient to txnkv.NewClient (#391)

Signed-off-by: disksing <i@disksing.com>
This commit is contained in:
disksing 2021-12-10 13:38:38 +08:00 committed by GitHub
parent c0e8766154
commit 87c1c58064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 33 deletions

View File

@ -20,7 +20,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/tikv/client-go/v2/tikv" "github.com/tikv/client-go/v2/txnkv"
) )
// KV represents a Key-Value pair. // KV represents a Key-Value pair.
@ -33,14 +33,14 @@ func (kv KV) String() string {
} }
var ( var (
client *tikv.KVStore client *txnkv.Client
pdAddr = flag.String("pd", "127.0.0.1:2379", "pd address") pdAddr = flag.String("pd", "127.0.0.1:2379", "pd address")
) )
// Init initializes information. // Init initializes information.
func initStore() { func initStore() {
var err error var err error
client, err = tikv.NewTxnClient([]string{*pdAddr}) client, err = txnkv.NewClient([]string{*pdAddr})
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -190,36 +190,6 @@ func NewKVStore(uuid string, pdClient pd.Client, spkv SafePointKV, tikvclient Cl
return store, nil return store, nil
} }
// NewTxnClient creates a txn client with pdAddrs.
func NewTxnClient(pdAddrs []string) (*KVStore, error) {
cfg := config.GetGlobalConfig()
pdClient, err := NewPDClient(pdAddrs)
if err != nil {
return nil, err
}
// init uuid
// FIXME: uuid will be a very long and ugly string, simplify it.
uuid := fmt.Sprintf("tikv-%v", pdClient.GetClusterID(context.TODO()))
tlsConfig, err := cfg.Security.ToTLSConfig()
if err != nil {
return nil, err
}
spkv, err := NewEtcdSafePointKV(pdAddrs, tlsConfig)
if err != nil {
return nil, err
}
s, err := NewKVStore(uuid, pdClient, spkv, NewRPCClient(WithSecurity(cfg.Security)))
if err != nil {
return nil, err
}
if cfg.TxnLocalLatches.Enabled {
s.EnableTxnLocalLatches(cfg.TxnLocalLatches.Capacity)
}
return s, nil
}
// NewPDClient creates pd.Client with pdAddrs. // NewPDClient creates pd.Client with pdAddrs.
func NewPDClient(pdAddrs []string) (pd.Client, error) { func NewPDClient(pdAddrs []string) (pd.Client, error) {
cfg := config.GetGlobalConfig() cfg := config.GetGlobalConfig()

57
txnkv/client.go Normal file
View File

@ -0,0 +1,57 @@
// Copyright 2021 TiKV Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package txnkv
import (
"context"
"fmt"
"github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/tikv"
)
// Client is a txn client.
type Client struct {
*tikv.KVStore
}
// NewClient creates a txn client with pdAddrs.
func NewClient(pdAddrs []string) (*Client, error) {
cfg := config.GetGlobalConfig()
pdClient, err := tikv.NewPDClient(pdAddrs)
if err != nil {
return nil, err
}
// init uuid
uuid := fmt.Sprintf("tikv-%v", pdClient.GetClusterID(context.TODO()))
tlsConfig, err := cfg.Security.ToTLSConfig()
if err != nil {
return nil, err
}
spkv, err := tikv.NewEtcdSafePointKV(pdAddrs, tlsConfig)
if err != nil {
return nil, err
}
s, err := tikv.NewKVStore(uuid, pdClient, spkv, tikv.NewRPCClient(tikv.WithSecurity(cfg.Security)))
if err != nil {
return nil, err
}
if cfg.TxnLocalLatches.Enabled {
s.EnableTxnLocalLatches(cfg.TxnLocalLatches.Capacity)
}
return &Client{KVStore: s}, nil
}