database.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/bin/bash
  2. #
  3. # Configures the database server for TFB
  4. #
  5. # Note: This is not used for Travis-CI. See run-ci.py to see
  6. # how databases are configured for Travis.
  7. #
  8. # Note on Compatibility: TFB *only* supports Ubuntu 14.04 64bit
  9. # (e.g. trusty64). However, it's nice to retain 12.04 support
  10. # where possible, as it's still heavily used.
  11. #
  12. # Database setup is one core area where we can help ensure TFB
  13. # works on 12.04 with minimal frustration. In some cases we
  14. # manually install the DB version that's expected, instead of the
  15. # 12.04 default. In other cases we can use a 12.04 specific
  16. # configuration file. These patches are not intended to enable
  17. # benchmarking (e.g. there are no guarantees that
  18. # the databases will be tuned for performance correctly), but
  19. # they do allow users on 12.04 to install and run most TFB tests.
  20. # Some tests internally have 12.04 incompatibilities, we make no
  21. # concentrated effort to address these cases, but PR's for specific
  22. # problems are welcome
  23. set -x
  24. export DEBIAN_FRONTEND=noninteractive
  25. source /etc/lsb-release
  26. export TFB_DISTRIB_ID=$DISTRIB_ID
  27. export TFB_DISTRIB_RELEASE=$DISTRIB_RELEASE
  28. export TFB_DISTRIB_CODENAME=$DISTRIB_CODENAME
  29. export TFB_DISTRIB_DESCRIPTION=$DISTRIB_DESCRIPTION
  30. ##############################
  31. # check environment
  32. ##############################
  33. # verify that $TFB_DBHOST is set
  34. echo "TFB_DBHOST: $TFB_DBHOST"
  35. [ -z "$TFB_DBHOST" ] && echo "ERROR: TFB_DBHOST is not set!"
  36. ##############################
  37. # Prerequisites
  38. ##############################
  39. sudo apt-get -y update
  40. # WARNING: DONT PUT A SPACE AFTER ANY BACKSLASH OR APT WILL BREAK
  41. # Dpkg::Options avoid hangs on Travis-CI, don't affect clean systems
  42. sudo apt-get -y install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
  43. build-essential \
  44. git \
  45. libev-dev \
  46. libpq-dev \
  47. libreadline6-dev \
  48. postgresql `# Installs 9.1 or 9.3, based on Ubuntu version` \
  49. redis-server `# Installs 2.4 or 2.6, based on Ubuntu version` \
  50. lsb-core `# Ensure that lsb_release can be used`
  51. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  52. # Create a user-owned directory for our databases
  53. sudo mkdir -p /ssd
  54. sudo mkdir -p /ssd/log
  55. sudo chown -R $USER:$USER /ssd
  56. # Additional user account (only use if required)
  57. sudo useradd benchmarkdbuser -p benchmarkdbpass
  58. ##############################
  59. # MySQL
  60. ##############################
  61. echo "Setting up MySQL database"
  62. sudo sh -c "echo mysql-server mysql-server/root_password_again select secret | debconf-set-selections"
  63. sudo sh -c "echo mysql-server mysql-server/root_password select secret | debconf-set-selections"
  64. sudo apt-get -y install mysql-server
  65. sudo stop mysql
  66. # disable checking of disk size
  67. sudo mv mysql /etc/init.d/mysql
  68. sudo chmod +x /etc/init.d/mysql
  69. sudo mv mysql.conf /etc/init/mysql.conf
  70. # use the my.cnf file to overwrite /etc/mysql/my.cnf
  71. sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
  72. sudo mv my.cnf /etc/mysql/my.cnf
  73. sudo rm -rf /ssd/mysql
  74. sudo rm -rf /ssd/log/mysql
  75. sudo cp -R -p /var/lib/mysql /ssd/
  76. sudo cp -R -p /var/log/mysql /ssd/log
  77. sudo cp usr.sbin.mysqld /etc/apparmor.d/
  78. sudo /etc/init.d/apparmor reload
  79. sudo start mysql
  80. # Insert data
  81. mysql -uroot -psecret < create.sql
  82. rm create.sql
  83. ##############################
  84. # Postgres
  85. ##############################
  86. echo "Setting up Postgres database"
  87. if [ "$TFB_DISTRIB_CODENAME" == "precise" ]; then
  88. echo "WARNING: Force upgrading Postgres for Ubuntu 12.04"
  89. sudo apt-get remove -y postgresql postgresql-9.1 postgresql-client-9.1
  90. echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
  91. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  92. sudo apt-get update
  93. sudo apt-get install -y postgresql-9.3 postgresql-client-9.3
  94. sudo service postgresql start
  95. fi
  96. sudo service postgresql stop
  97. # Sometimes this doesn't work with postgresql
  98. sudo killall -s 9 -u postgres
  99. sudo mv postgresql.conf /etc/postgresql/9.3/main/postgresql.conf
  100. sudo mv pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf
  101. sudo rm -rf /ssd/postgresql
  102. sudo cp -R -p /var/lib/postgresql/9.3/main /ssd/postgresql
  103. sudo mv 60-postgresql-shm.conf /etc/sysctl.d/60-postgresql-shm.conf
  104. sudo service postgresql start
  105. sudo -u postgres psql template1 < create-postgres-database.sql
  106. sudo -u benchmarkdbuser psql hello_world < create-postgres.sql
  107. rm create-postgres-database.sql create-postgres.sql
  108. # Last chance to make sure postgresql starts up correctly
  109. sudo killall -s 9 -u postgres
  110. sudo service postgresql restart
  111. ##############################
  112. # MongoDB
  113. #
  114. # Note for 12.04: Using mongodb.org ensures 2.6 is installed
  115. ##############################
  116. echo "Setting up MongoDB database"
  117. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  118. echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  119. sudo apt-get -y update
  120. sudo apt-get -y remove mongodb-clients
  121. sudo apt-get -y install mongodb-org
  122. sudo service mongod stop
  123. sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
  124. sudo cp mongodb.conf /etc/mongodb.conf
  125. sudo mv mongodb.conf /etc/mongod.conf
  126. sudo rm -rf /ssd/mongodb
  127. sudo rm -rf /ssd/log/mongodb
  128. sudo cp -R -p /var/lib/mongodb /ssd/
  129. sudo cp -R -p /var/log/mongodb /ssd/log/
  130. sudo service mongod start
  131. for i in {1..45}; do
  132. nc -z localhost 27017 && break || sleep 1;
  133. echo "Waiting for MongoDB ($i/45}"
  134. done
  135. nc -z localhost 27017
  136. if [ $? -eq 0 ]; then
  137. mongo < create.js
  138. rm create.js
  139. mongod --version
  140. else
  141. >&2 echo "MongoDB did not start, skipping"
  142. fi
  143. ##############################
  144. # Apache Cassandra
  145. ##############################
  146. echo "Setting up Apache Cassandra database"
  147. sudo apt-get install -qqy openjdk-7-jdk
  148. sudo addgroup --system cassandra
  149. sudo adduser --system --home /ssd/cassandra --no-create-home --ingroup cassandra cassandra
  150. export CASS_V=2.0.12
  151. wget -nv http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  152. sudo tar xzf apache-cassandra-$CASS_V-bin.tar.gz -C /opt
  153. sudo ln -s /opt/apache-cassandra-$CASS_V /opt/cassandra
  154. rm -rf /ssd/cassandra /ssd/log/cassandra
  155. mkdir -p /ssd/cassandra /ssd/log/cassandra
  156. sudo chown -R cassandra:cassandra /ssd/cassandra /ssd/log/cassandra
  157. cp cassandra/cassandra.yaml cassandra/cassandra.yaml.mod
  158. cat <<EOF > cassandra/cass_conf_replace.sed
  159. s/- seeds: "\([^"]*\)"/- seeds: "$TFB_DBHOST"/
  160. s/listen_address: \(.*\)/listen_address: $TFB_DBHOST/
  161. s/rpc_address: \(.*\)/rpc_address: $TFB_DBHOST/
  162. EOF
  163. sed -i -f cassandra/cass_conf_replace.sed cassandra/cassandra.yaml.mod
  164. sudo cp -f cassandra/cassandra.init /etc/init.d/cassandra
  165. sudo cp -f cassandra/cassandra.init.env /etc/default/cassandra
  166. sudo cp -f cassandra/cassandra.yaml.mod /opt/apache-cassandra-$CASS_V/conf/cassandra.yaml
  167. sudo cp -f cassandra/log4j-server.properties /opt/apache-cassandra-$CASS_V/conf
  168. sudo update-rc.d cassandra defaults
  169. sudo service cassandra restart
  170. for i in {1..45}; do
  171. nc -z $TFB_DBHOST 9160 && break || sleep 1;
  172. echo "Waiting for Cassandra ($i/45}"
  173. done
  174. nc -z $TFB_DBHOST 9160
  175. if [ $? -eq 0 ]; then
  176. cat cassandra/cleanup-keyspace.cql | /opt/apache-cassandra-$CASS_V/bin/cqlsh $TFB_DBHOST
  177. python cassandra/db-data-gen.py > cassandra/tfb-data.cql
  178. /opt/apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/create-keyspace.cql $TFB_DBHOST
  179. /opt/apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/tfb-data.cql $TFB_DBHOST
  180. else
  181. >&2 echo "Cassandra did not start, skipping"
  182. fi
  183. ##############################
  184. # Redis
  185. ##############################
  186. echo "Setting up Redis database"
  187. if [ "$TFB_DISTRIB_CODENAME" == "precise" ]; then
  188. echo "WARNING: Downgrading Redis configuration for Ubuntu 12.04"
  189. # On 12.04, Redis 2.4 is installed. It doesn't support
  190. # some of the 2.6 options, so we have to remove or comment
  191. # those
  192. sed -i 's/tcp-keepalive/# tcp-keepalive/' redis.conf
  193. sed -i 's/stop-writes-on-bgsave-error/# stop-writes-on-bgsave-error/' redis.conf
  194. sed -i 's/rdbchecksum/# rdbchecksum/' redis.conf
  195. sed -i 's/slave-read-only/# slave-read-only/' redis.conf
  196. sed -i 's/repl-disable-tcp-nodelay/# repl-disable-tcp-nodelay/' redis.conf
  197. sed -i 's/slave-priority/# slave-priority/' redis.conf
  198. sed -i 's/auto-aof-rewrite-percentage/# auto-aof-rewrite-percentage/' redis.conf
  199. sed -i 's/auto-aof-rewrite-min-size/# auto-aof-rewrite-min-size/' redis.conf
  200. sed -i 's/lua-time-limit/# lua-time-limit/' redis.conf
  201. sed -i 's/notify-keyspace-events/# notify-keyspace-events/' redis.conf
  202. sed -i 's/hash-max-ziplist-entries/# hash-max-ziplist-entries/' redis.conf
  203. sed -i 's/hash-max-ziplist-value/# hash-max-ziplist-value/' redis.conf
  204. sed -i 's/zset-max-ziplist-entries/# zset-max-ziplist-entries/' redis.conf
  205. sed -i 's/zset-max-ziplist-value/# zset-max-ziplist-value/' redis.conf
  206. sed -i 's/client-output-buffer-limit/# client-output-buffer-limit/' redis.conf
  207. sed -i 's/hz 10/# hz 10/' redis.conf
  208. sed -i 's/aof-rewrite-incremental-fsync/# aof-rewrite-incremental-fsync/' redis.conf
  209. fi
  210. sudo service redis-server stop
  211. sudo mv redis.conf /etc/redis/redis.conf
  212. sudo service redis-server start
  213. bash create-redis.sh
  214. rm create-redis.sh