#!/bin/bash

function cmd_ti_wait_syncing()
{
	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

	if [ -z "${2+x}" ]; then
		echo "[cmd wait/table] usage: <cmd> database table [wait_for_available=true] [timeout=1200]" >&2
		return 1
	fi

	local db="${1}"
	local table="${2}"

	# https://unix.stackexchange.com/questions/346878/can-i-get-the-exit-code-from-a-sub-shell-launched-with-command
	# we need to separate local declaration from the assignment into two separate steps
	# to get return exit code of subshell
	local mapped_db=""
	mapped_db=$("${integrated}/ops/ti.sh" -h "${cmd_hosts}" -m "${cmd_mod_names}" \
		-i "${cmd_indexes}" -k "${ti_args}" "${ti_file}" 'ch/mapped_database' "${db}")
	if [ $? -ne 0 ]; then
		echo "[cmd wait/table] can not get mapped name for database ${db}, got: ${mapped_db}" >&2
		return 1
	fi
	local mapped_table=""
	mapped_table=$("${integrated}/ops/ti.sh" -h "${cmd_hosts}" -m "${cmd_mod_names}" \
		-i "${cmd_indexes}" -k "${ti_args}" "${ti_file}" 'ch/mapped_table' "${db}" "${table}" 'false')
	if [ $? -ne 0 ]; then
		echo "[cmd wait/table] can not get mapped name for table ${db}.${table}, got: ${mapped_db}.${mapped_table}" >&2
		return 1
	fi

	local wait_for_available='true'
	if [ ! -z "${3+x}" ]; then
		local wait_for_available="${3}"
		if [ "${wait_for_available}" == 'available' ]; then
			local wait_for_available='true'
		fi
	fi

	local timeout='1200'
	if [ ! -z "${4+x}" ]; then
		local timeout="${4}"
	fi

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

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

	echo "=> wait for ${db}.${table} is syncing to tiflash"

	local index=`from_mods_random_mod "${mods}" 'tiflash'`
	if [ -z "${index}" ]; then
		echo "[cmd wait/table] no selected tiflash" >&2
		return 1
	fi

	local failed='true'
	local available=''
	for (( i = 0; i < "${timeout}"; i++ )); do
		local db_info=`"${ti}" -h "${cmd_hosts}" -m "${cmd_mod_names}" -i "${cmd_indexes}" -k "${ti_args}" \
			"${ti_file}" 'syncing/show' "${db}" | grep -v "^${db}$" | trim_host 2>/dev/null`
		local table_info=`echo "${db_info}" | grep " ${table} "`
		local table_status=`echo "${table_info}" | awk '{print $2}'`
		if [ ${?} == 0 ] && [ ! -z "${table_info}" ]; then
			if [ "${wait_for_available}" != 'true' ] || [ "${table_status}" == 'available' ]; then
				if [ "${table_status}" == 'available' ]; then
					local available='available'
				fi
				local failed='false'
				break
			fi
		fi
		if [ $((${i} % 10)) = 0 ] && [ ${i} -ge 10 ]; then
			if [ "${wait_for_available}" == 'true' ]; then
				echo "   #${i} waiting for ${db}.${table} learner storage available, now: ${table_status}"
			else
				echo "   #${i} waiting for ${db}.${table} in syncing list of pd"
			fi
		fi
		sleep 1
	done

	if [ "${failed}" == 'true' ]; then
		echo "   can not reach syncing status" >&2
		return 1
	else
		if [ ! -z "${available}" ]; then
			echo "   syncing and available"
		else
			echo "   syncing but not available yet"
			return 1
		fi
	fi
}

cmd_ti_wait_syncing "${@}"
