#!/bin/bash

function verify_raft_sync()
{
	local ti_file="${1}"
	local ti_args="${2}"
	local cmd_mod_names="${3}"
	local cmd_hosts="${4}"
	local cmd_indexes="${5}"
	local mods="${6}"
	shift 6

	local db='test'
	if [ ! -z "${1+x}" ] && [ ! -z "${1}" ]; then
		local db="${1}"
	fi

	local table='hello'
	if [ ! -z "${2+x}" ] && [ ! -z "${2}" ]; then
		local table="${2}"
	fi

	local ti="${integrated}/ops/ti.sh"

	local show_sql="show databases"
	local tidb_ok=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'mysql/tikv' "${show_sql}" | grep 'test'`
	if [ ! -z "${tidb_ok}" ]; then
		echo "OK     tidb: ${show_sql}"
	else
		echo "FAILED tidb: ${show_sql}"
		return 1
	fi

	local tiflash_ok=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'ch' "${show_sql}" | grep ''`
	if [ ! -z "${tiflash_ok}" ]; then
		echo "OK     tiflash: ${show_sql}"
	else
		echo "FAILED tiflash: ${show_sql}"
		return 1
	fi

	local drop_sql="drop table if exists ${db}.${table}"

	local drop_err=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" -k "${ti_args}" \
		"${ti_file}" 'mysql' "${drop_sql}" "${db}" 'false' 2>&1`
	if [ -z "${drop_err}" ]; then
		echo "OK     tidb: ${drop_sql}"
	else
		echo "FAILED tidb: ${drop_sql}, result=\"${drop_err}\""
		return 1
	fi

	local create_sql="create table if not exists ${db}.${table} (col varchar(10))"
	"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" -k "${ti_args}" \
		"${ti_file}" 'mysql' "${create_sql}" "${db}" 'false'
	local create_res=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" -k \
		"${ti_args}" "${ti_file}" 'mysql' "show create table ${db}.${table}" 2>&1`
	local create_ok=`echo "${create_res}" | grep 'InnoDB'`
	if [ ! -z "${create_ok}" ]; then
		echo "OK     tidb: ${create_sql}"
	else
		echo "FAILED tidb: ${create_sql}, result=\"${create_res}\""
		return 1
	fi

	local read_sql="select col from ${table}"
	local insert_sql="insert into ${db}.${table} (col) values ('world')"
	"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" -k "${ti_args}" \
		"${ti_file}" 'mysql' "${insert_sql}" "${db}" 'false'
	local insert_res=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" -k \
		"${ti_args}" "${ti_file}" 'mysql/tikv' "${read_sql}" "${db}" 2>&1`
	local insert_ok=`echo "${insert_res}" | grep 'world'`
	if [ ! -z "${insert_ok}" ]; then
		echo "OK     tidb: ${insert_sql}"
	else
		echo "FAILED tidb: ${insert_sql}, result=\"${insert_res}\""
		return 1
	fi

	local sync_sql="alter table ${db}.${table} set tiflash replica 1"
	local enable_sync_res=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'syncing/set' "${db}" "${table}" 2>&1`
	local enable_sync_ok=`echo "${enable_sync_res}" | grep '\->'`
	if [ ! -z "${enable_sync_ok}" ]; then
		echo "OK     tidb: ${sync_sql}"
	else
		echo "FAILED tidb: ${sync_sql}, result=\"${enable_sync_res}\""
		return 1
	fi

	"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'wait/available' "${db}" "${table}"

	local read_res=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'mysql/learner' "${read_sql}" "${db}" 2>&1`
	local read_ok=`echo "${read_res}" | grep 'world'`
	if [ -z "${read_ok}" ]; then
		echo "FAILED tiflash over tidb: ${read_sql}, result=\"${read_res}\""
		return 1
	else
		echo "OK     tiflash over tidb: ${read_sql}"
	fi

	local mapped_db=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'ch/mapped_database' "${db}"`
	local mapped_table=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'ch/mapped_table' "${db}" "${table}" "false"`
	local tiflash_read_sql="select col from ${mapped_table}"
	local read_res=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'ch' "${tiflash_read_sql}" "${mapped_db}" 'TabSeparated' 2>&1`
	local read_err=`echo "${read_res}" | grep 'Exception'`
	local read_ok=`echo "${read_res}" | grep 'world'`
	if [ ! -z "${read_err}" ]; then
		echo "WARN   tiflash: ${tiflash_read_sql}: ${read_err}"
	elif [ -z "${read_ok}" ]; then
		echo "WARN   tiflash: ${tiflash_read_sql}: result=\"${read_res}\""
	else
		echo "OK     tiflash: ${tiflash_read_sql}"
	fi

	local drop_err=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" \
		-k "${ti_args}" "${ti_file}" 'mysql' "${drop_sql}" "${db}" 'false'`
	if [ -z "${drop_err}" ]; then
		echo "OK     tidb: ${drop_sql}"
	else
		echo "FAILED tidb: ${drop_sql}, result=\"${drop_err}\""
		return 1
	fi
}

source "`cd $(dirname ${BASH_SOURCE[0]}) && pwd`/_env.sh"
auto_error_handle
verify_raft_sync "${@}"
