github.com/matrixorigin/matrixone@v1.2.0/pkg/bootstrap/versions/v1_2_0/upgrade.go (about) 1 // Copyright 2024 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package v1_2_0 16 17 import ( 18 "context" 19 20 "github.com/matrixorigin/matrixone/pkg/bootstrap/versions" 21 "github.com/matrixorigin/matrixone/pkg/catalog" 22 "github.com/matrixorigin/matrixone/pkg/util/executor" 23 "go.uber.org/zap" 24 ) 25 26 var ( 27 Handler = &versionHandle{ 28 metadata: versions.Version{ 29 Version: "1.2.0", 30 MinUpgradeVersion: "1.1.0", 31 UpgradeCluster: versions.Yes, 32 UpgradeTenant: versions.Yes, 33 VersionOffset: uint32(len(clusterUpgEntries) + len(tenantUpgEntries)), 34 }, 35 } 36 ) 37 38 type versionHandle struct { 39 metadata versions.Version 40 } 41 42 func (v *versionHandle) Metadata() versions.Version { 43 return v.metadata 44 } 45 46 func (v *versionHandle) Prepare( 47 ctx context.Context, 48 txn executor.TxnExecutor, 49 final bool) error { 50 51 for _, upgEntry := range UpgPrepareEntres { 52 err := upgEntry.Upgrade(txn, catalog.System_Account) 53 if err != nil { 54 getLogger().Error("prepare upgrade entry execute error", zap.Error(err), zap.String("upgrade entry", upgEntry.String())) 55 return err 56 } 57 } 58 59 return nil 60 } 61 62 func (v *versionHandle) HandleTenantUpgrade( 63 ctx context.Context, 64 tenantID int32, 65 txn executor.TxnExecutor) error { 66 67 for _, upgEntry := range tenantUpgEntries { 68 err := upgEntry.Upgrade(txn, uint32(tenantID)) 69 if err != nil { 70 getLogger().Error("tenant upgrade entry execute error", zap.Error(err), zap.Int32("tenantId", tenantID), zap.String("upgrade entry", upgEntry.String())) 71 return err 72 } 73 } 74 75 return nil 76 } 77 78 func (v *versionHandle) HandleClusterUpgrade( 79 ctx context.Context, 80 txn executor.TxnExecutor) error { 81 //if err := handleCreateIndexesForTaskTables(ctx, txn); err != nil { 82 // return err 83 //} 84 //if err := v.handleCreateTxnTrace(txn); err != nil { 85 // return err 86 //} 87 txn.Use(catalog.MO_CATALOG) 88 for _, upgEntry := range clusterUpgEntries { 89 err := upgEntry.Upgrade(txn, catalog.System_Account) 90 if err != nil { 91 getLogger().Error("cluster upgrade entry execute error", zap.Error(err), zap.String("upgrade entry", upgEntry.String())) 92 return err 93 } 94 } 95 return nil 96 } 97 98 func (v *versionHandle) HandleCreateFrameworkDeps(txn executor.TxnExecutor) error { 99 // create new upgrade framework tables for the first time, 100 // which means using v1.2.0 for the first time 101 // NOTE: The `alter table` statements used for upgrading system table rely on `mo_foreign_keys` and `mo_indexes`, 102 // so preprocessing is performed first 103 for _, upgEntry := range createFrameworkDepsEntres { 104 err := upgEntry.Upgrade(txn, catalog.System_Account) 105 if err != nil { 106 getLogger().Error("Handle create framework dependencies upgrade entry execute error", zap.Error(err), zap.String("upgrade entry", upgEntry.String())) 107 return err 108 } 109 } 110 return nil 111 } 112 113 //func handleCreateIndexesForTaskTables(ctx context.Context, 114 // txn executor.TxnExecutor) error { 115 // result, err := txn.Exec(`show indexes in mo_task.sys_async_task;`, executor.StatementOption{}) 116 // if err != nil { 117 // return err 118 // } 119 // defer result.Close() 120 // hasIndex := false 121 // result.ReadRows(func(rows int, cols []*vector.Vector) bool { 122 // hasIndex = true 123 // return false 124 // }) 125 // if hasIndex { 126 // return nil 127 // } 128 // 129 // indexSqls := []string{ 130 // fmt.Sprintf(`create index idx_task_status on %s.sys_async_task(task_status)`, 131 // catalog.MOTaskDB), 132 // fmt.Sprintf(`create index idx_task_runner on %s.sys_async_task(task_runner)`, 133 // catalog.MOTaskDB), 134 // fmt.Sprintf(`create index idx_task_executor on %s.sys_async_task(task_metadata_executor)`, 135 // catalog.MOTaskDB), 136 // fmt.Sprintf(`create index idx_task_epoch on %s.sys_async_task(task_epoch)`, 137 // catalog.MOTaskDB), 138 // fmt.Sprintf(`create index idx_account_id on %s.sys_daemon_task(account_id)`, 139 // catalog.MOTaskDB), 140 // fmt.Sprintf(`create index idx_last_heartbeat on %s.sys_daemon_task(last_heartbeat)`, 141 // catalog.MOTaskDB), 142 // } 143 // for _, sql := range indexSqls { 144 // r, err := txn.Exec(sql, executor.StatementOption{}) 145 // if err != nil { 146 // return err 147 // } 148 // r.Close() 149 // } 150 // return nil 151 //} 152 // 153 //func (v *versionHandle) handleCreateTxnTrace(txn executor.TxnExecutor) error { 154 // txn.Use(catalog.MO_CATALOG) 155 // res, err := txn.Exec("show databases", executor.StatementOption{}) 156 // if err != nil { 157 // return err 158 // } 159 // completed := false 160 // res.ReadRows(func(rows int, cols []*vector.Vector) bool { 161 // for i := 0; i < rows; i++ { 162 // if cols[0].GetStringAt(i) == trace.DebugDB { 163 // completed = true 164 // } 165 // } 166 // return true 167 // }) 168 // res.Close() 169 // 170 // if completed { 171 // return nil 172 // } 173 // 174 // for _, sql := range trace.InitSQLs { 175 // res, err = txn.Exec(sql, executor.StatementOption{}) 176 // if err != nil { 177 // return err 178 // } 179 // res.Close() 180 // } 181 // return nil 182 //}