Skip to content

Commit

Permalink
executor: disable paging for small limit (#41120)
Browse files Browse the repository at this point in the history
close #40741
  • Loading branch information
solotzg authored Feb 7, 2023
1 parent 79a5d3b commit 98aff8c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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 @@ -676,6 +676,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 @@ -573,6 +573,8 @@ type Request struct {
StoreBatchSize int
// ResourceGroupName is the name of the bind resource group.
ResourceGroupName string
// 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 @@ -402,7 +402,13 @@ func buildCopTasks(bo *Backoffer, ranges *KeyRanges, opt *buildCopTaskOpt) ([]*c
}
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)
}
}
}
}
Expand Down
33 changes: 31 additions & 2 deletions store/copr/coprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,6 @@ func TestBuildPagingTasks(t *testing.T) {
req := &kv.Request{}
req.Paging.Enable = true
req.Paging.MinPagingSize = paging.MinPagingSize
flashReq := &kv.Request{}
flashReq.StoreType = kv.TiFlash
tasks, err := buildTestCopTasks(bo, cache, buildCopRanges("a", "c"), req, nil)
require.NoError(t, err)
require.Len(t, tasks, 1)
Expand All @@ -525,6 +523,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.NewCodecPDClient(tikv.ModeTxn, 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 := buildTestCopTasks(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 98aff8c

Please sign in to comment.