#!/bin/bash
set -e

echo "clean up ..."
mkdir -p logs
mkdir -p SQLs
mkdir -p test_sqls
mkdir -p resultfiles

rm -rf ./logs/*
rm -rf ./SQLs/*
rm -rf ./test_sqls/*
rm -rf ./resultfiles/*
# Drop all old tables
beeline -u jdbc:hive2://localhost:10000  --maxWidth=1000 -f droptables.sql >./logs/dropoldtables.log 2>&1
cp droptables.sql ./logs/
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Clean old logs and drop old tables at: ${thisdate}"
echo "Drop old tables at: ${thisdate}" >> ./logs/dropoldtables.log


echo "Load data to DB ..."
cd ddl
source doLoad.sh
cd ../

seed=${LDENDDATE}
echo "seed = ${seed}"
echo "Generate test SQLs ..."
cd v2.11.0rc2/tools/
./dsqgen -input ../../tdh_templates/templates.lst -directory ../../tdh_templates/ -dialect tdh -scale 10000 -streams 9 -output ../../SQLs/ -RNGSEED ${seed}
cd ../../
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "dsqgen finished at: ${thisdate}"

echo "Power test ..."
time ./run.sh 0

echo ""
echo "Throughtput test 1 ..."
./run.sh 1 &
./run.sh 2 &
./run.sh 3 &
./run.sh 4 &
# end date will be the last date output from run.sh

wait

echo ""
echo "Maintenance test 1 ..."

cd MT
./doRefresh.sh 1 2 & 

sleep 60

echo "Data Accessibility Test"
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Collect tpcds15 disk4 status before failure at: ${thisdate}"
echo "Collect tpcds15 disk4 status before failure at: ${thisdate}" > ../logs/Disk_Status_Good.out
pdsh -w tpcds[15] "ls -l /mnt/disk4/hadoop " >> ../logs/Disk_Status_Good.out
pdsh -w tpcds[15] "ls -l /mnt/disk4/shiva-tabletserver " >> ../logs/Disk_Status_Good.out

thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "chmod -rw to make data dir not read, not write ${thisdate}"
pdsh -w tpcds[15] "chmod -rw -R /mnt/disk4/hadoop/data"
pdsh -w tpcds[15] "chmod -rw -R /mnt/disk4/shiva-tabletserver/data"

thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Collect tpcds15 disk4 status after chmod at: ${thisdate}"
echo "Collect tpcds15 disk4 status after chmod at: ${thisdate}" > ../logs/Disk_Status_Failed.out
pdsh -w tpcds[15] "ls -l /mnt/disk4/hadoop " >> ../logs/Disk_Status_Failed.out
pdsh -w tpcds[15] "ls -l /mnt/disk4/shiva-tabletserver " >> ../logs/Disk_Status_Failed.out

wait 

cd ../

echo ""
echo "Throughtput test 2 ..."
./run.sh 5 &
./run.sh 6 &
./run.sh 7 &
./run.sh 8 &
# end date will be the last date output from run.sh

wait

cd MT
echo ""
echo ""
echo "Maintenance test 2 ..."
./doRefresh.sh 2 2
cd ../
echo ""
echo ""
./Check_Update_data.sh

# collecting configuration details
thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo "Collect configuration details at: ${thisdate}"
echo "Collect Transwarp configuration details at: ${thisdate}" > ./logs/TDH_Config.log
./collect_Transwarp_ArgoDB_Configs.sh >> ./logs/TDH_Config.log
echo "Collect system configuration details at: ${thisdate}" > ./logs/Sys_Config.log
./collect_System_Configs.sh >> ./logs/Sys_Config.log

# get result files
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_0 /tmp/q64_resultfile_0
mv /tmp/q64_resultfile_0 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_1 /tmp/q64_resultfile_1
mv /tmp/q64_resultfile_1 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_2 /tmp/q64_resultfile_2
mv /tmp/q64_resultfile_2 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_3 /tmp/q64_resultfile_3
mv /tmp/q64_resultfile_3 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_4 /tmp/q64_resultfile_4
mv /tmp/q64_resultfile_4 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_5 /tmp/q64_resultfile_5
mv /tmp/q64_resultfile_5 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_6 /tmp/q64_resultfile_6
mv /tmp/q64_resultfile_6 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_7 /tmp/q64_resultfile_7
mv /tmp/q64_resultfile_7 ./resultfiles/
  hdfs dfs  -getmerge /argodbstorage1/user/hive/warehouse/tpcds_holodesk_10000.db/hive/q64_resultfile_8 /tmp/q64_resultfile_8
mv /tmp/q64_resultfile_8 ./resultfiles/

# backup logs and SQLs
cp -r logs logs_${seed}
cp -r SQLs SQLs_${seed}
cp -r test_sqls test_sqls_${seed}
cp -r resultfiles resultfiles_${seed}

thisdate=`date +%m-%d-%H:%M:%S.%2N`
echo " Finish at: ${thisdate}"

