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

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

Zardosht Kasheff's avatar
Zardosht Kasheff committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* 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 "ha_tokudb.h"

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

31
#define TOKU_METADB_NAME "tokudb_meta"
Zardosht Kasheff's avatar
Zardosht Kasheff committed
32

33 34 35 36 37 38
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
39 40 41 42 43 44
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);
45

46 47 48 49 50
static MYSQL_THDVAR_BOOL(commit_sync, 
    PLUGIN_VAR_THDLOCAL, 
    "sync on txn commit",
    /* check */ NULL, 
    /* update */ NULL,
Yoni Fogel's avatar
Yoni Fogel committed
51
    /* default*/ true
52
);
53
static MYSQL_THDVAR_UINT(pk_insert_mode,
54 55 56 57 58 59 60 61 62
    0,
    "set the primary key insert mode",
    NULL, 
    NULL, 
    1, // default
    0, // min?
    2, // max
    1 // blocksize
);
63
static MYSQL_THDVAR_BOOL(load_save_space,
64 65 66 67 68 69
    0,
    "if on, intial loads are slower but take less space",
    NULL, 
    NULL, 
    false
);
70
static MYSQL_THDVAR_BOOL(disable_slow_alter,
71 72 73 74 75 76
    0,
    "if on, alter tables that require copy are disabled",
    NULL, 
    NULL, 
    false
);
77
static MYSQL_THDVAR_BOOL(disable_hot_alter,
78 79 80 81 82 83
    0,
    "if on, hot alter table is disabled",
    NULL, 
    NULL, 
    false
);
84
static MYSQL_THDVAR_BOOL(create_index_online,
85 86 87 88 89 90
    0,
    "if on, create index done online",
    NULL, 
    NULL, 
    true
);
91
static MYSQL_THDVAR_BOOL(disable_prefetching,
92 93 94 95 96 97
    0,
    "if on, prefetching disabled",
    NULL, 
    NULL, 
   false
);
98
static MYSQL_THDVAR_BOOL(prelock_empty,
99 100 101 102 103 104
    0,
    "Tokudb Prelock Empty Table",
    NULL, 
    NULL, 
    true
);
105
static MYSQL_THDVAR_BOOL(log_client_errors,
106 107 108 109 110 111
    0,
    "Tokudb Log Client Errors",
    NULL, 
    NULL, 
    false
);
112
static MYSQL_THDVAR_UINT(block_size,
113 114 115 116 117 118
    0,
    "fractal tree block size",
    NULL, 
    NULL, 
    4<<20, // default
    4096,  // min
119
    ~0U,   // max
120 121
    1      // blocksize???
);
122
static MYSQL_THDVAR_UINT(read_block_size,
123 124 125 126 127 128
    0,
    "fractal tree read block size",
    NULL, 
    NULL, 
    128*1024, // default
    4096,  // min
129
    ~0U,   // max
130 131
    1      // blocksize???
);
132
static MYSQL_THDVAR_UINT(read_buf_size,
133 134 135 136 137 138 139 140 141
    0,
    "fractal tree read block size", //TODO: Is this a typo?
    NULL, 
    NULL, 
    128*1024, // default
    0,  // min
    1*1024*1024,   // max
    1      // blocksize???
);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#if TOKU_INCLUDE_UPSERT
static MYSQL_THDVAR_BOOL(disable_slow_update, 
    PLUGIN_VAR_THDLOCAL, 
    "disable slow update",
    NULL, // check
    NULL, // update
    false // default
);
static MYSQL_THDVAR_BOOL(disable_slow_upsert, 
    PLUGIN_VAR_THDLOCAL, 
    "disable slow upsert",
    NULL, // check
    NULL, // update
    false // default
);
#endif
158

159 160
static void tokudb_checkpoint_lock(THD * thd);
static void tokudb_checkpoint_unlock(THD * thd);
161

162
static void
163 164 165 166 167 168 169
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;
Yoni Fogel's avatar
Yoni Fogel committed
170
    *val= *(my_bool *) save ? true : false;
171 172 173 174 175 176 177 178 179
    if (*val) {
        tokudb_checkpoint_lock(thd);
    }
    else {
        tokudb_checkpoint_unlock(thd);
    }
}
  
static MYSQL_THDVAR_BOOL(checkpoint_lock,
180 181 182 183 184 185
    0,
    "Tokudb Checkpoint Lock",
    NULL, 
    tokudb_checkpoint_lock_update, 
    false
);
186

187 188 189 190 191 192 193
static const char *tokudb_row_format_names[] = {
    "tokudb_uncompressed",
    "tokudb_zlib",
    "tokudb_quicklz",
    "tokudb_lzma",
    "tokudb_fast",
    "tokudb_small",
194
    "tokudb_default",
195 196 197 198 199 200 201 202 203 204
    NullS
};

static TYPELIB tokudb_row_format_typelib = {
    array_elements(tokudb_row_format_names) - 1,
    "tokudb_row_format_typelib",
    tokudb_row_format_names,
    NULL
};

205
static MYSQL_THDVAR_ENUM(row_format, PLUGIN_VAR_OPCMDARG,
206 207
                         "Specifies the compression method for a table during this session. "
                         "Possible values are TOKUDB_UNCOMPRESSED, TOKUDB_ZLIB, TOKUDB_QUICKLZ, "
208 209
                         "TOKUDB_LZMA, TOKUDB_FAST, TOKUDB_SMALL and TOKUDB_DEFAULT",
                         NULL, NULL, SRV_ROW_FORMAT_DEFAULT, &tokudb_row_format_typelib);
210 211 212 213 214 215

srv_row_format_t get_row_format(THD *thd)
{
    return (srv_row_format_t) THDVAR(thd, row_format);
}

Zardosht Kasheff's avatar
Zardosht Kasheff committed
216 217 218 219 220 221 222 223
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);
224
#if TOKU_INCLUDE_XA
225 226 227 228
static int tokudb_xa_prepare(handlerton* hton, THD* thd, bool all);
static int tokudb_xa_recover(handlerton* hton, XID*  xid_list, uint  len);
static int tokudb_commit_by_xid(handlerton* hton, XID* xid);
static int tokudb_rollback_by_xid(handlerton* hton, XID*  xid);
229
#endif
230

231
#if defined(HA_GENERAL_ONLINE) || defined(HA_INPLACE_ADD_INDEX_NO_READ_WRITE)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
232
static uint tokudb_alter_table_flags(uint flags);
233
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
234 235 236
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);
237 238
static int tokudb_discover(handlerton *hton, THD* thd, const char *db, const char *name, uchar **frmblob, size_t *frmlen);
static int tokudb_discover2(handlerton *hton, THD* thd, const char *db, const char *name, bool translate_name,uchar **frmblob, size_t *frmlen);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
239 240 241 242 243 244
handlerton *tokudb_hton;

const char *ha_tokudb_ext = ".tokudb";
char *tokudb_data_dir;
ulong tokudb_debug;
DB_ENV *db_env;
245
DB* metadata_db;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
246 247
HASH tokudb_open_tables;
pthread_mutex_t tokudb_mutex;
248
pthread_mutex_t tokudb_meta_mutex;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
249

250 251 252 253 254
static PARTITIONED_COUNTER tokudb_primary_key_bytes_inserted;
void toku_hton_update_primary_key_bytes_inserted(uint64_t row_size) {
    increment_partitioned_counter(tokudb_primary_key_bytes_inserted, row_size);
}

255
static ulonglong tokudb_lock_timeout;
256 257
static ulong tokudb_cleaner_period;
static ulong tokudb_cleaner_iterations;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
258

259 260 261 262 263
#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) {
264 265
        snprintf(msg, ASSERT_MSGLEN, "Handlerton: %s ", expr_as_string);
        db_env->crash(db_env, msg, fun, file, line,caller_errno);
266 267
    }
    else {
268 269 270
        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);
271 272 273 274
    }
    abort();
}

Yoni Fogel's avatar
Yoni Fogel committed
275 276
//my_bool tokudb_shared_data = false;
static uint32_t tokudb_init_flags = 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
277 278 279 280
    DB_CREATE | DB_THREAD | DB_PRIVATE | 
    DB_INIT_LOCK | 
    DB_INIT_MPOOL |
    DB_INIT_TXN | 
281
    DB_INIT_LOG |
Zardosht Kasheff's avatar
Zardosht Kasheff committed
282
    DB_RECOVER;
Yoni Fogel's avatar
Yoni Fogel committed
283 284
static uint32_t tokudb_env_flags = 0;
// static uint32_t tokudb_lock_type = DB_LOCK_DEFAULT;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
285 286
// static ulong tokudb_log_buffer_size = 0;
// static ulong tokudb_log_file_size = 0;
287
static my_bool tokudb_directio = FALSE;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
288
static ulonglong tokudb_cache_size = 0;
289
static ulonglong tokudb_max_lock_memory = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
290
static char *tokudb_home;
291
static char *tokudb_tmp_dir;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
292 293 294 295
static char *tokudb_log_dir;
// static long tokudb_lock_scan_time = 0;
// static ulong tokudb_region_size = 0;
// static ulong tokudb_cache_parts = 1;
296
const char *tokudb_hton_name = "TokuDB";
Yoni Fogel's avatar
Yoni Fogel committed
297 298 299
static uint32_t tokudb_checkpointing_period;
uint32_t tokudb_write_status_frequency;
uint32_t tokudb_read_status_frequency;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
300
#ifdef TOKUDB_VERSION
301
char *tokudb_version = (char*) TOKUDB_VERSION;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
302
#else
303
char *tokudb_version;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
304
#endif
305
static int tokudb_fs_reserve_percent;  // file system reserve as a percentage of total disk space
Zardosht Kasheff's avatar
Zardosht Kasheff committed
306

307
#if defined(_WIN32)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
308 309 310
extern "C" {
#include "ydb.h"
}
311
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
// 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);
}
333

Zardosht Kasheff's avatar
Zardosht Kasheff committed
334 335
static int tokudb_init_func(void *p) {
    TOKUDB_DBUG_ENTER("tokudb_init_func");
Zardosht Kasheff's avatar
Zardosht Kasheff committed
336
    int r;
337 338 339 340 341 342 343
#if defined(_WIN64)
        r = toku_ydb_init();
        if (r) {
            printf("got error %d\n", r);
            goto error;
        }
#endif
344 345 346 347 348

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

349 350
    db_env = NULL;
    metadata_db = NULL;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
351 352 353

    tokudb_hton = (handlerton *) p;

354 355
    pthread_mutex_init(&tokudb_mutex, MY_MUTEX_INIT_FAST);
    pthread_mutex_init(&tokudb_meta_mutex, MY_MUTEX_INIT_FAST);
356
    (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
357 358 359

    tokudb_hton->state = SHOW_OPTION_YES;
    // tokudb_hton->flags= HTON_CAN_RECREATE;  // QQQ this came from skeleton
360
    tokudb_hton->flags = HTON_CLOSE_CURSORS_AT_COMMIT;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
361 362 363 364 365 366 367 368
#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;
369 370

    tokudb_hton->savepoint_offset = sizeof(SP_INFO_T);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
371 372 373
    tokudb_hton->savepoint_set = tokudb_savepoint;
    tokudb_hton->savepoint_rollback = tokudb_rollback_to_savepoint;
    tokudb_hton->savepoint_release = tokudb_release_savepoint;
374

375
    tokudb_hton->discover = tokudb_discover;
376 377 378
#if defined(MYSQL_HANDLERTON_INCLUDE_DISCOVER2)
    tokudb_hton->discover2 = tokudb_discover2;
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
379 380
    tokudb_hton->commit = tokudb_commit;
    tokudb_hton->rollback = tokudb_rollback;
381
#if TOKU_INCLUDE_XA
382 383 384 385
    tokudb_hton->prepare=tokudb_xa_prepare;
    tokudb_hton->recover=tokudb_xa_recover;
    tokudb_hton->commit_by_xid=tokudb_commit_by_xid;
    tokudb_hton->rollback_by_xid=tokudb_rollback_by_xid;
386
#endif
387

Zardosht Kasheff's avatar
Zardosht Kasheff committed
388 389 390
    tokudb_hton->panic = tokudb_end;
    tokudb_hton->flush_logs = tokudb_flush_logs;
    tokudb_hton->show_status = tokudb_show_status;
391
#if defined(HA_GENERAL_ONLINE) || defined(HA_INPLACE_ADD_INDEX_NO_READ_WRITE)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
392
    tokudb_hton->alter_table_flags = tokudb_alter_table_flags;
393
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    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");

424 425 426 427 428 429 430 431 432
    //
    // 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
433
    {
434
    char *tmp_dir = tokudb_tmp_dir;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
435
    char *data_dir = tokudb_data_dir;
436
    if (data_dir == 0) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
437
        data_dir = mysql_data_home;
438
    }
439 440
    if (tmp_dir == 0) {
        tmp_dir = data_dir;
441
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
442 443
    DBUG_PRINT("info", ("tokudb_data_dir: %s\n", data_dir));
    db_env->set_data_dir(db_env, data_dir);
444

445 446
    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
447 448 449 450 451 452 453
    }

    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);
    }

454
    // 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
455
    if (tokudb_cache_size == 0) {
456 457 458 459 460 461 462 463
        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
464 465 466
    }
    if (tokudb_cache_size) {
        DBUG_PRINT("info", ("tokudb_cache_size: %lld\n", tokudb_cache_size));
Yoni Fogel's avatar
Yoni Fogel committed
467
        r = db_env->set_cachesize(db_env, (uint32_t)(tokudb_cache_size >> 30), (uint32_t)(tokudb_cache_size % (1024L * 1024L * 1024L)), 1);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
468 469 470 471 472
        if (r) {
            DBUG_PRINT("info", ("set_cachesize %d\n", r));
            goto error; 
        }
    }
473 474 475 476 477 478 479 480 481 482 483 484
    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; 
        }
    }
    
Yoni Fogel's avatar
Yoni Fogel committed
485
    uint32_t gbytes, bytes; int parts;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    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

501 502 503 504 505 506
    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
507 508
    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
509
    r = db_env->set_generate_row_callback_for_put(db_env,generate_row_for_put);
510
    assert(!r);
511 512
    r = db_env->set_generate_row_callback_for_del(db_env,generate_row_for_del);
    assert(!r);
513
    db_env->set_update(db_env, tokudb_update_fun);
514
    db_env_set_direct_io(tokudb_directio == TRUE);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
515
    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
516 517 518 519 520 521 522 523

    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
524 525
    r = db_env->checkpointing_set_period(db_env, tokudb_checkpointing_period);
    assert(!r);
526 527 528 529
    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
530

531
    r = db_env->set_lock_timeout(db_env, tokudb_lock_timeout);
532 533
    assert(r == 0);

534 535 536 537 538 539 540
    r = db_create(&metadata_db, db_env, 0);
    if (r) {
        DBUG_PRINT("info", ("failed to create metadata db %d\n", r));
        goto error;
    }
    

541
    r= metadata_db->open(metadata_db, NULL, TOKU_METADB_NAME, NULL, DB_BTREE, DB_THREAD, 0);
542
    if (r) {
543 544 545 546 547 548
        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);
549 550 551 552 553
        r = db_create(&metadata_db, db_env, 0);
        if (r) {
            DBUG_PRINT("info", ("failed to create metadata db %d\n", r));
            goto error;
        }
554 555

        r= metadata_db->open(metadata_db, NULL, TOKU_METADB_NAME, NULL, DB_BTREE, DB_THREAD | DB_CREATE | DB_EXCL, my_umask);
556 557 558 559 560
        if (r) {
            goto error;
        }
    }

561 562
    tokudb_primary_key_bytes_inserted = create_partitioned_counter();

563 564 565
    //3938: succeeded, set the init status flag and unlock
    tokudb_hton_initialized = 1;
    rw_unlock(&tokudb_hton_initialized_lock);
Yoni Fogel's avatar
Yoni Fogel committed
566
    DBUG_RETURN(false);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
567 568

error:
569
    if (metadata_db) {
570 571
        int rr = metadata_db->close(metadata_db, 0);
        assert(rr==0);
572
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
573
    if (db_env) {
574 575
        int rr= db_env->close(db_env, 0);
        assert(rr==0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
576 577
        db_env = 0;
    }
578 579 580 581

    // 3938: failed to initialized, drop the flag and lock
    tokudb_hton_initialized = 0;
    rw_unlock(&tokudb_hton_initialized_lock);
Yoni Fogel's avatar
Yoni Fogel committed
582
    DBUG_RETURN(true);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
583 584 585 586
}

static int tokudb_done_func(void *p) {
    TOKUDB_DBUG_ENTER("tokudb_done_func");
587
    my_hash_free(&tokudb_open_tables);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
588
    pthread_mutex_destroy(&tokudb_mutex);
589
    pthread_mutex_destroy(&tokudb_meta_mutex);
590
#if defined(_WIN64)
591
    toku_ydb_destroy();
592
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
593 594 595 596 597 598 599 600 601 602
    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;
603 604 605 606 607 608 609 610
    
    // 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);

611
    if (metadata_db) {
612 613
        int r = metadata_db->close(metadata_db, 0);
        assert(r==0);
614
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
615 616 617 618
    if (db_env) {
        if (tokudb_init_flags & DB_INIT_LOG)
            tokudb_cleanup_log_files();
        error = db_env->close(db_env, 0);       // Error is logged
619
        assert(error==0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
620 621
        db_env = NULL;
    }
622 623 624 625 626

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

Zardosht Kasheff's avatar
Zardosht Kasheff committed
627 628 629 630
    TOKUDB_DBUG_RETURN(error);
}

static int tokudb_close_connection(handlerton * hton, THD * thd) {
631 632 633 634 635 636 637 638
    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
639 640 641 642 643 644
}

bool tokudb_flush_logs(handlerton * hton) {
    TOKUDB_DBUG_ENTER("tokudb_flush_logs");
    int error;
    bool result = 0;
Yoni Fogel's avatar
Yoni Fogel committed
645
    uint32_t curr_tokudb_checkpointing_period = 0;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
646 647 648 649 650 651 652 653 654 655 656 657 658

    //
    // 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
659

Zardosht Kasheff's avatar
Zardosht Kasheff committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    //
    // 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
679 680 681 682
    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
683
        goto exit;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
684
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
685 686 687

    result = 0;
exit:
Zardosht Kasheff's avatar
Zardosht Kasheff committed
688 689 690
    TOKUDB_DBUG_RETURN(result);
}

691 692 693 694
uint get_pk_insert_mode(THD* thd) {
    return THDVAR(thd, pk_insert_mode);
}

695 696 697 698
bool get_load_save_space(THD* thd) {
    return (THDVAR(thd, load_save_space) != 0);
}

699 700 701 702
bool get_disable_slow_alter(THD* thd) {
    return (THDVAR(thd, disable_slow_alter) != 0);
}

703 704 705 706
bool get_disable_hot_alter(THD* thd) {
    return THDVAR(thd, disable_hot_alter) != 0;
}

707 708 709 710
bool get_create_index_online(THD* thd) {
    return (THDVAR(thd, create_index_online) != 0);
}

711 712 713 714
bool get_disable_prefetching(THD* thd) {
    return (THDVAR(thd, disable_prefetching) != 0);
}

715 716 717
bool get_prelock_empty(THD* thd) {
    return (THDVAR(thd, prelock_empty) != 0);
}
718

719 720 721 722
bool get_log_client_errors(THD* thd) {
    return (THDVAR(thd, log_client_errors) != 0);
}

723 724 725 726
uint get_tokudb_block_size(THD* thd) {
    return THDVAR(thd, block_size);
}

727 728 729 730
uint get_tokudb_read_block_size(THD* thd) {
    return THDVAR(thd, read_block_size);
}

731 732 733 734
uint get_tokudb_read_buf_size(THD* thd) {
    return THDVAR(thd, read_buf_size);
}

735 736 737 738 739 740 741 742 743 744
#if TOKU_INCLUDE_UPSERT
bool get_disable_slow_update(THD* thd) {
    return (THDVAR(thd, disable_slow_update) != 0);
}

bool get_disable_slow_upsert(THD* thd) {
    return (THDVAR(thd, disable_slow_upsert) != 0);
}
#endif

745 746 747 748 749 750 751 752 753 754
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) {
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
        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);
        }
773 774
    }
    else {
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
        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);
        }
793 794 795 796 797
    }
    thd_proc_info(progress_info->thd, progress_info->status);
}


Yoni Fogel's avatar
Yoni Fogel committed
798
static void commit_txn_with_progress(DB_TXN* txn, uint32_t flags, THD* thd) {
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    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
820 821 822
static int tokudb_commit(handlerton * hton, THD * thd, bool all) {
    TOKUDB_DBUG_ENTER("tokudb_commit");
    DBUG_PRINT("trans", ("ending transaction %s", all ? "all" : "stmt"));
Yoni Fogel's avatar
Yoni Fogel committed
823
    uint32_t syncflag = THDVAR(thd, commit_sync) ? 0 : DB_TXN_NOSYNC;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
824 825 826
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
    DB_TXN **txn = all ? &trx->all : &trx->stmt;
    if (*txn) {
827
        if (tokudb_debug & TOKUDB_DEBUG_TXN) {
828
            TOKUDB_TRACE("doing txn commit:%d:%p\n", all, *txn);
829
        }
830 831
        // test hook to induce a crash on a debug build
        DBUG_EXECUTE_IF("tokudb_crash_commit_before", DBUG_SUICIDE(););
832
        commit_txn_with_progress(*txn, syncflag, thd);
833 834
        // test hook to induce a crash on a debug build
        DBUG_EXECUTE_IF("tokudb_crash_commit_after", DBUG_SUICIDE(););
835
        if (*txn == trx->sp_level) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
836
            trx->sp_level = 0;
837
        }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
838
        *txn = 0;
839
        trx->sub_sp_level = NULL;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
840 841
    } 
    else if (tokudb_debug & TOKUDB_DEBUG_TXN) {
842
        TOKUDB_TRACE("nothing to commit %d\n", all);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
843
    }
844
    reset_stmt_progress(&trx->stmt_progress);
845
    TOKUDB_DBUG_RETURN(0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
846 847 848 849 850 851 852 853
}

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) {
854
        if (tokudb_debug & TOKUDB_DEBUG_TXN) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
855
            TOKUDB_TRACE("rollback:%p\n", *txn);
856
        }
857
        abort_txn_with_progress(*txn, thd);
858 859 860 861
        if (*txn == trx->sp_level) {
            trx->sp_level = 0;
        }
        *txn = 0;
862
        trx->sub_sp_level = NULL;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
863 864 865
    } 
    else {
        if (tokudb_debug & TOKUDB_DEBUG_TXN) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
866
            TOKUDB_TRACE("abort0\n");
Zardosht Kasheff's avatar
Zardosht Kasheff committed
867 868
        }
    }
869
    reset_stmt_progress(&trx->stmt_progress);
870
    TOKUDB_DBUG_RETURN(0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
871 872
}

873 874
#if TOKU_INCLUDE_XA

875 876 877 878 879 880 881 882 883 884 885 886 887
static int tokudb_xa_prepare(handlerton* hton, THD* thd, bool all) {
    TOKUDB_DBUG_ENTER("tokudb_xa_prepare");
    int r = 0;
    DBUG_PRINT("trans", ("preparing 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) {
        if (tokudb_debug & TOKUDB_DEBUG_TXN) {
            TOKUDB_TRACE("doing txn prepare:%d:%p\n", all, txn);
        }
        // a TOKU_XA_XID is identical to a MYSQL_XID
        TOKU_XA_XID thd_xid;
        thd_get_xid(thd, (MYSQL_XID*) &thd_xid);
888 889
        // test hook to induce a crash on a debug build
        DBUG_EXECUTE_IF("tokudb_crash_prepare_before", DBUG_SUICIDE(););
890
        r = txn->xa_prepare(txn, &thd_xid);
891 892
        // test hook to induce a crash on a debug build
        DBUG_EXECUTE_IF("tokudb_crash_prepare_after", DBUG_SUICIDE(););
893 894 895 896 897 898
    } 
    else if (tokudb_debug & TOKUDB_DEBUG_TXN) {
        TOKUDB_TRACE("nothing to prepare %d\n", all);
    }
    TOKUDB_DBUG_RETURN(r);
}
899

900 901 902 903
static int tokudb_xa_recover(handlerton* hton, XID*  xid_list, uint  len) {
    TOKUDB_DBUG_ENTER("tokudb_xa_recover");
    int r = 0;
    if (len == 0 || xid_list == NULL) {
904
        TOKUDB_DBUG_RETURN(0);
905 906 907 908 909 910 911 912 913
    }
    long num_returned = 0;
    r = db_env->txn_xa_recover(
        db_env,
        (TOKU_XA_XID*)xid_list,
        len,
        &num_returned,
        DB_NEXT
        );
914
    assert(r == 0);
915 916
    TOKUDB_DBUG_RETURN((int)num_returned);
}
917

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
static int tokudb_commit_by_xid(handlerton* hton, XID* xid) {
    TOKUDB_DBUG_ENTER("tokudb_commit_by_xid");
    int r = 0;
    DB_TXN* txn = NULL;
    TOKU_XA_XID* toku_xid = (TOKU_XA_XID*)xid;

    r = db_env->get_txn_from_xid(db_env, toku_xid, &txn);
    if (r) { goto cleanup; }

    r = txn->commit(txn, 0);
    if (r) { goto cleanup; }

    r = 0;
cleanup:
    TOKUDB_DBUG_RETURN(r);
}
934

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
static int tokudb_rollback_by_xid(handlerton* hton, XID*  xid) {
    TOKUDB_DBUG_ENTER("tokudb_rollback_by_xid");
    int r = 0;
    DB_TXN* txn = NULL;
    TOKU_XA_XID* toku_xid = (TOKU_XA_XID*)xid;

    r = db_env->get_txn_from_xid(db_env, toku_xid, &txn);
    if (r) { goto cleanup; }

    r = txn->abort(txn);
    if (r) { goto cleanup; }

    r = 0;
cleanup:
    TOKUDB_DBUG_RETURN(r);
}
Zardosht Kasheff's avatar
Zardosht Kasheff committed
951

952 953
#endif

Zardosht Kasheff's avatar
Zardosht Kasheff committed
954 955 956
static int tokudb_savepoint(handlerton * hton, THD * thd, void *savepoint) {
    TOKUDB_DBUG_ENTER("tokudb_savepoint");
    int error;
957
    SP_INFO save_info = (SP_INFO)savepoint;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
958
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
959 960
    if (thd->in_sub_stmt) {
        assert(trx->stmt);
961
        error = db_env->txn_begin(db_env, trx->sub_sp_level, &(save_info->txn), DB_INHERIT_ISOLATION);
962 963 964 965 966
        if (error) {
            goto cleanup;
        }
        trx->sub_sp_level = save_info->txn;
        save_info->in_sub_stmt = true;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
967
    }
968
    else {
969
        error = db_env->txn_begin(db_env, trx->sp_level, &(save_info->txn), DB_INHERIT_ISOLATION);
970 971 972 973 974 975 976 977 978
        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
979 980 981 982 983 984
    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;
985 986 987 988
    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
989
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
990 991 992 993 994 995 996 997
    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
998 999 1000 1001 1002 1003 1004 1005
        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;
1006 1007 1008 1009 1010

    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
1011
    tokudb_trx_data *trx = (tokudb_trx_data *) thd_data_get(thd, hton->slot);
1012 1013 1014 1015 1016 1017 1018 1019 1020
    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
1021 1022 1023 1024
    }
    TOKUDB_DBUG_RETURN(error);
}

1025 1026 1027 1028 1029 1030
static int tokudb_discover(handlerton *hton, THD* thd, const char *db, const char *name, uchar **frmblob, size_t *frmlen) {
    return tokudb_discover2(hton, thd, db, name, true, frmblob, frmlen);
}

static int tokudb_discover2(handlerton *hton, THD* thd, const char *db, const char *name, bool translate_name,
                            uchar **frmblob, size_t *frmlen) {
1031 1032 1033 1034 1035 1036
    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;
1037
    DBT key, value;    
1038 1039
    memset(&key, 0, sizeof(key));
    memset(&value, 0, sizeof(&value));
1040
    
1041 1042 1043
    error = db_env->txn_begin(db_env, 0, &txn, 0);
    if (error) { goto cleanup; }

1044
    build_table_filename(path, sizeof(path) - 1, db, name, "", translate_name ? 0 : FN_IS_TMP);
1045 1046 1047 1048 1049
    error = open_status_dictionary(&status_db, path, txn);
    if (error) { goto cleanup; }

    key.data = &curr_key;
    key.size = sizeof(curr_key);
1050 1051

    error = status_db->getf_set(
1052
        status_db, 
1053 1054
        txn,
        0,
1055
        &key, 
1056 1057
        smart_dbt_callback_verify_frm,
        &value
1058 1059 1060 1061 1062
        );
    if (error) {
        goto cleanup;
    }

1063
    *frmblob = (uchar *)value.data;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    *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
1076

Yoni Fogel's avatar
Yoni Fogel committed
1077
static int store_dbname_tablename_size(TABLE *table, char *name, uint64_t size, THD *thd) {
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    char *tp = strrchr(name, '/');
    assert(tp);
    char *tablename = tp + 1;
    size_t tablename_length = strlen(tablename);

    char *dp = strchr(name, '/');
    char *dbname;
    size_t dbname_length;
    if (dp == tp) {
        dbname = name;
        dbname_length = tp - dbname;
    } else {
        dbname = dp + 1;
        dbname_length = tp - dbname;
    }

    table->field[0]->store(dbname, dbname_length, system_charset_info);
    table->field[1]->store(tablename, tablename_length, system_charset_info);
    table->field[2]->store(size, false);
    int error = schema_table_store_record(thd, table);
    return error;
}

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
static int tokudb_dictionary_info(TABLE *table, THD *thd) {
    int error;
    DB_TXN* txn = NULL;
    DBC* tmp_cursor = NULL;
    DBT curr_key;
    DBT curr_val;
    memset(&curr_key, 0, sizeof curr_key); 
    memset(&curr_val, 0, sizeof curr_val);
    error = db_env->txn_begin(db_env, 0, &txn, DB_READ_UNCOMMITTED);
    if (error) {
        goto cleanup;
    }
    error = db_env->get_cursor_for_directory(db_env, txn, &tmp_cursor);
    if (error) {
        goto cleanup;
    }
    while (error == 0) {
        error = tmp_cursor->c_get(
            tmp_cursor, 
            &curr_key, 
            &curr_val, 
            DB_NEXT
            );
        if (!error) {
1125 1126 1127
            // We store the NULL terminator in the directory so it's included in the size.
            // See #5789
            // Recalculate and check just to be safe.
1128 1129
            size_t dname_len = strlen((const char *)curr_key.data);
            size_t iname_len = strlen((const char *)curr_val.data);
1130 1131
            assert(dname_len == curr_key.size - 1);
            assert(iname_len == curr_val.size - 1);
1132 1133
            table->field[0]->store(
                (char *)curr_key.data,
1134
                dname_len,
1135 1136 1137 1138
                system_charset_info
                );
            table->field[1]->store(
                (char *)curr_val.data,
1139
                iname_len,
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
                system_charset_info
                );
            error = schema_table_store_record(thd, table);
        }
    }
    if (error == DB_NOTFOUND) {
        error = 0;
    }
cleanup:
    if (tmp_cursor) {
        int r = tmp_cursor->c_close(tmp_cursor);
        assert(r==0);
    }
    if (txn) {
        commit_txn(txn, 0);
    }
    return error;
}

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
static int tokudb_report_fractal_tree_info_for_db(const DBT *dname, const DBT *iname, TABLE *table, THD *thd) {
    int error;
    DB *db;
    uint64_t bt_num_blocks_allocated;
    uint64_t bt_num_blocks_in_use;
    uint64_t bt_size_allocated;
    uint64_t bt_size_in_use;

    error = db_create(&db, db_env, 0);
    if (error) {
        goto exit;
    }
    error = db->open(db, NULL, (char *)dname->data, NULL, DB_BTREE, 0, 0666);
    if (error) {
        goto exit;
    }
    error = db->get_fractal_tree_info64(db,
                                        &bt_num_blocks_allocated, &bt_num_blocks_in_use,
                                        &bt_size_allocated, &bt_size_in_use);
    {
        int close_error = db->close(db, 0);
        if (!error) {
            error = close_error;
        }
    }
    if (error) {
        goto exit;
    }

1188 1189 1190
    // We store the NULL terminator in the directory so it's included in the size.
    // See #5789
    // Recalculate and check just to be safe.
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
    {
        size_t dname_len = strlen((const char *)dname->data);
        size_t iname_len = strlen((const char *)iname->data);
        assert(dname_len == dname->size - 1);
        assert(iname_len == iname->size - 1);
        table->field[0]->store(
            (char *)dname->data,
            dname_len,
            system_charset_info
            );
        table->field[1]->store(
            (char *)iname->data,
            iname_len,
            system_charset_info
            );
    }
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    table->field[2]->store(bt_num_blocks_allocated, false);
    table->field[3]->store(bt_num_blocks_in_use, false);
    table->field[4]->store(bt_size_allocated, false);
    table->field[5]->store(bt_size_in_use, false);

    error = schema_table_store_record(thd, table);

exit:
    return error;
}

static int tokudb_fractal_tree_info(TABLE *table, THD *thd) {
    int error;
    DB_TXN* txn = NULL;
    DBC* tmp_cursor = NULL;
    DBT curr_key;
    DBT curr_val;
    memset(&curr_key, 0, sizeof curr_key);
    memset(&curr_val, 0, sizeof curr_val);
    error = db_env->txn_begin(db_env, 0, &txn, DB_READ_UNCOMMITTED);
    if (error) {
        goto cleanup;
    }
    error = db_env->get_cursor_for_directory(db_env, txn, &tmp_cursor);
    if (error) {
        goto cleanup;
    }
    while (error == 0) {
        error = tmp_cursor->c_get(
            tmp_cursor,
            &curr_key,
            &curr_val,
            DB_NEXT
            );
        if (!error) {
            error = tokudb_report_fractal_tree_info_for_db(&curr_key, &curr_val, table, thd);
        }
    }
    if (error == DB_NOTFOUND) {
        error = 0;
    }
cleanup:
    if (tmp_cursor) {
        int r = tmp_cursor->c_close(tmp_cursor);
        assert(r==0);
    }
    if (txn) {
        commit_txn(txn, 0);
    }
    return error;
}

struct tokudb_report_fractal_tree_block_map_iterator_extra {
    int64_t num_rows;
    int64_t i;
    uint64_t *checkpoint_counts;
    int64_t *blocknums;
    int64_t *diskoffs;
    int64_t *sizes;
};

// This iterator is called while holding the blocktable lock.  We should be as quick as possible.
// We don't want to do one call to get the number of rows, release the blocktable lock, and then do another call to get all the rows because the number of rows may change if we don't hold the lock.
// As a compromise, we'll do some mallocs inside the lock on the first call, but everything else should be fast.
static int tokudb_report_fractal_tree_block_map_iterator(uint64_t checkpoint_count,
                                                          int64_t num_rows,
                                                          int64_t blocknum,
                                                          int64_t diskoff,
                                                          int64_t size,
                                                          void *iter_extra) {
    struct tokudb_report_fractal_tree_block_map_iterator_extra *e = static_cast<struct tokudb_report_fractal_tree_block_map_iterator_extra *>(iter_extra);

    assert(num_rows > 0);
    if (e->num_rows == 0) {
        e->checkpoint_counts = (uint64_t *) my_malloc(num_rows * (sizeof *e->checkpoint_counts), MYF(MY_WME|MY_ZEROFILL|MY_FAE));
        e->blocknums = (int64_t *) my_malloc(num_rows * (sizeof *e->blocknums), MYF(MY_WME|MY_ZEROFILL|MY_FAE));
        e->diskoffs = (int64_t *) my_malloc(num_rows * (sizeof *e->diskoffs), MYF(MY_WME|MY_ZEROFILL|MY_FAE));
        e->sizes = (int64_t *) my_malloc(num_rows * (sizeof *e->sizes), MYF(MY_WME|MY_ZEROFILL|MY_FAE));
        e->num_rows = num_rows;
    }

    e->checkpoint_counts[e->i] = checkpoint_count;
    e->blocknums[e->i] = blocknum;
    e->diskoffs[e->i] = diskoff;
    e->sizes[e->i] = size;
    ++(e->i);

    return 0;
}

static int tokudb_report_fractal_tree_block_map_for_db(const DBT *dname, const DBT *iname, TABLE *table, THD *thd) {
    int error;
    DB *db;
    struct tokudb_report_fractal_tree_block_map_iterator_extra e = {
        .num_rows = 0,
        .i = 0,
        .checkpoint_counts = NULL,
        .blocknums = NULL,
        .diskoffs = NULL,
        .sizes = NULL,
    };

    error = db_create(&db, db_env, 0);
    if (error) {
        goto exit;
    }
    error = db->open(db, NULL, (char *)dname->data, NULL, DB_BTREE, 0, 0666);
    if (error) {
        goto exit;
    }
    error = db->iterate_fractal_tree_block_map(db, tokudb_report_fractal_tree_block_map_iterator, &e);
    {
        int close_error = db->close(db, 0);
        if (!error) {
            error = close_error;
        }
    }
    if (error) {
        goto exit;
    }

    // If not, we should have gotten an error and skipped this section of code
    assert(e.i == e.num_rows);
    for (int64_t i = 0; error == 0 && i < e.num_rows; ++i) {
1331 1332 1333
        // We store the NULL terminator in the directory so it's included in the size.
        // See #5789
        // Recalculate and check just to be safe.
1334 1335
        size_t dname_len = strlen((const char *)dname->data);
        size_t iname_len = strlen((const char *)iname->data);
1336 1337
        assert(dname_len == dname->size - 1);
        assert(iname_len == iname->size - 1);
1338 1339
        table->field[0]->store(
            (char *)dname->data,
1340
            dname_len,
1341 1342 1343 1344
            system_charset_info
            );
        table->field[1]->store(
            (char *)iname->data,
1345
            iname_len,
1346 1347 1348 1349 1350 1351 1352 1353 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 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
            system_charset_info
            );
        table->field[2]->store(e.checkpoint_counts[i], false);
        table->field[3]->store(e.blocknums[i], false);
        static const int64_t freelist_null = -1;
        static const int64_t diskoff_unused = -2;
        if (e.diskoffs[i] == diskoff_unused || e.diskoffs[i] == freelist_null) {
            table->field[4]->set_null();
        } else {
            table->field[4]->set_notnull();
            table->field[4]->store(e.diskoffs[i], false);
        }
        static const int64_t size_is_free = -1;
        if (e.sizes[i] == size_is_free) {
            table->field[5]->set_null();
        } else {
            table->field[5]->set_notnull();
            table->field[5]->store(e.sizes[i], false);
        }

        error = schema_table_store_record(thd, table);
    }

exit:
    if (e.checkpoint_counts != NULL) {
        my_free(e.checkpoint_counts, MYF(0));
        e.checkpoint_counts = NULL;
    }
    if (e.blocknums != NULL) {
        my_free(e.blocknums, MYF(0));
        e.blocknums = NULL;
    }
    if (e.diskoffs != NULL) {
        my_free(e.diskoffs, MYF(0));
        e.diskoffs = NULL;
    }
    if (e.sizes != NULL) {
        my_free(e.sizes, MYF(0));
        e.sizes = NULL;
    }
    return error;
}

static int tokudb_fractal_tree_block_map(TABLE *table, THD *thd) {
    int error;
    DB_TXN* txn = NULL;
    DBC* tmp_cursor = NULL;
    DBT curr_key;
    DBT curr_val;
    memset(&curr_key, 0, sizeof curr_key);
    memset(&curr_val, 0, sizeof curr_val);
    error = db_env->txn_begin(db_env, 0, &txn, DB_READ_UNCOMMITTED);
    if (error) {
        goto cleanup;
    }
    error = db_env->get_cursor_for_directory(db_env, txn, &tmp_cursor);
    if (error) {
        goto cleanup;
    }
    while (error == 0) {
        error = tmp_cursor->c_get(
            tmp_cursor,
            &curr_key,
            &curr_val,
            DB_NEXT
            );
        if (!error) {
            error = tokudb_report_fractal_tree_block_map_for_db(&curr_key, &curr_val, table, thd);
        }
    }
    if (error == DB_NOTFOUND) {
        error = 0;
    }
cleanup:
    if (tmp_cursor) {
        int r = tmp_cursor->c_close(tmp_cursor);
        assert(r==0);
    }
    if (txn) {
        commit_txn(txn, 0);
    }
    return error;
}

1430
static int tokudb_get_user_data_size(TABLE *table, THD *thd, bool exact) {
1431 1432 1433 1434
    int error;
    DB* curr_db = NULL;
    DB_TXN* txn = NULL;
    DBC* tmp_cursor = NULL;
1435
    DBC* tmp_table_cursor = NULL;
1436 1437
    DBT curr_key;
    DBT curr_val;
1438
    DB_TXN* tmp_txn = NULL;
1439 1440 1441 1442
    memset(&curr_key, 0, sizeof curr_key); 
    memset(&curr_val, 0, sizeof curr_val);
    pthread_mutex_lock(&tokudb_meta_mutex);

1443
    error = db_env->txn_begin(db_env, 0, &txn, DB_READ_UNCOMMITTED);
1444 1445 1446 1447 1448 1449 1450 1451
    if (error) {
        goto cleanup;
    }
    error = metadata_db->cursor(metadata_db, txn, &tmp_cursor, 0);
    if (error) {
        goto cleanup;
    }
    while (error == 0) {
1452
        tmp_txn = NULL;
1453 1454 1455 1456 1457 1458 1459
        //
        // 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;
        }
1460 1461 1462 1463 1464
        error = db_env->txn_begin(db_env, 0, &tmp_txn, DB_READ_UNCOMMITTED);
        if (error) {
            goto cleanup;
        }

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
        //
        // 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;
1476
            char* newname;
Yoni Fogel's avatar
Yoni Fogel committed
1477
            uint64_t curr_num_bytes = 0;
1478 1479
            DB_BTREE_STAT64 dict_stats;

1480 1481 1482
            error = db_create(&curr_db, db_env, 0);
            if (error) { goto cleanup; }

1483 1484
            newname = (char *)my_malloc(
                get_max_dict_name_path_length(name),
1485
                MYF(MY_WME|MY_ZEROFILL|MY_FAE));
1486 1487 1488

            make_name(newname, name, "main");
            
1489
            error = curr_db->open(curr_db, tmp_txn, newname, NULL, DB_BTREE, DB_THREAD, 0);
1490 1491 1492

            my_free(newname, MYF(0));

1493
            if (error == ENOENT) { error = 0; continue; }
1494 1495
            if (error) { goto cleanup; }

1496 1497 1498 1499 1500
            if (exact) {
                //
                // flatten if exact is required
                //
                uint curr_num_items = 0;                
1501
                error = curr_db->cursor(curr_db, tmp_txn, &tmp_table_cursor, 0);
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
                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;
                    }
1518 1519 1520
                }
                error = tmp_table_cursor->c_close(tmp_table_cursor);
                assert(error==0);
1521 1522 1523
                tmp_table_cursor = NULL;
            }

1524 1525
            error = curr_db->stat64(
                curr_db, 
1526
                tmp_txn, 
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
                &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
                //
Yoni Fogel's avatar
Yoni Fogel committed
1537
                uint64_t hpk_space = TOKUDB_HIDDEN_PRIMARY_KEY_LENGTH*dict_stats.bt_ndata;
1538 1539 1540
                curr_num_bytes = (hpk_space > curr_num_bytes) ? 0 : curr_num_bytes - hpk_space;
            }
            else {
1541 1542 1543
                //
                // one infinity byte per key needs to be subtracted
                //
Yoni Fogel's avatar
Yoni Fogel committed
1544
                uint64_t inf_byte_space = dict_stats.bt_ndata;
1545 1546 1547
                curr_num_bytes = (inf_byte_space > curr_num_bytes) ? 0 : curr_num_bytes - inf_byte_space;
            }

1548
            error = store_dbname_tablename_size(table, name, curr_num_bytes, thd);
1549
            if (error) goto cleanup;
1550

1551 1552 1553 1554 1555
            {
                int r = curr_db->close(curr_db, 0);
                assert(r==0);
                curr_db = NULL;
            }
1556
        }
1557 1558 1559 1560 1561

        if (tmp_txn) {
            commit_txn(tmp_txn, 0);
            tmp_txn = NULL;
        }
1562 1563 1564 1565 1566 1567
    }

    error = 0;

cleanup:
    if (tmp_cursor) {
1568 1569
        int r = tmp_cursor->c_close(tmp_cursor);
        assert(r==0);
1570
    }
1571
    if (tmp_table_cursor) {
1572
        int r = tmp_table_cursor->c_close(tmp_table_cursor);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1573
        assert(r==0);
1574
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1575 1576 1577 1578
    if (curr_db) {
        int r = curr_db->close(curr_db, 0);
        assert(r==0);
    }
1579 1580 1581
    if (tmp_txn) {
        commit_txn(tmp_txn, 0);
    }
1582
    if (txn) {
1583
        commit_txn(txn, 0);
1584 1585
    }
    if (error) {
1586
        sql_print_error("got an error %d in show_data_size\n", error);
1587 1588
    }
    pthread_mutex_unlock(&tokudb_meta_mutex);
1589 1590 1591
    return error;
}

1592
#define STATPRINT(legend, val) if (legend != NULL && val != NULL) stat_print(thd,   \
1593
                                          tokudb_hton_name, \
1594
                                          strlen(tokudb_hton_name), \
1595 1596 1597 1598 1599
                                          legend, \
                                          strlen(legend), \
                                          val, \
                                          strlen(val))

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

1602 1603 1604
static bool tokudb_show_engine_status(THD * thd, stat_print_fn * stat_print) {
    TOKUDB_DBUG_ENTER("tokudb_show_engine_status");
    int error;
1605 1606 1607 1608 1609
    uint64_t panic;
    const int panic_string_len = 1024;
    char panic_string[panic_string_len] = {'\0'};
    uint64_t num_rows;
    fs_redzone_state redzone_state;
1610
    const int bufsiz = 1024;
1611
    char buf[bufsiz];
1612

1613
#if MYSQL_VERSION_ID < 50500
1614
    {
1615 1616 1617
        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);
1618
    }
1619
#endif
1620 1621 1622 1623 1624
    error = db_env->get_engine_status_num_rows (db_env, &num_rows);
    TOKU_ENGINE_STATUS_ROW_S mystat[num_rows];
    error = db_env->get_engine_status (db_env, mystat, num_rows, &redzone_state, &panic, panic_string, panic_string_len);

    if (strlen(panic_string)) {
1625
        STATPRINT("Environment panic string", panic_string);
1626
    }
1627
    if (error == 0) {
1628 1629 1630 1631
        if (panic) {
            snprintf(buf, bufsiz, "%" PRIu64, panic);
            STATPRINT("Environment panic", buf);
        }
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
        
        if(redzone_state == FS_BLOCKED) {
            STATPRINT("*** URGENT WARNING ***", "FILE SYSTEM IS COMPLETELY FULL");
            snprintf(buf, bufsiz, "FILE SYSTEM IS COMPLETELY FULL");
        }
        else if (redzone_state == FS_GREEN) {
            snprintf(buf, bufsiz, "more than %d percent of total file system space", 2*tokudb_fs_reserve_percent);
        }
        else if (redzone_state == FS_YELLOW) {
            snprintf(buf, bufsiz, "*** WARNING *** FILE SYSTEM IS GETTING FULL (less than %d percent free)", 2*tokudb_fs_reserve_percent);
        } 
        else if (redzone_state == FS_RED){
            snprintf(buf, bufsiz, "*** WARNING *** FILE SYSTEM IS GETTING VERY FULL (less than %d percent free): INSERTS ARE PROHIBITED", tokudb_fs_reserve_percent);
        }
        else {
            snprintf(buf, bufsiz, "information unavailable, unknown redzone state %d", redzone_state);
        }
        STATPRINT ("disk free space", buf);

        for (uint64_t row = 0; row < num_rows; row++) {
            switch (mystat[row].type) {
            case FS_STATE:
                snprintf(buf, bufsiz, "%"PRIu64"", mystat[row].value.num);
                break;
            case UINT64:
                snprintf(buf, bufsiz, "%"PRIu64"", mystat[row].value.num);
                break;
            case CHARSTR:
                snprintf(buf, bufsiz, "%s", mystat[row].value.str);
                break;
            case UNIXTIME:
                {
1664
                    time_t t = mystat[row].value.num;
1665
                    char tbuf[26];
1666
                    snprintf(buf, bufsiz, "%.24s", ctime_r(&t, tbuf));
1667 1668 1669 1670 1671
                }
                break;
            case TOKUTIME:
                {
                    double t = tokutime_to_seconds(mystat[row].value.num);
1672
                    snprintf(buf, bufsiz, "%.6f", t);
1673 1674
                }
                break;
1675 1676 1677
            case PARCOUNT:
                {
                    uint64_t v = read_partitioned_counter(mystat[row].value.parcount);
1678
                    snprintf(buf, bufsiz, "%" PRIu64, v);
1679 1680
                }
                break;
1681
            default:
1682
                snprintf(buf, bufsiz, "UNKNOWN STATUS TYPE: %d", mystat[row].type);
1683 1684 1685
                break;                
            }
            STATPRINT(mystat[row].legend, buf);
1686 1687 1688 1689
        }
        uint64_t bytes_inserted = read_partitioned_counter(tokudb_primary_key_bytes_inserted);
        snprintf(buf, bufsiz, "%" PRIu64, bytes_inserted);
        STATPRINT("handlerton: primary key bytes inserted", buf);
1690
    }  
1691 1692 1693 1694
    if (error) { my_errno = error; }
    TOKUDB_DBUG_RETURN(error);
}

1695
static void tokudb_checkpoint_lock(THD * thd) {
1696 1697
    int error;
    tokudb_trx_data* trx = NULL;
1698
    char status_msg[200]; //buffer of 200 should be a good upper bound.
1699 1700
    trx = (tokudb_trx_data *) thd_data_get(thd, tokudb_hton->slot);
    if (!trx) {
1701
        error = create_tokudb_trx_data_instance(&trx);
1702 1703 1704 1705
        //
        // can only fail due to memory allocation, so ok to assert
        //
        assert(!error);
1706 1707 1708 1709 1710 1711
        thd_data_set(thd, tokudb_hton->slot, trx);
    }
    
    if (trx->checkpoint_lock_taken) {
        goto cleanup;
    }
1712 1713 1714 1715
    //
    // This can only fail if environment is not created, which is not possible
    // in handlerton
    //
1716 1717
    sprintf(status_msg, "Trying to grab checkpointing lock.");
    thd_proc_info(thd, status_msg);
1718
    error = db_env->checkpointing_postpone(db_env);
1719
    assert(!error);
1720 1721 1722

    trx->checkpoint_lock_taken = true;
cleanup:
1723
    return;
1724 1725
}

1726
static void tokudb_checkpoint_unlock(THD * thd) {
1727
    int error;
1728
    char status_msg[200]; //buffer of 200 should be a good upper bound.
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
    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
    //
1742 1743
    sprintf(status_msg, "Trying to release checkpointing lock.");
    thd_proc_info(thd, status_msg);
1744
    error = db_env->checkpointing_resume(db_env);
1745
    assert(!error);
1746 1747 1748 1749

    trx->checkpoint_lock_taken = false;
    
cleanup:
1750
    return;
1751 1752
}

1753
static bool tokudb_show_status(handlerton * hton, THD * thd, stat_print_fn * stat_print, enum ha_stat_type stat_type) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1754
    switch (stat_type) {
1755 1756 1757
    case HA_ENGINE_STATUS:
        return tokudb_show_engine_status(thd, stat_print);
        break;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1758
    default:
1759
        break;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1760
    }
Yoni Fogel's avatar
Yoni Fogel committed
1761
    return false;
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1762 1763 1764 1765 1766 1767
}

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);
}

1768
static void tokudb_cleanup_log_files(void) {
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
    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
        }

1793
        free(names);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1794 1795 1796 1797 1798
    }

    DBUG_VOID_RETURN;
}

1799
#if defined(HA_GENERAL_ONLINE)
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1800 1801 1802 1803 1804 1805 1806 1807 1808
//
// *******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 |
1809
            HA_ONLINE_ADD_UNIQUE_INDEX_NO_WRITES| HA_ONLINE_DROP_UNIQUE_INDEX_NO_WRITES|HA_GENERAL_ONLINE);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1810 1811

}
1812 1813 1814 1815
#elif defined(HA_INPLACE_ADD_INDEX_NO_READ_WRITE)
static uint tokudb_alter_table_flags(uint flags) {
    return HA_INPLACE_ADD_INDEX_NO_READ_WRITE
        |  HA_INPLACE_ADD_INDEX_NO_WRITE
1816
        |  HA_INPLACE_DROP_INDEX_NO_READ_WRITE
1817 1818 1819 1820
        |  HA_INPLACE_ADD_UNIQUE_INDEX_NO_READ_WRITE
        |  HA_INPLACE_ADD_UNIQUE_INDEX_NO_WRITE
        |  HA_INPLACE_DROP_UNIQUE_INDEX_NO_READ_WRITE;
}
1821
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836


// 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

1837 1838 1839 1840 1841 1842 1843
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;
1844
    db_env->set_lock_timeout(db_env, *timeout);
1845
}
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1846

1847
#define DEFAULT_LOCK_TIMEOUT_MSEC 4000
1848

1849 1850
static MYSQL_SYSVAR_ULONGLONG(lock_timeout, tokudb_lock_timeout,
        0, "TokuDB lock timeout", 
1851
        NULL, tokudb_lock_timeout_update, DEFAULT_LOCK_TIMEOUT_MSEC,
1852
        0, ~0ULL, 0);
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869

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,
1870
        0, ~0UL, 0);
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882

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);
}

1883
#define DEFAULT_CLEANER_ITERATIONS 5
1884

1885

1886
static MYSQL_SYSVAR_BOOL(directio, tokudb_directio,
1887 1888 1889 1890
  PLUGIN_VAR_READONLY,
  "TokuDB Enable Direct I/O ",
  NULL, NULL, FALSE);

1891 1892 1893
static MYSQL_SYSVAR_ULONG(cleaner_iterations, tokudb_cleaner_iterations,
        0, "TokuDB cleaner_iterations", 
        NULL, tokudb_cleaner_iterations_update, DEFAULT_CLEANER_ITERATIONS,
1894
        0, ~0UL, 0);
1895

1896 1897
static MYSQL_SYSVAR_ULONGLONG(cache_size, tokudb_cache_size,
        PLUGIN_VAR_READONLY, "TokuDB cache table size", NULL, NULL, 0,
1898 1899 1900
        0, ~0ULL, 0);
static MYSQL_SYSVAR_ULONGLONG(max_lock_memory, tokudb_max_lock_memory, PLUGIN_VAR_READONLY, "TokuDB max memory for locks", NULL, NULL, 0, 0, ~0ULL, 0);
static MYSQL_SYSVAR_ULONG(debug, tokudb_debug, 0, "TokuDB Debug", NULL, NULL, 0, 0, ~0UL, 0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1901 1902 1903 1904 1905 1906 1907

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);

1908
static MYSQL_SYSVAR_UINT(init_flags, tokudb_init_flags, PLUGIN_VAR_READONLY, "Sets TokuDB DB_ENV->open flags", NULL, NULL, tokudb_init_flags, 0, ~0U, 0);
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1909

1910 1911 1912
static MYSQL_SYSVAR_UINT(checkpointing_period, tokudb_checkpointing_period, 0, "TokuDB Checkpointing period", NULL, NULL, 60, 0, ~0U, 0);
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, ~0U, 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, ~0U, 0);
1913
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);
1914
static MYSQL_SYSVAR_STR(tmp_dir, tokudb_tmp_dir, PLUGIN_VAR_READONLY, "Tokudb Tmp Dir", NULL, NULL, NULL);
1915

Zardosht Kasheff's avatar
Zardosht Kasheff committed
1916 1917
static struct st_mysql_sys_var *tokudb_system_variables[] = {
    MYSQL_SYSVAR(cache_size),
1918
    MYSQL_SYSVAR(max_lock_memory),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1919 1920 1921 1922
    MYSQL_SYSVAR(data_dir),
    MYSQL_SYSVAR(log_dir),
    MYSQL_SYSVAR(debug),
    MYSQL_SYSVAR(commit_sync),
1923
    MYSQL_SYSVAR(lock_timeout),
1924 1925
    MYSQL_SYSVAR(cleaner_period),
    MYSQL_SYSVAR(cleaner_iterations),
1926
    MYSQL_SYSVAR(pk_insert_mode),
1927
    MYSQL_SYSVAR(load_save_space),
1928
    MYSQL_SYSVAR(disable_slow_alter),
1929
    MYSQL_SYSVAR(disable_hot_alter),
1930
    MYSQL_SYSVAR(create_index_online),
1931
    MYSQL_SYSVAR(disable_prefetching),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1932 1933
    MYSQL_SYSVAR(version),
    MYSQL_SYSVAR(init_flags),
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1934
    MYSQL_SYSVAR(checkpointing_period),
1935
    MYSQL_SYSVAR(prelock_empty),
1936
    MYSQL_SYSVAR(log_client_errors),
1937
    MYSQL_SYSVAR(checkpoint_lock),
1938 1939
    MYSQL_SYSVAR(write_status_frequency),
    MYSQL_SYSVAR(read_status_frequency),
1940
    MYSQL_SYSVAR(fs_reserve_percent),
1941
    MYSQL_SYSVAR(tmp_dir),
1942
    MYSQL_SYSVAR(block_size),
1943
    MYSQL_SYSVAR(read_block_size),
1944
    MYSQL_SYSVAR(read_buf_size),
1945
    MYSQL_SYSVAR(row_format),
1946
    MYSQL_SYSVAR(directio),
1947 1948 1949 1950
#if TOKU_INCLUDE_UPSERT
    MYSQL_SYSVAR(disable_slow_update),
    MYSQL_SYSVAR(disable_slow_upsert),
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
1951 1952 1953
    NULL
};

1954 1955 1956
struct st_mysql_storage_engine tokudb_storage_engine = { MYSQL_HANDLERTON_INTERFACE_VERSION };

static ST_FIELD_INFO tokudb_user_data_field_info[] = {
1957 1958 1959
    {"database_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"table_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"data_size", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
1960
    {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, SKIP_OPEN_TABLE}
1961 1962
};

1963 1964 1965
#if MYSQL_VERSION_ID >= 50600
static int tokudb_user_data_fill_table(THD *thd, TABLE_LIST *tables, Item *cond) {
#else
1966
static int tokudb_user_data_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) {
1967
#endif
1968 1969
    int error;
    TABLE *table = tables->table;
1970 1971 1972 1973 1974 1975
    
    // 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) {
1976 1977 1978
        my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), "TokuDB");
        error = -1;
    } else {
1979
        error = tokudb_get_user_data_size(table, thd, false);
1980
    }
1981 1982 1983

    // 3938: unlock the status flag lock
    rw_unlock(&tokudb_hton_initialized_lock);
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
    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;
}

1998
static struct st_mysql_information_schema tokudb_user_data_information_schema = { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };
1999

2000 2001 2002 2003
static struct st_mysql_information_schema tokudb_fractal_tree_info_information_schema = { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };

static struct st_mysql_information_schema tokudb_fractal_tree_block_map_information_schema = { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };

2004
static ST_FIELD_INFO tokudb_user_data_exact_field_info[] = {
2005 2006 2007
    {"database_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"table_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"data_size", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
2008
    {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, SKIP_OPEN_TABLE}
2009 2010
};

2011
static ST_FIELD_INFO tokudb_dictionary_field_info[] = {
2012 2013
    {"dictionary_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"internal_file_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
2014 2015 2016
    {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, SKIP_OPEN_TABLE}
};

2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
static ST_FIELD_INFO tokudb_fractal_tree_info_field_info[] = {
    {"dictionary_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"internal_file_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"bt_num_blocks_allocated", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"bt_num_blocks_in_use", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"bt_size_allocated", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"bt_size_in_use", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
    {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, SKIP_OPEN_TABLE}
};

static ST_FIELD_INFO tokudb_fractal_tree_block_map_field_info[] = {
    {"dictionary_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"internal_file_name", 256, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"checkpoint_count", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"blocknum", 0, MYSQL_TYPE_LONGLONG, 0, 0, NULL, SKIP_OPEN_TABLE },
    {"offset", 0, MYSQL_TYPE_LONGLONG, 0, MY_I_S_MAYBE_NULL, NULL, SKIP_OPEN_TABLE },
    {"size", 0, MYSQL_TYPE_LONGLONG, 0, MY_I_S_MAYBE_NULL, NULL, SKIP_OPEN_TABLE },
    {NULL, 0, MYSQL_TYPE_NULL, 0, 0, NULL, SKIP_OPEN_TABLE}
};

2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

#if MYSQL_VERSION_ID >= 50600
static int tokudb_dictionary_info_fill_table(THD *thd, TABLE_LIST *tables, Item *cond) {
#else
static int tokudb_dictionary_info_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) {
#endif
    int error;
    TABLE *table = tables->table;

    // 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) {
        my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), "TokuDB");
        error = -1;
    } else {
        error = tokudb_dictionary_info(table, thd);
    }

    //3938: unlock the status flag lock
    rw_unlock(&tokudb_hton_initialized_lock);
    return error;
}

2062 2063 2064
#if MYSQL_VERSION_ID >= 50600
static int tokudb_user_data_exact_fill_table(THD *thd, TABLE_LIST *tables, Item *cond) {
#else
2065
static int tokudb_user_data_exact_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) {
2066
#endif
2067 2068
    int error;
    TABLE *table = tables->table;
2069 2070 2071 2072 2073 2074

    // 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) {
2075 2076 2077
        my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), "TokuDB");
        error = -1;
    } else {
2078
        error = tokudb_get_user_data_size(table, thd, true);
2079
    }
2080 2081 2082

    //3938: unlock the status flag lock
    rw_unlock(&tokudb_hton_initialized_lock);
2083 2084 2085
    return error;
}

2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
#if MYSQL_VERSION_ID >= 50600
static int tokudb_fractal_tree_info_fill_table(THD *thd, TABLE_LIST *tables, Item *cond) {
#else
static int tokudb_fractal_tree_info_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) {
#endif
    int error;
    TABLE *table = tables->table;

    // 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) {
        my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), "TokuDB");
        error = -1;
    } else {
        error = tokudb_fractal_tree_info(table, thd);
    }

    //3938: unlock the status flag lock
    rw_unlock(&tokudb_hton_initialized_lock);
    return error;
}

#if MYSQL_VERSION_ID >= 50600
static int tokudb_fractal_tree_block_map_fill_table(THD *thd, TABLE_LIST *tables, Item *cond) {
#else
static int tokudb_fractal_tree_block_map_fill_table(THD *thd, TABLE_LIST *tables, COND *cond) {
#endif
    int error;
    TABLE *table = tables->table;

    // 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) {
        my_error(ER_PLUGIN_IS_NOT_LOADED, MYF(0), "TokuDB");
        error = -1;
    } else {
        error = tokudb_fractal_tree_block_map(table, thd);
    }

    //3938: unlock the status flag lock
    rw_unlock(&tokudb_hton_initialized_lock);
    return error;
}

2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
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;
}

2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
static int tokudb_dictionary_info_init(void *p) {
    ST_SCHEMA_TABLE *schema = (ST_SCHEMA_TABLE *) p;
    schema->fields_info = tokudb_dictionary_field_info;
    schema->fill_table = tokudb_dictionary_info_fill_table;
    return 0;
}

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

2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177
static int tokudb_fractal_tree_info_init(void *p) {
    ST_SCHEMA_TABLE *schema = (ST_SCHEMA_TABLE *) p;
    schema->fields_info = tokudb_fractal_tree_info_field_info;
    schema->fill_table = tokudb_fractal_tree_info_fill_table;
    return 0;
}

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

static int tokudb_fractal_tree_block_map_init(void *p) {
    ST_SCHEMA_TABLE *schema = (ST_SCHEMA_TABLE *) p;
    schema->fields_info = tokudb_fractal_tree_block_map_field_info;
    schema->fill_table = tokudb_fractal_tree_block_map_fill_table;
    return 0;
}

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

2178
static struct st_mysql_information_schema tokudb_user_data_exact_information_schema = { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION };
2179 2180

enum { TOKUDB_PLUGIN_VERSION = 0x0400 };
2181
#define TOKUDB_PLUGIN_VERSION_STR "1024"
2182 2183 2184

mysql_declare_plugin(tokudb) 
{
Zardosht Kasheff's avatar
Zardosht Kasheff committed
2185
    MYSQL_STORAGE_ENGINE_PLUGIN, 
2186
    &tokudb_storage_engine, 
2187
    tokudb_hton_name, 
Zardosht Kasheff's avatar
Zardosht Kasheff committed
2188
    "Tokutek Inc", 
2189
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
2190
    PLUGIN_LICENSE_PROPRIETARY,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
2191 2192
    tokudb_init_func,          /* plugin init */
    tokudb_done_func,          /* plugin deinit */
2193
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
Zardosht Kasheff's avatar
Zardosht Kasheff committed
2194 2195
    NULL,                      /* status variables */
    tokudb_system_variables,   /* system variables */
2196 2197 2198 2199
    NULL,                      /* config options */
#if MYSQL_VERSION_ID >= 50521
    0,                         /* flags */
#endif
2200 2201 2202 2203 2204 2205 2206
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_user_data_information_schema, 
    "TokuDB_user_data", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
2207
    PLUGIN_LICENSE_PROPRIETARY,
2208 2209 2210 2211 2212
    tokudb_user_data_init,     /* plugin init */
    tokudb_user_data_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
2213 2214 2215 2216
    NULL,                      /* config options */
#if MYSQL_VERSION_ID >= 50521
    0,                         /* flags */
#endif
2217 2218 2219 2220 2221 2222 2223
},
{
    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",
2224
    PLUGIN_LICENSE_PROPRIETARY,
2225 2226 2227 2228 2229
    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 */
2230 2231 2232 2233
    NULL,                      /* config options */
#if MYSQL_VERSION_ID >= 50521
    0,                         /* flags */
#endif
2234 2235 2236 2237
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_user_data_exact_information_schema, 
2238
    "TokuDB_file_map", 
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_PROPRIETARY,
    tokudb_dictionary_info_init,     /* plugin init */
    tokudb_dictionary_info_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    NULL,                      /* config options */
#if MYSQL_VERSION_ID >= 50521
    0,                         /* flags */
#endif
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_fractal_tree_info_information_schema, 
    "TokuDB_fractal_tree_info", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_PROPRIETARY,
    tokudb_fractal_tree_info_init,     /* plugin init */
    tokudb_fractal_tree_info_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    NULL,                      /* config options */
#if MYSQL_VERSION_ID >= 50521
    0,                         /* flags */
#endif
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_fractal_tree_block_map_information_schema, 
    "TokuDB_fractal_tree_block_map", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_PROPRIETARY,
    tokudb_fractal_tree_block_map_init,     /* plugin init */
    tokudb_fractal_tree_block_map_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    NULL,                      /* config options */
#if MYSQL_VERSION_ID >= 50521
    0,                         /* flags */
#endif
Zardosht Kasheff's avatar
Zardosht Kasheff committed
2285 2286 2287
}
mysql_declare_plugin_end;

2288 2289 2290 2291 2292 2293
#ifdef MARIA_PLUGIN_INTERFACE_VERSION

maria_declare_plugin(tokudb) 
{
    MYSQL_STORAGE_ENGINE_PLUGIN, 
    &tokudb_storage_engine, 
2294
    tokudb_hton_name, 
2295 2296
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
2297
    PLUGIN_LICENSE_PROPRIETARY,
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
    tokudb_init_func,          /* plugin init */
    tokudb_done_func,          /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    tokudb_system_variables,   /* system variables */
    TOKUDB_PLUGIN_VERSION_STR, /* string version */
    MariaDB_PLUGIN_MATURITY_STABLE /* maturity */
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_user_data_information_schema, 
    "TokuDB_user_data", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
2312
    PLUGIN_LICENSE_PROPRIETARY,
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
    tokudb_user_data_init,     /* plugin init */
    tokudb_user_data_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    TOKUDB_PLUGIN_VERSION_STR, /* string version */
    MariaDB_PLUGIN_MATURITY_STABLE /* maturity */
},
{
    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",
2327
    PLUGIN_LICENSE_PROPRIETARY,
2328 2329 2330 2331 2332 2333 2334
    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 */
    TOKUDB_PLUGIN_VERSION_STR, /* string version */
    MariaDB_PLUGIN_MATURITY_STABLE /* maturity */
2335 2336 2337 2338
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_user_data_exact_information_schema, 
2339
    "TokuDB_file_map", 
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_PROPRIETARY,
    tokudb_dictionary_info_init,     /* plugin init */
    tokudb_dictionary_info_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    TOKUDB_PLUGIN_VERSION_STR, /* string version */
    MariaDB_PLUGIN_MATURITY_STABLE /* maturity */
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_fractal_tree_info_information_schema, 
    "TokuDB_fractal_tree_info", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_PROPRIETARY,
    tokudb_fractal_tree_info_init,     /* plugin init */
    tokudb_fractal_tree_info_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    TOKUDB_PLUGIN_VERSION_STR, /* string version */
    MariaDB_PLUGIN_MATURITY_STABLE /* maturity */
},
{
    MYSQL_INFORMATION_SCHEMA_PLUGIN, 
    &tokudb_fractal_tree_block_map_information_schema, 
    "TokuDB_fractal_tree_block_map", 
    "Tokutek Inc", 
    "Tokutek TokuDB Storage Engine with Fractal Tree(tm) Technology",
    PLUGIN_LICENSE_PROPRIETARY,
    tokudb_fractal_tree_block_map_init,     /* plugin init */
    tokudb_fractal_tree_block_map_done,     /* plugin deinit */
    TOKUDB_PLUGIN_VERSION,     /* 4.0.0 */
    NULL,                      /* status variables */
    NULL,                      /* system variables */
    TOKUDB_PLUGIN_VERSION_STR, /* string version */
    MariaDB_PLUGIN_MATURITY_STABLE /* maturity */
2380 2381 2382 2383
}
maria_declare_plugin_end;

#endif