1 #ifndef HA_NDBCLUSTER_BINLOG_INCLUDED
2 #define HA_NDBCLUSTER_BINLOG_INCLUDED
3 
4 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
5 
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; version 2 of the License.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
18 */
19 
20 #include "sql_class.h"                      /* THD */
21 
22 // Typedefs for long names
23 typedef NdbDictionary::Object NDBOBJ;
24 typedef NdbDictionary::Column NDBCOL;
25 typedef NdbDictionary::Table NDBTAB;
26 typedef NdbDictionary::Index  NDBINDEX;
27 typedef NdbDictionary::Dictionary  NDBDICT;
28 typedef NdbDictionary::Event  NDBEVENT;
29 
30 #define IS_TMP_PREFIX(A) (is_prefix(A, tmp_file_prefix))
31 
32 #define INJECTOR_EVENT_LEN 200
33 
34 #define NDB_INVALID_SCHEMA_OBJECT 241
35 
36 /* server id's with high bit set is reservered */
37 #define NDB_ANYVALUE_FOR_NOLOGGING 0xFFFFFFFF
38 #define NDB_ANYVALUE_RESERVED      0x80000000
39 
40 extern handlerton *ndbcluster_hton;
41 
42 /*
43   The numbers below must not change as they
44   are passed between mysql servers, and if changed
45   would break compatablility.  Add new numbers to
46   the end.
47 */
48 enum SCHEMA_OP_TYPE
49 {
50   SOT_DROP_TABLE= 0,
51   SOT_CREATE_TABLE= 1,
52   SOT_RENAME_TABLE_NEW= 2,
53   SOT_ALTER_TABLE= 3,
54   SOT_DROP_DB= 4,
55   SOT_CREATE_DB= 5,
56   SOT_ALTER_DB= 6,
57   SOT_CLEAR_SLOCK= 7,
58   SOT_TABLESPACE= 8,
59   SOT_LOGFILE_GROUP= 9,
60   SOT_RENAME_TABLE= 10,
61   SOT_TRUNCATE_TABLE= 11
62 };
63 
64 const uint max_ndb_nodes= 64; /* multiple of 32 */
65 
66 static const char *ha_ndb_ext=".ndb";
67 static const char share_prefix[]= "./";
68 
69 class Ndb_table_guard
70 {
71 public:
Ndb_table_guard(NDBDICT * dict,const char * tabname)72   Ndb_table_guard(NDBDICT *dict, const char *tabname)
73     : m_dict(dict)
74   {
75     DBUG_ENTER("Ndb_table_guard");
76     m_ndbtab= m_dict->getTableGlobal(tabname);
77     m_invalidate= 0;
78     DBUG_PRINT("info", ("m_ndbtab: %p", m_ndbtab));
79     DBUG_VOID_RETURN;
80   }
~Ndb_table_guard()81   ~Ndb_table_guard()
82   {
83     DBUG_ENTER("~Ndb_table_guard");
84     if (m_ndbtab)
85     {
86       DBUG_PRINT("info", ("m_ndbtab: %p  m_invalidate: %d",
87                           m_ndbtab, m_invalidate));
88       m_dict->removeTableGlobal(*m_ndbtab, m_invalidate);
89     }
90     DBUG_VOID_RETURN;
91   }
get_table()92   const NDBTAB *get_table() { return m_ndbtab; }
invalidate()93   void invalidate() { m_invalidate= 1; }
release()94   const NDBTAB *release()
95   {
96     DBUG_ENTER("Ndb_table_guard::release");
97     const NDBTAB *tmp= m_ndbtab;
98     DBUG_PRINT("info", ("m_ndbtab: %p", m_ndbtab));
99     m_ndbtab = 0;
100     DBUG_RETURN(tmp);
101   }
102 private:
103   const NDBTAB *m_ndbtab;
104   NDBDICT *m_dict;
105   int m_invalidate;
106 };
107 
108 #ifdef HAVE_NDB_BINLOG
109 
110 #ifdef HAVE_PSI_INTERFACE
111 extern PSI_mutex_key key_injector_mutex, key_ndb_schema_share_mutex,
112                      key_ndb_schema_object_mutex;
113 extern PSI_cond_key key_injector_cond;
114 extern PSI_thread_key key_thread_ndb_binlog;
115 #endif /* HAVE_PSI_INTERFACE */
116 
117 extern pthread_t ndb_binlog_thread;
118 extern mysql_mutex_t injector_mutex;
119 extern mysql_cond_t  injector_cond;
120 
121 extern unsigned char g_node_id_map[max_ndb_nodes];
122 extern pthread_t ndb_util_thread;
123 extern mysql_mutex_t LOCK_ndb_util_thread;
124 extern mysql_cond_t COND_ndb_util_thread;
125 extern int ndbcluster_util_inited;
126 extern mysql_mutex_t ndbcluster_mutex;
127 extern HASH ndbcluster_open_tables;
128 extern Ndb_cluster_connection* g_ndb_cluster_connection;
129 extern long ndb_number_of_storage_nodes;
130 
131 /*
132   Initialize the binlog part of the ndb handlerton
133 */
134 void ndbcluster_binlog_init_handlerton();
135 /*
136   Initialize the binlog part of the NDB_SHARE
137 */
138 int ndbcluster_binlog_init_share(NDB_SHARE *share, TABLE *table);
139 
140 bool ndbcluster_check_if_local_table(const char *dbname, const char *tabname);
141 bool ndbcluster_check_if_local_tables_in_db(THD *thd, const char *dbname);
142 
143 int ndbcluster_create_binlog_setup(Ndb *ndb, const char *key,
144                                    uint key_len,
145                                    const char *db,
146                                    const char *table_name,
147                                    my_bool share_may_exist);
148 int ndbcluster_create_event(Ndb *ndb, const NDBTAB *table,
149                             const char *event_name, NDB_SHARE *share,
150                             int push_warning= 0);
151 int ndbcluster_create_event_ops(NDB_SHARE *share,
152                                 const NDBTAB *ndbtab,
153                                 const char *event_name);
154 int ndbcluster_log_schema_op(THD *thd, NDB_SHARE *share,
155                              const char *query, int query_length,
156                              const char *db, const char *table_name,
157                              uint32 ndb_table_id,
158                              uint32 ndb_table_version,
159                              enum SCHEMA_OP_TYPE type,
160                              const char *new_db,
161                              const char *new_table_name);
162 int ndbcluster_handle_drop_table(Ndb *ndb, const char *event_name,
163                                  NDB_SHARE *share,
164                                  const char *type_str);
165 void ndb_rep_event_name(String *event_name,
166                         const char *db, const char *tbl);
167 int ndb_create_table_from_engine(THD *thd, const char *db,
168                                  const char *table_name);
169 int ndbcluster_binlog_start();
170 pthread_handler_t ndb_binlog_thread_func(void *arg);
171 
172 /*
173   table mysql.ndb_apply_status
174 */
175 int ndbcluster_setup_binlog_table_shares(THD *thd);
176 extern NDB_SHARE *ndb_apply_status_share;
177 extern NDB_SHARE *ndb_schema_share;
178 
179 extern THD *injector_thd;
180 extern my_bool ndb_binlog_running;
181 extern my_bool ndb_binlog_tables_inited;
182 
183 bool
184 ndbcluster_show_status_binlog(THD* thd, stat_print_fn *stat_print,
185                               enum ha_stat_type stat_type);
186 
187 /*
188   prototypes for ndb handler utility function also needed by
189   the ndb binlog code
190 */
191 int cmp_frm(const NDBTAB *ndbtab, const void *pack_data,
192             uint pack_length);
193 int ndbcluster_find_all_files(THD *thd);
194 #endif /* HAVE_NDB_BINLOG */
195 
196 void ndb_unpack_record(TABLE *table, NdbValue *value,
197                        MY_BITMAP *defined, uchar *buf);
198 char *ndb_pack_varchar(const NDBCOL *col, char *buf,
199                        const char *str, int sz);
200 
201 NDB_SHARE *ndbcluster_get_share(const char *key,
202                                 TABLE *table,
203                                 bool create_if_not_exists,
204                                 bool have_lock);
205 NDB_SHARE *ndbcluster_get_share(NDB_SHARE *share);
206 void ndbcluster_free_share(NDB_SHARE **share, bool have_lock);
207 void ndbcluster_real_free_share(NDB_SHARE **share);
208 int handle_trailing_share(NDB_SHARE *share);
209 inline NDB_SHARE *get_share(const char *key,
210                             TABLE *table,
211                             bool create_if_not_exists= TRUE,
212                             bool have_lock= FALSE)
213 {
214   return ndbcluster_get_share(key, table, create_if_not_exists, have_lock);
215 }
216 
get_share(NDB_SHARE * share)217 inline NDB_SHARE *get_share(NDB_SHARE *share)
218 {
219   return ndbcluster_get_share(share);
220 }
221 
222 inline void free_share(NDB_SHARE **share, bool have_lock= FALSE)
223 {
224   ndbcluster_free_share(share, have_lock);
225 }
226 
227 inline
228 Thd_ndb *
get_thd_ndb(THD * thd)229 get_thd_ndb(THD *thd)
230 { return (Thd_ndb *) thd_get_ha_data(thd, ndbcluster_hton); }
231 
232 inline
233 void
set_thd_ndb(THD * thd,Thd_ndb * thd_ndb)234 set_thd_ndb(THD *thd, Thd_ndb *thd_ndb)
235 { thd_set_ha_data(thd, ndbcluster_hton, thd_ndb); }
236 
237 Ndb* check_ndb_in_thd(THD* thd);
238 
239 #endif /* HA_NDBCLUSTER_BINLOG_INCLUDED */
240