#!/bin/bash

# We run a select/update workload (ratio 1:7) against the master and a select workload
# against the slave.

if [ $DATABASE == "rethinkdb" ]; then
    # Clean up
    ssh $MASTER_SERVER rm -rf /tmp/Replication_master_performance
    ssh $SLAVE_SERVER rm -rf /tmp/Replication_slave_performance

    # The master part
    # TODO: We assume dbench being installed in that static directory on the master...
    ssh $MASTER_SERVER /home/teapot/rethinkdb/bench/dbench/dbench                                                                                        \
        -d \"/tmp/Replication_master_performance\" -H $MASTER_HOSTS            \
        {server}rethinkdb[/tmp/rethinkdb]:\"-m 32768 --master $MASTER_PORT $MASTER_SSD_DRIVES\"                                              \
        {client}stress[$MASTER_STRESS]:\"-c $CANONICAL_CLIENTS -d $CANONICAL_DURATION -w 0/1/0/7 -i $MASTER_TMP_KEY_FILE\"     \
        iostat:1 vmstat:1 rdbstat:1 &
    MASTER_PID=$!

    # Give master some time to startup...
    sleep 30

    # The slave part
    # TODO: We assume dbench being installed in that static directory on the slave...
    ssh $SLAVE_SERVER /home/teapot/rethinkdb/bench/dbench/dbench                                                                                        \
        -d \"/tmp/Replication_slave_performance\" -H $SLAVE_HOSTS            \
        {server}rethinkdb[/tmp/rethinkdb]:\"-m 32768 --slave-of $MASTER_SERVER:$MASTER_PORT $SLAVE_SSD_DRIVES\"                                              \
        {client}stress[$SLAVE_STRESS]:\"-c $CANONICAL_CLIENTS -d $CANONICAL_DURATION -w 0/0/0/1 -i $SLAVE_TMP_KEY_FILE\"     \
        iostat:1 vmstat:1 rdbstat:1
        
    # Wait for dbenches to finish
    wait $MASTER_PID
    
    # Collect results
    mkdir -p $BENCH_DIR/bench_output
    scp -r $MASTER_SERVER:/tmp/Replication_master_performance $BENCH_DIR/bench_output
    scp -r $SLAVE_SERVER:/tmp/Replication_slave_performance $BENCH_DIR/bench_output
else
    echo "No workload configuration for $DATABASE"
fi
