Skip to content

Commit

Permalink
util: use generics to implement max/min (pingcap#34197)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored May 6, 2022
1 parent dd0c3ed commit f88dab9
Show file tree
Hide file tree
Showing 98 changed files with 637 additions and 490 deletions.
2 changes: 1 addition & 1 deletion bindinfo/bind_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"errors"
"sync"

"github.com/cznic/mathutil"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/kvcache"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/memory"
)

Expand Down
3 changes: 2 additions & 1 deletion br/pkg/lightning/backend/kv/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/topsql/stmtstats"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -97,7 +98,7 @@ func (mb *kvMemBuf) Recycle(buf *bytesBuf) {

func (mb *kvMemBuf) AllocateBuf(size int) {
mb.Lock()
size = utils.MaxInt(units.MiB, int(utils.NextPowerOfTwo(int64(size)))*2)
size = mathutil.Max(units.MiB, int(utils.NextPowerOfTwo(int64(size)))*2)
if len(mb.availableBufs) > 0 && mb.availableBufs[0].cap >= size {
mb.buf = mb.availableBufs[0]
mb.availableBufs = mb.availableBufs[1:]
Expand Down
5 changes: 3 additions & 2 deletions br/pkg/lightning/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mathutil"
tikverror "github.com/tikv/client-go/v2/error"
"github.com/tikv/client-go/v2/oracle"
tikvclient "github.com/tikv/client-go/v2/tikv"
Expand Down Expand Up @@ -324,7 +325,7 @@ func NewLocalBackend(
dupeConcurrency: rangeConcurrency * 2,
batchWriteKVPairs: cfg.TikvImporter.SendKVPairs,
checkpointEnabled: cfg.Checkpoint.Enable,
maxOpenFiles: utils.MaxInt(maxOpenFiles, openFilesLowerThreshold),
maxOpenFiles: mathutil.Max(maxOpenFiles, openFilesLowerThreshold),

engineMemCacheSize: int(cfg.TikvImporter.EngineMemCacheSize),
localWriterMemCacheSize: int64(cfg.TikvImporter.LocalWriterMemCacheSize),
Expand Down Expand Up @@ -1171,7 +1172,7 @@ loopWrite:

for i := 0; i < len(metas); i += batch {
start := i * batch
end := utils.MinInt((i+1)*batch, len(metas))
end := mathutil.Min((i+1)*batch, len(metas))
ingestMetas := metas[start:end]
errCnt := 0
for errCnt < maxRetryTimes {
Expand Down
4 changes: 2 additions & 2 deletions br/pkg/lightning/backend/local/localhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/br/pkg/logutil"
split "github.com/pingcap/tidb/br/pkg/restore"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -208,7 +208,7 @@ func (local *local) SplitAndScatterRegionByRanges(

var syncLock sync.Mutex
// TODO, make this size configurable
size := utils.MinInt(len(splitKeyMap), runtime.GOMAXPROCS(0))
size := mathutil.Min(len(splitKeyMap), runtime.GOMAXPROCS(0))
ch := make(chan *splitInfo, size)
eg, splitCtx := errgroup.WithContext(ctx)

Expand Down
4 changes: 2 additions & 2 deletions br/pkg/lightning/checkpoints/checkpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import (
verify "github.com/pingcap/tidb/br/pkg/lightning/verification"
"github.com/pingcap/tidb/br/pkg/storage"
"github.com/pingcap/tidb/br/pkg/version/build"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
"modernc.org/mathutil"
)

type CheckpointStatus uint8
Expand Down Expand Up @@ -462,7 +462,7 @@ type RebaseCheckpointMerger struct {

func (merger *RebaseCheckpointMerger) MergeInto(cpd *TableCheckpointDiff) {
cpd.hasRebase = true
cpd.allocBase = mathutil.MaxInt64(cpd.allocBase, merger.AllocBase)
cpd.allocBase = mathutil.Max(cpd.allocBase, merger.AllocBase)
}

type DestroyedTableCheckpoint struct {
Expand Down
4 changes: 2 additions & 2 deletions br/pkg/lightning/mydump/csv_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/lightning/config"
"github.com/pingcap/tidb/br/pkg/lightning/worker"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mathutil"
)

var (
Expand Down Expand Up @@ -213,7 +213,7 @@ func (parser *CSVParser) peekBytes(cnt int) ([]byte, error) {
if len(parser.buf) == 0 {
return nil, io.EOF
}
cnt = utils.MinInt(cnt, len(parser.buf))
cnt = mathutil.Min(cnt, len(parser.buf))
return parser.buf[:cnt], nil
}

Expand Down
4 changes: 2 additions & 2 deletions br/pkg/lightning/mydump/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/br/pkg/lightning/worker"
"github.com/pingcap/tidb/br/pkg/storage"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -154,7 +154,7 @@ func MakeTableRegions(
execCtx, cancel := context.WithCancel(ctx)
defer cancel()

concurrency := utils.MaxInt(cfg.App.RegionConcurrency, 2)
concurrency := mathutil.Max(cfg.App.RegionConcurrency, 2)
fileChan := make(chan FileInfo, concurrency)
resultChan := make(chan fileRegionRes, concurrency)
var wg sync.WaitGroup
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/lightning/mydump/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ var defaultFileRouteRules = []*config.FileRouteRule{

// // RouteRule is a rule to route file path to target schema/table
type FileRouter interface {
// Route apply rule to path. Return nil if path doesn't math route rule;
// Route apply rule to path. Return nil if path doesn't match route rule;
// return error if path match route rule but the captured value for field is invalid
Route(path string) (*RouteResult, error)
}
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/lightning/mydump/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestMultiRouteRule(t *testing.T) {
}

// multi rule don't intersect with each other
// add another rule that math same pattern with the third rule, the result should be no different
// add another rule that match same pattern with the third rule, the result should be no different
p := &config.FileRouteRule{Pattern: `^(?P<schema>[^/.]+)\.(?P<table>[^./]+)(?:\.(?P<key>[0-9]+))?\.(?P<type>csv|sql)(?:\.(?P<cp>[A-Za-z0-9]+))?$`, Schema: "test_schema", Table: "test_table", Type: "$type", Key: "$key", Compression: "$cp"}
rules = append(rules, p)
r, err = NewFileRouter(rules)
Expand Down
4 changes: 2 additions & 2 deletions br/pkg/lightning/restore/check_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ import (
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"modernc.org/mathutil"
)

const (
Expand Down Expand Up @@ -1092,7 +1092,7 @@ func (rc *Controller) checkTableEmpty(ctx context.Context) error {

var lock sync.Mutex
tableNames := make([]string, 0)
concurrency := utils.MinInt(tableCount, rc.cfg.App.RegionConcurrency)
concurrency := mathutil.Min(tableCount, rc.cfg.App.RegionConcurrency)
ch := make(chan string, concurrency)
eg, gCtx := errgroup.WithContext(ctx)

Expand Down
4 changes: 2 additions & 2 deletions br/pkg/lightning/restore/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/br/pkg/lightning/metric"
"github.com/pingcap/tidb/br/pkg/pdutil"
"github.com/pingcap/tidb/br/pkg/utils"
tidbcfg "github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/driver"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tipb/go-tipb"
"github.com/tikv/client-go/v2/oracle"
pd "github.com/tikv/pd/client"
Expand Down Expand Up @@ -314,7 +314,7 @@ func (e *tikvChecksumManager) checksumDB(ctx context.Context, tableInfo *checkpo
break
}
if distSQLScanConcurrency > minDistSQLScanConcurrency {
distSQLScanConcurrency = utils.MaxInt(distSQLScanConcurrency/2, minDistSQLScanConcurrency)
distSQLScanConcurrency = mathutil.Max(distSQLScanConcurrency/2, minDistSQLScanConcurrency)
}
}

Expand Down
8 changes: 4 additions & 4 deletions br/pkg/lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ import (
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/mathutil"
pd "github.com/tikv/pd/client"
"go.uber.org/atomic"
"go.uber.org/multierr"
"go.uber.org/zap"
"modernc.org/mathutil"
)

const (
Expand Down Expand Up @@ -701,7 +701,7 @@ func (rc *Controller) restoreSchema(ctx context.Context) error {
// we can handle the duplicated created with createIfNotExist statement
// and we will check the schema in TiDB is valid with the datafile in DataCheck later.
logTask := log.L().Begin(zap.InfoLevel, "restore all schema")
concurrency := utils.MinInt(rc.cfg.App.RegionConcurrency, 8)
concurrency := mathutil.Min(rc.cfg.App.RegionConcurrency, 8)
childCtx, cancel := context.WithCancel(ctx)
worker := restoreSchemaWorker{
ctx: childCtx,
Expand Down Expand Up @@ -1629,12 +1629,12 @@ func (tr *TableRestore) restoreTable(

// rebase the allocator so it exceeds the number of rows.
if tr.tableInfo.Core.PKIsHandle && tr.tableInfo.Core.ContainsAutoRandomBits() {
cp.AllocBase = mathutil.MaxInt64(cp.AllocBase, tr.tableInfo.Core.AutoRandID)
cp.AllocBase = mathutil.Max(cp.AllocBase, tr.tableInfo.Core.AutoRandID)
if err := tr.alloc.Get(autoid.AutoRandomType).Rebase(context.Background(), cp.AllocBase, false); err != nil {
return false, err
}
} else {
cp.AllocBase = mathutil.MaxInt64(cp.AllocBase, tr.tableInfo.Core.AutoIncID)
cp.AllocBase = mathutil.Max(cp.AllocBase, tr.tableInfo.Core.AutoIncID)
if err := tr.alloc.Get(autoid.RowIDAllocType).Rebase(context.Background(), cp.AllocBase, false); err != nil {
return false, err
}
Expand Down
3 changes: 2 additions & 1 deletion br/pkg/lightning/restore/table_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/multierr"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -924,7 +925,7 @@ func (tr *TableRestore) importKV(
regionSplitSize = int64(config.SplitRegionSize)
if err := rc.taskMgr.CheckTasksExclusively(ctx, func(tasks []taskMeta) ([]taskMeta, error) {
if len(tasks) > 0 {
regionSplitSize = int64(config.SplitRegionSize) * int64(utils.MinInt(len(tasks), config.MaxSplitRegionSizeRatio))
regionSplitSize = int64(config.SplitRegionSize) * int64(mathutil.Min(len(tasks), config.MaxSplitRegionSizeRatio))
}
return nil, nil
}); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion br/pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/pingcap/tidb/store/pdtypes"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mathutil"
"github.com/tikv/client-go/v2/oracle"
pd "github.com/tikv/pd/client"
"go.uber.org/zap"
Expand Down Expand Up @@ -712,7 +713,7 @@ func (rc *Client) createTablesInWorkerPool(ctx context.Context, dom *domain.Doma
numOfTables := len(tables)

for lastSent := 0; lastSent < numOfTables; lastSent += int(rc.batchDdlSize) {
end := utils.MinInt(lastSent+int(rc.batchDdlSize), len(tables))
end := mathutil.Min(lastSent+int(rc.batchDdlSize), len(tables))
log.Info("create tables", zap.Int("table start", lastSent), zap.Int("table end", end))

tableSlice := tables[lastSent:end]
Expand Down
3 changes: 2 additions & 1 deletion br/pkg/task/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mathutil"
"github.com/spf13/pflag"
"github.com/tikv/client-go/v2/oracle"
"go.uber.org/zap"
Expand Down Expand Up @@ -480,7 +481,7 @@ func RunBackup(c context.Context, g glue.Glue, cmdName string, cfg *BackupConfig
}
}
updateCh = g.StartProgress(ctx, "Checksum", checksumProgress, !cfg.LogProgress)
schemasConcurrency := uint(utils.MinInt(backup.DefaultSchemaConcurrency, schemas.Len()))
schemasConcurrency := uint(mathutil.Min(backup.DefaultSchemaConcurrency, schemas.Len()))

err = schemas.BackupSchemas(
ctx, metawriter, mgr.GetStorage(), statsHandle, backupTS, schemasConcurrency, cfg.ChecksumConcurrency, skipChecksum, updateCh)
Expand Down
36 changes: 2 additions & 34 deletions br/pkg/utils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,17 @@ package utils

import (
"github.com/pingcap/log"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
)

// MinInt choice smallest integer from its arguments.
func MinInt(x int, xs ...int) int {
min := x
for _, n := range xs {
if n < min {
min = n
}
}
return min
}

// MaxInt choice biggest integer from its arguments.
func MaxInt(x int, xs ...int) int {
max := x
for _, n := range xs {
if n > max {
max = n
}
}
return max
}

// ClampInt restrict a value to a certain interval.
func ClampInt(n, min, max int) int {
if min > max {
log.Error("clamping integer with min > max", zap.Int("min", min), zap.Int("max", max))
}

return MinInt(max, MaxInt(min, n))
}

// MinInt64 choice smallest integer from its arguments.
func MinInt64(x int64, xs ...int64) int64 {
min := x
for _, n := range xs {
if n < min {
min = n
}
}
return min
return mathutil.Min(max, mathutil.Max(min, n))
}

// NextPowerOfTwo returns the smallest power of two greater than or equal to `i`
Expand Down
21 changes: 0 additions & 21 deletions br/pkg/utils/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestMinInt(t *testing.T) {
require.Equal(t, 1, MinInt(1, 2))
require.Equal(t, 1, MinInt(2, 1))
require.Equal(t, 1, MinInt(4, 2, 1, 3))
require.Equal(t, 1, MinInt(1, 1))
}

func TestMaxInt(t *testing.T) {
require.Equal(t, 2, MaxInt(1, 2))
require.Equal(t, 2, MaxInt(2, 1))
require.Equal(t, 4, MaxInt(4, 2, 1, 3))
require.Equal(t, 1, MaxInt(1, 1))
}

func TestClampInt(t *testing.T) {
require.Equal(t, 3, ClampInt(100, 1, 3))
require.Equal(t, 2, ClampInt(2, 1, 3))
Expand All @@ -30,13 +16,6 @@ func TestClampInt(t *testing.T) {
require.Equal(t, 1, ClampInt(100, 1, 1))
}

func TestMinInt64(t *testing.T) {
require.Equal(t, 1, MinInt(1, 2))
require.Equal(t, 1, MinInt(2, 1))
require.Equal(t, 1, MinInt(4, 2, 1, 3))
require.Equal(t, 1, MinInt(1, 1))
}

func TestNextPowerOfTwo(t *testing.T) {
require.Equal(t, int64(1), NextPowerOfTwo(1))
require.Equal(t, int64(4), NextPowerOfTwo(3))
Expand Down
6 changes: 3 additions & 3 deletions cmd/importer/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"sync"
"time"

"github.com/cznic/mathutil"
"github.com/pingcap/tidb/util/mathutil"
)

type datum struct {
Expand Down Expand Up @@ -75,8 +75,8 @@ func (d *datum) nextInt64() int64 {
defer d.Unlock()

if d.useRange {
d.intValue = mathutil.MinInt64(d.intValue, d.maxIntValue)
d.intValue = mathutil.MaxInt64(d.intValue, d.minIntValue)
d.intValue = mathutil.Min(d.intValue, d.maxIntValue)
d.intValue = mathutil.Max(d.intValue, d.minIntValue)
}
d.updateRemains()
return d.intValue
Expand Down
Loading

0 comments on commit f88dab9

Please sign in to comment.