OtterTune配置记录
0. 准备两台Ubuntu 18.04的虚拟机,安装mysql(供server-side存储调优数据用)和postgresql(供client-side存储业务数据用,这里以PostgreSQL为例。因为ottertune官方只给了postgres的knob configuration.....)。然后按照官方配置(https://github.com/cmu-db/ottertune/wiki/Linux-Quick-Setup)安装好必要的包。
Ref: https://www.cnblogs.com/leolztang/p/5094930.html
https://blog.csdn.net/skh2015java/article/details/80156278
https://blog.csdn.net/qq_19330913/article/details/80468494
https://www.cnblogs.com/hfdp/p/6088288.html
https://blog.csdn.net/chengyuqiang/article/details/70153980?utm_source=blogxgwz5
安装配置postgresql:
1 wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - 2 sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main" 3 sudo apt-get update 4 sudo apt-get install postgresql-9.6 5 sudo apt-get remove postgresql-10 #安装9.6时自动带上了10...不知道为啥。要卸载掉防止端口冲突。可以用sudo lsof -i:5432看一下 6 7 2、修改postgres数据库用户的密码 8 打开客户端工具(psql) 9 sudo -u postgres psql 10 其中,sudo -u postgres 是使用postgres 用户登录的意思。PostgreSQL数据默认会创建一个postgres的数据库用户作为数据库的管理员,密码是随机的 11 postgres=# ALTER USER postgres WITH PASSWORD '******'; 12 postgres=#为PostgreSQL下的命令提示符,--注意最后的分号; 13 14 3、退出PostgreSQL psql客户端 15 postgres=# \q 16 17 4、修改ubuntu操作系统的postgres用户的密码(密码要与数据库用户postgres的密码相同) 18 su root 19 删除PostgreSQL用户密码 20 sudo passwd -d postgres 21 passwd -d 是清空指定用户密码的意思 22 设置PostgreSQL系统用户的密码 23 sudo -u postgres passwd 24 按照提示,输入两次新密码 25 26 5、修改PostgresSQL数据库配置实现远程访问 27 sudo gedit /etc/postgresql/9.6/main/postgresql.conf 28 1.监听任何地址访问,修改连接权限 29 #listen_addresses = 'localhost' 改为 listen_addresses = '*' 30 2.启用密码验证 31 #password_encryption = on 改为 password_encryption = on 32 3. 确保端口为5432 33 port = 5432 # (change requires restart) 34 35 sudo gedit /etc/postgresql/9.6/main/pg_hba.conf 36 在文档末尾加上以下内容 37 host all all 0.0.0.0/0 trust 38 39 6、重启服务 40 /etc/init.d/postgresql restart 41 42 7、5432端口的防火墙设置 43 5432为postgreSQL默认的端口 44 sudo iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT 45 46 安装好之后可以 47 psql -U postgres -h 192.168.1.170 48 测试一下 49 50 Ref: https://blog.csdn.net/zj0078/article/details/71156064
修改系统默认python版本
原因:ubuntu系统安装了python2.7和python3.6,默认使用的是python 2.7。导致从2.7找不到包 解决办法:使用以下命令来修改默认python版本: sudo cp /usr/bin/python /usr/bin/python_bak sudo rm /usr/bin/python sudo ln -s /usr/bin/python3.6 /usr/bin/python 这样在终端中运行Python时,默认启动的就是3.6版本了
如果在C2步骤中出错,那么还需要修改默认java版本
sudo update-alternatives --config java 选择/usr/lib/jvm/jdk1.8.0_212/bin/java 所在的number
OtterTune分为两部分:server side: 包括一个MySQL数据库(用于存储调优数据,供ml model用),Django(FrontEnd User Interface),Celery(用于调度ML task);client side: Target_DBMS(存储用户的业务数据用,支持多种DBMS),Controller(用于控制target DBMS),Driver(用于调用controller,入口是fabfile.py)
Ref: https://github.com/cmu-db/ottertune/wiki/Getting-Started-ottertune
https://blog.csdn.net/weixin_40449300/article/details/87904128
https://blog.csdn.net/u013385018/article/details/84028360
https://blog.csdn.net/u013385018/article/details/84201771
说实话见到很多帖子都在讨论它,但大多都是空泛的复述论文,实际上很少有人真正deploy过......这点还是挺让我惊讶的
-------------------------------------------Test OtterTune with real target DBMS-------------------------------------------
这里以postgreSQL为例。
按照S1, S2, C1, C2, C3, C4, C5, S3, S4, C6的顺序执行如下步骤即可
-----------------------------------------------------Server-side配置-----------------------------------------------------
S1. 新建名为ottertune的数据库
mysqladmin create -u root -p ottertune
S2. 编辑配置文件
cd /ottertune/server/website
cp website/settings/credentials_TEMPLATE.py website/settings/credentials.py
并填入USERNAME, PASSWORD等信息,设置DEBUG=True
示例文件:
1 # 2 # OtterTune - credentials_TEMPLATE.py 3 # 4 # Copyright (c) 2017-18, Carnegie Mellon University Database Group 5 # 6 """ 7 Private/custom Django settings for the OtterTune project. 8 9 """ 10 # pylint: disable=invalid-name 11 12 # ============================================== 13 # SECRET KEY CONFIGURATION 14 # ============================================== 15 16 # SECURITY WARNING: keep the secret key used in production secret! 17 SECRET_KEY = 'ADD ME!!' 18 19 # ============================================== 20 # DATABASE CONFIGURATION 21 # ============================================== 22 23 DATABASES = { 24 'default': { 25 'ENGINE': 'django.db.backends.mysql', 26 'NAME': 'ottertune', 27 'USER': 'root', 28 'PASSWORD': '1', 29 'HOST': '', 30 'PORT': '', 31 'OPTIONS': { 32 'init_command': "SET sql_mode='STRICT_TRANS_TABLES',innodb_strict_mode=1", 33 }, 34 } 35 } 36 37 # ============================================== 38 # DEBUG CONFIGURATION 39 # ============================================== 40 41 # Can override the DEBUG setting here 42 DEBUG = True 43 44 # ============================================== 45 # MANAGER CONFIGURATION 46 # ============================================== 47 48 # Admin and managers for this project. These people receive private 49 # site alerts. 50 ADMINS = ( 51 # ('Your Name', 'your_email@example.com'), 52 ) 53 MANAGERS = ADMINS 54 55 # ============================================== 56 # GENERAL CONFIGURATION 57 # ============================================== 58 59 # Hosts/domain names that are valid for this site; required if DEBUG is False 60 # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts 61 ALLOWED_HOSTS = []
S3:Set up Django(用于监控)
Firstly, you need to migrate the Django models into the database. (相当于配置Django网站后端,将需要的表放进mysql的OtterTune数据库内)
python3 manage.py makemigrations website
python3 manage.py migrate
Create the super user
python manage.py createsuperuser
这一步操作完成后,将在mysql的OtterTune数据库中建立如下表:
值得注意的是website_knobcatalog和website_metriccatalog这两个表,分别存储了待观测的knob和metric名称。这是dump出来的表内容(以postgresql为例)
S4:Start Celery(用于调度ML task)
OtterTune uses Celery to schedule machine learning tasks. Before staring the celery worker, you may want to start a message broker required by celery. In our case, we use RabbitMq.
sudo rabbitmq-server -detached
Then you can start the celery worker:
python3 manage.py celery worker --loglevel=info --pool=threads
Then you can start Django Server:
python manage.py runserver 0.0.0.0:8000
OtterTune has periodical ML tasks (i.e. knob identification and metrics pruning), which run every time period. You can use celery beat to start the periodical tasks.
python3 manage.py celerybeat --verbosity=2 --loglevel=info
完成后打开http://127.0.0.1:8080,在里面建立一个tuning session,并记下upload code
-----------------------------------------------------Client-side配置-----------------------------------------------------
首先将ottertune、oltpbench都git clone到home/username/下面
C1. 编辑/ottertune/client/driver/driver_config.json,填入target DBMS类型、用户名密码等信息,并填写其中涉及到的所有配置文件
注意
1. 对于save_path这一项,要事先建立好对应的文件夹
2. 对于upload code这一项,要和S4中分配给的upload code一致
示例文件:
1 -------------/ottertune/client/driver/driver_config.json------------- 2 { 3 "database_type" : "postgres", 4 "database_name" : "tpcc", 5 "database_disk": "/dev/sda1", 6 "database_conf": "/etc/postgresql/9.6/main/postgresql.conf", 7 "database_save_path": "/home/tidb/ottertune", 8 "username" : "postgres", 9 "password" : "******", 10 "oltpbench_home": "/home/tidb/oltpbench", 11 "oltpbench_config": "/home/tidb/oltpbench/config/tpcc_config_postgres.xml", 12 "oltpbench_workload": "tpcc", 13 "oltpbench_log" : "/home/tidb/ottertune/client/driver/oltp.log", 14 "controller_config": "/home/tidb/ottertune/client/controller/config/sample_postgres_config.json", 15 "controller_log" : "/home/tidb/ottertune/client/driver/controller.log", 16 "save_path": "/home/tidb/ottertune/server/results", 17 "upload_url" : "http://127.0.0.1:8000", 18 "upload_code" : "0B58NG1HBQZ4UH24Q00Q", 19 "lhs_knob_path" : "/home/tidb/ottertune/client/driver/knobs/postgres-96.json", 20 "lhs_save_path" : "/home/tidb/ottertune/client/driver/configs" 21 } 22 23 24 -------------/home/tidb/ottertune/client/controller/config/sample_postgres_config.json------------- 25 { 26 "database_type" : "postgres", 27 "database_url" : "jdbc:postgresql://localhost:5432/postgres", 28 "username" : "postgres", 29 "password" : "asdfgh", 30 "upload_code" : "DEPRECATED", 31 "upload_url" : "DEPRECATED", 32 "workload_name" : "tpcc" 33 } 34 35 -------------/oltpbench/config/tpcc_config_postgres.xml------------- 36 <?xml version="1.0"?> 37 <parameters> 38 39 <!-- Connection details --> 40 <dbtype>postgres</dbtype> 41 <driver>org.postgresql.Driver</driver> 42 <DBUrl>jdbc:postgresql://localhost:5432/tpcc</DBUrl> 43 <username>postgres</username> 44 <password>******</password> 45 <isolation>TRANSACTION_READ_COMMITTED</isolation> 46 47 <!-- Scale factor is the number of warehouses in TPCC --> 48 <scalefactor>2</scalefactor> 49 50 <!-- The workload --> 51 <terminals>2</terminals> 52 <works> 53 <work> 54 <time>10</time> 55 <rate>10000</rate> 56 <ratelimited bench="tpcc">true</ratelimited> 57 <weights>45,43,4,4,4</weights> 58 </work> 59 </works> 60 61 <!-- TPCC specific --> 62 <transactiontypes> 63 <transactiontype> 64 <name>NewOrder</name> 65 </transactiontype> 66 <transactiontype> 67 <name>Payment</name> 68 </transactiontype> 69 <transactiontype> 70 <name>OrderStatus</name> 71 </transactiontype> 72 <transactiontype> 73 <name>Delivery</name> 74 </transactiontype> 75 <transactiontype> 76 <name>StockLevel</name> 77 </transactiontype> 78 </transactiontypes> 79 </parameters>
C2. 配置oltpbench(用于周期性在target DBMS上run benchmark):参考https://github.com/oltpbenchmark/oltpbench/wiki#Environment%20Setup
如果出现错误,sudo update-alternatives --config java,选择/usr/lib/jvm/jdk1.8.0_212/bin/java 所在的number
C3. 在target DBMS(postgres)中新建名为tpcc的数据库,供oltpbench用
psql -U postgres -h 192.168.1.170
postgres=# create database tpcc owner postgres;
postgres=# grant all privileges on database tpcc to postgres;
C4. load oltpbench into target DBMS
./oltpbenchmark -b tpcc -c config/tpcc_config_postgres.xml --create=true --load=true
C5. Build controller
cd ottertune/client/controller
gradle build
C6. 运行
运行时发现几个bug:
1. 在ottertune/client/driver/fabfile.py line236, 'Output into file' 需要改为 'Output Raw data into file'
2. loop一次之后发现这玩意把我的postgreSQL的配置(postgresql.conf)改崩溃了......感觉要自己魔改一番了...
目测是ottertune在修改时没有考虑好数据类型(原版的random_page_cost值是4.0 科学计数法在这里肯定是认不出来的啊)
After setting the above three configurations, you can run the loop in the description above. In each loop, it collects target DBMS info, uploads to the server, gets new recommended configuration, installs the config and restarts DBMS. Users can continue to run loops until they are satisfied with the recommended configuration. Functions are defined in the driver file ottertune/client/driver/fabfile.py
fab loop runs one single loop.
fab run_loops:max_iter=10 runs 10 loops. You can set max_iter to change the maximum iterations.
注意在刚开始运行时,因为ML model缺乏足够的训练数据,所以生成的configuration可能是random的:
-------------------------------------------Test OtterTune with fake workload data-------------------------------------------
这里fake workload data是用data_generator.py自己random生成出来的,不是真的在target DBMS上跑出来的knob-metric samples。
个人感觉这个就是用来测试下django interface的显示效果....实际意义不大
F0:首先完成上面的S1, S2, C1, C2, C3, C4操作
F1:生成一些fake workload data
cd /ottertune/server/website/script/controller_simulator/
python data_generator.py 2 5
这里2表示workload数量(模拟在target DBMS跑过的不同workload)
5表示对于每个workload,会被观测到的训练样本数(knob/metric samples. 模拟每个workload在target DBMS上观测到的结果)
生成好的数据会被放在generated_data文件夹下
F2:Initialize Django Server,初始化ottertune MySQL数据库
cd /ottertune/server/website fab
fab create_test_website
F3:启动Django
fab start_debug_server
F4:上传fake workload data到Django interface
cd /ottertune/server/website/script/controller_simulator/
python upload_data.py ./generated_data 0987654321
[其中0987654321是upload code,和下图网页上的upload code对应。详细可参考 https://github.com/cmu-db/ottertune/wiki/Finding-a-Session's-Upload-Code ]
在bash中可以看到,当上传好后,系统就会自动运行ML pipeline
F5:进入http://192.168.1.172:8000,用user/abcd123登陆,单击test_project -> tuning_session.
左边可以选择workload和要优化的metric,右边就可以看到metric在不同knob configuration下的表现。点击下面具体的Result ID就可以看到这一metric value对应的knob
posted on 2019-05-22 05:02 Pentium.Labs 阅读(2501) 评论(0) 编辑 收藏 举报