vitess.io/vitess@v0.16.2/go/test/endtoend/messaging/main_test.go (about)

     1  /*
     2  Copyright 2020 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 messaging
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/test/endtoend/cluster"
    26  	_ "vitess.io/vitess/go/vt/vtgate/grpcvtgateconn"
    27  )
    28  
    29  var (
    30  	clusterInstance      *cluster.LocalProcessCluster
    31  	shard0Primary        *cluster.Vttablet
    32  	shard0Replica        *cluster.Vttablet
    33  	shard1Primary        *cluster.Vttablet
    34  	lookupPrimary        *cluster.Vttablet
    35  	hostname             = "localhost"
    36  	cell                 = "zone1"
    37  	userKeyspace         = "user"
    38  	lookupKeyspace       = "lookup"
    39  	createShardedMessage = `create table sharded_message(
    40  		# required columns
    41  		id bigint NOT NULL COMMENT 'often an event id, can also be auto-increment or a sequence',
    42  		priority tinyint NOT NULL DEFAULT '50' COMMENT 'lower number priorities process first',
    43  		epoch bigint NOT NULL DEFAULT '0' COMMENT 'Vitess increments this each time it sends a message, and is used for incremental backoff doubling',
    44  		time_next bigint DEFAULT 0 COMMENT 'the earliest time the message will be sent in epoch nanoseconds. Must be null if time_acked is set',
    45  		time_acked bigint DEFAULT NULL COMMENT 'the time the message was acked in epoch nanoseconds. Must be null if time_next is set',
    46  
    47  		# add as many custom fields here as required
    48  		# optional - these are suggestions
    49  		tenant_id bigint,
    50  		message json,
    51  
    52  		# required indexes
    53  		primary key(id),
    54  		index poller_idx(time_acked, priority, time_next desc)
    55  
    56  		# add any secondary indexes or foreign keys - no restrictions
    57  	) comment 'vitess_message,vt_ack_wait=1,vt_purge_after=3,vt_batch_size=2,vt_cache_size=10,vt_poller_interval=1'`
    58  	createUnshardedMessage = `create table unsharded_message(
    59  		# required columns
    60  		id bigint NOT NULL COMMENT 'often an event id, can also be auto-increment or a sequence',
    61  		priority tinyint NOT NULL DEFAULT '50' COMMENT 'lower number priorities process first',
    62  		epoch bigint NOT NULL DEFAULT '0' COMMENT 'Vitess increments this each time it sends a message, and is used for incremental backoff doubling',
    63  		time_next bigint DEFAULT 0 COMMENT 'the earliest time the message will be sent in epoch nanoseconds. Must be null if time_acked is set',
    64  		time_acked bigint DEFAULT NULL COMMENT 'the time the message was acked in epoch nanoseconds. Must be null if time_next is set',
    65  
    66  		# add as many custom fields here as required
    67  		# optional - these are suggestions
    68  		tenant_id bigint,
    69  		message json,
    70  
    71  		# required indexes
    72  		primary key(id),
    73  		index poller_idx(time_acked, priority, time_next desc)
    74  
    75  		# add any secondary indexes or foreign keys - no restrictions
    76  	) comment 'vitess_message,vt_ack_wait=1,vt_purge_after=3,vt_batch_size=2,vt_cache_size=10,vt_poller_interval=1'`
    77  	userVschema = `{
    78  	  "sharded": true,
    79  	  "vindexes": {
    80  			"hash_index": {
    81  			  "type": "hash"
    82  			}
    83  	  },
    84  	  "tables": {
    85  			"sharded_message": {
    86  			  "column_vindexes": [
    87  					{
    88  					  "column": "id",
    89  					  "name": "hash_index"
    90  					}
    91  			  ]
    92  			}
    93  	  }
    94  	}`
    95  	lookupVschema = `{
    96  	  "sharded": false,
    97  	  "tables": {
    98  			"unsharded_message": {},
    99  			"vitess_message": {},
   100  			"vitess_message3": {},
   101  			"vitess_message4": {}
   102  	  }
   103  	}`
   104  )
   105  
   106  func TestMain(m *testing.M) {
   107  	defer cluster.PanicHandler(nil)
   108  	flag.Parse()
   109  
   110  	exitcode, err := func() (int, error) {
   111  		clusterInstance = cluster.NewCluster(cell, hostname)
   112  		defer clusterInstance.Teardown()
   113  
   114  		// Start topo server
   115  		if err := clusterInstance.StartTopo(); err != nil {
   116  			return 1, err
   117  		}
   118  
   119  		// Start unsharded keyspace
   120  		keyspace := cluster.Keyspace{
   121  			Name:      lookupKeyspace,
   122  			SchemaSQL: createUnshardedMessage,
   123  			VSchema:   lookupVschema,
   124  		}
   125  		if err := clusterInstance.StartUnshardedKeyspace(keyspace, 1, false); err != nil {
   126  			return 1, err
   127  		}
   128  
   129  		// Start sharded keyspace
   130  		keyspace = cluster.Keyspace{
   131  			Name:      userKeyspace,
   132  			SchemaSQL: createShardedMessage,
   133  			VSchema:   userVschema,
   134  		}
   135  		if err := clusterInstance.StartKeyspace(keyspace, []string{"-80", "80-"}, 1, false); err != nil {
   136  			return 1, err
   137  		}
   138  
   139  		// Start vtgate
   140  		if err := clusterInstance.StartVtgate(); err != nil {
   141  			return 1, err
   142  		}
   143  
   144  		shard0Primary = clusterInstance.Keyspaces[1].Shards[0].PrimaryTablet()
   145  		shard1Primary = clusterInstance.Keyspaces[1].Shards[1].PrimaryTablet()
   146  		lookupPrimary = clusterInstance.Keyspaces[0].Shards[0].PrimaryTablet()
   147  		shard0Replica = clusterInstance.Keyspaces[1].Shards[0].Vttablets[1]
   148  
   149  		return m.Run(), nil
   150  	}()
   151  	if err != nil {
   152  		fmt.Printf("%v\n", err)
   153  		os.Exit(1)
   154  	} else {
   155  		os.Exit(exitcode)
   156  	}
   157  
   158  }