Skip to content

Commit

Permalink
prefix safepoint kv with keyspace name (tikv#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmoebaProtozoa authored and zeminzhou committed Feb 28, 2024
1 parent aafb0ca commit d09d52b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
28 changes: 27 additions & 1 deletion tikv/safepoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/tikv/client-go/v2/internal/logutil"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/namespace"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -117,17 +118,42 @@ func (w *MockSafePointKV) Close() error {
return nil
}

// option represents safePoint kv configuration.
type option struct {
prefix string
}

// SafePointKVOpt is used to set safePoint kv option.
type SafePointKVOpt func(*option)

func WithPrefix(prefix string) SafePointKVOpt {
return func(opt *option) {
opt.prefix = prefix
}
}

// EtcdSafePointKV implements SafePointKV at runtime
type EtcdSafePointKV struct {
cli *clientv3.Client
}

// NewEtcdSafePointKV creates an instance of EtcdSafePointKV
func NewEtcdSafePointKV(addrs []string, tlsConfig *tls.Config) (*EtcdSafePointKV, error) {
func NewEtcdSafePointKV(addrs []string, tlsConfig *tls.Config, opts ...SafePointKVOpt) (*EtcdSafePointKV, error) {
// Apply options.
opt := &option{}
for _, o := range opts {
o(opt)
}
etcdCli, err := createEtcdKV(addrs, tlsConfig)
if err != nil {
return nil, err
}
// If a prefix is specified, wrap the etcd client with the target namespace.
if opt.prefix != "" {
etcdCli.KV = namespace.NewKV(etcdCli.KV, opt.prefix)
etcdCli.Watcher = namespace.NewWatcher(etcdCli.Watcher, opt.prefix)
etcdCli.Lease = namespace.NewLease(etcdCli.Lease, opt.prefix)
}
return &EtcdSafePointKV{cli: etcdCli}, nil
}

Expand Down
10 changes: 9 additions & 1 deletion txnkv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Client struct {
type option struct {
apiVersion kvrpcpb.APIVersion
keyspaceName string
spKVPrefix string
}

// ClientOpt is factory to set the client options.
Expand All @@ -55,6 +56,13 @@ func WithAPIVersion(apiVersion kvrpcpb.APIVersion) ClientOpt {
}
}

// WithSafePointKVPrefix is used to set client's safe point kv prefix.
func WithSafePointKVPrefix(prefix string) ClientOpt {
return func(opt *option) {
opt.spKVPrefix = prefix
}
}

// NewClient creates a txn client with pdAddrs.
func NewClient(pdAddrs []string, opts ...ClientOpt) (*Client, error) {
// Apply options.
Expand Down Expand Up @@ -94,7 +102,7 @@ func NewClient(pdAddrs []string, opts ...ClientOpt) (*Client, error) {
return nil, err
}

spkv, err := tikv.NewEtcdSafePointKV(pdAddrs, tlsConfig)
spkv, err := tikv.NewEtcdSafePointKV(pdAddrs, tlsConfig, tikv.WithPrefix(opt.spKVPrefix))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d09d52b

Please sign in to comment.