Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser/executor: admin show/set bdr role #48504

Merged
merged 14 commits into from
Nov 16, 2023
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 @@ -5488,3 +5490,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
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
27 changes: 27 additions & 0 deletions pkg/parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2318,6 +2318,8 @@ const (
AdminResetTelemetryID
AdminReloadStatistics
AdminFlushPlanCache
AdminSetBDRRole
AdminShowBDRRole
)

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

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 @@ -2413,6 +2424,7 @@ type AdminStmt struct {
Where ExprNode
StatementScope StatementScope
LimitSimple LimitSimple
BDRRole BDRRole
}

// Restore implements Node interface.
Expand Down Expand Up @@ -2567,6 +2579,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