~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/log.h

  • Committer: Brian Aker
  • Date: 2008-06-30 18:52:15 UTC
  • Revision ID: brian@tangent.org-20080630185215-n0fr4k0gk5hk785c
Remove dead position (should be in config/)

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 <mysys/iocache.h>
 
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
24
18
 
25
19
class Relay_log_info;
26
20
 
39
33
 
40
34
  virtual int open(const char *opt_name)=0;
41
35
  virtual void close()=0;
42
 
  virtual int log_xid(Session *session, my_xid xid)=0;
 
36
  virtual int log_xid(THD *thd, my_xid xid)=0;
43
37
  virtual void unlog(ulong cookie, my_xid xid)=0;
44
38
};
45
39
 
47
41
{
48
42
public:
49
43
  TC_LOG_DUMMY() {}
50
 
  int open(const char *opt_name __attribute__((unused)))
51
 
  { return 0; }
52
 
  void close(void)                          { }
53
 
  int log_xid(Session *session __attribute__((unused)),
54
 
              my_xid xid __attribute__((unused)))         { return 1; }
55
 
  void unlog(ulong cookie __attribute__((unused)),
56
 
             my_xid xid __attribute__((unused)))  { }
 
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)  { }
57
48
};
58
49
 
59
50
#ifdef HAVE_MMAP
81
72
  char logname[FN_REFLEN];
82
73
  File fd;
83
74
  my_off_t file_length;
84
 
  uint32_t npages, inited;
85
 
  unsigned char *data;
 
75
  uint npages, inited;
 
76
  uchar *data;
86
77
  struct st_page *pages, *syncing, *active, *pool, *pool_last;
87
78
  /*
88
79
    note that, e.g. LOCK_active is only used to protect
97
88
  TC_LOG_MMAP(): inited(0) {}
98
89
  int open(const char *opt_name);
99
90
  void close();
100
 
  int log_xid(Session *session, my_xid xid);
 
91
  int log_xid(THD *thd, my_xid xid);
101
92
  void unlog(ulong cookie, my_xid xid);
102
93
  int recover();
103
94
 
158
149
/* log event handler flags */
159
150
#define LOG_NONE       1
160
151
#define LOG_FILE       2
 
152
#define LOG_TABLE      4
161
153
 
162
154
class Log_event;
163
155
class Rows_log_event;
170
162
  (mmap+fsync is two times faster than write+fsync)
171
163
*/
172
164
 
173
 
class DRIZZLE_LOG
 
165
class MYSQL_LOG
174
166
{
175
167
public:
176
 
  DRIZZLE_LOG();
 
168
  MYSQL_LOG();
177
169
  void init_pthread_objects();
178
170
  void cleanup();
179
171
  bool open(const char *log_name,
182
174
            enum cache_type io_cache_type_arg);
183
175
  void init(enum_log_type log_type_arg,
184
176
            enum cache_type io_cache_type_arg);
185
 
  void close(uint32_t exiting);
 
177
  void close(uint exiting);
186
178
  inline bool is_open() { return log_state != LOG_CLOSED; }
187
179
  const char *generate_name(const char *log_name, const char *suffix,
188
180
                            bool strip_ext, char *buff);
201
193
  friend class Log_event;
202
194
};
203
195
 
204
 
class DRIZZLE_BIN_LOG: public TC_LOG, private DRIZZLE_LOG
 
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
205
227
{
206
228
 private:
207
229
  /* LOCK_log and LOCK_index are inited by init_pthread_objects() */
209
231
  pthread_mutex_t LOCK_prep_xids;
210
232
  pthread_cond_t  COND_prep_xids;
211
233
  pthread_cond_t update_cond;
212
 
  uint64_t bytes_written;
 
234
  ulonglong bytes_written;
213
235
  IO_CACHE index_file;
214
236
  char index_file_name[FN_REFLEN];
215
237
  /*
225
247
  ulong max_size;
226
248
  long prepared_xids; /* for tc log - number of xids to remember */
227
249
  // current file sequence number for load data infile binary logging
228
 
  uint32_t file_id;
229
 
  uint32_t open_count;                          // For replication
 
250
  uint file_id;
 
251
  uint open_count;                              // For replication
230
252
  int readers_count;
231
253
  bool need_start_event;
232
254
  /*
238
260
  */
239
261
  bool no_auto_events;
240
262
 
241
 
  uint64_t m_table_map_version;
 
263
  ulonglong m_table_map_version;
242
264
 
243
265
  int write_to_file(IO_CACHE *cache);
244
266
  /*
250
272
  void new_file_impl(bool need_lock);
251
273
 
252
274
public:
253
 
  DRIZZLE_LOG::generate_name;
254
 
  DRIZZLE_LOG::is_open;
 
275
  MYSQL_LOG::generate_name;
 
276
  MYSQL_LOG::is_open;
255
277
  /*
256
278
    These describe the log's format. This is used only for relay logs.
257
279
    _for_exec is used by the SQL thread, _for_queue by the I/O thread. It's
263
285
  Format_description_log_event *description_event_for_exec,
264
286
    *description_event_for_queue;
265
287
 
266
 
  DRIZZLE_BIN_LOG();
 
288
  MYSQL_BIN_LOG();
267
289
  /*
268
 
    note that there's no destructor ~DRIZZLE_BIN_LOG() !
 
290
    note that there's no destructor ~MYSQL_BIN_LOG() !
269
291
    The reason is that we don't want it to be automatically called
270
292
    on exit() - but only during the correct shutdown process
271
293
  */
272
294
 
273
295
  int open(const char *opt_name);
274
296
  void close();
275
 
  int log_xid(Session *session, my_xid xid);
 
297
  int log_xid(THD *thd, my_xid xid);
276
298
  void unlog(ulong cookie, my_xid xid);
277
299
  int recover(IO_CACHE *log, Format_description_log_event *fdle);
278
 
  bool is_table_mapped(Table *table) const
 
300
#if !defined(MYSQL_CLIENT)
 
301
  bool is_table_mapped(TABLE *table) const
279
302
  {
280
303
    return table->s->table_map_version == table_map_version();
281
304
  }
282
305
 
283
 
  uint64_t table_map_version() const { return m_table_map_version; }
 
306
  ulonglong table_map_version() const { return m_table_map_version; }
284
307
  void update_table_map_version() { ++m_table_map_version; }
285
308
 
286
 
  int flush_and_set_pending_rows_event(Session *session, Rows_log_event* event);
 
309
  int flush_and_set_pending_rows_event(THD *thd, Rows_log_event* event);
287
310
 
 
311
#endif /* !defined(MYSQL_CLIENT) */
288
312
  void reset_bytes_written()
289
313
  {
290
314
    bytes_written = 0;
291
315
  }
292
 
  void harvest_bytes_written(uint64_t* counter)
 
316
  void harvest_bytes_written(ulonglong* counter)
293
317
  {
 
318
#ifndef DBUG_OFF
 
319
    char buf1[22],buf2[22];
 
320
#endif
 
321
    DBUG_ENTER("harvest_bytes_written");
294
322
    (*counter)+=bytes_written;
 
323
    DBUG_PRINT("info",("counter: %s  bytes_written: %s", llstr(*counter,buf1),
 
324
                       llstr(bytes_written,buf2)));
295
325
    bytes_written=0;
296
 
    return;
 
326
    DBUG_VOID_RETURN;
297
327
  }
298
328
  void set_max_size(ulong max_size_arg);
299
329
  void signal_update();
300
 
  void wait_for_update_relay_log(Session* session);
301
 
  int  wait_for_update_bin_log(Session* session, const struct timespec * timeout);
 
330
  void wait_for_update_relay_log(THD* thd);
 
331
  int  wait_for_update_bin_log(THD* thd, const struct timespec * timeout);
302
332
  void set_need_start_event() { need_start_event = 1; }
303
333
  void init(bool no_auto_events_arg, ulong max_size);
304
334
  void init_pthread_objects();
315
345
  void new_file();
316
346
 
317
347
  bool write(Log_event* event_info); // binary log write
318
 
  bool write(Session *session, IO_CACHE *cache, Log_event *commit_event);
 
348
  bool write(THD *thd, IO_CACHE *cache, Log_event *commit_event);
319
349
 
320
350
  int  write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync);
321
351
 
322
 
  void start_union_events(Session *session, query_id_t query_id_param);
323
 
  void stop_union_events(Session *session);
324
 
  bool is_query_in_union(Session *session, query_id_t query_id_param);
 
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);
325
355
 
326
356
  /*
327
357
    v stands for vector
328
358
    invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
329
359
  */
330
 
  bool appendv(const char* buf,uint32_t len,...);
 
360
  bool appendv(const char* buf,uint len,...);
331
361
  bool append(Log_event* ev);
332
362
 
333
363
  void make_log_name(char* buf, const char* log_ident);
334
364
  bool is_active(const char* log_file_name);
335
365
  int update_log_index(LOG_INFO* linfo, bool need_update_threads);
336
 
  void rotate_and_purge(uint32_t flags);
 
366
  void rotate_and_purge(uint flags);
337
367
  bool flush_and_sync();
338
368
  int purge_logs(const char *to_log, bool included,
339
369
                 bool need_mutex, bool need_update_threads,
340
 
                 uint64_t *decrease_log_space);
 
370
                 ulonglong *decrease_log_space);
341
371
  int purge_logs_before_date(time_t purge_time);
342
372
  int purge_first_log(Relay_log_info* rli, bool included);
343
 
  bool reset_logs(Session* session);
344
 
  void close(uint32_t exiting);
 
373
  bool reset_logs(THD* thd);
 
374
  void close(uint exiting);
345
375
 
346
376
  // iterating through the log index file
347
377
  int find_log_pos(LOG_INFO* linfo, const char* log_name,
349
379
  int find_next_log(LOG_INFO* linfo, bool need_mutex);
350
380
  int get_current_log(LOG_INFO* linfo);
351
381
  int raw_get_current_log(LOG_INFO* linfo);
352
 
  uint32_t next_file_id();
 
382
  uint next_file_id();
353
383
  inline char* get_index_fname() { return index_file_name;}
354
384
  inline char* get_log_fname() { return log_file_name; }
355
385
  inline char* get_name() { return name; }
359
389
  inline void lock_index() { pthread_mutex_lock(&LOCK_index);}
360
390
  inline void unlock_index() { pthread_mutex_unlock(&LOCK_index);}
361
391
  inline IO_CACHE *get_index_file() { return &index_file;}
362
 
  inline uint32_t get_open_count() { return open_count; }
 
392
  inline uint32 get_open_count() { return open_count; }
363
393
};
364
394
 
365
395
class Log_event_handler
369
399
  virtual bool init()= 0;
370
400
  virtual void cleanup()= 0;
371
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;
372
407
  virtual bool log_error(enum loglevel level, const char *format,
373
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;
374
414
  virtual ~Log_event_handler() {}
375
415
};
376
416
 
377
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
 
378
452
/* Class which manages slow, general and error log event handlers */
379
453
class LOGGER
380
454
{
381
455
  rw_lock_t LOCK_logger;
382
456
  /* flag to check whether logger mutex is initialized */
383
 
  uint32_t inited;
 
457
  uint inited;
 
458
 
 
459
  /* available log handlers */
 
460
  Log_to_file_event_handler *file_log_handler;
384
461
 
385
462
  /* NULL-terminated arrays of log handlers */
386
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];
387
466
 
388
467
public:
389
468
 
390
 
  LOGGER() : inited(0)
 
469
  LOGGER() : inited(0), file_log_handler(NULL)
391
470
  {}
392
471
  void lock_shared() { rw_rdlock(&LOCK_logger); }
393
472
  void lock_exclusive() { rw_wrlock(&LOCK_logger); }
394
473
  void unlock() { rw_unlock(&LOCK_logger); }
395
 
  bool log_command(Session *session, enum enum_server_command command);
 
474
  bool is_log_table_enabled(uint log_table_type);
 
475
  bool log_command(THD *thd, enum enum_server_command command);
396
476
 
397
477
  /*
398
478
    We want to initialize all log mutexes as soon as possible,
401
481
    this function.
402
482
  */
403
483
  void init_base();
404
 
  bool flush_logs(Session *session);
 
484
  bool flush_logs(THD *thd);
405
485
  /* Perform basic logger cleanup. this will leave e.g. error log open. */
406
486
  void cleanup_base();
407
487
  /* Free memory. Nothing could be logged after this function is called */
408
488
  void cleanup_end();
409
489
  bool error_log_print(enum loglevel level, const char *format,
410
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
 
411
498
  /* we use this function to setup all enabled log event handlers */
412
 
  int set_handlers(uint32_t error_log_printer);
413
 
  void init_error_log(uint32_t error_log_printer);
 
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
  }
414
519
};
415
520
 
416
521
enum enum_binlog_format {
430
535
};
431
536
extern TYPELIB binlog_format_typelib;
432
537
 
433
 
int vprint_msg_to_log(enum loglevel level, const char *format, va_list args);
434
 
void sql_print_error(const char *format, ...) __attribute__((format(printf, 1, 2)));
435
 
void sql_print_warning(const char *format, ...) __attribute__((format(printf, 1, 2)));
436
 
void sql_print_information(const char *format, ...)
437
 
  __attribute__((format(printf, 1, 2)));
438
 
typedef void (*sql_print_message_func)(const char *format, ...)
439
 
  __attribute__((format(printf, 1, 2)));
440
 
extern sql_print_message_func sql_print_message_handlers[];
441
 
 
442
 
int error_log_print(enum loglevel level, const char *format,
443
 
                    va_list args);
444
 
 
445
 
void sql_perror(const char *message);
446
 
 
447
 
 
448
 
#endif /* DRIZZLE_SERVER_LOG_H */
 
538
#endif /* LOG_H */