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

sync to 1.1: fix db not found when mo_table_size #15444

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions pkg/sql/plan/function/func_mo.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,9 @@ func MoTableSize(ivecs []*vector.Vector, result vector.FunctionResultWrapper, pr
txn := proc.TxnOperator

var accountId uint32
var accSwitched bool
// XXX old code starts a new transaction. why?
for i := uint64(0); i < uint64(length); i++ {
if accSwitched {
// consider this situation:
// if a sql trys to gather all table's size in one query,
// this will traverse all tables, including the cluster table, belongs
// this account.
// but if these tables in `tbls` has orders: xxx, cluster table, xxx, xxx, xxx.
// the account id stored in proc.Ctx will be changed to system account id when process that cluster
// table, and causing the last three tables can not be found when call the `engine.Database()`.
// so should be first to switch bach the right account id here.
accSwitched = false
proc.Ctx = defines.AttachAccountId(proc.Ctx, accountId)
}
foolCtx := proc.Ctx

db, dbnull := dbs.GetStrValue(i)
tbl, tblnull := tbls.GetStrValue(i)
Expand All @@ -214,19 +202,17 @@ func MoTableSize(ivecs []*vector.Vector, result vector.FunctionResultWrapper, pr

if isClusterTable(dbStr, tblStr) {
//if it is the cluster table in the general account, switch into the sys account
accountId, err = defines.GetAccountId(proc.Ctx)
accountId, err = defines.GetAccountId(foolCtx)
if err != nil {
return err
}
if accountId != uint32(sysAccountID) {
accSwitched = true
proc.Ctx = defines.AttachAccountId(proc.Ctx, uint32(sysAccountID))
foolCtx = defines.AttachAccountId(foolCtx, uint32(sysAccountID))
}
}
ctx := proc.Ctx

var dbo engine.Database
dbo, err = e.Database(ctx, dbStr, txn)
dbo, err = e.Database(foolCtx, dbStr, txn)
if err != nil {
if moerr.IsMoErrCode(err, moerr.OkExpectedEOB) {
var buf bytes.Buffer
Expand All @@ -237,26 +223,29 @@ func MoTableSize(ivecs []*vector.Vector, result vector.FunctionResultWrapper, pr
dbStr2 := functionUtil.QuickBytesToStr(db2)
tblStr2 := functionUtil.QuickBytesToStr(tbl2)

buf.WriteString(fmt.Sprintf("%s-%s; ", dbStr2, tblStr2))
buf.WriteString(fmt.Sprintf("%s#%s; ", dbStr2, tblStr2))
}
attachedId, _ := defines.GetAccountId(proc.Ctx)

originalAccId, _ := defines.GetAccountId(proc.Ctx)
attachedAccId, _ := defines.GetAccountId(foolCtx)

logutil.Errorf(
fmt.Sprintf("db not found when mo_table_size: %s-%s, acc: %v-%d-%d, extra: %s",
dbStr, tblStr, accSwitched, accountId, attachedId, buf.String()))
fmt.Sprintf("db not found when mo_table_size: %s#%s, acc: %d-%d, extra: %s",
dbStr, tblStr, attachedAccId, originalAccId, buf.String()))
return moerr.NewInvalidArgNoCtx("db not found when mo_table_size", fmt.Sprintf("%s-%s", dbStr, tblStr))
}
return err
}
rel, err = dbo.Relation(ctx, tblStr, nil)
rel, err = dbo.Relation(foolCtx, tblStr, nil)
if err != nil {
return err
}

var oSize, iSize uint64
if oSize, err = originalTableSize(ctx, dbo, rel); err != nil {
if oSize, err = originalTableSize(foolCtx, dbo, rel); err != nil {
return err
}
if iSize, err = indexesTableSize(ctx, dbo, rel); err != nil {
if iSize, err = indexesTableSize(foolCtx, dbo, rel); err != nil {
return err
}

Expand Down
Loading