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

planner: remove pickFromBucket field in LRUPlanCache #40668

Merged
merged 7 commits into from
Jan 28, 2023
Merged
Changes from 2 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
4 changes: 2 additions & 2 deletions executor/seqtest/prepared_test.go
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
2 changes: 1 addition & 1 deletion expression/integration_serial_test.go
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
@@ -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()
23 changes: 9 additions & 14 deletions planner/core/plan_cache_lru.go
Original file line number Diff line number Diff line change
@@ -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)

@@ -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,
}
}

@@ -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)
18 changes: 9 additions & 9 deletions planner/core/plan_cache_lru_test.go
Original file line number Diff line number Diff line change
@@ -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
}
@@ -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)
@@ -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)
@@ -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)
@@ -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
}
@@ -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)}
@@ -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)
@@ -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
2 changes: 1 addition & 1 deletion planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

2 changes: 1 addition & 1 deletion planner/core/prepare_test.go
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 2 additions & 4 deletions session/session.go
Original file line number Diff line number Diff line change
@@ -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
}
@@ -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
}