~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 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
17
/**
18
  @file
19
20
  @brief
21
  logging of commands
22
23
  @todo
24
    Abort logging when we get an error in reading or writing log files
25
*/
26
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
27
#include <drizzled/server_includes.h>
663 by Brian Aker
End points on new replication (aka end transaction)
28
#include <drizzled/replicator.h>
614 by Brian Aker
Remove filtering (wrong layer, belongs in plugin).
29
#include <mysys/hash.h>
1 by brian
clean slate
30
212.5.38 by Monty Taylor
Moved my_dir (and removed references to a lot of places)
31
#include <mysys/my_dir.h>
1 by brian
clean slate
32
#include <stdarg.h>
33
212.5.10 by Monty Taylor
Moved drizzle/plugin*h to drizzled
34
#include <drizzled/plugin.h>
549 by Monty Taylor
Took gettext.h out of header files.
35
#include <drizzled/error.h>
722.4.1 by Mark Atwood
integrate errmsg plugin into sql_print_* functions
36
#include <drizzled/errmsg_print.h>
538 by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib.
37
#include <drizzled/gettext.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
38
#include <drizzled/data_home.h>
798.2.31 by Brian Aker
Vast majority of the replication code removed, new plugin points now
39
#include <drizzled/session.h>
40
#include <drizzled/handler.h>
572.1.4 by Monty Taylor
Removed a bunch of unusued tests and defines from autoconf.
41
960.2.41 by Monty Taylor
Made StorageEngine name private. Added constructor param and accessor method.
42
#include <string>
43
44
using namespace std;
45
46
static const string engine_name("binlog");
47
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
48
StorageEngine *binlog_engine= NULL;
49
50
/*
1 by brian
clean slate
51
  Save position of binary log transaction cache.
52
53
  SYNPOSIS
54
    binlog_trans_log_savepos()
55
520.1.22 by Brian Aker
Second pass of thd cleanup
56
    session      The thread to take the binlog data from
1 by brian
clean slate
57
    pos      Pointer to variable where the position will be stored
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
58
 
1 by brian
clean slate
59
  DESCRIPTION
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
60
 
1 by brian
clean slate
61
    Save the current position in the binary log transaction cache into
62
    the variable pointed to by 'pos'
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
63
*/
1 by brian
clean slate
64
65
static void
668 by Brian Aker
Removd dead TRX binlog code (we log straight... no attempt to optimize for
66
binlog_trans_log_savepos(Session *, my_off_t *pos)
1 by brian
clean slate
67
{
51.1.29 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
68
  assert(pos != NULL);
668 by Brian Aker
Removd dead TRX binlog code (we log straight... no attempt to optimize for
69
51.1.29 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
70
  return;
1 by brian
clean slate
71
}
72
73
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
74
class BinlogEngine : public StorageEngine
75
{
76
public:
964.1.4 by Monty Taylor
Moved flags into private area.
77
  BinlogEngine(const string &name_arg)
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
78
    : StorageEngine(name_arg,
79
                    HTON_NOT_USER_SELECTABLE | HTON_HIDDEN,
80
                    sizeof(my_off_t)) {}
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
81
 
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
82
  virtual int close_connection(Session *)
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
83
  {
84
  
85
    return 0;
86
  }
87
  
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
88
  virtual int prepare(Session *session, bool)
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
89
  {
90
    /*
91
      do nothing.
92
      just pretend we can do 2pc, so that MySQL won't
93
      switch to 1pc.
94
      real work will be done in DRIZZLE_BIN_LOG::log_xid()
95
    */
96
  
97
    (void)replicator_prepare(session);
98
  
99
    return 0;
100
  }
101
  
102
  /**
103
    This function is called once after each statement.
104
  
105
    It has the responsibility to flush the transaction cache to the
106
    binlog file on commits.
107
  
108
    @param engine  The binlog StorageEngine.
109
    @param session   The client thread that executes the transaction.
110
    @param all   This is @c true if this is a real transaction commit, and
111
                 @false otherwise.
112
  
113
    @see StorageEngine::commit
114
  */
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
115
  virtual int commit(Session *session, bool all)
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
116
  {
117
    /*
118
      Decision table for committing a transaction. The top part, the
119
      *conditions* represent different cases that can occur, and hte
120
      bottom part, the *actions*, represent what should be done in that
121
      particular case.
122
  
123
      Real transaction        'all' was true
124
  
125
      Statement in cache      There were at least one statement in the
126
                              transaction cache
127
  
128
      In transaction          We are inside a transaction
129
  
130
      Stmt modified non-trans The statement being committed modified a
131
                              non-transactional table
132
  
133
      All modified non-trans  Some statement before this one in the
134
                              transaction modified a non-transactional
135
                              table
136
  
137
  
138
      =============================  = = = = = = = = = = = = = = = =
139
      Real transaction               N N N N N N N N N N N N N N N N
140
      Statement in cache             N N N N N N N N Y Y Y Y Y Y Y Y
141
      In transaction                 N N N N Y Y Y Y N N N N Y Y Y Y
142
      Stmt modified non-trans        N N Y Y N N Y Y N N Y Y N N Y Y
143
      All modified non-trans         N Y N Y N Y N Y N Y N Y N Y N Y
144
  
145
      Action: (C)ommit/(A)ccumulate  C C - C A C - C - - - - A A - A
146
      =============================  = = = = = = = = = = = = = = = =
147
  
148
  
149
      =============================  = = = = = = = = = = = = = = = =
150
      Real transaction               Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y
151
      Statement in cache             N N N N N N N N Y Y Y Y Y Y Y Y
152
      In transaction                 N N N N Y Y Y Y N N N N Y Y Y Y
153
      Stmt modified non-trans        N N Y Y N N Y Y N N Y Y N N Y Y
154
      All modified non-trans         N Y N Y N Y N Y N Y N Y N Y N Y
155
  
156
      (C)ommit/(A)ccumulate/(-)      - - - - C C - C - - - - C C - C
157
      =============================  = = = = = = = = = = = = = = = =
158
  
159
      In other words, we commit the transaction if and only if both of
160
      the following are true:
161
       - We are not in a transaction and committing a statement
162
  
163
       - We are in a transaction and one (or more) of the following are
164
         true:
165
  
166
         - A full transaction is committed
167
  
168
           OR
169
  
170
         - A non-transactional statement is committed and there is
171
           no statement cached
172
  
173
      Otherwise, we accumulate the statement
174
    */
175
  
176
    if (all || (!session_test_options(session, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))) 
177
    {
178
      return replicator_end_transaction(session, all, true);
179
    }
180
  
181
    return(0);
182
  }
183
  
184
  /**
185
    This function is called when a transaction involving a transactional
186
    table is rolled back.
187
  
188
    It has the responsibility to flush the transaction cache to the
189
    binlog file. However, if the transaction does not involve
190
    non-transactional tables, nothing needs to be logged.
191
  
192
    @param engine  The binlog StorageEngine.
193
    @param session   The client thread that executes the transaction.
194
    @param all   This is @c true if this is a real transaction rollback, and
195
                 @false otherwise.
196
  
197
    @see StorageEngine::rollback
198
  */
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
199
  virtual int rollback(Session *session, bool all)
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
200
  {
201
    int error=0;
202
  
203
    /* TODO: Fix return type */
204
    (void)replicator_end_transaction(session, all, false);
205
  
206
    return(error);
207
  }
208
  
209
  /**
210
    @note
211
    How do we handle this (unlikely but legal) case:
212
    @verbatim
213
      [transaction] + [update to non-trans table] + [rollback to savepoint] ?
214
    @endverbatim
215
    The problem occurs when a savepoint is before the update to the
216
    non-transactional table. Then when there's a rollback to the savepoint, if we
217
    simply truncate the binlog cache, we lose the part of the binlog cache where
218
    the update is. If we want to not lose it, we need to write the SAVEPOINT
219
    command and the ROLLBACK TO SAVEPOINT command to the binlog cache. The latter
220
    is easy: it's just write at the end of the binlog cache, but the former
221
    should be *inserted* to the place where the user called SAVEPOINT. The
222
    solution is that when the user calls SAVEPOINT, we write it to the binlog
223
    cache (so no need to later insert it). As transactions are never intermixed
224
    in the binary log (i.e. they are serialized), we won't have conflicts with
225
    savepoint names when using mysqlbinlog or in the slave SQL thread.
226
    Then when ROLLBACK TO SAVEPOINT is called, if we updated some
227
    non-transactional table, we don't truncate the binlog cache but instead write
228
    ROLLBACK TO SAVEPOINT to it; otherwise we truncate the binlog cache (which
229
    will chop the SAVEPOINT command from the binlog cache, which is good as in
230
    that case there is no need to have it in the binlog).
231
  */
232
  
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
233
  virtual int savepoint_set_hook(Session *session, void *sv)
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
234
  {
235
    bool error;
236
    binlog_trans_log_savepos(session, (my_off_t*) sv);
237
    /* Write it to the binary log */
238
  
239
    error= replicator_statement(session, session->query, session->query_length);
240
  
241
    return(error);
242
  }
243
  
964.1.6 by Monty Taylor
Moved savepoint stuff to protected virtual methods, moved the offset to private, and moved the logic about adding the offset to the passed-in pointer and adding the offset to the global offset inside of the class.
244
  virtual int savepoint_rollback_hook(Session *session, void *)
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
245
  {
246
    bool error;
247
  
248
    error= replicator_statement(session, session->query, session->query_length);
249
  
250
    return error;
251
  }
252
960.2.38 by Monty Taylor
Removed extraneous send myself to myself argument.
253
  virtual handler* create(TABLE_SHARE*, MEM_ROOT*)
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
254
  {
255
    return NULL;
256
  }
257
258
259
};
260
1 by brian
clean slate
261
/*
262
  this function is mostly a placeholder.
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
263
  conceptually, binlog initialization (now mostly done in DRIZZLE_BIN_LOG::open)
1 by brian
clean slate
264
  should be moved here.
265
*/
266
267
int binlog_init(void *p)
268
{
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
269
  StorageEngine** engine= static_cast<StorageEngine **>(p);
270
271
  if (binlog_engine == NULL)
1 by brian
clean slate
272
  {
960.2.41 by Monty Taylor
Made StorageEngine name private. Added constructor param and accessor method.
273
    binlog_engine= new BinlogEngine(engine_name);
1 by brian
clean slate
274
  }
668 by Brian Aker
Removd dead TRX binlog code (we log straight... no attempt to optimize for
275
960.2.27 by Monty Taylor
Reworked transformed handlerton into class StorageEngine.
276
  *engine= binlog_engine;
277
278
  return 0;
1 by brian
clean slate
279
}
280
813.2.1 by Toru Maesaka
Renamed mysql_declare_plugin to drizzle_declare_plugin
281
drizzle_declare_plugin(binlog)
1 by brian
clean slate
282
{
319.1.1 by Grant Limberg
renamed all instances of MYSQL_ to DRIZZLE_
283
  DRIZZLE_STORAGE_ENGINE_PLUGIN,
1 by brian
clean slate
284
  "binlog",
177.4.3 by mark
ripped out more plugin ABI and API version checking, and plugin versions are now strings
285
  "1.0",
1 by brian
clean slate
286
  "MySQL AB",
287
  "This is a pseudo storage engine to represent the binlog in a transaction",
288
  PLUGIN_LICENSE_GPL,
289
  binlog_init, /* Plugin Init */
290
  NULL, /* Plugin Deinit */
291
  NULL,                       /* status variables                */
292
  NULL,                       /* system variables                */
293
  NULL                        /* config options                  */
294
}
813.2.2 by Toru Maesaka
Renamed mysql_declare_plugin_end to drizzle_declare_plugin_end
295
drizzle_declare_plugin_end;