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 ef54f97 commit 0dfe81a
Show file tree
Hide file tree
Showing 8 changed files with 7,194 additions and 6,972 deletions.
41 changes: 41 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,9 @@ const (
AlterTableDisableKeys

// TODO: Add more actions
AlterTableOrderByColumns
// AlterTableSetTiFlashReplica uses to set the table TiFlash replica.
AlterTableSetTiFlashReplica
)

// LockType is the type for AlterTableSpec.
Expand Down Expand Up @@ -1596,11 +1599,49 @@ type AlterTableSpec struct {
PartitionNames []model.CIStr
PartDefinitions []*PartitionDefinition
Num uint64
Visibility IndexVisibility
TiFlashReplica *TiFlashReplicaSpec
}

type TiFlashReplicaSpec struct {
Count uint64
Labels []string
}

// AlterOrderItem represents an item in order by at alter table stmt.
type AlterOrderItem struct {
node
Column *ColumnName
Desc bool
}

// Restore implements Node interface.
func (n *AlterOrderItem) Restore(ctx *RestoreCtx) error {
if err := n.Column.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterOrderItem.Column")
}
if n.Desc {
ctx.WriteKeyWord(" DESC")
}
return nil
}

// 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
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
10 changes: 10 additions & 0 deletions model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const (
ActionDropView ActionType = 24
ActionRecoverTable ActionType = 25
ActionModifySchemaCharsetAndCollate ActionType = 26
ActionLockTable ActionType = 27
ActionUnlockTable ActionType = 28
ActionRepairTable ActionType = 29
ActionSetTiFlashReplica ActionType = 30
ActionUpdateTiFlashReplicaStatus ActionType = 31
)

// AddIndexStr is a string related to the operation of "add index".
Expand Down Expand Up @@ -88,6 +93,11 @@ var actionMap = map[ActionType]string{
ActionDropView: "drop view",
ActionRecoverTable: "recover table",
ActionModifySchemaCharsetAndCollate: "modify schema charset and collate",
ActionLockTable: "lock table",
ActionUnlockTable: "unlock table",
ActionRepairTable: "repair table",
ActionSetTiFlashReplica: "set tiflash replica",
ActionUpdateTiFlashReplicaStatus: "update tiflash replica status",
}

// String return current ddl action in string
Expand Down
95 changes: 95 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,101 @@ 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.
type TableLockInfo struct {
Tp TableLockType
// Use array because there may be multiple sessions holding the same read lock.
Sessions []SessionInfo
State TableLockState
// TS is used to record the timestamp this table lock been locked.
TS uint64
}

// SessionInfo contain the session ID and the server ID.
type SessionInfo struct {
ServerID string
SessionID uint64
}

func (s SessionInfo) String() string {
return "server: " + s.ServerID + "_session: " + strconv.FormatUint(s.SessionID, 10)
}

// TableLockTpInfo is composed by schema ID, table ID and table lock type.
type TableLockTpInfo struct {
SchemaID int64
TableID int64
Tp TableLockType
}

// TableLockState is the state for table lock.
type TableLockState byte

const (
// TableLockStateNone means this table lock is absent.
TableLockStateNone TableLockState = iota
// TableLockStatePreLock means this table lock is pre-lock state. Other session doesn't hold this lock should't do corresponding operation according to the lock type.
TableLockStatePreLock
// TableLockStatePublic means this table lock is public state.
TableLockStatePublic
)

// String implements fmt.Stringer interface.
func (t TableLockState) String() string {
switch t {
case TableLockStatePreLock:
return "pre-lock"
case TableLockStatePublic:
return "public"
default:
return "none"
}
}

// TableLockType is the type of the table lock.
type TableLockType byte

const (
TableLockNone TableLockType = iota
// TableLockRead means the session with this lock can read the table (but not write it).
// Multiple sessions can acquire a READ lock for the table at the same time.
// Other sessions can read the table without explicitly acquiring a READ lock.
TableLockRead
// TableLockReadLocal is not supported.
TableLockReadLocal
// TableLockWrite means only the session with this lock has write/read permission.
// Only the session that holds the lock can access the table. No other session can access it until the lock is released.
TableLockWrite
// TableLockWriteLocal means the session with this lock has write/read permission, and the other session still has read permission.
TableLockWriteLocal
)

func (t TableLockType) String() string {
switch t {
case TableLockNone:
return "NONE"
case TableLockRead:
return "READ"
case TableLockReadLocal:
return "READ LOCAL"
case TableLockWriteLocal:
return "WRITE LOCAL"
case TableLockWrite:
return "WRITE"
}
return ""
}

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

// GetPartitionInfo returns the partition information.
Expand Down
2 changes: 1 addition & 1 deletion model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (*testModelSuite) TestModelBasic(c *C) {
c.Assert(tp.String(), Equals, "BTREE")
tp = IndexTypeHash
c.Assert(tp.String(), Equals, "HASH")
tp = 1E5
tp = 1e5
c.Assert(tp.String(), Equals, "")
has := index.HasPrefixIndex()
c.Assert(has, Equals, true)
Expand Down
Loading

0 comments on commit 0dfe81a

Please sign in to comment.