在arm64上编译vitess并运行local用例
在part 1中,我们讲过vitess本身不支持arm,并叙述了如何解决arm上Vitess的大部分依赖问题。本文将讲述在arm机器上如何编译vitess。按照本文方法所编译的vitess可以正常的运行vitess官方的local本地集群实例。
编译
运行环境
- Architecture:aarch64
- OS:ubuntu 18.04
- kernel:4.15.0-45-generic
- go:1.12.7
安装依赖并配置环境
sudo apt-get update && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y make automake libtool python-dev python-virtualenv python-mysqldb python-pip libssl-dev g++ git pkg-config bison curl unzip mysql-server openjdk-8-jdk zip ant wget
wget https://dl.google.com/go/go1.12.7.linux-arm64.tar.gz
sudo tar zxf go1.12.7.linux-arm64.tar.gz -C /usr/local
rm -rf go1.12.7.linux-arm64.tar.gz
mkdir -p ${HOME}/gopath
cat << EOF >> ~/.bashrc
export GOPATH=${HOME}/gopath
export MYSQL_FLAVOR=MySQL56
export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin
export GODEBUG=netdns=go
export VTROOT=${HOME}/gopath
export VTTOP=${HOME}/gopath
export VT_MYSQL_ROOT=/usr
export VTDATAROOT=${HOME}/vtdataroot
EOF
source ~/.bashrc
mkdir -p ${HOME}/vtdataroot
cd ${HOME}/gopath
go版本要求为Go 1.12+。不管是MySQL 5.6 或者MySQL 5.7,环境变量MYSQL_FLAVOR都应设置为MySQL56,设置为MySQL57会出错。
更改bootstrap.sh和dev.env
下载源代码,根据part 1更改文件bootstrap.sh和dev.env。
git clone https://github.com/vitessio/vitess.git \
src/vitess.io/vitess
cd src/vitess.io/vitess
执行编译
cd ${HOME}/gopath/src/vitess.io/vitess/
./bootstrap.sh
source dev.env
make build
运行local用例
安装MySQL
首先需要安装mysql-server,在上面的步骤中我们已经安装了MySQL5.7。
关闭AppArmor
某些MySQL版本默认的AppArmor配置不能被Vitess识别。如果没有关闭,可能会遇到下面的错误
E0823 02:15:13.109028 31181 mysqld.go:661] mysqld --initialize-insecure failed: /usr/sbin/mysqld: exit status 1, output: mysqld: [ERROR] Could not open required defaults file: /home/xjm/vtdataroot/vttest105636712/vt_0000000001/my.cnf
使用下面命令将AppArmor关闭,并重启机器以确保其生效。
sudo service apparmor stop
sudo service apparmor teardown # safe to ignore if this errors
sudo update-rc.d -f apparmor remove
或者将mysqld设置为complain模式
sudo apt install apparmor apparmor-profiles apparmor-utils
sudo aa-status
sudo aa-complain /usr/sbin/mysqld
修改ETCD_BINDIR
ETCD_BINDIR默认为"${VTROOT}/dist/etcd/etcd-${ETCD_VERSION}-${etcd_platform}-amd64/"。切换到${HOME}/gopath/src/vitess.io/vitess/examples/local目录下
cd ${HOME}/gopath/src/vitess.io/vitess/examples/local
将env.sh的设置ETCD_BINDIR的行替换为下面命令
if [ "$(arch)" == aarch64 ];then
ETCD_BINDIR="${VTROOT}/dist/etcd/etcd-${ETCD_VERSION}-${etcd_platform}-arm64/"
else
ETCD_BINDIR="${VTROOT}/dist/etcd/etcd-${ETCD_VERSION}-${etcd_platform}-amd64/"
fi
运行local cluster
rm -rf ~/vtdataroot/*
pkill -f vtdata
cd ${HOME}/gopath/src/vitess.io/vitess/
source dev.env
cd examples/local
./101_initial_cluster.sh
pgrep -fl vtdataroot
如果运行成功,我们将看到下面的内容
xjm@vitess-arm01:~/gopath/src/vitess.io/vitess/examples/local$ pgrep -fl vtdataroot
513 mysqld
592 vttablet
593 vttablet
594 vttablet
798 vtgate
29649 etcd
29721 vtctld
29869 mysqld_safe
29938 mysqld_safe
30610 mysqld_safe
32466 mysqld
32622 mysqld
连接mysql,并插入数据
xjm@vitess-arm01:~/gopath/src/vitess.io/vitess/examples/local$ mysql -h 127.0.0.1 -P 15306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.10-Vitess (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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 tables;
+-----------------------+
| Tables_in_vt_commerce |
+-----------------------+
| corder |
| customer |
| product |
+-----------------------+
3 rows in set (0.01 sec)
mysql> \q
Bye
xjm@vitess-arm01:~/gopath/src/vitess.io/vitess/examples/local$ mysql -h 127.0.0.1 -P 15306 < ../common/insert_commerce_data.sql
为mysql设置默认配置,mysql命令可以直接连接MySQL服务,
xjm@vitess-arm01:~/gopath/src/vitess.io/vitess/examples/local$ cat << EOF > ~/.my.cnf
> [client]
> host=127.0.0.1
> port=15306
> EOF
xjm@vitess-arm01:~/gopath/src/vitess.io/vitess/examples/local$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.10-Vitess (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> \q
Bye
安装和运行firefox浏览器,输入下面的url就可以web访问vtcld了
http://localhost:15000
垂直分割
创建一个新的数据库customer,将数据库commerce中的表customer和product迁移到数据库customer中。删除commerce中的表customer和product。
./201_customer_keyspace.sh
./202_customer_tablets.sh
./203_vertical_split.sh
./204_vertical_migrate_replicas.sh
./205_vertical_migrate_master.sh
./206_clean_commerce.sh
- 201_customer_keyspace.sh创建新的数据库customer;
- 202_customer_tablets.sh为数据库customer启动tablet(replica,replica,rdonly),并选择其中一个replica为master,数据库customer刚创建时没有数据,传给它的请求会重定向到commerce;
- 203_vertical_split.sh从commerce数据库中将表customer和product拷贝到customer中;
- 204_vertical_migrate_replicas.sh使传给数据库customer的replica和rdonly tablet的请求直接由customer处理,不再重定向给commerce;
- 205_vertical_migrate_master.sh使传给数据库customer的master tablet的请求直接由customer处理,不再重定向给commerce;
- 206_clean_commerce.sh删除commerce数据库中的表customer和product。
水平分割
customer数据库没有分片,当业务量增大到一定程度后,MySQL的性能会达到瓶颈。我们可以对其进行水平分割,即分片,根据列的大小将表分割成多个。本例中将分成-80和-80两个分片。
#### 序列号
表customer和order中有自增列,当分片时,不能很好地运行,Vitess提供了一个替代方案,序列表。序列表时一个未分片的单行表,Vitess可以产生单调增的id。VSchema允许您将表的列与序列表相关联。完成此操作后,该表上的插入将透明地从序列表中获取ID,填写值,然后将行路由到适当的分片。
序列表是未分片的表,因此将被存储在commerce数据库中。序列表的结构如下:
CREATE TABLE customer_seq (id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence';
INSERT INTO customer_seq (id, next_id, cache) VALUES (0, 1000, 100);
CREATE TABLE order_seq (id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence';
INSERT INTO order_seq (id, next_id, cache) VALUES (0, 1000, 100);
注意创建表语句中的“vitess_sequence” comment,VTTablet将使用该元数据将该表当作是序列号。
- id总是0;
- next_id设置为1000,该值应大于目前所使用的auto_increment的最大值;
cache指定VTTablet更新next_id前,缓冲值的个数
较大的缓存值性能较好,但会更快用尽这些值,因为在reparent操作期间,新的主服务器将从next_id值开始。更新VSchema告知VTGate服务器这些序列表
{ "tables": { "customer_seq": { "type": "sequence" }, "order_seq": { "type": "sequence" }, "product": {} } }
Vindexs索引
下一个需要确定的是分片键,即主索引,这是一个复杂的决定,需要考虑下面的问题:
- 哪些是最高QPS查询,这些查询的where语句是什么
- 列的基数,基数必须高,即列的可取值多
- 我们是否希望将同一事务中的某些行放在一起?
- 我们是否希望某些行在一起以支持分片内联接?
在本例中customer和order表都将选择customer_id作为主索引,数据库customer的VSshema如下:
{
"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"
}
}
}
}
按照下面步骤进行水平分片:
运行下面的命令,使上面的决定生效
./301_customer_sharded.sh
创建新的分片
将已存在的分片分割成更小的分片,Shard 0 已经存在,下面命令将添加两个新的分片-80和80-。CopySchema将直接将schema从shard 0拷贝到新的shard中。
./302_new_shards.sh
-80和80-有什么含义呢?
- 代表一个范围,左边-的是包含80,右边-是不包含80
- 它们的表示法为十六进制
- 前缀-:小于右值的所有值
- 后缀-:大于等于左值的所有值
- 单个-:表示所有值
- 水平分割
本过程于为split clone,于 VerticalSplitClone 类似
./303_horizontal_split.sh
- 迁移服务
与垂直分割中的migrate类似,将请求从shard 0 迁移到shard -80和shard 80-。
./304_migrate_replicas.sh
./305_migrate_master.sh
关闭并删除shard 0
./306_down_shard_0.sh ./307_delete_shard_0.sh