hatoku_hton.cc 71.4 KB
Newer Older
1
/* -*- mode: C; c-basic-offset: 4 -*- */
Zardosht Kasheff's avatar
Zardosht Kasheff committed
2
#define MYSQL_SERVER 1
3
#include "toku_mysql_priv.h"
Zardosht Kasheff's avatar
Zardosht Kasheff committed
4
#include <db.h>
Zardosht Kasheff's avatar
Zardosht Kasheff committed
5 6 7 8 9 10

extern "C" {
#include "stdint.h"
#if defined(_WIN32)
#include "misc.h"
#endif
Barry Perlman's avatar
Barry Perlman committed
11
#define __STDC_FORMAT_MACROS
Zardosht Kasheff's avatar
Zardosht Kasheff committed
12
#include <inttypes.h>
Zardosht Kasheff's avatar
Zardosht Kasheff committed
13
#include "toku_os.h"
14
#include "toku_time.h"
Zardosht Kasheff's avatar
Zardosht Kasheff committed
15 16
}

Zardosht Kasheff's avatar
Zardosht Kasheff committed
17 18 19 20 21 22 23 24 25 26
/* We define DTRACE after mysql_priv.h in case it disabled dtrace in the main server */
#ifdef HAVE_DTRACE
#define _DTRACE_VERSION 1
#else
#endif

#include <mysql/plugin.h>
#include "hatoku_hton.h"
#include "hatoku_defines.h"
#include "ha_tokudb.h"
27
#include "hatoku_assert.h"
Zardosht Kasheff's avatar
Zardosht Kasheff committed
28 29 30 31 32 33

#undef PACKAGE
#undef VERSION
#undef HAVE_DTRACE
#undef _DTRACE_VERSION

34
#define TOKU_METADB_NAME "tokudb_meta"
Zardosht Kasheff's avatar
Zardosht Kasheff committed
35

36 37 38 39 40 41 42
typedef struct savepoint_info {
    DB_TXN* txn;
    tokudb_trx_data* trx;
    bool in_sub_stmt;
} *SP_INFO, SP_INFO_T;


Zardosht Kasheff's avatar
Zardosht Kasheff committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
static inline void *thd_data_get(THD *thd, int slot) {
    return thd->ha_data[slot].ha_ptr;
}

static inline void thd_data_set(THD *thd, int slot, void *data) {
    thd->ha_data[slot].ha_ptr = data;
}

static uchar *tokudb_get_key(TOKUDB_SHARE * share, size_t * length, my_bool not_used __attribute__ ((unused))) {
    *length = share->table_name_length;
    return (uchar *) share->table_name;
}

static handler *tokudb_create_handler(handlerton * hton, TABLE_SHARE * table, MEM_ROOT * mem_root);
57 58 59 60 61 62 63 64
static MYSQL_THDVAR_BOOL(commit_sync, 
    PLUGIN_VAR_THDLOCAL, 
    "sync on txn commit",
    /* check */ NULL, 
    /* update */ NULL,
    /* default*/ TRUE
    );

65 66 67 68 69 70 71 72 73 74
static MYSQL_THDVAR_UINT(pk_insert_mode,
  0,
  "set the primary key insert mode",
  NULL, 
  NULL, 
  1, // default
  0, // min?
  2, // max
  1 // blocksize
  );
75 76 77 78 79 80 81
static MYSQL_THDVAR_BOOL(load_save_space,
  0,
  "if on, intial loads are slower but take less space",
  NULL, 
  NULL, 
  FALSE
  );
82 83 84 85 86 87 88
static MYSQL_THDVAR_BOOL(disable_slow_alter,
  0,
  "if on, alter tables that require copy are disabled",
  NULL, 
  NULL, 
  FALSE
  );
89 90 91 92 93 94 95
static MYSQL_THDVAR_BOOL(create_index_online,
  0,
  "if on, create index done online",
  NULL, 
  NULL, 
  FALSE
  );
96 97 98 99 100 101 102
static MYSQL_THDVAR_BOOL(disable_prefetching,
  0,
  "if on, prefetching disabled",
  NULL, 
  NULL, 
  FALSE
  );
103 104 105 106 107
static MYSQL_THDVAR_BOOL(prelock_empty,
  0,
  "Tokudb Prelock Empty Table",
  NULL, 
  NULL, 
108
  TRUE
109
  );
110 111 112 113 114 115 116 117 118 119
static MYSQL_THDVAR_UINT(block_size,
  0,
  "fractal tree block size",
  NULL, 
  NULL, 
  4<<20, // default
  4096,  // min
  ~0L,   // max
  1      // blocksize???
  );
120 121 122 123 124 125 126 127 128 129
static MYSQL_THDVAR_UINT(read_block_size,
  0,
  "fractal tree read block size",
  NULL, 
  NULL, 
  128*1024, // default
  4096,  // min
  ~0L,   // max
  1      // blocksize???
  );
130 131
static MYSQL_THDVAR_UINT(read_buf_size,
  0,
132
  "fractal tree read block size", //TODO: Is this a typo?
133 134 135
  NULL, 
  NULL, 
  128*1024, // default
136
  0,  // min
137 138 139 140
  1*1024*1024,   // max
  1      // blocksize???
  );

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
void tokudb_checkpoint_lock(THD * thd);
void tokudb_checkpoint_unlock(THD * thd);

static
void
tokudb_checkpoint_lock_update(
    THD* thd,
    struct st_mysql_sys_var* var,
    void* var_ptr,
    const void* save) 
{
    my_bool* val = (my_bool *) var_ptr;
    *val= *(my_bool *) save ? TRUE : FALSE;
    if (*val) {
        tokudb_checkpoint_lock(thd);
    }
    else {
        tokudb_checkpoint_unlock(thd);
    }
}
  
static MYSQL_THDVAR_BOOL(checkpoint_lock,
  0,
  "Tokudb Checkpoint Lock",
  NULL, 
  tokudb_checkpoint_lock_update, 
  FALSE
  );

Zardosht Kasheff's avatar
Zardosht Kasheff committed
170 171 172 173 174 175 176 177
static void tokudb_print_error(const DB_ENV * db_env, const char *db_errpfx, const char *buffer);
static void tokudb_cleanup_log_files(void);
static int tokudb_end(handlerton * hton, ha_panic_function type);
static bool tokudb_flush_logs(handlerton * hton);
static bool tokudb_show_status(handlerton * hton, THD * thd, stat_print_fn * print, enum ha_stat_type);
static int tokudb_close_connection(handlerton * hton, THD * thd);
static int tokudb_commit(handlerton * hton, THD * thd, bool all);
static int tokudb_rollback(handlerton * hton, THD * thd, bool all);
178
#if defined(HA_GENERAL_ONLINE)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
179
static uint tokudb_alter_table_flags(uint flags);
180
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
181 182 183
static int tokudb_rollback_to_savepoint(handlerton * hton, THD * thd, void *savepoint);
static int tokudb_savepoint(handlerton * hton, THD * thd, void *savepoint);
static int tokudb_release_savepoint(handlerton * hton, THD * thd, void *savepoint);
184 185 186 187
static int tokudb_discover(handlerton *hton, THD* thd, const char *db, 
                        const char *name,
                        uchar **frmblob, 
                        size_t *frmlen);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
188 189 190 191 192 193
handlerton *tokudb_hton;

const char *ha_tokudb_ext = ".tokudb";
char *tokudb_data_dir;
ulong tokudb_debug;
DB_ENV *db_env;
194
DB* metadata_db;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
195 196
HASH tokudb_open_tables;
pthread_mutex_t tokudb_mutex;
197
pthread_mutex_t tokudb_meta_mutex;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
198

199
static ulonglong tokudb_lock_timeout;
200 201
static ulong tokudb_cleaner_period;
static ulong tokudb_cleaner_iterations;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
202

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
#define ASSERT_MSGLEN 1024

void toku_hton_assert_fail(const char* expr_as_string, const char * fun, const char * file, int line, int caller_errno) {
    char msg[ASSERT_MSGLEN];
    if (db_env) {
	snprintf(msg, ASSERT_MSGLEN, "Handlerton: %s ", expr_as_string);
	db_env->crash(db_env, msg, fun, file, line,caller_errno);
    }
    else {
	snprintf(msg, ASSERT_MSGLEN, "Handlerton assertion failed, no env, %s, %d, %s, %s (errno=%d)\n", file, line, fun, expr_as_string, caller_errno);
	perror(msg);
	fflush(stderr);
    }
    abort();
}




Zardosht Kasheff's avatar
Zardosht Kasheff committed
222 223 224 225 226 227
//my_bool tokudb_shared_data = FALSE;
static u_int32_t tokudb_init_flags = 
    DB_CREATE | DB_THREAD | DB_PRIVATE | 
    DB_INIT_LOCK | 
    DB_INIT_MPOOL |
    DB_INIT_TXN | 
228
    DB_INIT_LOG |
Zardosht Kasheff's avatar
Zardosht Kasheff committed
229
    DB_RECOVER;
230
static u_int32_t tokudb_env_flags = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
231 232 233 234
// static u_int32_t tokudb_lock_type = DB_LOCK_DEFAULT;
// static ulong tokudb_log_buffer_size = 0;
// static ulong tokudb_log_file_size = 0;
static ulonglong tokudb_cache_size = 0;
235
static ulonglong tokudb_max_lock_memory = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
236
static char *tokudb_home;
237
static char *tokudb_tmp_dir;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
238 239 240 241 242 243
static char *tokudb_log_dir;
// static long tokudb_lock_scan_time = 0;
// static ulong tokudb_region_size = 0;
// static ulong tokudb_cache_parts = 1;
static const char tokudb_hton_name[] = "TokuDB";
static const int tokudb_hton_name_length = sizeof(tokudb_hton_name) - 1;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
244
static u_int32_t tokudb_checkpointing_period;
245 246
u_int32_t tokudb_write_status_frequency;
u_int32_t tokudb_read_status_frequency;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
247
#ifdef TOKUDB_VERSION
248
char *tokudb_version = (char*) TOKUDB_VERSION;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
249
#else
250
char *tokudb_version;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
251
#endif
252
static int tokudb_fs_reserve_percent;  // file system reserve as a percentage of total disk space
Zardosht Kasheff's avatar
Zardosht Kasheff committed
253

254
#if defined(_WIN32)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
255 256 257
extern "C" {
#include "ydb.h"
}
258
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
// A flag set if the handlerton is in an initialized, usable state,
// plus a reader-write lock to protect it without serializing reads.
// Since we don't have static initializers for the opaque rwlock type,
// use constructor and destructor functions to create and destroy
// the lock before and after main(), respectively.
static int tokudb_hton_initialized;
static rw_lock_t tokudb_hton_initialized_lock;

static void create_tokudb_hton_intialized_lock(void)  __attribute__((constructor));
static void destroy_tokudb_hton_initialized_lock(void) __attribute__((destructor));

static void create_tokudb_hton_intialized_lock(void)
{
    my_rwlock_init(&tokudb_hton_initialized_lock, 0);
}

static void destroy_tokudb_hton_initialized_lock(void)
{
    rwlock_destroy(&tokudb_hton_initialized_lock);
}
280

Zardosht Kasheff's avatar
Zardosht Kasheff committed
281 282
static int tokudb_init_func(void *p) {
    TOKUDB_DBUG_ENTER("tokudb_init_func");
Zardosht Kasheff's avatar
Zardosht Kasheff committed
283
    int r;
284 285 286 287 288 289 290
#if defined(_WIN64)
        r = toku_ydb_init();
        if (r) {
            printf("got error %d\n", r);
            goto error;
        }
#endif
291 292 293 294 295

    // 3938: lock the handlerton's initialized status flag for writing
    r = rw_wrlock(&tokudb_hton_initialized_lock);
    assert(r == 0);

296 297
    db_env = NULL;
    metadata_db = NULL;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
298 299 300

    tokudb_hton = (handlerton *) p;

301 302
    pthread_mutex_init(&tokudb_mutex, MY_MUTEX_INIT_FAST);
    pthread_mutex_init(&tokudb_meta_mutex, MY_MUTEX_INIT_FAST);
303
    (void) my_hash_init(&tokudb_open_tables, table_alias_charset, 32, 0, 0, (my_hash_get_key) tokudb_get_key, 0, 0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
304 305 306

    tokudb_hton->state = SHOW_OPTION_YES;
    // tokudb_hton->flags= HTON_CAN_RECREATE;  // QQQ this came from skeleton
307
    tokudb_hton->flags = HTON_CLOSE_CURSORS_AT_COMMIT;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
308 309 310 311 312 313 314 315
#ifdef DB_TYPE_TOKUDB
    tokudb_hton->db_type = DB_TYPE_TOKUDB;
#else
    tokudb_hton->db_type = DB_TYPE_UNKNOWN;
#endif

    tokudb_hton->create = tokudb_create_handler;
    tokudb_hton->close_connection = tokudb_close_connection;
316 317

    tokudb_hton->savepoint_offset = sizeof(SP_INFO_T);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
318 319 320
    tokudb_hton->savepoint_set = tokudb_savepoint;
    tokudb_hton->savepoint_rollback = tokudb_rollback_to_savepoint;
    tokudb_hton->savepoint_release = tokudb_release_savepoint;
321

322 323
    tokudb_hton->discover = tokudb_discover;
    
Zardosht Kasheff's avatar
Zardosht Kasheff committed
324 325 326 327 328
    tokudb_hton->commit = tokudb_commit;
    tokudb_hton->rollback = tokudb_rollback;
    tokudb_hton->panic = tokudb_end;
    tokudb_hton->flush_logs = tokudb_flush_logs;
    tokudb_hton->show_status = tokudb_show_status;
329
#if defined(HA_GENERAL_ONLINE)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
330
    tokudb_hton->alter_table_flags = tokudb_alter_table_flags;
331
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    if (!tokudb_home)
        tokudb_home = mysql_real_data_home;
    DBUG_PRINT("info", ("tokudb_home: %s", tokudb_home));
#if 0
    if (!tokudb_log_buffer_size) { // QQQ
        tokudb_log_buffer_size = max(table_cache_size * 512, 32 * 1024);
        DBUG_PRINT("info", ("computing tokudb_log_buffer_size %ld\n", tokudb_log_buffer_size));
    }
    tokudb_log_file_size = tokudb_log_buffer_size * 4;
    tokudb_log_file_size = MY_ALIGN(tokudb_log_file_size, 1024 * 1024L);
    tokudb_log_file_size = max(tokudb_log_file_size, 10 * 1024 * 1024L);
    DBUG_PRINT("info", ("computing tokudb_log_file_size: %ld\n", tokudb_log_file_size));
#endif
    if ((r = db_env_create(&db_env, 0))) {
        DBUG_PRINT("info", ("db_env_create %d\n", r));
        goto error;
    }

    DBUG_PRINT("info", ("tokudb_env_flags: 0x%x\n", tokudb_env_flags));
    r = db_env->set_flags(db_env, tokudb_env_flags, 1);
    if (r) { // QQQ
        if (tokudb_debug & TOKUDB_DEBUG_INIT) 
            TOKUDB_TRACE("%s:WARNING: flags=%x r=%d\n", __FUNCTION__, tokudb_env_flags, r); 
        // goto error;
    }

    // config error handling
    db_env->set_errcall(db_env, tokudb_print_error);
    db_env->set_errpfx(db_env, "TokuDB");

362 363 364 365 366 367 368 369 370
    //
    // set default comparison functions
    //
    r = db_env->set_default_bt_compare(db_env, tokudb_cmp_dbt_key);
    if (r) {
        DBUG_PRINT("info", ("set_default_bt_compare%d\n", r));
        goto error; 
    }

Zardosht Kasheff's avatar
Zardosht Kasheff committed
371
    {
372
    char *tmp_dir = tokudb_tmp_dir;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
373
    char *data_dir = tokudb_data_dir;
374
    if (data_dir == 0) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
375
        data_dir = mysql_data_home;
376
    }
377 378
    if (tmp_dir == 0) {
        tmp_dir = data_dir;
379
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
380 381
    DBUG_PRINT("info", ("tokudb_data_dir: %s\n", data_dir));
    db_env->set_data_dir(db_env, data_dir);
382

383 384
    DBUG_PRINT("info", ("tokudb_tmp_dir: %s\n", tmp_dir));
    db_env->set_tmp_dir(db_env, tmp_dir);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
385 386 387 388 389 390 391
    }

    if (tokudb_log_dir) {
        DBUG_PRINT("info", ("tokudb_log_dir: %s\n", tokudb_log_dir));
        db_env->set_lg_dir(db_env, tokudb_log_dir);
    }

392
    // config the cache table size to min(1/2 of physical memory, 1/8 of the process address space)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
393
    if (tokudb_cache_size == 0) {
394 395 396 397 398 399 400 401
        uint64_t physmem, maxdata;
        physmem = toku_os_get_phys_memory_size();
        tokudb_cache_size = physmem / 2;
        r = toku_os_get_max_process_data_size(&maxdata);
        if (r == 0) {
            if (tokudb_cache_size > maxdata / 8)
                tokudb_cache_size = maxdata / 8;
        }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
402 403 404 405 406 407 408 409 410
    }
    if (tokudb_cache_size) {
        DBUG_PRINT("info", ("tokudb_cache_size: %lld\n", tokudb_cache_size));
        r = db_env->set_cachesize(db_env, (u_int32_t)(tokudb_cache_size >> 30), (u_int32_t)(tokudb_cache_size % (1024L * 1024L * 1024L)), 1);
        if (r) {
            DBUG_PRINT("info", ("set_cachesize %d\n", r));
            goto error; 
        }
    }
411 412 413 414 415 416 417 418 419 420 421 422
    if (tokudb_max_lock_memory == 0) {
        tokudb_max_lock_memory = tokudb_cache_size/8;
    }
    if (tokudb_max_lock_memory) {
        DBUG_PRINT("info", ("tokudb_max_lock_memory: %lld\n", tokudb_max_lock_memory));
        r = db_env->set_lk_max_memory(db_env, tokudb_max_lock_memory);
        if (r) {
            DBUG_PRINT("info", ("set_lk_max_memory %d\n", r));
            goto error; 
        }
    }
    
Zardosht Kasheff's avatar
Zardosht Kasheff committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    u_int32_t gbytes, bytes; int parts;
    r = db_env->get_cachesize(db_env, &gbytes, &bytes, &parts);
    if (r == 0) 
        if (tokudb_debug & TOKUDB_DEBUG_INIT) 
            TOKUDB_TRACE("%s:tokudb_cache_size=%lld\n", __FUNCTION__, ((unsigned long long) gbytes << 30) + bytes);

#if 0
    // QQQ config the logs
    DBUG_PRINT("info", ("tokudb_log_file_size: %ld\n", tokudb_log_file_size));
    db_env->set_lg_max(db_env, tokudb_log_file_size);
    DBUG_PRINT("info", ("tokudb_log_buffer_size: %ld\n", tokudb_log_buffer_size));
    db_env->set_lg_bsize(db_env, tokudb_log_buffer_size);
    // DBUG_PRINT("info",("tokudb_region_size: %ld\n", tokudb_region_size));
    // db_env->set_lg_regionmax(db_env, tokudb_region_size);
#endif

    // config the locks
#if 0 // QQQ no lock types yet
    DBUG_PRINT("info", ("tokudb_lock_type: 0x%lx\n", tokudb_lock_type));
    db_env->set_lk_detect(db_env, tokudb_lock_type);
#endif
444 445 446 447
    r = db_env->set_lk_max_locks(db_env, 0xffffffff);
    if (r) {
        DBUG_PRINT("info", ("tokudb_set_max_locks %d\n", r));
        goto error;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
448 449
    }

450 451 452 453 454 455
    if (db_env->set_redzone) {
        r = db_env->set_redzone(db_env, tokudb_fs_reserve_percent);
        if (r && (tokudb_debug & TOKUDB_DEBUG_INIT))
            TOKUDB_TRACE("%s:%d r=%d\n", __FUNCTION__, __LINE__, r);
    }

Zardosht Kasheff's avatar
Zardosht Kasheff committed
456 457
    if (tokudb_debug & TOKUDB_DEBUG_INIT) TOKUDB_TRACE("%s:env open:flags=%x\n", __FUNCTION__, tokudb_init_flags);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
458
    r = db_env->set_generate_row_callback_for_put(db_env,generate_row_for_put);
459
    assert(!r);
460 461
    r = db_env->set_generate_row_callback_for_del(db_env,generate_row_for_del);
    assert(!r);
462
#if defined(HA_GENERAL_ONLINE)
463
    db_env->set_update(db_env, tokudb_update_fun);
464
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
465
    r = db_env->open(db_env, tokudb_home, tokudb_init_flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
466 467 468 469 470 471 472 473

    if (tokudb_debug & TOKUDB_DEBUG_INIT) TOKUDB_TRACE("%s:env opened:return=%d\n", __FUNCTION__, r);

    if (r) {
        DBUG_PRINT("info", ("env->open %d\n", r));
        goto error;
    }

Zardosht Kasheff's avatar
Zardosht Kasheff committed
474 475
    r = db_env->checkpointing_set_period(db_env, tokudb_checkpointing_period);
    assert(!r);
476 477 478 479
    r = db_env->cleaner_set_period(db_env, tokudb_cleaner_period);
    assert(r == 0);
    r = db_env->cleaner_set_iterations(db_env, tokudb_cleaner_iterations);
    assert(r == 0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
480

481
    r = db_env->set_lock_timeout(db_env, tokudb_lock_timeout);
482 483
    assert(r == 0);

484 485 486 487 488 489 490
    r = db_create(&metadata_db, db_env, 0);
    if (r) {
        DBUG_PRINT("info", ("failed to create metadata db %d\n", r));
        goto error;
    }
    

491
    r= metadata_db->open(metadata_db, NULL, TOKU_METADB_NAME, NULL, DB_BTREE, DB_THREAD, 0);
492
    if (r) {
493 494 495 496 497 498
        if (r != ENOENT) {
            sql_print_error("Got error %d when trying to open metadata_db", r);
            goto error;
        }
        r = metadata_db->close(metadata_db,0);
        assert(r == 0);
499 500 501 502 503
        r = db_create(&metadata_db, db_env, 0);
        if (r) {
            DBUG_PRINT("info", ("failed to create metadata db %d\n", r));
            goto error;
        }
504 505

        r= metadata_db->open(metadata_db, NULL, TOKU_METADB_NAME, NULL, DB_BTREE, DB_THREAD | DB_CREATE | DB_EXCL, my_umask);
506 507 508 509 510
        if (r) {
            goto error;
        }
    }

511 512 513
    //3938: succeeded, set the init status flag and unlock
    tokudb_hton_initialized = 1;
    rw_unlock(&tokudb_hton_initialized_lock);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
514 515 516
    DBUG_RETURN(FALSE);

error:
517
    if (metadata_db) {
518 519
        int rr = metadata_db->close(metadata_db, 0);
        assert(rr==0);
520
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
521
    if (db_env) {
522 523
        int rr= db_env->close(db_env, 0);
        assert(rr==0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
524 525
        db_env = 0;
    }
526 527 528 529

    // 3938: failed to initialized, drop the flag and lock
    tokudb_hton_initialized = 0;
    rw_unlock(&tokudb_hton_initialized_lock);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
530 531 532 533 534 535 536 537 538
    DBUG_RETURN(TRUE);
}

static int tokudb_done_func(void *p) {
    TOKUDB_DBUG_ENTER("tokudb_done_func");
    int error = 0;

    if (tokudb_open_tables.records)
        error = 1;
539
    my_hash_free(&tokudb_open_tables);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
540
    pthread_mutex_destroy(&tokudb_mutex);
541
    pthread_mutex_destroy(&tokudb_meta_mutex);
542 543 544
#if defined(_WIN64)
        toku_ydb_destroy();
#endif
545

Zardosht Kasheff's avatar
Zardosht Kasheff committed
546 547 548 549 550 551 552 553 554 555 556 557
    TOKUDB_DBUG_RETURN(0);
}



static handler *tokudb_create_handler(handlerton * hton, TABLE_SHARE * table, MEM_ROOT * mem_root) {
    return new(mem_root) ha_tokudb(hton, table);
}

int tokudb_end(handlerton * hton, ha_panic_function type) {
    TOKUDB_DBUG_ENTER("tokudb_end");
    int error = 0;
558 559 560 561 562 563 564 565
    
    // 3938: if we finalize the storage engine plugin, it is no longer
    // initialized. grab a writer lock for the duration of the
    // call, so we can drop the flag and destroy the mutexes
    // in isolation.
    rw_wrlock(&tokudb_hton_initialized_lock);
    assert(tokudb_hton_initialized);

566
    if (metadata_db) {
567 568
        int r = metadata_db->close(metadata_db, 0);
        assert(r==0);
569
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
570 571 572 573
    if (db_env) {
        if (tokudb_init_flags & DB_INIT_LOG)
            tokudb_cleanup_log_files();
        error = db_env->close(db_env, 0);       // Error is logged
574
        assert(error==0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
575 576
        db_env = NULL;
    }
577 578 579 580 581

    // 3938: drop the initialized flag and unlock
    tokudb_hton_initialized = 0;
    rw_unlock(&tokudb_hton_initialized_lock);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
582 583 584 585
    TOKUDB_DBUG_RETURN(error);
}

static int tokudb_close_connection(handlerton * hton, THD * thd) {
586 587 588 589 590 591 592 593
    int error = 0;
    tokudb_trx_data* trx = NULL;
    trx = (tokudb_trx_data *) thd_data_get(thd, tokudb_hton->slot);
    if (trx && trx->checkpoint_lock_taken) {
        error = db_env->checkpointing_resume(db_env);
    }
    my_free(trx, MYF(0));
    return error;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
594 595 596 597 598 599
}

bool tokudb_flush_logs(handlerton * hton) {
    TOKUDB_DBUG_ENTER("tokudb_flush_logs");
    int error;
    bool result = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613
    u_int32_t curr_tokudb_checkpointing_period = 0;

    //
    // get the current checkpointing period
    //
    error = db_env->checkpointing_get_period(
        db_env, 
        &curr_tokudb_checkpointing_period
        );
    if (error) {
        my_error(ER_ERROR_DURING_CHECKPOINT, MYF(0), error);
        result = 1;
        goto exit;
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
614

Zardosht Kasheff's avatar
Zardosht Kasheff committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    //
    // if the current period is not the same as the variable, that means
    // the user has changed the period and now we need to update it
    //
    if (tokudb_checkpointing_period != curr_tokudb_checkpointing_period) {
        error = db_env->checkpointing_set_period(
            db_env, 
            tokudb_checkpointing_period
            );
        if (error) {
            my_error(ER_ERROR_DURING_CHECKPOINT, MYF(0), error);
            result = 1;
            goto exit;
        }
    }
    
    //
    // take the checkpoint
    //
Zardosht Kasheff's avatar
Zardosht Kasheff committed
634 635 636 637
    error = db_env->txn_checkpoint(db_env, 0, 0, 0);
    if (error) {
        my_error(ER_ERROR_DURING_CHECKPOINT, MYF(0), error);
        result = 1;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
638
        goto exit;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
639
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
640 641 642

    result = 0;
exit:
Zardosht Kasheff's avatar
Zardosht Kasheff committed
643 644 645
    TOKUDB_DBUG_RETURN(result);
}

646 647 648 649
uint get_pk_insert_mode(THD* thd) {
    return THDVAR(thd, pk_insert_mode);
}

650 651 652 653
bool get_load_save_space(THD* thd) {
    return (THDVAR(thd, load_save_space) != 0);
}

654 655 656 657
bool get_disable_slow_alter(THD* thd) {
    return (THDVAR(thd, disable_slow_alter) != 0);
}

658 659 660 661
bool get_create_index_online(THD* thd) {
    return (THDVAR(thd, create_index_online) != 0);
}

662 663 664 665
bool get_disable_prefetching(THD* thd) {
    return (THDVAR(thd, disable_prefetching) != 0);
}

666 667 668
bool get_prelock_empty(THD* thd) {
    return (THDVAR(thd, prelock_empty) != 0);
}
669

670 671 672 673
uint get_tokudb_block_size(THD* thd) {
    return THDVAR(thd, block_size);
}

674 675 676 677
uint get_tokudb_read_block_size(THD* thd) {
    return THDVAR(thd, read_block_size);
}

678 679 680 681
uint get_tokudb_read_buf_size(THD* thd) {
    return THDVAR(thd, read_buf_size);
}

682 683 684 685 686 687 688 689 690 691
typedef struct txn_progress_info {
    char status[200];
    THD* thd;
} *TXN_PROGRESS_INFO;


void txn_progress_func(TOKU_TXN_PROGRESS progress, void* extra) {
    TXN_PROGRESS_INFO progress_info = (TXN_PROGRESS_INFO)extra;
    int r;
    if (progress->stalled_on_checkpoint) {
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
        if (progress->is_commit) {
            r = sprintf(
                progress_info->status, 
                "Writing committed changes to disk, processing commit of transaction, %"PRId64" out of %"PRId64, 
                progress->entries_processed, 
                progress->entries_total
                ); 
            assert(r >= 0);
        }
        else {
            r = sprintf(
                progress_info->status, 
                "Writing committed changes to disk, processing abort of transaction, %"PRId64" out of %"PRId64, 
                progress->entries_processed, 
                progress->entries_total
                ); 
            assert(r >= 0);
        }
710 711
    }
    else {
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
        if (progress->is_commit) {
            r = sprintf(
                progress_info->status, 
                "processing commit of transaction, %"PRId64" out of %"PRId64, 
                progress->entries_processed, 
                progress->entries_total
                ); 
            assert(r >= 0);
        }
        else {
            r = sprintf(
                progress_info->status, 
                "processing abort of transaction, %"PRId64" out of %"PRId64, 
                progress->entries_processed, 
                progress->entries_total
                ); 
            assert(r >= 0);
        }
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    }
    thd_proc_info(progress_info->thd, progress_info->status);
}


static void commit_txn_with_progress(DB_TXN* txn, u_int32_t flags, THD* thd) {
    int r;
    struct txn_progress_info info;
    info.thd = thd;
    r = txn->commit_with_progress(txn, flags, txn_progress_func, &info);
    if (r != 0) {
        sql_print_error("tried committing transaction %p and got error code %d", txn, r);
    }
    assert(r == 0);
}

static void abort_txn_with_progress(DB_TXN* txn, THD* thd) {
    int r;
    struct txn_progress_info info;
    info.thd = thd;
    r = txn->abort_with_progress(txn, txn_progress_func, &info);
    if (r != 0) {
        sql_print_error("tried aborting transaction %p and got error code %d", txn, r);
    }
    assert(r == 0);
}

Zardosht Kasheff's avatar
Zardosht Kasheff committed
757 758 759 760 761 762 763
static int tokudb_commit(handlerton * hton, THD * thd, bool all) {
    TOKUDB_DBUG_ENTER("tokudb_commit");
    DBUG_PRINT("trans", ("ending transaction %s", all ? "all" : "stmt"));
    u_int32_t syncflag = THDVAR(thd, commit_sync) ? 0 : DB_TXN_NOSYNC;
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
    DB_TXN **txn = all ? &trx->all : &trx->stmt;
    if (*txn) {
764
        if (tokudb_debug & TOKUDB_DEBUG_TXN) {
765
            TOKUDB_TRACE("doing txn commit:%d:%p\n", all, *txn);
766
        }
767
        commit_txn_with_progress(*txn, syncflag, thd);
768
        if (*txn == trx->sp_level) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
769
            trx->sp_level = 0;
770
        }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
771
        *txn = 0;
772
        trx->sub_sp_level = NULL;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
773 774
    } 
    else if (tokudb_debug & TOKUDB_DEBUG_TXN) {
775
        TOKUDB_TRACE("nothing to commit %d\n", all);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
776
    }
777
    reset_stmt_progress(&trx->stmt_progress);
778
    TOKUDB_DBUG_RETURN(0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
779 780 781 782 783 784 785 786
}

static int tokudb_rollback(handlerton * hton, THD * thd, bool all) {
    TOKUDB_DBUG_ENTER("tokudb_rollback");
    DBUG_PRINT("trans", ("aborting transaction %s", all ? "all" : "stmt"));
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
    DB_TXN **txn = all ? &trx->all : &trx->stmt;
    if (*txn) {
787
        if (tokudb_debug & TOKUDB_DEBUG_TXN) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
788
            TOKUDB_TRACE("rollback:%p\n", *txn);
789
        }
790
        abort_txn_with_progress(*txn, thd);
791 792 793 794
        if (*txn == trx->sp_level) {
            trx->sp_level = 0;
        }
        *txn = 0;
795
        trx->sub_sp_level = NULL;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
796 797 798
    } 
    else {
        if (tokudb_debug & TOKUDB_DEBUG_TXN) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
799
            TOKUDB_TRACE("abort0\n");
Zardosht Kasheff's avatar
Zardosht Kasheff committed
800 801
        }
    }
802
    reset_stmt_progress(&trx->stmt_progress);
803
    TOKUDB_DBUG_RETURN(0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
804 805 806 807 808 809
}


static int tokudb_savepoint(handlerton * hton, THD * thd, void *savepoint) {
    TOKUDB_DBUG_ENTER("tokudb_savepoint");
    int error;
810
    SP_INFO save_info = (SP_INFO)savepoint;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
811
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
812 813
    if (thd->in_sub_stmt) {
        assert(trx->stmt);
814
        error = db_env->txn_begin(db_env, trx->sub_sp_level, &(save_info->txn), DB_INHERIT_ISOLATION);
815 816 817 818 819
        if (error) {
            goto cleanup;
        }
        trx->sub_sp_level = save_info->txn;
        save_info->in_sub_stmt = true;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
820
    }
821
    else {
822
        error = db_env->txn_begin(db_env, trx->sp_level, &(save_info->txn), DB_INHERIT_ISOLATION);
823 824 825 826 827 828 829 830 831
        if (error) {
            goto cleanup;
        }
        trx->sp_level = save_info->txn;
        save_info->in_sub_stmt = false;
    }
    save_info->trx = trx;
    error = 0;
cleanup:
Zardosht Kasheff's avatar
Zardosht Kasheff committed
832 833 834 835 836 837
    TOKUDB_DBUG_RETURN(error);
}

static int tokudb_rollback_to_savepoint(handlerton * hton, THD * thd, void *savepoint) {
    TOKUDB_DBUG_ENTER("tokudb_rollback_to_savepoint");
    int error;
838 839 840 841
    SP_INFO save_info = (SP_INFO)savepoint;
    DB_TXN* parent = NULL;
    DB_TXN* txn_to_rollback = save_info->txn;

Zardosht Kasheff's avatar
Zardosht Kasheff committed
842
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
843 844 845 846 847 848 849 850
    parent = txn_to_rollback->parent;
    if (!(error = txn_to_rollback->abort(txn_to_rollback))) {
        if (save_info->in_sub_stmt) {
            trx->sub_sp_level = parent;
        }
        else {
            trx->sp_level = parent;
        }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
851 852 853 854 855 856 857 858
        error = tokudb_savepoint(hton, thd, savepoint);
    }
    TOKUDB_DBUG_RETURN(error);
}

static int tokudb_release_savepoint(handlerton * hton, THD * thd, void *savepoint) {
    TOKUDB_DBUG_ENTER("tokudb_release_savepoint");
    int error;
859 860 861 862 863

    SP_INFO save_info = (SP_INFO)savepoint;
    DB_TXN* parent = NULL;
    DB_TXN* txn_to_commit = save_info->txn;

Zardosht Kasheff's avatar
Zardosht Kasheff committed
864
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
865 866 867 868 869 870 871 872 873
    parent = txn_to_commit->parent;
    if (!(error = txn_to_commit->commit(txn_to_commit, 0))) {
        if (save_info->in_sub_stmt) {
            trx->sub_sp_level = parent;
        }
        else {
            trx->sp_level = parent;
        }
        save_info->txn = NULL;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
874 875 876 877
    }
    TOKUDB_DBUG_RETURN(error);
}

878 879 880 881
static int
smart_dbt_callback_verify_frm (DBT const *key, DBT  const *row, void *context) {
    DBT* stored_frm = (DBT *)context;
    stored_frm->size = row->size;
882 883 884
    stored_frm->data = (uchar *)my_malloc(row->size, MYF(MY_WME));
    assert(stored_frm->data);
    memcpy(stored_frm->data, row->data, row->size);
885 886
    return 0;
}
887 888 889 890 891 892 893 894 895 896 897
static int tokudb_discover(handlerton *hton, THD* thd, const char *db, 
                        const char *name,
                        uchar **frmblob, 
                        size_t *frmlen)
{
    TOKUDB_DBUG_ENTER("tokudb_discover");
    int error;
    DB* status_db = NULL;
    DB_TXN* txn = NULL;
    char path[FN_REFLEN + 1];
    HA_METADATA_KEY curr_key = hatoku_frm_data;
898 899 900 901
    DBT key, value;    
    bzero(&key, sizeof(key));
    bzero(&value, sizeof(&value));
    
902 903 904 905 906 907 908 909 910
    error = db_env->txn_begin(db_env, 0, &txn, 0);
    if (error) { goto cleanup; }

    build_table_filename(path, sizeof(path) - 1, db, name, "", 0);
    error = open_status_dictionary(&status_db, path, txn);
    if (error) { goto cleanup; }

    key.data = &curr_key;
    key.size = sizeof(curr_key);
911 912

    error = status_db->getf_set(
913
        status_db, 
914 915
        txn,
        0,
916
        &key, 
917 918
        smart_dbt_callback_verify_frm,
        &value
919 920 921 922 923
        );
    if (error) {
        goto cleanup;
    }

924
    *frmblob = (uchar *)value.data;
925 926 927 928 929 930 931 932 933 934 935 936
    *frmlen = value.size;

    error = 0;
cleanup:
    if (status_db) {
        status_db->close(status_db,0);
    }
    if (txn) {
        commit_txn(txn, 0);
    }
    TOKUDB_DBUG_RETURN(error);    
}
Zardosht Kasheff's avatar
Zardosht Kasheff committed
937

938 939 940 941
static int smart_dbt_do_nothing (DBT const *key, DBT  const *row, void *context) {
  return 0;
}

942

943
static int tokudb_get_user_data_size(THD *thd, bool exact, u_int64_t *data_size_ret) {
944
    int error;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
945
    u_int64_t num_bytes_in_db = 0;
946 947 948
    DB* curr_db = NULL;
    DB_TXN* txn = NULL;
    DBC* tmp_cursor = NULL;
949
    DBC* tmp_table_cursor = NULL;
950 951
    DBT curr_key;
    DBT curr_val;
952
    DB_TXN* tmp_txn = NULL;
953 954 955 956
    memset(&curr_key, 0, sizeof curr_key); 
    memset(&curr_val, 0, sizeof curr_val);
    pthread_mutex_lock(&tokudb_meta_mutex);

957
    error = db_env->txn_begin(db_env, 0, &txn, DB_READ_UNCOMMITTED);
958 959 960 961 962 963 964 965
    if (error) {
        goto cleanup;
    }
    error = metadata_db->cursor(metadata_db, txn, &tmp_cursor, 0);
    if (error) {
        goto cleanup;
    }
    while (error == 0) {
966
        tmp_txn = NULL;
967 968 969 970 971 972 973
        //
        // here, and in other places, check if process has been killed
        // if so, get out of function so user is not stalled
        //
        if (thd->killed) {
            break;
        }
974 975 976 977 978
        error = db_env->txn_begin(db_env, 0, &tmp_txn, DB_READ_UNCOMMITTED);
        if (error) {
            goto cleanup;
        }

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
        //
        // do not need this to be super fast, so use old simple API
        //
        error = tmp_cursor->c_get(
            tmp_cursor, 
            &curr_key, 
            &curr_val, 
            DB_NEXT
            );
        if (!error) {
            char* name = (char *)curr_key.data;
            char* newname = NULL;
            u_int64_t curr_num_bytes = 0;
            DB_BTREE_STAT64 dict_stats;

            newname = (char *)my_malloc(
                get_max_dict_name_path_length(name),
                MYF(MY_WME|MY_ZEROFILL)
                );
            if (newname == NULL) { 
                error = ENOMEM;
                goto cleanup;
            }

            make_name(newname, name, "main");

            error = db_create(&curr_db, db_env, 0);
            if (error) { goto cleanup; }
            
1008
            error = curr_db->open(curr_db, tmp_txn, newname, NULL, DB_BTREE, DB_THREAD, 0);
1009
            if (error == ENOENT) { error = 0; continue; }
1010 1011
            if (error) { goto cleanup; }

1012 1013 1014 1015 1016
            if (exact) {
                //
                // flatten if exact is required
                //
                uint curr_num_items = 0;                
1017
                error = curr_db->cursor(curr_db, tmp_txn, &tmp_table_cursor, 0);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
                if (error) {
                    tmp_table_cursor = NULL;
                    goto cleanup;
                }
                while (error != DB_NOTFOUND) {
                    error = tmp_table_cursor->c_getf_next(tmp_table_cursor, 0, smart_dbt_do_nothing, NULL);
                    if (error && error != DB_NOTFOUND) {
                        goto cleanup;
                    }
                    curr_num_items++;
                    //
                    // allow early exit if command has been killed
                    //
                    if ( (curr_num_items % 1000) == 0 && thd->killed) {
                        goto cleanup;
                    }
1034 1035 1036
                }
                error = tmp_table_cursor->c_close(tmp_table_cursor);
                assert(error==0);
1037 1038 1039
                tmp_table_cursor = NULL;
            }

1040 1041
            error = curr_db->stat64(
                curr_db, 
1042
                tmp_txn, 
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
                &dict_stats
                );
            if (error) { goto cleanup; }

            curr_num_bytes = dict_stats.bt_dsize;
            if (*(uchar *)curr_val.data) {
                //
                // in this case, we have a hidden primary key, do not
                // want to report space taken up by the hidden primary key to the user
                //
                u_int64_t hpk_space = TOKUDB_HIDDEN_PRIMARY_KEY_LENGTH*dict_stats.bt_ndata;
                curr_num_bytes = (hpk_space > curr_num_bytes) ? 0 : curr_num_bytes - hpk_space;
            }
            else {
1057 1058 1059
                //
                // one infinity byte per key needs to be subtracted
                //
1060 1061 1062 1063 1064 1065
                u_int64_t inf_byte_space = dict_stats.bt_ndata;
                curr_num_bytes = (inf_byte_space > curr_num_bytes) ? 0 : curr_num_bytes - inf_byte_space;
            }

            num_bytes_in_db += curr_num_bytes;

1066 1067 1068 1069 1070
            {
                int r = curr_db->close(curr_db, 0);
                assert(r==0);
                curr_db = NULL;
            }
1071 1072
            my_free(newname,MYF(MY_ALLOW_ZERO_PTR));
        }
1073 1074 1075 1076 1077

        if (tmp_txn) {
            commit_txn(tmp_txn, 0);
            tmp_txn = NULL;
        }
1078 1079
    }

1080
    *data_size_ret = num_bytes_in_db;
1081 1082 1083 1084 1085

    error = 0;

cleanup:
    if (tmp_cursor) {
1086 1087
        int r = tmp_cursor->c_close(tmp_cursor);
        assert(r==0);
1088
    }
1089
    if (tmp_table_cursor) {
1090
        int r = tmp_table_cursor->c_close(tmp_table_cursor);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1091
        assert(r==0);
1092
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1093 1094 1095 1096
    if (curr_db) {
        int r = curr_db->close(curr_db, 0);
        assert(r==0);
    }
1097 1098 1099
    if (tmp_txn) {
        commit_txn(tmp_txn, 0);
    }
1100
    if (txn) {
1101
        commit_txn(txn, 0);
1102 1103
    }
    if (error) {
1104
        sql_print_error("got an error %d in show_data_size\n", error);
1105 1106
    }
    pthread_mutex_unlock(&tokudb_meta_mutex);
1107 1108 1109
    return error;
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
/*** to be used when timestamps are sent in engine status as u_int64_t instead of as char strings
static void
format_time(u_int64_t time64, char *buf) {
    time_t timer = (time_t) time64;
    ctime_r(&timer, buf);
    size_t len = strlen(buf);
    assert(len < 26);
    char end;

    assert(len>=1);
    end = buf[len-1];
    while (end == '\n' || end == '\r') {
        buf[len-1] = '\0';
        len--;
        assert(len>=1);
        end = buf[len-1];
    }
}
***/

1130 1131 1132 1133 1134 1135 1136 1137
#define STATPRINT(legend, val) stat_print(thd, \
                                          tokudb_hton_name, \
                                          tokudb_hton_name_length, \
                                          legend, \
                                          strlen(legend), \
                                          val, \
                                          strlen(val))

1138
#define SHOWVAL(v) \
1139 1140 1141 1142 1143
    snprintf(buf, bufsiz, "%" PRIu64, engstat.v); \
    STATPRINT(#v, buf);

extern sys_var *intern_find_sys_var(const char *str, uint length, bool no_error);

1144 1145 1146
static bool tokudb_show_engine_status(THD * thd, stat_print_fn * stat_print) {
    TOKUDB_DBUG_ENTER("tokudb_show_engine_status");
    int error;
1147 1148
    const int bufsiz = 1024;
    char buf[bufsiz] = {'\0'};
1149 1150 1151

    ENGINE_STATUS engstat;

1152 1153 1154 1155 1156
    {
	sys_var * version = intern_find_sys_var("version", 0, false);
	snprintf(buf, bufsiz, "%s", version->value_ptr(thd, (enum_var_type)0, (LEX_STRING*)NULL));
	STATPRINT("Version", buf);
    }
1157 1158 1159 1160
    error = db_env->get_engine_status(db_env, &engstat, buf, bufsiz);
    if (strlen(buf)) {
	STATPRINT("Environment panic string", buf);
    }
1161
    if (error == 0) {
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
	if (engstat.env_panic) {
	    snprintf(buf, bufsiz, "%" PRIu64, engstat.env_panic);
	    STATPRINT("Environment panic", buf);
	}
	if (engstat.logger_panic) {
	    snprintf(buf, bufsiz, "%" PRIu64, engstat.logger_panic);
	    STATPRINT("logger panic", buf);
	    snprintf(buf, bufsiz, "%" PRIu64, engstat.logger_panic_errno);
	    STATPRINT("logger panic errno", buf);
	}


1174 1175 1176 1177
      if(engstat.enospc_threads_blocked) {
	  STATPRINT("*** URGENT WARNING ***", "FILE SYSTEM IS COMPLETELY FULL");
	  snprintf(buf, bufsiz, "FILE SYSTEM IS COMPLETELY FULL");
      }
1178
      else if (engstat.enospc_state == 0) {
1179 1180
	  snprintf(buf, bufsiz, "more than %d percent of total file system space", 2*tokudb_fs_reserve_percent);
      }
1181
      else if (engstat.enospc_state == 1) {
1182 1183
	  snprintf(buf, bufsiz, "*** WARNING *** FILE SYSTEM IS GETTING FULL (less than %d percent free)", 2*tokudb_fs_reserve_percent);
      } 
1184
      else if (engstat.enospc_state == 2){
1185 1186 1187
	  snprintf(buf, bufsiz, "*** WARNING *** FILE SYSTEM IS GETTING VERY FULL (less than %d percent free): INSERTS ARE PROHIBITED", tokudb_fs_reserve_percent);
      }
      else {
1188
	  snprintf(buf, bufsiz, "information unavailable %" PRIu64, engstat.enospc_state);
1189
      }
1190 1191
      STATPRINT ("disk free space", buf);

1192
      STATPRINT("time of environment creation", engstat.creationtime);
1193
      STATPRINT("time of engine startup", engstat.startuptime);
1194
      STATPRINT("time now", engstat.now);
1195

1196
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_period);
1197
      STATPRINT("checkpoint period", buf);
1198
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_footprint);
1199 1200 1201 1202 1203 1204
      STATPRINT("checkpoint status code (0 = idle)", buf);
      STATPRINT("last checkpoint began ", engstat.checkpoint_time_begin);
      STATPRINT("last complete checkpoint began ", engstat.checkpoint_time_begin_complete);
      STATPRINT("last complete checkpoint ended ", engstat.checkpoint_time_end);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_last_lsn);
      STATPRINT("last complete checkpoint LSN ", buf);
1205
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_count);
1206
      STATPRINT("checkpoints taken  ", buf);
1207
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_count_fail);
1208
      STATPRINT("checkpoints failed", buf);
1209 1210 1211 1212 1213
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_waiters_now);
      STATPRINT("checkpoint waiters now", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_waiters_max);
      STATPRINT("checkpoint waiters max", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_client_wait_on_mo);
1214
      STATPRINT("checkpoint client wait on mo lock, not for a checkpoint", buf);
1215
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_client_wait_on_cs);
1216
      STATPRINT("checkpoint client wait on cs lock, not for a checkpoint", buf);
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_sched_cs);
      STATPRINT("checkpoint sched wait on cs lock", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_client_cs);
      STATPRINT("checkpoint client wait on cs lock", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_txn_cs);
      STATPRINT("checkpoint txn wait on cs lock", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_other_cs);
      STATPRINT("checkpoint other wait on cs lock", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_sched_mo);
      STATPRINT("checkpoint sched wait on mo lock", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_client_mo);
      STATPRINT("checkpoint client wait on mo lock", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_txn_mo);
      STATPRINT("checkpoint txn wait on mo lock", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.checkpoint_wait_other_mo);
      STATPRINT("checkpoint other wait on mo lock", buf);
1233
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cleaner_period);
1234
      STATPRINT("cleaner period", buf);
1235
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cleaner_iterations);
1236
      STATPRINT("cleaner iterations", buf);
1237 1238 1239 1240 1241 1242 1243 1244 1245

      snprintf(buf, bufsiz, "%" PRIu64, engstat.txn_begin);
      STATPRINT("txn begin", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.txn_commit);
      STATPRINT("txn commits", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.txn_abort);
      STATPRINT("txn aborts", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.txn_close);
      STATPRINT("txn close (commit+abort)", buf);
1246 1247

      //3988
1248 1249
      SHOWVAL(txn_num_open);
      SHOWVAL(txn_max_open);
1250

1251 1252
      snprintf(buf, bufsiz, "%" PRIu64, engstat.txn_oldest_live);
      STATPRINT("txn oldest live", buf);
1253
      STATPRINT("txn oldest starttime", engstat.txn_oldest_live_starttime);
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
      snprintf(buf, bufsiz, "%" PRIu64, engstat.next_lsn);
      STATPRINT("next LSN", buf);

      snprintf(buf, bufsiz, "%" PRIu64, engstat.inserts);
      STATPRINT("dictionary inserts", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.inserts_fail);
      STATPRINT("dictionary inserts fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.deletes);
      STATPRINT("dictionary deletes", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.deletes_fail);
      STATPRINT("dictionary deletes fail", buf);
1265 1266 1267 1268
      snprintf(buf, bufsiz, "%" PRIu64, engstat.updates);
      STATPRINT("dictionary updates", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.updates_fail);
      STATPRINT("dictionary updates fail", buf);
1269 1270 1271 1272 1273 1274 1275 1276
      snprintf(buf, bufsiz, "%" PRIu64, engstat.updates_broadcast);
      STATPRINT("dictionary broadcast updates", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.updates_broadcast_fail);
      STATPRINT("dictionary broadcast updates fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.le_updates);
      STATPRINT("leafentry updates", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.le_updates_broadcast);
      STATPRINT("leafentry broadcast updates", buf);
1277 1278
      snprintf(buf, bufsiz, "%" PRIu64, engstat.descriptor_set);
      STATPRINT("descriptor_set", buf);
1279 1280 1281 1282 1283 1284
      snprintf(buf, bufsiz, "%" PRIu64, engstat.partial_fetch_hit);
      STATPRINT("partial_fetch_hit", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.partial_fetch_miss);
      STATPRINT("partial_fetch_miss", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.partial_fetch_compressed);
      STATPRINT("partial_fetch_compressed", buf);
1285 1286
      snprintf(buf, bufsiz, "%" PRIu64, engstat.partial_evictions_nonleaf);
      STATPRINT("partial_evictions_nonleaf", buf);
1287 1288
      snprintf(buf, bufsiz, "%" PRIu64, engstat.partial_evictions_leaf);
      STATPRINT("partial_evictions_leaf", buf);
1289 1290 1291 1292
      snprintf(buf, bufsiz, "%" PRIu64, engstat.msn_discards);
      STATPRINT("msn_discards", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.max_workdone);
      STATPRINT("max_workdone", buf);
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
      snprintf(buf, bufsiz, "%" PRIu64, engstat.total_searches);
      STATPRINT("total_searches", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.total_retries);
      STATPRINT("total_retries", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.max_search_excess_retries);
      STATPRINT("max_search_excess_retries", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.max_search_root_tries);
      STATPRINT("max_search_root_tries", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.search_root_retries);
      STATPRINT("search_root_retries", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.search_tries_gt_height);
      STATPRINT("search_tries_gt_height", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.search_tries_gt_heightplus3);
      STATPRINT("search_tries_gt_heightplus3", buf);
1307

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
      SHOWVAL(cleaner_executions);
      SHOWVAL(cleaner_total_nodes);
      SHOWVAL(cleaner_h1_nodes);
      SHOWVAL(cleaner_hgt1_nodes);
      SHOWVAL(cleaner_empty_nodes);
      SHOWVAL(cleaner_nodes_dirtied);
      SHOWVAL(cleaner_max_buffer_size);
      SHOWVAL(cleaner_min_buffer_size);
      SHOWVAL(cleaner_total_buffer_size);
      SHOWVAL(cleaner_max_buffer_workdone);
      SHOWVAL(cleaner_min_buffer_workdone);
      SHOWVAL(cleaner_total_buffer_workdone);
1320 1321 1322 1323
      SHOWVAL(cleaner_num_leaf_merges_started);
      SHOWVAL(cleaner_num_leaf_merges_running);
      SHOWVAL(cleaner_num_leaf_merges_completed);

1324
      SHOWVAL(cleaner_num_dirtied_for_leaf_merge);
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
      SHOWVAL(flush_total);
      SHOWVAL(flush_in_memory);
      SHOWVAL(flush_needed_io);
      SHOWVAL(flush_cascades);
      SHOWVAL(flush_cascades_1);
      SHOWVAL(flush_cascades_2);
      SHOWVAL(flush_cascades_3);
      SHOWVAL(flush_cascades_4);
      SHOWVAL(flush_cascades_5);
      SHOWVAL(flush_cascades_gt_5);
      SHOWVAL(disk_flush_leaf);
      SHOWVAL(disk_flush_nonleaf);
      SHOWVAL(disk_flush_leaf_for_checkpoint);
      SHOWVAL(disk_flush_nonleaf_for_checkpoint);
Barry Perlman's avatar
Barry Perlman committed
1339 1340
      SHOWVAL(create_leaf);
      SHOWVAL(create_nonleaf);
1341 1342
      SHOWVAL(destroy_leaf);
      SHOWVAL(destroy_nonleaf);
Barry Perlman's avatar
Barry Perlman committed
1343 1344 1345 1346 1347 1348 1349
      SHOWVAL(split_leaf);
      SHOWVAL(split_nonleaf);
      SHOWVAL(merge_leaf);
      SHOWVAL(merge_nonleaf);
      SHOWVAL(dirty_leaf);
      SHOWVAL(dirty_nonleaf);
      SHOWVAL(balance_leaf);
1350 1351 1352 1353
      SHOWVAL(hot_num_started);
      SHOWVAL(hot_num_completed);
      SHOWVAL(hot_num_aborted);
      SHOWVAL(hot_max_root_flush_count);
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
      SHOWVAL(msg_bytes_in);
      SHOWVAL(msg_bytes_out);
      SHOWVAL(msg_bytes_curr);
      SHOWVAL(msg_bytes_max);
      SHOWVAL(msg_num);
      SHOWVAL(msg_num_broadcast);
      SHOWVAL(num_basements_decompressed_normal);
      SHOWVAL(num_basements_decompressed_aggressive);
      SHOWVAL(num_basements_decompressed_prefetch);
      SHOWVAL(num_basements_decompressed_write);
      SHOWVAL(num_msg_buffer_decompressed_normal);
      SHOWVAL(num_msg_buffer_decompressed_aggressive);
      SHOWVAL(num_msg_buffer_decompressed_prefetch);
      SHOWVAL(num_msg_buffer_decompressed_write);
      SHOWVAL(num_pivots_fetched_query);
      SHOWVAL(num_pivots_fetched_prefetch);
      SHOWVAL(num_pivots_fetched_write);
      SHOWVAL(num_basements_fetched_normal);
      SHOWVAL(num_basements_fetched_aggressive);
      SHOWVAL(num_basements_fetched_prefetch);
      SHOWVAL(num_basements_fetched_write);
      SHOWVAL(num_msg_buffer_fetched_normal);
      SHOWVAL(num_msg_buffer_fetched_aggressive);
      SHOWVAL(num_msg_buffer_fetched_prefetch);
      SHOWVAL(num_msg_buffer_fetched_write);
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
      snprintf(buf, bufsiz, "%" PRIu64, engstat.multi_inserts);
      STATPRINT("dictionary inserts multi", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.multi_inserts_fail);
      STATPRINT("dictionary inserts multi fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.multi_deletes);
      STATPRINT("dictionary deletes multi", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.multi_deletes_fail);
      STATPRINT("dictionary deletes multi fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.multi_updates);
      STATPRINT("dictionary updates multi", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.multi_updates_fail);
      STATPRINT("dictionary updates multi fail", buf);

1393 1394 1395 1396 1397
      snprintf(buf, bufsiz, "%" PRIu64, engstat.point_queries);
      STATPRINT("dictionary point queries", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.sequential_queries);
      STATPRINT("dictionary sequential queries", buf);

1398
      //3988
1399 1400 1401 1402
      SHOWVAL(num_db_open);
      SHOWVAL(num_db_close);
      SHOWVAL(num_open_dbs);
      SHOWVAL(max_open_dbs);
1403

1404 1405 1406 1407 1408 1409 1410 1411 1412
      snprintf(buf, bufsiz, "%" PRIu64, engstat.le_max_committed_xr);
      STATPRINT("le_max_committed_xr", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.le_max_provisional_xr);
      STATPRINT("le_max_provisional_xr", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.le_max_memsize);
      STATPRINT("le_max_memsize", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.le_expanded);
      STATPRINT("le_expanded", buf);

1413
      const char * lockstat = (engstat.ydb_lock_ctr & 0x01) ? "Locked" : "Unlocked";
1414 1415
      u_int64_t lockctr     =  engstat.ydb_lock_ctr >> 1;   // lsb indicates if locked
      snprintf(buf, bufsiz, "%" PRIu64, lockctr);  
1416 1417 1418
      STATPRINT("ydb lock", lockstat);
      STATPRINT("ydb lock counter", buf);

1419
      snprintf(buf, bufsiz, "%" PRIu64, engstat.num_waiters_now);
1420
      STATPRINT("num_waiters_now", buf);
1421
      snprintf(buf, bufsiz, "%" PRIu64, engstat.max_waiters);
1422
      STATPRINT("max_waiters", buf);
1423 1424
      snprintf(buf, bufsiz, "%" PRIu64, engstat.total_sleep_time);
      STATPRINT("total_sleep_time", buf);
1425
      snprintf(buf, bufsiz, "%.6f", tokutime_to_seconds(engstat.max_time_ydb_lock_held));
1426
      STATPRINT("max_time_ydb_lock_held", buf);
1427
      snprintf(buf, bufsiz, "%.6f", tokutime_to_seconds(engstat.total_time_ydb_lock_held));
1428
      STATPRINT("total_time_ydb_lock_held", buf);
1429
      snprintf(buf, bufsiz, "%.6f", tokutime_to_seconds(engstat.total_time_since_start));
1430 1431
      STATPRINT("total_time_since_start", buf);

1432 1433 1434 1435 1436
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_lock_taken);  
      STATPRINT("cachetable lock taken", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_lock_released);  
      STATPRINT("cachetable lock released", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_hit);  
1437
      STATPRINT("cachetable hit", buf);
1438
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_miss);  
1439
      STATPRINT("cachetable miss", buf);
1440 1441
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_misstime);  
      STATPRINT("cachetable misstime", buf);
1442 1443
#if 0  
      // restore display when this is fixed
1444 1445
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_waittime);  
      STATPRINT("cachetable waittime", buf);
1446
#endif
1447
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_wait_reading);  
1448
      STATPRINT("cachetable wait reading", buf);
1449
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_wait_writing);  
1450
      STATPRINT("cachetable wait writing", buf);
1451 1452
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_wait_checkpoint);  
      STATPRINT("cachetable wait checkpoint", buf);
1453

1454
      snprintf(buf, bufsiz, "%" PRIu64, engstat.puts);  
1455
      STATPRINT("cachetable puts (new nodes)", buf);
1456
      snprintf(buf, bufsiz, "%" PRIu64, engstat.prefetches);  
1457
      STATPRINT("cachetable prefetches", buf);
1458
      snprintf(buf, bufsiz, "%" PRIu64, engstat.maybe_get_and_pins);  
1459
      STATPRINT("cachetable maybe_get_and_pins", buf);
1460
      snprintf(buf, bufsiz, "%" PRIu64, engstat.maybe_get_and_pin_hits);  
1461
      STATPRINT("cachetable maybe_get_and_pin_hits", buf);
1462
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_current);  
1463
      STATPRINT("cachetable size_current", buf);
1464
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_limit);  
1465
      STATPRINT("cachetable size_limit", buf);
1466 1467
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_max);  
      STATPRINT("cachetable size_max", buf);
1468 1469
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_writing);  
      STATPRINT("cachetable size_writing", buf);
1470 1471
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_nonleaf);  
      STATPRINT("cachetable size_nonleaf", buf);
1472 1473
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_leaf);  
      STATPRINT("cachetable size_leaf", buf);
1474 1475
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_rollback);  
      STATPRINT("cachetable size_rollback", buf);
1476 1477 1478 1479 1480
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_size_cachepressure);  
      STATPRINT("cachetable size_cachepressure", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.cachetable_evictions);  
      STATPRINT("cachetable evictions", buf);
      // cleaner_executions displayed with other cleaner thread info
1481

1482
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_locks_max);
1483
      STATPRINT("max range locks", buf);
1484
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_locks_curr);
1485
      STATPRINT("range locks in use", buf);
1486 1487 1488 1489
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_locks_max_memory);
      STATPRINT("memory available for range locks", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_locks_curr_memory);
      STATPRINT("memory in use for range locks", buf);
1490
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_lock_escalation_successes);
1491
      STATPRINT("range lock escalation successes", buf);
1492
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_lock_escalation_failures);
1493
      STATPRINT("range lock escalation failures", buf);
1494
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_read_locks);
1495
      STATPRINT("range read locks acquired", buf);
1496
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_read_locks_fail);
1497
      STATPRINT("range read locks unable to be acquired", buf);
1498
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_out_of_read_locks);
1499
      STATPRINT("range read locks exhausted", buf);
1500
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_write_locks);
1501
      STATPRINT("range write locks acquired", buf);
1502
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_write_locks_fail);
1503
      STATPRINT("range write locks unable to be acquired", buf);
1504
      snprintf(buf, bufsiz, "%" PRIu64, engstat.range_out_of_write_locks);
1505
      STATPRINT("range write locks exhausted", buf);
1506

1507
      //3988
1508 1509 1510 1511 1512
      SHOWVAL(range_lt_create);
      SHOWVAL(range_lt_create_fail);
      SHOWVAL(range_lt_destroy);
      SHOWVAL(range_lt_num);
      SHOWVAL(range_lt_num_max);
1513

1514 1515 1516 1517 1518 1519 1520 1521 1522
      snprintf(buf, bufsiz, "%" PRIu64, engstat.directory_read_locks);
      STATPRINT("directory_read_locks", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.directory_read_locks_fail);
      STATPRINT("directory_read_locks_fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.directory_write_locks);
      STATPRINT("directory_write_locks", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.directory_write_locks_fail);
      STATPRINT("directory_write_locks_fail", buf);

1523 1524 1525 1526
      snprintf(buf, bufsiz, "%" PRIu64, engstat.fsync_count);
      STATPRINT("fsync count", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.fsync_time);
      STATPRINT("fsync time", buf);
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537

      snprintf(buf, bufsiz, "%" PRIu64, engstat.logger_ilock_ctr);
      STATPRINT("logger ilock count", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.logger_olock_ctr);
      STATPRINT("logger olock count", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.logger_swap_ctr);
      STATPRINT("logger swap count", buf);

      STATPRINT("most recent disk full", engstat.enospc_most_recent);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.enospc_threads_blocked);
      STATPRINT("threads currently blocked by full disk", buf);
1538
      snprintf(buf, bufsiz, "%" PRIu64, engstat.enospc_ctr);
1539
      STATPRINT("ENOSPC blocked count", buf);
1540 1541 1542 1543 1544 1545
      snprintf(buf, bufsiz, "%" PRIu64, engstat.enospc_redzone_ctr);
      STATPRINT("ENOSPC reserve count (redzone)", buf);

      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_create);
      STATPRINT("loader create (success)", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_create_fail);
1546
      STATPRINT("loader create fail", buf);
1547 1548
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_put);
      STATPRINT("loader put", buf);
1549 1550
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_put_fail);
      STATPRINT("loader put_fail", buf);
1551 1552 1553 1554 1555 1556
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_close);
      STATPRINT("loader close (success)", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_close_fail);
      STATPRINT("loader close fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_abort);
      STATPRINT("loader abort", buf);
1557
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_current);
1558
      STATPRINT("loaders current", buf);
1559
      snprintf(buf, bufsiz, "%" PRIu64, engstat.loader_max);
1560 1561 1562 1563 1564
      STATPRINT("loader max", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.logsuppress);
      STATPRINT("log suppress (success) ", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.logsuppressfail);
      STATPRINT("log suppress fail", buf);
1565

1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_create);
      STATPRINT("indexer create (success)", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_create_fail);
      STATPRINT("indexer create fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_build);
      STATPRINT("indexer build (success)", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_build_fail);
      STATPRINT("indexer build fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_close);
      STATPRINT("indexer close (success)", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_close_fail);
      STATPRINT("indexer close fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_abort);
      STATPRINT("indexer abort", buf);
1580
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_current);
1581
      STATPRINT("indexers current", buf);
1582
      snprintf(buf, bufsiz, "%" PRIu64, engstat.indexer_max);
1583 1584
      STATPRINT("indexer max", buf);

1585 1586
#if 0
      // Restore these when we support upgrade again
1587 1588 1589 1590 1591 1592 1593 1594
      snprintf(buf, bufsiz, "%" PRIu64, engstat.upgrade_env_status);
      STATPRINT("upgrade env status", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.upgrade_header);
      STATPRINT("upgrade header", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.upgrade_nonleaf);
      STATPRINT("upgrade nonleaf", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.upgrade_leaf);
      STATPRINT("upgrade leaf", buf);
1595 1596
      snprintf(buf, bufsiz, "%" PRIu64, engstat.optimized_for_upgrade);
      STATPRINT("optimized for upgrade", buf);
1597 1598 1599 1600 1601

      snprintf(buf, bufsiz, "%" PRIu64, engstat.original_ver);
      STATPRINT("original version", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.ver_at_startup);
      STATPRINT("version at startup", buf);
1602 1603 1604
      snprintf(buf, bufsiz, "%" PRIu64, engstat.last_lsn_v13);
      STATPRINT("last LSN of version 13", buf);      
      STATPRINT("time of upgrade to version 14", engstat.upgrade_v14_time);
1605
#endif
1606 1607 1608 1609 1610 1611 1612
      
      snprintf(buf, bufsiz, "%" PRIu64, engstat.malloc_count);
      STATPRINT("malloc count", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.free_count);
      STATPRINT("free count", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.realloc_count);
      STATPRINT("realloc count", buf);
1613 1614 1615 1616
      snprintf(buf, bufsiz, "%" PRIu64, engstat.malloc_fail);
      STATPRINT("malloc fail", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.realloc_fail);
      STATPRINT("realloc fail", buf);
1617 1618 1619 1620 1621 1622
      snprintf(buf, bufsiz, "%" PRIu64, engstat.mem_requested);
      STATPRINT("mem requested", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.mem_used);
      STATPRINT("mem used", buf);
      snprintf(buf, bufsiz, "%" PRIu64, engstat.mem_freed);
      STATPRINT("mem freed", buf);
1623 1624
      snprintf(buf, bufsiz, "%" PRIu64, engstat.max_mem_in_use);
      STATPRINT("max mem in use", buf);
1625 1626
      snprintf(buf, bufsiz, "%" PRIu64, engstat.malloc_mmap_threshold);
      STATPRINT("malloc mmap threshold", buf);
1627 1628
      snprintf(buf, bufsiz, "%s", engstat.mallocator_version);
      STATPRINT("mallocator version", buf);
1629 1630 1631 1632 1633 1634
    }
    if (error) { my_errno = error; }
    TOKUDB_DBUG_RETURN(error);
}


1635
void tokudb_checkpoint_lock(THD * thd) {
1636 1637
    int error;
    tokudb_trx_data* trx = NULL;
1638
    char status_msg[200]; //buffer of 200 should be a good upper bound.
1639 1640
    trx = (tokudb_trx_data *) thd_data_get(thd, tokudb_hton->slot);
    if (!trx) {
1641
        error = create_tokudb_trx_data_instance(&trx);
1642 1643 1644 1645
        //
        // can only fail due to memory allocation, so ok to assert
        //
        assert(!error);
1646 1647 1648 1649 1650 1651
        thd_data_set(thd, tokudb_hton->slot, trx);
    }
    
    if (trx->checkpoint_lock_taken) {
        goto cleanup;
    }
1652 1653 1654 1655
    //
    // This can only fail if environment is not created, which is not possible
    // in handlerton
    //
1656 1657
    sprintf(status_msg, "Trying to grab checkpointing lock.");
    thd_proc_info(thd, status_msg);
1658
    error = db_env->checkpointing_postpone(db_env);
1659
    assert(!error);
1660 1661 1662

    trx->checkpoint_lock_taken = true;
cleanup:
1663
    return;
1664 1665
}

1666
void tokudb_checkpoint_unlock(THD * thd) {
1667
    int error;
1668
    char status_msg[200]; //buffer of 200 should be a good upper bound.
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
    tokudb_trx_data* trx = NULL;
    trx = (tokudb_trx_data *) thd_data_get(thd, tokudb_hton->slot);
    if (!trx) {
        error = 0;
        goto  cleanup;
    }
    if (!trx->checkpoint_lock_taken) {
        error = 0;
        goto  cleanup;
    }
    //
    // at this point, we know the checkpoint lock has been taken
    //
1682 1683
    sprintf(status_msg, "Trying to release checkpointing lock.");
    thd_proc_info(thd, status_msg);
1684
    error = db_env->checkpointing_resume(db_env);
1685
    assert(!error);
1686 1687 1688 1689

    trx->checkpoint_lock_taken = false;
    
cleanup:
1690
    return;
1691 1692
}

1693 1694 1695



Zardosht Kasheff's avatar
Zardosht Kasheff committed
1696 1697
bool tokudb_show_status(handlerton * hton, THD * thd, stat_print_fn * stat_print, enum ha_stat_type stat_type) {
    switch (stat_type) {
1698 1699 1700
    case HA_ENGINE_STATUS:
        return tokudb_show_engine_status(thd, stat_print);
        break;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1701
    default:
1702
        break;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1703
    }
1704
    return FALSE;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
}

static void tokudb_print_error(const DB_ENV * db_env, const char *db_errpfx, const char *buffer) {
    sql_print_error("%s:  %s", db_errpfx, buffer);
}

void tokudb_cleanup_log_files(void) {
    TOKUDB_DBUG_ENTER("tokudb_cleanup_log_files");
    char **names;
    int error;

    if ((error = db_env->txn_checkpoint(db_env, 0, 0, 0)))
        my_error(ER_ERROR_DURING_CHECKPOINT, MYF(0), error);

    if ((error = db_env->log_archive(db_env, &names, 0)) != 0) {
        DBUG_PRINT("error", ("log_archive failed (error %d)", error));
        db_env->err(db_env, error, "log_archive");
        DBUG_VOID_RETURN;
    }

    if (names) {
        char **np;
        for (np = names; *np; ++np) {
#if 1
            if (tokudb_debug)
                TOKUDB_TRACE("%s:cleanup:%s\n", __FUNCTION__, *np);
#else
            my_delete(*np, MYF(MY_WME));
#endif
        }

1736
        free(names);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1737 1738 1739 1740 1741
    }

    DBUG_VOID_RETURN;
}

1742
#if defined(HA_GENERAL_ONLINE)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1743 1744 1745 1746 1747 1748 1749 1750 1751
//
// *******NOTE*****
// If the flags HA_ONLINE_DROP_INDEX and HA_ONLINE_DROP_UNIQUE_INDEX
// are ever added, prepare_drop_index and final_drop_index will need to be modified
// so that the actual deletion of DB's is done in final_drop_index and not prepare_drop_index
//
static uint tokudb_alter_table_flags(uint flags)
{
    return (HA_ONLINE_ADD_INDEX_NO_WRITES| HA_ONLINE_DROP_INDEX_NO_WRITES |
1752
            HA_ONLINE_ADD_UNIQUE_INDEX_NO_WRITES| HA_ONLINE_DROP_UNIQUE_INDEX_NO_WRITES|HA_GENERAL_ONLINE);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1753 1754

}
1755
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770


// options flags
//   PLUGIN_VAR_THDLOCAL  Variable is per-connection
//   PLUGIN_VAR_READONLY  Server variable is read only
//   PLUGIN_VAR_NOSYSVAR  Not a server variable
//   PLUGIN_VAR_NOCMDOPT  Not a command line option
//   PLUGIN_VAR_NOCMDARG  No argument for cmd line
//   PLUGIN_VAR_RQCMDARG  Argument required for cmd line
//   PLUGIN_VAR_OPCMDARG  Argument optional for cmd line
//   PLUGIN_VAR_MEMALLOC  String needs memory allocated


// system variables

1771 1772 1773 1774 1775 1776 1777
static void tokudb_lock_timeout_update(THD * thd,
        struct st_mysql_sys_var * sys_var, 
        void * var, const void * save)
{
    ulonglong * timeout = (ulonglong *) var;

    *timeout = *(const ulonglong *) save;
1778
    db_env->set_lock_timeout(db_env, *timeout);
1779
}
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1780

1781
#define DEFAULT_LOCK_TIMEOUT_MSEC 4000
1782

1783 1784
static MYSQL_SYSVAR_ULONGLONG(lock_timeout, tokudb_lock_timeout,
        0, "TokuDB lock timeout", 
1785
        NULL, tokudb_lock_timeout_update, DEFAULT_LOCK_TIMEOUT_MSEC,
1786
        0, ~0LL, 0);
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819



static void tokudb_cleaner_period_update(THD * thd,
        struct st_mysql_sys_var * sys_var, 
        void * var, const void * save)
{
    ulong * cleaner_period = (ulong *) var;

    *cleaner_period = *(const ulong *) save;
    int r = db_env->cleaner_set_period(db_env, *cleaner_period);
    assert(r==0);
}

#define DEFAULT_CLEANER_PERIOD 1

static MYSQL_SYSVAR_ULONG(cleaner_period, tokudb_cleaner_period,
        0, "TokuDB cleaner_period", 
        NULL, tokudb_cleaner_period_update, DEFAULT_CLEANER_PERIOD,
        0, ~0LL, 0);


static void tokudb_cleaner_iterations_update(THD * thd,
        struct st_mysql_sys_var * sys_var, 
        void * var, const void * save)
{
    ulong * cleaner_iterations = (ulong *) var;

    *cleaner_iterations = *(const ulong *) save;
    int r = db_env->cleaner_set_iterations(db_env, *cleaner_iterations);
    assert(r==0);
}

1820
#define DEFAULT_CLEANER_ITERATIONS 5
1821 1822 1823 1824 1825 1826 1827 1828

static MYSQL_SYSVAR_ULONG(cleaner_iterations, tokudb_cleaner_iterations,
        0, "TokuDB cleaner_iterations", 
        NULL, tokudb_cleaner_iterations_update, DEFAULT_CLEANER_ITERATIONS,
        0, ~0LL, 0);



1829 1830 1831
static MYSQL_SYSVAR_ULONGLONG(cache_size, tokudb_cache_size,
        PLUGIN_VAR_READONLY, "TokuDB cache table size", NULL, NULL, 0,
        0, ~0LL, 0);
1832
static MYSQL_SYSVAR_ULONGLONG(max_lock_memory, tokudb_max_lock_memory, PLUGIN_VAR_READONLY, "TokuDB max memory for locks", NULL, NULL, 0, 0, ~0LL, 0);
1833
static MYSQL_SYSVAR_ULONG(debug, tokudb_debug, 0, "TokuDB Debug", NULL, NULL, 0, 0, ~0L, 0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1834 1835 1836 1837 1838 1839 1840 1841 1842

static MYSQL_SYSVAR_STR(log_dir, tokudb_log_dir, PLUGIN_VAR_READONLY, "TokuDB Log Directory", NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(data_dir, tokudb_data_dir, PLUGIN_VAR_READONLY, "TokuDB Data Directory", NULL, NULL, NULL);

static MYSQL_SYSVAR_STR(version, tokudb_version, PLUGIN_VAR_READONLY, "TokuDB Version", NULL, NULL, NULL);

static MYSQL_SYSVAR_UINT(init_flags, tokudb_init_flags, PLUGIN_VAR_READONLY, "Sets TokuDB DB_ENV->open flags", NULL, NULL, tokudb_init_flags, 0, ~0, 0);

Zardosht Kasheff's avatar
Zardosht Kasheff committed
1843
static MYSQL_SYSVAR_UINT(checkpointing_period, tokudb_checkpointing_period, 0, "TokuDB Checkpointing period", NULL, NULL, 60, 0, ~0L, 0);
1844 1845
static MYSQL_SYSVAR_UINT(write_status_frequency, tokudb_write_status_frequency, 0, "TokuDB frequency that show processlist updates status of writes", NULL, NULL, 1000, 0, ~0L, 0);
static MYSQL_SYSVAR_UINT(read_status_frequency, tokudb_read_status_frequency, 0, "TokuDB frequency that show processlist updates status of reads", NULL, NULL, 10000, 0, ~0L, 0);
1846
static MYSQL_SYSVAR_INT(fs_reserve_percent, tokudb_fs_reserve_percent, PLUGIN_VAR_READONLY, "TokuDB file system space reserve (percent free required)", NULL, NULL, 5, 0, 100, 0);
1847
static MYSQL_SYSVAR_STR(tmp_dir, tokudb_tmp_dir, PLUGIN_VAR_READONLY, "Tokudb Tmp Dir", NULL, NULL, NULL);
1848

Zardosht Kasheff's avatar
Zardosht Kasheff committed
1849 1850
static struct st_mysql_sys_var *tokudb_system_variables[] = {
    MYSQL_SYSVAR(cache_size),
1851
    MYSQL_SYSVAR(max_lock_memory),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1852 1853 1854 1855
    MYSQL_SYSVAR(data_dir),
    MYSQL_SYSVAR(log_dir),
    MYSQL_SYSVAR(debug),
    MYSQL_SYSVAR(commit_sync),
1856 1857 1858 1859 1860

    // XXX: implmement a new mysql system variable in our handlerton
    // called tokudb_lock_timeout. this variable defines the maximum
    // time that threads will wait for a lock to be acquired
    MYSQL_SYSVAR(lock_timeout),
1861 1862
    MYSQL_SYSVAR(cleaner_period),
    MYSQL_SYSVAR(cleaner_iterations),
1863 1864 1865

    // XXX remove the old tokudb_read_lock_wait session variable
    // XXX remove the old tokudb_write_lock_wait session variable
1866
    MYSQL_SYSVAR(pk_insert_mode),
1867
    MYSQL_SYSVAR(load_save_space),
1868
    MYSQL_SYSVAR(disable_slow_alter),
1869
    MYSQL_SYSVAR(create_index_online),
1870
    MYSQL_SYSVAR(disable_prefetching),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1871 1872
    MYSQL_SYSVAR(version),
    MYSQL_SYSVAR(init_flags),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1873
    MYSQL_SYSVAR(checkpointing_period),
1874
    MYSQL_SYSVAR(prelock_empty),
1875
    MYSQL_SYSVAR(checkpoint_lock),
1876 1877
    MYSQL_SYSVAR(write_status_frequency),
    MYSQL_SYSVAR(read_status_frequency),
1878
    MYSQL_SYSVAR(fs_reserve_percent),
1879
    MYSQL_SYSVAR(tmp_dir),
1880
    MYSQL_SYSVAR(block_size),
1881
    MYSQL_SYSVAR(read_block_size),
1882
    MYSQL_SYSVAR(read_buf_size),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1883 1884 1885
    NULL
};

1886 1887 1888 1889
struct st_mysql_storage_engine tokudb_storage_engine = { MYSQL_HANDLERTON_INTERFACE_VERSION };

static ST_FIELD_INFO tokudb_user_data_field_info[] = {
    {"User Data Size", 8, MYSQL_TYPE_LONGLONG, 0, 0, "user data size", SKIP_OPEN_TABLE },
1890
    {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, SKIP_OPEN_TABLE}
1891 1892 1893
};

static int tokudb_user_data_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) {
1894
    int error;
1895
    uint64_t data_size;
1896
    TABLE *table = tables->table;
1897 1898 1899 1900 1901 1902
    
    // 3938: Get a read lock on the status flag, since we must
    // read it before safely proceeding
    rw_rdlock(&tokudb_hton_initialized_lock);

    if (!tokudb_hton_initialized) {
1903 1904 1905 1906 1907 1908 1909 1910
        my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), "TokuDB");
        error = -1;
    } else {
        error = tokudb_get_user_data_size(thd, false, &data_size);
        if (error == 0) {
            table->field[0]->store(data_size, false);
            error = schema_table_store_record(thd, table);
        }
1911
    }
1912 1913 1914

    // 3938: unlock the status flag lock
    rw_unlock(&tokudb_hton_initialized_lock);
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
    return error;
}

static int tokudb_user_data_init(void *p) {
    ST_SCHEMA_TABLE *schema = (ST_SCHEMA_TABLE *) p;
    schema->fields_info = tokudb_user_data_field_info;
    schema->fill_table = tokudb_user_data_fill_table;
    return 0;
}

static int tokudb_user_data_done(void *p) {
    return 0;
}

struct st_mysql_information_schema tokudb_user_data_information_schema = { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };

static ST_FIELD_INFO tokudb_user_data_exact_field_info[] = {
    {"User Data Size", 8, MYSQL_TYPE_LONGLONG, 0, 0, "user data size", SKIP_OPEN_TABLE },
1933
    {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, SKIP_OPEN_TABLE}
1934 1935 1936
};

static int tokudb_user_data_exact_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) {
1937
    int error;
1938
    uint64_t data_size;
1939
    TABLE *table = tables->table;
1940 1941 1942 1943 1944 1945

    // 3938: Get a read lock on the status flag, since we must
    // read it before safely proceeding
    rw_rdlock(&tokudb_hton_initialized_lock);

    if (!tokudb_hton_initialized) {
1946 1947 1948 1949 1950 1951 1952 1953
        my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), "TokuDB");
        error = -1;
    } else {
        error = tokudb_get_user_data_size(thd, true, &data_size);
        if (error == 0) {
            table->field[0]->store(data_size, false);
            error = schema_table_store_record(thd, table);
        }
1954
    }
1955 1956 1957

    //3938: unlock the status flag lock
    rw_unlock(&tokudb_hton_initialized_lock);
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
    return error;
}

static int tokudb_user_data_exact_init(void *p) {
    ST_SCHEMA_TABLE *schema = (ST_SCHEMA_TABLE *) p;
    schema->fields_info = tokudb_user_data_exact_field_info;
    schema->fill_table = tokudb_user_data_exact_fill_table;
    return 0;
}

static int tokudb_user_data_exact_done(void *p) {
    return 0;
}

struct st_mysql_information_schema tokudb_user_data_exact_information_schema = { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };

enum { TOKUDB_PLUGIN_VERSION = 0x0400 };

mysql_declare_plugin(tokudb) 
{
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1978
    MYSQL_STORAGE_ENGINE_PLUGIN, 
1979
    &tokudb_storage_engine, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1980 1981
    "TokuDB", 
    "Tokutek Inc", 
1982
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
1983
    PLUGIN_LICENSE_GPL,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1984 1985
    tokudb_init_func,          /* plugin init */
    tokudb_done_func,          /* plugin deinit */
1986
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1987 1988 1989
    NULL,                      /* status variables */
    tokudb_system_variables,   /* system variables */
    NULL                       /* config options */
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_user_data_information_schema, 
    "TokuDB_user_data", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_GPL,
    tokudb_user_data_init,     /* plugin init */
    tokudb_user_data_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    NULL                       /* config options */
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_user_data_exact_information_schema, 
    "TokuDB_user_data_exact", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_GPL,
    tokudb_user_data_exact_init,     /* plugin init */
    tokudb_user_data_exact_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    NULL                       /* config options */
Zardosht Kasheff's avatar
Zardosht Kasheff committed
2018 2019 2020
}
mysql_declare_plugin_end;