Skip to content

Commit

Permalink
parser/executor: admin show/set bdr role (pingcap#48504)
Browse files Browse the repository at this point in the history
  • Loading branch information
okJiang authored Nov 16, 2023
1 parent fb58ab7 commit bee2876
Show file tree
Hide file tree
Showing 15 changed files with 11,147 additions and 10,880 deletions.
6 changes: 6 additions & 0 deletions pkg/executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ func (b *executorBuilder) build(p plannercore.Plan) exec.Executor {
return b.buildCTETableReader(v)
case *plannercore.CompactTable:
return b.buildCompactTable(v)
case *plannercore.AdminShowBDRRole:
return b.buildAdminShowBDRRole(v)
default:
if mp, ok := p.(MockPhysicalPlan); ok {
return mp.GetExecutor()
Expand Down Expand Up @@ -5489,3 +5491,7 @@ func (b *executorBuilder) buildCompactTable(v *plannercore.CompactTable) exec.Ex
tikvStore: tikvStore,
}
}

func (b *executorBuilder) buildAdminShowBDRRole(v *plannercore.AdminShowBDRRole) exec.Executor {
return &AdminShowBDRRoleExec{BaseExecutor: exec.NewBaseExecutor(b.ctx, v.Schema(), v.ID())}
}
31 changes: 31 additions & 0 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var (
_ exec.Executor = &sortexec.TopNExec{}
_ exec.Executor = &UnionExec{}
_ exec.Executor = &FastCheckTableExec{}
_ exec.Executor = &AdminShowBDRRoleExec{}

// GlobalMemoryUsageTracker is the ancestor of all the Executors' memory tracker and GlobalMemory Tracker
GlobalMemoryUsageTracker *memory.Tracker
Expand Down Expand Up @@ -2751,3 +2752,33 @@ func ColumnName(column string) string {
func escapeName(name string) string {
return strings.ReplaceAll(name, "`", "``")
}

// AdminShowBDRRoleExec represents a show BDR role executor.
type AdminShowBDRRoleExec struct {
exec.BaseExecutor

done bool
}

// Next implements the Executor Next interface.
func (e *AdminShowBDRRoleExec) Next(ctx context.Context, req *chunk.Chunk) error {
req.Reset()
if e.done {
return nil
}

return kv.RunInNewTxn(kv.WithInternalSourceType(ctx, kv.InternalTxnAdmin), e.Ctx().GetStore(), true, func(ctx context.Context, txn kv.Transaction) error {
role, err := meta.NewMeta(txn).GetBDRRole()
if err != nil {
return err
}

if role == "" {
role = string(ast.BDRRoleNone)
}

req.AppendString(0, role)
e.done = true
return nil
})
}
13 changes: 13 additions & 0 deletions pkg/executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/auth"
"github.com/pingcap/tidb/pkg/parser/model"
Expand Down Expand Up @@ -2786,6 +2787,8 @@ func (e *SimpleExec) executeAdmin(s *ast.AdminStmt) error {
return e.executeAdminReloadStatistics(s)
case ast.AdminFlushPlanCache:
return e.executeAdminFlushPlanCache(s)
case ast.AdminSetBDRRole:
return e.executeAdminSetBDRRole(s)
}
return nil
}
Expand Down Expand Up @@ -2822,6 +2825,16 @@ func (e *SimpleExec) executeAdminFlushPlanCache(s *ast.AdminStmt) error {
return nil
}

func (e *SimpleExec) executeAdminSetBDRRole(s *ast.AdminStmt) error {
if s.Tp != ast.AdminSetBDRRole {
return errors.New("This AdminStmt is not ADMIN SET BDR_ROLE")
}

return kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnAdmin), e.Ctx().GetStore(), true, func(ctx context.Context, txn kv.Transaction) error {
return errors.Trace(meta.NewMeta(txn).SetBDRRole(string(s.BDRRole)))
})
}

func (e *SimpleExec) executeSetResourceGroupName(s *ast.SetResourceGroupStmt) error {
if s.Name.L != "" {
if _, ok := e.is.ResourceGroupByName(s.Name); !ok {
Expand Down
1 change: 1 addition & 0 deletions pkg/meta/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go_test(
shard_count = 12,
deps = [
"//pkg/kv",
"//pkg/parser/ast",
"//pkg/parser/model",
"//pkg/store/mockstore",
"//pkg/testkit/testsetup",
Expand Down
18 changes: 16 additions & 2 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var (
mPolicyGlobalID = []byte("PolicyGlobalID")
mPolicyMagicByte = CurrentMagicByteVer
mDDLTableVersion = []byte("DDLTableVersion")
mBDRRole = []byte("BDRRole")
mMetaDataLock = []byte("metadataLock")
// the id for 'default' group, the internal ddl can ensure
// user created resource group won't duplicate with this id.
Expand Down Expand Up @@ -680,10 +681,23 @@ func (m *Meta) CreateTableOrView(dbID int64, tableInfo *model.TableInfo) error {
return m.txn.HSet(dbKey, tableKey, data)
}

// SetBDRRole write BDR role into storage.
func (m *Meta) SetBDRRole(role string) error {
return errors.Trace(m.txn.Set(mBDRRole, []byte(role)))
}

// GetBDRRole get BDR role from storage.
func (m *Meta) GetBDRRole() (string, error) {
v, err := m.txn.Get(mBDRRole)
if err != nil {
return "", errors.Trace(err)
}
return string(v), nil
}

// SetDDLTables write a key into storage.
func (m *Meta) SetDDLTables(ddlTableVersion DDLTableVersion) error {
err := m.txn.Set(mDDLTableVersion, ddlTableVersion.Bytes())
return errors.Trace(err)
return errors.Trace(m.txn.Set(mDDLTableVersion, ddlTableVersion.Bytes()))
}

// CheckDDLTableVersion check if the tables related to concurrent DDL exists.
Expand Down
10 changes: 10 additions & 0 deletions pkg/meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/store/mockstore"
"github.com/pingcap/tidb/pkg/util"
Expand Down Expand Up @@ -446,6 +447,15 @@ func TestMeta(t *testing.T) {
require.NoError(t, err)
require.Equal(t, schemaDiff, readDiff)

// Test for BDR role
role, err := m.GetBDRRole()
require.NoError(t, err)
require.Len(t, role, 0)
require.NoError(t, m.SetBDRRole(string(ast.BDRRolePrimary)))
role, err = m.GetBDRRole()
require.NoError(t, err)
require.Equal(t, string(ast.BDRRolePrimary), role)

err = txn.Commit(context.Background())
require.NoError(t, err)

Expand Down
30 changes: 29 additions & 1 deletion pkg/parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2305,7 +2305,7 @@ type AdminStmtType int

// Admin statement types.
const (
AdminShowDDL = iota + 1
AdminShowDDL AdminStmtType = iota + 1
AdminCheckTable
AdminShowDDLJobs
AdminCancelDDLJobs
Expand All @@ -2332,6 +2332,8 @@ const (
AdminResetTelemetryID
AdminReloadStatistics
AdminFlushPlanCache
AdminSetBDRRole
AdminShowBDRRole
)

// HandleRange represents a range where handle value >= Begin and < End.
Expand All @@ -2340,6 +2342,16 @@ type HandleRange struct {
End int64
}

// BDRRole represents the role of the cluster in BDR mode.
type BDRRole string

const (
BDRRoleNone BDRRole = "none"
BDRRolePrimary BDRRole = "primary"
BDRRoleSecondary BDRRole = "secondary"
BDRRoleLocalOnly BDRRole = "local_only"
)

type StatementScope int

const (
Expand Down Expand Up @@ -2427,6 +2439,7 @@ type AdminStmt struct {
Where ExprNode
StatementScope StatementScope
LimitSimple LimitSimple
BDRRole BDRRole
}

// Restore implements Node interface.
Expand Down Expand Up @@ -2581,6 +2594,21 @@ func (n *AdminStmt) Restore(ctx *format.RestoreCtx) error {
} else if n.StatementScope == StatementScopeGlobal {
ctx.WriteKeyWord("FLUSH GLOBAL PLAN_CACHE")
}
case AdminSetBDRRole:
switch n.BDRRole {
case BDRRoleNone:
ctx.WriteKeyWord("SET BDR ROLE NONE")
case BDRRolePrimary:
ctx.WriteKeyWord("SET BDR ROLE PRIMARY")
case BDRRoleSecondary:
ctx.WriteKeyWord("SET BDR ROLE SECONDARY")
case BDRRoleLocalOnly:
ctx.WriteKeyWord("SET BDR ROLE LOCAL_ONLY")
default:
return errors.New("Unsupported BDR role")
}
case AdminShowBDRRole:
ctx.WriteKeyWord("SHOW BDR ROLE")
default:
return errors.New("Unsupported AdminStmt type")
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ var tokenMap = map[string]int{
"BACKEND": backend,
"BACKUP": backup,
"BACKUPS": backups,
"BDR": bdr,
"BEGIN": begin,
"BETWEEN": between,
"BERNOULLI": bernoulli,
Expand Down Expand Up @@ -491,6 +492,7 @@ var tokenMap = map[string]int{
"LOCAL": local,
"LOCALTIME": localTime,
"LOCALTIMESTAMP": localTs,
"LOCAL_ONLY": local_only,
"LOCATION": location,
"LOCK": lock,
"LOCKED": locked,
Expand Down Expand Up @@ -684,6 +686,7 @@ var tokenMap = map[string]int{
"SCHEMAS": databases,
"SECOND_MICROSECOND": secondMicrosecond,
"SECOND": second,
"SECONDARY": secondary,
"SECONDARY_ENGINE": secondaryEngine,
"SECONDARY_LOAD": secondaryLoad,
"SECONDARY_UNLOAD": secondaryUnload,
Expand Down
Loading

0 comments on commit bee2876

Please sign in to comment.