Skip to content

Commit

Permalink
fix: fix bug can't match any group (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
gufengwyx8 committed Apr 10, 2024
1 parent aa7f09e commit 89c8bd1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
7 changes: 7 additions & 0 deletions pkg/mysql/thead/thead.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ var (
Col{Name: "expr", FieldType: consts.FieldTypeVarString},
Col{Name: "step", FieldType: consts.FieldTypeVarString},
}

OpenTables = Thead{
Col{Name: "Database", FieldType: consts.FieldTypeVarString},
Col{Name: "Table", FieldType: consts.FieldTypeVarString},
Col{Name: "In_use", FieldType: consts.FieldTypeInt24},
Col{Name: "Name_locked", FieldType: consts.FieldTypeInt24},
}
)

type Col struct {
Expand Down
36 changes: 22 additions & 14 deletions pkg/runtime/optimize/dal/show_open_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,37 @@ func optimizeShowOpenTables(ctx context.Context, o *optimize.Optimizer) (proto.P
}
}

duplicates := make(map[string]struct{})

plans := make([]proto.Plan, 0, len(clusters))
for _, cluster := range clusters {
ns := namespace.Load(tenant, cluster)
// check every group from namespace
groups := ns.DBGroups()
for i := 0; i < len(groups); i++ {
var ret *dal.ShowOpenTablesPlan
if db, ok := stmt.Like(); ok && db == cluster {
// filter in cluster
show := ast.NewBaseShow(groups[i])
stmtCopy := ast.ShowOpenTables{BaseShow: &show}
ret = dal.NewShowOpenTablesPlan(&stmtCopy)
} else {
// no filter or can't match any group
ret = dal.NewShowOpenTablesPlan(stmt)
if db, ok := stmt.Like(); !ok || db == cluster {
var ret *dal.ShowOpenTablesPlan
if ok {
// filter in cluster
show := ast.NewBaseShow(groups[i])
stmtCopy := ast.ShowOpenTables{BaseShow: &show}
ret = dal.NewShowOpenTablesPlan(&stmtCopy, duplicates, false)
} else {
// no filter
ret = dal.NewShowOpenTablesPlan(stmt, duplicates, false)
}
ret.BindArgs(o.Args)
ret.SetInvertedShards(invertedShards)
ret.SetInvertedDatabases(invertedDatabases)
ret.SetDatabase(groups[i])
plans = append(plans, ret)
}
ret.BindArgs(o.Args)
ret.SetInvertedShards(invertedShards)
ret.SetInvertedDatabases(invertedDatabases)
ret.SetDatabase(groups[i])
plans = append(plans, ret)
}
}
if len(plans) == 0 {
// can't match any group
return dal.NewShowOpenTablesPlan(stmt, duplicates, true), nil
}

return &dml.CompositePlan{
Plans: plans,
Expand Down
32 changes: 23 additions & 9 deletions pkg/runtime/plan/dal/show_open_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dal

import (
"context"
"github.com/arana-db/arana/pkg/mysql/thead"
"strings"
)

Expand All @@ -43,14 +44,18 @@ type ShowOpenTablesPlan struct {
Database string
Conn proto.DB
Stmt *ast.ShowOpenTables
invertedShards map[string]string // phy table name -> logical table name
invertedDatabases map[string]string // phy database name -> logical database name
invertedShards map[string]string // phy table name -> logical table name
invertedDatabases map[string]string // phy database name -> logical database name
duplicates map[string]struct{} // filter duplicates
empty bool // if can't match any group, return empty result
}

// NewShowOpenTablesPlan create ShowTables Plan
func NewShowOpenTablesPlan(stmt *ast.ShowOpenTables) *ShowOpenTablesPlan {
func NewShowOpenTablesPlan(stmt *ast.ShowOpenTables, duplicates map[string]struct{}, empty bool) *ShowOpenTablesPlan {
return &ShowOpenTablesPlan{
Stmt: stmt,
Stmt: stmt,
duplicates: duplicates,
empty: empty,
}
}

Expand All @@ -66,6 +71,10 @@ func (st *ShowOpenTablesPlan) ExecIn(ctx context.Context, conn proto.VConn) (pro
err error
)

if st.empty {
return emptyRs(), nil
}

if err = st.Stmt.Restore(ast.RestoreDefault, &sb, &indexes); err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -86,9 +95,6 @@ func (st *ShowOpenTablesPlan) ExecIn(ctx context.Context, conn proto.VConn) (pro

fields, _ := ds.Fields()

// filter duplicates
duplicates := make(map[string]struct{})

// 1. convert to logical table name
// 2. filter duplicated table name
ds = dataset.Pipe(ds,
Expand Down Expand Up @@ -123,16 +129,24 @@ func (st *ShowOpenTablesPlan) ExecIn(ctx context.Context, conn proto.VConn) (pro
}

tableName := vr.Values()[1].String()
if _, ok := duplicates[tableName]; ok {
if _, ok := st.duplicates[tableName]; ok {
return false
}
duplicates[tableName] = struct{}{}
st.duplicates[tableName] = struct{}{}
return true
}),
)
return resultx.New(resultx.WithDataset(ds)), nil
}

func emptyRs() proto.Result {
columns := thead.OpenTables.ToFields()
ds := &dataset.VirtualDataset{
Columns: columns,
}
return resultx.New(resultx.WithDataset(ds))
}

func (st *ShowOpenTablesPlan) SetDatabase(database string) {
st.Database = database
}
Expand Down

0 comments on commit 89c8bd1

Please sign in to comment.