Skip to content

Instantly share code, notes, and snippets.

@mattlord
Last active January 20, 2022 07:30
Show Gist options
  • Save mattlord/029265e7efbe0e0b5d77403ddd4af454 to your computer and use it in GitHub Desktop.
Save mattlord/029265e7efbe0e0b5d77403ddd4af454 to your computer and use it in GitHub Desktop.

Command

You pass vtcombo a combination of flags that you would normally pass to the individual components (vtgate, vtctld, vttablet). For example:

vtcombo -logtostderr=true -proto_topo "$(cat ./topology)" -schema_dir ./vschema -mysql_server_port 15306 -mysql_server_bind_address 0.0.0.0 -mysql_auth_server_impl none -db_socket /tmp/mysql.sock -db_host localhost -mycnf-file /etc/mysql/my.cnf -db_app_user root -db_allprivs_user root -db_appdebug_user root -db_dba_user root -db_repl_user root -dbddl_plugin vttest -port 15000 -grpc_port 15001 -service_map 'grpc-vtgateservice,grpc-vtctl,grpc-vtctld' -db_charset utf8mb4 -vschema_ddl_authorized_users='%'

Example Topology File (-proto_topo)

keyspaces {
  name: "myunshardedks"
  shards {
    name: "0"
  }
}

keyspaces {
  name: "myshardedks",
  shards {
    name: "-80"
  }
  shards {
    name: "80-"
  }
}

VSchema Files (-schema_dir)

This directory should have a subdir for each database/keyspace you want to manage. And within that subdir you should have a vschema.json file containing the valid vschema defintion for that database/keyspace.

Example

cat ./vschema/myshardedks/vschema.json

{
    "sharded": true,
    "vindexes": {
        "hash": {
            "type": "hash"
        }
    },
    "tables": {
        "customer": {
            "column_vindexes": [
                {
                    "column": "customer_id",
                    "name": "hash"
                }
            ],
            "auto_increment": {
                "column": "customer_id",
                "sequence": "customer_seq"
            }
        },
        "corder": {
            "column_vindexes": [
                {
                    "column": "customer_id",
                    "name": "hash"
                }
            ],
            "auto_increment": {
                "column": "order_id",
                "sequence": "order_seq"
            }
        }
    }
}

MySQL Server

This is a separate local server that can e.g. be setup with mysqld --initialize-insecure and the vtgate and vttablet components within vtcombo can talk to it via a unix domain socket. Everything is then automatically created in that MySQL instance by vtcombo. For example:

mysql> show databases;
+------------------------------------------------------+
| Database                                             |
+------------------------------------------------------+
| _vt                                                  |
| information_schema                                   |
| mysql                                                |
| performance_schema                                   |
| sys                                                  |
| vt_myshardedks-80                                    |
| vt_myshardedks80-                                    |
| vt_myunshardedks                                     |
+------------------------------------------------------+
8 rows in set (0.00 sec)

And you can then load and test your database objects as you would normally against the exposed vtgate component, e.g.:

mysql --protocol tcp --host localhost --port 15306 --database myshardedks --user root < /path/to/myshardedks/schema.sql

Simulated Vitess Cluster

You still see the same expected vitess cluster view that you would expect. For example:

$ mysql --protocol=tcp  --host localhost --port 15306 --user root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.9-vitess-13.0.0-SNAPSHOT Version: 13.0.0-SNAPSHOT (Git revision b3b07b3b0c branch 'main') built on Tue Jan 18 13:55:00 EST 2022 by matt@pslord.local using go1.17.2 darwin/arm64

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show vitess_tablets;
+------+---------------+-------+------------+---------+-----------------+----------+----------------------+
| Cell | Keyspace      | Shard | TabletType | State   | Alias           | Hostname | PrimaryTermStartTime |
+------+---------------+-------+------------+---------+-----------------+----------+----------------------+
| test | myshardedks   | -80   | PRIMARY    | SERVING | test-0000000004 |          | 2022-01-20T22:01:10Z |
| test | myshardedks   | -80   | REPLICA    | SERVING | test-0000000005 |          |                      |
| test | myshardedks   | -80   | RDONLY     | SERVING | test-0000000006 |          |                      |
| test | myshardedks   | 80-   | PRIMARY    | SERVING | test-0000000007 |          | 2022-01-20T22:01:11Z |
| test | myshardedks   | 80-   | REPLICA    | SERVING | test-0000000008 |          |                      |
| test | myshardedks   | 80-   | RDONLY     | SERVING | test-0000000009 |          |                      |
| test | myunshardedks | 0     | PRIMARY    | SERVING | test-0000000001 |          | 2022-01-20T22:01:10Z |
| test | myunshardedks | 0     | REPLICA    | SERVING | test-0000000002 |          |                      |
| test | myunshardedks | 0     | RDONLY     | SERVING | test-0000000003 |          |                      |
+------+---------------+-------+------------+---------+-----------------+----------+----------------------+
9 rows in set (0.00 sec)
$ vtctlclient -server localhost:15001 ListAllTablets
test-0000000001 myunshardedks 0 primary :8001 localhost:0 [] 2022-01-20T22:01:10Z
test-0000000002 myunshardedks 0 replica :8002 localhost:0 [] <null>
test-0000000003 myunshardedks 0 rdonly :8003 localhost:0 [] <null>
test-0000000004 myshardedks -80 primary :8004 localhost:0 [] 2022-01-20T22:01:10Z
test-0000000005 myshardedks -80 replica :8005 localhost:0 [] <null>
test-0000000006 myshardedks -80 rdonly :8006 localhost:0 [] <null>
test-0000000007 myshardedks 80- primary :8007 localhost:0 [] 2022-01-20T22:01:11Z
test-0000000008 myshardedks 80- replica :8008 localhost:0 [] <null>
test-0000000009 myshardedks 80- rdonly :8009 localhost:0 [] <null>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment