vitess.io/vitess@v0.16.2/go/cmd/vtctldclient/command/serving_graph.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package command
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	"vitess.io/vitess/go/cmd/vtctldclient/cli"
    25  
    26  	vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
    27  )
    28  
    29  var (
    30  	// DeleteSrvVSchema makes a DeleteSrvVSchema gRPC call to a vtctld.
    31  	DeleteSrvVSchema = &cobra.Command{
    32  		Use:                   "DeleteSrvVSchema <cell>",
    33  		Short:                 "Deletes the SrvVSchema object in the given cell.",
    34  		DisableFlagsInUseLine: true,
    35  		Args:                  cobra.ExactArgs(1),
    36  		RunE:                  commandDeleteSrvVSchema,
    37  	}
    38  	// GetSrvKeyspaceNames makes a GetSrvKeyspaceNames gRPC call to a vtctld.
    39  	GetSrvKeyspaceNames = &cobra.Command{
    40  		Use:                   "GetSrvKeyspaceNames [<cell> ...]",
    41  		Short:                 "Outputs a JSON mapping of cell=>keyspace names served in that cell. Omit to query all cells.",
    42  		DisableFlagsInUseLine: true,
    43  		Args:                  cobra.ArbitraryArgs,
    44  		RunE:                  commandGetSrvKeyspaceNames,
    45  	}
    46  	// GetSrvKeyspaces makes a GetSrvKeyspaces gRPC call to a vtctld.
    47  	GetSrvKeyspaces = &cobra.Command{
    48  		Use:                   "GetSrvKeyspaces <keyspace> [<cell> ...]",
    49  		Short:                 "Returns the SrvKeyspaces for the given keyspace in one or more cells.",
    50  		DisableFlagsInUseLine: true,
    51  		Args:                  cobra.MinimumNArgs(1),
    52  		RunE:                  commandGetSrvKeyspaces,
    53  	}
    54  	// GetSrvVSchema makes a GetSrvVSchema gRPC call to a vtctld.
    55  	GetSrvVSchema = &cobra.Command{
    56  		Use:                   "GetSrvVSchema cell",
    57  		Short:                 "Returns the SrvVSchema for the given cell.",
    58  		DisableFlagsInUseLine: true,
    59  		Args:                  cobra.ExactArgs(1),
    60  		RunE:                  commandGetSrvVSchema,
    61  	}
    62  	// GetSrvVSchemas makes a GetSrvVSchemas gRPC call to a vtctld.
    63  	GetSrvVSchemas = &cobra.Command{
    64  		Use:                   "GetSrvVSchemas [<cell> ...]",
    65  		Short:                 "Returns the SrvVSchema for all cells, optionally filtered by the given cells.",
    66  		DisableFlagsInUseLine: true,
    67  		Args:                  cobra.ArbitraryArgs,
    68  		RunE:                  commandGetSrvVSchemas,
    69  	}
    70  	// RebuildKeyspaceGraph makes one or more RebuildKeyspaceGraph gRPC calls to a vtctld.
    71  	RebuildKeyspaceGraph = &cobra.Command{
    72  		Use:                   "RebuildKeyspaceGraph [--cells=c1,c2,...] [--allow-partial] ks1 [ks2 ...]",
    73  		Short:                 "Rebuilds the serving data for the keyspace(s). This command may trigger an update to all connected clients.",
    74  		DisableFlagsInUseLine: true,
    75  		Args:                  cobra.MinimumNArgs(1),
    76  		RunE:                  commandRebuildKeyspaceGraph,
    77  	}
    78  	// RebuildVSchemaGraph makes a RebuildVSchemaGraph gRPC call to a vtctld.
    79  	RebuildVSchemaGraph = &cobra.Command{
    80  		Use:                   "RebuildVSchemaGraph [--cells=c1,c2,...]",
    81  		Short:                 "Rebuilds the cell-specific SrvVSchema from the global VSchema objects in the provided cells (or all cells if none provided).",
    82  		DisableFlagsInUseLine: true,
    83  		Args:                  cobra.NoArgs,
    84  		RunE:                  commandRebuildVSchemaGraph,
    85  	}
    86  )
    87  
    88  func commandDeleteSrvVSchema(cmd *cobra.Command, args []string) error {
    89  	cli.FinishedParsing(cmd)
    90  
    91  	cell := cmd.Flags().Arg(0)
    92  	_, err := client.DeleteSrvVSchema(commandCtx, &vtctldatapb.DeleteSrvVSchemaRequest{
    93  		Cell: cell,
    94  	})
    95  
    96  	return err
    97  }
    98  
    99  func commandGetSrvKeyspaceNames(cmd *cobra.Command, args []string) error {
   100  	cli.FinishedParsing(cmd)
   101  
   102  	cells := cmd.Flags().Args()
   103  	resp, err := client.GetSrvKeyspaceNames(commandCtx, &vtctldatapb.GetSrvKeyspaceNamesRequest{
   104  		Cells: cells,
   105  	})
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	data, err := cli.MarshalJSON(resp.Names)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	fmt.Printf("%s\n", data)
   116  	return nil
   117  }
   118  
   119  func commandGetSrvKeyspaces(cmd *cobra.Command, args []string) error {
   120  	cli.FinishedParsing(cmd)
   121  
   122  	keyspace := cmd.Flags().Arg(0)
   123  	cells := cmd.Flags().Args()[1:]
   124  
   125  	resp, err := client.GetSrvKeyspaces(commandCtx, &vtctldatapb.GetSrvKeyspacesRequest{
   126  		Keyspace: keyspace,
   127  		Cells:    cells,
   128  	})
   129  	if err != nil {
   130  		return err
   131  	}
   132  
   133  	data, err := cli.MarshalJSON(resp.SrvKeyspaces)
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	fmt.Printf("%s\n", data)
   139  
   140  	return nil
   141  }
   142  
   143  func commandGetSrvVSchema(cmd *cobra.Command, args []string) error {
   144  	cli.FinishedParsing(cmd)
   145  
   146  	cell := cmd.Flags().Arg(0)
   147  
   148  	resp, err := client.GetSrvVSchema(commandCtx, &vtctldatapb.GetSrvVSchemaRequest{
   149  		Cell: cell,
   150  	})
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	data, err := cli.MarshalJSON(resp.SrvVSchema)
   156  	if err != nil {
   157  		return err
   158  	}
   159  
   160  	fmt.Printf("%s\n", data)
   161  
   162  	return nil
   163  }
   164  
   165  func commandGetSrvVSchemas(cmd *cobra.Command, args []string) error {
   166  	cli.FinishedParsing(cmd)
   167  
   168  	cells := cmd.Flags().Args()[0:]
   169  
   170  	resp, err := client.GetSrvVSchemas(commandCtx, &vtctldatapb.GetSrvVSchemasRequest{
   171  		Cells: cells,
   172  	})
   173  	if err != nil {
   174  		return err
   175  	}
   176  
   177  	// By default, an empty array will serialize as `null`, but `[]` is a little nicer.
   178  	data := []byte("[]")
   179  
   180  	if len(resp.SrvVSchemas) > 0 {
   181  		data, err = cli.MarshalJSON(resp.SrvVSchemas)
   182  		if err != nil {
   183  			return err
   184  		}
   185  	}
   186  
   187  	fmt.Printf("%s\n", data)
   188  
   189  	return nil
   190  }
   191  
   192  var rebuildKeyspaceGraphOptions = struct {
   193  	Cells        []string
   194  	AllowPartial bool
   195  }{}
   196  
   197  func commandRebuildKeyspaceGraph(cmd *cobra.Command, args []string) error {
   198  	cli.FinishedParsing(cmd)
   199  
   200  	keyspaces := cmd.Flags().Args()
   201  	for _, ks := range keyspaces {
   202  		_, err := client.RebuildKeyspaceGraph(commandCtx, &vtctldatapb.RebuildKeyspaceGraphRequest{
   203  			Keyspace:     ks,
   204  			Cells:        rebuildKeyspaceGraphOptions.Cells,
   205  			AllowPartial: rebuildKeyspaceGraphOptions.AllowPartial,
   206  		})
   207  		if err != nil {
   208  			return fmt.Errorf("RebuildKeyspaceGraph(%v) failed: %v", ks, err)
   209  		}
   210  	}
   211  
   212  	return nil
   213  }
   214  
   215  var rebuildVSchemaGraphOptions = struct {
   216  	Cells []string
   217  }{}
   218  
   219  func commandRebuildVSchemaGraph(cmd *cobra.Command, args []string) error {
   220  	cli.FinishedParsing(cmd)
   221  
   222  	_, err := client.RebuildVSchemaGraph(commandCtx, &vtctldatapb.RebuildVSchemaGraphRequest{
   223  		Cells: rebuildVSchemaGraphOptions.Cells,
   224  	})
   225  	if err != nil {
   226  		return err
   227  	}
   228  
   229  	fmt.Println("RebuildVSchemaGraph: ok")
   230  
   231  	return nil
   232  }
   233  
   234  func init() {
   235  	Root.AddCommand(DeleteSrvVSchema)
   236  
   237  	Root.AddCommand(GetSrvKeyspaceNames)
   238  	Root.AddCommand(GetSrvKeyspaces)
   239  	Root.AddCommand(GetSrvVSchema)
   240  	Root.AddCommand(GetSrvVSchemas)
   241  
   242  	RebuildKeyspaceGraph.Flags().StringSliceVarP(&rebuildKeyspaceGraphOptions.Cells, "cells", "c", nil, "Specifies a comma-separated list of cells to update.")
   243  	RebuildKeyspaceGraph.Flags().BoolVar(&rebuildKeyspaceGraphOptions.AllowPartial, "allow-partial", false, "Specifies whether a SNAPSHOT keyspace is allowed to serve with an incomplete set of shards. Ignored for all other types of keyspaces.")
   244  	Root.AddCommand(RebuildKeyspaceGraph)
   245  
   246  	RebuildVSchemaGraph.Flags().StringSliceVarP(&rebuildVSchemaGraphOptions.Cells, "cells", "c", nil, "Specifies a comma-separated list of cells to look for tablets.")
   247  	Root.AddCommand(RebuildVSchemaGraph)
   248  }