vitess.io/vitess@v0.16.2/go/cmd/topo2topo/topo2topo.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 main
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  
    24  	"github.com/spf13/pflag"
    25  
    26  	"vitess.io/vitess/go/acl"
    27  	"vitess.io/vitess/go/exit"
    28  	"vitess.io/vitess/go/vt/grpccommon"
    29  	"vitess.io/vitess/go/vt/log"
    30  	"vitess.io/vitess/go/vt/logutil"
    31  	"vitess.io/vitess/go/vt/servenv"
    32  	"vitess.io/vitess/go/vt/topo"
    33  	"vitess.io/vitess/go/vt/topo/helpers"
    34  )
    35  
    36  var (
    37  	fromImplementation  string
    38  	fromServerAddress   string
    39  	fromRoot            string
    40  	toImplementation    string
    41  	toServerAddress     string
    42  	toRoot              string
    43  	compare             bool
    44  	doKeyspaces         bool
    45  	doShards            bool
    46  	doShardReplications bool
    47  	doTablets           bool
    48  	doRoutingRules      bool
    49  )
    50  
    51  func init() {
    52  	servenv.OnParse(func(fs *pflag.FlagSet) {
    53  		fs.StringVar(&fromImplementation, "from_implementation", fromImplementation, "topology implementation to copy data from")
    54  		fs.StringVar(&fromServerAddress, "from_server", fromServerAddress, "topology server address to copy data from")
    55  		fs.StringVar(&fromRoot, "from_root", fromRoot, "topology server root to copy data from")
    56  		fs.StringVar(&toImplementation, "to_implementation", toImplementation, "topology implementation to copy data to")
    57  		fs.StringVar(&toServerAddress, "to_server", toServerAddress, "topology server address to copy data to")
    58  		fs.StringVar(&toRoot, "to_root", toRoot, "topology server root to copy data to")
    59  		fs.BoolVar(&compare, "compare", compare, "compares data between topologies")
    60  		fs.BoolVar(&doKeyspaces, "do-keyspaces", doKeyspaces, "copies the keyspace information")
    61  		fs.BoolVar(&doShards, "do-shards", doShards, "copies the shard information")
    62  		fs.BoolVar(&doShardReplications, "do-shard-replications", doShardReplications, "copies the shard replication information")
    63  		fs.BoolVar(&doTablets, "do-tablets", doTablets, "copies the tablet information")
    64  		fs.BoolVar(&doRoutingRules, "do-routing-rules", doRoutingRules, "copies the routing rules")
    65  
    66  		acl.RegisterFlags(fs)
    67  	})
    68  }
    69  
    70  func main() {
    71  	defer exit.RecoverAll()
    72  	defer logutil.Flush()
    73  
    74  	fs := pflag.NewFlagSet("topo2topo", pflag.ExitOnError)
    75  	grpccommon.RegisterFlags(fs)
    76  	log.RegisterFlags(fs)
    77  	logutil.RegisterFlags(fs)
    78  
    79  	servenv.ParseFlags("topo2topo")
    80  
    81  	fromTS, err := topo.OpenServer(fromImplementation, fromServerAddress, fromRoot)
    82  	if err != nil {
    83  		log.Exitf("Cannot open 'from' topo %v: %v", fromImplementation, err)
    84  	}
    85  	toTS, err := topo.OpenServer(toImplementation, toServerAddress, toRoot)
    86  	if err != nil {
    87  		log.Exitf("Cannot open 'to' topo %v: %v", toImplementation, err)
    88  	}
    89  
    90  	ctx := context.Background()
    91  
    92  	if compare {
    93  		compareTopos(ctx, fromTS, toTS)
    94  		return
    95  	}
    96  	copyTopos(ctx, fromTS, toTS)
    97  }
    98  
    99  func copyTopos(ctx context.Context, fromTS, toTS *topo.Server) {
   100  	if doKeyspaces {
   101  		helpers.CopyKeyspaces(ctx, fromTS, toTS)
   102  	}
   103  	if doShards {
   104  		helpers.CopyShards(ctx, fromTS, toTS)
   105  	}
   106  	if doShardReplications {
   107  		helpers.CopyShardReplications(ctx, fromTS, toTS)
   108  	}
   109  	if doTablets {
   110  		helpers.CopyTablets(ctx, fromTS, toTS)
   111  	}
   112  	if doRoutingRules {
   113  		helpers.CopyRoutingRules(ctx, fromTS, toTS)
   114  	}
   115  }
   116  
   117  func compareTopos(ctx context.Context, fromTS, toTS *topo.Server) {
   118  	var err error
   119  	if doKeyspaces {
   120  		err = helpers.CompareKeyspaces(ctx, fromTS, toTS)
   121  		if err != nil {
   122  			log.Exitf("Compare keyspaces failed: %v", err)
   123  		}
   124  	}
   125  	if doShards {
   126  		err = helpers.CompareShards(ctx, fromTS, toTS)
   127  		if err != nil {
   128  			log.Exitf("Compare shards failed: %v", err)
   129  		}
   130  	}
   131  	if doShardReplications {
   132  		err = helpers.CompareShardReplications(ctx, fromTS, toTS)
   133  		if err != nil {
   134  			log.Exitf("Compare shard replications failed: %v", err)
   135  		}
   136  	}
   137  	if doTablets {
   138  		err = helpers.CompareTablets(ctx, fromTS, toTS)
   139  		if err != nil {
   140  			log.Exitf("Compare tablets failed: %v", err)
   141  		}
   142  	}
   143  	if err == nil {
   144  		fmt.Println("Topologies are in sync")
   145  		os.Exit(0)
   146  	}
   147  }