vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/gen4_compare_v3.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 engine
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  
    23  	"vitess.io/vitess/go/sqltypes"
    24  	querypb "vitess.io/vitess/go/vt/proto/query"
    25  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    26  	"vitess.io/vitess/go/vt/vterrors"
    27  )
    28  
    29  // Gen4CompareV3 is a Primitive used to compare V3 and Gen4's plans.
    30  type Gen4CompareV3 struct {
    31  	V3, Gen4   Primitive
    32  	HasOrderBy bool
    33  }
    34  
    35  var _ Primitive = (*Gen4CompareV3)(nil)
    36  var _ Gen4Comparer = (*Gen4CompareV3)(nil)
    37  
    38  // GetGen4Primitive implements the Gen4Comparer interface
    39  func (gc *Gen4CompareV3) GetGen4Primitive() Primitive {
    40  	return gc.Gen4
    41  }
    42  
    43  // RouteType implements the Primitive interface
    44  func (gc *Gen4CompareV3) RouteType() string {
    45  	return gc.Gen4.RouteType()
    46  }
    47  
    48  // GetKeyspaceName implements the Primitive interface
    49  func (gc *Gen4CompareV3) GetKeyspaceName() string {
    50  	return gc.Gen4.GetKeyspaceName()
    51  }
    52  
    53  // GetTableName implements the Primitive interface
    54  func (gc *Gen4CompareV3) GetTableName() string {
    55  	return gc.Gen4.GetTableName()
    56  }
    57  
    58  // GetFields implements the Primitive interface
    59  func (gc *Gen4CompareV3) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
    60  	return gc.Gen4.GetFields(ctx, vcursor, bindVars)
    61  }
    62  
    63  // NeedsTransaction implements the Primitive interface
    64  func (gc *Gen4CompareV3) NeedsTransaction() bool {
    65  	return gc.Gen4.NeedsTransaction()
    66  }
    67  
    68  // TryExecute implements the Primitive interface
    69  func (gc *Gen4CompareV3) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
    70  	var v3Err, gen4Err error
    71  	v3Result, gen4Result := &sqltypes.Result{}, &sqltypes.Result{}
    72  	if gc.Gen4 != nil {
    73  		gen4Result, gen4Err = gc.Gen4.TryExecute(ctx, vcursor, bindVars, wantfields)
    74  	}
    75  	if gc.V3 != nil {
    76  		v3Result, v3Err = gc.V3.TryExecute(ctx, vcursor, bindVars, wantfields)
    77  	}
    78  
    79  	if err := CompareErrors(v3Err, gen4Err, "v3", "Gen4"); err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	if err := gc.compareResults(v3Result, gen4Result); err != nil {
    84  		return nil, err
    85  	}
    86  	return gen4Result, nil
    87  }
    88  
    89  // TryStreamExecute implements the Primitive interface
    90  func (gc *Gen4CompareV3) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
    91  	var mu sync.Mutex
    92  	var v3Err, gen4Err error
    93  	v3Result, gen4Result := &sqltypes.Result{}, &sqltypes.Result{}
    94  
    95  	if gc.Gen4 != nil {
    96  		gen4Err = gc.Gen4.TryStreamExecute(ctx, vcursor, bindVars, wantfields, func(result *sqltypes.Result) error {
    97  			mu.Lock()
    98  			defer mu.Unlock()
    99  			gen4Result.AppendResult(result)
   100  			return nil
   101  		})
   102  	}
   103  	if gc.V3 != nil {
   104  		v3Err = gc.V3.TryStreamExecute(ctx, vcursor, bindVars, wantfields, func(result *sqltypes.Result) error {
   105  			mu.Lock()
   106  			defer mu.Unlock()
   107  			v3Result.AppendResult(result)
   108  			return nil
   109  		})
   110  	}
   111  
   112  	if err := CompareErrors(v3Err, gen4Err, "v3", "Gen4"); err != nil {
   113  		return err
   114  	}
   115  
   116  	if err := gc.compareResults(v3Result, gen4Result); err != nil {
   117  		return err
   118  	}
   119  	return callback(gen4Result)
   120  }
   121  
   122  func (gc *Gen4CompareV3) compareResults(v3Result *sqltypes.Result, gen4Result *sqltypes.Result) error {
   123  	var match bool
   124  	if gc.HasOrderBy {
   125  		match = sqltypes.ResultsEqual([]sqltypes.Result{*v3Result}, []sqltypes.Result{*gen4Result})
   126  	} else {
   127  		match = sqltypes.ResultsEqualUnordered([]sqltypes.Result{*v3Result}, []sqltypes.Result{*gen4Result})
   128  	}
   129  	if !match {
   130  		printMismatch(v3Result, gen4Result, gc.V3, gc.Gen4, "V3", "Gen4")
   131  		return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "results did not match, see VTGate's logs for more information")
   132  	}
   133  	return nil
   134  }
   135  
   136  // Inputs implements the Primitive interface
   137  func (gc *Gen4CompareV3) Inputs() []Primitive {
   138  	return []Primitive{gc.Gen4, gc.V3}
   139  }
   140  
   141  // description implements the Primitive interface
   142  func (gc *Gen4CompareV3) description() PrimitiveDescription {
   143  	return PrimitiveDescription{OperatorType: "Gen4CompareV3"}
   144  }