txnkv: add shortcut to get global timestamp (#395)

Signed-off-by: disksing <i@disksing.com>
This commit is contained in:
disksing 2022-02-11 12:40:35 +08:00 committed by GitHub
parent b5eb031edd
commit b7a3ace8e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -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
}