-
Notifications
You must be signed in to change notification settings - Fork 59
Add planned-reparent command to pscale branch vtctld #1240
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| package vtctld | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/planetscale/cli/internal/cmdutil" | ||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| plannedReparentOperationPollInterval = time.Second | ||
| plannedReparentOperationTimeoutBuffer = 30 * time.Second | ||
| plannedReparentOperationDefaultTimeout = 10 * time.Minute | ||
| ) | ||
|
|
||
| func PlannedReparentShardCmd(ch *cmdutil.Helper) *cobra.Command { | ||
| var flags struct { | ||
| keyspace string | ||
| shard string | ||
| newPrimary string | ||
| wait bool | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "planned-reparent-shard <database> <branch>", | ||
| Short: "Reparent a shard to a new primary", | ||
| Long: `Reparent a shard to a new primary using Vitess PlannedReparentShard. | ||
| Both the old and new primaries must be up and running. | ||
|
|
||
| To check on an existing operation, use the "status" subcommand: | ||
| pscale branch vtctld planned-reparent-shard status <db> <branch> <operation-id>`, | ||
| Args: cmdutil.RequiredArgs("database", "branch"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| database, branch := args[0], args[1] | ||
|
|
||
| client, err := ch.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| end := ch.Printer.PrintProgress( | ||
| fmt.Sprintf("Executing PlannedReparentShard on %s\u2026", | ||
| progressTarget(ch.Config.Organization, database, branch))) | ||
| defer end() | ||
|
|
||
| operation, err := client.PlannedReparentShard.Create(ctx, &ps.PlannedReparentShardRequest{ | ||
| Organization: ch.Config.Organization, | ||
| Database: database, | ||
| Branch: branch, | ||
| Keyspace: flags.keyspace, | ||
| Shard: flags.shard, | ||
| NewPrimary: flags.newPrimary, | ||
| }) | ||
| if err != nil { | ||
| return cmdutil.HandleError(err) | ||
| } | ||
|
|
||
| if !flags.wait { | ||
| end() | ||
| return ch.Printer.PrintJSON(map[string]string{"id": operation.ID}) | ||
| } | ||
|
|
||
| result, err := waitForPlannedReparentResult(ctx, client, ch.Config.Organization, database, branch, operation) | ||
| if err != nil { | ||
| return cmdutil.HandleError(err) | ||
| } | ||
|
|
||
| end() | ||
| return ch.Printer.PrettyPrintJSON(result) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace name") | ||
| cmd.Flags().StringVar(&flags.shard, "shard", "", "Shard range (e.g., '-80', '80-', or '-' for unsharded)") | ||
| cmd.Flags().StringVar(&flags.newPrimary, "new-primary", "", "Tablet alias to promote as the new primary") | ||
| cmd.Flags().BoolVar(&flags.wait, "wait", true, "Wait for the operation to complete") | ||
| cmd.MarkFlagRequired("keyspace") // nolint:errcheck | ||
| cmd.MarkFlagRequired("shard") // nolint:errcheck | ||
| cmd.MarkFlagRequired("new-primary") // nolint:errcheck | ||
|
|
||
| cmd.AddCommand(plannedReparentShardStatusCmd(ch)) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func plannedReparentShardStatusCmd(ch *cmdutil.Helper) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "status <database> <branch> <operation-id>", | ||
| Short: "Check the status of a planned reparent shard operation", | ||
| Args: cmdutil.RequiredArgs("database", "branch", "operation-id"), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| database, branch, id := args[0], args[1], args[2] | ||
|
|
||
| client, err := ch.Client() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| end := ch.Printer.PrintProgress( | ||
| fmt.Sprintf("Getting PlannedReparentShard operation on %s\u2026", | ||
| progressTarget(ch.Config.Organization, database, branch))) | ||
| defer end() | ||
|
|
||
| operation, err := client.PlannedReparentShard.Get(ctx, &ps.GetPlannedReparentShardRequest{ | ||
| Organization: ch.Config.Organization, | ||
| Database: database, | ||
| Branch: branch, | ||
| ID: id, | ||
| }) | ||
| if err != nil { | ||
| return cmdutil.HandleError(err) | ||
| } | ||
|
|
||
| end() | ||
| return ch.Printer.PrintJSON(operation) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func waitForPlannedReparentResult(ctx context.Context, client *ps.Client, organization, database, branch string, operation *ps.VtctldOperation) (json.RawMessage, error) { | ||
| result, done, err := plannedReparentOperationResult(operation) | ||
| if done || err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| request := &ps.GetPlannedReparentShardRequest{ | ||
| Organization: organization, | ||
| Database: database, | ||
| Branch: branch, | ||
| ID: operation.ID, | ||
| } | ||
|
|
||
| pollCtx, cancel := context.WithTimeout(ctx, plannedReparentOperationTimeout(operation)) | ||
| defer cancel() | ||
| ticker := time.NewTicker(plannedReparentOperationPollInterval) | ||
| defer ticker.Stop() | ||
|
|
||
| for { | ||
| select { | ||
| case <-pollCtx.Done(): | ||
| if errors.Is(pollCtx.Err(), context.DeadlineExceeded) { | ||
| return nil, fmt.Errorf("timed out waiting for planned reparent operation %s to finish", operation.ID) | ||
| } | ||
|
|
||
| return nil, pollCtx.Err() | ||
| case <-ticker.C: | ||
| } | ||
|
|
||
| op, err := client.PlannedReparentShard.Get(pollCtx, request) | ||
| if err != nil { | ||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| return nil, fmt.Errorf("timed out waiting for planned reparent operation %s to finish", operation.ID) | ||
| } | ||
|
|
||
| return nil, err | ||
| } | ||
|
|
||
| result, done, err = plannedReparentOperationResult(op) | ||
| if done || err != nil { | ||
| return result, err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func plannedReparentOperationResult(operation *ps.VtctldOperation) (json.RawMessage, bool, error) { | ||
| if !operation.Completed { | ||
| return nil, false, nil | ||
| } | ||
|
|
||
| switch operation.State { | ||
| case "completed": | ||
| if len(operation.Result) == 0 { | ||
| return json.RawMessage(`{}`), true, nil | ||
| } | ||
|
|
||
| return operation.Result, true, nil | ||
| case "failed", "cancelled": | ||
| if operation.Error != "" { | ||
| return nil, true, errors.New(operation.Error) | ||
| } | ||
|
|
||
| return nil, true, fmt.Errorf("planned reparent operation %s ended in state %q", operation.ID, operation.State) | ||
| default: | ||
| return nil, true, fmt.Errorf("planned reparent operation %s reached unexpected terminal state %q", operation.ID, operation.State) | ||
| } | ||
| } | ||
|
|
||
| func plannedReparentOperationTimeout(operation *ps.VtctldOperation) time.Duration { | ||
| if operation.Timeout > 0 { | ||
| return time.Duration(operation.Timeout)*time.Second + plannedReparentOperationTimeoutBuffer | ||
| } | ||
|
|
||
| return plannedReparentOperationDefaultTimeout | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package mock | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| ps "github.com/planetscale/planetscale-go/planetscale" | ||
| ) | ||
|
|
||
| type PlannedReparentShardService struct { | ||
| CreateFn func(context.Context, *ps.PlannedReparentShardRequest) (*ps.VtctldOperation, error) | ||
| CreateFnInvoked bool | ||
|
|
||
| GetFn func(context.Context, *ps.GetPlannedReparentShardRequest) (*ps.VtctldOperation, error) | ||
| GetFnInvoked bool | ||
| } | ||
|
|
||
| func (s *PlannedReparentShardService) Create(ctx context.Context, req *ps.PlannedReparentShardRequest) (*ps.VtctldOperation, error) { | ||
| s.CreateFnInvoked = true | ||
| return s.CreateFn(ctx, req) | ||
| } | ||
|
|
||
| func (s *PlannedReparentShardService) Get(ctx context.Context, req *ps.GetPlannedReparentShardRequest) (*ps.VtctldOperation, error) { | ||
| s.GetFnInvoked = true | ||
| return s.GetFn(ctx, req) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.