Skip to content

Commit

Permalink
coprocessor: split ranges if the slice is too big (#7454)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored and coocood committed Aug 23, 2018
1 parent f3325e2 commit ef6590e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions store/tikv/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sync/atomic"
"time"

"github.com/cznic/mathutil"
"github.com/juju/errors"
"github.com/opentracing/opentracing-go"
"github.com/pingcap/kvproto/pkg/coprocessor"
Expand Down Expand Up @@ -235,6 +236,9 @@ func (r *copRanges) split(key []byte) (*copRanges, *copRanges) {
return r.slice(0, n), r.slice(n, r.len())
}

// rangesPerTask limits the length of the ranges slice sent in one copTask.
const rangesPerTask = 25000

func buildCopTasks(bo *Backoffer, cache *RegionCache, ranges *copRanges, desc bool, streaming bool) ([]*copTask, error) {
start := time.Now()
rangesLen := ranges.len()
Expand All @@ -245,12 +249,19 @@ func buildCopTasks(bo *Backoffer, cache *RegionCache, ranges *copRanges, desc bo

var tasks []*copTask
appendTask := func(region RegionVerID, ranges *copRanges) {
tasks = append(tasks, &copTask{
region: region,
ranges: ranges,
respChan: make(chan *copResponse, 1),
cmdType: cmdType,
})
// TiKV will return gRPC error if the message is too large. So we need to limit the length of the ranges slice
// to make sure the message can be sent successfully.
rLen := ranges.len()
for i := 0; i < rLen; {
nextI := mathutil.Min(i+rangesPerTask, rLen)
tasks = append(tasks, &copTask{
region: region,
ranges: ranges.slice(i, nextI),
respChan: make(chan *copResponse, 1),
cmdType: cmdType,
})
i = nextI
}
}

err := splitRanges(bo, cache, ranges, appendTask)
Expand Down

0 comments on commit ef6590e

Please sign in to comment.