diff --git a/.gitignore b/.gitignore index 5944bc3..45099cb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ # Test folder # /test /test/gridfs_main.go -/test_mysql/vitess \ No newline at end of file +/mysql/vitess \ No newline at end of file diff --git a/mysql/Makefile b/mysql/Makefile new file mode 100644 index 0000000..ec2d575 --- /dev/null +++ b/mysql/Makefile @@ -0,0 +1,114 @@ +## +## Tagenal +## Tsinghua Univeristy +## + +V_KEYSPACE_USERS = users +V_KEYSPACE_CONFIG = config + +DATABASE_FOLDER_PATH = ./database + +VITESS_OPERATOR_PATH = ./vitess/examples/operator +VITESS_COMMON_PATH = ./vitess/examples/common + +# Aliases +MYSQL_CLIENT = mysql -h 127.0.0.1 -P 15306 -u user +VTCTL_CLIENT = vtctlclient -server=localhost:15999 + +# Unsharded commands +INIT_USERS_TABLES = $(VTCTL_CLIENT) ApplySchema -sql="$(shell cat $(DATABASE_FOLDER_PATH)/init/init_users.sql)" $(V_KEYSPACE_USERS) +INIT_USERS_VSCHEMA = $(VTCTL_CLIENT) ApplyVSchema -vschema='$(shell cat $(DATABASE_FOLDER_PATH)/vschema/vschema_users_initial.json)' $(V_KEYSPACE_USERS) + +# Sharded commands +SHARD_INIT_CONFIG_USERS_SEQUENCES_SQL = $(VTCTL_CLIENT) ApplySchema -sql="$(shell cat $(DATABASE_FOLDER_PATH)/init/init_users_seq.sql)" $(V_KEYSPACE_CONFIG) +SHARD_INIT_CONFIG_USERS_SEQUENCES_VSCHEMA = $(VTCTL_CLIENT) ApplyVSchema -vschema='$(shell cat $(DATABASE_FOLDER_PATH)/vschema/vschema_config_seq.json)' $(V_KEYSPACE_CONFIG) +SHARD_ALTER_USERS_TABLES_SQL = $(VTCTL_CLIENT) ApplySchema -sql="$(shell cat $(DATABASE_FOLDER_PATH)/init/alter_users_tables_sharding.sql)" $(V_KEYSPACE_USERS) +SHARD_INIT_USERS_VSCHEMA = $(VTCTL_CLIENT) ApplyVSchema -vschema='$(shell cat $(DATABASE_FOLDER_PATH)/vschema/vschema_users_shard.json)' $(V_KEYSPACE_USERS) +SHARD_INIT_RESHARD_USERS = $(VTCTL_CLIENT) Reshard $(V_KEYSPACE_USERS).user2user '-' '-80,80-' +SHARD_VERIFY_USERS_SHARDING_PROCESS = $(VTCTL_CLIENT) VDiff $(V_KEYSPACE_USERS).user2user +SHARD_SWITCH_READ_REPLICA_USERS = $(VTCTL_CLIENT) SwitchReads -tablet_type=replica $(V_KEYSPACE_USERS).user2user +SHARD_SWITCH_READ_RDONLY_USERS = $(VTCTL_CLIENT) SwitchReads -tablet_type=rdonly $(V_KEYSPACE_USERS).user2user +SHARD_SWITCH_WRITE_USERS = $(VTCTL_CLIENT) SwitchWrites $(V_KEYSPACE_USERS).user2user + +# Region sharding commands +REGION_SHARD_INIT_CONFIG_USERS_VSCHEMA = $(VTCTL_CLIENT) ApplyVSchema -vschema='$(shell cat $(DATABASE_FOLDER_PATH)/vschema/region/vschema_users_shard_region.json)' $(V_KEYSPACE_USERS) +REGION_SHARD_INIT_CONFIG_USER_LOOKUP_VINDEX = $(VTCTL_CLIENT) CreateLookupVindex -tablet_types=REPLICA $(V_KEYSPACE_USERS) '$(shell cat $(DATABASE_FOLDER_PATH)/vschema/region/vschema_users_shard_lookup_vindex.json)' +REGION_SHARD_EXTERNALIZE_USER_LOOKUP_VINDEX = $(VTCTL_CLIENT) ExternalizeVindex $(V_KEYSPACE_USERS).user_region_lookup + +GET_USERS_VSCHEMA = $(VTCTL_CLIENT) GetVSchema $(V_KEYSPACE_USERS) + +list_vtctld: + kubectl get pods --selector="planetscale.com/component=vtctld" -o custom-columns=":metadata.name" + +start_minikube: + minikube start --cpus=12 --memory=14000 --disk-size=80g + +start_minikube_dashboard: + minikube dashboard + +clone_vitess_github: + git clone git@github.com:vitessio/vitess.git + +install_vitess_operator: + kubectl apply -f $(VITESS_OPERATOR_PATH)/operator.yaml + +init_kubernetes_unsharded_database: + kubectl apply -f kubernetes/vitess_cluster_secret.yaml + kubectl apply -f kubernetes/vitess_cluster_config.yaml + kubectl apply -f kubernetes/init_cluster_vitess.yaml + +port_forwarding_vitess: + ./script/port_forwarding.sh + +init_unsharded_database: + $(INIT_USERS_TABLES) + $(INIT_USERS_VSCHEMA) + +init_users_increment_sequence: + $(SHARD_INIT_CONFIG_USERS_SEQUENCES_SQL) + $(SHARD_INIT_CONFIG_USERS_SEQUENCES_VSCHEMA) + $(INIT_USERS_VSCHEMA) + $(SHARD_ALTER_USERS_TABLES_SQL) + $(SHARD_INIT_USERS_VSCHEMA) + +init_sharded_database: + @echo Wait ... + @sleep 5 + kubectl apply -f kubernetes/init_cluster_vitess_sharded.yaml + +init_region_sharding_users: + $(REGION_SHARD_INIT_CONFIG_USERS_VSCHEMA) + $(REGION_SHARD_INIT_CONFIG_USER_LOOKUP_VINDEX) + @echo Wait ... + @sleep 5 + $(REGION_SHARD_EXTERNALIZE_USER_LOOKUP_VINDEX) + +resharding_process_users: + $(SHARD_INIT_RESHARD_USERS) + $(SHARD_VERIFY_USERS_SHARDING_PROCESS) + $(SHARD_SWITCH_READ_REPLICA_USERS) + $(SHARD_SWITCH_READ_RDONLY_USERS) + $(SHARD_SWITCH_WRITE_USERS) + +reshard_users_db: + $(SHARD_INIT_RESHARD_USERS) + +copy_locations_json_to_k8s: + kubectl cp ./database/locations/locations.json $(shell kubectl get pods --selector="planetscale.com/component=vtctld" -o custom-columns=":metadata.name"):/tmp/countries.json + kubectl cp ./database/locations/locations.json $(shell kubectl get pods --selector="planetscale.com/component=vtgate" -o custom-columns=":metadata.name"):/tmp/countries.json + +show_vttablets: + kubectl get pods --selector="planetscale.com/component=vttablet" -o custom-columns=":metadata.name" + +show_vitess_tablets: + echo "show vitess_tablets;" | $(MYSQL_CLIENT) --table + +insert_few_user_row: + $(MYSQL_CLIENT) < ./database/insert/insert_data_users.sql + +show_user_table: + @$(MYSQL_CLIENT) --table < ./database/select/select_user.sql + @$(MYSQL_CLIENT) --table < ./database/select/select_user_shard_1.sql + @$(MYSQL_CLIENT) --table < ./database/select/select_user_shard_2.sql + +.PHONY: set_aliases diff --git a/mysql/README.md b/mysql/README.md new file mode 100644 index 0000000..3ecac28 --- /dev/null +++ b/mysql/README.md @@ -0,0 +1,286 @@ +# Tagenal MySQL Cluster + +Tagenal's MySQL cluster relies on Vitess and Kubernetes. + +## Setup + +### **1.** Start minikube + +`make start_minikube` + +### **2.** Start kubernetes dashboard (optional) + +This command should be run in second terminal. + +`make start_minikube_dashboard` + +### **3.** Install vitess operator in kubernetes + +`make install_vitess_operator` + +Expected output: + +``` +kubectl apply -f ./vitess/examples/operator/operator.yaml +customresourcedefinition.apiextensions.k8s.io/etcdlockservers.planetscale.com created +customresourcedefinition.apiextensions.k8s.io/vitessbackups.planetscale.com created +customresourcedefinition.apiextensions.k8s.io/vitessbackupstorages.planetscale.com created +customresourcedefinition.apiextensions.k8s.io/vitesscells.planetscale.com created +customresourcedefinition.apiextensions.k8s.io/vitessclusters.planetscale.com created +customresourcedefinition.apiextensions.k8s.io/vitesskeyspaces.planetscale.com created +customresourcedefinition.apiextensions.k8s.io/vitessshards.planetscale.com created +serviceaccount/vitess-operator created +role.rbac.authorization.k8s.io/vitess-operator created +rolebinding.rbac.authorization.k8s.io/vitess-operator created +priorityclass.scheduling.k8s.io/vitess created +priorityclass.scheduling.k8s.io/vitess-operator-control-plane created +deployment.apps/vitess-operator created +``` + +### **4.** Initialize unsharded database in kubernetes + +`make init_kubernetes_unsharded_database` + +Expected output: + +``` +kubectl apply -f kubernetes/vitess_cluster_secret.yaml +secret/vitess-cluster-secret created +kubectl apply -f kubernetes/vitess_cluster_config.yaml +configmap/vitess-cluster-config-sharding created +kubectl apply -f kubernetes/init_cluster_vitess.yaml +vitesscluster.planetscale.com/vitess created +``` + +### **5.** Initialize unsharded database in kubernetes + +This command should be run in a third terminal. + +`make port_forwarding_vitess` + +Expected output: + +``` +./script/port_forwarding.sh +Forwarding from 127.0.0.1:15000 -> 15000 +Forwarding from [::1]:15000 -> 15000 +Forwarding from 127.0.0.1:15999 -> 15999 +Forwarding from [::1]:15999 -> 15999 +Forwarding from 127.0.0.1:15306 -> 3306 +Forwarding from [::1]:15306 -> 3306 +``` + +After this command, Vitess cluster dashboard (vtctld) should be accessible at http://localhost:15000. + + +### **6.** Initialize MySQL user table and Vitess VSchema + +`make init_unsharded_database` + +Expected output: + +``` +vtctlclient -server=localhost:15999 ApplySchema -sql="DROP TABLE IF EXISTS user; CREATE TABLE user ( _id INT NOT NULL auto_increment, timestamp CHAR(14) DEFAULT NULL, id CHAR(5) DEFAULT NULL, uid CHAR(5) DEFAULT NULL, name CHAR(9) DEFAULT NULL, gender CHAR(7) DEFAULT NULL, email CHAR(10) DEFAULT NULL, phone CHAR(10) DEFAULT NULL, dept CHAR(9) DEFAULT NULL, grade CHAR(7) DEFAULT NULL, language CHAR(3) DEFAULT NULL, region VARBINARY(256), role CHAR(6) DEFAULT NULL, preferTags CHAR(7) DEFAULT NULL, obtainedCredits CHAR(3) DEFAULT NULL, PRIMARY KEY(_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;" users +vtctlclient -server=localhost:15999 ApplyVSchema -vschema='{ "tables": { "user": {} } }' users +New VSchema object: +{ + "tables": { + "user": { + + } + } +} +If this is not what you expected, check the input data (as JSON parsing will skip unexpected fields). +``` + +### **7.** Initialize the incrementation sequence of the user table in the config keyspace + +`make init_users_increment_sequence` + +Expected output: + +``` +vtctlclient -server=localhost:15999 ApplySchema -sql="CREATE TABLE users_seq(id INT, next_id BIGINT, cache BIGINT, PRIMARY KEY(id)) comment 'vitess_sequence'; INSERT INTO users_seq(id, next_id, cache) VALUES(0, 1, 100);" config +vtctlclient -server=localhost:15999 ApplyVSchema -vschema='{ "tables": { "users_seq": { "type": "sequence" } } }' config +New VSchema object: +{ + "tables": { + "users_seq": { + "type": "sequence" + } + } +} +If this is not what you expected, check the input data (as JSON parsing will skip unexpected fields). +vtctlclient -server=localhost:15999 ApplyVSchema -vschema='{ "tables": { "user": {} } }' users +New VSchema object: +{ + "tables": { + "user": { + + } + } +} +If this is not what you expected, check the input data (as JSON parsing will skip unexpected fields). +vtctlclient -server=localhost:15999 ApplySchema -sql="ALTER TABLE user change _id _id INT NOT NULL;" users +vtctlclient -server=localhost:15999 ApplyVSchema -vschema='{ "sharded": true, "vindexes": { "hash": { "type": "hash" } }, "tables": { "user": { "column_vindexes": [ { "column": "_id", "name": "hash" } ], "auto_increment": { "column": "_id", "sequence": "users_seq" } } } }' users +New VSchema object: +{ + "sharded": true, + "vindexes": { + "hash": { + "type": "hash" + } + }, + "tables": { + "user": { + "columnVindexes": [ + { + "column": "_id", + "name": "hash" + } + ], + "autoIncrement": { + "column": "_id", + "sequence": "users_seq" + } + } + } +} +If this is not what you expected, check the input data (as JSON parsing will skip unexpected fields). +``` + +### **8.** Initialize sharded database in kubernetes + +`make init_sharded_database` + +Expected output: + +``` +Wait ... +kubectl apply -f kubernetes/init_cluster_vitess_sharded.yaml +vitesscluster.planetscale.com/vitess configured +``` + +### **9.** Kill previous port forwarding processes + +In the third terminal, where the `make port_forwarding_vitess` was run, we will run the following command: + +`ps` + +And expect at least these two lines in the output, (the content of the lines can slightly differ): + +``` +38950 ttys000 0:00.52 kubectl port-forward service/vitess-vtctld-047eb1a6 15000 15999 +38951 ttys000 0:00.42 kubectl port-forward service/vitess-vtgate-e05d80f4 15306:3306 +``` + +We will take the 2 foremost left numbers, in this case `38950` and `38951`, and run the following command: + +`kill 38950 38951` + +Finally, we can run the `make port_forwarding_vitess` again. + +### **10.** Insert the first few rows of the user table + +`make insert_few_user_row` + +Expected output: + +``` +mysql -h 127.0.0.1 -P 15306 -u user < ./database/insert/insert_data_users.sql +``` + +### **11.** Setup the region-based sharding + +`make init_region_sharding_users` + +Expected output: + +``` +vtctlclient -server=localhost:15999 ApplyVSchema -vschema='{ "sharded": true, "vindexes": { "region_vdx": { "type": "region_json", "params": { "region_map": "/tmp/countries.json", "region_bytes": "1" } } }, "tables": { "user": { "column_vindexes": [ { "columns": [ "_id", "region" ], "name": "region_vdx" } ], "auto_increment": { "column": "_id", "sequence": "users_seq" } } } }' users +New VSchema object: +{ + "sharded": true, + "vindexes": { + "region_vdx": { + "type": "region_json", + "params": { + "region_bytes": "1", + "region_map": "/tmp/countries.json" + } + } + }, + "tables": { + "user": { + "columnVindexes": [ + { + "name": "region_vdx", + "columns": [ + "_id", + "region" + ] + } + ], + "autoIncrement": { + "column": "_id", + "sequence": "users_seq" + } + } + } +} +If this is not what you expected, check the input data (as JSON parsing will skip unexpected fields). +vtctlclient -server=localhost:15999 CreateLookupVindex -tablet_types=REPLICA users '{ "sharded": true, "vindexes": { "user_region_lookup": { "type": "consistent_lookup_unique", "params": { "table": "users.user_lookup", "from": "_id", "to": "keyspace_id" }, "owner": "user" } }, "tables": { "user": { "column_vindexes": [ { "column": "_id", "name": "user_region_lookup" } ] } } }' +Wait ... +vtctlclient -server=localhost:15999 ExternalizeVindex users.user_region_lookup +``` + +### **12.** Start the resharding process of the cluster + +`make resharding_process_users` + +Expected output: + +``` +vtctlclient -server=localhost:15999 Reshard users.user2user '-' '-80,80-' +vtctlclient -server=localhost:15999 VDiff users.user2user +Summary for user: {ProcessedRows:5 MatchingRows:5 MismatchedRows:0 ExtraRowsSource:0 ExtraRowsTarget:0} +E1101 20:23:50.599068 52103 main.go:64] E1101 19:23:50.595298 vdiff.go:780] Draining extra row(s) found on the source starting with: [INT32(1) VARBINARY("\x01\x16k@\xb4J\xbaK\xd6")] +Summary for user_lookup: {ProcessedRows:5 MatchingRows:0 MismatchedRows:0 ExtraRowsSource:5 ExtraRowsTarget:0} +vtctlclient -server=localhost:15999 SwitchReads -tablet_type=replica users.user2user +vtctlclient -server=localhost:15999 SwitchReads -tablet_type=rdonly users.user2user +vtctlclient -server=localhost:15999 SwitchWrites users.user2user +``` + +### **13.** Test, display the user table + +`make show_user_table` + +Expected output: + +``` +Users ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+-----------+-------+------------+-----------------+ +| _id | timestamp | id | uid | name | gender | email | phone | dept | grade | language | region | role | preferTags | obtainedCredits | ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+-----------+-------+------------+-----------------+ +| 1 | 1506328859000 | u0 | 0 | user0 | male | email0 | phone0 | dept13 | grade1 | zh | Beijing | role2 | tags24 | 42 | +| 2 | 1506328859001 | u1 | 1 | user1 | female | email1 | phone1 | dept5 | grade1 | en | Beijing | role2 | tags7 | 22 | +| 3 | 1506328859002 | u2 | 2 | user2 | male | email2 | phone2 | dept4 | grade4 | en | Beijing | role2 | tags46 | 62 | +| 4 | 1506328859003 | u3 | 3 | user3 | female | email3 | phone3 | dept15 | grade4 | zh | Beijing | role1 | tags0 | 2 | +| 5 | 1506328859004 | u4 | 4 | user4 | male | email4 | phone4 | dept15 | grade4 | en | Hong Kong | role2 | tags18 | 63 | ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+-----------+-------+------------+-----------------+ +Users -80 ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+---------+-------+------------+-----------------+ +| _id | timestamp | id | uid | name | gender | email | phone | dept | grade | language | region | role | preferTags | obtainedCredits | ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+---------+-------+------------+-----------------+ +| 1 | 1506328859000 | u0 | 0 | user0 | male | email0 | phone0 | dept13 | grade1 | zh | Beijing | role2 | tags24 | 42 | +| 2 | 1506328859001 | u1 | 1 | user1 | female | email1 | phone1 | dept5 | grade1 | en | Beijing | role2 | tags7 | 22 | +| 3 | 1506328859002 | u2 | 2 | user2 | male | email2 | phone2 | dept4 | grade4 | en | Beijing | role2 | tags46 | 62 | +| 4 | 1506328859003 | u3 | 3 | user3 | female | email3 | phone3 | dept15 | grade4 | zh | Beijing | role1 | tags0 | 2 | ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+---------+-------+------------+-----------------+ +Users 80- ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+-----------+-------+------------+-----------------+ +| _id | timestamp | id | uid | name | gender | email | phone | dept | grade | language | region | role | preferTags | obtainedCredits | ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+-----------+-------+------------+-----------------+ +| 5 | 1506328859004 | u4 | 4 | user4 | male | email4 | phone4 | dept15 | grade4 | en | Hong Kong | role2 | tags18 | 63 | ++-----+---------------+------+------+-------+--------+--------+--------+--------+--------+----------+-----------+-------+------------+-----------------+ +``` \ No newline at end of file diff --git a/mysql/database/init/alter_users_tables_sharding.sql b/mysql/database/init/alter_users_tables_sharding.sql new file mode 100644 index 0000000..c46898e --- /dev/null +++ b/mysql/database/init/alter_users_tables_sharding.sql @@ -0,0 +1 @@ +ALTER TABLE user change _id _id INT NOT NULL; \ No newline at end of file diff --git a/mysql/database/init/init_lookup_user.sql b/mysql/database/init/init_lookup_user.sql new file mode 100644 index 0000000..2638b06 --- /dev/null +++ b/mysql/database/init/init_lookup_user.sql @@ -0,0 +1,5 @@ +CREATE TABLE users_lookup ( + _id INT NOT NULL, + keyspace_id VARBINARY(128), + PRIMARY KEY(_id) +); \ No newline at end of file diff --git a/mysql/database/init/init_users.sql b/mysql/database/init/init_users.sql new file mode 100644 index 0000000..8f5be74 --- /dev/null +++ b/mysql/database/init/init_users.sql @@ -0,0 +1,20 @@ +DROP TABLE IF EXISTS user; + +CREATE TABLE user ( + _id INT NOT NULL auto_increment, + timestamp CHAR(14) DEFAULT NULL, + id CHAR(5) DEFAULT NULL, + uid CHAR(5) DEFAULT NULL, + name CHAR(9) DEFAULT NULL, + gender CHAR(7) DEFAULT NULL, + email CHAR(10) DEFAULT NULL, + phone CHAR(10) DEFAULT NULL, + dept CHAR(9) DEFAULT NULL, + grade CHAR(7) DEFAULT NULL, + language CHAR(3) DEFAULT NULL, + region VARBINARY(256), + role CHAR(6) DEFAULT NULL, + preferTags CHAR(7) DEFAULT NULL, + obtainedCredits CHAR(3) DEFAULT NULL, + PRIMARY KEY(_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file diff --git a/mysql/database/init/init_users_seq.sql b/mysql/database/init/init_users_seq.sql new file mode 100644 index 0000000..41223ab --- /dev/null +++ b/mysql/database/init/init_users_seq.sql @@ -0,0 +1,2 @@ +CREATE TABLE users_seq(id INT, next_id BIGINT, cache BIGINT, PRIMARY KEY(id)) comment 'vitess_sequence'; +INSERT INTO users_seq(id, next_id, cache) VALUES(0, 1, 100); \ No newline at end of file diff --git a/mysql/database/insert/insert_data_users.sql b/mysql/database/insert/insert_data_users.sql new file mode 100644 index 0000000..5c473ea --- /dev/null +++ b/mysql/database/insert/insert_data_users.sql @@ -0,0 +1,7 @@ +USE users; +INSERT INTO user (timestamp,id,uid,name,gender,email,phone,dept,grade,language,region,role,preferTags,obtainedCredits) VALUES + ('1506328859000', 'u0', '0', 'user0', 'male', 'email0', 'phone0', 'dept13', 'grade1', 'zh', 'Beijing', 'role2', 'tags24', '42'), + ('1506328859001', 'u1', '1', 'user1', 'female', 'email1', 'phone1', 'dept5', 'grade1', 'en', 'Beijing', 'role2', 'tags7', '22'), + ('1506328859002', 'u2', '2', 'user2', 'male', 'email2', 'phone2', 'dept4', 'grade4', 'en', 'Beijing', 'role2', 'tags46', '62'), + ('1506328859003', 'u3', '3', 'user3', 'female', 'email3', 'phone3', 'dept15', 'grade4', 'zh', 'Beijing', 'role1', 'tags0', '2'), + ('1506328859004', 'u4', '4', 'user4', 'male', 'email4', 'phone4', 'dept15', 'grade4', 'en', 'Hong Kong', 'role2', 'tags18', '63'); diff --git a/mysql/database/insert/insert_data_users_third_region.sql b/mysql/database/insert/insert_data_users_third_region.sql new file mode 100644 index 0000000..8177fe1 --- /dev/null +++ b/mysql/database/insert/insert_data_users_third_region.sql @@ -0,0 +1,3 @@ +USE users; +INSERT INTO user (timestamp,id,uid,name,gender,email,phone,dept,grade,language,region,role,preferTags,obtainedCredits) VALUES + ('1506328859900', 'u99', '99', 'user99', 'male', 'email0', 'phone0', 'dept13', 'grade1', 'zh', 'Tokyo', 'role2', 'tags24', '42') diff --git a/mysql/database/insert/insert_more_data_users.sql b/mysql/database/insert/insert_more_data_users.sql new file mode 100644 index 0000000..e46d35f --- /dev/null +++ b/mysql/database/insert/insert_more_data_users.sql @@ -0,0 +1,18 @@ +USE users; +INSERT INTO user (timestamp,id,uid,name,gender,email,phone,dept,grade,language,region,role,preferTags,obtainedCredits) VALUES + ("1506328859005", "u5", "5", "user5", "male", "email5", "phone5", "dept16", "grade4", "zh", "Beijing", "role2", "tags11", "16"), + ("1506328859006", "u6", "6", "user6", "female", "email6", "phone6", "dept9", "grade2", "en", "Beijing", "role0", "tags4", "49"), + ("1506328859007", "u7", "7", "user7", "male", "email7", "phone7", "dept6", "grade4", "zh", "Beijing", "role1", "tags23", "8"), + ("1506328859008", "u8", "8", "user8", "male", "email8", "phone8", "dept5", "grade1", "zh", "Beijing", "role0", "tags3", "39"), + ("1506328859009", "u9", "9", "user9", "female", "email9", "phone9", "dept9", "grade4", "zh", "Beijing", "role0", "tags9", "69"), + ("1506328859010", "u10", "10", "user10", "female", "email10", "phone10", "dept17", "grade3", "zh", "Beijing", "role2", "tags48", "2"), + ("1506328859011", "u11", "11", "user11", "female", "email11", "phone11", "dept14", "grade1", "en", "Hong Kong", "role0", "tags8", "56"), + ("1506328859012", "u12", "12", "user12", "female", "email12", "phone12", "dept12", "grade2", "zh", "Hong Kong", "role0", "tags18", "3"), + ("1506328859013", "u13", "13", "user13", "female", "email13", "phone13", "dept3", "grade4", "zh", "Hong Kong", "role0", "tags10", "63"), + ("1506328859014", "u14", "14", "user14", "male", "email14", "phone14", "dept18", "grade1", "zh", "Hong Kong", "role2", "tags43", "10"), + ("1506328859015", "u15", "15", "user15", "female", "email15", "phone15", "dept3", "grade2", "zh", "Beijing", "role0", "tags46", "60"), + ("1506328859016", "u16", "16", "user16", "male", "email16", "phone16", "dept19", "grade3", "zh", "Hong Kong", "role1", "tags21", "55"), + ("1506328859017", "u17", "17", "user17", "male", "email17", "phone17", "dept7", "grade4", "en", "Beijing", "role1", "tags20", "51"), + ("1506328859018", "u18", "18", "user18", "female", "email18", "phone18", "dept12", "grade4", "zh", "Beijing", "role1", "tags1", "73"), + ("1506328859019", "u19", "19", "user19", "male", "email19", "phone19", "dept11", "grade1", "zh", "Beijing", "role1", "tags21", "69"), + ("1506328859020", "u20", "20", "user20", "male", "email20", "phone20", "dept4", "grade1", "zh", "Hong Kong", "role2", "tags18", "8"); \ No newline at end of file diff --git a/mysql/database/locations/locations.json b/mysql/database/locations/locations.json new file mode 100644 index 0000000..90bc5df --- /dev/null +++ b/mysql/database/locations/locations.json @@ -0,0 +1,4 @@ +{ + "Beijing": 1, + "Hong Kong": 200 +} \ No newline at end of file diff --git a/mysql/database/select/select_user.sql b/mysql/database/select/select_user.sql new file mode 100644 index 0000000..01341cd --- /dev/null +++ b/mysql/database/select/select_user.sql @@ -0,0 +1,3 @@ +\! echo 'Users' +USE users; +SELECT * FROM user LIMIT 20; diff --git a/mysql/database/select/select_user_shard_1.sql b/mysql/database/select/select_user_shard_1.sql new file mode 100644 index 0000000..065e912 --- /dev/null +++ b/mysql/database/select/select_user_shard_1.sql @@ -0,0 +1,3 @@ +\! echo 'Users -80' +USE users/-80; +SELECT * FROM user LIMIT 20; diff --git a/mysql/database/select/select_user_shard_2.sql b/mysql/database/select/select_user_shard_2.sql new file mode 100644 index 0000000..47e8931 --- /dev/null +++ b/mysql/database/select/select_user_shard_2.sql @@ -0,0 +1,3 @@ +\! echo 'Users 80-' +USE users/80-; +SELECT * FROM user LIMIT 20; \ No newline at end of file diff --git a/mysql/database/vschema/region/vschema_users_shard_lookup_vindex.json b/mysql/database/vschema/region/vschema_users_shard_lookup_vindex.json new file mode 100644 index 0000000..311089b --- /dev/null +++ b/mysql/database/vschema/region/vschema_users_shard_lookup_vindex.json @@ -0,0 +1,24 @@ +{ + "sharded": true, + "vindexes": { + "user_region_lookup": { + "type": "consistent_lookup_unique", + "params": { + "table": "users.user_lookup", + "from": "_id", + "to": "keyspace_id" + }, + "owner": "user" + } + }, + "tables": { + "user": { + "column_vindexes": [ + { + "column": "_id", + "name": "user_region_lookup" + } + ] + } + } +} \ No newline at end of file diff --git a/mysql/database/vschema/region/vschema_users_shard_region.json b/mysql/database/vschema/region/vschema_users_shard_region.json new file mode 100644 index 0000000..baac1f3 --- /dev/null +++ b/mysql/database/vschema/region/vschema_users_shard_region.json @@ -0,0 +1,29 @@ +{ + "sharded": true, + "vindexes": { + "region_vdx": { + "type": "region_json", + "params": { + "region_map": "/tmp/countries.json", + "region_bytes": "1" + } + } + }, + "tables": { + "user": { + "column_vindexes": [ + { + "columns": [ + "_id", + "region" + ], + "name": "region_vdx" + } + ], + "auto_increment": { + "column": "_id", + "sequence": "users_seq" + } + } + } +} \ No newline at end of file diff --git a/mysql/database/vschema/vschema_config_seq.json b/mysql/database/vschema/vschema_config_seq.json new file mode 100644 index 0000000..20a5b0a --- /dev/null +++ b/mysql/database/vschema/vschema_config_seq.json @@ -0,0 +1,7 @@ +{ + "tables": { + "users_seq": { + "type": "sequence" + } + } +} \ No newline at end of file diff --git a/test_mysql/database/vschema_database_users.json b/mysql/database/vschema/vschema_users_initial.json similarity index 55% rename from test_mysql/database/vschema_database_users.json rename to mysql/database/vschema/vschema_users_initial.json index b1cc3fd..8d0bae9 100644 --- a/test_mysql/database/vschema_database_users.json +++ b/mysql/database/vschema/vschema_users_initial.json @@ -1,5 +1,5 @@ { "tables": { - "users": {} + "user": {} } } \ No newline at end of file diff --git a/mysql/database/vschema/vschema_users_shard.json b/mysql/database/vschema/vschema_users_shard.json new file mode 100644 index 0000000..226b04c --- /dev/null +++ b/mysql/database/vschema/vschema_users_shard.json @@ -0,0 +1,22 @@ +{ + "sharded": true, + "vindexes": { + "hash": { + "type": "hash" + } + }, + "tables": { + "user": { + "column_vindexes": [ + { + "column": "_id", + "name": "hash" + } + ], + "auto_increment": { + "column": "_id", + "sequence": "users_seq" + } + } + } +} \ No newline at end of file diff --git a/mysql/kubernetes/init_cluster_vitess.yaml b/mysql/kubernetes/init_cluster_vitess.yaml new file mode 100644 index 0000000..8770d45 --- /dev/null +++ b/mysql/kubernetes/init_cluster_vitess.yaml @@ -0,0 +1,109 @@ +apiVersion: planetscale.com/v2 +kind: VitessCluster +metadata: + name: vitess +spec: + images: + vtctld: vitess/lite:v6.0.20-20200429 + vtgate: vitess/lite:v6.0.20-20200429 + vttablet: vitess/lite:v6.0.20-20200429 + vtbackup: vitess/lite:v6.0.20-20200429 + mysqld: + mysql56Compatible: vitess/lite:v6.0.20-20200429 + mysqldExporter: prom/mysqld-exporter:v0.11.0 + cells: + - name: zone1 + gateway: # VtGate + authentication: + static: + secret: + name: vitess-cluster-secret + key: users.json + replicas: 1 + resources: + requests: + cpu: 100m + memory: 256Mi + limits: + memory: 256Mi + vitessDashboard: + cells: + - zone1 + extraFlags: + security_policy: read-only + replicas: 1 + resources: + limits: + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + + keyspaces: + - name: users + turndownPolicy: Immediate + partitionings: + - equal: + parts: 1 + shardTemplate: + databaseInitScriptSecret: + name: vitess-cluster-secret + key: init_db.sql + replication: + enforceSemiSync: false + tabletPools: + - cell: zone1 + type: replica + replicas: 2 + vttablet: + extraFlags: + db_charset: utf8mb4 + resources: + requests: + cpu: 100m + memory: 256Mi + mysqld: + resources: + requests: + cpu: 100m + memory: 256Mi + dataVolumeClaimTemplate: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 10Gi + - name: config + turndownPolicy: Immediate + partitionings: + - equal: + parts: 1 + shardTemplate: + databaseInitScriptSecret: + name: vitess-cluster-secret + key: init_db.sql + replication: + enforceSemiSync: false + tabletPools: + - cell: zone1 + type: replica + replicas: 2 + vttablet: + extraFlags: + db_charset: utf8mb4 + resources: + requests: + cpu: 100m + memory: 256Mi + mysqld: + resources: + requests: + cpu: 100m + memory: 256Mi + dataVolumeClaimTemplate: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 10Gi + + updateStrategy: + type: Immediate diff --git a/mysql/kubernetes/init_cluster_vitess_sharded.yaml b/mysql/kubernetes/init_cluster_vitess_sharded.yaml new file mode 100644 index 0000000..3cc0f18 --- /dev/null +++ b/mysql/kubernetes/init_cluster_vitess_sharded.yaml @@ -0,0 +1,168 @@ +apiVersion: planetscale.com/v2 +kind: VitessCluster +metadata: + name: vitess +spec: + images: + vtctld: vitess/lite:v6.0.20-20200429 + vtgate: vitess/lite:v6.0.20-20200429 + vttablet: vitess/lite:v6.0.20-20200429 + vtbackup: vitess/lite:v6.0.20-20200429 + mysqld: + mysql56Compatible: vitess/lite:v6.0.20-20200429 + mysqldExporter: prom/mysqld-exporter:v0.11.0 + cells: + - name: zone1 # VTGATE + gateway: + authentication: + static: + secret: + name: vitess-cluster-secret + key: users.json + replicas: 1 + resources: + requests: + cpu: 100m + memory: 256Mi + limits: + memory: 256Mi + extraVolumeMounts: + - name: vitess-region-config-volume + mountPath: /tmp/countries.json + subPath: countries.json + extraVolumes: + - name: vitess-region-config-volume + configMap: + name: vitess-cluster-config-sharding + vitessDashboard: # VTCTLD + cells: + - zone1 + extraFlags: + security_policy: read-only + replicas: 1 + resources: + limits: + memory: 128Mi + requests: + cpu: 100m + memory: 128Mi + extraVolumeMounts: + - name: vitess-region-config-volume + mountPath: /tmp/countries.json + subPath: countries.json + extraVolumes: + - name: vitess-region-config-volume + configMap: + name: vitess-cluster-config-sharding + keyspaces: + - name: users + turndownPolicy: Immediate + partitionings: + - equal: + parts: 1 + shardTemplate: + databaseInitScriptSecret: + name: vitess-cluster-secret + key: init_db.sql + replication: + enforceSemiSync: false + tabletPools: + - cell: zone1 + type: replica + replicas: 2 + vttablet: + extraFlags: + db_charset: utf8mb4 + resources: + requests: + cpu: 100m + memory: 256Mi + mysqld: + resources: + requests: + cpu: 100m + memory: 256Mi + extraVolumeMounts: + - name: vitess-region-config-volume + mountPath: /tmp/countries.json + subPath: countries.json + extraVolumes: + - name: vitess-region-config-volume + configMap: + name: vitess-cluster-config-sharding + dataVolumeClaimTemplate: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 10Gi + - equal: + parts: 2 + shardTemplate: + databaseInitScriptSecret: + name: vitess-cluster-secret + key: init_db.sql + replication: + enforceSemiSync: false + tabletPools: + - cell: zone1 + type: replica + replicas: 2 + vttablet: + extraFlags: + db_charset: utf8mb4 + resources: + requests: + cpu: 100m + memory: 256Mi + mysqld: + resources: + requests: + cpu: 100m + memory: 256Mi + dataVolumeClaimTemplate: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 10Gi + extraVolumeMounts: + - name: vitess-region-config-volume + mountPath: /tmp/countries.json + subPath: countries.json + extraVolumes: + - name: vitess-region-config-volume + configMap: + name: vitess-cluster-config-sharding + - name: config + turndownPolicy: Immediate + partitionings: + - equal: + parts: 1 + shardTemplate: + databaseInitScriptSecret: + name: vitess-cluster-secret + key: init_db.sql + replication: + enforceSemiSync: false + tabletPools: + - cell: zone1 + type: replica + replicas: 2 + vttablet: + extraFlags: + db_charset: utf8mb4 + resources: + requests: + cpu: 100m + memory: 256Mi + mysqld: + resources: + requests: + cpu: 100m + memory: 256Mi + dataVolumeClaimTemplate: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 10Gi + updateStrategy: + type: Immediate diff --git a/test_mysql/kubernetes/init_namespaces.yaml b/mysql/kubernetes/init_namespaces.yaml similarity index 100% rename from test_mysql/kubernetes/init_namespaces.yaml rename to mysql/kubernetes/init_namespaces.yaml diff --git a/mysql/kubernetes/vitess_cluster_config.yaml b/mysql/kubernetes/vitess_cluster_config.yaml new file mode 100644 index 0000000..55d7b1b --- /dev/null +++ b/mysql/kubernetes/vitess_cluster_config.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: vitess-cluster-config-sharding + namespace: default +data: + countries.json: | + { + "Beijing": 1, + "Hong Kong": 200 + } diff --git a/test_mysql/kubernetes/init_cluster_vitess.yaml b/mysql/kubernetes/vitess_cluster_secret.yaml similarity index 58% rename from test_mysql/kubernetes/init_cluster_vitess.yaml rename to mysql/kubernetes/vitess_cluster_secret.yaml index df971a0..91f9b69 100644 --- a/test_mysql/kubernetes/init_cluster_vitess.yaml +++ b/mysql/kubernetes/vitess_cluster_secret.yaml @@ -1,86 +1,7 @@ -apiVersion: planetscale.com/v2 -kind: VitessCluster -metadata: - name: vitess - labels: - app: tagenal -spec: - images: - vtctld: vitess/lite:v6.0.20-20200429 - vtgate: vitess/lite:v6.0.20-20200429 - vttablet: vitess/lite:v6.0.20-20200429 - vtbackup: vitess/lite:v6.0.20-20200429 - mysqld: - mysql56Compatible: vitess/lite:v6.0.20-20200429 - mysqldExporter: prom/mysqld-exporter:v0.11.0 - cells: - - name: zone1 - gateway: # VtGate - authentication: - static: - secret: - name: tagenal-vitess-config - key: users.json - replicas: 2 - resources: - requests: - cpu: 100m - memory: 256Mi - limits: - memory: 256Mi - vitessDashboard: - cells: - - zone1 - extraFlags: - security_policy: read-only - replicas: 1 - resources: - limits: - memory: 128Mi - requests: - cpu: 100m - memory: 128Mi - - keyspaces: - - name: usersdb - turndownPolicy: Immediate - partitionings: - - equal: - parts: 1 - shardTemplate: - databaseInitScriptSecret: - name: tagenal-vitess-config - key: init_db.sql - replication: - enforceSemiSync: false - tabletPools: - - cell: zone1 - type: replica - replicas: 3 - vttablet: - extraFlags: - db_charset: utf8mb4 - resources: - requests: - cpu: 100m - memory: 256Mi - mysqld: - resources: - requests: - cpu: 100m - memory: 256Mi - dataVolumeClaimTemplate: - accessModes: ["ReadWriteOnce"] - resources: - requests: - storage: 10Gi - updateStrategy: - type: Immediate ---- apiVersion: v1 kind: Secret metadata: - name: tagenal-vitess-config + name: vitess-cluster-secret type: Opaque stringData: users.json: | diff --git a/test_mysql/script/port_forwarding.sh b/mysql/script/port_forwarding.sh similarity index 100% rename from test_mysql/script/port_forwarding.sh rename to mysql/script/port_forwarding.sh diff --git a/test_mysql/Makefile b/test_mysql/Makefile deleted file mode 100644 index 01692f3..0000000 --- a/test_mysql/Makefile +++ /dev/null @@ -1,35 +0,0 @@ - -start_minikube = minikube start --cpus=10 --memory=10000 --disk-size=30g - -vitess_operator_folder = ./vitess/examples/operator -vitess_common_folder = ./vitess/examples/common - -clone_vitess_repo = git clone git@github.com:vitessio/vitess.git - - -mysql_client_alias = mysql -h 127.0.0.1 -P 15306 -u user -vtctl_client_alias = vtctlclient -server=localhost:15999 - - -show_vitess_tablets = echo "show vitess_tablets;" | $(mysql_client_alias) --table -show_user_table_describe = echo "describe user;" | $(mysql_client_alias) --table - -init_Schema_users_table = $(vtctl_client_alias) ApplySchema -sql="$(cat database/init_users.sql)" usersdb -init_VSchema_usersdb = $(vtctl_client_alias) ApplyVSchema -vschema="$(cat database/vschema_database_users.json)" usersdb - -.PHONY: set_aliases - -dashboard_minikube: - minikube dashboard & - -start_minikube: - $(start_minikube) - -clone_vitess_github: - $(clone_vitess_github) - -show_vitess_tablets: - $(show_vitess_tablets) - -show_user_table_describe: - $(show_user_table_describe) \ No newline at end of file diff --git a/test_mysql/database/init_users.sql b/test_mysql/database/init_users.sql deleted file mode 100644 index f0020b0..0000000 --- a/test_mysql/database/init_users.sql +++ /dev/null @@ -1,18 +0,0 @@ -DROP TABLE IF EXISTS `user`; - -CREATE TABLE `user` ( - `timestamp` char(14) DEFAULT NULL, - `id` char(5) DEFAULT NULL, - `uid` char(5) DEFAULT NULL, - `name` char(9) DEFAULT NULL, - `gender` char(7) DEFAULT NULL, - `email` char(10) DEFAULT NULL, - `phone` char(10) DEFAULT NULL, - `dept` char(9) DEFAULT NULL, - `grade` char(7) DEFAULT NULL, - `language` char(3) DEFAULT NULL, - `region` char(10) DEFAULT NULL, - `role` char(6) DEFAULT NULL, - `preferTags` char(7) DEFAULT NULL, - `obtainedCredits` char(3) DEFAULT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file