From 4257916f00e8178ad931631a24a42f2eb8372dd1 Mon Sep 17 00:00:00 2001 From: Pravin Gohite Date: Tue, 12 Mar 2019 08:14:25 -0700 Subject: [PATCH] Added docker support for mysql (#1522) --- docker/Dockerfile | 4 +- docker/README.md | 12 ++++ docker/config_template_mysql.yaml | 100 ++++++++++++++++++++++++++++++ docker/docker-compose-mysql.yml | 39 ++++++++++++ docker/start.sh | 96 ++++++++++++++++------------ 5 files changed, 211 insertions(+), 40 deletions(-) create mode 100644 docker/config_template_mysql.yaml create mode 100644 docker/docker-compose-mysql.yml diff --git a/docker/Dockerfile b/docker/Dockerfile index 22c52ffbb5c..0c1592c58ac 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -44,6 +44,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ tcpdump \ netcat \ python-pip \ + mysql-client \ && rm -rf /var/lib/apt/lists/* RUN pip install cqlsh @@ -54,7 +55,8 @@ ENV CADENCE_HOME /cadence EXPOSE 7933 7934 7935 7939 COPY ./start.sh $CADENCE_HOME/start.sh -COPY ./config_template.yaml $CADENCE_HOME/config/docker_template.yaml +COPY ./config_template.yaml $CADENCE_HOME/config/docker_template_cassandra.yaml +COPY ./config_template_mysql.yaml $CADENCE_HOME/config/docker_template_mysql.yaml RUN chmod a+x $CADENCE_HOME/start.sh COPY --from=builder /go/src/github.com/uber/cadence/cadence-cassandra-tool /cadence diff --git a/docker/README.md b/docker/README.md index 85072cd3b79..29d0fed8f6a 100644 --- a/docker/README.md +++ b/docker/README.md @@ -50,6 +50,18 @@ docker-compose down docker-compose up ``` +Running cadence service with MySQL +----------------------------------------- + +Run cadence with MySQL instead of cassandra, use following commads: + +``` +docker-compose -f docker-compose-mysql.yml up +docker-compose -f docker-compose-mysql.yml down +``` + +Please note that SQL support is still in active developement and it is not production ready yet. + Quickstart for production ========================= In a typical production setting, dependencies (cassandra / statsd server) are diff --git a/docker/config_template_mysql.yaml b/docker/config_template_mysql.yaml new file mode 100644 index 00000000000..ec9ce88398e --- /dev/null +++ b/docker/config_template_mysql.yaml @@ -0,0 +1,100 @@ +log: + stdout: true + level: "${LOG_LEVEL}" + +persistence: + defaultStore: mysql-default + visibilityStore: mysql-visibility + numHistoryShards: ${NUM_HISTORY_SHARDS} + datastores: + mysql-default: + sql: + driverName: "mysql" + databaseName: "${DBNAME}" + connectAddr: "${MYSQL_SEEDS}:3306" + connectProtocol: "tcp" + user: "${MYSQL_USER}" + password: "${MYSQL_PWD}" + mysql-visibility: + sql: + driverName: "mysql" + databaseName: "${VISIBILITY_DBNAME}" + connectAddr: "${MYSQL_SEEDS}:3306" + connectProtocol: "tcp" + user: "${MYSQL_USER}" + password: "${MYSQL_PWD}" + +ringpop: + name: cadence + bootstrapMode: hosts + bootstrapHosts: ${RINGPOP_SEEDS_JSON_ARRAY} + maxJoinDuration: 30s + +services: + frontend: + rpc: + port: 7933 + bindOnIP: ${BIND_ON_IP} + metrics: + statsd: + hostPort: "${STATSD_ENDPOINT}" + prefix: "cadence-frontend" + + matching: + rpc: + port: 7935 + bindOnIP: ${BIND_ON_IP} + metrics: + statsd: + hostPort: "${STATSD_ENDPOINT}" + prefix: "cadence-matching" + + history: + rpc: + port: 7934 + bindOnIP: ${BIND_ON_IP} + metrics: + statsd: + hostPort: "${STATSD_ENDPOINT}" + prefix: "cadence-history" + + worker: + rpc: + port: 7939 + bindOnIP: ${BIND_ON_IP} + metrics: + statsd: + hostPort: "${STATSD_ENDPOINT}" + prefix: "cadence-worker" + +clustersInfo: + enableGlobalDomain: false + failoverVersionIncrement: 10 + masterClusterName: "active" + currentClusterName: "active" + clusterInitialFailoverVersion: + active: 0 + clusterAddress: + active: + rpcName: "cadence-frontend" + rpcAddress: "127.0.0.1:7933" + +dcRedirectionPolicy: + policy: "noop" + toDC: "" + +archival: + status: enabled + blobstore: + storeDirectory: "/tmp/blobstore/" + defaultBucket: + name: "cadence-development" + owner: "cadence" + retentionDays: 10 + customBuckets: + - name: "custom-bucket-1" + owner: "custom-owner-1" + retentionDays: 10 + - name: "custom-bucket-2" + owner: "custom-owner-2" + retentionDays: 5 diff --git a/docker/docker-compose-mysql.yml b/docker/docker-compose-mysql.yml new file mode 100644 index 00000000000..bd748840772 --- /dev/null +++ b/docker/docker-compose-mysql.yml @@ -0,0 +1,39 @@ +version: '3' +services: + mysql: + image: mysql:5.7 + ports: + - "3306:3306" + environment: + - "MYSQL_ROOT_PASSWORD=root" + statsd: + image: hopsoft/graphite-statsd + ports: + - "8080:80" + - "2003:2003" + - "8125:8125" + - "8126:8126" + cadence: + image: ubercadence/server:0.5.4 + ports: + - "7933:7933" + - "7934:7934" + - "7935:7935" + - "7939:7939" + environment: + - "DB=mysql" + - "MYSQL_USER=root" + - "MYSQL_PWD=root" + - "MYSQL_SEEDS=mysql" + - "STATSD_ENDPOINT=statsd:8125" + depends_on: + - mysql + - statsd + cadence-web: + image: ubercadence/web:3.1.2 + environment: + - "CADENCE_TCHANNEL_PEERS=cadence:7933" + ports: + - "8088:8088" + depends_on: + - cadence diff --git a/docker/start.sh b/docker/start.sh index 5ef11f21bc3..8cb755998b6 100644 --- a/docker/start.sh +++ b/docker/start.sh @@ -20,7 +20,26 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -setup_schema() { +CADENCE_HOME=$1 +DB="${DB:-cassandra}" +CFG_TEMPLATE=docker_template_$DB.yaml +SERVICES="${SERVICES:-history,matching,frontend,worker}" +RF=${RF:-1} +export LOG_LEVEL="${LOG_LEVEL:-info}" +export NUM_HISTORY_SHARDS=${NUM_HISTORY_SHARDS:-4} + +# cassandra env +export KEYSPACE="${KEYSPACE:-cadence}" +export VISIBILITY_KEYSPACE="${VISIBILITY_KEYSPACE:-cadence_visibility}" +export CASSANDRA_CONSISTENCY="${CASSANDRA_CONSISTENCY:-One}" + +#mysql env +export DBNAME="${DBNAME:-cadence}" +export VISIBILITY_DBNAME="${VISIBILITY_DBNAME:-cadence_visibility}" +export DB_PORT=${DB_PORT:-3306} + + +setup_cassandra_schema() { SCHEMA_DIR=$CADENCE_HOME/schema/cassandra/cadence/versioned $CADENCE_HOME/cadence-cassandra-tool --ep $CASSANDRA_SEEDS create -k $KEYSPACE --rf $RF $CADENCE_HOME/cadence-cassandra-tool --ep $CASSANDRA_SEEDS -k $KEYSPACE setup-schema -v 0.0 @@ -31,6 +50,21 @@ setup_schema() { $CADENCE_HOME/cadence-cassandra-tool --ep $CASSANDRA_SEEDS -k $VISIBILITY_KEYSPACE update-schema -d $VISIBILITY_SCHEMA_DIR } +setup_mysql_schema() { + mysql -h $MYSQL_SEEDS -u$MYSQL_USER -p$MYSQL_PWD < $CADENCE_HOME/schema/mysql/v57/cadence/database.sql + mysql -h $MYSQL_SEEDS -u$MYSQL_USER -p$MYSQL_PWD $DBNAME < $CADENCE_HOME/schema/mysql/v57/cadence/schema.sql + mysql -h $MYSQL_SEEDS -u$MYSQL_USER -p$MYSQL_PWD < $CADENCE_HOME/schema/mysql/v57/visibility/database.sql + mysql -h $MYSQL_SEEDS -u$MYSQL_USER -p$MYSQL_PWD $VISIBILITY_DBNAME < $CADENCE_HOME/schema/mysql/v57/visibility/schema.sql +} + +setup_schema() { + if [ "$DB" == "mysql" ]; then + setup_mysql_schema + else + setup_cassandra_schema + fi +} + wait_for_cassandra() { server=`echo $CASSANDRA_SEEDS | awk -F ',' '{print $1}'` until cqlsh --cqlversion=3.4.4 $server < /dev/null; do @@ -40,6 +74,26 @@ wait_for_cassandra() { echo 'cassandra started' } +wait_for_mysql() { + server=`echo $MYSQL_SEEDS | awk -F ',' '{print $1}'` + nc -z $server $DB_PORT < /dev/null + until [ $? -eq 0 ]; do + echo 'waiting for mysql to start up' + sleep 1 + nc -z $server $DB_PORT < /dev/null + done + echo 'mysql started' +} + +wait_for_db() { + if [ "$DB" == "mysql" ]; then + wait_for_mysql + else + wait_for_cassandra + fi +} + + json_array() { echo -n '[' while [ $# -gt 0 ]; do @@ -52,7 +106,6 @@ json_array() { } init_env() { - export HOST_IP=`hostname --ip-address` if [ "$BIND_ON_LOCALHOST" == true ] || [ "$BIND_ON_IP" == "127.0.0.1" ]; then @@ -69,55 +122,20 @@ init_env() { // this env variable is deprecated export BIND_ON_LOCALHOST=false - if [ -z "$KEYSPACE" ]; then - export KEYSPACE="cadence" - fi - - if [ -z "$VISIBILITY_KEYSPACE" ]; then - export VISIBILITY_KEYSPACE="cadence_visibility" - fi - - if [ -z "$CASSANDRA_SEEDS" ]; then - export CASSANDRA_SEEDS=$HOST_IP - fi - - if [ -z "$CASSANDRA_CONSISTENCY" ]; then - export CASSANDRA_CONSISTENCY="One" - fi - if [ -z "$RINGPOP_SEEDS" ]; then export RINGPOP_SEEDS_JSON_ARRAY="[\"$HOST_IP:7933\",\"$HOST_IP:7934\",\"$HOST_IP:7935\",\"$HOST_IP:7939\"]" else array=(${RINGPOP_SEEDS//,/ }) export RINGPOP_SEEDS_JSON_ARRAY=$(json_array "${array[@]}") fi - - if [ -z "$NUM_HISTORY_SHARDS" ]; then - export NUM_HISTORY_SHARDS=4 - fi - - if [ -z "$LOG_LEVEL" ]; then - export LOG_LEVEL="info" - fi } -CADENCE_HOME=$1 - -if [ -z "$RF" ]; then - RF=1 -fi - -if [ -z "$SERVICES" ]; then - SERVICES="history,matching,frontend,worker" -fi - init_env -wait_for_cassandra - +wait_for_db if [ "$SKIP_SCHEMA_SETUP" != true ]; then setup_schema fi # fix up config -envsubst < config/docker_template.yaml > config/docker.yaml +envsubst < config/$CFG_TEMPLATE > config/docker.yaml ./cadence-server --root $CADENCE_HOME --env docker start --services=$SERVICES