vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/reservedconn/reconnect1/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  	"os"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/test/endtoend/utils"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  
    29  	"github.com/stretchr/testify/require"
    30  
    31  	"vitess.io/vitess/go/mysql"
    32  	"vitess.io/vitess/go/test/endtoend/cluster"
    33  )
    34  
    35  var (
    36  	clusterInstance *cluster.LocalProcessCluster
    37  	vtParams        mysql.ConnParams
    38  	keyspaceName    = "ks"
    39  	cell            = "zone1"
    40  	hostname        = "localhost"
    41  	sqlSchema       = `create table test(id bigint primary key)Engine=InnoDB;`
    42  
    43  	vSchema = `
    44  		{	
    45  			"sharded":true,
    46  			"vindexes": {
    47  				"hash_index": {
    48  					"type": "hash"
    49  				}
    50  			},	
    51  			"tables": {
    52  				"test":{
    53  					"column_vindexes": [
    54  						{
    55  							"column": "id",
    56  							"name": "hash_index"
    57  						}
    58  					]
    59  				}
    60  			}
    61  		}
    62  	`
    63  )
    64  
    65  var enableSettingsPool bool
    66  
    67  func TestMain(m *testing.M) {
    68  	defer cluster.PanicHandler(nil)
    69  	flag.Parse()
    70  
    71  	code := runAllTests(m)
    72  	if code != 0 {
    73  		os.Exit(code)
    74  	}
    75  
    76  	println("running with settings pool enabled")
    77  	// run again with settings pool enabled.
    78  	enableSettingsPool = true
    79  	code = runAllTests(m)
    80  	os.Exit(code)
    81  }
    82  
    83  func runAllTests(m *testing.M) int {
    84  
    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  	if enableSettingsPool {
   100  		clusterInstance.VtTabletExtraArgs = append(clusterInstance.VtTabletExtraArgs, "--queryserver-enable-settings-pool")
   101  	}
   102  	if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, true); err != nil {
   103  		return 1
   104  	}
   105  
   106  	// Start vtgate
   107  	clusterInstance.VtGateExtraArgs = []string{"--lock_heartbeat_time", "2s"}
   108  	if err := clusterInstance.StartVtgate(); err != nil {
   109  		return 1
   110  	}
   111  
   112  	vtParams = mysql.ConnParams{
   113  		Host: clusterInstance.Hostname,
   114  		Port: clusterInstance.VtgateMySQLPort,
   115  	}
   116  	return m.Run()
   117  }
   118  
   119  func TestServingChange(t *testing.T) {
   120  	conn, err := mysql.Connect(context.Background(), &vtParams)
   121  	require.NoError(t, err)
   122  	defer conn.Close()
   123  
   124  	utils.Exec(t, conn, "use @rdonly")
   125  	utils.Exec(t, conn, "set sql_mode = ''")
   126  
   127  	// to see rdonly is available and
   128  	// also this will create reserved connection on rdonly on -80 and 80- shards.
   129  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   130  	for err != nil {
   131  		_, err = utils.ExecAllowError(t, conn, "select * from test")
   132  	}
   133  
   134  	// changing rdonly tablet to spare (non serving).
   135  	rdonlyTablet := clusterInstance.Keyspaces[0].Shards[0].Rdonly()
   136  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("ChangeTabletType", rdonlyTablet.Alias, "replica")
   137  	require.NoError(t, err)
   138  	rdonlyTablet.Type = "replica"
   139  
   140  	// this should fail as there is no rdonly present
   141  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   142  	require.Error(t, err)
   143  
   144  	// changing replica tablet to rdonly to make rdonly available for serving.
   145  	replicaTablet := clusterInstance.Keyspaces[0].Shards[0].Replica()
   146  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("ChangeTabletType", replicaTablet.Alias, "rdonly")
   147  	require.NoError(t, err)
   148  	replicaTablet.Type = "rdonly"
   149  
   150  	// to see/make the new rdonly available
   151  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("Ping", replicaTablet.Alias)
   152  	require.NoError(t, err)
   153  
   154  	// this should pass now as there is rdonly present
   155  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   156  	assert.NoError(t, err)
   157  }
   158  
   159  func TestServingChangeStreaming(t *testing.T) {
   160  	conn, err := mysql.Connect(context.Background(), &vtParams)
   161  	require.NoError(t, err)
   162  	defer conn.Close()
   163  
   164  	utils.Exec(t, conn, "set workload = olap")
   165  	utils.Exec(t, conn, "use @rdonly")
   166  	utils.Exec(t, conn, "set sql_mode = ''")
   167  
   168  	// to see rdonly is available and
   169  	// also this will create reserved connection on rdonly on -80 and 80- shards.
   170  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   171  	for err != nil {
   172  		_, err = utils.ExecAllowError(t, conn, "select * from test")
   173  	}
   174  
   175  	// changing rdonly tablet to spare (non serving).
   176  	rdonlyTablet := clusterInstance.Keyspaces[0].Shards[0].Rdonly()
   177  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("ChangeTabletType", rdonlyTablet.Alias, "replica")
   178  	require.NoError(t, err)
   179  	rdonlyTablet.Type = "replica"
   180  
   181  	// this should fail as there is no rdonly present
   182  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   183  	require.Error(t, err)
   184  
   185  	// changing replica tablet to rdonly to make rdonly available for serving.
   186  	replicaTablet := clusterInstance.Keyspaces[0].Shards[0].Replica()
   187  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("ChangeTabletType", replicaTablet.Alias, "rdonly")
   188  	require.NoError(t, err)
   189  	replicaTablet.Type = "rdonly"
   190  
   191  	// to see/make the new rdonly available
   192  	err = clusterInstance.VtctlclientProcess.ExecuteCommand("Ping", replicaTablet.Alias)
   193  	require.NoError(t, err)
   194  
   195  	// this should pass now as there is rdonly present
   196  	_, err = utils.ExecAllowError(t, conn, "select * from test")
   197  	assert.NoError(t, err)
   198  }