func (s *testKVSuite) SetUpSuite(c *C) { store, err := tidb.NewStore(fmt.Sprintf("%s://%s", *testStore, *testStorePath)) c.Assert(err, IsNil) s.s = store cacheS, _ := tidb.NewStore(fmt.Sprintf("%s://%s", *testStore, *testStorePath)) c.Assert(cacheS, Equals, store) }
func (s *testKVSuite) SetUpSuite(c *C) { store, err := tidb.NewStore(fmt.Sprintf("%s://%s", *testStore, *testStorePath)) c.Assert(err, IsNil) s.s = store cacheS, _ := tidb.NewStore(fmt.Sprintf("%s://%s", *testStore, *testStorePath)) c.Assert(cacheS, Equals, store) logLevel := os.Getenv("log_level") log.SetLevelByString(logLevel) }
func (t *testIsolationSuite) TestInc(c *C) { store, err := tidb.NewStore("memory://test/test_isolation") c.Assert(err, IsNil) defer store.Close() threadCnt := 4 ids := make(map[int64]struct{}, threadCnt*100) var m sync.Mutex var wg sync.WaitGroup wg.Add(threadCnt) for i := 0; i < threadCnt; i++ { go func() { defer wg.Done() for j := 0; j < 100; j++ { var id int64 err := kv.RunInNewTxn(store, true, func(txn kv.Transaction) error { var err1 error id, err1 = kv.IncInt64(txn, []byte("key"), 1) return err1 }) c.Assert(err, IsNil) m.Lock() _, ok := ids[id] ids[id] = struct{}{} m.Unlock() c.Assert(ok, IsFalse) } }() } wg.Wait() }
func (ts *testInfoBinderSuite) TestInfoBinder(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) defer store.Close() testKit := testkit.NewTestKit(c, store) testKit.MustExec("use test") testKit.MustExec("create table t (c1 int, c2 int)") domain := sessionctx.GetDomain(testKit.Se.(context.Context)) src := "SELECT c1 from t" l := parser.NewLexer(src) c.Assert(parser.YYParse(l), Equals, 0) stmts := l.Stmts() c.Assert(len(stmts), Equals, 1) v := &optimizer.InfoBinder{ Info: domain.InfoSchema(), DefaultSchema: model.NewCIStr("test"), } selectStmt := stmts[0].(*ast.SelectStmt) selectStmt.Accept(v) verifier := &binderVerifier{ c: c, } selectStmt.Accept(verifier) }
func main() { flag.Parse() if *lease < 0 { log.Fatalf("invalid lease seconds %d", *lease) } tidb.SetSchemaLease(time.Duration(*lease) * time.Second) log.SetLevelByString(*logLevel) store, err := tidb.NewStore(fmt.Sprintf("%s://%s", *store, *storePath)) if err != nil { log.Fatal(err) } var driver relay.IDriver driver = relay.NewTiDBDriver(store) replayer, err := relay.NewReplayer(driver, *relayPath, *check) if err != nil { log.Fatal(err) } replayer.OnRecordRead = func(rec *relay.Record) { fmt.Printf("%s\n", rec) } err = replayer.Run() if err != nil { log.Fatal(errors.ErrorStack(err)) } }
func (p *testPerfSchemaSuit) TestInsert(c *C) { defer testleak.AfterTest(c)() store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) defer store.Close() se := newSession(c, store, "") defer se.Close() mustExec(c, se, `insert into performance_schema.setup_actors values("localhost", "nieyy", "contributor", "NO", "NO");`) checkResult(c, se, 0, 0) cnt := mustQuery(c, se, "select * from performance_schema.setup_actors") c.Assert(cnt, Equals, 2) mustExec(c, se, `insert into performance_schema.setup_actors (host, user, role) values ("localhost", "lijian", "contributor");`) checkResult(c, se, 0, 0) cnt = mustQuery(c, se, "select * from performance_schema.setup_actors") c.Assert(cnt, Equals, 3) mustExec(c, se, `insert into performance_schema.setup_objects values("EVENT", "test", "%", "NO", "NO")`) checkResult(c, se, 0, 0) cnt = mustQuery(c, se, "select * from performance_schema.setup_objects") c.Assert(cnt, Equals, 13) mustExec(c, se, `insert into performance_schema.setup_objects (object_schema, object_name) values ("test1", "%")`) checkResult(c, se, 0, 0) cnt = mustQuery(c, se, "select * from performance_schema.setup_objects") c.Assert(cnt, Equals, 14) mustFailExec(c, se, `insert into performance_schema.setup_actors (host) values (null);`) mustFailExec(c, se, `insert into performance_schema.setup_objects (object_type) values (null);`) mustFailExec(c, se, `insert into performance_schema.setup_instruments values("select", "N/A", "N/A");`) mustFailExec(c, se, `insert into performance_schema.setup_consumers values("events_stages_current", "N/A");`) mustFailExec(c, se, `insert into performance_schema.setup_timers values("timer1", "XXXSECOND");`) }
func (p *testPerfSchemaSuit) TestConcurrentStatement(c *C) { defer testleak.AfterTest(c)() store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory + "/test_con_stmt") c.Assert(err, IsNil) defer store.Close() se := newSession(c, store, "test_con_stmt") mustExec(c, se, "drop table if exists test") mustExec(c, se, "create table test (a int, b int)") var wg sync.WaitGroup iFunc := func(a, b int) { defer wg.Done() sess := newSession(c, store, "test_con_stmt") mustExec(c, sess, fmt.Sprintf(`INSERT INTO test VALUES (%d, %d);`, a, b)) } sFunc := func() { defer wg.Done() sess := newSession(c, store, "test_con_stmt") mustQuery(c, sess, "select * from performance_schema.events_statements_current") mustQuery(c, sess, "select * from performance_schema.events_statements_history") } cnt := 10 for i := 0; i < cnt; i++ { wg.Add(2) go iFunc(i, i) go sFunc() } wg.Wait() }
func (ts *testNameResolverSuite) TestNameResolver(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) defer store.Close() testKit := testkit.NewTestKit(c, store) testKit.MustExec("use test") testKit.MustExec("create table t1 (c1 int, c2 int)") testKit.MustExec("create table t2 (c1 int, c2 int)") testKit.MustExec("create table t3 (c1 int, c2 int)") ctx := testKit.Se.(context.Context) domain := sessionctx.GetDomain(ctx) db.BindCurrentSchema(ctx, "test") for _, tc := range resolverTestCases { node, err := parser.ParseOneStmt(tc.src, "", "") c.Assert(err, IsNil) resolveErr := plan.ResolveName(node, domain.InfoSchema(), ctx) if tc.valid { c.Assert(resolveErr, IsNil) verifier := &resolverVerifier{c: c, src: tc.src} node.Accept(verifier) } else { c.Assert(resolveErr, NotNil, Commentf("%s", tc.src)) } } }
func (s *testDBSuite) SetUpSuite(c *C) { trySkipDBTest(c) var err error s.schemaName = "test_db" uri := "memory://test" s.store, err = tidb.NewStore(uri) c.Assert(err, IsNil) s.db, err = sql.Open("tidb", fmt.Sprintf("%s/%s", uri, s.schemaName)) c.Assert(err, IsNil) s.s, err = tidb.CreateSession(s.store) c.Assert(err, IsNil) s.mustExec(c, "create table t1 (c1 int, c2 int, c3 int, primary key(c1))") s.mustExec(c, "create table t2 (c1 int, c2 int, c3 int)") // set proper schema lease s.lease = 500 * time.Millisecond ctx := s.s.(context.Context) sessionctx.GetDomain(ctx).SetLease(s.lease) }
func (p *testFromSuit) SetUpSuite(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) p.vars = map[string]interface{}{} p.txn, _ = store.Begin() p.cols = []*column.Col{ { ColumnInfo: model.ColumnInfo{ ID: 0, Name: model.NewCIStr("id"), Offset: 0, DefaultValue: 0, FieldType: *types.NewFieldType(mysql.TypeLonglong), }, }, { ColumnInfo: model.ColumnInfo{ ID: 1, Name: model.NewCIStr("name"), Offset: 1, DefaultValue: nil, FieldType: *types.NewFieldType(mysql.TypeVarchar), }, }, } p.tbl = tables.NewTable(1, "t", "test", p.cols, &simpleAllocator{}) variable.BindSessionVars(p) var i int64 for i = 0; i < 10; i++ { p.tbl.AddRecord(p, []interface{}{i * 10, "hello"}) } }
func (s *testValidatorSuite) TestValidator(c *C) { cases := []struct { sql string inPrepare bool err error }{ {"select ?", false, parser.ErrSyntax}, {"select ?", true, nil}, {"create table t(id int not null auto_increment default 2, key (id))", true, errors.New("Invalid default value for 'id'")}, {"create table t(id int not null default 2 auto_increment, key (id))", true, errors.New("Invalid default value for 'id'")}, {"create table t(id int not null auto_increment)", true, errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")}, {"create table t(id int not null auto_increment, c int auto_increment, key (id, c))", true, errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")}, {"create table t(id int not null auto_increment, c int, key (c, id))", true, errors.New("Incorrect table definition; there can be only one auto column and it must be defined as a key")}, {"create table t(id decimal auto_increment, key (id))", true, errors.New("Incorrect column specifier for column 'id'")}, {"create table t(id float auto_increment, key (id))", true, nil}, } store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) se, err := tidb.CreateSession(store) c.Assert(err, IsNil) for _, ca := range cases { stmts, err1 := tidb.Parse(se.(context.Context), ca.sql) c.Assert(err1, IsNil) c.Assert(stmts, HasLen, 1) stmt := stmts[0] err = optimizer.Validate(stmt, ca.inPrepare) c.Assert(terror.ErrorEqual(err, ca.err), IsTrue) } }
func main() { tidb.RegisterLocalStore("boltdb", boltdb.Driver{}) tidb.RegisterStore("tikv", tikv.Driver{}) metric.RunMetric(3 * time.Second) printer.PrintTiDBInfo() runtime.GOMAXPROCS(runtime.NumCPU()) flag.Parse() if *lease < 0 { log.Fatalf("invalid lease seconds %d", *lease) } tidb.SetSchemaLease(time.Duration(*lease) * time.Second) cfg := &server.Config{ Addr: fmt.Sprintf(":%s", *port), LogLevel: *logLevel, StatusAddr: fmt.Sprintf(":%s", *statusPort), Socket: *socket, } log.SetLevelByString(cfg.LogLevel) store, err := tidb.NewStore(fmt.Sprintf("%s://%s", *store, *storePath)) if err != nil { log.Fatal(errors.ErrorStack(err)) } // Create a session to load information schema. se, err := tidb.CreateSession(store) if err != nil { log.Fatal(errors.ErrorStack(err)) } se.Close() var driver server.IDriver driver = server.NewTiDBDriver(store) var svr *server.Server svr, err = server.NewServer(cfg, driver) if err != nil { log.Fatal(errors.ErrorStack(err)) } sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) go func() { sig := <-sc log.Infof("Got signal [%d] to exit.", sig) svr.Close() os.Exit(0) }() log.Error(svr.Run()) }
func (p *testShowSuit) SetUpSuite(c *C) { var err error store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) p.vars = map[string]interface{}{} p.txn, _ = store.Begin() variable.BindSessionVars(p) }
func (p *testShowSuit) SetUpSuite(c *C) { var err error store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) p.ctx = mock.NewContext() p.txn, _ = store.Begin() variable.BindSessionVars(p.ctx) }
func createStore() kv.Storage { fullPath := fmt.Sprintf("%s://%s", *store, *storePath) store, err := tidb.NewStore(fullPath) if err != nil { log.Fatal(errors.ErrorStack(err)) } return store }
func (s *testDBSuite) TestUpdateMultipleTable(c *C) { defer testleak.AfterTest(c) store, err := tidb.NewStore("memory://update_multiple_table") c.Assert(err, IsNil) tk := testkit.NewTestKit(c, store) tk.MustExec("use test") tk.MustExec("create table t1 (c1 int, c2 int)") tk.MustExec("insert t1 values (1, 1), (2, 2)") tk.MustExec("create table t2 (c1 int, c2 int)") tk.MustExec("insert t2 values (1, 3), (2, 5)") ctx := tk.Se.(context.Context) domain := sessionctx.GetDomain(ctx) is := domain.InfoSchema() db, ok := is.SchemaByName(model.NewCIStr("test")) c.Assert(ok, IsTrue) t1Tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t1")) c.Assert(err, IsNil) t1Info := t1Tbl.Meta() // Add a new column in write only state. newColumn := &model.ColumnInfo{ ID: 100, Name: model.NewCIStr("c3"), Offset: 2, DefaultValue: 9, FieldType: *types.NewFieldType(mysql.TypeLonglong), State: model.StateWriteOnly, } t1Info.Columns = append(t1Info.Columns, newColumn) kv.RunInNewTxn(store, false, func(txn kv.Transaction) error { m := meta.NewMeta(txn) _, err = m.GenSchemaVersion() c.Assert(err, IsNil) c.Assert(m.UpdateTable(db.ID, t1Info), IsNil) return nil }) err = domain.Reload() c.Assert(err, IsNil) tk.MustExec("update t1, t2 set t1.c1 = 8, t2.c2 = 10 where t1.c2 = t2.c1") tk.MustQuery("select * from t1").Check(testkit.Rows("8 1", "8 2")) tk.MustQuery("select * from t2").Check(testkit.Rows("1 10", "2 10")) newColumn.State = model.StatePublic kv.RunInNewTxn(store, false, func(txn kv.Transaction) error { m := meta.NewMeta(txn) _, err = m.GenSchemaVersion() c.Assert(err, IsNil) c.Assert(m.UpdateTable(db.ID, t1Info), IsNil) return nil }) err = domain.Reload() c.Assert(err, IsNil) tk.MustQuery("select * from t1").Check(testkit.Rows("8 1 9", "8 2 9")) }
func (ts *testTypeInferrerSuite) TestInterType(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) defer store.Close() testKit := testkit.NewTestKit(c, store) testKit.MustExec("use test") testKit.MustExec("create table t (c1 int, c2 double, c3 text)") cases := []struct { expr string tp byte }{ {"c1", mysql.TypeLong}, {"+1", mysql.TypeLonglong}, {"-1", mysql.TypeLonglong}, {"-'1'", mysql.TypeDouble}, {"~1", mysql.TypeLonglong}, {"!true", mysql.TypeLonglong}, {"c1 is true", mysql.TypeLonglong}, {"c2 is null", mysql.TypeLonglong}, {"cast(1 as decimal)", mysql.TypeNewDecimal}, {"1 and 1", mysql.TypeLonglong}, {"1 or 1", mysql.TypeLonglong}, {"1 xor 1", mysql.TypeLonglong}, {"'1' & 2", mysql.TypeLonglong}, {"'1' | 2", mysql.TypeLonglong}, {"'1' ^ 2", mysql.TypeLonglong}, {"'1' << 1", mysql.TypeLonglong}, {"'1' >> 1", mysql.TypeLonglong}, {"1 + '1'", mysql.TypeDouble}, {"1 + 1.1", mysql.TypeNewDecimal}, {"1 div 2", mysql.TypeLonglong}, {"1 > any (select 1)", mysql.TypeLonglong}, {"exists (select 1)", mysql.TypeLonglong}, {"1 in (2, 3)", mysql.TypeLonglong}, {"'abc' like 'abc'", mysql.TypeLonglong}, {"'abc' rlike 'abc'", mysql.TypeLonglong}, {"(1+1)", mysql.TypeLonglong}, } for _, ca := range cases { ctx := testKit.Se.(context.Context) stmts, err := tidb.Parse(ctx, "select "+ca.expr+" from t") c.Assert(err, IsNil) c.Assert(stmts, HasLen, 1) stmt := stmts[0].(*ast.SelectStmt) is := sessionctx.GetDomain(ctx).InfoSchema() err = optimizer.ResolveName(stmt, is, ctx) c.Assert(err, IsNil) optimizer.InferType(stmt) tp := stmt.GetResultFields()[0].Column.Tp c.Assert(tp, Equals, ca.tp, Commentf("for %s", ca.expr)) } }
func (ts *testSuite) SetUpSuite(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) ts.store = store ts.charsetInfo = &ast.CharsetOpt{ Chs: "utf8", Col: "utf8_bin", } }
func (p *testIndexSuit) SetUpSuite(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) p.store = store se, _ := tidb.CreateSession(store) p.ctx = se.(context.Context) tbInfo := &model.TableInfo{ ID: 2, Name: model.NewCIStr("t2"), State: model.StatePublic, Columns: []*model.ColumnInfo{ { ID: 0, Name: model.NewCIStr("id"), Offset: 0, DefaultValue: 0, FieldType: *types.NewFieldType(mysql.TypeLonglong), State: model.StatePublic, }, { ID: 1, Name: model.NewCIStr("name"), Offset: 1, DefaultValue: nil, FieldType: *types.NewFieldType(mysql.TypeVarchar), State: model.StatePublic, }, }, Indices: []*model.IndexInfo{ { Name: model.NewCIStr("id"), Table: model.NewCIStr("t2"), Columns: []*model.IndexColumn{ { Name: model.NewCIStr("id"), Offset: 0, Length: 0, }, }, Unique: false, Primary: false, State: model.StatePublic, }, }, } p.tbl, err = tables.TableFromMeta(&simpleAllocator{}, tbInfo) c.Assert(err, IsNil) var i int64 for i = 0; i < 10; i++ { p.tbl.AddRecord(p.ctx, []interface{}{i * 10, "hello"}) } }
func (p *testIndexSuit) SetUpSuite(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) p.store = store se, _ := tidb.CreateSession(store) p.ctx = se.(context.Context) p.cols = []*column.Col{ { ColumnInfo: model.ColumnInfo{ ID: 0, Name: model.NewCIStr("id"), Offset: 0, DefaultValue: 0, FieldType: *types.NewFieldType(mysql.TypeLonglong), State: model.StatePublic, }, }, { ColumnInfo: model.ColumnInfo{ ID: 1, Name: model.NewCIStr("name"), Offset: 1, DefaultValue: nil, FieldType: *types.NewFieldType(mysql.TypeVarchar), State: model.StatePublic, }, }, } p.tbl = tables.NewTable(2, "t2", p.cols, &simpleAllocator{}) idxCol := &column.IndexedCol{ IndexInfo: model.IndexInfo{ Name: model.NewCIStr("id"), Table: model.NewCIStr("t2"), Columns: []*model.IndexColumn{ { Name: model.NewCIStr("id"), Offset: 0, Length: 0, }, }, Unique: false, Primary: false, State: model.StatePublic, }, X: kv.NewKVIndex("i", "id", 0, false), } p.tbl.AddIndex(idxCol) var i int64 for i = 0; i < 10; i++ { p.tbl.AddRecord(p.ctx, []interface{}{i * 10, "hello"}, 0) } }
func (p *testIndexSuit) SetUpSuite(c *C) { store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) p.store = store p.vars = map[string]interface{}{} p.txn, _ = p.store.Begin() p.cols = []*column.Col{ { ColumnInfo: model.ColumnInfo{ ID: 0, Name: model.NewCIStr("id"), Offset: 0, DefaultValue: 0, FieldType: *types.NewFieldType(mysql.TypeLonglong), }, }, { ColumnInfo: model.ColumnInfo{ ID: 1, Name: model.NewCIStr("name"), Offset: 1, DefaultValue: nil, FieldType: *types.NewFieldType(mysql.TypeVarchar), }, }, } p.tbl = tables.NewTable(2, "t2", "test", p.cols, &simpleAllocator{}) idxCol := &column.IndexedCol{ IndexInfo: model.IndexInfo{ Name: model.NewCIStr("id"), Table: model.NewCIStr("t2"), Columns: []*model.IndexColumn{ { Name: model.NewCIStr("id"), Offset: 0, Length: 0, }, }, Unique: false, Primary: false, }, X: kv.NewKVIndex("i", "id", false), } p.tbl.AddIndex(idxCol) variable.BindSessionVars(p) var i int64 for i = 0; i < 10; i++ { p.tbl.AddRecord(p, []interface{}{i * 10, "hello"}) } }
func (t *testIsolationSuite) TestMultiInc(c *C) { store, err := tidb.NewStore("memory://test/test_isolation") c.Assert(err, IsNil) defer store.Close() threadCnt := 4 incCnt := 100 keyCnt := 4 keys := make([][]byte, 0, keyCnt) for i := 0; i < keyCnt; i++ { keys = append(keys, []byte(fmt.Sprintf("test_key_%d", i))) } var wg sync.WaitGroup wg.Add(threadCnt) for i := 0; i < threadCnt; i++ { go func() { defer wg.Done() for j := 0; j < incCnt; j++ { err1 := kv.RunInNewTxn(store, true, func(txn kv.Transaction) error { for _, key := range keys { _, err2 := kv.IncInt64(txn, key, 1) if err2 != nil { return err2 } } return nil }) c.Assert(err1, IsNil) } }() } wg.Wait() for i := 0; i < keyCnt; i++ { err = kv.RunInNewTxn(store, false, func(txn kv.Transaction) error { for _, key := range keys { id, err1 := kv.GetInt64(txn, key) if err1 != nil { return err1 } c.Assert(id, Equals, int64(threadCnt*incCnt)) } return nil }) c.Assert(err, IsNil) } }
func (p *testPerfSchemaSuit) TestInstrument(c *C) { defer testleak.AfterTest(c)() store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory + "/test_instrument_db") c.Assert(err, IsNil) defer store.Close() se := newSession(c, store, "test_instrument_db") defer se.Close() cnt := mustQuery(c, se, "select * from performance_schema.setup_instruments") c.Assert(cnt, Greater, 0) mustExec(c, se, "drop database test_instrument_db") }
func (ts *TidbTestSuite) SetUpSuite(c *C) { store, err := tidb.NewStore("memory:///tmp/tidb") c.Assert(err, IsNil) ts.tidbdrv = NewTiDBDriver(store) cfg := &Config{ Addr: ":4001", LogLevel: "debug", } server, err := NewServer(cfg, ts.tidbdrv) c.Assert(err, IsNil) ts.server = server go ts.server.Run() time.Sleep(time.Millisecond * 100) }
func (s *testDBSuite) TestTruncateTable(c *C) { defer testleak.AfterTest(c) store, err := tidb.NewStore("memory://truncate_table") c.Assert(err, IsNil) tk := testkit.NewTestKit(c, store) tk.MustExec("use test") tk.MustExec("create table t (c1 int, c2 int)") tk.MustExec("insert t values (1, 1), (2, 2)") ctx := tk.Se.(context.Context) is := sessionctx.GetDomain(ctx).InfoSchema() oldTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) c.Assert(err, IsNil) oldTblID := oldTblInfo.Meta().ID tk.MustExec("truncate table t") tk.MustExec("insert t values (3, 3), (4, 4)") tk.MustQuery("select * from t").Check(testkit.Rows("3 3", "4 4")) is = sessionctx.GetDomain(ctx).InfoSchema() newTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) c.Assert(err, IsNil) c.Assert(newTblInfo.Meta().ID, Greater, oldTblID) // verify that the old table data has been deleted by background worker. tablePrefix := tablecodec.EncodeTablePrefix(oldTblID) hasOldTableData := true for i := 0; i < 30; i++ { err = kv.RunInNewTxn(store, false, func(txn kv.Transaction) error { it, err1 := txn.Seek(tablePrefix) if err1 != nil { return err1 } if !it.Valid() { hasOldTableData = false } else { hasOldTableData = it.Key().HasPrefix(tablePrefix) } it.Close() return nil }) c.Assert(err, IsNil) if !hasOldTableData { break } time.Sleep(time.Millisecond * 100) } c.Assert(hasOldTableData, IsFalse) }
func (s *testTypeInferrerSuite) TestColumnInfoModified(c *C) { defer testleak.AfterTest(c)() store, err := tidb.NewStore(tidb.EngineGoLevelDBMemory) c.Assert(err, IsNil) defer store.Close() testKit := testkit.NewTestKit(c, store) testKit.MustExec("use test") testKit.MustExec("drop table if exists tab0") testKit.MustExec("CREATE TABLE tab0(col0 INTEGER, col1 INTEGER, col2 INTEGER)") testKit.MustExec("SELECT + - (- CASE + col0 WHEN + CAST( col0 AS SIGNED ) THEN col1 WHEN 79 THEN NULL WHEN + - col1 THEN col0 / + col0 END ) * - 16 FROM tab0") ctx := testKit.Se.(context.Context) is := sessionctx.GetDomain(ctx).InfoSchema() col, _ := is.ColumnByName(model.NewCIStr("test"), model.NewCIStr("tab0"), model.NewCIStr("col1")) c.Assert(col.Tp, Equals, mysql.TypeLong) }
func (s *testRelaySuite) TestRelay(c *C) { s.mustExec(c, "create table t (c1 int, c2 int, primary key(c1))") s.mustExec(c, "insert into t values (1, 1), (2, 2), (3, 3)") s.mustQuery(c, "select * from t") s.mustQuery(c, "select * from t where c1 = 1") stmt, err := s.db.Prepare("select * from t where c1 = ?") c.Assert(err, IsNil) r, err := stmt.Query(1) c.Assert(err, IsNil) s.discardRows(c, r) r, err = stmt.Query(2) c.Assert(err, IsNil) s.discardRows(c, r) r, err = stmt.Query(10) c.Assert(err, IsNil) s.discardRows(c, r) stmt.Close() var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func(i int) { defer wg.Done() s.mustExec(c, "insert into t values (?, ?)", i*10, i*10) }(i) } wg.Wait() s.mustExec(c, "insert into t values (4, 4)") s.server.Close() s.server = nil time.Sleep(1 * time.Second) store, err := tidb.NewStore("memory://test_replay/test_replay") c.Assert(err, IsNil) defer store.Close() replayer, err := NewReplayer(NewTiDBDriver(store), s.path, false) c.Assert(err, IsNil) replayer.OnRecordRead = func(rec *Record) { //log.Errorf("record %s", rec) } err = replayer.Run() c.Assert(err, IsNil) }
func main() { metric.RunMetric(3 * time.Second) printer.PrintTiDBInfo() runtime.GOMAXPROCS(runtime.NumCPU()) flag.Parse() if *lease <= 0 { log.Fatalf("invalid lease seconds %d", *lease) } tidb.SetSchemaLease(time.Duration(*lease) * time.Second) cfg := &server.Config{ Addr: fmt.Sprintf(":%s", *port), LogLevel: *logLevel, } log.SetLevelByString(cfg.LogLevel) store, err := tidb.NewStore(fmt.Sprintf("%s://%s", *store, *storePath)) if err != nil { log.Fatal(err) } var driver server.IDriver driver = server.NewTiDBDriver(store) var svr *server.Server svr, err = server.NewServer(cfg, driver) if err != nil { log.Fatal(err) } sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) go func() { sig := <-sc log.Infof("Got signal [%d] to exit.", sig) svr.Close() os.Exit(0) }() log.Error(svr.Run()) }
func (ts *TidbTestSuite) SetUpSuite(c *C) { store, err := tidb.NewStore("goleveldb:///tmp/tidb") c.Assert(err, IsNil) CreateTiDBTestDatabase(store) ts.tidbdrv = NewTiDBDriver(store) cfg := &Config{ Addr: ":4000", User: "******", Password: "", LogLevel: "debug", } server, err := NewServer(cfg, ts.tidbdrv) c.Assert(err, IsNil) ts.server = server go ts.server.Run() time.Sleep(time.Millisecond * 100) }
func (ts *TidbTestSuite) SetUpSuite(c *C) { log.SetLevelByString("error") store, err := tidb.NewStore("memory:///tmp/tidb") c.Assert(err, IsNil) ts.tidbdrv = NewTiDBDriver(store) cfg := &Config{ Addr: ":4001", LogLevel: "debug", StatusAddr: ":10090", ReportStatus: true, } server, err := NewServer(cfg, ts.tidbdrv) c.Assert(err, IsNil) ts.server = server go ts.server.Run() time.Sleep(time.Millisecond * 100) }