Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

backend/local: skip split regions if engine total size is smaller than region size #524

Merged
merged 5 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions lightning/backend/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,10 +1190,14 @@ func (local *local) ImportEngine(ctx context.Context, engineUUID uuid.UUID) erro
log.L().Info("start import engine", zap.Stringer("uuid", engineUUID),
zap.Int("ranges", len(ranges)))

// if all the kv can fit in one region, skip split regions. TiDB will split one region for
// the table when table is created.
needSplit := len(ranges) > 1 || lf.TotalSize > local.regionSplitSize || lf.Length > regionMaxKeyCount

// split region by given ranges
for i := 0; i < maxRetryTimes; i++ {
err = local.SplitAndScatterRegionByRanges(ctx, ranges)
if err == nil || errors.Cause(err) == context.Canceled {
err = local.SplitAndScatterRegionByRanges(ctx, ranges, needSplit)
if err == nil || common.IsContextCanceledError(err) {
break
}

Expand All @@ -1220,7 +1224,8 @@ func (local *local) ImportEngine(ctx context.Context, engineUUID uuid.UUID) erro
ranges = unfinishedRanges
}

log.L().Info("import engine success", zap.Stringer("uuid", engineUUID))
log.L().Info("import engine success", zap.Stringer("uuid", engineUUID),
zap.Int64("size", lf.TotalSize), zap.Int64("kvs", lf.Length))
return nil
}

Expand Down
9 changes: 7 additions & 2 deletions lightning/backend/localhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ var (
// TODO remove this file and use br internal functions
// This File include region split & scatter operation just like br.
// we can simply call br function, but we need to change some function signature of br

func (local *local) SplitAndScatterRegionByRanges(ctx context.Context, ranges []Range) error {
// When the ranges total size is small, we can skip the split to avoid generate empty regions.
func (local *local) SplitAndScatterRegionByRanges(ctx context.Context, ranges []Range, needSplit bool) error {
if len(ranges) == 0 {
return nil
}
Expand Down Expand Up @@ -97,6 +97,11 @@ func (local *local) SplitAndScatterRegionByRanges(ctx context.Context, ranges []
log.L().Info("paginate scan region finished", log.ZapRedactBinary("minKey", minKey), log.ZapRedactBinary("maxKey", maxKey),
zap.Int("regions", len(regions)))

if !needSplit {
scatterRegions = append(scatterRegions, regions...)
break
}

regionMap := make(map[uint64]*split.RegionInfo)
for _, region := range regions {
regionMap[region.Region.GetId()] = region
Expand Down
4 changes: 2 additions & 2 deletions lightning/backend/localhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func (s *localSuite) doTestBatchSplitRegionByRanges(c *C, hook clientHook, errPa
start = end
}

err = local.SplitAndScatterRegionByRanges(ctx, ranges)
err = local.SplitAndScatterRegionByRanges(ctx, ranges, true)
if len(errPat) == 0 {
c.Assert(err, IsNil)
} else {
Expand Down Expand Up @@ -504,7 +504,7 @@ func (s *localSuite) doTestBatchSplitByRangesWithClusteredIndex(c *C, hook clien
start = e
}

err := local.SplitAndScatterRegionByRanges(ctx, ranges)
err := local.SplitAndScatterRegionByRanges(ctx, ranges, true)
c.Assert(err, IsNil)

startKey := codec.EncodeBytes([]byte{}, rangeKeys[0])
Expand Down