#
# BUG#22516559 MYSQL INSTANCE STALLS WHEN SYNCING FTS INDEX
#

--source include/have_innodb.inc
--source include/have_debug_sync.inc
--source include/not_valgrind.inc
--source include/not_embedded.inc
--source include/not_crashrep.inc
--source include/maybe_versioning.inc

connect (con1,localhost,root,,);
connection default;

--echo # Case 1: Test select and insert(row in both disk and cache)
CREATE TABLE t1 (
        FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
        title VARCHAR(200),
        FULLTEXT(title)
) ENGINE = InnoDB;

INSERT INTO t1(title) VALUES('mysql');
INSERT INTO t1(title) VALUES('database');

connection con1;

SET @old_dbug = @@SESSION.debug_dbug;
SET debug_dbug = '+d,fts_instrument_sync_debug';

SET DEBUG_SYNC= 'fts_write_node SIGNAL written WAIT_FOR selected';

send INSERT INTO t1(title) VALUES('mysql database');

connection default;

SET DEBUG_SYNC= 'now WAIT_FOR written';

SET GLOBAL innodb_ft_aux_table="test/t1";
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE;
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE;
SET GLOBAL innodb_ft_aux_table=default;

SELECT * FROM t1 WHERE MATCH(title) AGAINST('mysql database');

SET DEBUG_SYNC= 'now SIGNAL selected';

connection con1;
--reap

SET @old_dbug = @@SESSION.debug_dbug;

SET GLOBAL innodb_ft_aux_table="test/t1";
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE;
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE;
SET GLOBAL innodb_ft_aux_table=default;

SELECT * FROM t1 WHERE MATCH(title) AGAINST('mysql database');

connection default;

DROP TABLE t1;

--echo # Case 2: Test insert and insert(sync)
CREATE TABLE t1 (
        FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
        title VARCHAR(200),
        FULLTEXT(title)
) ENGINE = InnoDB;

INSERT INTO t1(title) VALUES('mysql');
INSERT INTO t1(title) VALUES('database');

connection con1;

SET debug_dbug = '+d,fts_instrument_sync_debug';

SET DEBUG_SYNC= 'fts_write_node SIGNAL written WAIT_FOR inserted';

send INSERT INTO t1(title) VALUES('mysql database');

connection default;

SET DEBUG_SYNC= 'now WAIT_FOR written';

INSERT INTO t1(title) VALUES('mysql database');

SET DEBUG_SYNC= 'now SIGNAL inserted';

connection con1;
--reap

SET debug_dbug = @old_dbug;

SET GLOBAL innodb_ft_aux_table="test/t1";
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE;
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE;
SET GLOBAL innodb_ft_aux_table=default;

SELECT * FROM t1 WHERE MATCH(title) AGAINST('mysql database');

connection default;
disconnect con1;

DROP TABLE t1;

--echo # Case 3: Test insert crash recovery
--let $_expect_file_name= `select regexp_replace(@@tmpdir, '^.*/','')`
--let $_expect_file_name= $MYSQLTEST_VARDIR/tmp/$_expect_file_name.expect

CREATE TABLE t1 (
        FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
        title VARCHAR(200),
        FULLTEXT(title)
) ENGINE = InnoDB;

INSERT INTO t1(title) VALUES('database');

--write_line restart $MYSQLTEST_VARDIR/tmp/mysqld.1.expect

SET debug_dbug = '+d,fts_instrument_sync_debug,fts_write_node_crash';

--error 2013
INSERT INTO t1(title) VALUES('mysql');

--source include/start_mysqld.inc

-- echo After restart
# PAGE_ROOT_AUTO_INC could contain last failed autoinc value. Avoid
# doing show the result of auto increment field
SELECT title FROM t1 WHERE MATCH(title) AGAINST ('mysql database');

SET @old_dbug = @@SESSION.debug_dbug;

SET debug_dbug = '+d,fts_instrument_sync_debug';

INSERT INTO t1(title) VALUES('mysql');

SET debug_dbug = @old_dbug;

SELECT title FROM t1 WHERE MATCH(title) AGAINST ('mysql database');

DROP TABLE t1;

--echo # Case 4: Test sync commit & rollback in background
CREATE TABLE t1(
        id INT AUTO_INCREMENT,
        title VARCHAR(100),
        FULLTEXT(title),
        PRIMARY KEY(id)) ENGINE=InnoDB;

SET debug_dbug = '+d,fts_instrument_sync';
INSERT INTO t1(title) VALUES('mysql');
SET debug_dbug = @old_dbug;

--source include/restart_mysqld.inc

SET @old_global_dbug = @@GLOBAL.debug_dbug;
SET @old_dbug = @@SESSION.debug_dbug;
SET GLOBAL debug_dbug='+d,fts_instrument_sync,fts_instrument_sync_interrupted';
INSERT INTO t1(title) VALUES('database');
SET GLOBAL debug_dbug = @old_global_dbug;

SET debug_dbug = '+d,fts_instrument_sync_debug';
INSERT INTO t1(title) VALUES('good');
SET debug_dbug = @old_dbug;

SET GLOBAL innodb_ft_aux_table="test/t1";
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE;
SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE;
SET GLOBAL innodb_ft_aux_table=default;

SELECT * FROM t1 WHERE MATCH(title) AGAINST ('mysql database good');

DROP TABLE t1;

--echo #
--echo # MDEV-26273 InnoDB fts DDL fails when
--echo # innodb_force_recovery is set to 2
--echo #

let $restart_parameters=--innodb_force_recovery=2;
--source include/restart_mysqld.inc
CREATE TABLE t1 (FTS_DOC_ID BIGINT UNSIGNED KEY,
                 f1 CHAR(200)) ENGINE=InnoDB;
ALTER TABLE t1 ADD FULLTEXT INDEX(f1);
DROP TABLE t1;
let $restart_parameters=;
--source include/restart_mysqld.inc
