Skip to content
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

Add OnlineDDL show support #13738

Merged
merged 26 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions go/cmd/vtctldclient/command/onlineddl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package command

import (
"fmt"
"os"
"strings"
"time"

"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/vtctl/schematools"

vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

var (
OnlineDDL = &cobra.Command{
Use: "OnlineDDL <cmd> <keyspace> [args]",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately cobra won't support a positional argument before the subcommand name (or at least i couldn't see an easy/obvious way)

Short: "Operates on online DDL (schema migrations).",
DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(2),
}
OnlineDDLShow = &cobra.Command{
Use: "show",
Short: "Display information about online DDL operations.",
Example: `OnlineDDL show test_keyspace 82fa54ac_e83e_11ea_96b7_f875a4d24e90
OnlineDDL show test_keyspace all
OnlineDDL show --order descending test_keyspace all
OnlineDDL show --limit 10 test_keyspace all
OnlineDDL show --skip 5 --limit 10 test_keyspace all
OnlineDDL show test_keyspace running
OnlineDDL show test_keyspace complete
OnlineDDL show test_keyspace failed`,
DisableFlagsInUseLine: true,
Args: cobra.RangeArgs(1, 2),
RunE: commandOnlineDDLShow,
}
)

var onlineDDLShowArgs = struct {
JSON bool
OrderStr string
Limit uint64
Skip uint64
}{
OrderStr: "ascending",
}

func commandOnlineDDLShow(cmd *cobra.Command, args []string) error {
var order vtctldatapb.QueryOrdering
switch strings.ToLower(onlineDDLShowArgs.OrderStr) {
case "":
order = vtctldatapb.QueryOrdering_NONE
case "asc", "ascending":
order = vtctldatapb.QueryOrdering_ASCENDING
case "desc", "descending":
order = vtctldatapb.QueryOrdering_DESCENDING
default:
return fmt.Errorf("invalid ordering %s (choices are 'asc', 'ascending', 'desc', 'descending')", onlineDDLShowArgs.OrderStr)
}

cli.FinishedParsing(cmd)

req := &vtctldatapb.GetSchemaMigrationsRequest{
Keyspace: cmd.Flags().Arg(0),
Order: order,
Limit: onlineDDLShowArgs.Limit,
Skip: onlineDDLShowArgs.Skip,
}

switch arg := cmd.Flags().Arg(1); arg {
case "", "all":
case "recent":
req.Recent = protoutil.DurationToProto(7 * 24 * time.Hour)
default:
if status, err := schematools.ParseSchemaMigrationStatus(arg); err == nil {
// Argument is a status name.
req.Status = status
} else if schema.IsOnlineDDLUUID(arg) {
req.Uuid = arg
} else {
req.MigrationContext = arg
}
}

resp, err := client.GetSchemaMigrations(commandCtx, req)
if err != nil {
return err
}

switch {
case onlineDDLShowArgs.JSON:
data, err := cli.MarshalJSON(resp)
if err != nil {
return err
}
fmt.Printf("%s\n", data)
default:
res, err := sqltypes.MarshalResult(schematools.MarshallableSchemaMigrations(resp.Migrations))
if err != nil {
return err
}

cli.WriteQueryResultTable(os.Stdout, res)
}
return nil
}

func init() {
OnlineDDLShow.Flags().BoolVar(&onlineDDLShowArgs.JSON, "json", false, "Output JSON instead of human-readable table.")
OnlineDDLShow.Flags().StringVar(&onlineDDLShowArgs.OrderStr, "order", "asc", "Sort the results by `id` property of the Schema migration.")
OnlineDDLShow.Flags().Uint64Var(&onlineDDLShowArgs.Limit, "limit", 0, "Limit number of rows returned in output.")
OnlineDDLShow.Flags().Uint64Var(&onlineDDLShowArgs.Skip, "skip", 0, "Skip specified number of rows returned in output.")

OnlineDDL.AddCommand(OnlineDDLShow)
Root.AddCommand(OnlineDDL)
}
1 change: 1 addition & 0 deletions go/flags/endtoend/vtctldclient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Available Commands:
GetWorkflows Gets all vreplication workflows (Reshard, MoveTables, etc) in the given keyspace.
LegacyVtctlCommand Invoke a legacy vtctlclient command. Flag parsing is best effort.
MoveTables Perform commands related to moving tables from a source keyspace to a target keyspace.
OnlineDDL Operates on online DDL (schema migrations).
PingTablet Checks that the specified tablet is awake and responding to RPCs. This command can be blocked by other in-flight operations.
PlannedReparentShard Reparents the shard to a new primary, or away from an old primary. Both the old and new primaries must be up and running.
RebuildKeyspaceGraph Rebuilds the serving data for the keyspace(s). This command may trigger an update to all connected clients.
Expand Down
Loading
Loading