vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/reservedconn/reconnect2/main_test.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 reservedconn
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  
    26  	"vitess.io/vitess/go/test/endtoend/utils"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  
    30  	"github.com/stretchr/testify/require"
    31  
    32  	"vitess.io/vitess/go/mysql"
    33  	"vitess.io/vitess/go/test/endtoend/cluster"
    34  )
    35  
    36  var (
    37  	clusterInstance *cluster.LocalProcessCluster
    38  	vtParams        mysql.ConnParams
    39  	keyspaceName    = "ks"
    40  	cell            = "zone1"
    41  	hostname        = "localhost"
    42  	sqlSchema       = `create table test(id bigint primary key)Engine=InnoDB;`
    43  
    44  	vSchema = `
    45  		{	
    46  			"sharded":true,
    47  			"vindexes": {
    48  				"hash_index": {
    49  					"type": "hash"
    50  				}
    51  			},	
    52  			"tables": {
    53  				"test":{
    54  					"column_vindexes": [
    55  						{
    56  							"column": "id",
    57  							"name": "hash_index"
    58  						}
    59  					]
    60  				}
    61  			}
    62  		}
    63  	`
    64  )
    65  
    66  var enableSettingsPool bool
    67  
    68  func TestMain(m *testing.M) {
    69  	defer cluster.PanicHandler(nil)
    70  	flag.Parse()
    71  
    72  	code := runAllTests(m)
    73  	if code != 0 {
    74  		os.Exit(code)
    75  	}
    76  
    77  	println("running with settings pool enabled")
    78  	// run again with settings pool enabled.
    79  	enableSettingsPool = true
    80  	code = runAllTests(m)
    81  	os.Exit(code)
    82  }
    83  
    84  func runAllTests(m *testing.M) int {
    85  	clusterInstance = cluster.NewCluster(cell, hostname)
    86  	defer clusterInstance.Teardown()
    87  
    88  	// Start topo server
    89  	if err := clusterInstance.StartTopo(); err != nil {
    90  		return 1
    91  	}
    92  
    93  	// Start keyspace
    94  	keyspace := &cluster.Keyspace{
    95  		Name:      keyspaceName,
    96  		SchemaSQL: sqlSchema,
    97  		VSchema:   vSchema,
    98  	}
    99  	clusterInstance.VtTabletExtraArgs = []string{"--queryserver-config-transaction-timeout", "5"}
   100  	if enableSettingsPool {
   101  		clusterInstance.VtTabletExtraArgs = append(clusterInstance.VtTabletExtraArgs, "--queryserver-enable-settings-pool")
   102  	}
   103  	if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, true); err != nil {
   104  		return 1
   105  	}
   106  
   107  	// Start vtgate
   108  	clusterInstance.VtGateExtraArgs = []string{"--lock_heartbeat_time", "2s"}
   109  	if err := clusterInstance.StartVtgate(); err != nil {
   110  		return 1
   111  	}
   112  
   113  	vtParams = mysql.ConnParams{
   114  		Host: clusterInstance.Hostname,
   115  		Port: clusterInstance.VtgateMySQLPort,
   116  	}
   117  	return m.Run()
   118  }
   119  
   120  func TestTabletChange(t *testing.T) {
   121  	conn, err := mysql.Connect(context.Background(), &vtParams)
   122  	require.NoError(t, err)
   123  	defer conn.Close()
   124  
   125  	utils.Exec(t, conn, "use @primary")
   126  	utils.Exec(t, conn, "set sql_mode = ''")
   127  
   128  	// this will create reserved connection on primary on -80 and 80- shards.
   129  	utils.Exec(t, conn, "select * from test")
   130  
   131  	// Change Primary
   132  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("PlannedReparentShard", "--", "--keyspace_shard", fmt.Sprintf("%s/%s", keyspaceName, "-80"))
   133  	require.NoError(t, err)
   134  
   135  	// this should pass as there is a new primary tablet and is serving.
   136  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   137  	assert.NoError(t, err)
   138  }
   139  
   140  func TestTabletChangeStreaming(t *testing.T) {
   141  	conn, err := mysql.Connect(context.Background(), &vtParams)
   142  	require.NoError(t, err)
   143  	defer conn.Close()
   144  
   145  	utils.Exec(t, conn, "set workload = olap")
   146  	utils.Exec(t, conn, "use @primary")
   147  	utils.Exec(t, conn, "set sql_mode = ''")
   148  
   149  	// this will create reserved connection on primary on -80 and 80- shards.
   150  	utils.Exec(t, conn, "select * from test")
   151  
   152  	// Change Primary
   153  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("PlannedReparentShard", "--", "--keyspace_shard", fmt.Sprintf("%s/%s", keyspaceName, "-80"))
   154  	require.NoError(t, err)
   155  
   156  	// this should pass as there is a new primary tablet and is serving.
   157  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   158  	assert.NoError(t, err)
   159  }