~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
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
 */
1 by brian
clean slate
19
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
20
#ifndef DRIZZLE_SERVER_LOG_H
21
#define DRIZZLE_SERVER_LOG_H
1 by brian
clean slate
22
584.1.14 by Monty Taylor
Removed field.h from common_includes.
23
#include <drizzled/common.h>
481.1.16 by Monty Taylor
Merged iocache.h addition.
24
#include <mysys/iocache.h>
575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
25
#include <drizzled/xid.h>
481.1.16 by Monty Taylor
Merged iocache.h addition.
26
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
27
class Table;
1 by brian
clean slate
28
class Relay_log_info;
575.1.5 by Monty Taylor
Moved stuff to handlerton.cc
29
class Session;
1 by brian
clean slate
30
class Format_description_log_event;
31
32
/*
33
  Transaction Coordinator log - a base abstract class
34
  for two different implementations
35
*/
36
class TC_LOG
37
{
38
  public:
39
  int using_heuristic_recover();
40
  TC_LOG() {}
41
  virtual ~TC_LOG() {}
42
43
  virtual int open(const char *opt_name)=0;
44
  virtual void close()=0;
520.1.22 by Brian Aker
Second pass of thd cleanup
45
  virtual int log_xid(Session *session, my_xid xid)=0;
1 by brian
clean slate
46
  virtual void unlog(ulong cookie, my_xid xid)=0;
47
};
48
49
class TC_LOG_DUMMY: public TC_LOG // use it to disable the logging
50
{
51
public:
52
  TC_LOG_DUMMY() {}
212.3.1 by Jay Pipes
Fix for Bug#252309 "log output to tables is enabled but ineffective".
53
  int open(const char *opt_name __attribute__((unused)))
53.2.32 by Monty Taylor
First large swath at getting handler stuff clean.
54
  { return 0; }
55
  void close(void)                          { }
520.1.22 by Brian Aker
Second pass of thd cleanup
56
  int log_xid(Session *session __attribute__((unused)),
212.3.1 by Jay Pipes
Fix for Bug#252309 "log output to tables is enabled but ineffective".
57
              my_xid xid __attribute__((unused)))         { return 1; }
58
  void unlog(ulong cookie __attribute__((unused)),
59
             my_xid xid __attribute__((unused)))  { }
1 by brian
clean slate
60
};
61
62
#ifdef HAVE_MMAP
63
class TC_LOG_MMAP: public TC_LOG
64
{
65
  public:                // only to keep Sun Forte on sol9x86 happy
66
  typedef enum {
67
    POOL,                 // page is in pool
68
    ERROR,                // last sync failed
69
    DIRTY                 // new xids added since last sync
70
  } PAGE_STATE;
71
72
  private:
73
  typedef struct st_page {
74
    struct st_page *next; // page a linked in a fifo queue
75
    my_xid *start, *end;  // usable area of a page
76
    my_xid *ptr;          // next xid will be written here
77
    int size, free;       // max and current number of free xid slots on the page
78
    int waiters;          // number of waiters on condition
79
    PAGE_STATE state;     // see above
80
    pthread_mutex_t lock; // to access page data or control structure
81
    pthread_cond_t  cond; // to wait for a sync
82
  } PAGE;
83
84
  char logname[FN_REFLEN];
85
  File fd;
86
  my_off_t file_length;
482 by Brian Aker
Remove uint.
87
  uint32_t npages, inited;
481 by Brian Aker
Remove all of uchar.
88
  unsigned char *data;
1 by brian
clean slate
89
  struct st_page *pages, *syncing, *active, *pool, *pool_last;
90
  /*
91
    note that, e.g. LOCK_active is only used to protect
92
    'active' pointer, to protect the content of the active page
93
    one has to use active->lock.
94
    Same for LOCK_pool and LOCK_sync
95
  */
96
  pthread_mutex_t LOCK_active, LOCK_pool, LOCK_sync;
97
  pthread_cond_t COND_pool, COND_active;
98
99
  public:
100
  TC_LOG_MMAP(): inited(0) {}
101
  int open(const char *opt_name);
102
  void close();
520.1.22 by Brian Aker
Second pass of thd cleanup
103
  int log_xid(Session *session, my_xid xid);
1 by brian
clean slate
104
  void unlog(ulong cookie, my_xid xid);
105
  int recover();
106
107
  private:
108
  void get_active_from_pool();
109
  int sync();
110
  int overflow();
111
};
112
#else
113
#define TC_LOG_MMAP TC_LOG_DUMMY
114
#endif
115
116
extern TC_LOG *tc_log;
117
extern TC_LOG_MMAP tc_log_mmap;
118
extern TC_LOG_DUMMY tc_log_dummy;
119
120
/* log info errors */
121
#define LOG_INFO_EOF -1
122
#define LOG_INFO_IO  -2
123
#define LOG_INFO_INVALID -3
124
#define LOG_INFO_SEEK -4
125
#define LOG_INFO_MEM -6
126
#define LOG_INFO_FATAL -7
127
#define LOG_INFO_IN_USE -8
128
#define LOG_INFO_EMFILE -9
129
130
131
/* bitmap to SQL_LOG::close() */
132
#define LOG_CLOSE_INDEX		1
133
#define LOG_CLOSE_TO_BE_OPENED	2
134
#define LOG_CLOSE_STOP_EVENT	4
135
136
class Relay_log_info;
137
138
typedef struct st_log_info
139
{
140
  char log_file_name[FN_REFLEN];
141
  my_off_t index_file_offset, index_file_start_offset;
142
  my_off_t pos;
143
  bool fatal; // if the purge happens to give us a negative offset
144
  pthread_mutex_t lock;
145
  st_log_info()
146
    : index_file_offset(0), index_file_start_offset(0),
147
      pos(0), fatal(0)
148
    {
149
      log_file_name[0] = '\0';
150
      pthread_mutex_init(&lock, MY_MUTEX_INIT_FAST);
151
    }
152
  ~st_log_info() { pthread_mutex_destroy(&lock);}
153
} LOG_INFO;
154
155
/*
156
  Currently we have only 3 kinds of logging functions: old-fashioned
157
  logs, stdout and csv logging routines.
158
*/
159
#define MAX_LOG_HANDLERS_NUM 3
160
161
/* log event handler flags */
162
#define LOG_NONE       1
163
#define LOG_FILE       2
164
165
class Log_event;
166
class Rows_log_event;
167
168
enum enum_log_type { LOG_UNKNOWN, LOG_NORMAL, LOG_BIN };
169
enum enum_log_state { LOG_OPENED, LOG_CLOSED, LOG_TO_BE_OPENED };
170
171
/*
172
  TODO use mmap instead of IO_CACHE for binlog
173
  (mmap+fsync is two times faster than write+fsync)
174
*/
175
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
176
class DRIZZLE_LOG
1 by brian
clean slate
177
{
178
public:
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
179
  DRIZZLE_LOG();
1 by brian
clean slate
180
  void init_pthread_objects();
181
  void cleanup();
182
  bool open(const char *log_name,
183
            enum_log_type log_type,
184
            const char *new_name,
185
            enum cache_type io_cache_type_arg);
186
  void init(enum_log_type log_type_arg,
187
            enum cache_type io_cache_type_arg);
482 by Brian Aker
Remove uint.
188
  void close(uint32_t exiting);
1 by brian
clean slate
189
  inline bool is_open() { return log_state != LOG_CLOSED; }
190
  const char *generate_name(const char *log_name, const char *suffix,
191
                            bool strip_ext, char *buff);
192
  int generate_new_name(char *new_name, const char *log_name);
193
 protected:
194
  /* LOCK_log is inited by init_pthread_objects() */
195
  pthread_mutex_t LOCK_log;
196
  char *name;
197
  char log_file_name[FN_REFLEN];
198
  char time_buff[20], db[NAME_LEN + 1];
199
  bool write_error, inited;
200
  IO_CACHE log_file;
201
  enum_log_type log_type;
202
  volatile enum_log_state log_state;
203
  enum cache_type io_cache_type;
204
  friend class Log_event;
205
};
206
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
207
class DRIZZLE_BIN_LOG: public TC_LOG, private DRIZZLE_LOG
1 by brian
clean slate
208
{
209
 private:
210
  /* LOCK_log and LOCK_index are inited by init_pthread_objects() */
211
  pthread_mutex_t LOCK_index;
212
  pthread_mutex_t LOCK_prep_xids;
213
  pthread_cond_t  COND_prep_xids;
214
  pthread_cond_t update_cond;
151 by Brian Aker
Ulonglong to uint64_t
215
  uint64_t bytes_written;
1 by brian
clean slate
216
  IO_CACHE index_file;
217
  char index_file_name[FN_REFLEN];
218
  /*
219
     The max size before rotation (usable only if log_type == LOG_BIN: binary
220
     logs and relay logs).
221
     For a binlog, max_size should be max_binlog_size.
222
     For a relay log, it should be max_relay_log_size if this is non-zero,
223
     max_binlog_size otherwise.
224
     max_size is set in init(), and dynamically changed (when one does SET
225
     GLOBAL MAX_BINLOG_SIZE|MAX_RELAY_LOG_SIZE) by fix_max_binlog_size and
226
     fix_max_relay_log_size).
227
  */
228
  ulong max_size;
229
  long prepared_xids; /* for tc log - number of xids to remember */
230
  // current file sequence number for load data infile binary logging
482 by Brian Aker
Remove uint.
231
  uint32_t file_id;
232
  uint32_t open_count;				// For replication
1 by brian
clean slate
233
  int readers_count;
234
  bool need_start_event;
235
  /*
236
    no_auto_events means we don't want any of these automatic events :
237
    Start/Rotate/Stop. That is, in 4.x when we rotate a relay log, we don't
238
    want a Rotate_log event to be written to the relay log. When we start a
239
    relay log etc. So in 4.x this is 1 for relay logs, 0 for binlogs.
240
    In 5.0 it's 0 for relay logs too!
241
  */
242
  bool no_auto_events;
243
151 by Brian Aker
Ulonglong to uint64_t
244
  uint64_t m_table_map_version;
1 by brian
clean slate
245
246
  int write_to_file(IO_CACHE *cache);
247
  /*
248
    This is used to start writing to a new log file. The difference from
249
    new_file() is locking. new_file_without_locking() does not acquire
250
    LOCK_log.
251
  */
252
  void new_file_without_locking();
253
  void new_file_impl(bool need_lock);
254
255
public:
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
256
  DRIZZLE_LOG::generate_name;
257
  DRIZZLE_LOG::is_open;
1 by brian
clean slate
258
  /*
259
    These describe the log's format. This is used only for relay logs.
260
    _for_exec is used by the SQL thread, _for_queue by the I/O thread. It's
261
    necessary to have 2 distinct objects, because the I/O thread may be reading
262
    events in a different format from what the SQL thread is reading (consider
263
    the case of a master which has been upgraded from 5.0 to 5.1 without doing
264
    RESET MASTER, or from 4.x to 5.0).
265
  */
266
  Format_description_log_event *description_event_for_exec,
267
    *description_event_for_queue;
268
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
269
  DRIZZLE_BIN_LOG();
1 by brian
clean slate
270
  /*
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
271
    note that there's no destructor ~DRIZZLE_BIN_LOG() !
1 by brian
clean slate
272
    The reason is that we don't want it to be automatically called
273
    on exit() - but only during the correct shutdown process
274
  */
275
276
  int open(const char *opt_name);
277
  void close();
520.1.22 by Brian Aker
Second pass of thd cleanup
278
  int log_xid(Session *session, my_xid xid);
1 by brian
clean slate
279
  void unlog(ulong cookie, my_xid xid);
280
  int recover(IO_CACHE *log, Format_description_log_event *fdle);
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
281
  bool is_table_mapped(Table *table) const;
1 by brian
clean slate
282
151 by Brian Aker
Ulonglong to uint64_t
283
  uint64_t table_map_version() const { return m_table_map_version; }
1 by brian
clean slate
284
  void update_table_map_version() { ++m_table_map_version; }
285
520.1.22 by Brian Aker
Second pass of thd cleanup
286
  int flush_and_set_pending_rows_event(Session *session, Rows_log_event* event);
1 by brian
clean slate
287
288
  void reset_bytes_written()
289
  {
290
    bytes_written = 0;
291
  }
151 by Brian Aker
Ulonglong to uint64_t
292
  void harvest_bytes_written(uint64_t* counter)
1 by brian
clean slate
293
  {
294
    (*counter)+=bytes_written;
295
    bytes_written=0;
51.1.30 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
296
    return;
1 by brian
clean slate
297
  }
298
  void set_max_size(ulong max_size_arg);
299
  void signal_update();
520.1.22 by Brian Aker
Second pass of thd cleanup
300
  void wait_for_update_relay_log(Session* session);
301
  int  wait_for_update_bin_log(Session* session, const struct timespec * timeout);
1 by brian
clean slate
302
  void set_need_start_event() { need_start_event = 1; }
303
  void init(bool no_auto_events_arg, ulong max_size);
304
  void init_pthread_objects();
305
  void cleanup();
306
  bool open(const char *log_name,
307
            enum_log_type log_type,
308
            const char *new_name,
309
	    enum cache_type io_cache_type_arg,
310
	    bool no_auto_events_arg, ulong max_size,
311
            bool null_created);
312
  bool open_index_file(const char *index_file_name_arg,
313
                       const char *log_name);
314
  /* Use this to start writing a new log file */
315
  void new_file();
316
317
  bool write(Log_event* event_info); // binary log write
520.1.22 by Brian Aker
Second pass of thd cleanup
318
  bool write(Session *session, IO_CACHE *cache, Log_event *commit_event);
1 by brian
clean slate
319
320
  int  write_cache(IO_CACHE *cache, bool lock_log, bool flush_and_sync);
321
520.1.22 by Brian Aker
Second pass of thd cleanup
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);
1 by brian
clean slate
325
326
  /*
327
    v stands for vector
328
    invoked as appendv(buf1,len1,buf2,len2,...,bufn,lenn,0)
329
  */
482 by Brian Aker
Remove uint.
330
  bool appendv(const char* buf,uint32_t len,...);
1 by brian
clean slate
331
  bool append(Log_event* ev);
332
333
  void make_log_name(char* buf, const char* log_ident);
334
  bool is_active(const char* log_file_name);
335
  int update_log_index(LOG_INFO* linfo, bool need_update_threads);
482 by Brian Aker
Remove uint.
336
  void rotate_and_purge(uint32_t flags);
1 by brian
clean slate
337
  bool flush_and_sync();
338
  int purge_logs(const char *to_log, bool included,
339
                 bool need_mutex, bool need_update_threads,
151 by Brian Aker
Ulonglong to uint64_t
340
                 uint64_t *decrease_log_space);
1 by brian
clean slate
341
  int purge_logs_before_date(time_t purge_time);
342
  int purge_first_log(Relay_log_info* rli, bool included);
520.1.22 by Brian Aker
Second pass of thd cleanup
343
  bool reset_logs(Session* session);
482 by Brian Aker
Remove uint.
344
  void close(uint32_t exiting);
1 by brian
clean slate
345
346
  // iterating through the log index file
347
  int find_log_pos(LOG_INFO* linfo, const char* log_name,
348
		   bool need_mutex);
349
  int find_next_log(LOG_INFO* linfo, bool need_mutex);
350
  int get_current_log(LOG_INFO* linfo);
351
  int raw_get_current_log(LOG_INFO* linfo);
482 by Brian Aker
Remove uint.
352
  uint32_t next_file_id();
1 by brian
clean slate
353
  inline char* get_index_fname() { return index_file_name;}
354
  inline char* get_log_fname() { return log_file_name; }
355
  inline char* get_name() { return name; }
356
  inline pthread_mutex_t* get_log_lock() { return &LOCK_log; }
357
  inline IO_CACHE* get_log_file() { return &log_file; }
358
359
  inline void lock_index() { pthread_mutex_lock(&LOCK_index);}
360
  inline void unlock_index() { pthread_mutex_unlock(&LOCK_index);}
361
  inline IO_CACHE *get_index_file() { return &index_file;}
205 by Brian Aker
uint32 -> uin32_t
362
  inline uint32_t get_open_count() { return open_count; }
1 by brian
clean slate
363
};
364
365
class Log_event_handler
366
{
367
public:
368
  Log_event_handler() {}
369
  virtual bool init()= 0;
370
  virtual void cleanup()= 0;
371
372
  virtual bool log_error(enum loglevel level, const char *format,
373
                         va_list args)= 0;
374
  virtual ~Log_event_handler() {}
375
};
376
377
378
/* Class which manages slow, general and error log event handlers */
379
class LOGGER
380
{
381
  rw_lock_t LOCK_logger;
382
  /* flag to check whether logger mutex is initialized */
482 by Brian Aker
Remove uint.
383
  uint32_t inited;
1 by brian
clean slate
384
385
  /* NULL-terminated arrays of log handlers */
386
  Log_event_handler *error_log_handler_list[MAX_LOG_HANDLERS_NUM + 1];
387
388
public:
389
438.1.1 by Brian Aker
First pass of removing historical logging.
390
  LOGGER() : inited(0)
1 by brian
clean slate
391
  {}
392
  void lock_shared() { rw_rdlock(&LOCK_logger); }
393
  void lock_exclusive() { rw_wrlock(&LOCK_logger); }
394
  void unlock() { rw_unlock(&LOCK_logger); }
520.1.22 by Brian Aker
Second pass of thd cleanup
395
  bool log_command(Session *session, enum enum_server_command command);
1 by brian
clean slate
396
397
  /*
398
    We want to initialize all log mutexes as soon as possible,
399
    but we cannot do it in constructor, as safe_mutex relies on
400
    initialization, performed by MY_INIT(). This why this is done in
401
    this function.
402
  */
403
  void init_base();
520.1.22 by Brian Aker
Second pass of thd cleanup
404
  bool flush_logs(Session *session);
1 by brian
clean slate
405
  /* Perform basic logger cleanup. this will leave e.g. error log open. */
406
  void cleanup_base();
407
  /* Free memory. Nothing could be logged after this function is called */
408
  void cleanup_end();
409
  bool error_log_print(enum loglevel level, const char *format,
410
                      va_list args);
411
  /* we use this function to setup all enabled log event handlers */
482 by Brian Aker
Remove uint.
412
  int set_handlers(uint32_t error_log_printer);
413
  void init_error_log(uint32_t error_log_printer);
1 by brian
clean slate
414
};
415
416
extern TYPELIB binlog_format_typelib;
417
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
418
void sql_print_error(const char *format, ...) __attribute__((format(printf, 1, 2)));
419
void sql_print_warning(const char *format, ...) __attribute__((format(printf, 1, 2)));
420
void sql_print_information(const char *format, ...)
421
  __attribute__((format(printf, 1, 2)));
422
typedef void (*sql_print_message_func)(const char *format, ...)
423
  __attribute__((format(printf, 1, 2)));
424
extern sql_print_message_func sql_print_message_handlers[];
425
426
int error_log_print(enum loglevel level, const char *format,
427
                    va_list args);
428
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
429
void sql_perror(const char *message);
430
431
243.1.5 by Jay Pipes
* Pulled the remainder of the log and parse stuff out into
432
#endif /* DRIZZLE_SERVER_LOG_H */