# Copyright 2023 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Preparation.
=> DBGInvoke __init_fail_point()

mysql> drop table if exists test.t
mysql> create table test.t (id int, value varchar(64))
mysql> insert into test.t values(1,'a'),(2,'b'),(3,'c')
mysql> alter table test.t set tiflash replica 1

func> wait_table test t
mysql> analyze table test.t


# Data.

## exception before mpp register non root mpp task
=> DBGInvoke __disable_fail_point(pause_before_register_non_root_mpp_task)
=> DBGInvoke __enable_pause_fail_point(pause_before_register_non_root_mpp_task, 5)
mysql> use test; set @@tidb_isolation_read_engines='tiflash'; set @@tidb_allow_mpp=1; select count(value), id from t group by id;
+--------------+------+
| count(value) | id   |
+--------------+------+
|            1 |    1 |
|            1 |    2 |
|            1 |    3 |
+--------------+------+
=> DBGInvoke __disable_fail_point(pause_before_register_non_root_mpp_task)
=> DBGInvoke __enable_pause_fail_point(pause_before_register_non_root_mpp_task, 15)
mysql> use test; set @@tidb_isolation_read_engines='tiflash'; set @@tidb_allow_mpp=1; select count(value), id from t group by id;
{#REGEXP}.*Can't find task*
=> DBGInvoke __disable_fail_point(pause_before_register_non_root_mpp_task)

=> DBGInvoke __disable_fail_point(pause_before_make_non_root_mpp_task_active)
=> DBGInvoke __enable_pause_fail_point(pause_before_make_non_root_mpp_task_active, 5)
mysql> use test; set @@tidb_isolation_read_engines='tiflash'; set @@tidb_allow_mpp=1; select count(value), id from t group by id;
+--------------+------+
| count(value) | id   |
+--------------+------+
|            1 |    1 |
|            1 |    2 |
|            1 |    3 |
+--------------+------+
=> DBGInvoke __disable_fail_point(pause_before_make_non_root_mpp_task_active)
=> DBGInvoke __enable_pause_fail_point(pause_before_make_non_root_mpp_task_active, 15)
mysql> use test; set @@tidb_isolation_read_engines='tiflash'; set @@tidb_allow_mpp=1; select count(value), id from t group by id;
{#REGEXP}.*Can't find task*
=> DBGInvoke __disable_fail_point(pause_before_make_non_root_mpp_task_active)


# Clean up.
mysql> drop table if exists test.t
