~jlukas79/+junk/mysql-server

« back to all changes in this revision

Viewing changes to sql/backup/backup_kernel.h

manual merge 6.0-main --> 6.0-bka-review

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#ifndef _BACKUP_KERNEL_API_H
2
2
#define _BACKUP_KERNEL_API_H
3
3
 
4
 
#include <backup/api_types.h>
5
 
#include <backup/catalog.h>
6
4
#include <backup/logger.h>
 
5
#include <backup/stream_services.h>
7
6
 
8
7
/**
9
8
  @file
19
18
#define DATA_BUFFER_SIZE  (1024*1024)
20
19
 
21
20
/*
 
21
  Functions used to initialize and shut down the online backup system.
 
22
  
 
23
  Note: these functions are called at plugin load and plugin shutdown time,
 
24
  respectively.
 
25
 */ 
 
26
int backup_init();
 
27
void backup_shutdown();
 
28
 
 
29
/*
22
30
  Called from the big switch in mysql_execute_command() to execute
23
31
  backup related statement
24
32
*/
25
33
int execute_backup_command(THD*, LEX*);
26
34
 
27
 
namespace backup {
 
35
// forward declarations
28
36
 
29
 
class Image_info;
30
37
class Backup_info;
31
38
class Restore_info;
32
39
 
33
 
} // backup namespace
34
 
 
35
 
// Backup kernel API
36
 
 
37
 
int mysql_backup(THD*, backup::Backup_info&, backup::OStream&);
38
 
int mysql_restore(THD*, backup::Restore_info&, backup::IStream&);
39
 
 
40
 
 
41
40
namespace backup {
42
41
 
 
42
class Mem_allocator;
 
43
class Stream;
 
44
class Output_stream;
 
45
class Input_stream;
 
46
class Native_snapshot;
 
47
 
 
48
int write_table_data(THD*, Backup_info&, Output_stream&);
 
49
int restore_table_data(THD*, Restore_info&, Input_stream&);
 
50
 
 
51
}
 
52
 
43
53
/**
44
 
  Represents a location where backup archive can be stored.
45
 
 
46
 
  The class is supposed to represent the location on the abstract level
47
 
  so that it is easier to add new types of locations.
48
 
 
49
 
  Currently we support only files on the server's file system. Thus the
50
 
  only type of location is a path to a file.
51
 
 */
52
 
 
53
 
struct Location
 
54
  Instances of this class are used for creating required context and performing
 
55
  backup/restore operations.
 
56
  
 
57
  @see kernel.cc
 
58
 */ 
 
59
class Backup_restore_ctx: public backup::Logger 
54
60
{
55
 
  /// Type of location
56
 
  enum enum_type {SERVER_FILE, INVALID};
57
 
  bool read;
58
 
 
59
 
  virtual enum_type type() const
60
 
  { return INVALID; }
61
 
 
62
 
  virtual ~Location() {}  // we want to inherit this class
63
 
 
64
 
  /// Deallocate any resources used by Location object.
65
 
  virtual void free()
66
 
  { delete this; }  // use destructor to free resources
 
61
 public:
 
62
 
 
63
  Backup_restore_ctx(THD*);
 
64
  ~Backup_restore_ctx();
 
65
 
 
66
  bool is_valid() const;
 
67
  ulonglong op_id() const;
 
68
 
 
69
  Backup_info*  prepare_for_backup(LEX_STRING location, const char*, bool);
 
70
  Restore_info* prepare_for_restore(LEX_STRING location, const char*);  
 
71
 
 
72
  int do_backup();
 
73
  int do_restore();
 
74
  int fatal_error(int, ...);
 
75
 
 
76
  int close();
 
77
 
 
78
  THD* thd() const { return m_thd; }
 
79
 
 
80
 private:
 
81
 
 
82
  /** Indicates if a backup/restore operation is in progress. */
 
83
  static bool is_running;
 
84
  static pthread_mutex_t  run_lock; ///< To guard @c is_running flag.
67
85
 
68
86
  /** 
69
 
    Determine if the location is valid
70
 
  
71
 
    An invalid location will not be used.
 
87
    @brief State of a context object. 
72
88
    
73
 
    @retval TRUE  Location is valid.
74
 
    @retval FALSE Location is invalid.
75
 
  */
76
 
  virtual bool is_valid() =0;
77
 
  
78
 
  /// Describe location for debug purposes
79
 
  virtual const char* describe()
80
 
  { return "Invalid location"; }
 
89
    Backup/restore can be performed only if object is prepared for that operation.
 
90
   */
 
91
  enum { CREATED,
 
92
         PREPARED_FOR_BACKUP,
 
93
         PREPARED_FOR_RESTORE,
 
94
         CLOSED } m_state;
81
95
 
 
96
  ulonglong m_thd_options;  ///< For saving thd->options.
82
97
  /**
83
 
    Remove any system resources connected to that location.
84
 
 
85
 
    When location is opened for writing, some system resources will be usually
86
 
    created (depending on the type of the location). For example, a file in the
87
 
    file system. This method should remove/free any such resources. If no
88
 
    resources were created, the method should do nothing.
89
 
    
90
 
    @returns OK on success, ERROR otherwise.
 
98
    If backup/restore was interrupted by an error, this member stores the error 
 
99
    number.
91
100
   */ 
92
 
  virtual result_t remove() =0;
93
 
 
94
 
  /**
95
 
    Interpret string passed to BACKUP/RESTORE statement as backup location
96
 
    and construct corresponding Location object.
97
 
 
98
 
    @returns NULL if the string doesn't denote a valid location
99
 
   */
100
 
  static Location* find(const LEX_STRING&);
 
101
  int m_error;
 
102
  
 
103
  const char *m_path;   ///< Path to where the backup image file is located.
 
104
 
 
105
  /** If true, the backup image file is deleted at clean-up time. */
 
106
  bool m_remove_loc;
 
107
 
 
108
  backup::Stream *m_stream; ///< Pointer to the backup stream object, if opened.
 
109
  backup::Image_info *m_catalog;  ///< Pointer to the image catalogue object.
 
110
 
 
111
  /** Memory allocator for backup stream library. */
 
112
  static backup::Mem_allocator *mem_alloc;
 
113
 
 
114
  int prepare(LEX_STRING location);
 
115
  void disable_fkey_constraints();
 
116
  int  restore_triggers_and_events();
 
117
  
 
118
  /** 
 
119
    Indicates if tables have been locked with @c lock_tables_for_restore()
 
120
  */
 
121
  bool m_tables_locked; 
 
122
 
 
123
  int lock_tables_for_restore();
 
124
  int unlock_tables();
 
125
  
 
126
  friend class Backup_info;
 
127
  friend class Restore_info;
 
128
  friend int backup_init();
 
129
  friend void backup_shutdown();
 
130
  friend bstream_byte* bstream_alloc(unsigned long int);
 
131
  friend void bstream_free(bstream_byte *ptr);
101
132
};
102
133
 
 
134
/// Check if instance is correctly created.
 
135
inline
 
136
bool Backup_restore_ctx::is_valid() const
 
137
{
 
138
  return m_error == 0;
 
139
}
 
140
 
 
141
/// Return global id of the backup/restore operation.
 
142
inline
 
143
ulonglong Backup_restore_ctx::op_id() const
 
144
{
 
145
  return m_op_id; // inherited from Logger class
 
146
}
 
147
 
 
148
/// Disable foreign key constraint checks (needed during restore).
 
149
inline
 
150
void Backup_restore_ctx::disable_fkey_constraints()
 
151
{
 
152
  m_thd->options|= OPTION_NO_FOREIGN_KEY_CHECKS;
 
153
}
103
154
 
104
155
/**
105
 
  Specialization of @c Image_info which adds methods for selecting items
106
 
  to backup.
107
 
 
108
 
  When Backup_info object is created it is empty and ready for adding items
109
 
  to it. Methods @c add_table() @c add_db(), @c add_dbs() and @c add_all_dbs()
110
 
  can be used for that purpose (currently only databases and tables are
111
 
  supported). After populating info object with items it should be "closed"
112
 
  with a call to @c close() method. After that it is ready for use as a
113
 
  description of backup archive to be created.
 
156
  Report error and move context object into error state.
 
157
  
 
158
  After this method is called the context object is in error state and
 
159
  cannot be normally used. It still can be examined for saved error messages.
 
160
  The code of the error reported here is saved in m_error member.
 
161
  
 
162
  Only one fatal error can be reported. If context is already in error
 
163
  state when this method is called, it does nothing.
 
164
  
 
165
  @return error code given as input or stored in the context object if
 
166
  a fatal error was reported before.
 
167
 */ 
 
168
inline
 
169
int Backup_restore_ctx::fatal_error(int error_code, ...)
 
170
{
 
171
  if (m_error)
 
172
    return m_error;
 
173
 
 
174
  va_list args;
 
175
 
 
176
  m_error= error_code;
 
177
  m_remove_loc= TRUE;
 
178
 
 
179
  va_start(args,error_code);
 
180
  v_report_error(backup::log_level::ERROR, error_code, args);
 
181
  va_end(args);
 
182
 
 
183
  return error_code;
 
184
}
 
185
 
 
186
/*
 
187
  Now, when Backup_restore_ctx is defined, include definitions
 
188
  of Backup_info and Restore_info classes.
114
189
*/
115
 
class Backup_info: public Image_info, public Logger
116
 
{
117
 
 public:
118
 
 
119
 
  Backup_info(THD*);
120
 
  ~Backup_info();
121
 
 
122
 
  bool is_valid()
123
 
  {
124
 
    bool ok= TRUE;
125
 
 
126
 
    switch (m_state) {
127
 
 
128
 
    case ERROR:
129
 
      ok= FALSE;
130
 
      break;
131
 
 
132
 
    case INIT:
133
 
      ok= (i_s_tables != NULL);
134
 
      break;
135
 
 
136
 
    default:
137
 
      ok= TRUE;
138
 
    }
139
 
 
140
 
    if (!ok)
141
 
      m_state= ERROR;
142
 
 
143
 
    return ok;
144
 
  }
145
 
 
146
 
  int add_dbs(List< ::LEX_STRING >&);
147
 
  int add_all_dbs();
148
 
 
149
 
  bool close();
150
 
 
151
 
 private:
152
 
 
153
 
  /// State of the info structure.
154
 
  enum {INIT,   // structure ready for filling
155
 
        READY,  // structure ready for backup (tables opened)
156
 
        DONE,   // tables are closed
157
 
        ERROR
158
 
       }   m_state;
159
 
 
160
 
  int find_backup_engine(const ::TABLE *const, const Table_ref&);
161
 
 
162
 
  Table_item* add_table(Db_item&, const Table_ref&);
163
 
 
164
 
  int add_db_items(Db_item&);
165
 
  int add_table_items(Table_item&);
166
 
 
167
 
  THD    *m_thd;
168
 
  TABLE  *i_s_tables;
169
 
  String binlog_file_name; ///< stores name of the binlog at VP time
170
 
 
171
 
  /**
172
 
    @brief Storage for table and database names.
173
 
 
174
 
    When adding tables or databases to the backup catalogue, their names
175
 
    are stored in String objects, and these objects are appended to this
176
 
    list so that they can be freed when Backup_info object is destroyed.
177
 
  */
178
 
  // FIXME: use better solution, e.g., MEM_ROOT
179
 
  List<String>  name_strings;
180
 
 
181
 
  void save_binlog_pos(const ::LOG_INFO &li)
182
 
  {
183
 
    binlog_file_name= li.log_file_name;
184
 
    binlog_file_name.copy();
185
 
    binlog_pos.pos= (unsigned long int)li.pos;
186
 
    binlog_pos.file= binlog_file_name.c_ptr();
187
 
  }
188
 
 
189
 
  friend int write_table_data(THD*, Backup_info&, OStream&);
190
 
};
191
 
 
192
 
 
193
 
/**
194
 
  Specialization of @c Image_info which is used to select and restore items
195
 
  from a backup image.
196
 
 
197
 
  An instance of this class is created by reading backup image header and it
198
 
  describes its contents. @c Restore_info methods select which items
199
 
  should be restored.
200
 
 
201
 
  @note This class is not fully implemented. Right now it is not possible to
202
 
  select items to restore - always all items are restored.
203
 
 */
204
 
 
205
 
class Restore_info: public Image_info, public Logger
206
 
{
207
 
  bool m_valid;
208
 
  THD  *m_thd;
209
 
  const Db_ref *curr_db;
210
 
 
211
 
  CHARSET_INFO *system_charset;
212
 
  bool same_sys_charset;
213
 
 
214
 
 public:
215
 
 
216
 
  Restore_info(THD*, IStream&);
217
 
  ~Restore_info();
218
 
 
219
 
  bool is_valid() const
220
 
  { return m_valid; }
221
 
 
222
 
  int restore_all_dbs()
223
 
  { return 0; }
224
 
 
225
 
  /// Determine if given item is selected for restore.
226
 
  bool selected(const Image_info::Item&)
227
 
  { return TRUE; }
228
 
 
229
 
  result_t restore_item(Item &it, String &sdata, String&);
230
 
 
231
 
  friend int restore_table_data(THD*, Restore_info&, IStream&);
232
 
  friend int ::bcat_add_item(st_bstream_image_header*,
233
 
                             struct st_bstream_item_info*);
234
 
};
235
 
 
236
 
} // backup namespace
 
190
 
 
191
#include <backup/backup_info.h>
 
192
#include <backup/restore_info.h>
237
193
 
238
194
#endif