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

sessionctx: move shardRand from TransactionContext to SessionVars to reduce allocation #39661

Merged
merged 9 commits into from Dec 8, 2022
2 changes: 1 addition & 1 deletion executor/insert_common.go
Expand Up @@ -1008,7 +1008,7 @@ func (e *InsertValues) allocAutoRandomID(ctx context.Context, fieldType *types.F
if err != nil {
return 0, err
}
currentShard := e.ctx.GetSessionVars().TxnCtx.GetCurrentShard(1)
currentShard := e.ctx.GetSessionVars().GetCurrentShard(1)
return shardFmt.Compose(currentShard, autoRandomID), nil
}

Expand Down
17 changes: 17 additions & 0 deletions session/bench_test.go
Expand Up @@ -1855,6 +1855,22 @@ func BenchmarkCompileStmt(b *testing.B) {
b.StopTimer()
}

func BenchmarkAutoIncrement(b *testing.B) {
se, do, st := prepareBenchSession()
defer func() {
se.Close()
do.Close()
st.Close()
}()
mustExecute(se, "create table auto_inc (id int unsigned key nonclustered auto_increment) shard_row_id_bits=4 auto_id_cache 1;")
mustExecute(se, "set @@tidb_enable_mutation_checker = false")
b.ResetTimer()
for i := 0; i < b.N; i++ {
mustExecute(se, "insert into auto_inc values ()")
}
b.StopTimer()
}

// TestBenchDaily collects the daily benchmark test result and generates a json output file.
// The format of the json output is described by the BenchOutput.
// Used by this command in the Makefile
Expand Down Expand Up @@ -1887,5 +1903,6 @@ func TestBenchDaily(t *testing.T) {
BenchmarkHashPartitionPruningMultiSelect,
BenchmarkInsertIntoSelect,
BenchmarkCompileStmt,
BenchmarkAutoIncrement,
)
}
17 changes: 10 additions & 7 deletions sessionctx/variable/session.go
Expand Up @@ -196,7 +196,6 @@ type TxnCtxNoNeedToRestore struct {
ShardStep int
shardRemain int
currentShard int64
shardRand *rand.Rand

// unchangedRowKeys is used to store the unchanged rows that needs to lock for pessimistic transaction.
unchangedRowKeys map[string]struct{}
Expand Down Expand Up @@ -246,21 +245,22 @@ type SavepointRecord struct {
}

// GetCurrentShard returns the shard for the next `count` IDs.
func (tc *TransactionContext) GetCurrentShard(count int) int64 {
if tc.shardRand == nil {
tc.shardRand = rand.New(rand.NewSource(int64(tc.StartTS))) // #nosec G404
func (sv *SessionVars) GetCurrentShard(count int) int64 {
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
tc := sv.TxnCtx
if sv.shardRand == nil {
sv.shardRand = rand.New(rand.NewSource(int64(tc.StartTS))) // #nosec G404
}
if tc.shardRemain <= 0 {
tc.updateShard()
tc.updateShard(sv.shardRand)
tc.shardRemain = tc.ShardStep
}
tc.shardRemain -= count
return tc.currentShard
}

func (tc *TransactionContext) updateShard() {
func (tc *TransactionContext) updateShard(shardRand *rand.Rand) {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], tc.shardRand.Uint64())
binary.LittleEndian.PutUint64(buf[:], shardRand.Uint64())
tc.currentShard = int64(murmur3.Sum32(buf[:]))
}

Expand Down Expand Up @@ -1322,6 +1322,9 @@ type SessionVars struct {

// StoreBatchSize indicates the batch size limit of store batch, set this field to 0 to disable store batch.
StoreBatchSize int

// shardRand is used by TxnCtx, for the GetCurrentShard() method.
shardRand *rand.Rand
}

// GetNewChunkWithCapacity Attempt to request memory from the chunk pool
Expand Down
3 changes: 1 addition & 2 deletions table/tables/tables.go
Expand Up @@ -1521,8 +1521,7 @@ func allocHandleIDs(ctx context.Context, sctx sessionctx.Context, t table.Table,
// shard = 0010000000000000000000000000000000000000000000000000000000000000
return 0, 0, autoid.ErrAutoincReadFailed
}
txnCtx := sctx.GetSessionVars().TxnCtx
shard := txnCtx.GetCurrentShard(int(n))
shard := sctx.GetSessionVars().GetCurrentShard(int(n))
base = shardFmt.Compose(shard, base)
maxID = shardFmt.Compose(shard, maxID)
}
Expand Down