~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Prafulla Tekawade
  • Date: 2010-07-18 03:36:32 UTC
  • mto: (1662.1.4 rollup)
  • mto: This revision was merged to the branch mainline in revision 1664.
  • Revision ID: prafulla_t@users.sourceforge.net-20100718033632-p7q6qtgliqbhe38p
Fix for Bug 592444

There were two problems:
o. In greedy_search optimizer method, best_extension_by_limited search
   maintains join embedding(nestedness) of tables added so far, so that 
   correct(valid)  join order is selected
   These are requirements from nested outer join executioner.
   The problem was, embedding_map was not correctly updated when a table 
   is added to optimal plan outside best_extension_by_limited search, 
   by greedy_search method. We need to update join->cur_embedding_map
   correctly here so that execution plan for other tables get
   generated.
   Invoked checked_interleaving_with_nj from greedy_search on the
   best_table selected. Fixed its prototype to take only one JoinTab
   This is same as mysql 5.1 source tree.
o. The other problem was, join->cur_embedding_map was not restored correctly
   when a table is added to the optimal plan to reflect the current embedding 
   map. 
   Taken good documented method restore_prev_nj_state which restores 
   cur_embedding_map from mysql 5.1 source tree and modified it for drizzled 
   code.

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