Skip to content

Commit

Permalink
This is an automated cherry-pick of #44384
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
xuyifangreeneyes authored and ti-chi-bot committed Jun 5, 2023
1 parent ac8669c commit 2c341f6
Show file tree
Hide file tree
Showing 6 changed files with 902 additions and 40 deletions.
167 changes: 167 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,173 @@ type SessionVars struct {

// EnableINLJoinInnerMultiPattern indicates whether enable multi pattern for index join inner side
EnableINLJoinInnerMultiPattern bool
<<<<<<< HEAD
=======

// Enable late materialization: push down some selection condition to tablescan.
EnableLateMaterialization bool

// EnableRowLevelChecksum indicates whether row level checksum is enabled.
EnableRowLevelChecksum bool

// TiFlashComputeDispatchPolicy indicates how to dipatch task to tiflash_compute nodes.
// Only for disaggregated-tiflash mode.
TiFlashComputeDispatchPolicy tiflashcompute.DispatchPolicy

// SlowTxnThreshold is the threshold of slow transaction logs
SlowTxnThreshold uint64

// LoadBasedReplicaReadThreshold is the threshold for the estimated wait duration of a store.
// If exceeding the threshold, try other stores using replica read.
LoadBasedReplicaReadThreshold time.Duration

// OptOrderingIdxSelThresh is the threshold for optimizer to consider the ordering index.
// If there exists an index whose estimated selectivity is smaller than this threshold, the optimizer won't
// use the ExpectedCnt to adjust the estimated row count for index scan.
OptOrderingIdxSelThresh float64

// EnableMPPSharedCTEExecution indicates whether we enable the shared CTE execution strategy on MPP side.
EnableMPPSharedCTEExecution bool

// OptimizerFixControl control some details of the optimizer behavior through the tidb_opt_fix_control variable.
OptimizerFixControl map[uint64]string

// HypoIndexes are for the Index Advisor.
HypoIndexes map[string]map[string]map[string]*model.IndexInfo // dbName -> tblName -> idxName -> idxInfo

// Runtime Filter Group
// Runtime filter type: only support IN or MIN_MAX now.
// Runtime filter type can take multiple values at the same time.
runtimeFilterTypes []RuntimeFilterType
// Runtime filter mode: only support OFF, LOCAL now
runtimeFilterMode RuntimeFilterMode
}

var (
// variables below are for the optimizer fix control.

// TiDBOptFixControl44262 controls whether to allow to use dynamic-mode to access partitioning tables without global-stats (#44262).
TiDBOptFixControl44262 uint64 = 44262
// TiDBOptFixControl44389 controls whether to consider non-point ranges of some CNF item when building ranges.
TiDBOptFixControl44389 uint64 = 44389
)

// GetOptimizerFixControlValue returns the specified value of the optimizer fix control.
func (s *SessionVars) GetOptimizerFixControlValue(key uint64) (value string, exist bool) {
if s.OptimizerFixControl == nil {
return "", false
}
value, exist = s.OptimizerFixControl[key]
return
}

// planReplayerSessionFinishedTaskKeyLen is used to control the max size for the finished plan replayer task key in session
// in order to control the used memory
const planReplayerSessionFinishedTaskKeyLen = 128

// AddPlanReplayerFinishedTaskKey record finished task key in session
func (s *SessionVars) AddPlanReplayerFinishedTaskKey(key replayer.PlanReplayerTaskKey) {
if len(s.PlanReplayerFinishedTaskKey) >= planReplayerSessionFinishedTaskKeyLen {
s.initializePlanReplayerFinishedTaskKey()
}
s.PlanReplayerFinishedTaskKey[key] = struct{}{}
}

func (s *SessionVars) initializePlanReplayerFinishedTaskKey() {
s.PlanReplayerFinishedTaskKey = make(map[replayer.PlanReplayerTaskKey]struct{}, planReplayerSessionFinishedTaskKeyLen)
}

// CheckPlanReplayerFinishedTaskKey check whether the key exists
func (s *SessionVars) CheckPlanReplayerFinishedTaskKey(key replayer.PlanReplayerTaskKey) bool {
if s.PlanReplayerFinishedTaskKey == nil {
s.initializePlanReplayerFinishedTaskKey()
return false
}
_, ok := s.PlanReplayerFinishedTaskKey[key]
return ok
}

// IsPlanReplayerCaptureEnabled indicates whether capture or continues capture enabled
func (s *SessionVars) IsPlanReplayerCaptureEnabled() bool {
return s.EnablePlanReplayerCapture || s.EnablePlanReplayedContinuesCapture
}

// GetNewChunkWithCapacity Attempt to request memory from the chunk pool
// thread safety
func (s *SessionVars) GetNewChunkWithCapacity(fields []*types.FieldType, capacity int, maxCachesize int, pool chunk.Allocator) *chunk.Chunk {
if pool == nil {
return chunk.New(fields, capacity, maxCachesize)
}
s.ChunkPool.mu.Lock()
defer s.ChunkPool.mu.Unlock()
if pool.CheckReuseAllocSize() && (!s.GetUseChunkAlloc()) {
s.StmtCtx.SetUseChunkAlloc()
}
chk := pool.Alloc(fields, capacity, maxCachesize)
return chk
}

// ExchangeChunkStatus give the status to preUseChunkAlloc
func (s *SessionVars) ExchangeChunkStatus() {
s.preUseChunkAlloc = s.GetUseChunkAlloc()
}

// GetUseChunkAlloc return useChunkAlloc status
func (s *SessionVars) GetUseChunkAlloc() bool {
return s.StmtCtx.GetUseChunkAllocStatus()
}

// SetAlloc Attempt to set the buffer pool address
func (s *SessionVars) SetAlloc(alloc chunk.Allocator) {
if !s.EnableReuseCheck {
return
}
s.ChunkPool.Alloc = alloc
}

// IsAllocValid check if chunk reuse is enable or ChunkPool is inused.
func (s *SessionVars) IsAllocValid() bool {
if !s.EnableReuseCheck {
return false
}
s.ChunkPool.mu.Lock()
defer s.ChunkPool.mu.Unlock()
return s.ChunkPool.Alloc != nil
}

// ClearAlloc indicates stop reuse chunk
func (s *SessionVars) ClearAlloc(alloc *chunk.Allocator, b bool) {
if !b {
s.ChunkPool.Alloc = nil
return
}

// If an error is reported, re-apply for alloc
// Prevent the goroutine left before, affecting the execution of the next sql
// issuse 38918
s.ChunkPool.mu.Lock()
s.ChunkPool.Alloc = nil
s.ChunkPool.mu.Unlock()
*alloc = chunk.NewAllocator()
}

// GetPreparedStmtByName returns the prepared statement specified by stmtName.
func (s *SessionVars) GetPreparedStmtByName(stmtName string) (interface{}, error) {
stmtID, ok := s.PreparedStmtNameToID[stmtName]
if !ok {
return nil, ErrStmtNotFound
}
return s.GetPreparedStmtByID(stmtID)
}

// GetPreparedStmtByID returns the prepared statement specified by stmtID.
func (s *SessionVars) GetPreparedStmtByID(stmtID uint32) (interface{}, error) {
stmt, ok := s.PreparedStmts[stmtID]
if !ok {
return nil, ErrStmtNotFound
}
return stmt, nil
>>>>>>> 85d6323e3a3 (util/ranger: consider good non-point ranges from CNF item (#44384))
}

// InitStatementContext initializes a StatementContext, the object is reused to reduce allocation.
Expand Down
70 changes: 70 additions & 0 deletions util/ranger/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "ranger",
srcs = [
"checker.go",
"detacher.go",
"points.go",
"ranger.go",
"types.go",
],
importpath = "github.com/pingcap/tidb/util/ranger",
visibility = ["//visibility:public"],
deps = [
"//errno",
"//expression",
"//kv",
"//parser/ast",
"//parser/charset",
"//parser/format",
"//parser/model",
"//parser/mysql",
"//parser/terror",
"//sessionctx",
"//sessionctx/stmtctx",
"//sessionctx/variable",
"//types",
"//types/parser_driver",
"//util/chunk",
"//util/codec",
"//util/collate",
"//util/dbterror",
"//util/mathutil",
"@com_github_pingcap_errors//:errors",
"@org_golang_x_exp//slices",
],
)

go_test(
name = "ranger_test",
timeout = "short",
srcs = [
"bench_test.go",
"main_test.go",
"ranger_test.go",
"types_test.go",
],
data = glob(["testdata/**"]),
flaky = True,
shard_count = 26,
deps = [
":ranger",
"//config",
"//expression",
"//parser/ast",
"//parser/model",
"//parser/mysql",
"//planner/core",
"//session",
"//sessionctx",
"//sessionctx/variable",
"//testkit",
"//testkit/testdata",
"//testkit/testsetup",
"//types",
"//util/collate",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
],
)
Loading

0 comments on commit 2c341f6

Please sign in to comment.