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

support restore system table (#1.2-dev) #16462

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
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
107 changes: 92 additions & 15 deletions pkg/frontend/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/google/uuid"

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/defines"
pbplan "github.com/matrixorigin/matrixone/pkg/pb/plan"
Expand Down Expand Up @@ -60,6 +61,41 @@ var (
restoreTableDataFmt = "insert into `%s`.`%s` SELECT * FROM `%s`.`%s` {snapshot = '%s'}"

skipDbs = []string{"mysql", "system", "system_metrics", "mo_task", "mo_debug", "information_schema", "mo_catalog"}

needSkipTablesInMocatalog = map[string]int8{
"mo_database": 1,
"mo_tables": 1,
"mo_columns": 1,
"mo_table_partitions": 1,
"mo_foreign_keys": 1,
"mo_indexes": 1,
"mo_account": 1,

catalog.MOVersionTable: 1,
catalog.MOUpgradeTable: 1,
catalog.MOUpgradeTenantTable: 1,
catalog.MOAutoIncrTable: 1,

"mo_user": 0,
"mo_role": 0,
"mo_user_grant": 0,
"mo_role_grant": 0,
"mo_role_privs": 0,
"mo_user_defined_function": 0,
"mo_stored_procedure": 0,
"mo_mysql_compatibility_mode": 0,
"mo_stages": 0,
"mo_pubs": 1,

"mo_sessions": 1,
"mo_configurations": 1,
"mo_locks": 1,
"mo_variables": 1,
"mo_transactions": 1,
"mo_cache": 1,

"mo_snapshots": 1,
}
)

type snapshotRecord struct {
Expand Down Expand Up @@ -304,6 +340,10 @@ func doRestoreSnapshot(ctx context.Context, ses *Session, stmt *tree.RestoreSnap
}
}

if needSkipDb(dbName) {
return moerr.NewInternalError(ctx, "can't restore db: %v", dbName)
}

// restore as a txn
if err = bh.Exec(ctx, "begin;"); err != nil {
return err
Expand Down Expand Up @@ -430,6 +470,11 @@ func restoreToAccount(
return
}
}

// restore system db
if err = restoreSystemDatabase(ctx, bh, snapshotName, toAccountId); err != nil {
return
}
return
}

Expand Down Expand Up @@ -503,12 +548,6 @@ func restoreToDatabaseOrTable(
}

for _, tblInfo := range tableInfos {
if needSkipTable(dbName, tblInfo.tblName) {
// TODO skip tables which should not to be restored
getLogger().Info(fmt.Sprintf("[%s] skip table: %v.%v", snapshotName, dbName, tblInfo.tblName))
continue
}

key := genKey(dbName, tblInfo.tblName)

// skip table which is foreign key related
Expand All @@ -529,6 +568,37 @@ func restoreToDatabaseOrTable(
return
}

func restoreSystemDatabase(
ctx context.Context,
bh BackgroundExec,
snapshotName string,
toAccountId uint32) (err error) {
getLogger().Info(fmt.Sprintf("[%s] start to restore system database: %s", snapshotName, moCatalog))
tableInfos, err := getTableInfos(ctx, bh, snapshotName, moCatalog, "")
if err != nil {
return
}

curAccountId, err := defines.GetAccountId(ctx)
if err != nil {
return
}

for _, tblInfo := range tableInfos {
if needSkipTable(curAccountId, moCatalog, tblInfo.tblName) {
// TODO skip tables which should not to be restored
getLogger().Info(fmt.Sprintf("[%s] skip restore system table: %v.%v", snapshotName, moCatalog, tblInfo.tblName))
continue
}

getLogger().Info(fmt.Sprintf("[%s] start to restore system table: %v.%v", snapshotName, moCatalog, tblInfo.tblName))
if err = recreateTable(ctx, bh, snapshotName, tblInfo, toAccountId); err != nil {
return
}
}
return
}

func restoreTablesWithFk(
ctx context.Context,
bh BackgroundExec,
Expand Down Expand Up @@ -668,17 +738,20 @@ func needSkipDb(dbName string) bool {
return slices.Contains(skipDbs, dbName)
}

func needSkipTable(dbName string, tblName string) bool {
func needSkipTable(accountId uint32, dbName string, tblName string) bool {
// TODO determine which tables should be skipped

if dbName == "information_schema" {
return true
}

if dbName == "mo_catalog" {
return true
if accountId == sysAccountID {
return dbName == moCatalog && needSkipTablesInMocatalog[tblName] == 1
} else {
if dbName == moCatalog {
if needSkip, ok := needSkipTablesInMocatalog[tblName]; ok {
return needSkip == 1
} else {
return true
}
}
}

return false
}

Expand Down Expand Up @@ -1035,14 +1108,18 @@ func getTableInfoMap(
tblName string,
tblKeys []string) (tblInfoMap map[string]*tableInfo, err error) {
tblInfoMap = make(map[string]*tableInfo)
curAccountId, err := defines.GetAccountId(ctx)
if err != nil {
return
}
for _, key := range tblKeys {
if _, ok := tblInfoMap[key]; ok {
return
}

// filter by dbName and tblName
d, t := splitKey(key)
if needSkipDb(d) || needSkipTable(d, t) {
if needSkipDb(d) || needSkipTable(curAccountId, d, t) {
continue
}
if dbName != "" && dbName != d {
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/plan/build_show_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/container/types"
"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
"github.com/matrixorigin/matrixone/pkg/sql/util"
)
Expand Down Expand Up @@ -68,6 +69,14 @@ func ConstructCreateTableSQL(tableObjRef *plan.ObjectRef, tableDef *plan.TableDe
accountId != catalog.System_Account {
continue
}

if util.IsClusterTableAttribute(colName) &&
isClusterTable &&
accountId == catalog.System_Account &&
!snapshot.TS.Equal(timestamp.Timestamp{}) {
continue
}

//-------------------------------------------------------------------------------------------------------------
buf := bytes.NewBuffer(make([]byte, 0, 64))

Expand Down
Loading
Loading