From b8d7ac5969a5f4d99100b552dba7b2320ea5f814 Mon Sep 17 00:00:00 2001 From: tangenta Date: Sat, 6 Jun 2020 21:56:43 +0800 Subject: [PATCH 1/2] cherry pick #17714 to release-4.0 Signed-off-by: sre-bot --- ddl/ddl_api.go | 21 ++++++++++++++------- executor/ddl_test.go | 6 ++++++ meta/autoid/errors.go | 9 +++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 71b3b62c4f97c..0e9894f48cf78 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2209,20 +2209,27 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6 if err != nil { return errors.Trace(err) } - autoIncID, err := t.Allocators(ctx).Get(tp).NextGlobalAutoID(t.Meta().ID) + var actionType model.ActionType + switch tp { + case autoid.AutoRandomType: + if t.Meta().AutoRandomBits == 0 { + return errors.Trace(ErrInvalidAutoRandom.GenWithStackByArgs(autoid.AutoRandomRebaseNotApplicable)) + } + actionType = model.ActionRebaseAutoRandomBase + case autoid.RowIDAllocType: + actionType = model.ActionRebaseAutoID + } + + autoID, err := t.Allocators(ctx).Get(tp).NextGlobalAutoID(t.Meta().ID) if err != nil { return errors.Trace(err) } - // If newBase < autoIncID, we need to do a rebase before returning. + // If newBase < autoID, we need to do a rebase before returning. // Assume there are 2 TiDB servers: TiDB-A with allocator range of 0 ~ 30000; TiDB-B with allocator range of 30001 ~ 60000. // If the user sends SQL `alter table t1 auto_increment = 100` to TiDB-B, // and TiDB-B finds 100 < 30001 but returns without any handling, // then TiDB-A may still allocate 99 for auto_increment column. This doesn't make sense for the user. - newBase = mathutil.MaxInt64(newBase, autoIncID) - actionType := model.ActionRebaseAutoID - if tp == autoid.AutoRandomType { - actionType = model.ActionRebaseAutoRandomBase - } + newBase = mathutil.MaxInt64(newBase, autoID) job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 476d4017ec004..f7403b71fd56e 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -938,6 +938,12 @@ func (s *testAutoRandomSuite) TestAutoRandomTableOption(c *C) { c.Assert(orderedHandles[i], Equals, i+3000000) } tk.MustExec("drop table alter_table_auto_random_option") + + // Alter auto_random_base on non auto_random table. + tk.MustExec("create table alter_auto_random_normal (a int)") + _, err = tk.Exec("alter table alter_auto_random_normal auto_random_base = 100") + c.Assert(err, NotNil) + c.Assert(strings.Contains(err.Error(), autoid.AutoRandomRebaseNotApplicable), IsTrue, Commentf(err.Error())) } // Test filter different kind of allocators. diff --git a/meta/autoid/errors.go b/meta/autoid/errors.go index 68d7b4850417c..fc5907acd0689 100644 --- a/meta/autoid/errors.go +++ b/meta/autoid/errors.go @@ -47,4 +47,13 @@ const ( AutoRandomNonPositive = "the value of auto_random should be positive" // AutoRandomAvailableAllocTimesNote is reported when a table containing auto_random is created. AutoRandomAvailableAllocTimesNote = "Available implicit allocation times: %d" +<<<<<<< HEAD +======= + // AutoRandomExplicitInsertDisabledErrMsg is reported when auto_random column value is explicitly specified, but the session var 'allow_auto_random_explicit_insert' is false. + AutoRandomExplicitInsertDisabledErrMsg = "Explicit insertion on auto_random column is disabled. Try to set @@allow_auto_random_explicit_insert = true." + // AutoRandomOnNonBigIntColumn is reported when define auto random to non bigint column + AutoRandomOnNonBigIntColumn = "auto_random option must be defined on `bigint` column, but not on `%s` column" + // AutoRandomRebaseNotApplicable is reported when alter auto_random base on a non auto_random table. + AutoRandomRebaseNotApplicable = "alter auto_random_base of a non auto_random table" +>>>>>>> a4eb75a... ddl: reject alter auto_random_base on a non auto_random table (#17714) ) From a8ca78cd39d7397f4d229c0b1d8774233739f70b Mon Sep 17 00:00:00 2001 From: tangenta Date: Sun, 7 Jun 2020 23:45:57 +0800 Subject: [PATCH 2/2] resolve conflicts --- meta/autoid/errors.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/meta/autoid/errors.go b/meta/autoid/errors.go index fc5907acd0689..86f2b3cb4b44b 100644 --- a/meta/autoid/errors.go +++ b/meta/autoid/errors.go @@ -47,13 +47,10 @@ const ( AutoRandomNonPositive = "the value of auto_random should be positive" // AutoRandomAvailableAllocTimesNote is reported when a table containing auto_random is created. AutoRandomAvailableAllocTimesNote = "Available implicit allocation times: %d" -<<<<<<< HEAD -======= // AutoRandomExplicitInsertDisabledErrMsg is reported when auto_random column value is explicitly specified, but the session var 'allow_auto_random_explicit_insert' is false. AutoRandomExplicitInsertDisabledErrMsg = "Explicit insertion on auto_random column is disabled. Try to set @@allow_auto_random_explicit_insert = true." // AutoRandomOnNonBigIntColumn is reported when define auto random to non bigint column AutoRandomOnNonBigIntColumn = "auto_random option must be defined on `bigint` column, but not on `%s` column" // AutoRandomRebaseNotApplicable is reported when alter auto_random base on a non auto_random table. AutoRandomRebaseNotApplicable = "alter auto_random_base of a non auto_random table" ->>>>>>> a4eb75a... ddl: reject alter auto_random_base on a non auto_random table (#17714) )