1 /*
2    Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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 // Typedefs for long names
26 typedef NdbDictionary::Object NDBOBJ;
27 typedef NdbDictionary::Column NDBCOL;
28 typedef NdbDictionary::Table NDBTAB;
29 typedef NdbDictionary::Index  NDBINDEX;
30 typedef NdbDictionary::Dictionary  NDBDICT;
31 typedef NdbDictionary::Event  NDBEVENT;
32 
33 #define IS_TMP_PREFIX(A) (is_prefix(A, tmp_file_prefix))
34 
35 #define INJECTOR_EVENT_LEN 200
36 
37 #define NDB_INVALID_SCHEMA_OBJECT 241
38 
39 extern handlerton *ndbcluster_hton;
40 
41 class Ndb_event_data
42 {
43 public:
Ndb_event_data(NDB_SHARE * the_share)44   Ndb_event_data(NDB_SHARE *the_share) :
45     shadow_table(0),
46     share(the_share)
47   {
48     ndb_value[0]= 0;
49     ndb_value[1]= 0;
50   }
~Ndb_event_data()51   ~Ndb_event_data()
52   {
53     if (shadow_table)
54       closefrm(shadow_table, 1);
55     shadow_table= 0;
56     free_root(&mem_root, MYF(0));
57     share= 0;
58     /*
59        ndbvalue[] allocated with my_multi_malloc
60        so only first pointer should be freed
61     */
62     my_free(ndb_value[0], MYF(MY_WME|MY_ALLOW_ZERO_PTR));
63   }
64   MEM_ROOT mem_root;
65   TABLE *shadow_table;
66   NDB_SHARE *share;
67   NdbValue *ndb_value[2];
68 };
69 
70 /*
71   The numbers below must not change as they
72   are passed between mysql servers, and if changed
73   would break compatablility.  Add new numbers to
74   the end.
75 */
76 enum SCHEMA_OP_TYPE
77 {
78   SOT_DROP_TABLE= 0,
79   SOT_CREATE_TABLE= 1,
80   SOT_RENAME_TABLE_NEW= 2,
81   SOT_ALTER_TABLE_COMMIT= 3,
82   SOT_DROP_DB= 4,
83   SOT_CREATE_DB= 5,
84   SOT_ALTER_DB= 6,
85   SOT_CLEAR_SLOCK= 7,
86   SOT_TABLESPACE= 8,
87   SOT_LOGFILE_GROUP= 9,
88   SOT_RENAME_TABLE= 10,
89   SOT_TRUNCATE_TABLE= 11,
90   SOT_RENAME_TABLE_PREPARE= 12,
91   SOT_ONLINE_ALTER_TABLE_PREPARE= 13,
92   SOT_ONLINE_ALTER_TABLE_COMMIT= 14,
93   SOT_CREATE_USER= 15,
94   SOT_DROP_USER= 16,
95   SOT_RENAME_USER= 17,
96   SOT_GRANT= 18,
97   SOT_REVOKE= 19
98 };
99 
100 const uint max_ndb_nodes= 256; /* multiple of 32 */
101 
102 static const char *ha_ndb_ext=".ndb";
103 
104 #ifdef HAVE_NDB_BINLOG
105 #define NDB_EXCEPTIONS_TABLE_SUFFIX "$EX"
106 #define NDB_EXCEPTIONS_TABLE_SUFFIX_LOWER "$ex"
107 
108 const uint error_conflict_fn_violation= 9999;
109 #endif /* HAVE_NDB_BINLOG */
110 
111 
112 class Mutex_guard
113 {
114 public:
Mutex_guard(pthread_mutex_t & mutex)115   Mutex_guard(pthread_mutex_t &mutex) : m_mutex(mutex)
116   {
117     pthread_mutex_lock(&m_mutex);
118   };
~Mutex_guard()119   ~Mutex_guard()
120   {
121     pthread_mutex_unlock(&m_mutex);
122   };
123 private:
124   pthread_mutex_t &m_mutex;
125 };
126 
127 
128 extern Ndb_cluster_connection* g_ndb_cluster_connection;
129 
130 extern unsigned char g_node_id_map[max_ndb_nodes];
131 extern pthread_mutex_t LOCK_ndb_util_thread;
132 extern pthread_cond_t COND_ndb_util_thread;
133 extern pthread_mutex_t LOCK_ndb_index_stat_thread;
134 extern pthread_cond_t COND_ndb_index_stat_thread;
135 extern pthread_mutex_t ndbcluster_mutex;
136 extern HASH ndbcluster_open_tables;
137 
138 /*
139   Initialize the binlog part of the ndb handlerton
140 */
141 void ndbcluster_binlog_init_handlerton();
142 /*
143   Initialize the binlog part of the NDB_SHARE
144 */
145 int ndbcluster_binlog_init_share(THD *thd, NDB_SHARE *share, TABLE *table);
146 int ndbcluster_create_binlog_setup(THD *thd, Ndb *ndb, const char *key,
147                                    uint key_len,
148                                    const char *db,
149                                    const char *table_name,
150                                    TABLE * table);
151 int ndbcluster_create_event(THD *thd, Ndb *ndb, const NDBTAB *table,
152                             const char *event_name, NDB_SHARE *share,
153                             int push_warning= 0);
154 int ndbcluster_create_event_ops(THD *thd,
155                                 NDB_SHARE *share,
156                                 const NDBTAB *ndbtab,
157                                 const char *event_name);
158 int ndbcluster_log_schema_op(THD *thd,
159                              const char *query, int query_length,
160                              const char *db, const char *table_name,
161                              uint32 ndb_table_id,
162                              uint32 ndb_table_version,
163                              enum SCHEMA_OP_TYPE type,
164                              const char *new_db,
165                              const char *new_table_name);
166 int ndbcluster_drop_event(THD *thd, Ndb *ndb, NDB_SHARE *share,
167                           const char *type_str,
168                           const char * dbname, const char * tabname);
169 int ndbcluster_handle_drop_table(THD *thd, Ndb *ndb, NDB_SHARE *share,
170                                  const char *type_str,
171                                  const char * db, const char * tabname);
172 void ndb_rep_event_name(String *event_name,
173                         const char *db, const char *tbl, my_bool full);
174 #ifdef HAVE_NDB_BINLOG
175 int
176 ndbcluster_get_binlog_replication_info(THD *thd, Ndb *ndb,
177                                        const char* db,
178                                        const char* table_name,
179                                        uint server_id,
180                                        const TABLE *table,
181                                        Uint32* binlog_flags,
182                                        const st_conflict_fn_def** conflict_fn,
183                                        st_conflict_fn_arg* args,
184                                        Uint32* num_args);
185 int
186 ndbcluster_apply_binlog_replication_info(THD *thd,
187                                          NDB_SHARE *share,
188                                          const NDBTAB* ndbtab,
189                                          TABLE* table,
190                                          const st_conflict_fn_def* conflict_fn,
191                                          const st_conflict_fn_arg* args,
192                                          Uint32 num_args,
193                                          bool do_set_binlog_flags,
194                                          Uint32 binlog_flags);
195 int
196 ndbcluster_read_binlog_replication(THD *thd, Ndb *ndb,
197                                    NDB_SHARE *share,
198                                    const NDBTAB *ndbtab,
199                                    uint server_id,
200                                    TABLE *table,
201                                    bool do_set_binlog_flags);
202 #endif
203 int ndb_create_table_from_engine(THD *thd, const char *db,
204                                  const char *table_name);
205 int ndbcluster_binlog_start();
206 
207 
208 /*
209   Setup function for the ndb binlog component. The function should be
210   called on startup until it succeeds(to allow initial setup) and with
211   regular intervals afterwards to reconnect after a lost cluster
212   connection
213 */
214 bool ndb_binlog_setup(THD *thd);
215 
216 /*
217   Will return true when the ndb binlog component is properly setup
218   and ready to receive events from the cluster. As long as function
219   returns false, all tables in this MySQL Server are opened in read only
220   mode to avoid writes before the binlog is ready to record them.
221  */
222 bool ndb_binlog_is_read_only(void);
223 
224 extern NDB_SHARE *ndb_apply_status_share;
225 extern NDB_SHARE *ndb_schema_share;
226 
227 extern my_bool ndb_binlog_running;
228 
229 bool
230 ndbcluster_show_status_binlog(THD* thd, stat_print_fn *stat_print,
231                               enum ha_stat_type stat_type);
232 
233 /*
234   prototypes for ndb handler utility function also needed by
235   the ndb binlog code
236 */
237 int cmp_frm(const NDBTAB *ndbtab, const void *pack_data,
238             uint pack_length);
239 int ndbcluster_find_all_files(THD *thd);
240 
241 char *ndb_pack_varchar(const NDBCOL *col, char *buf,
242                        const char *str, int sz);
243 
244 NDB_SHARE *ndbcluster_get_share(const char *key,
245                                 TABLE *table,
246                                 bool create_if_not_exists,
247                                 bool have_lock);
248 NDB_SHARE *ndbcluster_get_share(NDB_SHARE *share);
249 void ndbcluster_free_share(NDB_SHARE **share, bool have_lock);
250 void ndbcluster_real_free_share(NDB_SHARE **share);
251 int handle_trailing_share(THD *thd, NDB_SHARE *share);
252 int ndbcluster_prepare_rename_share(NDB_SHARE *share, const char *new_key);
253 int ndbcluster_rename_share(THD *thd, NDB_SHARE *share);
254 int ndbcluster_undo_rename_share(THD *thd, NDB_SHARE *share);
255 inline NDB_SHARE *get_share(const char *key,
256                             TABLE *table,
257                             bool create_if_not_exists= TRUE,
258                             bool have_lock= FALSE)
259 {
260   return ndbcluster_get_share(key, table, create_if_not_exists, have_lock);
261 }
262 
get_share(NDB_SHARE * share)263 inline NDB_SHARE *get_share(NDB_SHARE *share)
264 {
265   return ndbcluster_get_share(share);
266 }
267 
268 inline void free_share(NDB_SHARE **share, bool have_lock= FALSE)
269 {
270   ndbcluster_free_share(share, have_lock);
271 }
272 
273 void set_binlog_flags(NDB_SHARE *share);
274 
275 /*
276   Helper functions
277 */
278 bool
279 ndbcluster_check_if_local_table(const char *dbname, const char *tabname);
280 bool
281 ndbcluster_check_if_local_tables_in_db(THD *thd, const char *dbname);
282 
283 bool ndbcluster_anyvalue_is_reserved(Uint32 anyValue);
284 bool ndbcluster_anyvalue_is_nologging(Uint32 anyValue);
285 void ndbcluster_anyvalue_set_nologging(Uint32& anyValue);
286 bool ndbcluster_anyvalue_is_serverid_in_range(Uint32 serverId);
287 void ndbcluster_anyvalue_set_normal(Uint32& anyValue);
288 Uint32 ndbcluster_anyvalue_get_serverid(Uint32 anyValue);
289 void ndbcluster_anyvalue_set_serverid(Uint32& anyValue, Uint32 serverId);
290 
291 #ifndef DBUG_OFF
292 void dbug_ndbcluster_anyvalue_set_userbits(Uint32& anyValue);
293 #endif
294