Skip to content

Commit

Permalink
*: run a bootstrap session before store is ready (pingcap#2481)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored and hanfei1991 committed Jan 17, 2017
1 parent e79a460 commit e1ecc10
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 95 deletions.
6 changes: 4 additions & 2 deletions bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func (s *testSessionSuite) TestBootstrap(c *C) {
defer testleak.AfterTest(c)()
store := newStore(c, s.dbName)
store := newStoreWithBootstrap(c, s.dbName)
se := newSession(c, store, s.dbName)
mustExecSQL(c, se, "USE mysql;")
r := mustExecSQL(c, se, `select * from user;`)
Expand Down Expand Up @@ -121,6 +121,7 @@ func (s *testSessionSuite) TestBootstrapWithError(c *C) {
store := newStore(c, s.dbNameBootstrap)
s.bootstrapWithOnlyDDLWork(store, c)

BootstrapSession(store)
se := newSession(c, store, s.dbNameBootstrap)
mustExecSQL(c, se, "USE mysql;")
r := mustExecSQL(c, se, `select * from user;`)
Expand Down Expand Up @@ -153,7 +154,7 @@ func (s *testSessionSuite) TestBootstrapWithError(c *C) {
// Test case for upgrade
func (s *testSessionSuite) TestUpgrade(c *C) {
defer testleak.AfterTest(c)()
store := newStore(c, s.dbName)
store := newStoreWithBootstrap(c, s.dbName)
se := newSession(c, store, s.dbName)
mustExecSQL(c, se, "USE mysql;")

Expand Down Expand Up @@ -195,6 +196,7 @@ func (s *testSessionSuite) TestUpgrade(c *C) {
c.Assert(ver, Equals, int64(0))

// Create a new session then upgrade() will run automatically.
BootstrapSession(store)
se2 := newSession(c, store, s.dbName)
r = mustExecSQL(c, se2, `SELECT VARIABLE_VALUE from mysql.TiDB where VARIABLE_NAME="tidb_server_version";`)
row, err = r.Next()
Expand Down
4 changes: 4 additions & 0 deletions ddl/ddl_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (s *testDBSuite) SetUpSuite(c *C) {
s.schemaName = "test_db"
s.store, err = tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
tidb.BootstrapSession(s.store)
localstore.MockRemoteStore = true
s.s, err = tidb.CreateSession(s.store)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -755,6 +756,7 @@ func (s *testDBSuite) TestUpdateMultipleTable(c *C) {
defer testleak.AfterTest(c)
store, err := tidb.NewStore("memory://update_multiple_table")
c.Assert(err, IsNil)
tidb.BootstrapSession(store)
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (c1 int, c2 int)")
Expand Down Expand Up @@ -814,6 +816,7 @@ func (s *testDBSuite) TestTruncateTable(c *C) {
defer testleak.AfterTest(c)
store, err := tidb.NewStore("memory://truncate_table")
c.Assert(err, IsNil)
tidb.BootstrapSession(store)
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
tk.MustExec("create table t (c1 int, c2 int)")
Expand Down Expand Up @@ -864,6 +867,7 @@ func (s *testDBSuite) TestRenameTable(c *C) {
defer testleak.AfterTest(c)
store, err := tidb.NewStore("memory://rename_table")
c.Assert(err, IsNil)
tidb.BootstrapSession(store)
s.tk = testkit.NewTestKit(c, store)
s.tk.MustExec("use test")

Expand Down
2 changes: 2 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (s *testSuite) SetUpSuite(c *C) {
c.Assert(err, IsNil)
s.store = store
}
err := tidb.BootstrapSession(s.store)
c.Assert(err, IsNil)
logLevel := os.Getenv("log_level")
log.SetLevelByString(logLevel)
executor.BaseLookupTableTaskSize = 2
Expand Down
6 changes: 6 additions & 0 deletions perfschema/perfschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func (p *testPerfSchemaSuit) TestInsert(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
c.Assert(err, IsNil)
defer store.Close()
err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
se := newSession(c, store, "")
defer se.Close()
mustExec(c, se, `insert into performance_schema.setup_actors values("localhost", "nieyy", "contributor", "NO", "NO");`)
Expand Down Expand Up @@ -108,6 +110,8 @@ func (p *testPerfSchemaSuit) TestInstrument(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory + "/test_instrument_db")
c.Assert(err, IsNil)
defer store.Close()
err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
se := newSession(c, store, "test_instrument_db")
defer se.Close()

Expand All @@ -122,6 +126,8 @@ func (p *testPerfSchemaSuit) TestConcurrentStatement(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory + "/test_con_stmt")
c.Assert(err, IsNil)
defer store.Close()
err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
se := newSession(c, store, "test_con_stmt")

mustExec(c, se, "drop table if exists test")
Expand Down
3 changes: 1 addition & 2 deletions plan/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package plan_test

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/parser"
Expand Down Expand Up @@ -81,7 +80,7 @@ var resolverTestCases = []resolverTestCase{
}

func (ts *testNameResolverSuite) TestNameResolver(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
testKit := testkit.NewTestKit(c, store)
Expand Down
15 changes: 13 additions & 2 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package plan_test

import (
"github.com/juju/errors"
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/plan"
Expand All @@ -34,7 +36,7 @@ type testTypeInferrerSuite struct {
}

func (ts *testTypeInferrerSuite) TestInferType(c *C) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
testKit := testkit.NewTestKit(c, store)
Expand Down Expand Up @@ -196,7 +198,7 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {

func (s *testTypeInferrerSuite) TestColumnInfoModified(c *C) {
defer testleak.AfterTest(c)()
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
testKit := testkit.NewTestKit(c, store)
Expand All @@ -210,3 +212,12 @@ func (s *testTypeInferrerSuite) TestColumnInfoModified(c *C) {
col := table.FindCol(tbl.Cols(), "col1")
c.Assert(col.Tp, Equals, mysql.TypeLong)
}

func newStoreWithBootstrap() (kv.Storage, error) {
store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory)
if err != nil {
return nil, errors.Trace(err)
}
err = tidb.BootstrapSession(store)
return store, errors.Trace(err)
}
2 changes: 2 additions & 0 deletions privilege/privileges/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type testCacheSuite struct {
func (s *testCacheSuite) SetUpSuite(c *C) {
store, err := tidb.NewStore("memory://mysql")
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
s.store = store
}

Expand Down
2 changes: 2 additions & 0 deletions privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func mustExec(c *C, se tidb.Session, sql string) {
func newStore(c *C, dbPath string) kv.Storage {
store, err := tidb.NewStore("memory" + "://" + dbPath)
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
return store
}

Expand Down
2 changes: 2 additions & 0 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (ts *TidbTestSuite) SetUpSuite(c *C) {
log.SetLevelByString("error")
store, err := tidb.NewStore("memory:///tmp/tidb")
c.Assert(err, IsNil)
err = tidb.BootstrapSession(store)
c.Assert(err, IsNil)
ts.tidbdrv = NewTiDBDriver(store)
cfg := &Config{
Addr: ":4001",
Expand Down
20 changes: 16 additions & 4 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,14 +702,29 @@ func chooseMinLease(n1 time.Duration, n2 time.Duration) time.Duration {

// CreateSession creates a new session environment.
func CreateSession(store kv.Storage) (Session, error) {
s, err := createSession(store)
if err != nil {
return nil, errors.Trace(err)
}

// TODO: Add auth here
privChecker := &privileges.UserPrivileges{}
privilege.BindPrivilegeChecker(s, privChecker)

return s, nil
}

// BootstrapSession runs the first time when the TiDB server start.
func BootstrapSession(store kv.Storage) error {
ver := getStoreBootstrapVersion(store)
if ver == notBootstrapped {
runInBootstrapSession(store, bootstrap)
} else if ver < currentBootstrapVersion {
runInBootstrapSession(store, upgrade)
}

return createSession(store)
_, err := domap.Get(store)
return errors.Trace(err)
}

// runInBootstrapSession create a special session for boostrap to run.
Expand Down Expand Up @@ -753,9 +768,6 @@ func createSession(store kv.Storage) (*session, error) {
// session implements variable.GlobalVarAccessor. Bind it to ctx.
s.sessionVars.GlobalVarsAccessor = s

// TODO: Add auth here
privChecker := &privileges.UserPrivileges{}
privilege.BindPrivilegeChecker(s, privChecker)
return s, nil
}

Expand Down
Loading

0 comments on commit e1ecc10

Please sign in to comment.