Skip to content

Commit

Permalink
Merge branch 'master' into patrol-concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot[bot] authored Oct 29, 2024
2 parents e570e3c + 20087e2 commit 74d4fdc
Show file tree
Hide file tree
Showing 290 changed files with 3,529 additions and 1,808 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Make Check
run: |
SWAGGER=1 make build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pd-docker-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ jobs:
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Make
run: make docker-image
2 changes: 1 addition & 1 deletion .github/workflows/pd-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: ${{ matrix.name }}
env:
WORKER_ID: ${{ matrix.worker_id }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tso-consistency-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Make TSO Consistency Test
run: make test-tso-consistency
2 changes: 1 addition & 1 deletion .github/workflows/tso-function-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.23'
- name: Make TSO Function Test
run: make test-tso-function
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ go.work*
embedded_assets_handler.go
*.log
*.bin
third_bin
19 changes: 18 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ linters:
- gofmt
- revive
- errcheck
- exportloopref
- copyloopvar
- goimports
- depguard
- asasalint
- asciicheck
- bidichk
- durationcheck
- gocheckcompilerdirectives
- gochecksumtype
- makezero
- protogetter
- reassign
- intrange
linters-settings:
gocritic:
# Which checks should be disabled; can't be combined with 'enabled-checks'; default is empty
Expand All @@ -32,18 +42,25 @@ linters-settings:
excludes:
- G402
- G404
# TODO: enable G115 after fixing the issues
- G115
testifylint:
enable:
- blank-import
- bool-compare
- compares
- empty
- error-is-as
- error-nil
- expected-actual
- formatter
- len
- negative-positive
- require-error
- suite-dont-use-pkg
- suite-extra-assert-call
- suite-subtest-run
- useless-assert
disable:
- float-compare
- go-require
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21-alpine as builder
FROM golang:1.23-alpine as builder

RUN apk add --no-cache \
make \
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ SHELL := env PATH='$(PATH)' GOBIN='$(GO_TOOLS_BIN_PATH)' $(shell which bash)

install-tools:
@mkdir -p $(GO_TOOLS_BIN_PATH)
@which golangci-lint >/dev/null 2>&1 || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GO_TOOLS_BIN_PATH) v1.56.2
@which golangci-lint >/dev/null 2>&1 || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GO_TOOLS_BIN_PATH) v1.61.0
@grep '_' tools.go | sed 's/"//g' | awk '{print $$2}' | xargs go install

.PHONY: install-tools
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ If you're interested in contributing to PD, see [CONTRIBUTING.md](./CONTRIBUTING

## Build

1. Make sure [*Go*](https://golang.org/) (version 1.21) is installed.
1. Make sure [*Go*](https://golang.org/) (version 1.23) is installed.
2. Use `make` to install PD. `pd-server` will be installed in the `bin` directory.

## Usage
Expand Down
173 changes: 18 additions & 155 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/tikv/pd/client/tlsutil"
"github.com/tikv/pd/client/tsoutil"
"go.uber.org/zap"
"google.golang.org/grpc"
)

const (
Expand Down Expand Up @@ -174,69 +173,6 @@ type Client interface {
Close()
}

// GetStoreOp represents available options when getting stores.
type GetStoreOp struct {
excludeTombstone bool
}

// GetStoreOption configures GetStoreOp.
type GetStoreOption func(*GetStoreOp)

// WithExcludeTombstone excludes tombstone stores from the result.
func WithExcludeTombstone() GetStoreOption {
return func(op *GetStoreOp) { op.excludeTombstone = true }
}

// RegionsOp represents available options when operate regions
type RegionsOp struct {
group string
retryLimit uint64
skipStoreLimit bool
}

// RegionsOption configures RegionsOp
type RegionsOption func(op *RegionsOp)

// WithGroup specify the group during Scatter/Split Regions
func WithGroup(group string) RegionsOption {
return func(op *RegionsOp) { op.group = group }
}

// WithRetry specify the retry limit during Scatter/Split Regions
func WithRetry(retry uint64) RegionsOption {
return func(op *RegionsOp) { op.retryLimit = retry }
}

// WithSkipStoreLimit specify if skip the store limit check during Scatter/Split Regions
func WithSkipStoreLimit() RegionsOption {
return func(op *RegionsOp) { op.skipStoreLimit = true }
}

// GetRegionOp represents available options when getting regions.
type GetRegionOp struct {
needBuckets bool
allowFollowerHandle bool
outputMustContainAllKeyRange bool
}

// GetRegionOption configures GetRegionOp.
type GetRegionOption func(op *GetRegionOp)

// WithBuckets means getting region and its buckets.
func WithBuckets() GetRegionOption {
return func(op *GetRegionOp) { op.needBuckets = true }
}

// WithAllowFollowerHandle means that client can send request to follower and let it handle this request.
func WithAllowFollowerHandle() GetRegionOption {
return func(op *GetRegionOp) { op.allowFollowerHandle = true }
}

// WithOutputMustContainAllKeyRange means the output must contain all key ranges.
func WithOutputMustContainAllKeyRange() GetRegionOption {
return func(op *GetRegionOp) { op.outputMustContainAllKeyRange = true }
}

var (
// errUnmatchedClusterID is returned when found a PD with a different cluster ID.
errUnmatchedClusterID = errors.New("[pd] unmatched cluster id")
Expand All @@ -250,60 +186,6 @@ var (
errNoServiceModeReturned = errors.New("[pd] no service mode returned")
)

// ClientOption configures client.
type ClientOption func(c *client)

// WithGRPCDialOptions configures the client with gRPC dial options.
func WithGRPCDialOptions(opts ...grpc.DialOption) ClientOption {
return func(c *client) {
c.option.gRPCDialOptions = append(c.option.gRPCDialOptions, opts...)
}
}

// WithCustomTimeoutOption configures the client with timeout option.
func WithCustomTimeoutOption(timeout time.Duration) ClientOption {
return func(c *client) {
c.option.timeout = timeout
}
}

// WithForwardingOption configures the client with forwarding option.
func WithForwardingOption(enableForwarding bool) ClientOption {
return func(c *client) {
c.option.enableForwarding = enableForwarding
}
}

// WithTSOServerProxyOption configures the client to use TSO server proxy,
// i.e., the client will send TSO requests to the API leader (the TSO server
// proxy) which will forward the requests to the TSO servers.
func WithTSOServerProxyOption(useTSOServerProxy bool) ClientOption {
return func(c *client) {
c.option.useTSOServerProxy = useTSOServerProxy
}
}

// WithMaxErrorRetry configures the client max retry times when connect meets error.
func WithMaxErrorRetry(count int) ClientOption {
return func(c *client) {
c.option.maxRetryTimes = count
}
}

// WithMetricsLabels configures the client with metrics labels.
func WithMetricsLabels(labels prometheus.Labels) ClientOption {
return func(c *client) {
c.option.metricsLabels = labels
}
}

// WithInitMetricsOption configures the client with metrics labels.
func WithInitMetricsOption(initMetrics bool) ClientOption {
return func(c *client) {
c.option.initMetrics = initMetrics
}
}

var _ Client = (*client)(nil)

// serviceModeKeeper is for service mode switching.
Expand All @@ -324,9 +206,7 @@ func (k *serviceModeKeeper) close() {
k.tsoSvcDiscovery.Close()
fallthrough
case pdpb.ServiceMode_PD_SVC_MODE:
if k.tsoClient != nil {
k.tsoClient.close()
}
k.tsoClient.close()
case pdpb.ServiceMode_UNKNOWN_SVC_MODE:
}
}
Expand Down Expand Up @@ -560,6 +440,7 @@ func newClientWithKeyspaceName(
}
clientCtx, clientCancel := context.WithCancel(ctx)
c := &client{
keyspaceID: nullKeyspaceID,
updateTokenConnectionCh: make(chan struct{}, 1),
ctx: clientCtx,
cancel: clientCancel,
Expand All @@ -573,19 +454,21 @@ func newClientWithKeyspaceName(
opt(c)
}

updateKeyspaceIDCb := func() error {
if err := c.initRetry(c.loadKeyspaceMeta, keyspaceName); err != nil {
updateKeyspaceIDFunc := func() error {
keyspaceMeta, err := c.LoadKeyspace(clientCtx, keyspaceName)
if err != nil {
return err
}
c.keyspaceID = keyspaceMeta.GetId()
// c.keyspaceID is the source of truth for keyspace id.
c.pdSvcDiscovery.SetKeyspaceID(c.keyspaceID)
return nil
}

// Create a PD service discovery with null keyspace id, then query the real id with the keyspace name,
// finally update the keyspace id to the PD service discovery for the following interactions.
c.pdSvcDiscovery = newPDServiceDiscovery(
clientCtx, clientCancel, &c.wg, c.setServiceMode, updateKeyspaceIDCb, nullKeyspaceID, c.svrUrls, c.tlsCfg, c.option)
c.pdSvcDiscovery = newPDServiceDiscovery(clientCtx, clientCancel, &c.wg,
c.setServiceMode, updateKeyspaceIDFunc, nullKeyspaceID, c.svrUrls, c.tlsCfg, c.option)
if err := c.setup(); err != nil {
c.cancel()
if c.pdSvcDiscovery != nil {
Expand All @@ -600,32 +483,6 @@ func newClientWithKeyspaceName(
return c, nil
}

func (c *client) initRetry(f func(s string) error, str string) error {
var err error
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for i := 0; i < c.option.maxRetryTimes; i++ {
if err = f(str); err == nil {
return nil
}
select {
case <-c.ctx.Done():
return err
case <-ticker.C:
}
}
return errors.WithStack(err)
}

func (c *client) loadKeyspaceMeta(keyspace string) error {
keyspaceMeta, err := c.LoadKeyspace(context.TODO(), keyspace)
if err != nil {
return err
}
c.keyspaceID = keyspaceMeta.GetId()
return nil
}

func (c *client) setup() error {
// Init the metrics.
if c.option.initMetrics {
Expand Down Expand Up @@ -697,7 +554,7 @@ func (c *client) resetTSOClientLocked(mode pdpb.ServiceMode) {
case pdpb.ServiceMode_API_SVC_MODE:
newTSOSvcDiscovery = newTSOServiceDiscovery(
c.ctx, MetaStorageClient(c), c.pdSvcDiscovery,
c.GetClusterID(c.ctx), c.keyspaceID, c.tlsCfg, c.option)
c.keyspaceID, c.tlsCfg, c.option)
// At this point, the keyspace group isn't known yet. Starts from the default keyspace group,
// and will be updated later.
newTSOCli = newTSOClient(c.ctx, c.option,
Expand Down Expand Up @@ -797,6 +654,12 @@ func (c *client) UpdateOption(option DynamicOption, value any) error {
return errors.New("[pd] invalid value type for EnableFollowerHandle option, it should be bool")
}
c.option.setEnableFollowerHandle(enable)
case TSOClientRPCConcurrency:
value, ok := value.(int)
if !ok {
return errors.New("[pd] invalid value type for TSOClientRPCConcurrency option, it should be int")
}
c.option.setTSOClientRPCConcurrency(value)
default:
return errors.New("[pd] unsupported client option")
}
Expand Down Expand Up @@ -876,7 +739,7 @@ func (c *client) dispatchTSORequestWithRetry(ctx context.Context, dcLocation str
err error
req *tsoRequest
)
for i := 0; i < dispatchRetryCount; i++ {
for i := range dispatchRetryCount {
// Do not delay for the first time.
if i > 0 {
time.Sleep(dispatchRetryDelay)
Expand Down Expand Up @@ -1699,7 +1562,7 @@ func (c *client) GetExternalTimestamp(ctx context.Context) (uint64, error) {
}
resErr := resp.GetHeader().GetError()
if resErr != nil {
return 0, errors.Errorf("[pd]" + resErr.Message)
return 0, errors.New("[pd]" + resErr.Message)
}
return resp.GetTimestamp(), nil
}
Expand All @@ -1721,7 +1584,7 @@ func (c *client) SetExternalTimestamp(ctx context.Context, timestamp uint64) err
}
resErr := resp.GetHeader().GetError()
if resErr != nil {
return errors.Errorf("[pd]" + resErr.Message)
return errors.New("[pd]" + resErr.Message)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion client/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/tikv/pd/client

go 1.21
go 1.23

require (
github.com/BurntSushi/toml v0.3.1
Expand Down
Loading

0 comments on commit 74d4fdc

Please sign in to comment.