From f7c43cb3e7ba54792091451de4010908470878ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Bl=C3=A5udd?= Date: Wed, 18 Nov 2020 17:05:39 +0100 Subject: [PATCH] Bug#32169848 THD_NDB TRANSACTION FUNCTIONALITY [#5] Rename "lock_count" to "external_lock_count", increment the variable only when transaction has been registered sucessfully and thus remove two unneccessary decrement calls. Improve description of the "external_lock_count" variable. Change-Id: I5bfa91fdca8fa2d377b170696b6086293d8cb743 --- storage/ndb/plugin/ha_ndbcluster.cc | 22 +++++++++++----------- storage/ndb/plugin/ndb_thd_ndb.h | 9 ++++++++- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/storage/ndb/plugin/ha_ndbcluster.cc b/storage/ndb/plugin/ha_ndbcluster.cc index 62bf4fe94b10..f59ea959a1c3 100644 --- a/storage/ndb/plugin/ha_ndbcluster.cc +++ b/storage/ndb/plugin/ha_ndbcluster.cc @@ -1138,7 +1138,6 @@ Thd_ndb::Thd_ndb(THD *thd) connection = ndb_get_cluster_connection(); m_connect_count = connection->get_connect_count(); ndb = new Ndb(connection, ""); - lock_count = 0; save_point_count = 0; trans = NULL; m_handler = NULL; @@ -7360,17 +7359,16 @@ int ha_ndbcluster::external_lock(THD *thd, int lock_type) { DBUG_PRINT("enter", ("lock_type != F_UNLCK " "this: %p thd: %p thd_ndb: %p " - "thd_ndb->lock_count: %d", - this, thd, thd_ndb, thd_ndb->lock_count)); + "thd_ndb->external_lock_count: %d", + this, thd, thd_ndb, thd_ndb->external_lock_count)); - if ((error = start_statement(thd, thd_ndb, thd_ndb->lock_count++))) { - thd_ndb->lock_count--; + if ((error = start_statement(thd, thd_ndb, thd_ndb->external_lock_count))) { return error; } if ((error = init_handler_for_statement(thd))) { - thd_ndb->lock_count--; return error; } + thd_ndb->external_lock_count++; return 0; } else { Thd_ndb *thd_ndb = m_thd_ndb; @@ -7378,11 +7376,13 @@ int ha_ndbcluster::external_lock(THD *thd, int lock_type) { DBUG_PRINT("enter", ("lock_type == F_UNLCK " "this: %p thd: %p thd_ndb: %p " - "thd_ndb->lock_count: %d", - this, thd, thd_ndb, thd_ndb->lock_count)); + "thd_ndb->external_lock_count: %d", + this, thd, thd_ndb, thd_ndb->external_lock_count)); + + thd_ndb->external_lock_count--; + if (thd_ndb->external_lock_count == 0) { + DBUG_PRINT("trans", ("Last external_lock() unlock")); - if (!--thd_ndb->lock_count) { - DBUG_PRINT("trans", ("Last external_lock")); const bool autocommit_enabled = !thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN); // Only the 'CREATE TABLE ... SELECT' variant of the @@ -7392,7 +7392,7 @@ int ha_ndbcluster::external_lock(THD *thd, int lock_type) { if (thd_ndb->trans && (autocommit_enabled || is_create_table_select)) { /* - Unlock is done without a transaction commit / rollback. + Unlock is done without transaction commit / rollback. This happens if the thread didn't update any rows as a part of normal DMLs or `CREATE TABLE ... SELECT` DDL . We must in this case close the transaction to release resources diff --git a/storage/ndb/plugin/ndb_thd_ndb.h b/storage/ndb/plugin/ndb_thd_ndb.h index 705d7b08328b..dd4f42708d27 100644 --- a/storage/ndb/plugin/ndb_thd_ndb.h +++ b/storage/ndb/plugin/ndb_thd_ndb.h @@ -54,7 +54,14 @@ class Thd_ndb { class Ndb_cluster_connection *connection; class Ndb *ndb; class ha_ndbcluster *m_handler; - uint lock_count; + + // Reference counter for external_lock() calls. The counter controls that + // the handlerton is registered as being part of the MySQL transaction only at + // the first external_lock() call (when the counter is zero). Also the counter + // controls that any started NDB transaction is closed when external_lock(.., + // F_UNLOCK) is called for the last time (i.e when the counter is back to zero + // again). + uint external_lock_count{0}; // Reference counter for start_stmt() calls. The counter controls that the // handlerton is registered as being part of the MySQL transaction at the