Skip to content

Commit

Permalink
planner: remove pickFromBucket field in LRUPlanCache (#40668)
Browse files Browse the repository at this point in the history
ref #36598
  • Loading branch information
fzzf678 authored Jan 28, 2023
1 parent 0220773 commit bca433f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 34 deletions.
4 changes: 2 additions & 2 deletions executor/seqtest/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func TestPrepareWithAggregation(t *testing.T) {
tk.MustExec(fmt.Sprintf(`set @@tidb_enable_prepared_plan_cache=%v`, flag))

se, err := session.CreateSession4TestWithOpt(store, &session.Opt{
PreparedPlanCache: plannercore.NewLRUPlanCache(100, 0.1, math.MaxUint64, plannercore.PickPlanFromBucket, tk.Session()),
PreparedPlanCache: plannercore.NewLRUPlanCache(100, 0.1, math.MaxUint64, tk.Session()),
})
require.NoError(t, err)
tk.SetSession(se)
Expand Down Expand Up @@ -599,7 +599,7 @@ func TestPrepareDealloc(t *testing.T) {
tk.MustExec(`set @@tidb_enable_prepared_plan_cache=true`)

se, err := session.CreateSession4TestWithOpt(store, &session.Opt{
PreparedPlanCache: plannercore.NewLRUPlanCache(3, 0.1, math.MaxUint64, plannercore.PickPlanFromBucket, tk.Session()),
PreparedPlanCache: plannercore.NewLRUPlanCache(3, 0.1, math.MaxUint64, tk.Session()),
})
require.NoError(t, err)
tk.SetSession(se)
Expand Down
2 changes: 1 addition & 1 deletion expression/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3790,7 +3790,7 @@ func TestPreparePlanCacheOnCachedTable(t *testing.T) {

var err error
se, err := session.CreateSession4TestWithOpt(store, &session.Opt{
PreparedPlanCache: plannercore.NewLRUPlanCache(100, 0.1, math.MaxUint64, plannercore.PickPlanFromBucket, tk.Session()),
PreparedPlanCache: plannercore.NewLRUPlanCache(100, 0.1, math.MaxUint64, tk.Session()),
})
require.NoError(t, err)
tk.SetSession(se)
Expand Down
3 changes: 1 addition & 2 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func newTestKitWithRoot(t *testing.T, store kv.Storage) *testkit.TestKit {

func newTestKitWithPlanCache(t *testing.T, store kv.Storage) *testkit.TestKit {
tk := testkit.NewTestKit(t, store)
se, err := session.CreateSession4TestWithOpt(store, &session.Opt{PreparedPlanCache: plannercore.NewLRUPlanCache(100,
0.1, math.MaxUint64, plannercore.PickPlanFromBucket, tk.Session())})
se, err := session.CreateSession4TestWithOpt(store, &session.Opt{PreparedPlanCache: plannercore.NewLRUPlanCache(100, 0.1, math.MaxUint64, tk.Session())})
require.NoError(t, err)
tk.SetSession(se)
tk.RefreshConnectionID()
Expand Down
23 changes: 9 additions & 14 deletions planner/core/plan_cache_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ type LRUPlanCache struct {
lruList *list.List
// lock make cache thread safe
lock sync.Mutex

// pickFromBucket get one element from bucket. The LRUPlanCache can not work if it is nil
pickFromBucket func(map[*list.Element]struct{}, *planCacheMatchOpts) (*list.Element, bool)
// onEvict will be called if any eviction happened, only for test use now
onEvict func(kvcache.Key, kvcache.Value)

Expand All @@ -67,21 +64,19 @@ type LRUPlanCache struct {

// NewLRUPlanCache creates a PCLRUCache object, whose capacity is "capacity".
// NOTE: "capacity" should be a positive value.
func NewLRUPlanCache(capacity uint, guard float64, quota uint64,
pickFromBucket func(map[*list.Element]struct{}, *planCacheMatchOpts) (*list.Element, bool), sctx sessionctx.Context) *LRUPlanCache {
func NewLRUPlanCache(capacity uint, guard float64, quota uint64, sctx sessionctx.Context) *LRUPlanCache {
if capacity < 1 {
capacity = 100
logutil.BgLogger().Info("capacity of LRU cache is less than 1, will use default value(100) init cache")
}
return &LRUPlanCache{
capacity: capacity,
size: 0,
buckets: make(map[string]map[*list.Element]struct{}, 1), //Generally one query has one plan
lruList: list.New(),
pickFromBucket: pickFromBucket,
quota: quota,
guard: guard,
sctx: sctx,
capacity: capacity,
size: 0,
buckets: make(map[string]map[*list.Element]struct{}, 1), //Generally one query has one plan
lruList: list.New(),
quota: quota,
guard: guard,
sctx: sctx,
}
}

Expand Down Expand Up @@ -260,7 +255,7 @@ func (l *LRUPlanCache) memoryControl() {
}

// PickPlanFromBucket pick one plan from bucket
func PickPlanFromBucket(bucket map[*list.Element]struct{}, matchOpts *planCacheMatchOpts) (*list.Element, bool) {
func (l *LRUPlanCache) pickFromBucket(bucket map[*list.Element]struct{}, matchOpts *planCacheMatchOpts) (*list.Element, bool) {
for k := range bucket {
plan := k.Value.(*planCacheEntry).PlanValue.(*PlanCacheValue)
ok1 := plan.matchOpts.paramTypes.CheckTypesCompatibility4PC(matchOpts.paramTypes)
Expand Down
18 changes: 9 additions & 9 deletions planner/core/plan_cache_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func randomPlanCacheValue(types []*types.FieldType) *PlanCacheValue {

func TestLRUPCPut(t *testing.T) {
// test initialize
lruA := NewLRUPlanCache(0, 0, 0, PickPlanFromBucket, MockContext())
lruA := NewLRUPlanCache(0, 0, 0, MockContext())
require.Equal(t, lruA.capacity, uint(100))

maxMemDroppedKv := make(map[kvcache.Key]kvcache.Value)
lru := NewLRUPlanCache(3, 0, 0, PickPlanFromBucket, MockContext())
lru := NewLRUPlanCache(3, 0, 0, MockContext())
lru.onEvict = func(key kvcache.Key, value kvcache.Value) {
maxMemDroppedKv[key] = value
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestLRUPCPut(t *testing.T) {
}

func TestLRUPCGet(t *testing.T) {
lru := NewLRUPlanCache(3, 0, 0, PickPlanFromBucket, MockContext())
lru := NewLRUPlanCache(3, 0, 0, MockContext())

keys := make([]*planCacheKey, 5)
vals := make([]*PlanCacheValue, 5)
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestLRUPCGet(t *testing.T) {
}

func TestLRUPCDelete(t *testing.T) {
lru := NewLRUPlanCache(3, 0, 0, PickPlanFromBucket, MockContext())
lru := NewLRUPlanCache(3, 0, 0, MockContext())

keys := make([]*planCacheKey, 3)
vals := make([]*PlanCacheValue, 3)
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestLRUPCDelete(t *testing.T) {
}

func TestLRUPCDeleteAll(t *testing.T) {
lru := NewLRUPlanCache(3, 0, 0, PickPlanFromBucket, MockContext())
lru := NewLRUPlanCache(3, 0, 0, MockContext())

keys := make([]*planCacheKey, 3)
vals := make([]*PlanCacheValue, 3)
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestLRUPCDeleteAll(t *testing.T) {

func TestLRUPCSetCapacity(t *testing.T) {
maxMemDroppedKv := make(map[kvcache.Key]kvcache.Value)
lru := NewLRUPlanCache(5, 0, 0, PickPlanFromBucket, MockContext())
lru := NewLRUPlanCache(5, 0, 0, MockContext())
lru.onEvict = func(key kvcache.Key, value kvcache.Value) {
maxMemDroppedKv[key] = value
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestLRUPCSetCapacity(t *testing.T) {
}

func TestIssue37914(t *testing.T) {
lru := NewLRUPlanCache(3, 0.1, 1, PickPlanFromBucket, MockContext())
lru := NewLRUPlanCache(3, 0.1, 1, MockContext())

pTypes := []*types.FieldType{types.NewFieldType(mysql.TypeFloat), types.NewFieldType(mysql.TypeDouble)}
key := &planCacheKey{database: strconv.FormatInt(int64(1), 10)}
Expand All @@ -330,7 +330,7 @@ func TestIssue37914(t *testing.T) {
}

func TestIssue38244(t *testing.T) {
lru := NewLRUPlanCache(3, 0, 0, PickPlanFromBucket, MockContext())
lru := NewLRUPlanCache(3, 0, 0, MockContext())
require.Equal(t, uint(3), lru.capacity)

keys := make([]*planCacheKey, 5)
Expand All @@ -357,7 +357,7 @@ func TestLRUPlanCacheMemoryUsage(t *testing.T) {
pTypes := []*types.FieldType{types.NewFieldType(mysql.TypeFloat), types.NewFieldType(mysql.TypeDouble)}
ctx := MockContext()
ctx.GetSessionVars().EnablePreparedPlanCacheMemoryMonitor = true
lru := NewLRUPlanCache(3, 0, 0, PickPlanFromBucket, ctx)
lru := NewLRUPlanCache(3, 0, 0, ctx)
evict := make(map[kvcache.Key]kvcache.Value)
lru.onEvict = func(key kvcache.Key, value kvcache.Value) {
evict[key] = value
Expand Down
2 changes: 1 addition & 1 deletion planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestInitLRUWithSystemVar(t *testing.T) {
tk.MustQuery("select @@session.tidb_prepared_plan_cache_size").Check(testkit.Rows("1"))
sessionVar := tk.Session().GetSessionVars()

lru := plannercore.NewLRUPlanCache(uint(sessionVar.PreparedPlanCacheSize), 0, 0, plannercore.PickPlanFromBucket, tk.Session())
lru := plannercore.NewLRUPlanCache(uint(sessionVar.PreparedPlanCacheSize), 0, 0, tk.Session())
require.NotNil(t, lru)
}

Expand Down
2 changes: 1 addition & 1 deletion planner/core/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ func TestPlanCacheSwitchDB(t *testing.T) {

// DB is not specified
se2, err := session.CreateSession4TestWithOpt(store, &session.Opt{
PreparedPlanCache: core.NewLRUPlanCache(100, 0.1, math.MaxUint64, core.PickPlanFromBucket, tk.Session()),
PreparedPlanCache: core.NewLRUPlanCache(100, 0.1, math.MaxUint64, tk.Session()),
})
require.NoError(t, err)
tk2 := testkit.NewTestKitWithSession(t, store, se2)
Expand Down
6 changes: 2 additions & 4 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,7 @@ func (s *session) GetPlanCache(isNonPrepared bool) sessionctx.PlanCache {
}
if s.nonPreparedPlanCache == nil { // lazy construction
s.nonPreparedPlanCache = plannercore.NewLRUPlanCache(uint(s.GetSessionVars().NonPreparedPlanCacheSize),
variable.PreparedPlanCacheMemoryGuardRatio.Load(), plannercore.PreparedPlanCacheMaxMemory.Load(),
plannercore.PickPlanFromBucket, s)
variable.PreparedPlanCacheMemoryGuardRatio.Load(), plannercore.PreparedPlanCacheMaxMemory.Load(), s)
}
return s.nonPreparedPlanCache
}
Expand All @@ -477,8 +476,7 @@ func (s *session) GetPlanCache(isNonPrepared bool) sessionctx.PlanCache {
}
if s.preparedPlanCache == nil { // lazy construction
s.preparedPlanCache = plannercore.NewLRUPlanCache(uint(s.GetSessionVars().PreparedPlanCacheSize),
variable.PreparedPlanCacheMemoryGuardRatio.Load(), plannercore.PreparedPlanCacheMaxMemory.Load(),
plannercore.PickPlanFromBucket, s)
variable.PreparedPlanCacheMemoryGuardRatio.Load(), plannercore.PreparedPlanCacheMaxMemory.Load(), s)
}
return s.preparedPlanCache
}
Expand Down

0 comments on commit bca433f

Please sign in to comment.