Skip to content

Commit

Permalink
*: replace interface{} with any (#7774)
Browse files Browse the repository at this point in the history
ref #4399

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
  • Loading branch information
CabinfeverB authored Jan 31, 2024
1 parent 9721654 commit cd0ffba
Show file tree
Hide file tree
Showing 146 changed files with 663 additions and 650 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters:
- gosec
- bodyclose
- testifylint
- gofmt
disable:
- errcheck
linters-settings:
Expand Down Expand Up @@ -41,3 +42,10 @@ linters-settings:
- require-error
- suite-dont-use-pkg
- suite-extra-assert-call
gofmt:
# https://golangci-lint.run/usage/linters/#gofmt
# disable for faster check
simplify: false
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ static: install-tools pre-build

@ for mod in $(SUBMODULES); do cd $$mod && $(MAKE) static && cd $(ROOT_PATH) > /dev/null; done

# Because CI downloads the dashboard code and runs gofmt, we can't add this check into static now.
fmt:
@ echo "gofmt ..."
@ gofmt -s -l -w -r 'interface{} -> any' -d $(PACKAGE_DIRECTORIES) 2>&1 | awk '{ print } END { if (NR > 0) { exit 1 } }'

tidy:
@ go mod tidy
git diff go.mod go.sum | cat
Expand Down
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type Client interface {
// WatchGlobalConfig returns a stream with all global config and updates
WatchGlobalConfig(ctx context.Context, configPath string, revision int64) (chan []GlobalConfigItem, error)
// UpdateOption updates the client option.
UpdateOption(option DynamicOption, value interface{}) error
UpdateOption(option DynamicOption, value any) error

// GetExternalTimestamp returns external timestamp
GetExternalTimestamp(ctx context.Context) (uint64, error)
Expand Down Expand Up @@ -691,7 +691,7 @@ func (c *client) GetServiceDiscovery() ServiceDiscovery {
}

// UpdateOption updates the client option.
func (c *client) UpdateOption(option DynamicOption, value interface{}) error {
func (c *client) UpdateOption(option DynamicOption, value any) error {
switch option {
case MaxTSOBatchWaitInterval:
interval, ok := value.(time.Duration)
Expand Down
4 changes: 2 additions & 2 deletions client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
)

// respHandleFunc is the function to handle the HTTP response.
type respHandleFunc func(resp *http.Response, res interface{}) error
type respHandleFunc func(resp *http.Response, res any) error

// clientInner is the inner implementation of the PD HTTP client, which contains some fundamental fields.
// It is wrapped by the `client` struct to make sure the inner implementation won't be exposed and could
Expand Down Expand Up @@ -328,7 +328,7 @@ func (c *client) WithCallerID(callerID string) Client {

// WithRespHandler sets and returns a new client with the given HTTP response handler.
func (c *client) WithRespHandler(
handler func(resp *http.Response, res interface{}) error,
handler func(resp *http.Response, res any) error,
) Client {
newClient := *c
newClient.respHandler = handler
Expand Down
30 changes: 15 additions & 15 deletions client/http/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ type Client interface {
GetStore(context.Context, uint64) (*StoreInfo, error)
SetStoreLabels(context.Context, int64, map[string]string) error
/* Config-related interfaces */
GetConfig(context.Context) (map[string]interface{}, error)
SetConfig(context.Context, map[string]interface{}, ...float64) error
GetScheduleConfig(context.Context) (map[string]interface{}, error)
SetScheduleConfig(context.Context, map[string]interface{}) error
GetConfig(context.Context) (map[string]any, error)
SetConfig(context.Context, map[string]any, ...float64) error
GetScheduleConfig(context.Context) (map[string]any, error)
SetScheduleConfig(context.Context, map[string]any) error
GetClusterVersion(context.Context) (string, error)
GetCluster(context.Context) (*metapb.Cluster, error)
GetClusterStatus(context.Context) (*ClusterState, error)
GetReplicateConfig(context.Context) (map[string]interface{}, error)
GetReplicateConfig(context.Context) (map[string]any, error)
/* Scheduler-related interfaces */
GetSchedulers(context.Context) ([]string, error)
CreateScheduler(ctx context.Context, name string, storeID uint64) error
Expand Down Expand Up @@ -100,7 +100,7 @@ type Client interface {
// This allows the caller to customize how the response is handled, including error handling logic.
// Additionally, it is important for the caller to handle the content of the response body properly
// in order to ensure that it can be read and marshaled correctly into `res`.
WithRespHandler(func(resp *http.Response, res interface{}) error) Client
WithRespHandler(func(resp *http.Response, res any) error) Client
// WithBackoffer sets and returns a new client with the given backoffer.
WithBackoffer(*retry.Backoffer) Client
// Close gracefully closes the HTTP client.
Expand Down Expand Up @@ -323,8 +323,8 @@ func (c *client) SetStoreLabels(ctx context.Context, storeID int64, storeLabels
}

// GetConfig gets the configurations.
func (c *client) GetConfig(ctx context.Context) (map[string]interface{}, error) {
var config map[string]interface{}
func (c *client) GetConfig(ctx context.Context) (map[string]any, error) {
var config map[string]any
err := c.request(ctx, newRequestInfo().
WithName(getConfigName).
WithURI(Config).
Expand All @@ -337,7 +337,7 @@ func (c *client) GetConfig(ctx context.Context) (map[string]interface{}, error)
}

// SetConfig sets the configurations. ttlSecond is optional.
func (c *client) SetConfig(ctx context.Context, config map[string]interface{}, ttlSecond ...float64) error {
func (c *client) SetConfig(ctx context.Context, config map[string]any, ttlSecond ...float64) error {
configJSON, err := json.Marshal(config)
if err != nil {
return errors.Trace(err)
Expand All @@ -356,8 +356,8 @@ func (c *client) SetConfig(ctx context.Context, config map[string]interface{}, t
}

// GetScheduleConfig gets the schedule configurations.
func (c *client) GetScheduleConfig(ctx context.Context) (map[string]interface{}, error) {
var config map[string]interface{}
func (c *client) GetScheduleConfig(ctx context.Context) (map[string]any, error) {
var config map[string]any
err := c.request(ctx, newRequestInfo().
WithName(getScheduleConfigName).
WithURI(ScheduleConfig).
Expand All @@ -370,7 +370,7 @@ func (c *client) GetScheduleConfig(ctx context.Context) (map[string]interface{},
}

// SetScheduleConfig sets the schedule configurations.
func (c *client) SetScheduleConfig(ctx context.Context, config map[string]interface{}) error {
func (c *client) SetScheduleConfig(ctx context.Context, config map[string]any) error {
configJSON, err := json.Marshal(config)
if err != nil {
return errors.Trace(err)
Expand Down Expand Up @@ -453,8 +453,8 @@ func (c *client) GetClusterStatus(ctx context.Context) (*ClusterState, error) {
}

// GetReplicateConfig gets the replication configurations.
func (c *client) GetReplicateConfig(ctx context.Context) (map[string]interface{}, error) {
var config map[string]interface{}
func (c *client) GetReplicateConfig(ctx context.Context) (map[string]any, error) {
var config map[string]any
err := c.request(ctx, newRequestInfo().
WithName(getReplicateConfigName).
WithURI(ReplicateConfig).
Expand Down Expand Up @@ -694,7 +694,7 @@ func (c *client) GetSchedulers(ctx context.Context) ([]string, error) {

// CreateScheduler creates a scheduler to PD cluster.
func (c *client) CreateScheduler(ctx context.Context, name string, storeID uint64) error {
inputJSON, err := json.Marshal(map[string]interface{}{
inputJSON, err := json.Marshal(map[string]any{
"name": name,
"store_id": storeID,
})
Expand Down
4 changes: 2 additions & 2 deletions client/http/request_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type requestInfo struct {
uri string
method string
body []byte
res interface{}
res any
respHandler respHandleFunc
bo *retry.Backoffer
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func (ri *requestInfo) WithBody(body []byte) *requestInfo {
}

// WithResp sets the response struct of the request.
func (ri *requestInfo) WithResp(res interface{}) *requestInfo {
func (ri *requestInfo) WithResp(res any) *requestInfo {
ri.res = res
return ri
}
Expand Down
2 changes: 1 addition & 1 deletion client/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ type LabelRule struct {
Index int `json:"index"`
Labels []RegionLabel `json:"labels"`
RuleType string `json:"rule_type"`
Data interface{} `json:"data"`
Data any `json:"data"`
}

// LabelRulePatch is the patch to update the label rules.
Expand Down
2 changes: 1 addition & 1 deletion client/pd_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func (c *pdServiceDiscovery) checkFollowerHealth(ctx context.Context) {
func (c *pdServiceDiscovery) Close() {
c.closeOnce.Do(func() {
log.Info("[pd] close pd service discovery client")
c.clientConns.Range(func(key, cc interface{}) bool {
c.clientConns.Range(func(key, cc any) bool {
if err := cc.(*grpc.ClientConn).Close(); err != nil {
log.Error("[pd] failed to close grpc clientConn", errs.ZapError(errs.ErrCloseGRPCConn, err))
}
Expand Down
6 changes: 3 additions & 3 deletions client/tso_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type tsoRequest struct {
}

var tsoReqPool = sync.Pool{
New: func() interface{} {
New: func() any {
return &tsoRequest{
done: make(chan error, 1),
physical: 0,
Expand Down Expand Up @@ -137,7 +137,7 @@ func (c *tsoClient) Close() {
c.wg.Wait()

log.Info("close tso client")
c.tsoDispatcher.Range(func(_, dispatcherInterface interface{}) bool {
c.tsoDispatcher.Range(func(_, dispatcherInterface any) bool {
if dispatcherInterface != nil {
dispatcher := dispatcherInterface.(*tsoDispatcher)
tsoErr := errors.WithStack(errClosing)
Expand Down Expand Up @@ -237,7 +237,7 @@ func (c *tsoClient) updateTSOGlobalServAddr(addr string) error {

func (c *tsoClient) gcAllocatorServingAddr(curAllocatorMap map[string]string) {
// Clean up the old TSO allocators
c.tsoAllocators.Range(func(dcLocationKey, _ interface{}) bool {
c.tsoAllocators.Range(func(dcLocationKey, _ any) bool {
dcLocation := dcLocationKey.(string)
// Skip the Global TSO Allocator
if dcLocation == globalDCLocation {
Expand Down
14 changes: 7 additions & 7 deletions client/tso_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ func (req *tsoRequest) Wait() (physical int64, logical int64, err error) {

func (c *tsoClient) updateTSODispatcher() {
// Set up the new TSO dispatcher and batch controller.
c.GetTSOAllocators().Range(func(dcLocationKey, _ interface{}) bool {
c.GetTSOAllocators().Range(func(dcLocationKey, _ any) bool {
dcLocation := dcLocationKey.(string)
if !c.checkTSODispatcher(dcLocation) {
c.createTSODispatcher(dcLocation)
}
return true
})
// Clean up the unused TSO dispatcher
c.tsoDispatcher.Range(func(dcLocationKey, dispatcher interface{}) bool {
c.tsoDispatcher.Range(func(dcLocationKey, dispatcher any) bool {
dcLocation := dcLocationKey.(string)
// Skip the Global TSO Allocator
if dcLocation == globalDCLocation {
Expand Down Expand Up @@ -173,7 +173,7 @@ func (c *tsoClient) tsCancelLoop() {
defer ticker.Stop()
for {
// Watch every dc-location's tsDeadlineCh
c.GetTSOAllocators().Range(func(dcLocation, _ interface{}) bool {
c.GetTSOAllocators().Range(func(dcLocation, _ any) bool {
c.watchTSDeadline(tsCancelLoopCtx, dcLocation.(string))
return true
})
Expand Down Expand Up @@ -343,7 +343,7 @@ func (c *tsoClient) handleDispatcher(
defer func() {
log.Info("[tso] exit tso dispatcher", zap.String("dc-location", dc))
// Cancel all connections.
connectionCtxs.Range(func(_, cc interface{}) bool {
connectionCtxs.Range(func(_, cc any) bool {
cc.(*tsoConnectionContext).cancel()
return true
})
Expand Down Expand Up @@ -532,7 +532,7 @@ func (c *tsoClient) allowTSOFollowerProxy(dc string) bool {
// connectionCtxs will only have only one stream to choose when the TSO Follower Proxy is off.
func (c *tsoClient) chooseStream(connectionCtxs *sync.Map) (connectionCtx *tsoConnectionContext) {
idx := 0
connectionCtxs.Range(func(_, cc interface{}) bool {
connectionCtxs.Range(func(_, cc any) bool {
j := rand.Intn(idx + 1)
if j < 1 {
connectionCtx = cc.(*tsoConnectionContext)
Expand Down Expand Up @@ -587,7 +587,7 @@ func (c *tsoClient) tryConnectToTSO(
cc.(*tsoConnectionContext).cancel()
connectionCtxs.Store(newAddr, connectionCtx)
}
connectionCtxs.Range(func(addr, cc interface{}) bool {
connectionCtxs.Range(func(addr, cc any) bool {
if addr.(string) != newAddr {
cc.(*tsoConnectionContext).cancel()
connectionCtxs.Delete(addr)
Expand Down Expand Up @@ -700,7 +700,7 @@ func (c *tsoClient) tryConnectToTSOWithProxy(dispatcherCtx context.Context, dc s
return errors.Errorf("cannot find the allocator leader in %s", dc)
}
// GC the stale one.
connectionCtxs.Range(func(addr, cc interface{}) bool {
connectionCtxs.Range(func(addr, cc any) bool {
if _, ok := tsoStreamBuilders[addr.(string)]; !ok {
cc.(*tsoConnectionContext).cancel()
connectionCtxs.Delete(addr)
Expand Down
2 changes: 1 addition & 1 deletion client/tso_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (c *tsoServiceDiscovery) Close() {
c.cancel()
c.wg.Wait()

c.clientConns.Range(func(key, cc interface{}) bool {
c.clientConns.Range(func(key, cc any) bool {
if err := cc.(*grpc.ClientConn).Close(); err != nil {
log.Error("[tso] failed to close gRPC clientConn", errs.ZapError(errs.ErrCloseGRPCConn, err))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/autoscaling/calculation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestGetScaledTiKVGroups(t *testing.T) {
informer core.StoreSetInformer
healthyInstances []instance
expectedPlan []*Plan
errorChecker func(err error, msgAndArgs ...interface{})
errorChecker func(err error, msgAndArgs ...any)
}{
{
name: "no scaled tikv group",
Expand Down
6 changes: 3 additions & 3 deletions pkg/autoscaling/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ type data struct {
}

type result struct {
Metric metric `json:"metric"`
Value []interface{} `json:"value"`
Metric metric `json:"metric"`
Value []any `json:"value"`
}

type metric struct {
Expand Down Expand Up @@ -121,7 +121,7 @@ func (c *normalClient) buildCPUMockData(component ComponentType) {
var results []result
for i := 0; i < instanceCount; i++ {
results = append(results, result{
Value: []interface{}{time.Now().Unix(), fmt.Sprintf("%f", mockResultValue)},
Value: []any{time.Now().Unix(), fmt.Sprintf("%f", mockResultValue)},
Metric: metric{
Instance: pods[i],
Cluster: mockClusterName,
Expand Down
2 changes: 1 addition & 1 deletion pkg/btree/btree_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func allrev[T Item[T]](t *BTreeG[T]) (out []T) {
return
}

func assertEq(t *testing.T, desc string, got, need interface{}) {
func assertEq(t *testing.T, desc string, got, need any) {
if !reflect.DeepEqual(need, got) {
t.Fatalf("%s failed: need %T %v, but got %T %v", desc, need, need, got, got)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import "github.com/tikv/pd/pkg/utils/syncutil"
// Cache is an interface for cache system
type Cache interface {
// Put puts an item into cache.
Put(key uint64, value interface{})
Put(key uint64, value any)
// Get retrieves an item from cache.
Get(key uint64) (interface{}, bool)
Get(key uint64) (any, bool)
// Peek reads an item from cache. The action is no considered 'Use'.
Peek(key uint64) (interface{}, bool)
Peek(key uint64) (any, bool)
// Remove eliminates an item from cache.
Remove(key uint64)
// Elems return all items in cache.
Expand Down Expand Up @@ -59,7 +59,7 @@ func newThreadSafeCache(cache Cache) Cache {
}

// Put puts an item into cache.
func (c *threadSafeCache) Put(key uint64, value interface{}) {
func (c *threadSafeCache) Put(key uint64, value any) {
c.lock.Lock()
defer c.lock.Unlock()
c.cache.Put(key, value)
Expand All @@ -68,14 +68,14 @@ func (c *threadSafeCache) Put(key uint64, value interface{}) {
// Get retrieves an item from cache.
// When Get method called, LRU and TwoQueue cache will rearrange entries
// so we must use write lock.
func (c *threadSafeCache) Get(key uint64) (interface{}, bool) {
func (c *threadSafeCache) Get(key uint64) (any, bool) {
c.lock.Lock()
defer c.lock.Unlock()
return c.cache.Get(key)
}

// Peek reads an item from cache. The action is no considered 'Use'.
func (c *threadSafeCache) Peek(key uint64) (interface{}, bool) {
func (c *threadSafeCache) Peek(key uint64) (any, bool) {
c.lock.RLock()
defer c.lock.RUnlock()
return c.cache.Peek(key)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func TestFifoFromLastSameElems(t *testing.T) {
cache.Put(1, &testStruct{value: "3"})
fun := func() []*Item {
return cache.FromLastSameElems(
func(i interface{}) (bool, string) {
func(i any) (bool, string) {
result, ok := i.(*testStruct)
if result == nil {
return ok, ""
Expand Down
Loading

0 comments on commit cd0ffba

Please sign in to comment.