Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store/tikv: prepare for moving coprocessor out #22732

Merged
merged 8 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions store/tikv/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/pingcap/tidb/store/tikv/metrics"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
"github.com/pingcap/tidb/store/tikv/util"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -1712,7 +1713,7 @@ func (b *batched) forgetPrimary() {
// batchExecutor is txn controller providing rate control like utils
type batchExecutor struct {
rateLim int // concurrent worker numbers
rateLimiter *rateLimit // rate limiter for concurrency control, maybe more strategies
rateLimiter *util.RateLimit // rate limiter for concurrency control, maybe more strategies
committer *twoPhaseCommitter // here maybe more different type committer in the future
action twoPhaseCommitAction // the work action type
backoffer *Backoffer // Backoffer
Expand All @@ -1729,19 +1730,19 @@ func newBatchExecutor(rateLimit int, committer *twoPhaseCommitter,
// initUtils do initialize batchExecutor related policies like rateLimit util
func (batchExe *batchExecutor) initUtils() error {
// init rateLimiter by injected rate limit number
batchExe.rateLimiter = newRateLimit(batchExe.rateLim)
batchExe.rateLimiter = util.NewRateLimit(batchExe.rateLim)
return nil
}

// startWork concurrently do the work for each batch considering rate limit
func (batchExe *batchExecutor) startWorker(exitCh chan struct{}, ch chan error, batches []batchMutations) {
for idx, batch1 := range batches {
waitStart := time.Now()
if exit := batchExe.rateLimiter.getToken(exitCh); !exit {
if exit := batchExe.rateLimiter.GetToken(exitCh); !exit {
batchExe.tokenWaitDuration += time.Since(waitStart)
batch := batch1
go func() {
defer batchExe.rateLimiter.putToken()
defer batchExe.rateLimiter.PutToken()
var singleBatchBackoffer *Backoffer
if _, ok := batchExe.action.(actionCommit); ok {
// Because the secondary batches of the commit actions are implemented to be
Expand Down Expand Up @@ -1826,7 +1827,7 @@ func (batchExe *batchExecutor) process(batches []batchMutations) error {

func getTxnPriority(txn *tikvTxn) pb.CommandPri {
if pri := txn.us.GetOption(kv.Priority); pri != nil {
return kvPriorityToCommandPri(pri.(int))
return PriorityToPB(pri.(int))
}
return pb.CommandPri_Normal
}
Expand All @@ -1838,7 +1839,8 @@ func getTxnSyncLog(txn *tikvTxn) bool {
return false
}

func kvPriorityToCommandPri(pri int) pb.CommandPri {
// PriorityToPB converts priority type to wire type.
func PriorityToPB(pri int) pb.CommandPri {
switch pri {
case kv.PriorityLow:
return pb.CommandPri_Low
Expand Down
28 changes: 12 additions & 16 deletions store/tikv/batch_coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv/logutil"
"github.com/pingcap/tidb/store/tikv/tikvrpc"
"github.com/pingcap/tidb/store/tikv/util"
"github.com/pingcap/tidb/util/memory"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -176,18 +177,13 @@ func (c *CopClient) sendBatch(ctx context.Context, req *kv.Request, vars *kv.Var
return copErrorResponse{err}
}
it := &batchCopIterator{
store: c.store,
req: req,
finishCh: make(chan struct{}),
vars: vars,
memTracker: req.MemTracker,
clientHelper: clientHelper{
LockResolver: c.store.GetLockResolver(),
RegionCache: c.store.GetRegionCache(),
Client: c.store.GetTiKVClient(),
minCommitTSPushed: &minCommitTSPushed{data: make(map[uint64]struct{}, 5)},
},
rpcCancel: NewRPCanceller(),
store: c.store,
req: req,
finishCh: make(chan struct{}),
vars: vars,
memTracker: req.MemTracker,
ClientHelper: NewClientHelper(c.store, util.NewTSSet(5)),
rpcCancel: NewRPCanceller(),
}
ctx = context.WithValue(ctx, RPCCancellerCtxKey{}, it.rpcCancel)
it.tasks = tasks
Expand All @@ -197,7 +193,7 @@ func (c *CopClient) sendBatch(ctx context.Context, req *kv.Request, vars *kv.Var
}

type batchCopIterator struct {
clientHelper
*ClientHelper

store *KVStore
req *kv.Request
Expand Down Expand Up @@ -317,7 +313,7 @@ func (b *batchCopIterator) retryBatchCopTask(ctx context.Context, bo *Backoffer,
ranges = append(ranges, *ran)
})
}
return buildBatchCopTasks(bo, b.RegionCache, NewKeyRanges(ranges), b.req.StoreType)
return buildBatchCopTasks(bo, b.regionCache, NewKeyRanges(ranges), b.req.StoreType)
}

func (b *batchCopIterator) handleTaskOnce(ctx context.Context, bo *Backoffer, task *batchCopTask) ([]*batchCopTask, error) {
Expand All @@ -343,8 +339,8 @@ func (b *batchCopIterator) handleTaskOnce(ctx context.Context, bo *Backoffer, ta
}

req := tikvrpc.NewRequest(task.cmdType, &copReq, kvrpcpb.Context{
IsolationLevel: pbIsolationLevel(b.req.IsolationLevel),
Priority: kvPriorityToCommandPri(b.req.Priority),
IsolationLevel: IsolationLevelToPB(b.req.IsolationLevel),
Priority: PriorityToPB(b.req.Priority),
NotFillCache: b.req.NotFillCache,
RecordTimeStat: true,
RecordScanStat: true,
Expand Down
Loading