-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
innodb_engine.c
2440 lines (2034 loc) · 66.5 KB
/
innodb_engine.c
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) 2011, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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, version 2.0, 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.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
***********************************************************************/
/**************************************************//**
@file innodb_engine.c
InnoDB Memcached Engine code
Extracted and modified from NDB memcached project
04/12/2011 Jimmy Yang
*******************************************************/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include <arpa/inet.h>
#include "default_engine.h"
#include <memcached/util.h>
#include <memcached/config_parser.h>
#include <unistd.h>
#include "innodb_engine.h"
#include "innodb_engine_private.h"
#include "innodb_api.h"
#include "hash_item_util.h"
#include "innodb_cb_api.h"
/** Define also present in daemon/memcached.h */
#define KEY_MAX_LENGTH 250
/** Time (in seconds) that background thread sleeps before it wakes
up and commit idle connection transactions */
#define BK_COMMIT_THREAD_SLEEP_INTERVAL 5
/** Maximum number of connections that background thread processes each
time */
#define BK_MAX_PROCESS_COMMIT 5
/** Minimum time (in seconds) that a connection has been idle, that makes
it candidate for background thread to commit it */
#define CONN_IDLE_TIME_TO_BK_COMMIT 5
/** Tells whether memcached plugin is being shutdown */
static bool memcached_shutdown = false;
/** Tells whether the background thread is exited */
static bool bk_thd_exited = true;
/** Tells whether all connections need to release MDL locks */
bool release_mdl_lock = false;
/** InnoDB Memcached engine configuration info */
typedef struct eng_config_info {
char* option_string; /*!< memcached config option
string */
void* cb_ptr; /*!< call back function ptr */
unsigned int eng_read_batch_size; /*!< read batch size */
unsigned int eng_write_batch_size; /*!< write batch size */
bool eng_enable_binlog; /*!< whether binlog is
enabled specifically for
this memcached engine */
} eng_config_info_t;
extern option_t config_option_names[];
/** Check if global read lock is active */
extern
bool
handler_check_global_read_lock_active();
/** Check the input key name implies a table mapping switch. The name
would start with "@@", and in the format of "@@new_table_mapping.key"
or simply "@@new_table_mapping" */
/**********************************************************************//**
Unlock a table and commit the transaction
return 0 if fail to commit the transaction */
extern
int
handler_unlock_table(
/*=================*/
void* my_thd, /*!< in: thread */
void* my_table, /*!< in: Table metadata */
int my_lock_mode); /*!< in: lock mode */
/*******************************************************************//**
Get InnoDB Memcached engine handle
@return InnoDB Memcached engine handle */
static inline
struct innodb_engine*
innodb_handle(
/*==========*/
ENGINE_HANDLE* handle) /*!< in: Generic engine handle */
{
return((struct innodb_engine*) handle);
}
/*******************************************************************//**
Cleanup idle connections if "clear_all" is false, and clean up all
connections if "clear_all" is true.
@return number of connection cleaned */
static
void
innodb_conn_clean_data(
/*===================*/
innodb_conn_data_t* conn_data,
bool has_lock,
bool free_all);
/*******************************************************************//**
Get default Memcached engine handle
@return default Memcached engine handle */
static inline
struct default_engine*
default_handle(
/*===========*/
struct innodb_engine* eng)
{
return((struct default_engine*) eng->default_engine);
}
/****** Gateway to the default_engine's create_instance() function */
ENGINE_ERROR_CODE
create_my_default_instance(
/*=======================*/
uint64_t,
GET_SERVER_API,
ENGINE_HANDLE **);
/*********** FUNCTIONS IMPLEMENTING THE PUBLISHED API BEGIN HERE ********/
/*******************************************************************//**
Create InnoDB Memcached Engine.
@return ENGINE_SUCCESS if successful, otherwise, error code */
ENGINE_ERROR_CODE
create_instance(
/*============*/
uint64_t interface, /*!< in: protocol version,
currently always 1 */
GET_SERVER_API get_server_api, /*!< in: Callback the engines
may call to get the public
server interface */
ENGINE_HANDLE** handle ) /*!< out: Engine handle */
{
ENGINE_ERROR_CODE err_ret;
struct innodb_engine* innodb_eng;
SERVER_HANDLE_V1 *api = get_server_api();
if (interface != 1 || api == NULL) {
return(ENGINE_ENOTSUP);
}
innodb_eng = malloc(sizeof(struct innodb_engine));
if (innodb_eng == NULL) {
return(ENGINE_ENOMEM);
}
memset(innodb_eng, 0, sizeof(*innodb_eng));
innodb_eng->engine.interface.interface = 1;
innodb_eng->engine.get_info = innodb_get_info;
innodb_eng->engine.initialize = innodb_initialize;
innodb_eng->engine.destroy = innodb_destroy;
innodb_eng->engine.allocate = innodb_allocate;
innodb_eng->engine.remove = innodb_remove;
innodb_eng->engine.release = innodb_release;
innodb_eng->engine.clean_engine= innodb_clean_engine;
innodb_eng->engine.get = innodb_get;
innodb_eng->engine.get_stats = innodb_get_stats;
innodb_eng->engine.reset_stats = innodb_reset_stats;
innodb_eng->engine.store = innodb_store;
innodb_eng->engine.arithmetic = innodb_arithmetic;
innodb_eng->engine.flush = innodb_flush;
innodb_eng->engine.unknown_command = innodb_unknown_command;
innodb_eng->engine.item_set_cas = item_set_cas;
innodb_eng->engine.get_item_info = innodb_get_item_info;
innodb_eng->engine.get_stats_struct = NULL;
innodb_eng->engine.errinfo = NULL;
innodb_eng->engine.bind = innodb_bind;
innodb_eng->server = *api;
innodb_eng->get_server_api = get_server_api;
/* configuration, with default values*/
innodb_eng->info.info.description = "InnoDB Memcache " VERSION;
innodb_eng->info.info.num_features = 3;
innodb_eng->info.info.features[0].feature = ENGINE_FEATURE_CAS;
innodb_eng->info.info.features[1].feature =
ENGINE_FEATURE_PERSISTENT_STORAGE;
innodb_eng->info.info.features[0].feature = ENGINE_FEATURE_LRU;
/* Now call create_instace() for the default engine */
err_ret = create_my_default_instance(interface, get_server_api,
&(innodb_eng->default_engine));
if (err_ret != ENGINE_SUCCESS) {
free(innodb_eng);
return(err_ret);
}
innodb_eng->clean_stale_conn = false;
innodb_eng->initialized = true;
*handle = (ENGINE_HANDLE*) &innodb_eng->engine;
return(ENGINE_SUCCESS);
}
/*******************************************************************//**
background thread to commit trx.
@return dummy parameter */
static
void*
innodb_bk_thread(
/*=============*/
void* arg)
{
ENGINE_HANDLE* handle;
struct innodb_engine* innodb_eng;
innodb_conn_data_t* conn_data;
void* thd = NULL;
bk_thd_exited = false;
handle = (ENGINE_HANDLE*) (arg);
innodb_eng = innodb_handle(handle);
if (innodb_eng->enable_binlog) {
/* This thread will commit the transactions
on behalf of the other threads. It will "pretend"
to be each connection thread while doing it. */
thd = handler_create_thd(true);
}
conn_data = UT_LIST_GET_FIRST(innodb_eng->conn_data);
while(!memcached_shutdown) {
innodb_conn_data_t* next_conn_data;
uint64_t time;
uint64_t trx_start = 0;
uint64_t processed_count = 0;
if (handler_check_global_read_lock_active()) {
release_mdl_lock = true;
} else {
release_mdl_lock = false;
}
/* Do the cleanup every innodb_eng->bk_commit_interval
seconds. We also check if the plugin is being shutdown
every second */
for (uint i = 0; i < innodb_eng->bk_commit_interval; i++) {
sleep(1);
/* If memcached is being shutdown, break */
if (memcached_shutdown) {
break;
}
}
time = mci_get_time();
if (UT_LIST_GET_LEN(innodb_eng->conn_data) == 0) {
continue;
}
if (!conn_data) {
conn_data = UT_LIST_GET_FIRST(innodb_eng->conn_data);
}
if (conn_data) {
next_conn_data = UT_LIST_GET_NEXT(conn_list, conn_data);
} else {
next_conn_data = NULL;
}
/* Set the clean_stale_conn to prevent force clean in
innodb_conn_clean. */
LOCK_CONN_IF_NOT_LOCKED(false, innodb_eng);
innodb_eng->clean_stale_conn = true;
UNLOCK_CONN_IF_NOT_LOCKED(false, innodb_eng);
while (conn_data) {
if (release_mdl_lock && !conn_data->is_stale) {
int err;
if(conn_data->is_waiting_for_mdl) {
goto next_item;
}
err = LOCK_CURRENT_CONN_TRYLOCK(conn_data);
if (err != 0) {
goto next_item;
}
/* We have got the lock here */
} else {
LOCK_CURRENT_CONN_IF_NOT_LOCKED(false, conn_data);
}
if (conn_data->is_stale) {
UNLOCK_CURRENT_CONN_IF_NOT_LOCKED(
false, conn_data);
LOCK_CONN_IF_NOT_LOCKED(false, innodb_eng);
UT_LIST_REMOVE(conn_list, innodb_eng->conn_data,
conn_data);
UNLOCK_CONN_IF_NOT_LOCKED(false, innodb_eng);
innodb_conn_clean_data(conn_data, false, true);
goto next_item;
}
if (release_mdl_lock) {
if (conn_data->thd) {
handler_thd_attach(conn_data->thd, NULL);
}
if (conn_data->in_use) {
UNLOCK_CURRENT_CONN_IF_NOT_LOCKED(false, conn_data);
goto next_item;
}
innodb_reset_conn(conn_data, true, true,
innodb_eng->enable_binlog);
if(conn_data->mysql_tbl) {
handler_unlock_table(conn_data->thd,
conn_data->mysql_tbl,
HDL_READ);
conn_data->mysql_tbl = NULL;
}
/*Close the data cursor */
if (conn_data->crsr) {
innodb_cb_cursor_close(conn_data->crsr);
conn_data->crsr = NULL;
}
if(conn_data->crsr_trx != NULL) {
ib_cb_trx_release(conn_data->crsr_trx);
conn_data->crsr_trx = NULL;
}
UNLOCK_CURRENT_CONN_IF_NOT_LOCKED(false, conn_data);
goto next_item;
}
if (conn_data->crsr_trx) {
trx_start = ib_cb_trx_get_start_time(
conn_data->crsr_trx);
}
/* Check the trx, if it is qualified for
reset and commit */
if ((conn_data->n_writes_since_commit > 0
|| conn_data->n_reads_since_commit > 0)
&& trx_start
&& (time - trx_start > CONN_IDLE_TIME_TO_BK_COMMIT)
&& !conn_data->in_use) {
/* binlog is running, make the thread
attach to conn_data->thd for binlog
committing */
if (thd && conn_data->thd) {
handler_thd_attach(
conn_data->thd, NULL);
}
innodb_reset_conn(conn_data, true, true,
innodb_eng->enable_binlog);
processed_count++;
}
UNLOCK_CURRENT_CONN_IF_NOT_LOCKED(false, conn_data);
next_item:
conn_data = next_conn_data;
/* Process BK_MAX_PROCESS_COMMIT (5) trx at a time */
if (!release_mdl_lock &&
processed_count > BK_MAX_PROCESS_COMMIT) {
break;
}
if (conn_data) {
next_conn_data = UT_LIST_GET_NEXT(
conn_list, conn_data);
}
}
/* Set the clean_stale_conn back. */
LOCK_CONN_IF_NOT_LOCKED(false, innodb_eng);
innodb_eng->clean_stale_conn = false;
UNLOCK_CONN_IF_NOT_LOCKED(false, innodb_eng);
}
bk_thd_exited = true;
/* Change to its original state before close the MySQL THD */
if (thd) {
handler_thd_attach(thd, NULL);
handler_close_thd(thd);
}
pthread_detach(pthread_self());
pthread_exit(NULL);
return((void*) 0);
}
/*******************************************************************//**
Get engine info.
@return engine info */
static
const engine_info*
innodb_get_info(
/*============*/
ENGINE_HANDLE* handle) /*!< in: Engine handle */
{
return(&innodb_handle(handle)->info.info);
}
/*******************************************************************//**
Initialize InnoDB Memcached Engine.
@return ENGINE_SUCCESS if successful */
static
ENGINE_ERROR_CODE
innodb_initialize(
/*==============*/
ENGINE_HANDLE* handle, /*!< in/out: InnoDB memcached
engine */
const char* config_str) /*!< in: configure string */
{
ENGINE_ERROR_CODE return_status = ENGINE_SUCCESS;
struct innodb_engine* innodb_eng = innodb_handle(handle);
struct default_engine* def_eng = default_handle(innodb_eng);
eng_config_info_t* my_eng_config;
pthread_attr_t attr;
my_eng_config = (eng_config_info_t*) config_str;
/* If no call back function registered (InnoDB engine failed to load),
load InnoDB Memcached engine should fail too */
if (!my_eng_config->cb_ptr) {
return(ENGINE_TMPFAIL);
}
/* Register the call back function */
register_innodb_cb((void*) my_eng_config->cb_ptr);
innodb_eng->read_batch_size = (my_eng_config->eng_read_batch_size
? my_eng_config->eng_read_batch_size
: CONN_NUM_READ_COMMIT);
innodb_eng->write_batch_size = (my_eng_config->eng_write_batch_size
? my_eng_config->eng_write_batch_size
: CONN_NUM_WRITE_COMMIT);
innodb_eng->enable_binlog = my_eng_config->eng_enable_binlog;
innodb_eng->cfg_status = innodb_cb_get_cfg();
/* If binlog is not enabled by InnoDB memcached plugin, let's
check whether innodb_direct_access_enable_binlog is turned on */
if (!innodb_eng->enable_binlog) {
innodb_eng->enable_binlog = innodb_eng->cfg_status
& IB_CFG_BINLOG_ENABLED;
}
innodb_eng->enable_mdl = innodb_eng->cfg_status & IB_CFG_MDL_ENABLED;
innodb_eng->trx_level = ib_cb_cfg_trx_level();
innodb_eng->bk_commit_interval = ib_cb_cfg_bk_commit_interval();
UT_LIST_INIT(innodb_eng->conn_data);
pthread_mutex_init(&innodb_eng->conn_mutex, NULL);
pthread_mutex_init(&innodb_eng->cas_mutex, NULL);
pthread_mutex_init(&innodb_eng->flush_mutex, NULL);
/* Fetch InnoDB specific settings */
innodb_eng->meta_info = innodb_config(
NULL, 0, &innodb_eng->meta_hash);
if (!innodb_eng->meta_info) {
return(ENGINE_TMPFAIL);
}
if (innodb_eng->default_engine) {
return_status = def_eng->engine.initialize(
innodb_eng->default_engine,
my_eng_config->option_string);
}
memcached_shutdown = false;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&innodb_eng->bk_thd_for_commit, &attr, innodb_bk_thread,
handle);
return(return_status);
}
extern void handler_close_thd(void*);
/*******************************************************************//**
Close table using handler functions.
@param conn_data cursor information of connection */
void
innodb_close_mysql_table(
/*=====================*/
innodb_conn_data_t* conn_data) /*!< in: connection
cursor*/
{
if (conn_data->mysql_tbl) {
assert(conn_data->thd);
handler_unlock_table(conn_data->thd,
conn_data->mysql_tbl,
HDL_READ);
conn_data->mysql_tbl = NULL;
}
if (conn_data->thd) {
handler_close_thd(conn_data->thd);
conn_data->thd = NULL;
}
}
/*******************************************************************//**
Cleanup idle connections if "clear_all" is false, and clean up all
connections if "clear_all" is true.
@return number of connection cleaned */
static
void
innodb_conn_clean_data(
/*===================*/
innodb_conn_data_t* conn_data,
bool has_lock,
bool free_all)
{
if (!conn_data) {
return;
}
LOCK_CURRENT_CONN_IF_NOT_LOCKED(has_lock, conn_data);
if (conn_data->idx_crsr) {
innodb_cb_cursor_close(conn_data->idx_crsr);
conn_data->idx_crsr = NULL;
}
if (conn_data->idx_read_crsr) {
innodb_cb_cursor_close(conn_data->idx_read_crsr);
conn_data->idx_read_crsr = NULL;
}
if (conn_data->crsr) {
innodb_cb_cursor_close(conn_data->crsr);
conn_data->crsr = NULL;
}
if (conn_data->read_crsr) {
innodb_cb_cursor_close(conn_data->read_crsr);
conn_data->read_crsr = NULL;
}
if (conn_data->crsr_trx) {
ib_err_t err;
innodb_cb_trx_commit(conn_data->crsr_trx);
err = ib_cb_trx_release(conn_data->crsr_trx);
assert(err == DB_SUCCESS);
conn_data->crsr_trx = NULL;
}
innodb_close_mysql_table(conn_data);
if (conn_data->tpl) {
ib_cb_tuple_delete(conn_data->tpl);
conn_data->tpl = NULL;
}
if (conn_data->idx_tpl) {
ib_cb_tuple_delete(conn_data->idx_tpl);
conn_data->idx_tpl = NULL;
}
if (conn_data->read_tpl) {
ib_cb_tuple_delete(conn_data->read_tpl);
conn_data->read_tpl = NULL;
}
if (conn_data->sel_tpl) {
ib_cb_tuple_delete(conn_data->sel_tpl);
conn_data->sel_tpl = NULL;
}
UNLOCK_CURRENT_CONN_IF_NOT_LOCKED(has_lock, conn_data);
if (free_all) {
if (conn_data->result) {
free(conn_data->result);
conn_data->result = NULL;
}
if (conn_data->row_buf) {
free(conn_data->row_buf);
conn_data->row_buf = NULL;
conn_data->row_buf_len = 0;
}
if (conn_data->cmd_buf) {
free(conn_data->cmd_buf);
conn_data->cmd_buf = NULL;
conn_data->cmd_buf_len = 0;
}
if (conn_data->mul_col_buf) {
free(conn_data->mul_col_buf);
conn_data->mul_col_buf = NULL;
conn_data->mul_col_buf_len = 0;
}
pthread_mutex_destroy(&conn_data->curr_conn_mutex);
free(conn_data);
}
}
/*******************************************************************//**
Cleanup idle connections if "clear_all" is false, and clean up all
connections if "clear_all" is true.
@return number of connection cleaned */
static
int
innodb_conn_clean(
/*==============*/
innodb_engine_t* engine, /*!< in/out: InnoDB memcached
engine */
bool clear_all, /*!< in: Clear all connection */
bool has_lock) /*!< in: Has engine mutext */
{
innodb_conn_data_t* conn_data;
innodb_conn_data_t* next_conn_data;
int num_freed = 0;
void* thd = NULL;
if (engine->enable_binlog && clear_all) {
thd = handler_create_thd(true);
}
LOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
conn_data = UT_LIST_GET_FIRST(engine->conn_data);
while (conn_data) {
void* cookie = conn_data->conn_cookie;
next_conn_data = UT_LIST_GET_NEXT(conn_list, conn_data);
if (!clear_all && !conn_data->in_use) {
innodb_conn_data_t* check_data;
check_data = engine->server.cookie->get_engine_specific(
cookie);
/* The check data is the original conn_data stored
in connection "cookie", it can be set to NULL if
connection closed, or to a new conn_data if it is
closed and reopened. So verify and see if our
current conn_data is stale */
if (!check_data || check_data != conn_data) {
assert(conn_data->is_stale);
}
}
/* If current conn is stale or clear_all is true,
clean up it.*/
if (conn_data->is_stale) {
/* If bk thread is doing the same thing, stop
the loop to avoid confliction.*/
if (engine->clean_stale_conn)
break;
UT_LIST_REMOVE(conn_list, engine->conn_data,
conn_data);
innodb_conn_clean_data(conn_data, false, true);
num_freed++;
} else {
if (clear_all) {
UT_LIST_REMOVE(conn_list, engine->conn_data,
conn_data);
if (thd && conn_data->thd ) {
handler_thd_attach(conn_data->thd,
NULL);
}
innodb_reset_conn(conn_data, false, true,
engine->enable_binlog);
if (conn_data->thd) {
handler_thd_attach(
conn_data->thd, NULL);
}
innodb_conn_clean_data(conn_data, false, true);
engine->server.cookie->store_engine_specific(
cookie, NULL);
num_freed++;
}
}
conn_data = next_conn_data;
}
assert(!clear_all || engine->conn_data.count == 0);
UNLOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
if (thd) {
handler_thd_attach(thd, NULL);
handler_close_thd(thd);
}
return(num_freed);
}
/*******************************************************************//**
Destroy and Free InnoDB Memcached engine */
static
void
innodb_destroy(
/*===========*/
ENGINE_HANDLE* handle, /*!< in: Destroy the engine instance */
bool force) /*!< in: Force to destroy */
{
struct innodb_engine* innodb_eng = innodb_handle(handle);
struct default_engine *def_eng = default_handle(innodb_eng);
memcached_shutdown = true;
/* Wait for the background thread to exit */
while (!bk_thd_exited) {
sleep(1);
}
innodb_conn_clean(innodb_eng, true, false);
if (innodb_eng->meta_hash) {
HASH_CLEANUP(innodb_eng->meta_hash, meta_cfg_info_t*);
}
pthread_mutex_destroy(&innodb_eng->conn_mutex);
pthread_mutex_destroy(&innodb_eng->cas_mutex);
pthread_mutex_destroy(&innodb_eng->flush_mutex);
if (innodb_eng->default_engine) {
def_eng->engine.destroy(innodb_eng->default_engine, force);
}
free(innodb_eng);
}
/** Defines for connection initialization to indicate if we will
do a read or write operation, or in the case of CONN_MODE_NONE, just get
the connection's conn_data structure */
enum conn_mode {
CONN_MODE_READ,
CONN_MODE_WRITE,
CONN_MODE_NONE
};
/*******************************************************************//**
Opens mysql table if enable_binlog or enable_mdl is set
@param conn_data connection cursor data
@param conn_optioin read or write operation
@param engine Innodb memcached engine
@returns DB_SUCCESS on success and DB_ERROR on failure */
ib_err_t
innodb_open_mysql_table(
/*====================*/
innodb_conn_data_t* conn_data, /*!< in/out:Connection cursor data */
int conn_option, /*!< in: Read or write operation */
innodb_engine_t* engine) /*!< in: InnoDB memcached engine */
{
meta_cfg_info_t* meta_info;
meta_info = conn_data->conn_meta;
conn_data->is_waiting_for_mdl = true;
/* Close the table before opening it again */
innodb_close_mysql_table(conn_data);
if (conn_option == CONN_MODE_READ) {
conn_data->is_waiting_for_mdl = false;
return (DB_SUCCESS);
}
if (!conn_data->thd) {
conn_data->thd = handler_create_thd(engine->enable_binlog);
if (!conn_data->thd) {
return(DB_ERROR);
}
}
if (!conn_data->mysql_tbl) {
conn_data->mysql_tbl =
handler_open_table(
conn_data->thd,
meta_info->col_info[CONTAINER_DB].col_name,
meta_info->col_info[CONTAINER_TABLE].col_name,
HDL_WRITE);
}
conn_data->is_waiting_for_mdl = false;
if(!conn_data->mysql_tbl) {
return(DB_LOCK_WAIT);
}
return (DB_SUCCESS);
}
/*******************************************************************//**
Cleanup connections
@return number of connection cleaned */
/* Initialize a connection's cursor and transactions
@return the connection's conn_data structure */
static
innodb_conn_data_t*
innodb_conn_init(
/*=============*/
innodb_engine_t* engine, /*!< in/out: InnoDB memcached
engine */
const void* cookie, /*!< in: This connection's
cookie */
int conn_option, /*!< in: whether it is
for read or write operation*/
ib_lck_mode_t lock_mode, /*!< in: Table lock mode */
bool has_lock, /*!< in: Has engine mutex */
meta_cfg_info_t* new_meta_info) /*!< in: meta info for
table to open or NULL */
{
innodb_conn_data_t* conn_data;
meta_cfg_info_t* meta_info;
meta_index_t* meta_index;
ib_err_t err = DB_SUCCESS;
ib_crsr_t crsr;
ib_crsr_t read_crsr;
ib_crsr_t idx_crsr;
bool trx_updated = false;
/* Get this connection's conn_data */
conn_data = engine->server.cookie->get_engine_specific(cookie);
assert(!conn_data || !conn_data->in_use);
if (!conn_data) {
LOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
conn_data = engine->server.cookie->get_engine_specific(cookie);
if (conn_data) {
UNLOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
goto have_conn;
}
if (UT_LIST_GET_LEN(engine->conn_data) > 2048) {
/* Some of conn_data can be stale, recycle them */
innodb_conn_clean(engine, false, true);
}
conn_data = malloc(sizeof(*conn_data));
if (!conn_data) {
UNLOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
return(NULL);
}
memset(conn_data, 0, sizeof(*conn_data));
conn_data->result = malloc(sizeof(mci_item_t));
if (!conn_data->result) {
UNLOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
free(conn_data);
conn_data = NULL;
return(NULL);
}
conn_data->conn_meta = new_meta_info
? new_meta_info
: engine->meta_info;
conn_data->row_buf = malloc(1024);
if (!conn_data->row_buf) {
UNLOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
free(conn_data->result);
free(conn_data);
conn_data = NULL;
return(NULL);
}
conn_data->row_buf_len = 1024;
conn_data->cmd_buf = malloc(1024);
if (!conn_data->cmd_buf) {
UNLOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
free(conn_data->row_buf);
free(conn_data->result);
free(conn_data);
conn_data = NULL;
return(NULL);
}
conn_data->cmd_buf_len = 1024;
conn_data->is_flushing = false;
conn_data->conn_cookie = (void*) cookie;
/* Add connection to the list after all memory allocations */
UT_LIST_ADD_LAST(conn_list, engine->conn_data, conn_data);
engine->server.cookie->store_engine_specific(
cookie, conn_data);
pthread_mutex_init(&conn_data->curr_conn_mutex, NULL);
UNLOCK_CONN_IF_NOT_LOCKED(has_lock, engine);
}
have_conn:
meta_info = conn_data->conn_meta;
meta_index = &meta_info->index_info;
assert(engine->conn_data.count > 0);
if (conn_option == CONN_MODE_NONE) {
return(conn_data);
}
LOCK_CURRENT_CONN_IF_NOT_LOCKED(has_lock, conn_data);
/* If flush is running, then wait for it complete. */
if (conn_data->is_flushing) {
/* Request flush_mutex for waiting for flush
completed. */
pthread_mutex_lock(&engine->flush_mutex);
pthread_mutex_unlock(&engine->flush_mutex);
}
/* This special case added to facilitate unlocking
of MDL lock during FLUSH TABLE WITH READ LOCK */
if (engine &&
release_mdl_lock &&
(engine->enable_binlog || engine->enable_mdl)) {
if ( DB_SUCCESS !=
innodb_open_mysql_table(conn_data,
conn_option,
engine)){
UNLOCK_CURRENT_CONN_IF_NOT_LOCKED(
has_lock, conn_data);
return NULL;
}
}
conn_data->in_use = true;
crsr = conn_data->crsr;
read_crsr = conn_data->read_crsr;
if (lock_mode == IB_LOCK_TABLE_X) {
if(!conn_data->crsr_trx) {
conn_data->crsr_trx = ib_cb_trx_begin(
engine->trx_level, true, false);
} else {
/* Write cursor transaction exists.
Reuse this transaction.*/
if (ib_cb_trx_read_only(conn_data->crsr_trx)) {
innodb_cb_trx_commit(
conn_data->crsr_trx);
}
err = ib_cb_trx_start(conn_data->crsr_trx,
engine->trx_level,
true, false, NULL);
assert(err == DB_SUCCESS);
}
err = innodb_api_begin(
engine,
meta_info->col_info[CONTAINER_DB].col_name,