~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/log.h

Removed dead variable, sorted authors file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
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
 
#ifndef DRIZZLE_SERVER_LOG_H
21
 
#define DRIZZLE_SERVER_LOG_H
22
 
 
23
 
#include <drizzled/common.h>
24
 
#include <mysys/iocache.h>
25
 
#include <drizzled/xid.h>
26
 
 
27
 
class Table;
 
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
 
28
19
class Relay_log_info;
29
 
class Session;
 
20
 
30
21
class Format_description_log_event;
31
22
 
32
23
/*
42
33
 
43
34
  virtual int open(const char *opt_name)=0;
44
35
  virtual void close()=0;
45
 
  virtual int log_xid(Session *session, my_xid xid)=0;
 
36
  virtual int log_xid(THD *thd, my_xid xid)=0;
46
37
  virtual void unlog(ulong cookie, my_xid xid)=0;
47
38
};
48
39
 
50
41
{
51
42
public:
52
43
  TC_LOG_DUMMY() {}
53
 
  int open(const char *)
 
44
  int open(const char *opt_name __attribute__((__unused__)))
54
45
  { return 0; }
55
46
  void close(void)                          { }
56
 
  int log_xid(Session *, my_xid)         { return 1; }
57
 
  void unlog(ulong, my_xid)  { }
 
47
  int log_xid(THD *thd __attribute__((__unused__)),
 
48
              my_xid xid __attribute__((__unused__)))         { return 1; }
 
49
  void unlog(ulong cookie __attribute__((__unused__)),
 
50
             my_xid xid __attribute__((__unused__)))  { }
58
51
};
59
52
 
60
53
#ifdef HAVE_MMAP
82
75
  char logname[FN_REFLEN];
83
76
  File fd;
84
77
  my_off_t file_length;
85
 
  uint32_t npages, inited;
86
 
  unsigned char *data;
 
78
  uint npages, inited;
 
79
  uchar *data;
87
80
  struct st_page *pages, *syncing, *active, *pool, *pool_last;
88
81
  /*
89
82
    note that, e.g. LOCK_active is only used to protect
98
91
  TC_LOG_MMAP(): inited(0) {}
99
92
  int open(const char *opt_name);
100
93
  void close();
101
 
  int log_xid(Session *session, my_xid xid);
 
94
  int log_xid(THD *thd, my_xid xid);
102
95
  void unlog(ulong cookie, my_xid xid);
103
96
  int recover();
104
97
 
159
152
/* log event handler flags */
160
153
#define LOG_NONE       1
161
154
#define LOG_FILE       2
 
155
#define LOG_TABLE      4
162
156
 
163
157
class Log_event;
164
158
class Rows_log_event;
171
165
  (mmap+fsync is two times faster than write+fsync)
172
166
*/
173
167
 
174
 
class DRIZZLE_LOG
 
168
class MYSQL_LOG
175
169
{
176
170
public:
177
 
  DRIZZLE_LOG();
 
171
  MYSQL_LOG();
178
172
  void init_pthread_objects();
179
173
  void cleanup();
180
174
  bool open(const char *log_name,
183
177
            enum cache_type io_cache_type_arg);
184
178
  void init(enum_log_type log_type_arg,
185
179
            enum cache_type io_cache_type_arg);
186
 
  void close(uint32_t exiting);
 
180
  void close(uint exiting);
187
181
  inline bool is_open() { return log_state != LOG_CLOSED; }
188
182
  const char *generate_name(const char *log_name, const char *suffix,
189
183
                            bool strip_ext, char *buff);
202
196
  friend class Log_event;
203
197
};
204
198
 
205
 
class DRIZZLE_BIN_LOG: public TC_LOG, private DRIZZLE_LOG
 
199
class MYSQL_QUERY_LOG: public MYSQL_LOG
 
200
{
 
201
public:
 
202
  MYSQL_QUERY_LOG() : last_time(0) {}
 
203
  void reopen_file();
 
204
  bool write(time_t event_time, const char *user_host,
 
205
             uint user_host_len, int thread_id,
 
206
             const char *command_type, uint command_type_len,
 
207
             const char *sql_text, uint sql_text_len);
 
208
  bool write(THD *thd, time_t current_time, time_t query_start_arg,
 
209
             const char *user_host, uint user_host_len,
 
210
             uint64_t query_utime, uint64_t lock_utime, bool is_command,
 
211
             const char *sql_text, uint sql_text_len);
 
212
  bool open_slow_log(const char *log_name)
 
213
  {
 
214
    char buf[FN_REFLEN];
 
215
    return open(generate_name(log_name, "-slow.log", 0, buf), LOG_NORMAL, 0,
 
216
                WRITE_CACHE);
 
217
  }
 
218
  bool open_query_log(const char *log_name)
 
219
  {
 
220
    char buf[FN_REFLEN];
 
221
    return open(generate_name(log_name, ".log", 0, buf), LOG_NORMAL, 0,
 
222
                WRITE_CACHE);
 
223
  }
 
224
 
 
225
private:
 
226
  time_t last_time;
 
227
};
 
228
 
 
229
class MYSQL_BIN_LOG: public TC_LOG, private MYSQL_LOG
206
230
{
207
231
 private:
208
232
  /* LOCK_log and LOCK_index are inited by init_pthread_objects() */
226
250
  ulong max_size;
227
251
  long prepared_xids; /* for tc log - number of xids to remember */
228
252
  // current file sequence number for load data infile binary logging
229
 
  uint32_t file_id;
230
 
  uint32_t open_count;                          // For replication
 
253
  uint file_id;
 
254
  uint open_count;                              // For replication
231
255
  int readers_count;
232
256
  bool need_start_event;
233
257
  /*
251
275
  void new_file_impl(bool need_lock);
252
276
 
253
277
public:
254
 
  DRIZZLE_LOG::generate_name;
255
 
  DRIZZLE_LOG::is_open;
 
278
  MYSQL_LOG::generate_name;
 
279
  MYSQL_LOG::is_open;
256
280
  /*
257
281
    These describe the log's format. This is used only for relay logs.
258
282
    _for_exec is used by the SQL thread, _for_queue by the I/O thread. It's
264
288
  Format_description_log_event *description_event_for_exec,
265
289
    *description_event_for_queue;
266
290
 
267
 
  DRIZZLE_BIN_LOG();
 
291
  MYSQL_BIN_LOG();
268
292
  /*
269
 
    note that there's no destructor ~DRIZZLE_BIN_LOG() !
 
293
    note that there's no destructor ~MYSQL_BIN_LOG() !
270
294
    The reason is that we don't want it to be automatically called
271
295
    on exit() - but only during the correct shutdown process
272
296
  */
273
297
 
274
298
  int open(const char *opt_name);
275
299
  void close();
276
 
  int log_xid(Session *session, my_xid xid);
 
300
  int log_xid(THD *thd, my_xid xid);
277
301
  void unlog(ulong cookie, my_xid xid);
278
302
  int recover(IO_CACHE *log, Format_description_log_event *fdle);
279
 
  bool is_table_mapped(Table *table) const;
 
303
#if !defined(MYSQL_CLIENT)
 
304
  bool is_table_mapped(TABLE *table) const
 
305
  {
 
306
    return table->s->table_map_version == table_map_version();
 
307
  }
280
308
 
281
309
  uint64_t table_map_version() const { return m_table_map_version; }
282
310
  void update_table_map_version() { ++m_table_map_version; }
283
311
 
284
 
  int flush_and_set_pending_rows_event(Session *session, Rows_log_event* event);
 
312
  int flush_and_set_pending_rows_event(THD *thd, Rows_log_event* event);
285
313
 
 
314
#endif /* !defined(MYSQL_CLIENT) */
286
315
  void reset_bytes_written()
287
316
  {
288
317
    bytes_written = 0;
295
324
  }
296
325
  void set_max_size(ulong max_size_arg);
297
326
  void signal_update();
298
 
  void wait_for_update_relay_log(Session* session);
299
 
  int  wait_for_update_bin_log(Session* session, const struct timespec * timeout);
 
327
  void wait_for_update_relay_log(THD* thd);
 
328
  int  wait_for_update_bin_log(THD* thd, const struct timespec * timeout);
300
329
  void set_need_start_event() { need_start_event = 1; }
301
330
  void init(bool no_auto_events_arg, ulong max_size);
302
331
  void init_pthread_objects();
313
342
  void new_file();
314
343
 
315
344
  bool write(Log_event* event_info); // binary log write
316
 
  bool write(Session *session, IO_CACHE *cache, Log_event *commit_event);
 
345
  bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event);
317
346
 
318
347
  int  write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync);
319
348
 
320
 
  void start_union_events(Session *session, query_id_t query_id_param);
321
 
  void stop_union_events(Session *session);
322
 
  bool is_query_in_union(Session *session, query_id_t query_id_param);
 
349
  void start_union_events(THD *thd, query_id_t query_id_param);
 
350
  void stop_union_events(THD *thd);
 
351
  bool is_query_in_union(THD *thd, query_id_t query_id_param);
323
352
 
324
353
  /*
325
354
    v stands for vector
326
355
    invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
327
356
  */
328
 
  bool appendv(const char* buf,uint32_t len,...);
 
357
  bool appendv(const char* buf,uint len,...);
329
358
  bool append(Log_event* ev);
330
359
 
331
360
  void make_log_name(char* buf, const char* log_ident);
332
361
  bool is_active(const char* log_file_name);
333
362
  int update_log_index(LOG_INFO* linfo, bool need_update_threads);
334
 
  void rotate_and_purge(uint32_t flags);
 
363
  void rotate_and_purge(uint flags);
335
364
  bool flush_and_sync();
336
365
  int purge_logs(const char *to_log, bool included,
337
366
                 bool need_mutex, bool need_update_threads,
338
367
                 uint64_t *decrease_log_space);
339
368
  int purge_logs_before_date(time_t purge_time);
340
369
  int purge_first_log(Relay_log_info* rli, bool included);
341
 
  bool reset_logs(Session* session);
342
 
  void close(uint32_t exiting);
 
370
  bool reset_logs(THD* thd);
 
371
  void close(uint exiting);
343
372
 
344
373
  // iterating through the log index file
345
374
  int find_log_pos(LOG_INFO* linfo, const char* log_name,
347
376
  int find_next_log(LOG_INFO* linfo, bool need_mutex);
348
377
  int get_current_log(LOG_INFO* linfo);
349
378
  int raw_get_current_log(LOG_INFO* linfo);
350
 
  uint32_t next_file_id();
 
379
  uint next_file_id();
351
380
  inline char* get_index_fname() { return index_file_name;}
352
381
  inline char* get_log_fname() { return log_file_name; }
353
382
  inline char* get_name() { return name; }
357
386
  inline void lock_index() { pthread_mutex_lock(&LOCK_index);}
358
387
  inline void unlock_index() { pthread_mutex_unlock(&LOCK_index);}
359
388
  inline IO_CACHE *get_index_file() { return &index_file;}
360
 
  inline uint32_t get_open_count() { return open_count; }
 
389
  inline uint32 get_open_count() { return open_count; }
361
390
};
362
391
 
363
 
 
364
 
File open_binlog(IO_CACHE *log, const char *log_file_name,
365
 
                 const char **errmsg);
366
 
bool flush_error_log(void);
367
 
 
368
392
class Log_event_handler
369
393
{
370
394
public:
372
396
  virtual bool init()= 0;
373
397
  virtual void cleanup()= 0;
374
398
 
 
399
  virtual bool log_slow(THD *thd, time_t current_time,
 
400
                        time_t query_start_arg, const char *user_host,
 
401
                        uint user_host_len, uint64_t query_utime,
 
402
                        uint64_t lock_utime, bool is_command,
 
403
                        const char *sql_text, uint sql_text_len)= 0;
375
404
  virtual bool log_error(enum loglevel level, const char *format,
376
405
                         va_list args)= 0;
 
406
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
407
                           uint user_host_len, int thread_id,
 
408
                           const char *command_type, uint command_type_len,
 
409
                           const char *sql_text, uint sql_text_len,
 
410
                           CHARSET_INFO *client_cs)= 0;
377
411
  virtual ~Log_event_handler() {}
378
412
};
379
413
 
 
414
 
 
415
/* type of the log table */
 
416
#define QUERY_LOG_SLOW 1
 
417
#define QUERY_LOG_GENERAL 2
 
418
 
 
419
class Log_to_file_event_handler: public Log_event_handler
 
420
{
 
421
  MYSQL_QUERY_LOG mysql_log;
 
422
  MYSQL_QUERY_LOG mysql_slow_log;
 
423
  bool is_initialized;
 
424
public:
 
425
  Log_to_file_event_handler(): is_initialized(false)
 
426
  {}
 
427
  virtual bool init();
 
428
  virtual void cleanup();
 
429
 
 
430
  virtual bool log_slow(THD *thd, time_t current_time,
 
431
                        time_t query_start_arg, const char *user_host,
 
432
                        uint user_host_len, uint64_t query_utime,
 
433
                        uint64_t lock_utime, bool is_command,
 
434
                        const char *sql_text, uint sql_text_len);
 
435
  virtual bool log_error(enum loglevel level, const char *format,
 
436
                         va_list args);
 
437
  virtual bool log_general(THD *thd, time_t event_time, const char *user_host,
 
438
                           uint user_host_len, int thread_id,
 
439
                           const char *command_type, uint command_type_len,
 
440
                           const char *sql_text, uint sql_text_len,
 
441
                           CHARSET_INFO *client_cs);
 
442
  void flush();
 
443
  void init_pthread_objects();
 
444
  MYSQL_QUERY_LOG *get_mysql_slow_log() { return &mysql_slow_log; }
 
445
  MYSQL_QUERY_LOG *get_mysql_log() { return &mysql_log; }
 
446
};
 
447
 
 
448
 
 
449
/* Class which manages slow, general and error log event handlers */
 
450
class LOGGER
 
451
{
 
452
  rw_lock_t LOCK_logger;
 
453
  /* flag to check whether logger mutex is initialized */
 
454
  uint inited;
 
455
 
 
456
  /* available log handlers */
 
457
  Log_to_file_event_handler *file_log_handler;
 
458
 
 
459
  /* NULL-terminated arrays of log handlers */
 
460
  Log_event_handler *error_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
461
  Log_event_handler *slow_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
462
  Log_event_handler *general_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
 
463
 
 
464
public:
 
465
 
 
466
  LOGGER() : inited(0), file_log_handler(NULL)
 
467
  {}
 
468
  void lock_shared() { rw_rdlock(&LOCK_logger); }
 
469
  void lock_exclusive() { rw_wrlock(&LOCK_logger); }
 
470
  void unlock() { rw_unlock(&LOCK_logger); }
 
471
  bool log_command(THD *thd, enum enum_server_command command);
 
472
 
 
473
  /*
 
474
    We want to initialize all log mutexes as soon as possible,
 
475
    but we cannot do it in constructor, as safe_mutex relies on
 
476
    initialization, performed by MY_INIT(). This why this is done in
 
477
    this function.
 
478
  */
 
479
  void init_base();
 
480
  bool flush_logs(THD *thd);
 
481
  /* Perform basic logger cleanup. this will leave e.g. error log open. */
 
482
  void cleanup_base();
 
483
  /* Free memory. Nothing could be logged after this function is called */
 
484
  void cleanup_end();
 
485
  bool error_log_print(enum loglevel level, const char *format,
 
486
                      va_list args);
 
487
  bool slow_log_print(THD *thd, const char *query, uint query_length,
 
488
                      uint64_t current_utime);
 
489
  bool general_log_print(THD *thd,enum enum_server_command command,
 
490
                         const char *format, va_list args);
 
491
  bool general_log_write(THD *thd, enum enum_server_command command,
 
492
                         const char *query, uint query_length);
 
493
 
 
494
  /* we use this function to setup all enabled log event handlers */
 
495
  int set_handlers(uint error_log_printer,
 
496
                   uint slow_log_printer,
 
497
                   uint general_log_printer);
 
498
  void init_error_log(uint error_log_printer);
 
499
  void init_slow_log(uint slow_log_printer);
 
500
  void init_general_log(uint general_log_printer);
 
501
  void deactivate_log_handler(THD* thd, uint log_type);
 
502
  bool activate_log_handler(THD* thd, uint log_type);
 
503
  MYSQL_QUERY_LOG *get_slow_log_file_handler()
 
504
  { 
 
505
    if (file_log_handler)
 
506
      return file_log_handler->get_mysql_slow_log();
 
507
    return NULL;
 
508
  }
 
509
  MYSQL_QUERY_LOG *get_log_file_handler()
 
510
  { 
 
511
    if (file_log_handler)
 
512
      return file_log_handler->get_mysql_log();
 
513
    return NULL;
 
514
  }
 
515
};
 
516
 
 
517
enum enum_binlog_format {
 
518
  /*
 
519
    statement-based except for cases where only row-based can work (UUID()
 
520
    etc):
 
521
  */
 
522
  BINLOG_FORMAT_MIXED= 0,
 
523
  BINLOG_FORMAT_STMT= 1, // statement-based
 
524
  BINLOG_FORMAT_ROW= 2, // row_based
 
525
/*
 
526
  This value is last, after the end of binlog_format_typelib: it has no
 
527
  corresponding cell in this typelib. We use this value to be able to know if
 
528
  the user has explicitely specified a binlog format at startup or not.
 
529
*/
 
530
  BINLOG_FORMAT_UNSPEC= 3
 
531
};
380
532
extern TYPELIB binlog_format_typelib;
381
533
 
382
 
void sql_print_error(const char *format, ...) __attribute__((format(printf, 1, 2)));
383
 
void sql_print_warning(const char *format, ...) __attribute__((format(printf, 1, 2)));
384
 
void sql_print_information(const char *format, ...)
385
 
  __attribute__((format(printf, 1, 2)));
386
 
typedef void (*sql_print_message_func)(const char *format, ...)
387
 
  __attribute__((format(printf, 1, 2)));
388
 
extern sql_print_message_func sql_print_message_handlers[];
389
 
 
390
 
void sql_perror(const char *message);
391
 
 
392
 
 
393
 
#endif /* DRIZZLE_SERVER_LOG_H */
 
534
#endif /* LOG_H */