Skip to content

Commit

Permalink
*: suport alter table set flash replica syntax (pingcap#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and lzmhhh123 committed Jan 17, 2020
1 parent bcc8f7a commit 7166a7b
Show file tree
Hide file tree
Showing 8 changed files with 7,106 additions and 6,995 deletions.
22 changes: 20 additions & 2 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1616,8 +1616,7 @@ const (
AlterTablePartition
AlterTableEnableKeys
AlterTableDisableKeys

// TODO: Add more actions
AlterTableSetTiFlashReplica
)

// LockType is the type for AlterTableSpec.
Expand Down Expand Up @@ -1696,11 +1695,30 @@ type AlterTableSpec struct {
PartitionNames []model.CIStr
PartDefinitions []*PartitionDefinition
Num uint64
TiFlashReplica *TiFlashReplicaSpec
}

type TiFlashReplicaSpec struct {
Count uint64
Labels []string
}

// Restore implements Node interface.
func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
switch n.Tp {
case AlterTableSetTiFlashReplica:
ctx.WriteKeyWord("SET TIFLASH REPLICA ")
ctx.WritePlainf("%d", n.TiFlashReplica.Count)
if len(n.TiFlashReplica.Labels) == 0 {
break
}
ctx.WriteKeyWord(" LOCATION LABELS ")
for i, v := range n.TiFlashReplica.Labels {
if i > 0 {
ctx.WritePlain(", ")
}
ctx.WriteString(v)
}
case AlterTableOption:
switch {
case len(n.Options) == 2 &&
Expand Down
2 changes: 2 additions & 0 deletions go.mod1
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ require (
github.com/sirupsen/logrus v1.3.0
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2
)

go 1.13
3 changes: 3 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ var tokenMap = map[string]int{
"KEY_BLOCK_SIZE": keyBlockSize,
"KEYS": keys,
"KILL": kill,
"LABELS": labels,
"LAST": last,
"LEADING": leading,
"LEFT": left,
Expand All @@ -353,6 +354,7 @@ var tokenMap = map[string]int{
"LOCAL": local,
"LOCALTIME": localTime,
"LOCALTIMESTAMP": localTs,
"LOCATION": location,
"LOCK": lock,
"LONG": long,
"LONGBLOB": longblobType,
Expand Down Expand Up @@ -456,6 +458,7 @@ var tokenMap = map[string]int{
"REPEATABLE": repeatable,
"REPLACE": replace,
"RESPECT": respect,
"REPLICA": replica,
"REPLICATION": replication,
"REQUIRE": require,
"RESTRICT": restrict,
Expand Down
4 changes: 4 additions & 0 deletions model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const (
ActionModifySchemaCharsetAndCollate ActionType = 26
ActionLockTable ActionType = 27
ActionUnlockTable ActionType = 28
ActionSetTiFlashReplica ActionType = 29
ActionUpdateTiFlashReplicaStatus ActionType = 30
)

// AddIndexStr is a string related to the operation of "add index".
Expand Down Expand Up @@ -92,6 +94,8 @@ var actionMap = map[ActionType]string{
ActionModifySchemaCharsetAndCollate: "modify schema charset and collate",
ActionLockTable: "lock table",
ActionUnlockTable: "unlock table",
ActionSetTiFlashReplica: "set tiflash replica",
ActionUpdateTiFlashReplicaStatus: "update tiflash replica status",
}

// String return current ddl action in string
Expand Down
10 changes: 10 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ type TableInfo struct {

// Version means the version of the table info.
Version uint16 `json:"version"`

// TiFlashReplica means the TiFlash replica info.
TiFlashReplica *TiFlashReplicaInfo `json:"tiflash_replica"`
}

// TableLockInfo provides meta data describing a table lock.
Expand Down Expand Up @@ -335,6 +338,13 @@ func (t TableLockType) String() string {
return ""
}

// TiFlashReplicaInfo means the flash replica info.
type TiFlashReplicaInfo struct {
Count uint64
LocationLabels []string
Available bool
}

// GetPartitionInfo returns the partition information.
func (t *TableInfo) GetPartitionInfo() *PartitionInfo {
if t.Partition != nil && t.Partition.Enable {
Expand Down
Loading

0 comments on commit 7166a7b

Please sign in to comment.