1 /*
2    Copyright (c) 2004, 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 #ifndef CONSUMER_RESTORE_HPP
26 #define CONSUMER_RESTORE_HPP
27 
28 #include "consumer.hpp"
29 
30 bool map_nodegroups(Uint32 *ng_array, Uint32 no_parts);
31 
32 struct restore_callback_t {
33   class BackupRestore *restore;
34   class TupleS tup;
35   class NdbTransaction *connection;
36   int    retries;
37   int error_code;
38   Uint32 fragId;
39   Uint32 n_bytes;
40   restore_callback_t *next;
41 };
42 
43 struct char_n_padding_struct {
44 Uint32 n_old;
45 Uint32 n_new;
46 char new_row[1];
47 };
48 
49 enum AttrConvType { ACT_UNSUPPORTED = 0, ACT_PRESERVING = 1, ACT_LOSSY =-1 };
50 typedef  AttrConvType (*AttrCheckCompatFunc)(const NDBCOL &old_col,
51                                              const NDBCOL &new_col);
52 
53 struct PromotionRules {
54   NDBCOL::Type old_type;
55   NDBCOL::Type new_type;
56   AttrCheckCompatFunc  attr_check_compatability;
57   AttrConvertFunc attr_convert;
58 };
59 
60 class BackupRestore : public BackupConsumer
61 {
62 public:
BackupRestore(const char * ndb_connectstring,int ndb_nodeid,NODE_GROUP_MAP * ng_map,uint ng_map_len,Uint32 parallelism=1)63   BackupRestore(const char* ndb_connectstring,
64                 int ndb_nodeid,
65                 NODE_GROUP_MAP *ng_map,
66                 uint ng_map_len,
67                 Uint32 parallelism=1) :
68     m_ndb(NULL),
69     m_cluster_connection(NULL),
70     m_ndb_connectstring(ndb_connectstring),
71     m_ndb_nodeid(ndb_nodeid)
72   {
73     m_nodegroup_map = ng_map;
74     m_nodegroup_map_len = ng_map_len;
75     m_n_tablespace = 0;
76     m_n_logfilegroup = 0;
77     m_n_datafile = 0;
78     m_n_undofile = 0;
79     m_n_tables = 0;
80     m_logBytes = m_dataBytes = 0;
81     m_logCount = m_dataCount = 0;
82     m_restore = false;
83     m_restore_meta = false;
84     m_no_restore_disk = false;
85     m_restore_epoch = false;
86     m_parallelism = parallelism;
87     m_callback = 0;
88     m_free_callback = 0;
89     m_temp_error = false;
90     m_no_upgrade = false;
91     m_tableChangesMask = 0;
92     m_preserve_trailing_spaces = false;
93     m_transactions = 0;
94     m_cache.m_old_table = 0;
95     m_disable_indexes = false;
96     m_rebuild_indexes = false;
97   }
98 
99   virtual ~BackupRestore();
100   virtual bool init(Uint32 tableChangesMask);
101   virtual void release();
102   virtual bool object(Uint32 type, const void* ptr);
103   virtual bool table(const TableS &);
104   virtual bool endOfTables();
105   virtual void tuple(const TupleS &, Uint32 fragId);
106   virtual void tuple_free();
107   virtual void tuple_a(restore_callback_t *cb);
108   virtual void tuple_SYSTAB_0(restore_callback_t *cb, const TableS &);
109   virtual int restoreAutoIncrement(restore_callback_t *cb,
110                                     Uint32 tableId, Uint64 value);
111   virtual void cback(int result, restore_callback_t *cb);
112   virtual bool errorHandler(restore_callback_t *cb);
113   virtual void exitHandler();
114   virtual void endOfTuples();
115   virtual void logEntry(const LogEntry &);
116   virtual void endOfLogEntrys();
117   virtual bool finalize_table(const TableS &);
118   virtual bool rebuild_indexes(const TableS&);
119   virtual bool has_temp_error();
120   virtual bool createSystable(const TableS & table);
121   virtual bool table_compatible_check(const TableS & tableS);
122   virtual bool column_compatible_check(const char* tableName,
123                                        const NDBCOL* backupCol,
124                                        const NDBCOL* dbCol);
125   virtual bool update_apply_status(const RestoreMetaData &metaData);
126   virtual bool report_started(unsigned node_id, unsigned backup_id);
127   virtual bool report_meta_data(unsigned node_id, unsigned backup_id);
128   virtual bool report_data(unsigned node_id, unsigned backup_id);
129   virtual bool report_log(unsigned node_id, unsigned backup_id);
130   virtual bool report_completed(unsigned node_id, unsigned backup_id);
131   void connectToMysql();
132   bool map_in_frm(char *new_data, const char *data,
133                   uint data_len, uint *new_data_len);
134   bool search_replace(char *search_str, char **new_data,
135                       const char **data, const char *end_data,
136                       uint *new_data_len);
137   bool map_nodegroups(Uint32 *ng_array, Uint32 no_parts);
138   Uint32 map_ng(Uint32 ng);
139   bool translate_frm(NdbDictionary::Table *table);
140 
141   static AttrConvType check_compat_sizes(const NDBCOL &old_col,
142                                          const NDBCOL &new_col);
143   static AttrConvType check_compat_promotion(const NDBCOL &old_col,
144                                              const NDBCOL &new_col);
145   static AttrConvType check_compat_lossy(const NDBCOL &old_col,
146                                          const NDBCOL &new_col);
147 
148   // bitset conversion handler
149   static void*
150   convert_bitset(const void * source, void * target, bool &truncated);
151 
152   // char/binary array conversion handler
153   template< typename S, typename T >
154   static void *
155   convert_array(const void * source, void * target, bool & truncated);
156 
157   // integral type conversion handler
158   // (follows MySQL replication semantics truncating to nearest legal value)
159   template< typename T, typename S >
160   static void *
161   convert_integral(const void * source, void * target, bool & truncated);
162 
163   // returns the handler function checking type conversion compatibility
164   AttrCheckCompatFunc
165   get_attr_check_compatability(const NDBCOL::Type &old_type,
166                                const NDBCOL::Type &new_type);
167 
168   // returns the handler function converting a value
169   AttrConvertFunc
170   get_convert_func(const NDBCOL::Type &old_type,
171                    const NDBCOL::Type &new_type);
172 
173   Ndb * m_ndb;
174   Ndb_cluster_connection * m_cluster_connection;
175   const char* m_ndb_connectstring;
176   int m_ndb_nodeid;
177   bool m_restore;
178   bool m_restore_meta;
179   bool m_no_restore_disk;
180   bool m_restore_epoch;
181   bool m_no_upgrade; // for upgrade ArrayType from 5.0 backup file.
182   bool m_disable_indexes;
183   bool m_rebuild_indexes;
184   Uint32 m_tableChangesMask;
185   static bool m_preserve_trailing_spaces;
186 
187   Uint32 m_n_tablespace;
188   Uint32 m_n_logfilegroup;
189   Uint32 m_n_datafile;
190   Uint32 m_n_undofile;
191   Uint32 m_n_tables;
192 
193   Uint64 m_logBytes;
194   Uint64 m_dataBytes;
195 
196   Uint32 m_logCount;
197   Uint32 m_dataCount;
198 
199   Uint32 m_parallelism;
200   volatile Uint32 m_transactions;
201 
202   restore_callback_t *m_callback;
203   restore_callback_t *m_free_callback;
204   bool m_temp_error;
205 
206   /**
207    * m_new_table_ids[X] = Y;
208    *   X - old table id
209    *   Y != 0  - new table
210    */
211   Vector<const NdbDictionary::Table*> m_new_tables;
212   struct {
213     const NdbDictionary::Table* m_old_table;
214     const NdbDictionary::Table* m_new_table;
215   } m_cache;
216   const NdbDictionary::Table* get_table(const NdbDictionary::Table* );
217 
218   Vector<const NdbDictionary::Table*> m_indexes;
219   Vector<Vector<NdbDictionary::Index *> > m_index_per_table; //
220   Vector<NdbDictionary::Tablespace*> m_tablespaces;    // Index by id
221   Vector<NdbDictionary::LogfileGroup*> m_logfilegroups;// Index by id
222   Vector<NdbDictionary::HashMap*> m_hashmaps;
223 
224   static const PromotionRules m_allowed_promotion_attrs[];
225 };
226 
227 #endif
228