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

     1  /*
     2  Copyright 2022 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  	"fmt"
    22  	"sync"
    23  
    24  	"vitess.io/vitess/go/vt/vtctl/workflow"
    25  
    26  	"vitess.io/vitess/go/vt/log"
    27  
    28  	vdiff2 "vitess.io/vitess/go/vt/vttablet/tabletmanager/vdiff"
    29  
    30  	"vitess.io/vitess/go/vt/proto/tabletmanagerdata"
    31  )
    32  
    33  type VDiffOutput struct {
    34  	mu        sync.Mutex
    35  	Request   *tabletmanagerdata.VDiffRequest
    36  	Responses map[string]*tabletmanagerdata.VDiffResponse
    37  	Err       error
    38  }
    39  
    40  func (wr *Wrangler) VDiff2(ctx context.Context, keyspace, workflowName string, action vdiff2.VDiffAction, actionArg, uuid string,
    41  	options *tabletmanagerdata.VDiffOptions) (*VDiffOutput, error) {
    42  
    43  	log.Infof("VDiff2 called with %s, %s, %s, %s, %s, %+v", keyspace, workflowName, action, actionArg, uuid, options)
    44  
    45  	req := &tabletmanagerdata.VDiffRequest{
    46  		Keyspace:  keyspace,
    47  		Workflow:  workflowName,
    48  		Action:    string(action),
    49  		ActionArg: actionArg,
    50  		Options:   options,
    51  		VdiffUuid: uuid,
    52  	}
    53  	output := &VDiffOutput{
    54  		Request:   req,
    55  		Responses: make(map[string]*tabletmanagerdata.VDiffResponse),
    56  		Err:       nil,
    57  	}
    58  
    59  	ts, err := wr.buildTrafficSwitcher(ctx, keyspace, workflowName)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	if action == vdiff2.CreateAction && ts.frozen {
    64  		return nil, fmt.Errorf("invalid VDiff run: writes have been already been switched for workflow %s.%s",
    65  			keyspace, workflowName)
    66  	}
    67  
    68  	output.Err = ts.ForAllTargets(func(target *workflow.MigrationTarget) error {
    69  		resp, err := wr.tmc.VDiff(ctx, target.GetPrimary().Tablet, req)
    70  		output.mu.Lock()
    71  		defer output.mu.Unlock()
    72  		output.Responses[target.GetShard().ShardName()] = resp
    73  		return err
    74  	})
    75  	if output.Err != nil {
    76  		log.Errorf("Error executing action %s: %v", action, output.Err)
    77  		return nil, output.Err
    78  	}
    79  
    80  	return output, nil
    81  }