|
| 1 | +package vtctld |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/planetscale/cli/internal/cmdutil" |
| 11 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + plannedReparentOperationPollInterval = time.Second |
| 17 | + plannedReparentOperationTimeoutBuffer = 30 * time.Second |
| 18 | + plannedReparentOperationDefaultTimeout = 10 * time.Minute |
| 19 | +) |
| 20 | + |
| 21 | +func PlannedReparentShardCmd(ch *cmdutil.Helper) *cobra.Command { |
| 22 | + var flags struct { |
| 23 | + keyspace string |
| 24 | + shard string |
| 25 | + newPrimary string |
| 26 | + wait bool |
| 27 | + } |
| 28 | + |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "planned-reparent-shard <database> <branch>", |
| 31 | + Short: "Reparent a shard to a new primary", |
| 32 | + Long: `Reparent a shard to a new primary using Vitess PlannedReparentShard. |
| 33 | +Both the old and new primaries must be up and running. |
| 34 | +
|
| 35 | +To check on an existing operation, use the "status" subcommand: |
| 36 | + pscale branch vtctld planned-reparent-shard status <db> <branch> <operation-id>`, |
| 37 | + Args: cmdutil.RequiredArgs("database", "branch"), |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + ctx := cmd.Context() |
| 40 | + database, branch := args[0], args[1] |
| 41 | + |
| 42 | + client, err := ch.Client() |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + end := ch.Printer.PrintProgress( |
| 48 | + fmt.Sprintf("Executing PlannedReparentShard on %s\u2026", |
| 49 | + progressTarget(ch.Config.Organization, database, branch))) |
| 50 | + defer end() |
| 51 | + |
| 52 | + operation, err := client.PlannedReparentShard.Create(ctx, &ps.PlannedReparentShardRequest{ |
| 53 | + Organization: ch.Config.Organization, |
| 54 | + Database: database, |
| 55 | + Branch: branch, |
| 56 | + Keyspace: flags.keyspace, |
| 57 | + Shard: flags.shard, |
| 58 | + NewPrimary: flags.newPrimary, |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + return cmdutil.HandleError(err) |
| 62 | + } |
| 63 | + |
| 64 | + if !flags.wait { |
| 65 | + end() |
| 66 | + return ch.Printer.PrintJSON(map[string]string{"id": operation.ID}) |
| 67 | + } |
| 68 | + |
| 69 | + result, err := waitForPlannedReparentResult(ctx, client, ch.Config.Organization, database, branch, operation) |
| 70 | + if err != nil { |
| 71 | + return cmdutil.HandleError(err) |
| 72 | + } |
| 73 | + |
| 74 | + end() |
| 75 | + return ch.Printer.PrettyPrintJSON(result) |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace name") |
| 80 | + cmd.Flags().StringVar(&flags.shard, "shard", "", "Shard range (e.g., '-80', '80-', or '-' for unsharded)") |
| 81 | + cmd.Flags().StringVar(&flags.newPrimary, "new-primary", "", "Tablet alias to promote as the new primary") |
| 82 | + cmd.Flags().BoolVar(&flags.wait, "wait", true, "Wait for the operation to complete") |
| 83 | + cmd.MarkFlagRequired("keyspace") // nolint:errcheck |
| 84 | + cmd.MarkFlagRequired("shard") // nolint:errcheck |
| 85 | + cmd.MarkFlagRequired("new-primary") // nolint:errcheck |
| 86 | + |
| 87 | + cmd.AddCommand(plannedReparentShardStatusCmd(ch)) |
| 88 | + |
| 89 | + return cmd |
| 90 | +} |
| 91 | + |
| 92 | +func plannedReparentShardStatusCmd(ch *cmdutil.Helper) *cobra.Command { |
| 93 | + cmd := &cobra.Command{ |
| 94 | + Use: "status <database> <branch> <operation-id>", |
| 95 | + Short: "Check the status of a planned reparent shard operation", |
| 96 | + Args: cmdutil.RequiredArgs("database", "branch", "operation-id"), |
| 97 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 98 | + ctx := cmd.Context() |
| 99 | + database, branch, id := args[0], args[1], args[2] |
| 100 | + |
| 101 | + client, err := ch.Client() |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + end := ch.Printer.PrintProgress( |
| 107 | + fmt.Sprintf("Getting PlannedReparentShard operation on %s\u2026", |
| 108 | + progressTarget(ch.Config.Organization, database, branch))) |
| 109 | + defer end() |
| 110 | + |
| 111 | + operation, err := client.PlannedReparentShard.Get(ctx, &ps.GetPlannedReparentShardRequest{ |
| 112 | + Organization: ch.Config.Organization, |
| 113 | + Database: database, |
| 114 | + Branch: branch, |
| 115 | + ID: id, |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + return cmdutil.HandleError(err) |
| 119 | + } |
| 120 | + |
| 121 | + end() |
| 122 | + return ch.Printer.PrintJSON(operation) |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + return cmd |
| 127 | +} |
| 128 | + |
| 129 | +func waitForPlannedReparentResult(ctx context.Context, client *ps.Client, organization, database, branch string, operation *ps.VtctldOperation) (json.RawMessage, error) { |
| 130 | + result, done, err := plannedReparentOperationResult(operation) |
| 131 | + if done || err != nil { |
| 132 | + return result, err |
| 133 | + } |
| 134 | + |
| 135 | + request := &ps.GetPlannedReparentShardRequest{ |
| 136 | + Organization: organization, |
| 137 | + Database: database, |
| 138 | + Branch: branch, |
| 139 | + ID: operation.ID, |
| 140 | + } |
| 141 | + |
| 142 | + pollCtx, cancel := context.WithTimeout(ctx, plannedReparentOperationTimeout(operation)) |
| 143 | + defer cancel() |
| 144 | + ticker := time.NewTicker(plannedReparentOperationPollInterval) |
| 145 | + defer ticker.Stop() |
| 146 | + |
| 147 | + for { |
| 148 | + select { |
| 149 | + case <-pollCtx.Done(): |
| 150 | + if errors.Is(pollCtx.Err(), context.DeadlineExceeded) { |
| 151 | + return nil, fmt.Errorf("timed out waiting for planned reparent operation %s to finish", operation.ID) |
| 152 | + } |
| 153 | + |
| 154 | + return nil, pollCtx.Err() |
| 155 | + case <-ticker.C: |
| 156 | + } |
| 157 | + |
| 158 | + op, err := client.PlannedReparentShard.Get(pollCtx, request) |
| 159 | + if err != nil { |
| 160 | + if errors.Is(err, context.DeadlineExceeded) { |
| 161 | + return nil, fmt.Errorf("timed out waiting for planned reparent operation %s to finish", operation.ID) |
| 162 | + } |
| 163 | + |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + |
| 167 | + result, done, err = plannedReparentOperationResult(op) |
| 168 | + if done || err != nil { |
| 169 | + return result, err |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func plannedReparentOperationResult(operation *ps.VtctldOperation) (json.RawMessage, bool, error) { |
| 175 | + if !operation.Completed { |
| 176 | + return nil, false, nil |
| 177 | + } |
| 178 | + |
| 179 | + switch operation.State { |
| 180 | + case "completed": |
| 181 | + if len(operation.Result) == 0 { |
| 182 | + return json.RawMessage(`{}`), true, nil |
| 183 | + } |
| 184 | + |
| 185 | + return operation.Result, true, nil |
| 186 | + case "failed", "cancelled": |
| 187 | + if operation.Error != "" { |
| 188 | + return nil, true, errors.New(operation.Error) |
| 189 | + } |
| 190 | + |
| 191 | + return nil, true, fmt.Errorf("planned reparent operation %s ended in state %q", operation.ID, operation.State) |
| 192 | + default: |
| 193 | + return nil, true, fmt.Errorf("planned reparent operation %s reached unexpected terminal state %q", operation.ID, operation.State) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func plannedReparentOperationTimeout(operation *ps.VtctldOperation) time.Duration { |
| 198 | + if operation.Timeout > 0 { |
| 199 | + return time.Duration(operation.Timeout)*time.Second + plannedReparentOperationTimeoutBuffer |
| 200 | + } |
| 201 | + |
| 202 | + return plannedReparentOperationDefaultTimeout |
| 203 | +} |
0 commit comments