vitess.io/vitess@v0.16.2/go/test/endtoend/tabletmanager/main_test.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 tabletmanager
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"path"
    25  	"testing"
    26  	"time"
    27  
    28  	"vitess.io/vitess/go/mysql"
    29  	"vitess.io/vitess/go/test/endtoend/cluster"
    30  	tabletpb "vitess.io/vitess/go/vt/proto/topodata"
    31  	tmc "vitess.io/vitess/go/vt/vttablet/grpctmclient"
    32  )
    33  
    34  var (
    35  	clusterInstance                  *cluster.LocalProcessCluster
    36  	tmClient                         *tmc.Client
    37  	primaryTabletParams              mysql.ConnParams
    38  	replicaTabletParams              mysql.ConnParams
    39  	primaryTablet                    cluster.Vttablet
    40  	replicaTablet                    cluster.Vttablet
    41  	rdonlyTablet                     cluster.Vttablet
    42  	hostname                         = "localhost"
    43  	keyspaceName                     = "ks"
    44  	shardName                        = "0"
    45  	keyspaceShard                    = "ks/" + shardName
    46  	dbName                           = "vt_" + keyspaceName
    47  	username                         = "vt_dba"
    48  	cell                             = "zone1"
    49  	tabletHealthcheckRefreshInterval = 5 * time.Second
    50  	tabletUnhealthyThreshold         = tabletHealthcheckRefreshInterval * 2
    51  	sqlSchema                        = `
    52  	create table t1(
    53  		id bigint,
    54  		value varchar(16),
    55  		primary key(id)
    56  	) Engine=InnoDB DEFAULT CHARSET=utf8;
    57  	CREATE VIEW v1 AS SELECT id, value FROM t1;
    58  `
    59  
    60  	vSchema = `
    61  	{
    62      "sharded": true,
    63      "vindexes": {
    64        "hash": {
    65          "type": "hash"
    66        }
    67      },
    68      "tables": {
    69        "t1": {
    70          "column_vindexes": [
    71            {
    72              "column": "id",
    73              "name": "hash"
    74            }
    75          ]
    76        }
    77      }
    78    }`
    79  )
    80  
    81  func TestMain(m *testing.M) {
    82  	defer cluster.PanicHandler(nil)
    83  	flag.Parse()
    84  
    85  	exitCode := func() int {
    86  		clusterInstance = cluster.NewCluster(cell, hostname)
    87  		defer clusterInstance.Teardown()
    88  
    89  		// Start topo server
    90  		err := clusterInstance.StartTopo()
    91  		if err != nil {
    92  			return 1
    93  		}
    94  
    95  		// List of users authorized to execute vschema ddl operations
    96  		clusterInstance.VtGateExtraArgs = []string{
    97  			"--vschema_ddl_authorized_users=%",
    98  			"--discovery_low_replication_lag", tabletUnhealthyThreshold.String(),
    99  		}
   100  		// Set extra tablet args for lock timeout
   101  		clusterInstance.VtTabletExtraArgs = []string{
   102  			"--lock_tables_timeout", "5s",
   103  			"--watch_replication_stream",
   104  			"--heartbeat_enable",
   105  			"--health_check_interval", tabletHealthcheckRefreshInterval.String(),
   106  			"--unhealthy_threshold", tabletUnhealthyThreshold.String(),
   107  		}
   108  
   109  		// Start keyspace
   110  		keyspace := &cluster.Keyspace{
   111  			Name:      keyspaceName,
   112  			SchemaSQL: sqlSchema,
   113  			VSchema:   vSchema,
   114  		}
   115  
   116  		if err = clusterInstance.StartUnshardedKeyspace(*keyspace, 1, true); err != nil {
   117  			return 1
   118  		}
   119  
   120  		// Start vtgate
   121  		if err = clusterInstance.StartVtgate(); err != nil {
   122  			return 1
   123  		}
   124  
   125  		// Collect table paths and ports
   126  		tablets := clusterInstance.Keyspaces[0].Shards[0].Vttablets
   127  		for _, tablet := range tablets {
   128  			if tablet.Type == "primary" {
   129  				primaryTablet = *tablet
   130  			} else if tablet.Type != "rdonly" {
   131  				replicaTablet = *tablet
   132  			} else {
   133  				rdonlyTablet = *tablet
   134  			}
   135  		}
   136  
   137  		// Set mysql tablet params
   138  		primaryTabletParams = mysql.ConnParams{
   139  			Uname:      username,
   140  			DbName:     dbName,
   141  			UnixSocket: path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d/mysql.sock", primaryTablet.TabletUID)),
   142  		}
   143  		replicaTabletParams = mysql.ConnParams{
   144  			Uname:      username,
   145  			DbName:     dbName,
   146  			UnixSocket: path.Join(os.Getenv("VTDATAROOT"), fmt.Sprintf("/vt_%010d/mysql.sock", replicaTablet.TabletUID)),
   147  		}
   148  
   149  		// create tablet manager client
   150  		tmClient = tmc.NewClient()
   151  
   152  		return m.Run()
   153  	}()
   154  	os.Exit(exitCode)
   155  }
   156  
   157  func tmcLockTables(ctx context.Context, tabletGrpcPort int) error {
   158  	vtablet := getTablet(tabletGrpcPort)
   159  	return tmClient.LockTables(ctx, vtablet)
   160  }
   161  
   162  func tmcUnlockTables(ctx context.Context, tabletGrpcPort int) error {
   163  	vtablet := getTablet(tabletGrpcPort)
   164  	return tmClient.UnlockTables(ctx, vtablet)
   165  }
   166  
   167  func tmcStopReplication(ctx context.Context, tabletGrpcPort int) error {
   168  	vtablet := getTablet(tabletGrpcPort)
   169  	return tmClient.StopReplication(ctx, vtablet)
   170  }
   171  
   172  func tmcStartReplication(ctx context.Context, tabletGrpcPort int) error {
   173  	vtablet := getTablet(tabletGrpcPort)
   174  	return tmClient.StartReplication(ctx, vtablet, false)
   175  }
   176  
   177  func tmcResetReplicationParameters(ctx context.Context, tabletGrpcPort int) error {
   178  	vttablet := getTablet(tabletGrpcPort)
   179  	return tmClient.ResetReplicationParameters(ctx, vttablet)
   180  }
   181  
   182  func tmcPrimaryPosition(ctx context.Context, tabletGrpcPort int) (string, error) {
   183  	vtablet := getTablet(tabletGrpcPort)
   184  	return tmClient.PrimaryPosition(ctx, vtablet)
   185  }
   186  
   187  func tmcStartReplicationUntilAfter(ctx context.Context, tabletGrpcPort int, positon string, waittime time.Duration) error {
   188  	vtablet := getTablet(tabletGrpcPort)
   189  	return tmClient.StartReplicationUntilAfter(ctx, vtablet, positon, waittime)
   190  }
   191  
   192  func getTablet(tabletGrpcPort int) *tabletpb.Tablet {
   193  	portMap := make(map[string]int32)
   194  	portMap["grpc"] = int32(tabletGrpcPort)
   195  	return &tabletpb.Tablet{Hostname: hostname, PortMap: portMap}
   196  }