This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
forked from facebookarchive/webscalesql-5.6
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ha_rocksdb.cc
3792 lines (3166 loc) · 107 KB
/
ha_rocksdb.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012, Monty Program Ab
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
#include <mysql/plugin.h>
#include "ha_rocksdb.h"
#include "sql_class.h"
#include "sql_array.h"
#include "my_bit.h"
#include <sstream>
#include "rdb_datadic.h"
#include "rdb_locks.h"
#include "rdb_rowmods.h"
#include "rdb_cf_options.h"
#include "rdb_cf_manager.h"
#include "rocksdb/table.h"
#include "rocksdb/utilities/convenience.h"
/* This is here to get PRIu64, PRId64 */
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
void dbug_dump_database(rocksdb::DB *db);
static handler *rocksdb_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root);
void key_copy(uchar *to_key, uchar *from_record, KEY *key_info,
uint key_length);
///////////////////////////////////////////////////////////
// Parameters and settings
///////////////////////////////////////////////////////////
extern const longlong ROCKSDB_WRITE_BUFFER_SIZE_DEFAULT;
static char * rocksdb_default_cf_options;
static char * rocksdb_cf_options_file;
Cf_options rocksdb_cf_options_map;
///////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////
handlerton *rocksdb_hton;
rocksdb::DB *rdb= NULL;
static std::shared_ptr<rocksdb::Statistics> rocksdb_stats;
rocksdb::ColumnFamilyOptions default_cf_opts;
Column_family_manager cf_manager;
Table_ddl_manager ddl_manager;
LockTable row_locks;
/*
Hash used to track the number of open tables; variable for example share
methods
*/
static HASH rocksdb_open_tables;
/* The mutex used to init the hash; variable for example share methods */
mysql_mutex_t rocksdb_mutex;
//////////////////////////////////////////////////////////////////////////////
// Options parse support functions
//////////////////////////////////////////////////////////////////////////////
static int
rocksdb_cf_options_file_validate(THD* thd,
struct st_mysql_sys_var* var,
void* save,
struct st_mysql_value* value)
{
/* The option is read-only, it should never be updated */
DBUG_ASSERT(0);
return 1;
}
static void
rocksdb_cf_options_file_update(THD* thd,
struct st_mysql_sys_var* var,
void* var_ptr,
const void* save)
{
/* The option is read-only, it should never be updated */
DBUG_ASSERT(0);
}
//////////////////////////////////////////////////////////////////////////////
// Options definitions
//////////////////////////////////////////////////////////////////////////////
static long long rocksdb_block_cache_size;
static uint64_t rocksdb_info_log_level;
static uint64_t rocksdb_index_type;
static uint64_t rocksdb_write_sync;
static rocksdb::WriteOptions rocksdb_write_options;
static rocksdb::DBOptions init_db_options() {
rocksdb::DBOptions o;
o.create_if_missing = true;
return o;
}
static rocksdb::DBOptions db_options = init_db_options();
static rocksdb::BlockBasedTableOptions table_options;
static const char* info_log_level_names[] = {
"debug_level",
"info_level",
"warn_level",
"error_level",
"fatal_level",
NullS
};
static TYPELIB info_log_level_typelib = {
array_elements(info_log_level_names) - 1,
"info_log_level_typelib",
info_log_level_names,
nullptr
};
static const char* index_type_names[] = {
"kBinarySearch",
"kHashSearch",
NullS
};
static TYPELIB index_type_typelib = {
array_elements(index_type_names) - 1,
"index_type_typelib",
index_type_names,
nullptr
};
enum write_sync_options {
WRITE_SYNC_OFF,
WRITE_SYNC_ON_COMMIT,
WRITE_SYNC_BACKGROUND
};
static const char* write_sync_names[] = {
"off",
"on_commit",
"background",
NullS
};
static TYPELIB write_sync_typelib = {
array_elements(write_sync_names) - 1,
"write_sync_typelib",
write_sync_names,
nullptr
};
//TODO: 0 means don't wait at all, and we don't support it yet?
static MYSQL_THDVAR_ULONG(lock_wait_timeout, PLUGIN_VAR_RQCMDARG,
"Number of seconds to wait for lock",
NULL, NULL, /*default*/ 1, /*min*/ 1, /*max*/ 1024*1024*1024, 0);
static MYSQL_THDVAR_BOOL(bulk_load, PLUGIN_VAR_RQCMDARG,
"Use bulk-load mode for inserts", NULL, NULL, FALSE);
static MYSQL_THDVAR_ULONG(max_row_locks, PLUGIN_VAR_RQCMDARG,
"Maximum number of locks a transaction can have",
NULL, NULL, /*default*/ 1024*1024*1024, /*min*/ 1, /*max*/ 1024*1024*1024, 0);
static MYSQL_THDVAR_ULONG(bulk_load_size, PLUGIN_VAR_RQCMDARG,
"Max #records in a batch for bulk-load mode",
NULL, NULL, /*default*/ 1000, /*min*/ 1, /*max*/ 1024*1024*1024, 0);
static MYSQL_SYSVAR_BOOL(create_if_missing,
*reinterpret_cast<my_bool*>(&db_options.create_if_missing),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::create_if_missing for RocksDB",
NULL, NULL, db_options.create_if_missing);
static MYSQL_SYSVAR_BOOL(create_missing_column_families,
*reinterpret_cast<my_bool*>(&db_options.create_missing_column_families),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::create_missing_column_families for RocksDB",
NULL, NULL, db_options.create_missing_column_families);
static MYSQL_SYSVAR_BOOL(error_if_exists,
*reinterpret_cast<my_bool*>(&db_options.error_if_exists),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::error_if_exists for RocksDB",
NULL, NULL, db_options.error_if_exists);
static MYSQL_SYSVAR_BOOL(paranoid_checks,
*reinterpret_cast<my_bool*>(&db_options.paranoid_checks),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::paranoid_checks for RocksDB",
NULL, NULL, db_options.paranoid_checks);
static MYSQL_SYSVAR_ENUM(info_log_level,
rocksdb_info_log_level,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::info_log_level for RocksDB",
NULL, NULL, db_options.info_log_level, &info_log_level_typelib);
static MYSQL_SYSVAR_INT(max_open_files,
db_options.max_open_files,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_open_files for RocksDB",
NULL, NULL, db_options.max_open_files,
/* min */ -1, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_ULONG(max_total_wal_size,
db_options.max_total_wal_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_total_wal_size for RocksDB",
NULL, NULL, db_options.max_total_wal_size,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(disableDataSync,
*reinterpret_cast<my_bool*>(&db_options.disableDataSync),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::disableDataSync for RocksDB",
NULL, NULL, db_options.disableDataSync);
static MYSQL_SYSVAR_BOOL(use_fsync,
*reinterpret_cast<my_bool*>(&db_options.use_fsync),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::use_fsync for RocksDB",
NULL, NULL, db_options.use_fsync);
static MYSQL_SYSVAR_ULONG(delete_obsolete_files_period_micros,
db_options.delete_obsolete_files_period_micros,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::delete_obsolete_files_period_micros for RocksDB",
NULL, NULL, db_options.delete_obsolete_files_period_micros,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_INT(max_background_compactions,
db_options.max_background_compactions,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_background_compactions for RocksDB",
NULL, NULL, db_options.max_background_compactions,
/* min */ 1, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_INT(max_background_flushes,
db_options.max_background_flushes,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_background_flushes for RocksDB",
NULL, NULL, db_options.max_background_flushes,
/* min */ 1, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_ULONG(max_log_file_size,
db_options.max_log_file_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_log_file_size for RocksDB",
NULL, NULL, db_options.max_log_file_size,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_ULONG(log_file_time_to_roll,
db_options.log_file_time_to_roll,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::log_file_time_to_roll for RocksDB",
NULL, NULL, db_options.log_file_time_to_roll,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_ULONG(keep_log_file_num,
db_options.keep_log_file_num,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::keep_log_file_num for RocksDB",
NULL, NULL, db_options.keep_log_file_num,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_ULONG(max_manifest_file_size,
db_options.max_manifest_file_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::max_manifest_file_size for RocksDB",
NULL, NULL, db_options.max_manifest_file_size,
/* min */ 0L, /* max */ ULONG_MAX, 0);
static MYSQL_SYSVAR_INT(table_cache_numshardbits,
db_options.table_cache_numshardbits,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::table_cache_numshardbits for RocksDB",
NULL, NULL, db_options.table_cache_numshardbits,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_INT(table_cache_remove_scan_count_limit,
db_options.table_cache_remove_scan_count_limit,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::table_cache_remove_scan_count_limit for RocksDB",
NULL, NULL, db_options.table_cache_remove_scan_count_limit,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_ULONG(WAL_ttl_seconds,
db_options.WAL_ttl_seconds,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::WAL_ttl_seconds for RocksDB",
NULL, NULL, db_options.WAL_ttl_seconds,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_ULONG(WAL_size_limit_MB,
db_options.WAL_size_limit_MB,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::WAL_size_limit_MB for RocksDB",
NULL, NULL, db_options.WAL_size_limit_MB,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_ULONG(manifest_preallocation_size,
db_options.manifest_preallocation_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::manifest_preallocation_size for RocksDB",
NULL, NULL, db_options.manifest_preallocation_size,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(allow_os_buffer,
*reinterpret_cast<my_bool*>(&db_options.allow_os_buffer),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::allow_os_buffer for RocksDB",
NULL, NULL, db_options.allow_os_buffer);
static MYSQL_SYSVAR_BOOL(allow_mmap_reads,
*reinterpret_cast<my_bool*>(&db_options.allow_mmap_reads),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::allow_mmap_reads for RocksDB",
NULL, NULL, db_options.allow_mmap_reads);
static MYSQL_SYSVAR_BOOL(allow_mmap_writes,
*reinterpret_cast<my_bool*>(&db_options.allow_mmap_writes),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::allow_mmap_writes for RocksDB",
NULL, NULL, db_options.allow_mmap_writes);
static MYSQL_SYSVAR_BOOL(is_fd_close_on_exec,
*reinterpret_cast<my_bool*>(&db_options.is_fd_close_on_exec),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::is_fd_close_on_exec for RocksDB",
NULL, NULL, db_options.is_fd_close_on_exec);
static MYSQL_SYSVAR_BOOL(skip_log_error_on_recovery,
*reinterpret_cast<my_bool*>(&db_options.skip_log_error_on_recovery),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::skip_log_error_on_recovery for RocksDB",
NULL, NULL, db_options.skip_log_error_on_recovery);
static MYSQL_SYSVAR_UINT(stats_dump_period_sec,
db_options.stats_dump_period_sec,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::stats_dump_period_sec for RocksDB",
NULL, NULL, db_options.stats_dump_period_sec,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_BOOL(advise_random_on_open,
*reinterpret_cast<my_bool*>(&db_options.advise_random_on_open),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::advise_random_on_open for RocksDB",
NULL, NULL, db_options.advise_random_on_open);
static MYSQL_SYSVAR_ULONG(db_write_buffer_size,
db_options.db_write_buffer_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::db_write_buffer_size for RocksDB",
NULL, NULL, db_options.db_write_buffer_size,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(use_adaptive_mutex,
*reinterpret_cast<my_bool*>(&db_options.use_adaptive_mutex),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::use_adaptive_mutex for RocksDB",
NULL, NULL, db_options.use_adaptive_mutex);
static MYSQL_SYSVAR_ULONG(bytes_per_sync,
db_options.bytes_per_sync,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::bytes_per_sync for RocksDB",
NULL, NULL, db_options.bytes_per_sync,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(enable_thread_tracking,
*reinterpret_cast<my_bool*>(&db_options.enable_thread_tracking),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"DBOptions::enable_thread_tracking for RocksDB",
NULL, NULL, db_options.enable_thread_tracking);
static MYSQL_SYSVAR_LONGLONG(block_cache_size, rocksdb_block_cache_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"block_cache size for RocksDB",
NULL, NULL, /* RocksDB's default is 8 MB: */ 8*1024*1024L,
/* min */ 1024L, /* max */ LONGLONG_MAX, /* Block size */1024L);
static MYSQL_SYSVAR_BOOL(cache_index_and_filter_blocks,
*reinterpret_cast<my_bool*>(&table_options.cache_index_and_filter_blocks),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::cache_index_and_filter_blocks for RocksDB",
NULL, NULL, table_options.cache_index_and_filter_blocks);
static MYSQL_SYSVAR_ENUM(index_type,
rocksdb_index_type,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::index_type for RocksDB",
NULL, NULL, (uint64_t)table_options.index_type, &index_type_typelib);
static MYSQL_SYSVAR_BOOL(hash_index_allow_collision,
*reinterpret_cast<my_bool*>(&table_options.hash_index_allow_collision),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::hash_index_allow_collision for RocksDB",
NULL, NULL, table_options.hash_index_allow_collision);
static MYSQL_SYSVAR_BOOL(no_block_cache,
*reinterpret_cast<my_bool*>(&table_options.no_block_cache),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::no_block_cache for RocksDB",
NULL, NULL, table_options.no_block_cache);
static MYSQL_SYSVAR_ULONG(block_size,
table_options.block_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::block_size for RocksDB",
NULL, NULL, table_options.block_size,
/* min */ 1L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_INT(block_size_deviation,
table_options.block_size_deviation,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::block_size_deviation for RocksDB",
NULL, NULL, table_options.block_size_deviation,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_INT(block_restart_interval,
table_options.block_restart_interval,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::block_restart_interval for RocksDB",
NULL, NULL, table_options.block_restart_interval,
/* min */ 0, /* max */ INT_MAX, 0);
static MYSQL_SYSVAR_BOOL(whole_key_filtering,
*reinterpret_cast<my_bool*>(&table_options.whole_key_filtering),
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"BlockBasedTableOptions::whole_key_filtering for RocksDB",
NULL, NULL, table_options.whole_key_filtering);
static MYSQL_SYSVAR_STR(default_cf_options, rocksdb_default_cf_options,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"default cf options for RocksDB",
NULL, NULL, "");
static MYSQL_SYSVAR_STR(cf_options_file, rocksdb_cf_options_file,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"path to cnf file with cf options for RocksDB",
rocksdb_cf_options_file_validate,
rocksdb_cf_options_file_update, "");
static MYSQL_SYSVAR_ENUM(write_sync,
rocksdb_write_sync,
PLUGIN_VAR_RQCMDARG,
"WriteOptions::write_sync for RocksDB",
NULL, NULL, WRITE_SYNC_OFF, &write_sync_typelib);
static MYSQL_SYSVAR_BOOL(write_disable_wal,
*reinterpret_cast<my_bool*>(&rocksdb_write_options.disableWAL),
PLUGIN_VAR_RQCMDARG,
"WriteOptions::disableWAL for RocksDB",
NULL, NULL, rocksdb_write_options.disableWAL);
static MYSQL_SYSVAR_ULONG(write_timeout_hint_us,
rocksdb_write_options.timeout_hint_us,
PLUGIN_VAR_RQCMDARG,
"WriteOptions::timeout_hint_us for RocksDB",
NULL, NULL, rocksdb_write_options.timeout_hint_us,
/* min */ 0L, /* max */ LONG_MAX, 0);
static MYSQL_SYSVAR_BOOL(write_ignore_missing_column_families,
*reinterpret_cast<my_bool*>(
&rocksdb_write_options.ignore_missing_column_families),
PLUGIN_VAR_RQCMDARG,
"WriteOptions::ignore_missing_column_families for RocksDB",
NULL, NULL, rocksdb_write_options.ignore_missing_column_families);
const longlong ROCKSDB_WRITE_BUFFER_SIZE_DEFAULT=4194304;
static struct st_mysql_sys_var* rocksdb_system_variables[]= {
MYSQL_SYSVAR(lock_wait_timeout),
MYSQL_SYSVAR(max_row_locks),
MYSQL_SYSVAR(bulk_load),
MYSQL_SYSVAR(bulk_load_size),
MYSQL_SYSVAR(create_if_missing),
MYSQL_SYSVAR(create_missing_column_families),
MYSQL_SYSVAR(error_if_exists),
MYSQL_SYSVAR(paranoid_checks),
MYSQL_SYSVAR(info_log_level),
MYSQL_SYSVAR(max_open_files),
MYSQL_SYSVAR(max_total_wal_size),
MYSQL_SYSVAR(disableDataSync),
MYSQL_SYSVAR(use_fsync),
MYSQL_SYSVAR(delete_obsolete_files_period_micros),
MYSQL_SYSVAR(max_background_compactions),
MYSQL_SYSVAR(max_background_flushes),
MYSQL_SYSVAR(max_log_file_size),
MYSQL_SYSVAR(log_file_time_to_roll),
MYSQL_SYSVAR(keep_log_file_num),
MYSQL_SYSVAR(max_manifest_file_size),
MYSQL_SYSVAR(table_cache_numshardbits),
MYSQL_SYSVAR(table_cache_remove_scan_count_limit),
MYSQL_SYSVAR(WAL_ttl_seconds),
MYSQL_SYSVAR(WAL_size_limit_MB),
MYSQL_SYSVAR(manifest_preallocation_size),
MYSQL_SYSVAR(allow_os_buffer),
MYSQL_SYSVAR(allow_mmap_reads),
MYSQL_SYSVAR(allow_mmap_writes),
MYSQL_SYSVAR(is_fd_close_on_exec),
MYSQL_SYSVAR(skip_log_error_on_recovery),
MYSQL_SYSVAR(stats_dump_period_sec),
MYSQL_SYSVAR(advise_random_on_open),
MYSQL_SYSVAR(db_write_buffer_size),
MYSQL_SYSVAR(use_adaptive_mutex),
MYSQL_SYSVAR(bytes_per_sync),
MYSQL_SYSVAR(enable_thread_tracking),
MYSQL_SYSVAR(block_cache_size),
MYSQL_SYSVAR(cache_index_and_filter_blocks),
MYSQL_SYSVAR(index_type),
MYSQL_SYSVAR(hash_index_allow_collision),
MYSQL_SYSVAR(no_block_cache),
MYSQL_SYSVAR(block_size),
MYSQL_SYSVAR(block_size_deviation),
MYSQL_SYSVAR(block_restart_interval),
MYSQL_SYSVAR(whole_key_filtering),
MYSQL_SYSVAR(default_cf_options),
MYSQL_SYSVAR(cf_options_file),
MYSQL_SYSVAR(write_sync),
MYSQL_SYSVAR(write_disable_wal),
MYSQL_SYSVAR(write_timeout_hint_us),
MYSQL_SYSVAR(write_ignore_missing_column_families),
NULL
};
static rocksdb::WriteOptions get_write_options() {
rocksdb::WriteOptions opt(rocksdb_write_options);
switch (rocksdb_write_sync) {
case WRITE_SYNC_OFF:
opt.sync = false;
break;
case WRITE_SYNC_ON_COMMIT:
opt.sync = true;
break;
case WRITE_SYNC_BACKGROUND:
// this option is not implemented yet
DBUG_ASSERT(0);
break;
}
return opt;
}
///////////////////////////////////////////////////////////////////////////////////////////
/**
@brief
Function we use in the creation of our hash to get key.
*/
static uchar* rocksdb_get_key(ROCKSDB_SHARE *share, size_t *length,
my_bool not_used __attribute__((unused)))
{
*length=share->table_name_length;
return (uchar*) share->table_name;
}
/*
The following is needed as an argument for thd_enter_cond, irrespectively of
whether we're compiling with P_S or not.
*/
PSI_stage_info stage_waiting_on_row_lock= { 0, "Waiting for row lock", 0};
#ifdef HAVE_PSI_INTERFACE
static PSI_stage_info *all_rocksdb_stages[]=
{
& stage_waiting_on_row_lock
};
static PSI_mutex_key ex_key_mutex_example, ex_key_mutex_ROCKSDB_SHARE_mutex;
static PSI_mutex_info all_rocksdb_mutexes[]=
{
{ &ex_key_mutex_example, "rocksdb", PSI_FLAG_GLOBAL},
{ &ex_key_mutex_ROCKSDB_SHARE_mutex, "ROCKSDB_SHARE::mutex", 0}
};
static void init_rocksdb_psi_keys()
{
const char* category= "rocksdb";
int count;
if (PSI_server == NULL)
return;
count= array_elements(all_rocksdb_mutexes);
PSI_server->register_mutex(category, all_rocksdb_mutexes, count);
count= array_elements(all_rocksdb_stages);
mysql_stage_register(category, all_rocksdb_stages, count);
}
#endif
static int bytewise_compare(const rocksdb::Slice& a, const rocksdb::Slice& b)
{
size_t a_size= a.size();
size_t b_size= b.size();
size_t len= (a_size < b_size) ? a_size : b_size;
int res;
if ((res= memcmp(a.data(), b.data(), len)))
return res;
/* Ok, res== 0 */
if (a_size != b_size)
{
return a_size < b_size? -1 : 1;
}
return 0;
}
/*
The keys are in form: {index_number} {mem-comparable-key}
(todo: knowledge about this format is shared between this class and
RDBSE_KEYDEF)
*/
class Primary_key_comparator : public rocksdb::Comparator
{
public:
int Compare(const rocksdb::Slice& a, const rocksdb::Slice& b) const
{
return bytewise_compare(a,b);
}
/* The following is not needed by RocksDB, but conceptually should be here: */
static ulong get_hashnr(const char *key, size_t key_len);
const char* Name() const { return "RocksDB_SE_v3.1"; }
//TODO: advanced funcs:
// - FindShortestSeparator
// - FindShortSuccessor
// for now, do-nothing implementations:
void FindShortestSeparator(std::string* start, const rocksdb::Slice& limit) const {}
void FindShortSuccessor(std::string* key) const {}
};
class Reverse_comparator : public rocksdb::Comparator
{
int Compare(const rocksdb::Slice& a, const rocksdb::Slice& b) const
{
return -bytewise_compare(a,b);
}
const char* Name() const { return "rev:RocksDB_SE_v3.1"; }
void FindShortestSeparator(std::string* start, const rocksdb::Slice& limit) const {}
void FindShortSuccessor(std::string* key) const {}
};
Primary_key_comparator rocksdb_pk_comparator;
Reverse_comparator rocksdb_rev_pk_comparator;
/*
This function doesn't support reverse comparisons. They are handled by the
caller.
*/
int compare_mem_comparable_keys(const uchar *a, size_t a_len, const uchar *b, size_t b_len)
{
rocksdb::Slice a_slice((char*)a, a_len);
rocksdb::Slice b_slice((char*)b, b_len);
return rocksdb_pk_comparator.Compare(a_slice, b_slice);
}
/*
This is a rocksdb connection. Its members represent the current transaction,
which consists of:
- the snapshot
- the changes we've made but are not seeing yet.
The changes are made to individual tables, which store them here and then
this object commits them on commit.
*/
class Rdb_transaction
{
public:
/* The snapshot the transaction is used */
const rocksdb::Snapshot *snapshot;
/* The changes made by this transaction (and not yet applied) */
Row_table changes;
uint n_writes;
/*
These are needed to use LF-Hash. They are allocated per-thread. Logically,
they are not part of the transaction but it's convenient to have them here.
*/
LF_PINS *pins;
/* Row locks taken by this transaction */
Dynamic_array<Row_lock*> trx_locks;
int timeout_sec; /* Cached value of @@rocksdb_lock_wait_timeout */
int max_row_locks;
void set_params(int timeout_sec_arg, int max_row_locks_arg)
{
timeout_sec= timeout_sec_arg;
max_row_locks= max_row_locks_arg;
}
Row_lock *get_lock(const uchar* key, size_t keylen, bool *timed_out)
{
Row_lock *lock;
if (trx_locks.elements() > max_row_locks)
{
*timed_out= false;
return NULL;
}
if (!(lock= row_locks.get_lock(pins, key, keylen, timeout_sec)))
{
*timed_out= true;
return NULL;
}
return lock;
}
void add_lock(Row_lock* lock)
{
trx_locks.append(lock);
}
void release_last_lock()
{
row_locks.release_lock(pins, trx_locks.at(trx_locks.elements() - 1));
trx_locks.pop();
}
void release_locks()
{
int size= trx_locks.elements();
for (int i= 0; i < size; i++)
row_locks.release_lock(pins, trx_locks.at(i));
trx_locks.clear();
}
bool commit()
{
bool res= false;
flush_batch();
/* rollback() will delete snapshot, batch and locks */
rollback();
return res;
}
private:
int flush_batch_intern()
{
bool res= false;
rocksdb::WriteBatch batch;
if (changes.is_empty())
return false;
for (int reverse=0; reverse <= 1; reverse++)
{
Row_table_iter iter(&changes, (bool)reverse);
for (iter.SeekToFirst(); iter.Valid(); iter.Next())
{
if (iter.is_tombstone())
{
batch.Delete(iter.keydef()->get_cf(), iter.key());
}
else
{
batch.Put(iter.keydef()->get_cf(), iter.key(), iter.value());
}
}
}
rocksdb::Status s= rdb->Write(get_write_options(), &batch);
res= !s.ok(); // we return true when something failed
return res;
}
public:
int flush_batch()
{
bool bres= flush_batch_intern();
changes.reinit();
n_writes= 0;
return bres;
}
void prepare_for_write()
{
/* Currently, we don't do anything here */
}
/*
This must be called when last statement is rolled back, but the transaction
continues
*/
void rollback_stmt() { changes.rollback_stmt(); }
void start_stmt() { changes.start_stmt(); }
void rollback()
{
if (snapshot)
{
rdb->ReleaseSnapshot(snapshot);
snapshot= NULL;
}
changes.reinit();
release_locks();
n_writes= 0;
}
Rdb_transaction() :
snapshot(NULL), n_writes(0), pins(NULL)
{
changes.init();
}
~Rdb_transaction()
{
if (snapshot)
{
rdb->ReleaseSnapshot(snapshot);
snapshot= NULL;
}
changes.cleanup();
}
};
static Rdb_transaction* &get_trx_from_thd(THD *thd)
{
return *(Rdb_transaction**) thd_ha_data(thd, rocksdb_hton);
}
/*
TODO: maybe, call this in external_lock() and store in ha_rocksdb..
*/
static Rdb_transaction *get_or_create_trx(THD *thd)
{
Rdb_transaction*& trx= get_trx_from_thd(thd);
if (trx == NULL)
{
if (!(trx= new Rdb_transaction))
return NULL;
trx->pins= row_locks.get_pins();
trx->snapshot= rdb->GetSnapshot();
}
else
{
if (!trx->snapshot)
trx->snapshot= rdb->GetSnapshot();
}
return trx;
}
static int rocksdb_close_connection(handlerton* hton, THD* thd)
{
Rdb_transaction*& trx= get_trx_from_thd(thd);
if (trx && trx->pins)
row_locks.put_pins(trx->pins);
delete trx;
trx= NULL;
return 0;
}
static int rocksdb_commit(handlerton* hton, THD* thd, bool commit_trx)
{
if (commit_trx)
{
Rdb_transaction*& trx= get_trx_from_thd(thd);
if (trx)
trx->commit(); // todo: return error code.
}
else
{
/*
Committing a statement within a transaction. We don't need to do anything
here. trx->start_stmt() will notify Rdb_transaction that another
statement has started.
*/
}
return 0;
}
static int rocksdb_rollback(handlerton* hton, THD* thd, bool rollback_trx)
{
if (rollback_trx)
{
/* Discard the changes made by the transaction */
Rdb_transaction*& trx= get_trx_from_thd(thd);
if (trx)
trx->rollback();
}
else
{
Rdb_transaction*& trx= get_trx_from_thd(thd);
if (trx)
trx->rollback_stmt();
}
return 0;
}
/*
This is called for SHOW ENGINE ROCKSDB STATUS|LOGS|etc.
For now, produce info about live files (which gives an imprecise idea about
what column families are there)
*/
static bool rocksdb_show_status(handlerton* hton,
THD* thd,
stat_print_fn* stat_print,
enum ha_stat_type stat_type)
{
bool res= false;
if (stat_type == HA_ENGINE_STATUS)
{
std::vector<rocksdb::LiveFileMetaData> metadata_vec;
rdb->GetLiveFilesMetaData(&metadata_vec);
std::ostringstream buf;
for (auto it= metadata_vec.begin(); it!=metadata_vec.end(); it++)
{
buf << "cf=" << it->column_family_name;
buf << " name=" << it->name;
buf << " size=" << it->size;
buf << std::endl;
}
std::string str= buf.str();
res= stat_print(thd, rocksdb_hton_name,
(uint)strlen(rocksdb_hton_name),
STRING_WITH_LEN("live_files"),
str.c_str(), str.size());
}
return res;
}
void get_cf_options(const std::string &cf_name, rocksdb::ColumnFamilyOptions *opts)
{
*opts = default_cf_opts;
rocksdb_cf_options_map.Get(cf_name, opts);
// Set the comparator according to 'rev:'
if (is_cf_name_reverse(cf_name.c_str()))
opts->comparator= &rocksdb_rev_pk_comparator;
else
opts->comparator= &rocksdb_pk_comparator;
}