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

*: fix data race in the file of prepare_test.go #7232

Merged
merged 4 commits into from Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/prepared.go
Expand Up @@ -152,7 +152,7 @@ func (e *PrepareExec) Next(ctx context.Context, chk *chunk.Chunk) error {
Params: sorter.markers,
SchemaVersion: e.is.SchemaMetaVersion(),
}
prepared.UseCache = plan.PreparedPlanCacheEnabled && (vars.ImportingData || plan.Cacheable(stmt))
prepared.UseCache = plan.PreparedPlanCacheEnabled() && (vars.ImportingData || plan.Cacheable(stmt))

// We try to build the real statement of preparedStmt.
for i := range prepared.Params {
Expand Down
24 changes: 12 additions & 12 deletions executor/prepared_test.go
Expand Up @@ -27,16 +27,16 @@ import (
)

func (s *testSuite) TestPrepared(c *C) {
orgEnable := plan.PreparedPlanCacheEnabled
orgEnable := plan.PreparedPlanCacheEnabled()
orgCapacity := plan.PreparedPlanCacheCapacity
defer func() {
plan.PreparedPlanCacheEnabled = orgEnable
plan.SetPreparedPlanCache(orgEnable)
plan.PreparedPlanCacheCapacity = orgCapacity
}()
flags := []bool{false, true}
ctx := context.Background()
for _, flag := range flags {
plan.PreparedPlanCacheEnabled = flag
plan.SetPreparedPlanCache(flag)
plan.PreparedPlanCacheCapacity = 100
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -187,16 +187,16 @@ func (s *testSuite) TestPrepared(c *C) {
}

func (s *testSuite) TestPreparedLimitOffset(c *C) {
orgEnable := plan.PreparedPlanCacheEnabled
orgEnable := plan.PreparedPlanCacheEnabled()
orgCapacity := plan.PreparedPlanCacheCapacity
defer func() {
plan.PreparedPlanCacheEnabled = orgEnable
plan.SetPreparedPlanCache(orgEnable)
plan.PreparedPlanCacheCapacity = orgCapacity
}()
flags := []bool{false, true}
ctx := context.Background()
for _, flag := range flags {
plan.PreparedPlanCacheEnabled = flag
plan.SetPreparedPlanCache(flag)
plan.PreparedPlanCacheCapacity = 100
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand All @@ -223,15 +223,15 @@ func (s *testSuite) TestPreparedLimitOffset(c *C) {
}

func (s *testSuite) TestPreparedNullParam(c *C) {
orgEnable := plan.PreparedPlanCacheEnabled
orgEnable := plan.PreparedPlanCacheEnabled()
orgCapacity := plan.PreparedPlanCacheCapacity
defer func() {
plan.PreparedPlanCacheEnabled = orgEnable
plan.SetPreparedPlanCache(orgEnable)
plan.PreparedPlanCacheCapacity = orgCapacity
}()
flags := []bool{false, true}
for _, flag := range flags {
plan.PreparedPlanCacheEnabled = flag
plan.SetPreparedPlanCache(flag)
plan.PreparedPlanCacheCapacity = 100
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -287,15 +287,15 @@ func (s *testSuite) TestPrepareMaxParamCountCheck(c *C) {
}

func (s *testSuite) TestPrepareWithAggregation(c *C) {
orgEnable := plan.PreparedPlanCacheEnabled
orgEnable := plan.PreparedPlanCacheEnabled()
orgCapacity := plan.PreparedPlanCacheCapacity
defer func() {
plan.PreparedPlanCacheEnabled = orgEnable
plan.SetPreparedPlanCache(orgEnable)
plan.PreparedPlanCacheCapacity = orgCapacity
}()
flags := []bool{false, true}
for _, flag := range flags {
plan.PreparedPlanCacheEnabled = flag
plan.SetPreparedPlanCache(flag)
plan.PreparedPlanCacheCapacity = 100
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
30 changes: 28 additions & 2 deletions plan/cache.go
Expand Up @@ -14,6 +14,7 @@
package plan

import (
"sync/atomic"
"time"

"github.com/pingcap/tidb/mysql"
Expand All @@ -24,12 +25,37 @@ import (
)

var (
// PreparedPlanCacheEnabled stores the global config "prepared-plan-cache-enabled".
PreparedPlanCacheEnabled bool
// preparedPlanCacheEnabledValue stores the global config "prepared-plan-cache-enabled".
// If the value of "prepared-plan-cache-enabled" is true, preparedPlanCacheEnabledValue's value is 1.
// Otherwise, preparedPlanCacheEnabledValue's value is 0.
preparedPlanCacheEnabledValue int32
// PreparedPlanCacheCapacity stores the global config "prepared-plan-cache-capacity".
PreparedPlanCacheCapacity uint
)

const (
preparedPlanCacheEnabled = 1
preparedPlanCacheUnable = 0
)

// SetPreparedPlanCache sets isEnabled to true, then prepared plan cache is enabled.
func SetPreparedPlanCache(isEnabled bool) {
if isEnabled {
atomic.StoreInt32(&preparedPlanCacheEnabledValue, preparedPlanCacheEnabled)
} else {
atomic.StoreInt32(&preparedPlanCacheEnabledValue, preparedPlanCacheUnable)
}
}

// PreparedPlanCacheEnabled returns whether the prepared plan cache is enabled.
func PreparedPlanCacheEnabled() bool {
isEnabled := atomic.LoadInt32(&preparedPlanCacheEnabledValue)
if isEnabled == preparedPlanCacheEnabled {
return true
}
return false
}

type pstmtPlanCacheKey struct {
database string
connID uint64
Expand Down
2 changes: 1 addition & 1 deletion plan/point_get_plan.go
Expand Up @@ -114,7 +114,7 @@ func (p *PointGetPlan) SetChildren(...PhysicalPlan) {}
func (p *PointGetPlan) ResolveIndices() {}

func tryFastPlan(ctx sessionctx.Context, node ast.Node) Plan {
if PreparedPlanCacheEnabled {
if PreparedPlanCacheEnabled() {
// Do not support plan cache.
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions plan/prepare_test.go
Expand Up @@ -30,15 +30,15 @@ func (s *testPrepareSuite) TestPrepareCache(c *C) {
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
orgEnable := plan.PreparedPlanCacheEnabled
orgEnable := plan.PreparedPlanCacheEnabled()
orgCapacity := plan.PreparedPlanCacheCapacity
defer func() {
dom.Close()
store.Close()
plan.PreparedPlanCacheEnabled = orgEnable
plan.SetPreparedPlanCache(orgEnable)
plan.PreparedPlanCacheCapacity = orgCapacity
}()
plan.PreparedPlanCacheEnabled = true
plan.SetPreparedPlanCache(true)
plan.PreparedPlanCacheCapacity = 100
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
Expand Down
4 changes: 2 additions & 2 deletions session/session.go
Expand Up @@ -1161,7 +1161,7 @@ func createSession(store kv.Storage) (*session, error) {
sessionVars: variable.NewSessionVars(),
ddlOwnerChecker: dom.DDL().OwnerManager(),
}
if plan.PreparedPlanCacheEnabled {
if plan.PreparedPlanCacheEnabled() {
s.preparedPlanCache = kvcache.NewSimpleLRUCache(plan.PreparedPlanCacheCapacity)
}
s.mu.values = make(map[fmt.Stringer]interface{})
Expand All @@ -1183,7 +1183,7 @@ func createSessionWithDomain(store kv.Storage, dom *domain.Domain) (*session, er
parser: parser.New(),
sessionVars: variable.NewSessionVars(),
}
if plan.PreparedPlanCacheEnabled {
if plan.PreparedPlanCacheEnabled() {
s.preparedPlanCache = kvcache.NewSimpleLRUCache(plan.PreparedPlanCacheCapacity)
}
s.mu.values = make(map[fmt.Stringer]interface{})
Expand Down
4 changes: 2 additions & 2 deletions tidb-server/main.go
Expand Up @@ -384,8 +384,8 @@ func setGlobalVars() {
plan.AllowCartesianProduct = cfg.Performance.CrossJoin
privileges.SkipWithGrant = cfg.Security.SkipGrantTable

plan.PreparedPlanCacheEnabled = cfg.PreparedPlanCache.Enabled
if plan.PreparedPlanCacheEnabled {
plan.SetPreparedPlanCache(cfg.PreparedPlanCache.Enabled)
if plan.PreparedPlanCacheEnabled() {
plan.PreparedPlanCacheCapacity = cfg.PreparedPlanCache.Capacity
}

Expand Down