|
| 1 | +package replicasetcmd |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/mitchellh/mapstructure" |
| 9 | + "github.com/tarantool/tt/cli/connector" |
| 10 | + "github.com/tarantool/tt/cli/replicaset" |
| 11 | + "github.com/tarantool/tt/cli/running" |
| 12 | +) |
| 13 | + |
| 14 | +// DowngradeOpts contains options used for the downgrade process. |
| 15 | +type DowngradeOpts struct { |
| 16 | + // ChosenReplicasetAliases is a list of replicaset names specified by |
| 17 | + // the user for the downgrade. |
| 18 | + ChosenReplicasetAliases []string |
| 19 | + // Timeout period (in seconds) for waiting on LSN synchronization. |
| 20 | + Timeout int |
| 21 | + // DowngradeVersion is a version to downgrade the schema to. |
| 22 | + DowngradeVersion string |
| 23 | +} |
| 24 | + |
| 25 | +//go:embed lua/downgrade.lua |
| 26 | +var downgradeMasterLua string |
| 27 | + |
| 28 | +func filterComments(script string) string { |
| 29 | + var filteredLines []string |
| 30 | + lines := strings.Split(script, "\n") |
| 31 | + for _, line := range lines { |
| 32 | + trimmedLine := strings.TrimSpace(line) |
| 33 | + if !strings.HasPrefix(trimmedLine, "--") { |
| 34 | + filteredLines = append(filteredLines, line) |
| 35 | + } |
| 36 | + } |
| 37 | + return strings.Join(filteredLines, "\n") |
| 38 | +} |
| 39 | + |
| 40 | +// Downgrade downgrades tarantool schema. |
| 41 | +func Downgrade(discoveryCtx DiscoveryCtx, opts DowngradeOpts, |
| 42 | + connOpts connector.ConnectOpts) error { |
| 43 | + replicasets, err := getReplicasets(discoveryCtx) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + replicasets = fillAliases(replicasets) |
| 49 | + replicasetsToDowngrade, err := filterReplicasetsByAliases(replicasets, |
| 50 | + opts.ChosenReplicasetAliases) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + return internalDowngrade(replicasetsToDowngrade, opts.Timeout, |
| 56 | + opts.DowngradeVersion, connOpts) |
| 57 | +} |
| 58 | + |
| 59 | +func internalDowngrade(replicasets []replicaset.Replicaset, lsnTimeout int, version string, |
| 60 | + connOpts connector.ConnectOpts) error { |
| 61 | + for _, replicaset := range replicasets { |
| 62 | + err := downgradeReplicaset(replicaset, lsnTimeout, version, connOpts) |
| 63 | + if err != nil { |
| 64 | + fmt.Printf("• %s: error\n", replicaset.Alias) |
| 65 | + return fmt.Errorf("replicaset %s: %w", replicaset.Alias, err) |
| 66 | + } |
| 67 | + fmt.Printf("• %s: ok\n", replicaset.Alias) |
| 68 | + } |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func downgradeMaster(master *instanceMeta, version string) (syncInfo, error) { |
| 73 | + var downgradeInfo syncInfo |
| 74 | + fullMasterName := running.GetAppInstanceName(master.run) |
| 75 | + res, err := master.conn.Eval(filterComments(downgradeMasterLua), |
| 76 | + []interface{}{version}, connector.RequestOpts{}) |
| 77 | + if err != nil { |
| 78 | + return downgradeInfo, fmt.Errorf( |
| 79 | + "failed to execute downgrade script on master instance - %s: %w", |
| 80 | + fullMasterName, err) |
| 81 | + } |
| 82 | + |
| 83 | + if err := mapstructure.Decode(res[0], &downgradeInfo); err != nil { |
| 84 | + return downgradeInfo, fmt.Errorf( |
| 85 | + "failed to decode response from master instance - %s: %w", |
| 86 | + fullMasterName, err) |
| 87 | + } |
| 88 | + |
| 89 | + if downgradeInfo.Err != nil { |
| 90 | + return downgradeInfo, fmt.Errorf( |
| 91 | + "master instance downgrade failed - %s: %s", |
| 92 | + fullMasterName, *downgradeInfo.Err) |
| 93 | + } |
| 94 | + return downgradeInfo, nil |
| 95 | +} |
| 96 | + |
| 97 | +func downgradeReplicaset(replicaset replicaset.Replicaset, lsnTimeout int, version string, |
| 98 | + connOpts connector.ConnectOpts) error { |
| 99 | + master, replicas, err := collectRWROInfo(replicaset, connOpts) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + defer closeConnectors(master, replicas) |
| 105 | + |
| 106 | + // Downgrade master instance, collect LSN and IID from master instance. |
| 107 | + downgradeInfo, err := downgradeMaster(master, version) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + // Downgrade replica instances. |
| 113 | + masterLSN := downgradeInfo.LSN |
| 114 | + masterIID := downgradeInfo.IID |
| 115 | + |
| 116 | + for _, replica := range replicas { |
| 117 | + fullReplicaName := running.GetAppInstanceName(replica.run) |
| 118 | + err := waitLSN(replica.conn, masterIID, masterLSN, lsnTimeout) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("can't ensure that downgrade operations performed on "+ |
| 121 | + "%s are replicated to %s to perform snapshotting on it: error "+ |
| 122 | + "waiting LSN %d in vclock component %d: %w", |
| 123 | + running.GetAppInstanceName(master.run), fullReplicaName, |
| 124 | + masterLSN, masterIID, err) |
| 125 | + } |
| 126 | + err = snapshot(&replica) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
0 commit comments