github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/dm/bootstrap/upgrade.go (about) 1 // Copyright 2022 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package bootstrap 15 16 import ( 17 "context" 18 "sort" 19 20 "github.com/coreos/go-semver/semver" 21 "go.uber.org/zap" 22 ) 23 24 // UpgradeFunc defines the function for upgrade. 25 type UpgradeFunc struct { 26 Version semver.Version 27 Upgrade func(ctx context.Context) error 28 Rollback func(ctx context.Context) error 29 } 30 31 // Upgrader represents the upgrader interface. 32 type Upgrader interface { 33 UpgradeFuncs() []UpgradeFunc 34 } 35 36 // DefaultUpgrader defines the default Upgrade steps. 37 type DefaultUpgrader struct { 38 Upgrader 39 40 logger *zap.Logger 41 } 42 43 // NewDefaultUpgrader returns a new DefaultUpgrader. 44 func NewDefaultUpgrader(pLogger *zap.Logger) *DefaultUpgrader { 45 return &DefaultUpgrader{ 46 logger: pLogger.With(zap.String("component", "upgrader")), 47 } 48 } 49 50 // Upgrade run the upgrade function in order. 51 // if any upgrade failed, try to rollback all of them. 52 func (upgrader *DefaultUpgrader) Upgrade(ctx context.Context, fromVer semver.Version) error { 53 var ( 54 err error 55 rollbackFuncs = make([]func(ctx context.Context) error, 0) 56 ) 57 for _, upgradeFunc := range upgrader.upgradeFuncs() { 58 if upgradeFunc.Version.Compare(fromVer) <= 0 { 59 continue 60 } 61 upgrader.logger.Info("start upgrading", zap.Stringer("internal_version", upgradeFunc.Version)) 62 rollbackFuncs = append(rollbackFuncs, upgradeFunc.Rollback) 63 if err = upgradeFunc.Upgrade(ctx); err != nil { 64 upgrader.logger.Error("upgrade failed", zap.Error(err)) 65 break 66 } 67 } 68 if err != nil { 69 for i := len(rollbackFuncs) - 1; i >= 0; i-- { 70 rollback := rollbackFuncs[i] 71 if rollback != nil { 72 if err2 := rollback(ctx); err2 != nil { 73 upgrader.logger.Error("rollback failed", zap.Error(err2)) 74 continue 75 } 76 } 77 } 78 } 79 return err 80 } 81 82 // upgradeFuncs sort the upgrade functions for Upgrader. 83 // NOTE: though we sort the upgrade functions, the upgrade order should still be linear. 84 // e.g. If we release in such order: v6.1.0, v6.2.0, v6.1.1 85 // the upgrade order should be: v6.1.0, v6.1.1, v6.2.0 86 // but it's impossible to do that since v6.2.0 was released before v6.1.1 87 // That is to say, we should not upgrade patch version after we upgrade minor version, as well as minor and major. 88 func (upgrader *DefaultUpgrader) upgradeFuncs() []UpgradeFunc { 89 funcs := upgrader.UpgradeFuncs() 90 sort.Slice(funcs, func(i, j int) bool { 91 return funcs[i].Version.LessThan(funcs[j].Version) 92 }) 93 return funcs 94 }