vitess.io/vitess@v0.16.2/go/vt/vtctld/vtctld.go (about)

     1  /*
     2  Copyright 2019 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 vtctld contains all the code to expose a vtctld server
    18  // based on the provided topo.Server.
    19  package vtctld
    20  
    21  import (
    22  	"context"
    23  
    24  	"github.com/spf13/pflag"
    25  
    26  	"vitess.io/vitess/go/vt/servenv"
    27  
    28  	"vitess.io/vitess/go/acl"
    29  	"vitess.io/vitess/go/vt/discovery"
    30  	"vitess.io/vitess/go/vt/log"
    31  	"vitess.io/vitess/go/vt/topo"
    32  	"vitess.io/vitess/go/vt/vtctl"
    33  	"vitess.io/vitess/go/vt/wrangler"
    34  
    35  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    36  	vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
    37  )
    38  
    39  var (
    40  	enableRealtimeStats = false
    41  	durabilityPolicy    = "none"
    42  	sanitizeLogMessages = false
    43  )
    44  
    45  func init() {
    46  	for _, cmd := range []string{"vtcombo", "vtctld"} {
    47  		servenv.OnParseFor(cmd, registerVtctldFlags)
    48  	}
    49  }
    50  
    51  func registerVtctldFlags(fs *pflag.FlagSet) {
    52  	fs.StringVar(&durabilityPolicy, "durability_policy", durabilityPolicy, "type of durability to enforce. Default is none. Other values are dictated by registered plugins")
    53  	fs.BoolVar(&sanitizeLogMessages, "vtctld_sanitize_log_messages", sanitizeLogMessages, "When true, vtctld sanitizes logging.")
    54  }
    55  
    56  // InitVtctld initializes all the vtctld functionality.
    57  func InitVtctld(ts *topo.Server) error {
    58  	actionRepo := NewActionRepository(ts)
    59  
    60  	// keyspace actions
    61  	actionRepo.RegisterKeyspaceAction("ValidateKeyspace",
    62  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace string) (string, error) {
    63  			return "", wr.ValidateKeyspace(ctx, keyspace, false)
    64  		})
    65  
    66  	actionRepo.RegisterKeyspaceAction("ValidateSchemaKeyspace",
    67  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace string) (string, error) {
    68  			return "", wr.ValidateSchemaKeyspace(ctx, keyspace, nil /*excludeTables*/, false /*includeViews*/, false /*skipNoPrimary*/, false /*includeVSchema*/)
    69  		})
    70  
    71  	actionRepo.RegisterKeyspaceAction("ValidateVersionKeyspace",
    72  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace string) (string, error) {
    73  			return "", wr.ValidateVersionKeyspace(ctx, keyspace)
    74  		})
    75  
    76  	actionRepo.RegisterKeyspaceAction("ValidatePermissionsKeyspace",
    77  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace string) (string, error) {
    78  			return "", wr.ValidatePermissionsKeyspace(ctx, keyspace)
    79  		})
    80  
    81  	// shard actions
    82  	actionRepo.RegisterShardAction("ValidateShard",
    83  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string) (string, error) {
    84  			return "", wr.ValidateShard(ctx, keyspace, shard, false)
    85  		})
    86  
    87  	actionRepo.RegisterShardAction("ValidateSchemaShard",
    88  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string) (string, error) {
    89  			return "", wr.ValidateSchemaShard(ctx, keyspace, shard, nil, false, false /*includeVSchema*/)
    90  		})
    91  
    92  	actionRepo.RegisterShardAction("ValidateVersionShard",
    93  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string) (string, error) {
    94  			return "", wr.ValidateVersionShard(ctx, keyspace, shard)
    95  		})
    96  
    97  	actionRepo.RegisterShardAction("ValidatePermissionsShard",
    98  		func(ctx context.Context, wr *wrangler.Wrangler, keyspace, shard string) (string, error) {
    99  			return "", wr.ValidatePermissionsShard(ctx, keyspace, shard)
   100  		})
   101  
   102  	// tablet actions
   103  	actionRepo.RegisterTabletAction("Ping", "",
   104  		func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias) (string, error) {
   105  			ti, err := wr.TopoServer().GetTablet(ctx, tabletAlias)
   106  			if err != nil {
   107  				return "", err
   108  			}
   109  			return "", wr.TabletManagerClient().Ping(ctx, ti.Tablet)
   110  		})
   111  
   112  	actionRepo.RegisterTabletAction("RefreshState", acl.ADMIN,
   113  		func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias) (string, error) {
   114  			ti, err := wr.TopoServer().GetTablet(ctx, tabletAlias)
   115  			if err != nil {
   116  				return "", err
   117  			}
   118  			return "", wr.TabletManagerClient().RefreshState(ctx, ti.Tablet)
   119  		})
   120  
   121  	actionRepo.RegisterTabletAction("DeleteTablet", acl.ADMIN,
   122  		func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias) (string, error) {
   123  			return "", wr.DeleteTablet(ctx, tabletAlias, false)
   124  		})
   125  
   126  	actionRepo.RegisterTabletAction("ReloadSchema", acl.ADMIN,
   127  		func(ctx context.Context, wr *wrangler.Wrangler, tabletAlias *topodatapb.TabletAlias) (string, error) {
   128  			_, err := wr.VtctldServer().ReloadSchema(ctx, &vtctldatapb.ReloadSchemaRequest{
   129  				TabletAlias: tabletAlias,
   130  			})
   131  			return "", err
   132  		})
   133  
   134  	var healthCheck discovery.HealthCheck
   135  	if enableRealtimeStats {
   136  		ctx := context.Background()
   137  		cells, err := ts.GetKnownCells(ctx)
   138  		if err != nil {
   139  			log.Errorf("Failed to get the list of known cells, failed to instantiate the healthcheck at startup: %v", err)
   140  		} else {
   141  			healthCheck = vtctl.NewHealthCheck(ctx, ts, localCell, cells)
   142  		}
   143  	}
   144  
   145  	// Serve the REST API
   146  	initAPI(context.Background(), ts, actionRepo, healthCheck)
   147  
   148  	// Serve the topology endpoint in the REST API at /topodata
   149  	initExplorer(ts)
   150  
   151  	return nil
   152  }