Skip to content

Commit

Permalink
cherry pick pingcap#15012 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: sre-bot <sre-bot@pingcap.com>
  • Loading branch information
zimulala authored and sre-bot committed Mar 3, 2020
1 parent 700d9de commit 3a8d508
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const (
MaxLogFileSize = 4096 // MB
// DefTxnTotalSizeLimit is the default value of TxnTxnTotalSizeLimit.
DefTxnTotalSizeLimit = 100 * 1024 * 1024
// DefMaxIndexLength is the maximum index length(in bytes). This value is consistent with MySQL.
DefMaxIndexLength = 3072
// DefMaxOfMaxIndexLength is the maximum index length(in bytes) for TiDB v3.0.7 and previous version.
DefMaxOfMaxIndexLength = 3072 * 4
)

// Valid config maps
Expand Down Expand Up @@ -89,6 +93,7 @@ type Config struct {
Plugin Plugin `toml:"plugin" json:"plugin"`
PessimisticTxn PessimisticTxn `toml:"pessimistic-txn" json:"pessimistic-txn"`
CheckMb4ValueInUTF8 bool `toml:"check-mb4-value-in-utf8" json:"check-mb4-value-in-utf8"`
MaxIndexLength int `toml:"max-index-length" json:"max-index-length"`
// AlterPrimaryKey is used to control alter primary key feature.
AlterPrimaryKey bool `toml:"alter-primary-key" json:"alter-primary-key"`
// TreatOldVersionUTF8AsUTF8MB4 is use to treat old version table/column UTF8 charset as UTF8MB4. This is for compatibility.
Expand Down Expand Up @@ -503,6 +508,7 @@ var defaultConf = Config{
EnableStreaming: false,
EnableBatchDML: false,
CheckMb4ValueInUTF8: true,
MaxIndexLength: 3072,
AlterPrimaryKey: false,
TreatOldVersionUTF8AsUTF8MB4: true,
EnableTableLock: false,
Expand Down Expand Up @@ -753,6 +759,9 @@ func (c *Config) Valid() error {
if c.Store == "mocktikv" && !c.RunDDL {
return fmt.Errorf("can't disable DDL on mocktikv")
}
if c.MaxIndexLength < DefMaxIndexLength || c.MaxIndexLength > DefMaxOfMaxIndexLength {
return fmt.Errorf("max-index-length should be [%d, %d]", DefMaxIndexLength, DefMaxOfMaxIndexLength)
}
if c.Log.File.MaxSize > MaxLogFileSize {
return fmt.Errorf("invalid max log file size=%v which is larger than max=%v", c.Log.File.MaxSize, MaxLogFileSize)
}
Expand Down
3 changes: 3 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ check-mb4-value-in-utf8 = true
# treat-old-version-utf8-as-utf8mb4 use for upgrade compatibility. Set to true will treat old version table/column UTF8 charset as UTF8MB4.
treat-old-version-utf8-as-utf8mb4 = true

# max-index-length is used to deal with compatibility issues from v3.0.7 and previous version upgrades. It can only be in [3072, 3072*4].
max-index-length = 3072

# enable-table-lock is used to control table lock feature. Default is false, indicate the table lock feature is disabled.
enable-table-lock = false

Expand Down
30 changes: 30 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ enable-batch-dml = true
server-version = "test_version"
repair-mode = true
max-server-connections = 200
<<<<<<< HEAD
=======
mem-quota-query = 10000
max-index-length = 3080
>>>>>>> 1771cf8... ddl, config: add a variable to control the max index length (#15012)
[performance]
txn-total-size-limit=2000
[tikv-client]
Expand Down Expand Up @@ -236,6 +241,19 @@ engines = ["tiflash"]
c.Assert(conf.MaxServerConnections, Equals, uint32(200))
c.Assert(conf.Experimental.AllowAutoRandom, IsTrue)
c.Assert(conf.IsolationRead.Engines, DeepEquals, []string{"tiflash"})
<<<<<<< HEAD
=======
c.Assert(conf.MaxIndexLength, Equals, 3080)

_, err = f.WriteString(`
[log.file]
log-rotate = true`)
c.Assert(err, IsNil)
err = conf.Load(configFile)
tmp := err.(*ErrConfigValidationFailed)
c.Assert(isAllDeprecatedConfigItems(tmp.UndecodedItems), IsTrue)

>>>>>>> 1771cf8... ddl, config: add a variable to control the max index length (#15012)
c.Assert(f.Close(), IsNil)
c.Assert(os.Remove(configFile), IsNil)

Expand Down Expand Up @@ -385,6 +403,18 @@ func (s *testConfigSuite) TestAllowAutoRandomValid(c *C) {
checkValid(false, false, true)
}

func (s *testConfigSuite) TestMaxIndexLength(c *C) {
conf := NewConfig()
checkValid := func(indexLen int, shouldBeValid bool) {
conf.MaxIndexLength = indexLen
c.Assert(conf.Valid() == nil, Equals, shouldBeValid)
}
checkValid(DefMaxIndexLength, true)
checkValid(DefMaxIndexLength-1, false)
checkValid(DefMaxOfMaxIndexLength, true)
checkValid(DefMaxOfMaxIndexLength+1, false)
}

func (s *testConfigSuite) TestParsePath(c *C) {
etcdAddrs, disableGC, err := ParsePath("tikv://node1:2379,node2:2379")
c.Assert(err, IsNil)
Expand Down
3 changes: 2 additions & 1 deletion ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
pumpcli "github.com/pingcap/tidb-tools/tidb-binlog/pump_client"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -99,7 +100,7 @@ var (
errKeyPart0 = terror.ClassDDL.New(mysql.ErrKeyPart0, mysql.MySQLErrName[mysql.ErrKeyPart0])
errIncorrectPrefixKey = terror.ClassDDL.New(mysql.ErrWrongSubKey, mysql.MySQLErrName[mysql.ErrWrongSubKey])
errTooLongKey = terror.ClassDDL.New(mysql.ErrTooLongKey,
fmt.Sprintf(mysql.MySQLErrName[mysql.ErrTooLongKey], maxPrefixLength))
fmt.Sprintf(mysql.MySQLErrName[mysql.ErrTooLongKey], config.GetGlobalConfig().MaxIndexLength))
errKeyColumnDoesNotExits = terror.ClassDDL.New(mysql.ErrKeyColumnDoesNotExits, mysql.MySQLErrName[mysql.ErrKeyColumnDoesNotExits])
errUnknownTypeLength = terror.ClassDDL.New(mysql.ErrUnknownTypeLength, mysql.MySQLErrName[mysql.ErrUnknownTypeLength])
errUnknownFractionLength = terror.ClassDDL.New(mysql.ErrUnknownFractionLength, mysql.MySQLErrName[mysql.ErrUnknownFractionLength])
Expand Down
8 changes: 4 additions & 4 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/config"
ddlutil "github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
Expand All @@ -49,7 +50,6 @@ import (
)

const (
maxPrefixLength = 3072
// MaxCommentLength is exported for testing.
MaxCommentLength = 1024
)
Expand Down Expand Up @@ -78,7 +78,7 @@ func buildIndexColumns(columns []*model.ColumnInfo, indexPartSpecifications []*a
sumLength += indexColumnLength

// The sum of all lengths must be shorter than the max length for prefix.
if sumLength > maxPrefixLength {
if sumLength > config.GetGlobalConfig().MaxIndexLength {
return nil, errors.Trace(errTooLongKey)
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func checkIndexPrefixLength(columns []*model.ColumnInfo, idxColumns []*model.Ind
}
sumLength += indexColumnLength
// The sum of all lengths must be shorter than the max length for prefix.
if sumLength > maxPrefixLength {
if sumLength > config.GetGlobalConfig().MaxIndexLength {
return errors.Trace(errTooLongKey)
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func checkIndexColumn(col *model.ColumnInfo, ic *ast.IndexPartSpecification) err
}

// Specified length must be shorter than the max length for prefix.
if ic.Length > maxPrefixLength {
if ic.Length > config.GetGlobalConfig().MaxIndexLength {
return errors.Trace(errTooLongKey)
}
return nil
Expand Down
18 changes: 18 additions & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ func (s *testSerialSuite) TearDownSuite(c *C) {
}
}

func (s *testSerialSuite) TestChangeMaxIndexLength(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
cfg := config.GetGlobalConfig()
newCfg := *cfg
originalMaxIndexLen := cfg.MaxIndexLength
newCfg.MaxIndexLength = config.DefMaxOfMaxIndexLength
config.StoreGlobalConfig(&newCfg)
defer func() {
newCfg.MaxIndexLength = originalMaxIndexLen
config.StoreGlobalConfig(&newCfg)
}()

tk.MustExec("create table t (c1 varchar(3073), index(c1)) charset = ascii;")
tk.MustExec(fmt.Sprintf("create table t1 (c1 varchar(%d), index(c1)) charset = ascii;", config.DefMaxOfMaxIndexLength))
tk.MustGetErrCode(fmt.Sprintf("create table t2 (c1 varchar(%d), index(c1)) charset = ascii;", config.DefMaxOfMaxIndexLength+1), mysql.ErrTooLongKey)
tk.MustExec("drop table t, t1")
}

func (s *testSerialSuite) TestPrimaryKey(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down

0 comments on commit 3a8d508

Please sign in to comment.