~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to sql/log.h

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef LOG_H
 
17
#define LOG_H
 
18
 
 
19
class Relay_log_info;
 
20
 
 
21
class Format_description_log_event;
 
22
 
 
23
/*
 
24
  Transaction Coordinator log - a base abstract class
 
25
  for two different implementations
 
26
*/
 
27
class TC_LOG
 
28
{
 
29
  public:
 
30
  int using_heuristic_recover();
 
31
  TC_LOG() {}
 
32
  virtual ~TC_LOG() {}
 
33
 
 
34
  virtual int open(const char *opt_name)=0;
 
35
  virtual void close()=0;
 
36
  virtual int log_xid(THD *thd, my_xid xid)=0;
 
37
  virtual void unlog(ulong cookie, my_xid xid)=0;
 
38
};
 
39
 
 
40
class TC_LOG_DUMMY: public TC_LOG // use it to disable the logging
 
41
{
 
42
public:
 
43
  TC_LOG_DUMMY() {}
 
44
  int open(const char *opt_name)        { return 0; }
 
45
  void close()                          { }
 
46
  int log_xid(THD *thd, my_xid xid)         { return 1; }
 
47
  void unlog(ulong cookie, my_xid xid)  { }
 
48
};
 
49
 
 
50
#ifdef HAVE_MMAP
 
51
class TC_LOG_MMAP: public TC_LOG
 
52
{
 
53
  public:                // only to keep Sun Forte on sol9x86 happy
 
54
  typedef enum {
 
55
    POOL,                 // page is in pool
 
56
    ERROR,                // last sync failed
 
57
    DIRTY                 // new xids added since last sync
 
58
  } PAGE_STATE;
 
59
 
 
60
  private:
 
61
  typedef struct st_page {
 
62
    struct st_page *next; // page a linked in a fifo queue
 
63
    my_xid *start, *end;  // usable area of a page
 
64
    my_xid *ptr;          // next xid will be written here
 
65
    int size, free;       // max and current number of free xid slots on the page
 
66
    int waiters;          // number of waiters on condition
 
67
    PAGE_STATE state;     // see above
 
68
    pthread_mutex_t lock; // to access page data or control structure
 
69
    pthread_cond_t  cond; // to wait for a sync
 
70
  } PAGE;
 
71
 
 
72
  char logname[FN_REFLEN];
 
73
  File fd;
 
74
  my_off_t file_length;
 
75
  uint npages, inited;
 
76
  uchar *data;
 
77
  struct st_page *pages, *syncing, *active, *pool, *pool_last;
 
78
  /*
 
79
    note that, e.g. LOCK_active is only used to protect
 
80
    'active' pointer, to protect the content of the active page
 
81
    one has to use active->lock.
 
82
    Same for LOCK_pool and LOCK_sync
 
83
  */
 
84
  pthread_mutex_t LOCK_active, LOCK_pool, LOCK_sync;
 
85
  pthread_cond_t COND_pool, COND_active;
 
86
 
 
87
  public:
 
88
  TC_LOG_MMAP(): inited(0) {}
 
89
  int open(const char *opt_name);
 
90
  void close();
 
91
  int log_xid(THD *thd, my_xid xid);
 
92
  void unlog(ulong cookie, my_xid xid);
 
93
  int recover();
 
94
 
 
95
  private:
 
96
  void get_active_from_pool();
 
97
  int sync();
 
98
  int overflow();
 
99
};
 
100
#else
 
101
#define TC_LOG_MMAP TC_LOG_DUMMY
 
102
#endif
 
103
 
 
104
extern TC_LOG *tc_log;
 
105
extern TC_LOG_MMAP tc_log_mmap;
 
106
extern TC_LOG_DUMMY tc_log_dummy;
 
107
 
 
108
/* log info errors */
 
109
#define LOG_INFO_EOF -1
 
110
#define LOG_INFO_IO  -2
 
111
#define LOG_INFO_INVALID -3
 
112
#define LOG_INFO_SEEK -4
 
113
#define LOG_INFO_MEM -6
 
114
#define LOG_INFO_FATAL -7
 
115
#define LOG_INFO_IN_USE -8
 
116
#define LOG_INFO_EMFILE -9
 
117
 
 
118
 
 
119
/* bitmap to SQL_LOG::close() */
 
120
#define LOG_CLOSE_INDEX         1
 
121
#define LOG_CLOSE_TO_BE_OPENED  2
 
122
#define LOG_CLOSE_STOP_EVENT    4
 
123
 
 
124
class Relay_log_info;
 
125
 
 
126
typedef struct st_log_info
 
127
{
 
128
  char log_file_name[FN_REFLEN];
 
129
  my_off_t index_file_offset, index_file_start_offset;
 
130
  my_off_t pos;
 
131
  bool fatal; // if the purge happens to give us a negative offset
 
132
  pthread_mutex_t lock;
 
133
  st_log_info()
 
134
    : index_file_offset(0), index_file_start_offset(0),
 
135
      pos(0), fatal(0)
 
136
    {
 
137
      log_file_name[0] = '\0';
 
138
      pthread_mutex_init(&lock, MY_MUTEX_INIT_FAST);
 
139
    }
 
140
  ~st_log_info() { pthread_mutex_destroy(&lock);}
 
141
} LOG_INFO;
 
142
 
 
143
/*
 
144
  Currently we have only 3 kinds of logging functions: old-fashioned
 
145
  logs, stdout and csv logging routines.
 
146
*/
 
147
#define MAX_LOG_HANDLERS_NUM 3
 
148
 
 
149
/* log event handler flags */
 
150
#define LOG_NONE       1
 
151
#define LOG_FILE       2
 
152
#define LOG_TABLE      4
 
153
 
 
154
class Log_event;
 
155
class Rows_log_event;
 
156
 
 
157
enum enum_log_type { LOG_UNKNOWN, LOG_NORMAL, LOG_BIN };
 
158
enum enum_log_state { LOG_OPENED, LOG_CLOSED, LOG_TO_BE_OPENED };
 
159
 
 
160
/*
 
161
  TODO use mmap instead of IO_CACHE for binlog
 
162
  (mmap+fsync is two times faster than write+fsync)
 
163
*/
 
164
 
 
165
class MYSQL_LOG
 
166
{
 
167
public:
 
168
  MYSQL_LOG();
 
169
  void init_pthread_objects();
 
170
  void cleanup();
 
171
  bool open(const char *log_name,
 
172
            enum_log_type log_type,
 
173
            const char *new_name,
 
174
            enum cache_type io_cache_type_arg);
 
175
  void init(enum_log_type log_type_arg,
 
176
            enum cache_type io_cache_type_arg);
 
177
  void close(uint exiting);
 
178
  inline bool is_open() { return log_state != LOG_CLOSED; }
 
179
  const char *generate_name(const char *log_name, const char *suffix,
 
180
                            bool strip_ext, char *buff);
 
181
  int generate_new_name(char *new_name, const char *log_name);
 
182
 protected:
 
183
  /* LOCK_log is inited by init_pthread_objects() */
 
184
  pthread_mutex_t LOCK_log;
 
185
  char *name;
 
186
  char log_file_name[FN_REFLEN];
 
187
  char time_buff[20], db[NAME_LEN + 1];
 
188
  bool write_error, inited;
 
189
  IO_CACHE log_file;
 
190
  enum_log_type log_type;
 
191
  volatile enum_log_state log_state;
 
192
  enum cache_type io_cache_type;
 
193
  friend class Log_event;
 
194
};
 
195
 
 
196
class MYSQL_QUERY_LOG: public MYSQL_LOG
 
197
{
 
198
public:
 
199
  MYSQL_QUERY_LOG() : last_time(0) {}
 
200
  void reopen_file();
 
201
  bool write(time_t event_time, const char *user_host,
 
202
             uint user_host_len, int thread_id,
 
203
             const char *command_type, uint command_type_len,
 
204
             const char *sql_text, uint sql_text_len);
 
205
  bool write(THD *thd, time_t current_time, time_t query_start_arg,
 
206
             const char *user_host, uint user_host_len,
 
207
             ulonglong query_utime, ulonglong lock_utime, bool is_command,
 
208
             const char *sql_text, uint sql_text_len);
 
209
  bool open_slow_log(const char *log_name)
 
210
  {
 
211
    char buf[FN_REFLEN];
 
212
    return open(generate_name(log_name, "-slow.log", 0, buf), LOG_NORMAL, 0,
 
213
                WRITE_CACHE);
 
214
  }
 
215
  bool open_query_log(const char *log_name)
 
216
  {
 
217
    char buf[FN_REFLEN];
 
218
    return open(generate_name(log_name, ".log", 0, buf), LOG_NORMAL, 0,
 
219
                WRITE_CACHE);
 
220
  }
 
221
 
 
222
private:
 
223
  time_t last_time;
 
224
};
 
225
 
 
226
class MYSQL_BIN_LOG: public TC_LOG, private MYSQL_LOG
 
227
{
 
228
 private:
 
229
  /* LOCK_log and LOCK_index are inited by init_pthread_objects() */
 
230
  pthread_mutex_t LOCK_index;
 
231
  pthread_mutex_t LOCK_prep_xids;
 
232
  pthread_cond_t  COND_prep_xids;
 
233
  pthread_cond_t update_cond;
 
234
  ulonglong bytes_written;
 
235
  IO_CACHE index_file;
 
236
  char index_file_name[FN_REFLEN];
 
237
  /*
 
238
     The max size before rotation (usable only if log_type == LOG_BIN: binary
 
239
     logs and relay logs).
 
240
     For a binlog, max_size should be max_binlog_size.
 
241
     For a relay log, it should be max_relay_log_size if this is non-zero,
 
242
     max_binlog_size otherwise.
 
243
     max_size is set in init(), and dynamically changed (when one does SET
 
244
     GLOBAL MAX_BINLOG_SIZE|MAX_RELAY_LOG_SIZE) by fix_max_binlog_size and
 
245
     fix_max_relay_log_size).
 
246
  */
 
247
  ulong max_size;
 
248
  long prepared_xids; /* for tc log - number of xids to remember */
 
249
  // current file sequence number for load data infile binary logging
 
250
  uint file_id;
 
251
  uint open_count;                              // For replication
 
252
  int readers_count;
 
253
  bool need_start_event;
 
254
  /*
 
255
    no_auto_events means we don't want any of these automatic events :
 
256
    Start/Rotate/Stop. That is, in 4.x when we rotate a relay log, we don't
 
257
    want a Rotate_log event to be written to the relay log. When we start a
 
258
    relay log etc. So in 4.x this is 1 for relay logs, 0 for binlogs.
 
259
    In 5.0 it's 0 for relay logs too!
 
260
  */
 
261
  bool no_auto_events;
 
262
 
 
263
  ulonglong m_table_map_version;
 
264
 
 
265
  int write_to_file(IO_CACHE *cache);
 
266
  /*
 
267
    This is used to start writing to a new log file. The difference from
 
268
    new_file() is locking. new_file_without_locking() does not acquire
 
269
    LOCK_log.
 
270
  */
 
271
  void new_file_without_locking();
 
272
  void new_file_impl(bool need_lock);
 
273
 
 
274
public:
 
275
  MYSQL_LOG::generate_name;
 
276
  MYSQL_LOG::is_open;
 
277
  /*
 
278
    These describe the log's format. This is used only for relay logs.
 
279
    _for_exec is used by the SQL thread, _for_queue by the I/O thread. It's
 
280
    necessary to have 2 distinct objects, because the I/O thread may be reading
 
281
    events in a different format from what the SQL thread is reading (consider
 
282
    the case of a master which has been upgraded from 5.0 to 5.1 without doing
 
283
    RESET MASTER, or from 4.x to 5.0).
 
284
  */
 
285
  Format_description_log_event *description_event_for_exec,
 
286
    *description_event_for_queue;
 
287
 
 
288
  MYSQL_BIN_LOG();
 
289
  /*
 
290
    note that there's no destructor ~MYSQL_BIN_LOG() !
 
291
    The reason is that we don't want it to be automatically called
 
292
    on exit() - but only during the correct shutdown process
 
293
  */
 
294
 
 
295
  int open(const char *opt_name);
 
296
  void close();
 
297
  int log_xid(THD *thd, my_xid xid);
 
298
  void unlog(ulong cookie, my_xid xid);
 
299
  int recover(IO_CACHE *log, Format_description_log_event *fdle);
 
300
#if !defined(MYSQL_CLIENT)
 
301
  bool is_table_mapped(TABLE *table) const
 
302
  {
 
303
    return table->s->table_map_version == table_map_version();
 
304
  }
 
305
 
 
306
  ulonglong table_map_version() const { return m_table_map_version; }
 
307
  void update_table_map_version() { ++m_table_map_version; }
 
308
 
 
309
  int flush_and_set_pending_rows_event(THD *thd, Rows_log_event* event);
 
310
 
 
311
#endif /* !defined(MYSQL_CLIENT) */
 
312
  void reset_bytes_written()
 
313
  {
 
314
    bytes_written = 0;
 
315
  }
 
316
  void harvest_bytes_written(ulonglong* counter)
 
317
  {
 
318
#ifndef DBUG_OFF
 
319
    char buf1[22],buf2[22];
 
320
#endif
 
321
    DBUG_ENTER("harvest_bytes_written");
 
322
    (*counter)+=bytes_written;
 
323
    DBUG_PRINT("info",("counter: %s  bytes_written: %s", llstr(*counter,buf1),
 
324
                       llstr(bytes_written,buf2)));
 
325
    bytes_written=0;
 
326
    DBUG_VOID_RETURN;
 
327
  }
 
328
  void set_max_size(ulong max_size_arg);
 
329
  void signal_update();
 
330
  void wait_for_update_relay_log(THD* thd);
 
331
  int  wait_for_update_bin_log(THD* thd, const struct timespec * timeout);
 
332
  void set_need_start_event() { need_start_event = 1; }
 
333
  void init(bool no_auto_events_arg, ulong max_size);
 
334
  void init_pthread_objects();
 
335
  void cleanup();
 
336
  bool open(const char *log_name,
 
337
            enum_log_type log_type,
 
338
            const char *new_name,
 
339
            enum cache_type io_cache_type_arg,
 
340
            bool no_auto_events_arg, ulong max_size,
 
341
            bool null_created);
 
342
  bool open_index_file(const char *index_file_name_arg,
 
343
                       const char *log_name);
 
344
  /* Use this to start writing a new log file */
 
345
  void new_file();
 
346
 
 
347
  bool write(Log_event* event_info); // binary log write
 
348
  bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event);
 
349
 
 
350
  int  write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync);
 
351
 
 
352
  void start_union_events(THD *thd, query_id_t query_id_param);
 
353
  void stop_union_events(THD *thd);
 
354
  bool is_query_in_union(THD *thd, query_id_t query_id_param);
 
355
 
 
356
  /*
 
357
    v stands for vector
 
358
    invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
 
359
  */
 
360
  bool appendv(const char* buf,uint len,...);
 
361
  bool append(Log_event* ev);
 
362
 
 
363
  void make_log_name(char* buf, const char* log_ident);
 
364
  bool is_active(const char* log_file_name);
 
365
  int update_log_index(LOG_INFO* linfo, bool need_update_threads);
 
366
  void rotate_and_purge(uint flags);
 
367
  bool flush_and_sync();
 
368
  int purge_logs(const char *to_log, bool included,
 
369
                 bool need_mutex, bool need_update_threads,
 
370
                 ulonglong *decrease_log_space);
 
371
  int purge_logs_before_date(time_t purge_time);
 
372
  int purge_first_log(Relay_log_info* rli, bool included);
 
373
  bool reset_logs(THD* thd);
 
374
  void close(uint exiting);
 
375
 
 
376
  // iterating through the log index file
 
377
  int find_log_pos(LOG_INFO* linfo, const char* log_name,
 
378
                   bool need_mutex);
 
379
  int find_next_log(LOG_INFO* linfo, bool need_mutex);
 
380
  int get_current_log(LOG_INFO* linfo);
 
381
  int raw_get_current_log(LOG_INFO* linfo);
 
382
  uint next_file_id();
 
383
  inline char* get_index_fname() { return index_file_name;}
 
384
  inline char* get_log_fname() { return log_file_name; }
 
385
  inline char* get_name() { return name; }
 
386
  inline pthread_mutex_t* get_log_lock() { return &LOCK_log; }
 
387
  inline IO_CACHE* get_log_file() { return &log_file; }
 
388
 
 
389
  inline void lock_index() { pthread_mutex_lock(&LOCK_index);}
 
390
  inline void unlock_index() { pthread_mutex_unlock(&LOCK_index);}
 
391
  inline IO_CACHE *get_index_file() { return &index_file;}
 
392
  inline uint32 get_open_count() { return open_count; }
 
393
};
 
394
 
 
395
class Log_event_handler
 
396
{
 
397
public:
 
398
  Log_event_handler() {}
 
399
  virtual bool init()= 0;
 
400
  virtual void cleanup()= 0;
 
401
 
 
402
  virtual bool log_slow(THD *thd, time_t current_time,
 
403
                        time_t query_start_arg, const char *user_host,
 
404
                        uint user_host_len, ulonglong query_utime,
 
405
                        ulonglong lock_utime, bool is_command,
 
406
                        const char *sql_text, uint sql_text_len)= 0;
 
407
  virtual bool log_error(enum loglevel level, const char *format,
 
408
                         va_list args)= 0;
 
409
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
410
                           uint user_host_len, int thread_id,
 
411
                           const char *command_type, uint command_type_len,
 
412
                           const char *sql_text, uint sql_text_len,
 
413
                           CHARSET_INFO *client_cs)= 0;
 
414
  virtual ~Log_event_handler() {}
 
415
};
 
416
 
 
417
 
 
418
/* type of the log table */
 
419
#define QUERY_LOG_SLOW 1
 
420
#define QUERY_LOG_GENERAL 2
 
421
 
 
422
class Log_to_file_event_handler: public Log_event_handler
 
423
{
 
424
  MYSQL_QUERY_LOG mysql_log;
 
425
  MYSQL_QUERY_LOG mysql_slow_log;
 
426
  bool is_initialized;
 
427
public:
 
428
  Log_to_file_event_handler(): is_initialized(FALSE)
 
429
  {}
 
430
  virtual bool init();
 
431
  virtual void cleanup();
 
432
 
 
433
  virtual bool log_slow(THD *thd, time_t current_time,
 
434
                        time_t query_start_arg, const char *user_host,
 
435
                        uint user_host_len, ulonglong query_utime,
 
436
                        ulonglong lock_utime, bool is_command,
 
437
                        const char *sql_text, uint sql_text_len);
 
438
  virtual bool log_error(enum loglevel level, const char *format,
 
439
                         va_list args);
 
440
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
441
                           uint user_host_len, int thread_id,
 
442
                           const char *command_type, uint command_type_len,
 
443
                           const char *sql_text, uint sql_text_len,
 
444
                           CHARSET_INFO *client_cs);
 
445
  void flush();
 
446
  void init_pthread_objects();
 
447
  MYSQL_QUERY_LOG *get_mysql_slow_log() { return &mysql_slow_log; }
 
448
  MYSQL_QUERY_LOG *get_mysql_log() { return &mysql_log; }
 
449
};
 
450
 
 
451
 
 
452
/* Class which manages slow, general and error log event handlers */
 
453
class LOGGER
 
454
{
 
455
  rw_lock_t LOCK_logger;
 
456
  /* flag to check whether logger mutex is initialized */
 
457
  uint inited;
 
458
 
 
459
  /* available log handlers */
 
460
  Log_to_file_event_handler *file_log_handler;
 
461
 
 
462
  /* NULL-terminated arrays of log handlers */
 
463
  Log_event_handler *error_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
464
  Log_event_handler *slow_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
465
  Log_event_handler *general_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
466
 
 
467
public:
 
468
 
 
469
  LOGGER() : inited(0), file_log_handler(NULL)
 
470
  {}
 
471
  void lock_shared() { rw_rdlock(&LOCK_logger); }
 
472
  void lock_exclusive() { rw_wrlock(&LOCK_logger); }
 
473
  void unlock() { rw_unlock(&LOCK_logger); }
 
474
  bool is_log_table_enabled(uint log_table_type);
 
475
  bool log_command(THD *thd, enum enum_server_command command);
 
476
 
 
477
  /*
 
478
    We want to initialize all log mutexes as soon as possible,
 
479
    but we cannot do it in constructor, as safe_mutex relies on
 
480
    initialization, performed by MY_INIT(). This why this is done in
 
481
    this function.
 
482
  */
 
483
  void init_base();
 
484
  bool flush_logs(THD *thd);
 
485
  /* Perform basic logger cleanup. this will leave e.g. error log open. */
 
486
  void cleanup_base();
 
487
  /* Free memory. Nothing could be logged after this function is called */
 
488
  void cleanup_end();
 
489
  bool error_log_print(enum loglevel level, const char *format,
 
490
                      va_list args);
 
491
  bool slow_log_print(THD *thd, const char *query, uint query_length,
 
492
                      ulonglong current_utime);
 
493
  bool general_log_print(THD *thd,enum enum_server_command command,
 
494
                         const char *format, va_list args);
 
495
  bool general_log_write(THD *thd, enum enum_server_command command,
 
496
                         const char *query, uint query_length);
 
497
 
 
498
  /* we use this function to setup all enabled log event handlers */
 
499
  int set_handlers(uint error_log_printer,
 
500
                   uint slow_log_printer,
 
501
                   uint general_log_printer);
 
502
  void init_error_log(uint error_log_printer);
 
503
  void init_slow_log(uint slow_log_printer);
 
504
  void init_general_log(uint general_log_printer);
 
505
  void deactivate_log_handler(THD* thd, uint log_type);
 
506
  bool activate_log_handler(THD* thd, uint log_type);
 
507
  MYSQL_QUERY_LOG *get_slow_log_file_handler()
 
508
  { 
 
509
    if (file_log_handler)
 
510
      return file_log_handler->get_mysql_slow_log();
 
511
    return NULL;
 
512
  }
 
513
  MYSQL_QUERY_LOG *get_log_file_handler()
 
514
  { 
 
515
    if (file_log_handler)
 
516
      return file_log_handler->get_mysql_log();
 
517
    return NULL;
 
518
  }
 
519
};
 
520
 
 
521
enum enum_binlog_format {
 
522
  /*
 
523
    statement-based except for cases where only row-based can work (UUID()
 
524
    etc):
 
525
  */
 
526
  BINLOG_FORMAT_MIXED= 0,
 
527
  BINLOG_FORMAT_STMT= 1, // statement-based
 
528
  BINLOG_FORMAT_ROW= 2, // row_based
 
529
/*
 
530
  This value is last, after the end of binlog_format_typelib: it has no
 
531
  corresponding cell in this typelib. We use this value to be able to know if
 
532
  the user has explicitely specified a binlog format at startup or not.
 
533
*/
 
534
  BINLOG_FORMAT_UNSPEC= 3
 
535
};
 
536
extern TYPELIB binlog_format_typelib;
 
537
 
 
538
#endif /* LOG_H */