Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao committed Nov 7, 2022
1 parent 3b281c3 commit 3685f4f
Show file tree
Hide file tree
Showing 7 changed files with 787 additions and 716 deletions.
35 changes: 17 additions & 18 deletions autoid_service/autoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,25 +396,24 @@ func (s *Service) allocAutoID(ctx context.Context, req *autoid.AutoIDRequest) (*
Min: val.base,
Max: val.base,
}, nil
} else {
// This item is not initialized, get the data from remote.
var currentEnd int64
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta)
err := kv.RunInNewTxn(ctx, s.store, true, func(ctx context.Context, txn kv.Transaction) error {
idAcc := meta.NewMeta(txn).GetAutoIDAccessors(req.DbID, req.TblID).RowID()
var err1 error
currentEnd, err1 = idAcc.Get()
if err1 != nil {
return err1
}
val.end = currentEnd
return nil
})
return &autoid.AutoIDResponse{
Min: currentEnd,
Max: currentEnd,
}, err
}
// This item is not initialized, get the data from remote.
var currentEnd int64
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta)
err := kv.RunInNewTxn(ctx, s.store, true, func(ctx context.Context, txn kv.Transaction) error {
idAcc := meta.NewMeta(txn).GetAutoIDAccessors(req.DbID, req.TblID).RowID()
var err1 error
currentEnd, err1 = idAcc.Get()
if err1 != nil {
return err1
}
val.end = currentEnd
return nil
})
return &autoid.AutoIDResponse{
Min: currentEnd,
Max: currentEnd,
}, err
}

val.Lock()
Expand Down
60 changes: 0 additions & 60 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2750,66 +2750,6 @@ func TestCreateTableWithAutoIdCache(t *testing.T) {
tk.MustGetErrMsg("alter table t auto_id_cache = 9223372036854775808", "table option auto_id_cache overflows int64")
}

func TestRenameTableForAutoIncrement(t *testing.T) {
store, _ := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test;")
tk.MustExec("drop table if exists t1, t2, t3;")
tk.MustExec("create table t1 (id int key auto_increment);")
tk.MustExec("insert into t1 values ()")
tk.MustExec("rename table t1 to t11")
tk.MustExec("insert into t11 values ()")
// TODO(tiancaiamao): fix bug and uncomment here, rename table should not discard the cached AUTO_ID.
// tk.MustQuery("select * from t11").Check(testkit.Rows("1", "2"))

// auto_id_cache 1 use another implementation and do not have such bug.
tk.MustExec("create table t2 (id int key auto_increment) auto_id_cache 1;")
tk.MustExec("insert into t2 values ()")
tk.MustExec("rename table t2 to t22")
tk.MustExec("insert into t22 values ()")
tk.MustQuery("select * from t22").Check(testkit.Rows("1", "2"))

tk.MustExec("create table t3 (id int key auto_increment) auto_id_cache 100;")
tk.MustExec("insert into t3 values ()")
tk.MustExec("rename table t3 to t33")
tk.MustExec("insert into t33 values ()")
// TODO(tiancaiamao): fix bug and uncomment here, rename table should not discard the cached AUTO_ID.
// tk.MustQuery("select * from t33").Check(testkit.Rows("1", "2"))
}

func TestAlterTableAutoIDCache(t *testing.T) {
store, _ := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("USE test;")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (id int key auto_increment)")
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("1"))
tk.MustQuery("show table t next_row_id").Check(testkit.Rows("test t id 2000001 AUTO_INCREMENT"))

tk.MustExec("alter table t auto_id_cache = 100")
tk.MustQuery("show table t next_row_id").Check(testkit.Rows("test t id 2000001 AUTO_INCREMENT"))
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("1", "2000001"))
tk.MustQuery("show table t next_row_id").Check(testkit.Rows("test t id 2000101 AUTO_INCREMENT"))

// Note that auto_id_cache=1 use a different implementation.
tk.MustExec("alter table t auto_id_cache = 1")
tk.MustQuery("show table t next_row_id").Check(testkit.Rows("test t id 2000101 AUTO_INCREMENT"))
tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("1", "2000001", "2000101"))
tk.MustQuery("show table t next_row_id").Check(testkit.Rows("test t id 2000102 AUTO_INCREMENT"))

// alter table from auto_id_cache=1 to default will discard the IDs cached by the autoid service.
// This is because they are two component and TiDB can't tell the autoid service to "save position and exit".
tk.MustExec("alter table t auto_id_cache = 20000")
tk.MustQuery("show table t next_row_id").Check(testkit.Rows("test t id 2004101 AUTO_INCREMENT"))

tk.MustExec("insert into t values ()")
tk.MustQuery("select * from t").Check(testkit.Rows("1", "2000001", "2000101", "2004101"))
tk.MustQuery("show table t next_row_id").Check(testkit.Rows("test t id 2024101 AUTO_INCREMENT"))
}

func TestAlterIndexVisibility(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down

0 comments on commit 3685f4f

Please sign in to comment.