#!/usr/bin/env bash

yum -y install fuse lsof
export GRPC_GO_LOG_VERBOSITY_LEVEL=0
export WAREHOUSES=16
export THREADS=4
export STORES=4
mkdir -p /random/logs /random/error-logs /random/config
for((idx=0;idx<=${STORES};idx++));
do
  cp -f /random/tiflash-${idx}.toml /random/config/tiflash-$1-${idx}.toml
  old="tiflash-learner-${idx}.toml"
  new="config\/tiflash-learner-$1-${idx}.toml"
  sed -i "s/${old}/${new}/g" /random/config/tiflash-$1-${idx}.toml
  cp -f /random/tiflash-learner-${idx}.toml /random/config/tiflash-learner-$1-${idx}.toml
  old="tiflash_tikv_${idx}.log"
  new="tiflash_tikv_$1_${idx}.log"
  sed -i "s/${old}/${new}/g" /random/config/tiflash-learner-$1-${idx}.toml
  umount -f /tmp/ffs-client-${idx}
  rm -rf /tmp/ffs-client-${idx}/*
done
nohup /random/random.test -test.paniconexit0 -test.timeout=240h0m0s -test.v=true -test.run=TestRandom --random=true --client=/tmp --log-level=debug > /random/logs/random_$1.log 2>&1 &
sleep 10s
mkdir -p /tmp/pd-0/data /tmp/tidb-0
nohup /random/pd-server --name=pd-0 --data-dir=/tmp/pd-0/data --peer-urls=http://127.0.0.1:2380 --advertise-peer-urls=http://127.0.0.1:2380 --client-urls=http://127.0.0.1:2379 --advertise-client-urls=http://127.0.0.1:2379 --log-file=/tmp/pd-0/pd.log --initial-cluster=pd-0=http://127.0.0.1:2380 &
sleep 10s
for((idx=0;idx<=${STORES};idx++));
do
  nohup /random/tikv-server --addr=127.0.0.1:2016${idx} --advertise-addr=127.0.0.1:2016${idx} --status-addr=127.0.0.1:2018${idx} --pd=http://127.0.0.1:2379 --config=/random/tikv.toml --data-dir=/tmp/ffs-client-${idx}/tikv-${idx} --log-file=/random/logs/tikv_$1_${idx}.log &
  sleep 10s
done
sleep 10s
nohup /random/tidb-server -P 4000 --store=tikv --host=127.0.0.1 --status=10080 --path=127.0.0.1:2379 --log-file=/tmp/tidb-0/tidb.log &
sleep 10s
for((idx=0;idx<=${STORES};idx++));
do
  nohup /random/tiflash/tiflash server --config-file=/random/config/tiflash-$1-${idx}.toml &
  sleep 10s
done
delay=`expr ${1} \* 60 + 60`
sleep ${delay}s
curl --retry 5 --retry-delay 5 http://127.0.0.1:2379
/random/go-tpc -H 127.0.0.1 -P 4000 tpcc --warehouses ${WAREHOUSES} --threads ${THREADS} prepare
 sleep 10s
/random/go-tpc -H 127.0.0.1 -P 4000 tpcc --warehouses ${WAREHOUSES} --threads ${THREADS} run --time 1m
sleep 10s
mysql -h127.0.0.1 -P4000 -uroot -e"ALTER DATABASE test SET TIFLASH REPLICA 1;"
sleep 180s
mysql -h127.0.0.1 -P4000 -uroot -e"SELECT * FROM information_schema.tiflash_replica WHERE TABLE_SCHEMA = 'test';"
for i in {1..1000}
do
  echo "loop-$i"
  ps aux | grep "tiflash" |grep -v grep| awk '{print $2}' | xargs kill -9
  ps aux | grep "tidb-server" |grep -v grep| awk '{print $2}' | xargs kill -9
  ps aux | grep "tikv-server" |grep -v grep| awk '{print $2}' | xargs kill -9
  sleep 30s
  rand=`expr $RANDOM % 4`
  if [[ 0 == $rand ]]
  then
    curl --retry 5 --retry-delay 5 -X POST http://127.0.0.1:10100/client/stop
    sleep 180s
    for((idx=0;idx<=${STORES};idx++));
    do
      for((j=0;j<=10;j++));
      do
        umount -f /tmp/ffs-client-${idx}
        rm -rf /tmp/ffs-client-${idx}/*
        pid=`lsof -i:2016${idx}|grep " (LISTEN)"|awk '{print $2}'|uniq`
        if [ $pid ]; then
          kill -9 $pid
          sleep 120s
        fi
        pid=`lsof -i:2017${idx}|grep " (LISTEN)"|awk '{print $2}'|uniq`
        if [ $pid ]; then
          kill -9 $pid
          sleep 120s
        fi
        umount -f /tmp/ffs-client-${idx}
        rm -rf /tmp/ffs-client-${idx}/*
        sleep 3s
      done
    done
    sleep 10s
    curl --retry 5 --retry-delay 5 -X POST http://127.0.0.1:10100/client/start
    sleep 30s
    mounted=`df -h|grep ffs|wc -l`
    echo "mounted ${mounted}"
    if [ ${mounted} = 5 ]; then
      echo "mounted ${mounted} ok"
    else
      echo "failed to mount"
      mv /random/logs/random_$1.log /random/error-logs/random_$1.log;
      df -h|grep ffs
      exit 1
    fi
  else
    for((idx=0;idx<=${STORES};idx++));
    do
      for((j=0;j<=10;j++));
      do
        pid=`lsof -i:2016${idx}|grep " (LISTEN)"|awk '{print $2}'|uniq`
        if [ $pid ]; then
          kill -9 $pid
          sleep 120s
        fi
        pid=`lsof -i:2017${idx}|grep " (LISTEN)"|awk '{print $2}'|uniq`
        if [ $pid ]; then
          kill -9 $pid
          sleep 120s
        fi
        sleep 3s
      done
    done
  fi
  sleep 30s
  for((idx=0;idx<=${STORES};idx++));
  do
    pid=`lsof -i:2016${idx}|grep " (LISTEN)"|awk '{print $2}'|uniq`
    if [ $pid ]; then
      kill -9 $pid
      sleep 120s
    fi
    nohup /random/tikv-server --addr=127.0.0.1:2016${idx} --advertise-addr=127.0.0.1:2016${idx} --status-addr=127.0.0.1:2018${idx} --pd=http://127.0.0.1:2379 --config=/random/tikv.toml --data-dir=/tmp/ffs-client-${idx}/tikv-${idx} --log-file=/random/logs/tikv_$1_${idx}.log &
    sleep 10s
  done
  sleep 10s
  pid=`lsof -i:4000|grep " (LISTEN)"|awk '{print $2}'|uniq`
  if [ $pid ]; then
    kill -9 $pid
    sleep 120s
  fi
  nohup /random/tidb-server -P 4000 --store=tikv --host=127.0.0.1 --status=10080 --path=127.0.0.1:2379 --log-file=/tmp/tidb-0/tidb.log &
  sleep 10s
  for((idx=0;idx<=${STORES};idx++));
  do
    pid=`lsof -i:2017${idx}|grep " (LISTEN)"|awk '{print $2}'|uniq`
    if [ $pid ]; then
      kill -9 $pid
      sleep 120s
    fi
    nohup /random/tiflash/tiflash server --config-file=/random/config/tiflash-$1-${idx}.toml &
    sleep 10s
  done
  sleep 180s
  if grep -q 'panic: test timed out after' /random/logs/random_$1.log; then
      grep 'panic: test timed out after' /random/logs/random_$1.log
      exit 0
  elif grep -q 'FATAL' /random/logs/tikv_$1_*.log /random/logs/tiflash_tikv_$1_*.log /random/logs/random_$1.log; then
    grep 'FATAL' /random/logs/tikv_$1_*.log /random/logs/tiflash_tikv_$1_*.log /random/logs/random_$1.log
    mv /random/logs/tikv_$1_*.log /random/error-logs/.
    mv /random/logs/tiflash_tikv_$1_*.log /random/error-logs/.
    mv /random/logs/random_$1.log /random/error-logs/random_$1.log;
    df -h|grep ffs
    exit 1
  fi
  /random/go-tpc -H 127.0.0.1 -P 4000 tpcc --warehouses ${WAREHOUSES} --threads ${THREADS} run --time 1m
  sleep 10s
done
exit 0
