~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

[patch 112/129] Merge patch for revision 1925 from InnoDB SVN:
revno: 1925
revision-id: svn-v4:16c675df-0fcb-4bc9-8058-dcc011a37293:branches/zip:6169
parent: svn-v4:16c675df-0fcb-4bc9-8058-dcc011a37293:branches/zip:6163
committer: calvin
timestamp: Thu 2009-11-12 12:40:43 +0000
message:
  branches/zip: add test case for bug#46676
  
  This crash is reproducible with InnoDB plugin 1.0.4 + MySQL 5.1.37.
  But no longer reproducible after MySQL 5.1.38 (with plugin 1.0.5).
  Add test case to catch future regression.
added:
  mysql-test/innodb_bug46676.result 6169@16c675df-0fcb-4bc9-8058-dcc011a37293:branches%2Fzip%2Fmysql-test%2Finnodb_bug46676.result
  mysql-test/innodb_bug46676.test 6169@16c675df-0fcb-4bc9-8058-dcc011a37293:branches%2Fzip%2Fmysql-test%2Finnodb_bug46676.test
diff:
=== added file 'mysql-test/innodb_bug46676.result'

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include "config.h"
21
 
 
22
 
#include <boost/scoped_array.hpp>
23
 
 
24
21
#include <drizzled/plugin/logging.h>
25
22
#include <drizzled/gettext.h>
26
23
#include <drizzled/session.h>
27
 
#include <boost/date_time.hpp>
28
24
#include <boost/program_options.hpp>
29
25
#include <drizzled/module/option_map.h>
30
26
#include <libgearman/gearman.h>
31
27
#include <limits.h>
 
28
#include <sys/time.h>
32
29
#include <sys/types.h>
33
30
#include <sys/stat.h>
34
31
#include <fcntl.h>
35
32
#include <cstdio>
36
33
#include <cerrno>
37
 
#include <memory>
38
 
 
39
 
 
40
 
namespace drizzle_plugin
41
 
{
42
 
 
 
34
 
 
35
using namespace drizzled;
43
36
namespace po= boost::program_options;
44
37
 
45
38
/* TODO make this dynamic as needed */
46
39
static const int MAX_MSG_LEN= 32*1024;
47
40
 
 
41
static bool sysvar_logging_gearman_enable;
 
42
static char* sysvar_logging_gearman_host= NULL;
 
43
static char* sysvar_logging_gearman_function= NULL;
 
44
 
 
45
 
 
46
/* stolen from mysys/my_getsystime
 
47
   until the Session has a good utime "now" we can use
 
48
   will have to use this instead */
 
49
 
 
50
static uint64_t get_microtime()
 
51
{
 
52
#if defined(HAVE_GETHRTIME)
 
53
  return gethrtime()/1000;
 
54
#else
 
55
  uint64_t newtime;
 
56
  struct timeval t;
 
57
  /*
 
58
    The following loop is here because gettimeofday may fail on some systems
 
59
  */
 
60
  while (gettimeofday(&t, NULL) != 0) {}
 
61
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
 
62
  return newtime;
 
63
#endif  /* defined(HAVE_GETHRTIME) */
 
64
}
 
65
 
48
66
/* quote a string to be safe to include in a CSV line
49
67
   that means backslash quoting all commas, doublequotes, backslashes,
50
68
   and all the ASCII unprintable characters
153
171
  return dst;
154
172
}
155
173
 
156
 
class LoggingGearman :
157
 
  public drizzled::plugin::Logging
 
174
class LoggingGearman : public plugin::Logging
158
175
{
159
176
 
160
 
  const std::string _host;
161
 
  const std::string _function;
162
 
 
163
 
  int _gearman_client_ok;
164
 
  gearman_client_st _gearman_client;
165
 
 
166
 
  LoggingGearman();
167
 
  LoggingGearman(const LoggingGearman&);
 
177
  int gearman_client_ok;
 
178
  gearman_client_st gearman_client;
168
179
 
169
180
public:
170
181
 
171
 
  LoggingGearman(const std::string &host,
172
 
                 const std::string &function) :
173
 
    drizzled::plugin::Logging("LoggingGearman"),
174
 
    _host(host),
175
 
    _function(function),
176
 
    _gearman_client_ok(0),
177
 
    _gearman_client()
 
182
  LoggingGearman()
 
183
    : plugin::Logging("LoggingGearman"),
 
184
      gearman_client_ok(0)
178
185
  {
179
186
    gearman_return_t ret;
180
187
 
181
 
 
182
 
    if (gearman_client_create(&_gearman_client) == NULL)
 
188
    if (sysvar_logging_gearman_enable == false)
 
189
      return;
 
190
 
 
191
    if (sysvar_logging_gearman_host == NULL)
 
192
      return;
 
193
 
 
194
 
 
195
    if (gearman_client_create(&gearman_client) == NULL)
183
196
    {
184
197
      char errmsg[STRERROR_MAX];
185
198
      strerror_r(errno, errmsg, sizeof(errmsg));
186
 
      drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
187
 
                              errmsg);
 
199
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
 
200
                    errmsg);
188
201
      return;
189
202
    }
190
203
 
191
204
    /* TODO, be able to override the port */
192
205
    /* TODO, be able send to multiple servers */
193
 
    ret= gearman_client_add_server(&_gearman_client,
194
 
                                   host.c_str(), 0);
 
206
    ret= gearman_client_add_server(&gearman_client,
 
207
                                   sysvar_logging_gearman_host, 0);
195
208
    if (ret != GEARMAN_SUCCESS)
196
209
    {
197
 
      drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
198
 
                              gearman_client_error(&_gearman_client));
 
210
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
 
211
                    gearman_client_error(&gearman_client));
199
212
      return;
200
213
    }
201
214
 
202
 
    _gearman_client_ok= 1;
 
215
    gearman_client_ok= 1;
203
216
 
204
217
  }
205
218
 
206
219
  ~LoggingGearman()
207
220
  {
208
 
    if (_gearman_client_ok)
 
221
    if (gearman_client_ok)
209
222
    {
210
 
      gearman_client_free(&_gearman_client);
 
223
      gearman_client_free(&gearman_client);
211
224
    }
212
225
  }
213
226
 
214
 
  virtual bool post(drizzled::Session *session)
 
227
  virtual bool post(Session *session)
215
228
  {
216
 
    boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
 
229
    char msgbuf[MAX_MSG_LEN];
217
230
    int msgbuf_len= 0;
218
231
  
219
232
    assert(session != NULL);
222
235
       but that crashes the server, so for now, we just lie a little bit
223
236
    */
224
237
 
225
 
    if (not _gearman_client_ok)
 
238
    if (!gearman_client_ok)
226
239
        return false;
227
240
  
228
241
    /* TODO, the session object should have a "utime command completed"
229
242
       inside itself, so be more accurate, and so this doesnt have to
230
243
       keep calling current_utime, which can be slow */
231
244
  
232
 
    boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
233
 
    boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
234
 
    uint64_t t_mark= (mytime-epoch).total_microseconds();
 
245
    uint64_t t_mark= get_microtime();
235
246
  
236
 
 
237
247
    // buffer to quotify the query
238
248
    unsigned char qs[255];
239
249
  
240
250
    // to avoid trying to printf %s something that is potentially NULL
241
 
    drizzled::util::string::const_shared_ptr dbs(session->schema());
 
251
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
242
252
  
243
253
    msgbuf_len=
244
 
      snprintf(msgbuf.get(), MAX_MSG_LEN,
 
254
      snprintf(msgbuf, MAX_MSG_LEN,
245
255
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
246
256
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
247
257
               "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
249
259
               session->thread_id,
250
260
               session->getQueryId(),
251
261
               // dont need to quote the db name, always CSV safe
252
 
               (int)dbs->size(), dbs->c_str(),
 
262
               (int)session->db.length(), dbs,
253
263
               // do need to quote the query
254
 
               quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
 
264
               quotify((const unsigned char *)session->getQueryString().c_str(),
 
265
                       session->getQueryLength(), qs, sizeof(qs)),
255
266
               // command_name is defined in drizzled/sql_parse.cc
256
267
               // dont need to quote the command name, always CSV safe
257
 
               (int)drizzled::command_name[session->command].length,
258
 
               drizzled::command_name[session->command].str,
 
268
               (int)command_name[session->command].length,
 
269
               command_name[session->command].str,
259
270
               // counters are at end, to make it easier to add more
260
271
               (t_mark - session->getConnectMicroseconds()),
261
272
               (t_mark - session->start_utime),
265
276
               session->tmp_table,
266
277
               session->total_warn_count,
267
278
               session->getServerId(),
268
 
               drizzled::glob_hostname
 
279
               glob_hostname
269
280
               );
270
281
  
271
282
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
272
283
  
273
 
    (void) gearman_client_do_background(&_gearman_client,
274
 
                                        _function.c_str(),
 
284
    (void) gearman_client_do_background(&gearman_client,
 
285
                                        sysvar_logging_gearman_function,
275
286
                                        NULL,
276
 
                                        (void *) msgbuf.get(),
 
287
                                        (void *) msgbuf,
277
288
                                        (size_t) msgbuf_len,
278
289
                                        job_handle);
279
290
  
283
294
 
284
295
static LoggingGearman *handler= NULL;
285
296
 
286
 
static int logging_gearman_plugin_init(drizzled::module::Context &context)
 
297
static int logging_gearman_plugin_init(module::Context &context)
287
298
{
288
 
  const drizzled::module::option_map &vm= context.getOptions();
289
 
 
290
 
  handler= new LoggingGearman(vm["host"].as<std::string>(),
291
 
                              vm["function"].as<std::string>());
 
299
  handler= new LoggingGearman();
292
300
  context.add(handler);
293
 
  context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
294
 
  context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
295
301
 
296
302
  return 0;
297
303
}
298
304
 
299
305
static void init_options(drizzled::module::option_context &context)
300
306
{
301
 
  context("host",
302
 
          po::value<std::string>()->default_value("localhost"),
303
 
          N_("Hostname for logging to a Gearman server"));
304
 
  context("function",
305
 
          po::value<std::string>()->default_value("drizzlelog"),
306
 
          N_("Gearman Function to send logging to"));
 
307
  context("enable",
 
308
          po::value<bool>(&sysvar_logging_gearman_enable)->default_value(false)->zero_tokens(),
 
309
          N_("Enable logging to a gearman server"));
307
310
}
308
311
 
309
 
} /* namespace drizzle_plugin */
 
312
static DRIZZLE_SYSVAR_BOOL(
 
313
                           enable,
 
314
                           sysvar_logging_gearman_enable,
 
315
                           PLUGIN_VAR_NOCMDARG,
 
316
                           N_("Enable logging to a gearman server"),
 
317
                           NULL, /* check func */
 
318
                           NULL, /* update func */
 
319
                           false /* default */);
 
320
 
 
321
static DRIZZLE_SYSVAR_STR(
 
322
                          host,
 
323
                          sysvar_logging_gearman_host,
 
324
                          PLUGIN_VAR_READONLY,
 
325
                          N_("Hostname for logging to a Gearman server"),
 
326
                          NULL, /* check func */
 
327
                          NULL, /* update func*/
 
328
                          "localhost" /* default */);
 
329
 
 
330
static DRIZZLE_SYSVAR_STR(
 
331
                          function,
 
332
                          sysvar_logging_gearman_function,
 
333
                          PLUGIN_VAR_READONLY,
 
334
                          N_("Gearman Function to send logging to"),
 
335
                          NULL, /* check func */
 
336
                          NULL, /* update func*/
 
337
                          "drizzlelog" /* default */);
 
338
 
 
339
static drizzle_sys_var* logging_gearman_system_variables[]= {
 
340
  DRIZZLE_SYSVAR(enable),
 
341
  DRIZZLE_SYSVAR(host),
 
342
  DRIZZLE_SYSVAR(function),
 
343
  NULL
 
344
};
310
345
 
311
346
DRIZZLE_DECLARE_PLUGIN
312
347
{
315
350
    "0.1",
316
351
    "Mark Atwood <mark@fallenpegasus.com>",
317
352
    N_("Log queries to a Gearman server"),
318
 
    drizzled::PLUGIN_LICENSE_GPL,
319
 
    drizzle_plugin::logging_gearman_plugin_init,
320
 
    NULL,
321
 
    drizzle_plugin::init_options
 
353
    PLUGIN_LICENSE_GPL,
 
354
    logging_gearman_plugin_init,
 
355
    logging_gearman_system_variables,
 
356
    init_options
322
357
}
323
358
DRIZZLE_DECLARE_PLUGIN_END;