github.com/KinWaiYuen/client-go/v2@v2.5.4/config/client.go (about)

     1  // Copyright 2021 TiKV Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/config/client.go
    19  //
    20  
    21  // Copyright 2021 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package config
    36  
    37  import (
    38  	"fmt"
    39  	"time"
    40  
    41  	"google.golang.org/grpc/encoding/gzip"
    42  )
    43  
    44  const (
    45  	// DefStoreLivenessTimeout is the default value for store liveness timeout.
    46  	DefStoreLivenessTimeout = "1s"
    47  )
    48  
    49  // TiKVClient is the config for tikv client.
    50  type TiKVClient struct {
    51  	// GrpcConnectionCount is the max gRPC connections that will be established
    52  	// with each tikv-server.
    53  	GrpcConnectionCount uint `toml:"grpc-connection-count" json:"grpc-connection-count"`
    54  	// After a duration of this time in seconds if the client doesn't see any activity it pings
    55  	// the server to see if the transport is still alive.
    56  	GrpcKeepAliveTime uint `toml:"grpc-keepalive-time" json:"grpc-keepalive-time"`
    57  	// After having pinged for keepalive check, the client waits for a duration of Timeout in seconds
    58  	// and if no activity is seen even after that the connection is closed.
    59  	GrpcKeepAliveTimeout uint `toml:"grpc-keepalive-timeout" json:"grpc-keepalive-timeout"`
    60  	// GrpcCompressionType is the compression type for gRPC channel: none or gzip.
    61  	GrpcCompressionType string `toml:"grpc-compression-type" json:"grpc-compression-type"`
    62  	// CommitTimeout is the max time which command 'commit' will wait.
    63  	CommitTimeout string      `toml:"commit-timeout" json:"commit-timeout"`
    64  	AsyncCommit   AsyncCommit `toml:"async-commit" json:"async-commit"`
    65  	// MaxBatchSize is the max batch size when calling batch commands API.
    66  	MaxBatchSize uint `toml:"max-batch-size" json:"max-batch-size"`
    67  	// If TiKV load is greater than this, TiDB will wait for a while to avoid little batch.
    68  	OverloadThreshold uint `toml:"overload-threshold" json:"overload-threshold"`
    69  	// MaxBatchWaitTime in nanosecond is the max wait time for batch.
    70  	MaxBatchWaitTime time.Duration `toml:"max-batch-wait-time" json:"max-batch-wait-time"`
    71  	// BatchWaitSize is the max wait size for batch.
    72  	BatchWaitSize uint `toml:"batch-wait-size" json:"batch-wait-size"`
    73  	// EnableChunkRPC indicate the data encode in chunk format for coprocessor requests.
    74  	EnableChunkRPC bool `toml:"enable-chunk-rpc" json:"enable-chunk-rpc"`
    75  	// If a Region has not been accessed for more than the given duration (in seconds), it
    76  	// will be reloaded from the PD.
    77  	RegionCacheTTL uint `toml:"region-cache-ttl" json:"region-cache-ttl"`
    78  	// If a store has been up to the limit, it will return error for successive request to
    79  	// prevent the store occupying too much token in dispatching level.
    80  	StoreLimit int64 `toml:"store-limit" json:"store-limit"`
    81  	// StoreLivenessTimeout is the timeout for store liveness check request.
    82  	StoreLivenessTimeout string           `toml:"store-liveness-timeout" json:"store-liveness-timeout"`
    83  	CoprCache            CoprocessorCache `toml:"copr-cache" json:"copr-cache"`
    84  	// TTLRefreshedTxnSize controls whether a transaction should update its TTL or not.
    85  	TTLRefreshedTxnSize      int64  `toml:"ttl-refreshed-txn-size" json:"ttl-refreshed-txn-size"`
    86  	ResolveLockLiteThreshold uint64 `toml:"resolve-lock-lite-threshold" json:"resolve-lock-lite-threshold"`
    87  }
    88  
    89  // AsyncCommit is the config for the async commit feature. The switch to enable it is a system variable.
    90  type AsyncCommit struct {
    91  	// Use async commit only if the number of keys does not exceed KeysLimit.
    92  	KeysLimit uint `toml:"keys-limit" json:"keys-limit"`
    93  	// Use async commit only if the total size of keys does not exceed TotalKeySizeLimit.
    94  	TotalKeySizeLimit uint64 `toml:"total-key-size-limit" json:"total-key-size-limit"`
    95  	// The duration within which is safe for async commit or 1PC to commit with an old schema.
    96  	// The following two fields should NOT be modified in most cases. If both async commit
    97  	// and 1PC are disabled in the whole cluster, they can be set to zero to avoid waiting in DDLs.
    98  	SafeWindow time.Duration `toml:"safe-window" json:"safe-window"`
    99  	// The duration in addition to SafeWindow to make DDL safe.
   100  	AllowedClockDrift time.Duration `toml:"allowed-clock-drift" json:"allowed-clock-drift"`
   101  }
   102  
   103  // CoprocessorCache is the config for coprocessor cache.
   104  type CoprocessorCache struct {
   105  	// The capacity in MB of the cache. Zero means disable coprocessor cache.
   106  	CapacityMB float64 `toml:"capacity-mb" json:"capacity-mb"`
   107  
   108  	// No json fields for below config. Intend to hide them.
   109  
   110  	// Only cache requests that containing small number of ranges. May to be changed in future.
   111  	AdmissionMaxRanges uint64 `toml:"admission-max-ranges" json:"-"`
   112  	// Only cache requests whose result set is small.
   113  	AdmissionMaxResultMB float64 `toml:"admission-max-result-mb" json:"-"`
   114  	// Only cache requests takes notable time to process.
   115  	AdmissionMinProcessMs uint64 `toml:"admission-min-process-ms" json:"-"`
   116  }
   117  
   118  // DefaultTiKVClient returns default config for TiKVClient.
   119  func DefaultTiKVClient() TiKVClient {
   120  	return TiKVClient{
   121  		GrpcConnectionCount:  4,
   122  		GrpcKeepAliveTime:    10,
   123  		GrpcKeepAliveTimeout: 3,
   124  		GrpcCompressionType:  "none",
   125  		CommitTimeout:        "41s",
   126  		AsyncCommit: AsyncCommit{
   127  			// FIXME: Find an appropriate default limit.
   128  			KeysLimit:         256,
   129  			TotalKeySizeLimit: 4 * 1024, // 4 KiB
   130  			SafeWindow:        2 * time.Second,
   131  			AllowedClockDrift: 500 * time.Millisecond,
   132  		},
   133  
   134  		MaxBatchSize:      128,
   135  		OverloadThreshold: 200,
   136  		MaxBatchWaitTime:  0,
   137  		BatchWaitSize:     8,
   138  
   139  		EnableChunkRPC: true,
   140  
   141  		RegionCacheTTL:       600,
   142  		StoreLimit:           0,
   143  		StoreLivenessTimeout: DefStoreLivenessTimeout,
   144  
   145  		TTLRefreshedTxnSize: 32 * 1024 * 1024,
   146  
   147  		CoprCache: CoprocessorCache{
   148  			CapacityMB:            1000,
   149  			AdmissionMaxRanges:    500,
   150  			AdmissionMaxResultMB:  10,
   151  			AdmissionMinProcessMs: 5,
   152  		},
   153  
   154  		ResolveLockLiteThreshold: 16,
   155  	}
   156  }
   157  
   158  // Valid checks if this config is valid.
   159  func (config *TiKVClient) Valid() error {
   160  	if config.GrpcConnectionCount == 0 {
   161  		return fmt.Errorf("grpc-connection-count should be greater than 0")
   162  	}
   163  	if config.GrpcCompressionType != "none" && config.GrpcCompressionType != gzip.Name {
   164  		return fmt.Errorf("grpc-compression-type should be none or %s, but got %s", gzip.Name, config.GrpcCompressionType)
   165  	}
   166  	return nil
   167  }