Skip to content

Commit

Permalink
ci: make golangci happy (#1066)
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp authored Nov 21, 2023
1 parent 1c2351c commit 812a791
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
6 changes: 3 additions & 3 deletions internal/client/client_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/internal/client/mock_server"
"github.com/tikv/client-go/v2/internal/client/mockserver"
"github.com/tikv/client-go/v2/tikvrpc"
)

func TestPanicInRecvLoop(t *testing.T) {
require.Nil(t, failpoint.Enable("tikvclient/panicInFailPendingRequests", `panic`))
require.Nil(t, failpoint.Enable("tikvclient/gotErrorInRecvLoop", `return("0")`))

server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
require.True(t, port > 0)
defer server.Stop()

Expand All @@ -81,7 +81,7 @@ func TestPanicInRecvLoop(t *testing.T) {
}

func TestRecvErrorInMultipleRecvLoops(t *testing.T) {
server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
require.True(t, port > 0)
defer server.Stop()
addr := server.Addr()
Expand Down
10 changes: 5 additions & 5 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/internal/client/mock_server"
"github.com/tikv/client-go/v2/internal/client/mockserver"
"github.com/tikv/client-go/v2/internal/logutil"
"github.com/tikv/client-go/v2/tikvrpc"
"go.uber.org/zap"
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestCancelTimeoutRetErr(t *testing.T) {
}

func TestSendWhenReconnect(t *testing.T) {
server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
require.True(t, port > 0)

rpcClient := NewRPCClient()
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestCollapseResolveLock(t *testing.T) {
}

func TestForwardMetadataByUnaryCall(t *testing.T) {
server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
require.True(t, port > 0)
defer server.Stop()
addr := fmt.Sprintf("%s:%d", "127.0.0.1", port)
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestForwardMetadataByUnaryCall(t *testing.T) {
}

func TestForwardMetadataByBatchCommands(t *testing.T) {
server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
require.True(t, port > 0)
defer server.Stop()
addr := server.Addr()
Expand Down Expand Up @@ -650,7 +650,7 @@ func TestBatchClientRecoverAfterServerRestart(t *testing.T) {
conf.TiKVClient.MaxBatchSize = 128
})()

server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
require.True(t, port > 0)
require.True(t, server.IsRunning())
addr := server.Addr()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/client/mock_tikv_service_test.go
//

package mock_server
package mockserver

import (
"context"
Expand All @@ -36,6 +36,7 @@ import (
"google.golang.org/grpc"
)

// MockServer is a mock tikv server for testing purpose.
type MockServer struct {
tikvpb.TikvServer
grpcServer *grpc.Server
Expand All @@ -49,27 +50,31 @@ type MockServer struct {
}
}

// KvGet implements the TikvServer interface.
func (s *MockServer) KvGet(ctx context.Context, req *kvrpcpb.GetRequest) (*kvrpcpb.GetResponse, error) {
if err := s.checkMetadata(ctx); err != nil {
return nil, err
}
return &kvrpcpb.GetResponse{}, nil
}

// KvScan implements the TikvServer interface.
func (s *MockServer) KvPrewrite(ctx context.Context, req *kvrpcpb.PrewriteRequest) (*kvrpcpb.PrewriteResponse, error) {
if err := s.checkMetadata(ctx); err != nil {
return nil, err
}
return &kvrpcpb.PrewriteResponse{}, nil
}

// KvCommit implements the TikvServer interface.
func (s *MockServer) CoprocessorStream(req *coprocessor.Request, ss tikvpb.Tikv_CoprocessorStreamServer) error {
if err := s.checkMetadata(ss.Context()); err != nil {
return err
}
return ss.Send(&coprocessor.Response{})
}

// KvBatchGet implements the TikvServer interface.
func (s *MockServer) BatchCommands(ss tikvpb.Tikv_BatchCommandsServer) error {
if err := s.checkMetadata(ss.Context()); err != nil {
return err
Expand Down Expand Up @@ -101,6 +106,7 @@ func (s *MockServer) BatchCommands(ss tikvpb.Tikv_BatchCommandsServer) error {
}
}

// SetMetaChecker set the meta checker for mock server.
func (s *MockServer) SetMetaChecker(check func(context.Context) error) {
s.metaChecker.Lock()
s.metaChecker.check = check
Expand All @@ -116,19 +122,23 @@ func (s *MockServer) checkMetadata(ctx context.Context) error {
return nil
}

// IsRunning returns true is the mock server is running.
func (s *MockServer) IsRunning() bool {
return atomic.LoadInt64(&s.running) == 1
}

// Addr returns the address of the mock server.
func (s *MockServer) Addr() string {
return s.addr
}

// Stop stops the mock server.
func (s *MockServer) Stop() {
s.grpcServer.Stop()
atomic.StoreInt64(&s.running, 0)
}

// Start starts the mock server.
func (s *MockServer) Start(addr string) int {
if addr == "" {
addr = fmt.Sprintf("%s:%d", "127.0.0.1", 0)
Expand Down Expand Up @@ -159,7 +169,7 @@ func (s *MockServer) Start(addr string) int {
return port
}

// StartMockTikvService try to start a gRPC server and retrun the server instance and binded port.
// StartMockTikvService try to start a gRPC server and return the server instance and binded port.
func StartMockTikvService() (*MockServer, int) {
server := &MockServer{}
port := server.Start("")
Expand Down
6 changes: 3 additions & 3 deletions internal/locate/region_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import (
"github.com/tikv/client-go/v2/config/retry"
"github.com/tikv/client-go/v2/internal/apicodec"
"github.com/tikv/client-go/v2/internal/client"
"github.com/tikv/client-go/v2/internal/client/mock_server"
"github.com/tikv/client-go/v2/internal/client/mockserver"
"github.com/tikv/client-go/v2/internal/mockstore/mocktikv"
"github.com/tikv/client-go/v2/tikvrpc"
"google.golang.org/grpc"
Expand Down Expand Up @@ -707,7 +707,7 @@ func (s *testRegionRequestToSingleStoreSuite) TestKVReadTimeoutWithDisableBatchC
conf.TiKVClient.MaxBatchSize = 0
})()

server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
s.True(port > 0)
server.SetMetaChecker(func(ctx context.Context) error {
return context.DeadlineExceeded
Expand Down Expand Up @@ -742,7 +742,7 @@ func (s *testRegionRequestToSingleStoreSuite) TestBatchClientSendLoopPanic() {
conf.TiKVClient.MaxBatchSize = 128
})()

server, port := mock_server.StartMockTikvService()
server, port := mockserver.StartMockTikvService()
s.True(port > 0)
rpcClient := client.NewRPCClient()
fnClient := &fnClient{fn: func(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (response *tikvrpc.Response, err error) {
Expand Down
8 changes: 7 additions & 1 deletion tikv/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
zap "go.uber.org/zap"
)

// We don't want gc to sweep out the cached info belong to other processes, like coprocessor.
// GCScanLockLimit We don't want gc to sweep out the cached info belong to other processes, like coprocessor.
const GCScanLockLimit = txnlock.ResolvedCacheSize / 2

// GC does garbage collection (GC) of the TiKV cluster.
Expand Down Expand Up @@ -98,30 +98,36 @@ func (s *KVStore) resolveLocks(ctx context.Context, safePoint uint64, concurrenc
return nil
}

// BaseRegionLockResolver is a base implementation of RegionLockResolver.
type BaseRegionLockResolver struct {
identifier string
store Storage
}

// NewRegionLockResolver creates a new BaseRegionLockResolver.
func NewRegionLockResolver(identifier string, store Storage) *BaseRegionLockResolver {
return &BaseRegionLockResolver{
identifier: identifier,
store: store,
}
}

// Identifier represents the name of this resolver.
func (l *BaseRegionLockResolver) Identifier() string {
return l.identifier
}

// ResolveLocksInOneRegion tries to resolve expired locks for one region.
func (l *BaseRegionLockResolver) ResolveLocksInOneRegion(bo *Backoffer, locks []*txnlock.Lock, loc *locate.KeyLocation) (*locate.KeyLocation, error) {
return batchResolveLocksInOneRegion(bo, l.GetStore(), locks, loc)
}

// ScanLocksInOneRegion return locks and location with given start key in a region.
func (l *BaseRegionLockResolver) ScanLocksInOneRegion(bo *Backoffer, key []byte, maxVersion uint64, scanLimit uint32) ([]*txnlock.Lock, *locate.KeyLocation, error) {
return scanLocksInOneRegionWithStartKey(bo, l.GetStore(), key, maxVersion, scanLimit)
}

// GetStore is used to get store to GetRegionCache and SendReq for this lock resolver.
func (l *BaseRegionLockResolver) GetStore() Storage {
return l.store
}
Expand Down

0 comments on commit 812a791

Please sign in to comment.