Skip to content

Commit

Permalink
executor: disable paging for small limit (#41120) (#41121)
Browse files Browse the repository at this point in the history
close #40741
  • Loading branch information
ti-chi-bot authored Feb 7, 2023
1 parent 45f2d06 commit 3eeca82
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (builder *RequestBuilder) SetDAGRequest(dag *tipb.DAGRequest) *RequestBuild
if limit != nil && limit.Limit < estimatedRegionRowCount {
builder.Request.Concurrency = 1
}
builder.Request.LimitSize = limit.GetLimit()
}
return builder
}
Expand Down
1 change: 1 addition & 0 deletions distsql/request_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ func TestScanLimitConcurrency(t *testing.T) {
Build()
require.NoError(t, err)
require.Equal(t, tt.concurrency, actual.Concurrency)
require.Equal(t, actual.LimitSize, tt.limit)
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ type Request struct {
FixedRowCountHint []int
// StoreBatchSize indicates the batch size of coprocessor in the same store.
StoreBatchSize int
// LimitSize indicates whether the request is scan and limit
LimitSize uint64
}

// CoprRequestAdjuster is used to check and adjust a copr request according to specific rules.
Expand Down
8 changes: 7 additions & 1 deletion store/copr/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,13 @@ func buildCopTasks(bo *Backoffer, cache *RegionCache, ranges *KeyRanges, req *kv
}
i = nextI
if req.Paging.Enable {
pagingSize = paging.GrowPagingSize(pagingSize, req.Paging.MaxPagingSize)
if req.LimitSize != 0 && req.LimitSize < pagingSize {
// disable paging for small limit.
task.paging = false
task.pagingSize = 0
} else {
pagingSize = paging.GrowPagingSize(pagingSize, req.Paging.MaxPagingSize)
}
}
taskID++
}
Expand Down
31 changes: 31 additions & 0 deletions store/copr/coprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,37 @@ func TestBuildPagingTasks(t *testing.T) {
require.Equal(t, tasks[0].pagingSize, paging.MinPagingSize)
}

func TestBuildPagingTasksDisablePagingForSmallLimit(t *testing.T) {
mockClient, cluster, pdClient, err := testutils.NewMockTiKV("", nil)
require.NoError(t, err)
defer func() {
pdClient.Close()
err = mockClient.Close()
require.NoError(t, err)
}()
_, regionIDs, _ := testutils.BootstrapWithMultiRegions(cluster, []byte("g"), []byte("n"), []byte("t"))

pdCli := &tikv.CodecPDClient{Client: pdClient}
defer pdCli.Close()

cache := NewRegionCache(tikv.NewRegionCache(pdCli))
defer cache.Close()

bo := backoff.NewBackofferWithVars(context.Background(), 3000, nil)

req := &kv.Request{}
req.Paging.Enable = true
req.Paging.MinPagingSize = paging.MinPagingSize
req.LimitSize = 1
tasks, err := buildCopTasks(bo, cache, buildCopRanges("a", "c"), req, nil)
require.NoError(t, err)
require.Len(t, tasks, 1)
require.Len(t, tasks, 1)
taskEqual(t, tasks[0], regionIDs[0], 0, "a", "c")
require.False(t, tasks[0].paging)
require.Equal(t, tasks[0].pagingSize, uint64(0))
}

func toCopRange(r kv.KeyRange) *coprocessor.KeyRange {
coprRange := coprocessor.KeyRange{}
coprRange.Start = r.StartKey
Expand Down

0 comments on commit 3eeca82

Please sign in to comment.