vitess.io/vitess@v0.16.2/go/vt/wrangler/validator.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 wrangler
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  
    23  	"vitess.io/vitess/go/vt/logutil"
    24  
    25  	vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
    26  )
    27  
    28  // consumeValidationResults consumes results from Validate(Keyspace|Shard)? methods.
    29  // If there are any results (synonymous with "validation failure") then the
    30  // overall method returns a generic error instructing the user to look in the
    31  // vtctld logs, and each validation failure is logged at the error level.
    32  func consumeValidationResults(logger logutil.Logger, results []string) (err error) {
    33  	for _, result := range results {
    34  		if err == nil {
    35  			err = errors.New("some validation errors - see log")
    36  		}
    37  
    38  		logger.Error(errors.New(result))
    39  	}
    40  
    41  	return err
    42  }
    43  
    44  // Validate a whole TopologyServer tree
    45  func (wr *Wrangler) Validate(ctx context.Context, pingTablets bool) error {
    46  	resp, err := wr.VtctldServer().Validate(ctx, &vtctldatapb.ValidateRequest{
    47  		PingTablets: pingTablets,
    48  	})
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	aggrResults := resp.Results
    54  	for _, keyspaceResults := range resp.ResultsByKeyspace {
    55  		aggrResults = append(aggrResults, keyspaceResults.Results...)
    56  		for _, shardResults := range keyspaceResults.ResultsByShard {
    57  			aggrResults = append(aggrResults, shardResults.Results...)
    58  		}
    59  	}
    60  
    61  	return consumeValidationResults(wr.Logger(), aggrResults)
    62  }
    63  
    64  // ValidateKeyspace will validate a bunch of information in a keyspace
    65  // is correct.
    66  func (wr *Wrangler) ValidateKeyspace(ctx context.Context, keyspace string, pingTablets bool) error {
    67  	resp, err := wr.VtctldServer().ValidateKeyspace(ctx, &vtctldatapb.ValidateKeyspaceRequest{
    68  		Keyspace:    keyspace,
    69  		PingTablets: pingTablets,
    70  	})
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	aggrResults := resp.Results
    76  	for _, shardResults := range resp.ResultsByShard {
    77  		aggrResults = append(aggrResults, shardResults.Results...)
    78  	}
    79  
    80  	return consumeValidationResults(wr.Logger(), aggrResults)
    81  }
    82  
    83  // ValidateShard will validate a bunch of information in a shard is correct.
    84  func (wr *Wrangler) ValidateShard(ctx context.Context, keyspace, shard string, pingTablets bool) error {
    85  	resp, err := wr.VtctldServer().ValidateShard(ctx, &vtctldatapb.ValidateShardRequest{
    86  		Keyspace:    keyspace,
    87  		Shard:       shard,
    88  		PingTablets: pingTablets,
    89  	})
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	return consumeValidationResults(wr.Logger(), resp.Results)
    95  }