Skip to content

Commit

Permalink
add table lock info to tableInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 committed Apr 30, 2019
1 parent 3d3170b commit 798dc77
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 44 deletions.
33 changes: 1 addition & 32 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1189,41 +1189,10 @@ type LockTablesStmt struct {
TableLocks []TableLock
}

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

const (
TableLockNone TableLockType = iota
// TableLockRead means the session with this lock has read permission, other session can't read/write until the lock is released.
TableLockRead
// TableLockReadLocal is not supported. TableLockReadLocal enables nonconflicting INSERT statements by other sessions to execute while the lock is held.
TableLockReadLocal
// TableLockWrite means only the session with this lock has write/read permission, other session can't read/write until the lock is released.
TableLockWrite
// TableLockWriteLocal means the session with this lock has write/read permission, and the other session is 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 ""
}

// TableLock contains the table name and lock type.
type TableLock struct {
Table *TableName
Type TableLockType
Type model.TableLockType
}

// Accept implements Node Accept interface.
Expand Down
6 changes: 4 additions & 2 deletions model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ const (
ActionTruncateTablePartition ActionType = 23
ActionDropView ActionType = 24
ActionRecoverTable ActionType = 25
ActionTableLock ActionType = 26
ActionLockTable ActionType = 26
ActionUnlockTable ActionType = 27
)

// AddIndexStr is a string related to the operation of "add index".
Expand Down Expand Up @@ -87,7 +88,8 @@ var actionMap = map[ActionType]string{
ActionTruncateTablePartition: "truncate partition",
ActionDropView: "drop view",
ActionRecoverTable: "recover table",
ActionTableLock: "table lock",
ActionLockTable: "lock table",
ActionUnlockTable: "unlock table",
}

// String return current ddl action in string
Expand Down
84 changes: 84 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package model

import (
"encoding/json"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -224,11 +225,94 @@ type TableInfo struct {
Compression string `json:"compression"`

View *ViewInfo `json:"view"`
// Lock represent the table lock info.
Lock *TableLockInfo `json:"Lock"`

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

// TableLockInfo provides meta data describing a table lock.
type TableLockInfo struct {
Tp TableLockType
// Use array because there maybe multiple session both hold same read lock.
Sessions []SessionInfo
State TableLockState
// TS use to record the timestamp of this table lock been public 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)
}

// DBTableID is composed by database ID and table ID.
type DBTableID struct {
DBID int64
TableID int64
}

// 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 has read permission, other session can't read/write until the lock is released.
TableLockRead
// TableLockReadLocal is not supported. TableLockReadLocal enables nonconflicting INSERT statements by other sessions to execute while the lock is held.
TableLockReadLocal
// TableLockWrite means only the session with this lock has write/read permission, other session can't read/write until the lock is released.
TableLockWrite
// TableLockWriteLocal means the session with this lock has write/read permission, and the other session is 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 ""
}

// GetPartitionInfo returns the partition information.
func (t *TableInfo) GetPartitionInfo() *PartitionInfo {
if t.Partition != nil && t.Partition.Enable {
Expand Down
10 changes: 5 additions & 5 deletions parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -8481,26 +8481,26 @@ TableLock:
{
$$ = ast.TableLock{
Table: $1.(*ast.TableName),
Type: $2.(ast.TableLockType),
Type: $2.(model.TableLockType),
}
}

LockType:
"READ"
{
$$ = ast.TableLockRead
$$ = model.TableLockRead
}
| "READ" "LOCAL"
{
$$ = ast.TableLockReadLocal
$$ = model.TableLockReadLocal
}
| "WRITE"
{
$$ = ast.TableLockWrite
$$ = model.TableLockWrite
}
| "WRITE" "LOCAL"
{
$$ = ast.TableLockWriteLocal
$$ = model.TableLockWriteLocal
}

TableLockList:
Expand Down

0 comments on commit 798dc77

Please sign in to comment.