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

loaddata: support concurrent writing to TiKV #42667

Merged
merged 6 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 15 additions & 8 deletions executor/importer/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ type LoadDataController struct {
checksum config.PostOpLevel
addIndex bool
analyze config.PostOpLevel
threadCnt int64
ThreadCnt int64
BatchSize int64
maxWriteSpeed config.ByteSize // per second
splitFile bool
Expand Down Expand Up @@ -300,8 +300,13 @@ func (e *LoadDataController) initFieldParams(plan *plannercore.LoadData) error {
return nil
}

var ignoreInTest = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that it is an atomic type to avoid data race report.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will turn on race check in bazel in next commit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


func (e *LoadDataController) initDefaultOptions() {
threadCnt := runtime.NumCPU()
if intest.InTest && !ignoreInTest {
threadCnt = 1
}
if e.Format == LoadDataFormatParquet {
threadCnt = int(math.Max(1, float64(threadCnt)*0.75))
}
Expand All @@ -311,7 +316,7 @@ func (e *LoadDataController) initDefaultOptions() {
e.checksum = config.OpLevelRequired
e.addIndex = true
e.analyze = config.OpLevelOptional
e.threadCnt = int64(threadCnt)
e.ThreadCnt = int64(threadCnt)
e.BatchSize = 1000
e.maxWriteSpeed = unlimitedWriteSpeed
e.splitFile = false
Expand Down Expand Up @@ -402,8 +407,8 @@ func (e *LoadDataController) initOptions(seCtx sessionctx.Context, options []*pl
}
if opt, ok := specifiedOptions[threadOption]; ok {
// boolean true will be taken as 1
e.threadCnt, isNull, err = opt.Value.EvalInt(seCtx, chunk.Row{})
if err != nil || isNull || e.threadCnt <= 0 {
e.ThreadCnt, isNull, err = opt.Value.EvalInt(seCtx, chunk.Row{})
if err != nil || isNull || e.ThreadCnt <= 0 {
return exeerrors.ErrInvalidOptionVal.FastGenByArgs(opt.Name)
}
}
Expand Down Expand Up @@ -454,8 +459,8 @@ func (e *LoadDataController) adjustOptions() {
}
// max value is cpu-count
numCPU := int64(runtime.NumCPU())
if e.threadCnt > numCPU {
e.threadCnt = numCPU
if e.ThreadCnt > numCPU {
e.ThreadCnt = numCPU
}
if e.maxWriteSpeed < minWriteSpeed {
e.maxWriteSpeed = minWriteSpeed
Expand Down Expand Up @@ -692,8 +697,10 @@ func (e *LoadDataController) GetLoadDataReaderInfos() []LoadDataReaderInfo {
}

// GetParser returns a parser for the data file.
func (e *LoadDataController) GetParser(ctx context.Context, dataFileInfo LoadDataReaderInfo) (
parser mydump.Parser, err error) {
func (e *LoadDataController) GetParser(
ctx context.Context,
dataFileInfo LoadDataReaderInfo,
) (parser mydump.Parser, err error) {
reader, err2 := dataFileInfo.Opener(ctx)
if err2 != nil {
return nil, err2
Expand Down
17 changes: 11 additions & 6 deletions executor/importer/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ import (
)

func TestInitDefaultOptions(t *testing.T) {
ignoreInTest = true
t.Cleanup(func() {
ignoreInTest = false
})

e := LoadDataController{}
e.initDefaultOptions()
require.Equal(t, LogicalImportMode, e.ImportMode)
require.Equal(t, config.ByteSize(50<<30), e.diskQuota)
require.Equal(t, config.OpLevelRequired, e.checksum)
require.Equal(t, true, e.addIndex)
require.Equal(t, config.OpLevelOptional, e.analyze)
require.Equal(t, int64(runtime.NumCPU()), e.threadCnt)
require.Equal(t, int64(runtime.NumCPU()), e.ThreadCnt)
require.Equal(t, int64(1000), e.BatchSize)
require.Equal(t, unlimitedWriteSpeed, e.maxWriteSpeed)
require.Equal(t, false, e.splitFile)
Expand All @@ -49,8 +54,8 @@ func TestInitDefaultOptions(t *testing.T) {

e = LoadDataController{Format: LoadDataFormatParquet}
e.initDefaultOptions()
require.Greater(t, e.threadCnt, int64(0))
require.Equal(t, int64(math.Max(1, float64(runtime.NumCPU())*0.75)), e.threadCnt)
require.Greater(t, e.ThreadCnt, int64(0))
require.Equal(t, int64(math.Max(1, float64(runtime.NumCPU())*0.75)), e.ThreadCnt)
}

func TestInitOptions(t *testing.T) {
Expand Down Expand Up @@ -165,7 +170,7 @@ func TestInitOptions(t *testing.T) {
require.Equal(t, config.OpLevelOptional, e.checksum, sql)
require.False(t, e.addIndex, sql)
require.Equal(t, config.OpLevelRequired, e.analyze, sql)
require.Equal(t, int64(runtime.NumCPU()), e.threadCnt, sql)
require.Equal(t, int64(runtime.NumCPU()), e.ThreadCnt, sql)
require.Equal(t, int64(2000), e.BatchSize, sql)
require.Equal(t, config.ByteSize(200<<20), e.maxWriteSpeed, sql)
require.True(t, e.splitFile, sql)
Expand All @@ -176,12 +181,12 @@ func TestInitOptions(t *testing.T) {
func TestAdjustOptions(t *testing.T) {
e := LoadDataController{
diskQuota: 1,
threadCnt: 100000000,
ThreadCnt: 100000000,
maxWriteSpeed: 10,
}
e.adjustOptions()
require.Equal(t, minDiskQuota, e.diskQuota)
require.Equal(t, int64(runtime.NumCPU()), e.threadCnt)
require.Equal(t, int64(runtime.NumCPU()), e.ThreadCnt)
require.Equal(t, minWriteSpeed, e.maxWriteSpeed)
}

Expand Down
Loading