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/copr: set upper limit for extra concurrency #41135

Merged
merged 12 commits into from
Feb 7, 2023
10 changes: 8 additions & 2 deletions store/copr/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"math"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -72,6 +73,8 @@ const (
smallTaskSigma = 0.5
)

var smallTaskConcurrencyLimit = 20 * runtime.NumCPU()
you06 marked this conversation as resolved.
Show resolved Hide resolved

// CopClient is coprocessor client.
type CopClient struct {
kv.RequestTypeSupportedChecker
Expand Down Expand Up @@ -592,8 +595,11 @@ func smallTaskConcurrency(tasks []*copTask) (int, int) {
}
// Calculate the extra concurrency for small tasks
// extra concurrency = tasks / (1 + sigma * sqrt(log(tasks ^ 2)))
extraConc := float64(res) / (1 + smallTaskSigma*math.Sqrt(2*math.Log(float64(res))))
return res, int(extraConc)
extraConc := int(float64(res) / (1 + smallTaskSigma*math.Sqrt(2*math.Log(float64(res)))))
if extraConc > smallTaskConcurrencyLimit {
extraConc = smallTaskConcurrencyLimit
}
return res, extraConc
}

type copIterator struct {
Expand Down
18 changes: 18 additions & 0 deletions store/copr/coprocessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,21 @@ func TestBuildCopTasksWithRowCountHint(t *testing.T) {
// task[3] ["t"-"z"]
require.Equal(t, tasks[3].RowCountHint, 10)
}

func TestSmallTaskConcurrency(t *testing.T) {
originSmallTaskLimit := smallTaskConcurrencyLimit
defer func() {
smallTaskConcurrencyLimit = originSmallTaskLimit
}()
smallTaskConcurrencyLimit = 10
smallTaskCount := 1000
tasks := make([]*copTask, 0, smallTaskCount)
for i := 0; i < smallTaskCount; i++ {
tasks = append(tasks, &copTask{
RowCountHint: 1,
})
}
count, conc := smallTaskConcurrency(tasks)
require.Equal(t, smallTaskConcurrencyLimit, conc)
require.Equal(t, smallTaskCount, count)
}