From 0be70a1b773ce66ef1803fcce19522fd9c60c07d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Lindstr=C3=B6m?= <jan.lindstrom@mariadb.com>
Date: Mon, 17 Aug 2020 08:57:13 +0300
Subject: [PATCH] MDEV-23483: Set Galera SST thd as system thread

Revert change to MDL and set SST donor thread as a system thread.
Joiner thread was already a system thread.
---
 mysql-test/suite/galera/t/mdev-22543.test | 20 +++++++++---------
 sql/mdl.cc                                | 25 ++++++++---------------
 sql/wsrep_sst.cc                          | 18 +++++-----------
 sql/wsrep_utils.cc                        |  4 +++-
 sql/wsrep_utils.h                         |  2 +-
 5 files changed, 27 insertions(+), 42 deletions(-)

diff --git a/mysql-test/suite/galera/t/mdev-22543.test b/mysql-test/suite/galera/t/mdev-22543.test
index 53662e36942..1e7d3712639 100644
--- a/mysql-test/suite/galera/t/mdev-22543.test
+++ b/mysql-test/suite/galera/t/mdev-22543.test
@@ -5,15 +5,15 @@
 --source include/galera_cluster.inc
 --source include/have_debug.inc
 --source include/have_debug_sync.inc
- 
+
 --let $node_1 = node_1
 --let $node_2 = node_2
 --source include/auto_increment_offset_save.inc
- 
+
 --let $galera_connection_name = node_1_ctrl
 --let $galera_server_number = 1
 --source include/galera_connect.inc
- 
+
 #
 # Run UPDATE on node_1 and make it block before table locks are taken.
 # This should block FTWRL.
@@ -23,10 +23,10 @@ CREATE TABLE t1 (f1 INT PRIMARY KEY, f2 INT);
 INSERT INTO t1 VALUES (1, 1);
 SET DEBUG_SYNC = "before_lock_tables_takes_lock SIGNAL sync_point_reached WAIT_FOR sync_point_continue";
 --send UPDATE t1 SET f2 = 2 WHERE f1 = 1
- 
+
 --connection node_1_ctrl
 SET DEBUG_SYNC = "now WAIT_FOR sync_point_reached";
- 
+
 #
 # Restart node_2, force SST.
 #
@@ -40,19 +40,19 @@ SET DEBUG_SYNC = "now WAIT_FOR sync_point_reached";
 # If the bug is present, FTWRL times out on node_1 in couple of
 # seconds and node_2 fails to join.
 --sleep 10
- 
+
 --connection node_1_ctrl
 SET DEBUG_SYNC = "now SIGNAL sync_point_continue";
- 
+
 --connection node_1
 --reap
 SET DEBUG_SYNC = "RESET";
- 
+
 --connection node_2
 --enable_reconnect
 --source include/wait_until_connected_again.inc
- 
+
 --connection node_1
 DROP TABLE t1;
- 
+
 --source include/auto_increment_offset_restore.inc
diff --git a/sql/mdl.cc b/sql/mdl.cc
index 14a1f17fe86..9eeb82eeffd 100644
--- a/sql/mdl.cc
+++ b/sql/mdl.cc
@@ -25,7 +25,6 @@
 #include <mysql/psi/mysql_stage.h>
 #include "wsrep_mysqld.h"
 #include "wsrep_thd.h"
-#include "wsrep_sst.h"
 
 #ifdef HAVE_PSI_INTERFACE
 static PSI_mutex_key key_MDL_wait_LOCK_wait_status;
@@ -2138,26 +2137,18 @@ MDL_context::acquire_lock(MDL_request *mdl_request, double lock_wait_timeout)
     wait_status= m_wait.timed_wait(m_owner, &abs_shortwait, FALSE,
                                    mdl_request->key.get_wait_state_name());
 
-    THD* thd= m_owner->get_thd();
-
     if (wait_status != MDL_wait::EMPTY)
       break;
     /* Check if the client is gone while we were waiting. */
-    if (! thd_is_connected(thd))
+    if (! thd_is_connected(m_owner->get_thd()))
     {
-#if defined(WITH_WSREP) && !defined(EMBEDDED_LIBRARY)
-      // During SST client might not be connected
-      if (!wsrep_is_sst_progress())
-#endif
-      {
-        /*
-         * The client is disconnected. Don't wait forever:
-         * assume it's the same as a wait timeout, this
-         * ensures all error handling is correct.
-         */
-        wait_status= MDL_wait::TIMEOUT;
-        break;
-      }
+      /*
+       * The client is disconnected. Don't wait forever:
+       * assume it's the same as a wait timeout, this
+       * ensures all error handling is correct.
+       */
+      wait_status= MDL_wait::TIMEOUT;
+      break;
     }
 
     mysql_prlock_wrlock(&lock->m_rwlock);
diff --git a/sql/wsrep_sst.cc b/sql/wsrep_sst.cc
index a6accd52910..af3a80cb67c 100644
--- a/sql/wsrep_sst.cc
+++ b/sql/wsrep_sst.cc
@@ -1,4 +1,4 @@
-/* Copyright 2008-2015 Codership Oy <http://www.codership.com>
+/* Copyright 2008-2020 Codership Oy <http://www.codership.com>
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -192,7 +192,6 @@ bool wsrep_before_SE()
 
 static bool            sst_complete = false;
 static bool            sst_needed   = false;
-static bool            sst_in_progress = false;
 
 #define WSREP_EXTEND_TIMEOUT_INTERVAL 30
 #define WSREP_TIMEDWAIT_SECONDS 10
@@ -1545,11 +1544,11 @@ static void* sst_donor_thread (void* a)
   wsrep_uuid_t  ret_uuid= WSREP_UUID_UNDEFINED;
   // seqno of complete SST
   wsrep_seqno_t ret_seqno= WSREP_SEQNO_UNDEFINED;
-  // SST is now in progress
-  sst_in_progress= true;
 
-  wsp::thd thd(FALSE); // we turn off wsrep_on for this THD so that it can
-                       // operate with wsrep_ready == OFF
+  // We turn off wsrep_on for this THD so that it can
+  // operate with wsrep_ready == OFF
+  // We also set this SST thread THD as system thread
+  wsp::thd thd(FALSE, true);
   wsp::process proc(arg->cmd, "r", arg->env);
 
   err= proc.error();
@@ -1648,8 +1647,6 @@ wait_signal:
   wsrep->sst_sent (wsrep, &state_id, -err);
   proc.wait();
 
-  sst_in_progress= false;
-
   return NULL;
 }
 
@@ -1824,8 +1821,3 @@ void wsrep_SE_initialized()
 {
   SE_initialized = true;
 }
-
-bool wsrep_is_sst_progress()
-{
-  return (sst_in_progress);
-}
diff --git a/sql/wsrep_utils.cc b/sql/wsrep_utils.cc
index 599ece4cb40..e86be3e5d93 100644
--- a/sql/wsrep_utils.cc
+++ b/sql/wsrep_utils.cc
@@ -413,7 +413,7 @@ process::wait ()
   return err_;
 }
 
-thd::thd (my_bool won) : init(), ptr(new THD(0))
+thd::thd (my_bool won, bool system_thread) : init(), ptr(new THD(0))
 {
   if (ptr)
   {
@@ -421,6 +421,8 @@ thd::thd (my_bool won) : init(), ptr(new THD(0))
     ptr->store_globals();
     ptr->variables.option_bits&= ~OPTION_BIN_LOG; // disable binlog
     ptr->variables.wsrep_on = won;
+    if (system_thread)
+      ptr->system_thread= SYSTEM_THREAD_GENERIC;
     ptr->security_ctx->master_access= ~(ulong)0;
     lex_start(ptr);
   }
diff --git a/sql/wsrep_utils.h b/sql/wsrep_utils.h
index 616a6d3f457..d4d9e62a620 100644
--- a/sql/wsrep_utils.h
+++ b/sql/wsrep_utils.h
@@ -298,7 +298,7 @@ class thd
 
 public:
 
-  thd(my_bool wsrep_on);
+  thd(my_bool wsrep_on, bool system_thread=false);
   ~thd();
   THD* const ptr;
 };