1 /*
2    Copyright (c) 2004, 2020, 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; // used also for time precision
45 Uint32 n_new;
46 char new_row[1];
47 };
48 
49 enum AttrConvType { ACT_UNSUPPORTED = 0, ACT_PRESERVING = 1, ACT_LOSSY =-1,
50                     ACT_STAGING_PRESERVING = 2, ACT_STAGING_LOSSY = -2 };
51 typedef  AttrConvType (*AttrCheckCompatFunc)(const NDBCOL &old_col,
52                                              const NDBCOL &new_col);
53 
54 struct PromotionRules {
55   NDBCOL::Type old_type;
56   NDBCOL::Type new_type;
57   AttrCheckCompatFunc  attr_check_compatability;
58   AttrConvertFunc attr_convert;
59 };
60 
61 class BackupRestore : public BackupConsumer
62 {
63 public:
BackupRestore(Ndb_cluster_connection * conn,NODE_GROUP_MAP * ng_map,uint ng_map_len,const char * instance_name,Uint32 parallelism)64   BackupRestore(Ndb_cluster_connection *conn,
65                 NODE_GROUP_MAP *ng_map,
66                 uint ng_map_len,
67                 const char *instance_name,
68                 Uint32 parallelism) :
69     m_ndb(NULL),
70     m_cluster_connection(conn)
71   {
72     m_nodegroup_map = ng_map;
73     m_nodegroup_map_len = ng_map_len;
74     m_n_tablespace = 0;
75     m_n_logfilegroup = 0;
76     m_n_datafile = 0;
77     m_n_undofile = 0;
78     m_n_tables = 0;
79     m_logBytes = m_dataBytes = 0;
80     m_logCount = m_dataCount = 0;
81     m_metadata_work_requested = 0;
82     m_restore = false;
83     m_restore_meta = false;
84     m_no_restore_disk = false;
85     m_restore_epoch_requested = false;
86     m_restore_epoch = false;
87     m_parallelism = parallelism;
88     m_callback = 0;
89     m_free_callback = 0;
90     m_temp_error = false;
91     m_no_upgrade = false;
92     m_tableChangesMask = 0;
93     m_preserve_trailing_spaces = false;
94     m_transactions = 0;
95     m_pk_update_warning_count = 0;
96     m_cache.m_old_table = 0;
97     m_disable_indexes = false;
98     m_rebuild_indexes = false;
99     snprintf(m_instance_name, BackupRestore::INSTANCE_ID_LEN, "%s", instance_name);
100   }
101 
102   virtual ~BackupRestore();
103   virtual bool init(Uint32 tableChangesMask);
104   virtual void release();
105   virtual bool object(Uint32 type, const void* ptr);
106   virtual bool table(const TableS &);
107   virtual bool fk(Uint32 type, const void* ptr);
108   virtual bool endOfTables();
109   virtual bool endOfTablesFK();
110   virtual void tuple(const TupleS &, Uint32 fragId);
111   virtual void tuple_free();
112   virtual void tuple_a(restore_callback_t *cb);
113   virtual void tuple_SYSTAB_0(restore_callback_t *cb, const TableS &);
114   virtual void cback(int result, restore_callback_t *cb);
115   virtual bool errorHandler(restore_callback_t *cb);
116   virtual void exitHandler();
117   virtual void endOfTuples();
118   virtual void logEntry(const LogEntry &);
119   virtual void endOfLogEntrys();
120   virtual bool prepare_staging(const TableS &);
121   virtual bool finalize_staging(const TableS &);
122   virtual bool finalize_table(const TableS &);
123   virtual bool rebuild_indexes(const TableS&);
124   virtual bool has_temp_error();
125   virtual bool createSystable(const TableS & table);
126   virtual bool table_compatible_check(TableS & tableS);
127   virtual bool check_blobs(TableS & tableS);
128   virtual bool column_compatible_check(const char* tableName,
129                                        const NDBCOL* backupCol,
130                                        const NDBCOL* dbCol);
131   virtual bool update_apply_status(const RestoreMetaData &metaData, bool snapshotstart);
132   virtual bool report_started(unsigned node_id, unsigned backup_id);
133   virtual bool report_meta_data(unsigned node_id, unsigned backup_id);
134   virtual bool report_data(unsigned node_id, unsigned backup_id);
135   virtual bool report_log(unsigned node_id, unsigned backup_id);
136   virtual bool report_completed(unsigned node_id, unsigned backup_id);
137   bool map_in_frm(char *new_data, const char *data,
138                   uint data_len, uint *new_data_len) const;
139   bool search_replace(char *search_str, char **new_data,
140                       const char **data, const char *end_data,
141                       uint *new_data_len) const;
142   bool map_nodegroups(Uint32 *ng_array, Uint32 no_parts) const;
143   Uint32 map_ng(Uint32 ng) const;
144   bool translate_frm(NdbDictionary::Table *table) const;
145   bool isMissingTable(const TableS& table);
146   bool getPkMappingIndex(TableS* table);
147   bool tryCreatePkMappingIndex(TableS* table,
148                                const char* table_name);
149   bool dropPkMappingIndex(const TableS* table);
150 
151   static AttrConvType check_compat_sizes(const NDBCOL &old_col,
152                                          const NDBCOL &new_col);
153   static AttrConvType check_compat_precision(const NDBCOL &old_col,
154                                              const NDBCOL &new_col);
155   static AttrConvType check_compat_char_binary(const NDBCOL &old_col,
156                                                const NDBCOL &new_col);
157   static AttrConvType check_compat_char_to_text(const NDBCOL &old_col,
158                                                 const NDBCOL &new_col);
159   static AttrConvType check_compat_text_to_char(const NDBCOL &old_col,
160                                                 const NDBCOL &new_col);
161   static AttrConvType check_compat_text_to_text(const NDBCOL &old_col,
162                                                 const NDBCOL &new_col);
163   static AttrConvType check_compat_binary_to_blob(const NDBCOL &old_col,
164                                                   const NDBCOL &new_col);
165   static AttrConvType check_compat_blob_to_binary(const NDBCOL &old_col,
166                                                   const NDBCOL &new_col);
167   static AttrConvType check_compat_blob_to_blob(const NDBCOL &old_col,
168                                                 const NDBCOL &new_col);
169   static AttrConvType check_compat_promotion(const NDBCOL &old_col,
170                                              const NDBCOL &new_col);
171   static AttrConvType check_compat_lossy(const NDBCOL &old_col,
172                                          const NDBCOL &new_col);
173 
174   // bitset conversion handler
175   static void*
176   convert_bitset(const void * source, void * target, bool &truncated);
177 
178   // char/binary array conversion handler
179   template< typename S, typename T >
180   static void *
181   convert_array(const void * source, void * target, bool & truncated);
182 
183   // integral type conversion handler
184   // (follows MySQL replication semantics truncating to nearest legal value)
185   template< typename T, typename S >
186   static void *
187   convert_integral(const void * source, void * target, bool & truncated);
188 
189   // times with fractional seconds, see wl#946
190   static void*
191   convert_time_time2(const void * source, void * target, bool &);
192   static void*
193   convert_time2_time(const void * source, void * target, bool &);
194   static void*
195   convert_time2_time2(const void * source, void * target, bool &);
196   static void*
197   convert_datetime_datetime2(const void * source, void * target, bool &);
198   static void*
199   convert_datetime2_datetime(const void * source, void * target, bool &);
200   static void*
201   convert_datetime2_datetime2(const void * source, void * target, bool &);
202   static void*
203   convert_timestamp_timestamp2(const void * source, void * target, bool &);
204   static void*
205   convert_timestamp2_timestamp(const void * source, void * target, bool &);
206   static void*
207   convert_timestamp2_timestamp2(const void * source, void * target, bool &);
208 
209   // returns the handler function checking type conversion compatibility
210   AttrCheckCompatFunc
211   get_attr_check_compatability(const NDBCOL::Type &old_type,
212                                const NDBCOL::Type &new_type);
213 
214   // returns the handler function converting a value
215   AttrConvertFunc
216   get_convert_func(const NDBCOL::Type &old_type,
217                    const NDBCOL::Type &new_type);
218 
219   void update_next_auto_val(Uint32 orig_table_id,
220                             Uint64 next_val);
221 
222 
223   Ndb * m_ndb;
224   Ndb_cluster_connection * m_cluster_connection;
225 
226   bool m_restore;
227 
228   // flags set on all threads to indicate that metadata/epoch restore
229   // was requested - will ensure all threads init data needed by later
230   // stages of ndb_restore
231   bool m_metadata_work_requested;
232   bool m_restore_epoch_requested;
233 
234   // flags set only on thread 1 to indicate that thread 1 must
235   // do restore work, like restoring epoch/rebuilding indices
236   bool m_restore_meta;
237   bool m_restore_epoch;
238   bool m_disable_indexes;
239   bool m_rebuild_indexes;
240 
241   bool m_no_upgrade; // for upgrade ArrayType from 5.0 backup file.
242   bool m_no_restore_disk;
243   Uint32 m_tableChangesMask;
244   static bool m_preserve_trailing_spaces;
245 
246   Uint32 m_n_tablespace;
247   Uint32 m_n_logfilegroup;
248   Uint32 m_n_datafile;
249   Uint32 m_n_undofile;
250   Uint32 m_n_tables;
251 
252   Uint64 m_logBytes;
253   Uint64 m_dataBytes;
254 
255   Uint32 m_logCount;
256   Uint32 m_dataCount;
257 
258   static const Uint32 INSTANCE_ID_LEN = 20;
259   char m_instance_name[INSTANCE_ID_LEN];
260 
261   Uint32 m_parallelism;
262   volatile Uint32 m_transactions;
263 
264   restore_callback_t *m_callback;
265   restore_callback_t *m_free_callback;
266   bool m_temp_error;
267   Uint64 m_pk_update_warning_count;
268 
269   /**
270    * m_new_table_ids[X] = Y;
271    *   X - old table id
272    *   Y != 0  - new table
273    */
274   Vector<const NdbDictionary::Table*> m_new_tables;
275   struct {
276     const NdbDictionary::Table* m_old_table;
277     const NdbDictionary::Table* m_new_table;
278   } m_cache;
279   const NdbDictionary::Table* get_table(const TableS &);
280 
281   Vector<Uint64> m_auto_values;
282   Vector<const NdbDictionary::Table*> m_indexes;
283   Vector<Vector<NdbDictionary::Index *> > m_index_per_table; //
284   Vector<NdbDictionary::Tablespace*> m_tablespaces;    // Index by id
285   Vector<NdbDictionary::LogfileGroup*> m_logfilegroups;// Index by id
286   Vector<NdbDictionary::HashMap*> m_hashmaps;
287   Vector<const NdbDictionary::ForeignKey*> m_fks;
288 
289   static const PromotionRules m_allowed_promotion_attrs[];
290 };
291 
292 #endif
293