1 /*
2    Copyright (c) 2011, 2021, Oracle and/or its affiliates.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "ndb_thd_ndb.h"
26 #include "mysql/plugin.h"          // thd_get_thread_id
27 
28 /*
29   Default value for max number of transactions createable against NDB from
30   the handler. Should really be 2 but there is a transaction to much allocated
31   when lock table is used, and one extra to used for global schema lock.
32 */
33 static const int MAX_TRANSACTIONS= 4;
34 
35 
36 Thd_ndb*
seize(THD * thd)37 Thd_ndb::seize(THD* thd)
38 {
39   DBUG_ENTER("seize_thd_ndb");
40 
41   Thd_ndb* thd_ndb= new Thd_ndb(thd);
42   if (thd_ndb == NULL)
43     DBUG_RETURN(NULL);
44 
45   if (thd_ndb->ndb->init(MAX_TRANSACTIONS) != 0)
46   {
47     DBUG_PRINT("error", ("Ndb::init failed, error: %d  message: %s",
48                          thd_ndb->ndb->getNdbError().code,
49                          thd_ndb->ndb->getNdbError().message));
50 
51     delete thd_ndb;
52     thd_ndb= NULL;
53   }
54   else
55   {
56     thd_ndb->ndb->setCustomData64(thd_get_thread_id(thd));
57   }
58   DBUG_RETURN(thd_ndb);
59 }
60 
61 
62 void
release(Thd_ndb * thd_ndb)63 Thd_ndb::release(Thd_ndb* thd_ndb)
64 {
65   DBUG_ENTER("release_thd_ndb");
66   delete thd_ndb;
67   DBUG_VOID_RETURN;
68 }
69 
70 
71 bool
recycle_ndb(void)72 Thd_ndb::recycle_ndb(void)
73 {
74   DBUG_ENTER("recycle_ndb");
75   DBUG_PRINT("enter", ("ndb: 0x%lx", (long)ndb));
76 
77   assert(global_schema_lock_trans == NULL);
78   assert(trans == NULL);
79 
80   delete ndb;
81   if ((ndb= new Ndb(connection, "")) == NULL)
82   {
83     DBUG_PRINT("error",("failed to allocate Ndb object"));
84     DBUG_RETURN(false);
85   }
86 
87   if (ndb->init(MAX_TRANSACTIONS) != 0)
88   {
89     delete ndb;
90     ndb= NULL;
91     DBUG_PRINT("error", ("Ndb::init failed, %d  message: %s",
92                          ndb->getNdbError().code,
93                          ndb->getNdbError().message));
94     DBUG_RETURN(false);
95   }
96   else
97   {
98     ndb->setCustomData64(thd_get_thread_id(m_thd));
99   }
100 
101   /* Reset last commit epoch for this 'session'. */
102   m_last_commit_epoch_session = 0;
103 
104   DBUG_RETURN(true);
105 }
106 
107 
108 bool
valid_ndb(void) const109 Thd_ndb::valid_ndb(void) const
110 {
111   // The ndb object should be valid as long as a
112   // global schema lock transaction is ongoing
113   if (global_schema_lock_trans)
114     return true;
115 
116   // The ndb object should be valid as long as a
117   // transaction is ongoing
118   if (trans)
119     return true;
120 
121   if (unlikely(m_connect_count != connection->get_connect_count()))
122     return false;
123 
124   return true;
125 }
126 
127 
128 void
init_open_tables()129 Thd_ndb::init_open_tables()
130 {
131   count= 0;
132   m_error= FALSE;
133   my_hash_reset(&open_tables);
134 }
135 
136 
137 /*
138   Used for every additional row operation, to update the guesstimate
139   of pending bytes to send, and to check if it is now time to flush a batch.
140 */
141 
142 bool
add_row_check_if_batch_full(uint size)143 Thd_ndb::add_row_check_if_batch_full(uint size)
144 {
145   if (m_unsent_bytes == 0)
146     free_root(&m_batch_mem_root, MY_MARK_BLOCKS_FREE);
147 
148   uint unsent= m_unsent_bytes;
149   unsent+= size;
150   m_unsent_bytes= unsent;
151   return unsent >= m_batch_size;
152 }
153