From b7a3ace8e6f211cb4fef0ee91af7d54761c855cb Mon Sep 17 00:00:00 2001 From: disksing Date: Fri, 11 Feb 2022 12:40:35 +0800 Subject: [PATCH] txnkv: add shortcut to get global timestamp (#395) Signed-off-by: disksing --- txnkv/client.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/txnkv/client.go b/txnkv/client.go index dc00239a..3b3e7348 100644 --- a/txnkv/client.go +++ b/txnkv/client.go @@ -19,7 +19,10 @@ import ( "fmt" "github.com/tikv/client-go/v2/config" + "github.com/tikv/client-go/v2/internal/retry" + "github.com/tikv/client-go/v2/oracle" "github.com/tikv/client-go/v2/tikv" + "github.com/tikv/client-go/v2/txnkv/transaction" ) // Client is a txn client. @@ -55,3 +58,13 @@ func NewClient(pdAddrs []string) (*Client, error) { } return &Client{KVStore: s}, nil } + +// GetTimestamp returns the current global timestamp. +func (c *Client) GetTimestamp(ctx context.Context) (uint64, error) { + bo := retry.NewBackofferWithVars(ctx, transaction.TsoMaxBackoff, nil) + startTS, err := c.GetTimestampWithRetry(bo, oracle.GlobalTxnScope) + if err != nil { + return 0, err + } + return startTS, nil +}