forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_ps_cache.cpp
More file actions
945 lines (892 loc) · 33.8 KB
/
Copy pathob_ps_cache.cpp
File metadata and controls
945 lines (892 loc) · 33.8 KB
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
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_PC
#include "ob_ps_cache.h"
#include "sql/plan_cache/ob_ps_sql_utils.h"
#include "sql/plan_cache/ob_ps_cache_callback.h"
#include "sql/resolver/cmd/ob_call_procedure_stmt.h"
#include "sql/udr/ob_udr_mgr.h"
#include "share/schema/ob_schema_getter_guard.h"
#include "lib/rc/ob_rc.h"
namespace oceanbase
{
using namespace common;
using namespace share::schema;
namespace sql
{
ObPsCache::ObPsCache()
: next_ps_stmt_id_(0),
inited_(false),
tenant_id_(OB_INVALID_ID),
host_(),
stmt_id_map_(),
stmt_info_map_(),
mem_limit_pct_(0),
mem_high_pct_(0),
mem_low_pct_(0),
hit_count_(0),
access_count_(0),
mutex_(common::ObLatchIds::PS_CACHE_EVICT_MUTEX_LOCK),
mem_context_(NULL),
inner_allocator_(NULL),
tg_id_(-1)
{
}
void ObPsCache::destroy()
{
if (inited_) {
// ps_stmt_id和ps_stmt_info创建时,会给其增加引用计数
// 现在PsCache要析构了,对所有内部对象减去1,如果引用计数到0,会显式free内存
TG_DESTROY(tg_id_);
cache_evict_all_ps();
if (NULL != mem_context_) {
DESTROY_CONTEXT(mem_context_);
mem_context_ = NULL;
}
inited_ = false;
}
}
ObPsCache::~ObPsCache()
{
int ret = OB_SUCCESS;
destroy();
LOG_INFO("release ps plan cache", "bt", lbt(), K(tenant_id_), K(ret));
}
int ObPsCache::mtl_init(ObPsCache* &ps_cache)
{
int ret = OB_SUCCESS;
uint64_t tenant_id = lib::current_resource_owner_id();
int64_t mem_limit = lib::get_tenant_memory_limit(tenant_id);
ps_cache->inited_ = false;
return ret;
}
void ObPsCache::mtl_stop(ObPsCache * &ps_cache)
{
if (OB_LIKELY(nullptr != ps_cache && ps_cache->is_inited())) {
TG_CANCEL(ps_cache->tg_id_, ps_cache->evict_task_);
TG_STOP(ps_cache->tg_id_);
}
}
int ObPsCache::init(const int64_t hash_bucket,
const uint64_t tenant_id)
{
int ret = OB_SUCCESS;
lib::ObMutexGuard guard(mutex_);
if (!inited_) {
ObMemAttr attr;
attr.label_ = ObModIds::OB_SQL_PS_CACHE;
attr.tenant_id_ = tenant_id;
attr.ctx_id_ = ObCtxIds::PS_CACHE_CTX_ID;
lib::ContextParam param;
param.set_properties(lib::ALLOC_THREAD_SAFE |
lib::RETURN_MALLOC_DEFAULT)
.set_parallel(4)
.set_mem_attr(attr);
if (OB_FAIL(stmt_id_map_.create(hash::cal_next_prime(hash_bucket),
ObModIds::OB_HASH_BUCKET_PS_CACHE,
ObModIds::OB_HASH_NODE_PS_CACHE,
tenant_id))) {
LOG_WARN("failed to init sql_id_map", K(ret));
} else if (OB_FAIL(stmt_info_map_.create(hash::cal_next_prime(hash_bucket),
ObModIds::OB_HASH_BUCKET_PS_INFO,
ObModIds::OB_HASH_NODE_PS_INFO,
tenant_id))) {
LOG_WARN("FAILED TO INIT sql_plan_map", K(ret));
} else if (OB_FAIL(ROOT_CONTEXT->CREATE_CONTEXT(
mem_context_,
param))) {
LOG_WARN("create memory entity failed", K(ret));
} else if (OB_FAIL(TG_CREATE_TENANT(lib::TGDefIDs::PsCacheEvict, tg_id_))) {
LOG_WARN("failed to create tg", K(ret));
} else if (OB_FAIL(TG_START(tg_id_))) {
LOG_WARN("failed to start tg", K(ret));
} else if (OB_FAIL(TG_SCHEDULE(tg_id_, evict_task_, GCONF.plan_cache_evict_interval, true))) {
LOG_WARN("failed to schedule refresh task", K(ret));
} else if (OB_ISNULL(mem_context_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("NULL memory entity returned", K(ret));
} else {
evict_task_.ps_cache_ = this;
inner_allocator_ = &mem_context_->get_allocator();
tenant_id_ = tenant_id;
host_ = const_cast<ObAddr &>(GCTX.self_addr());
inited_ = true;
LOG_INFO("init ps cache success", K(GCTX.self_addr()), K(tenant_id), K(hash_bucket));
}
}
return ret;
}
//for close a session explicitly
int ObPsCache::deref_ps_stmt(const ObPsStmtId stmt_id, bool erase_item/*=false*/)
{
int ret = OB_SUCCESS;
LOG_DEBUG("close ps stmt", K(stmt_id));
ObPsStmtInfoGuard guard;
if (OB_FAIL(get_stmt_info_guard(stmt_id, guard))) {
LOG_WARN("get stmt info guard failed", K(ret), K(stmt_id));
} else {
ObPsStmtInfo *ps_info = guard.get_stmt_info();
const ObPsSqlKey ps_sql_key = ps_info->get_sql_key();
int tmp_ret = OB_SUCCESS;
if (erase_item) { // dec cached ref
if (OB_FAIL(erase_stmt_item(stmt_id, ps_sql_key))) {
LOG_WARN("fail to erase stmt", K(ret));
}
} else { // dec session ref
if (OB_ISNULL(ps_info->get_ps_item())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(*ps_info));
} else {
ps_info->get_ps_item()->dec_ref_count_check_erase();
}
}
if (OB_SUCCESS != (tmp_ret = deref_stmt_info(stmt_id))) {
ret = tmp_ret; //previous ret ignore
LOG_WARN("deref stmt info failed", K(ret), K(stmt_id), K(ps_sql_key));
} else {
LOG_TRACE("deref stmt info success", K(stmt_id), K(ps_sql_key), K(ret));
}
}
return ret;
}
// for ~ObPLFunction
int ObPsCache::deref_all_ps_stmt(const ObIArray<ObPsStmtId> &ps_stmt_ids)
{
int ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < ps_stmt_ids.count(); ++i) {
const ObPsStmtId stmt_id = ps_stmt_ids.at(i);
if (OB_FAIL(deref_ps_stmt(stmt_id))) {
LOG_WARN("failed to close ps stmt", K(ret), K(stmt_id));
}
}
return ret;
}
int ObPsCache::set_mem_conf(const ObPCMemPctConf &conf)
{
int ret = OB_SUCCESS;
if (conf.limit_pct_ != get_mem_limit_pct()) {
set_mem_limit_pct(conf.limit_pct_);
LOG_INFO("update ob_plan_cache_percentage",
"new value", conf.limit_pct_,
"old value", get_mem_limit_pct());
}
if (conf.high_pct_ != get_mem_high_pct()) {
set_mem_high_pct(conf.high_pct_);
LOG_INFO("update ob_plan_cache_evict_high_percentage",
"new value", conf.high_pct_,
"old value", get_mem_high_pct());
}
if (conf.low_pct_ != get_mem_low_pct()) {
set_mem_low_pct(conf.low_pct_);
LOG_INFO("update ob_plan_cache_evict_low_percentage",
"new value", conf.low_pct_,
"old value", get_mem_low_pct());
}
return ret;
}
int ObPsCache::get_stmt_info_guard(const ObPsStmtId ps_stmt_id,
ObPsStmtInfoGuard &guard)
{
int ret = OB_SUCCESS;
ObPsStmtInfo *stmt_info = NULL;
if (OB_FAIL(ref_stmt_info(ps_stmt_id, stmt_info))) {
LOG_WARN("ref stmt info failed", K(ret), K(ps_stmt_id));
} else if (OB_ISNULL(stmt_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_info should not be null", K(ps_stmt_id));
} else {
guard.set_ps_cache(*this);
guard.set_stmt_info(*stmt_info);
guard.set_ps_stmt_id(ps_stmt_id);
LOG_TRACE("success to get stmt info guard", K(ps_stmt_id), K(*stmt_info));
}
return ret;
}
//1.set stmt_id_map_成功,创建新的stmt_item
//2.set stmt_id_map_返回OB_HASH_EXIST时,尝试从stmt_id_map_中获取stmt_item
// 1)获取成功,返回stmt_item
// 2) 报OB_HASH_NOT_EXIST, 则递归调get_or_add_stmt_item,尝试重新创建
int ObPsCache::get_or_add_stmt_item(const ObPsSqlKey &ps_key,
const bool is_contain_tmp_tbl,
ObPsStmtItem *&ps_item_value)
{
int ret = OB_SUCCESS;
ObPsStmtId new_stmt_id = gen_new_ps_stmt_id();
ObPsStmtItem tmp_item_value(new_stmt_id);
ObPsSqlKey tmp_ps_key = ps_key;
tmp_ps_key.inc_id_ = is_contain_tmp_tbl ? new_stmt_id : OB_INVALID_ID;
tmp_item_value.assign_sql_key(tmp_ps_key);
//will deep copy
ObPsStmtItem *new_item_value = NULL;
//由于stmt_id_map_中的value是ObPsStmtItem的指针,因此这里需要copy整个内存
if (!is_inited()) {
ret = OB_NOT_INIT;
LOG_WARN("not init", K(ret));
} else {
WITH_CONTEXT(mem_context_) {
if (OB_FAIL(ObPsSqlUtils::alloc_new_var(*inner_allocator_, tmp_item_value, new_item_value))) {
LOG_WARN("alloc new var failed", K(ret));
} else if (OB_ISNULL(new_item_value)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("new_ps_item_value should not be null");
}
}
}
if (OB_SUCC(ret)) {
const ObPsSqlKey inner_ps_key = new_item_value->get_sql_key();
new_item_value->check_erase_inc_ref_count(); //inc ref count for ps cache, ignore ret;
ret = stmt_id_map_.set_refactored(inner_ps_key, new_item_value);
if (OB_SUCC(ret)) {
//do nothing
LOG_INFO("add stmt item", K(inner_ps_key), K(*new_item_value));
ps_item_value = new_item_value;
} else if (OB_HASH_EXIST == ret) {
ret = OB_SUCCESS;
//may be other session has set
//inc ref count
ObPsStmtItem *tmp_item_value = NULL;
if (OB_FAIL(inner_ref_stmt_item(inner_ps_key, tmp_item_value))) {
LOG_WARN("get stmt item failed", K(ret));
if (OB_HASH_NOT_EXIST == ret) {//stmt item被删除,需要重新创建
if (OB_FAIL(get_or_add_stmt_item(ps_key, is_contain_tmp_tbl, ps_item_value))) {
LOG_WARN("fail to get or add stmt item", K(ret));
}
} else {
LOG_WARN("unexpected error", K(ret), K(inner_ps_key));
}
} else {
if (OB_ISNULL(tmp_item_value)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ps_item value should not be null", K(ret));
} else {
ps_item_value = tmp_item_value;
}
}
//no matter succ or not release
new_item_value->~ObPsStmtItem();
inner_allocator_->free(new_item_value);
} else {
LOG_WARN("unexpected error", K(ret), K(new_stmt_id));
new_item_value->~ObPsStmtItem();
inner_allocator_->free(new_item_value);
}
}
return ret;
}
// 通过plan sql key, 从key -> PsStmtIdMap中获取Item
//will increase ref count
#define LOG_WARN_IGNORE_PS_NOTFOUND(ret, fmt, args...) \
do {\
if (common::OB_HASH_NOT_EXIST == ret) {\
LOG_DEBUG(fmt, ##args);\
} else {\
LOG_WARN(fmt, ##args);\
}\
} while(0);
//从stmt_id_map_获取指定ps_sql_key对应的ps_stmt_item
//1. 若call_back_ret == OB_TRY_EGAIN,说明当前item的ref_cout == 0, 已经准备淘汰;
// 此时,重试直到报OB_HASH_NOT_EXIST,说明其他session已经将item从stmt_id_map_中删除;
// 将OB_HASH_NOT_EXIST错误码返回给外层,期待创建新的ps_stmt_item
//2. 该接口只有在prepare阶段使用,execute和close阶段不应该调用
//3. 该接口会返回三类错误码:
// 1) OB_SUCCESS: 成功获取ps_stmt_item
// 2) OB_HASH_NOT_EXIST: a.stmt_id_map_中本身就不存在当前key b.开始key对应的引用计数为0,重试若干次后变为该错误码
// 3) OB_EGAIN: 尝试了MAX_RETRY_CNT,ps_stmt_item的ref_count仍旧为0
int ObPsCache::inner_ref_stmt_item(const ObPsSqlKey &ps_sql_key,
ObPsStmtItem *&ps_stmt_item)
{
int ret = OB_SUCCESS;
int callback_ret = OB_SUCCESS;
ObPsStmtItemRefAtomicOp op;
int64_t MAX_RETRY_CNT = 2000;
int64_t retry_cnt = 0;
//get stmt_item and inc ref count
do {
ps_stmt_item = NULL;
ret = stmt_id_map_.read_atomic(ps_sql_key, op);
switch(ret) {
case OB_SUCCESS: {
if (OB_SUCCESS != (callback_ret = op.get_callback_ret())) {
ret = callback_ret;
if (OB_EAGAIN == ret) {
LOG_INFO("try again", K(ret), "stmt_id", ps_sql_key, K(retry_cnt));
ob_usleep(static_cast<uint32_t>(500)); //sleep 500us
}
} else if (OB_FAIL(op.get_value(ps_stmt_item))) {
LOG_WARN("failed to get ps stmt item", K(ret), K(ps_sql_key));
}
break;
}
case OB_HASH_NOT_EXIST: {
LOG_WARN_IGNORE_PS_NOTFOUND(ret, "entry not exist", K(ret), K(ps_sql_key));
break;
}
case OB_EAGAIN: {
LOG_WARN("try again", K(ret), "stmt_id", ps_sql_key, K(retry_cnt));
ob_usleep(static_cast<uint32_t>(500)); //sleep 500us
break;
}
default: {
LOG_WARN("failed to get ps stmt item", K(ret), K(ps_sql_key));
}
}
retry_cnt++;
} while(OB_EAGAIN == ret && retry_cnt < MAX_RETRY_CNT);
return ret;
}
int ObPsCache::deref_stmt_item(const ObPsSqlKey &ps_sql_key)
{
int ret = OB_SUCCESS;
ObPsStmtItemDerefAtomicOp op;
if (OB_FAIL(stmt_id_map_.read_atomic(ps_sql_key, op))) {
LOG_WARN("deref stmt item failed", K(ps_sql_key), K(ret));
} else if (op.get_ret() != OB_SUCCESS) {
ret = OB_ERR_UNEXPECTED;
}
return ret;
}
int ObPsCache::ref_stmt_item(const ObPsSqlKey &ps_sql_key, ObPsStmtItem *&stmt_item)
{
int ret = OB_SUCCESS;
stmt_item = nullptr;
if (ps_sql_key.ps_sql_.empty()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("empty sql", K(ret), K(ps_sql_key));
} else if (OB_FAIL(inner_ref_stmt_item(ps_sql_key, stmt_item))) {
LOG_WARN_IGNORE_PS_NOTFOUND(ret, "ps item value not exist", K(ret), K(ps_sql_key));
} else if (OB_ISNULL(stmt_item)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get stmt id failed", K(ret));
}
return ret;
}
int ObPsCache::get_or_add_stmt_info(const PsCacheInfoCtx &info_ctx,
const ObResultSet &result,
ObSchemaGetterGuard &schema_guard,
ObPsStmtItem *ps_item,
ObPsStmtInfo *&ref_ps_info)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(ps_item)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(ps_item));
} else if (OB_ISNULL(info_ctx.normalized_sql_.ptr())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("normalized sql is null", K(ret), K(ps_item));
} else if (OB_ISNULL(info_ctx.raw_params_) || OB_ISNULL(info_ctx.fixed_param_idx_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("param is null", K(ret));
} else if (OB_FAIL(ref_stmt_info(ps_item->get_ps_stmt_id(), ref_ps_info))) {
if (OB_HASH_NOT_EXIST == ret) {
ret = OB_SUCCESS;
int64_t tenant_version = OB_INVALID_VERSION;
ObArenaAllocator allocator;
ObPsStmtInfo tmp_stmt_info(&allocator);
tmp_stmt_info.assign_sql_key(*ps_item);
tmp_stmt_info.set_stmt_type(info_ctx.stmt_type_);
tmp_stmt_info.set_ps_item(ps_item);
// calc check_sum with normalized sql
uint64_t ps_stmt_checksum = ob_crc64(info_ctx.normalized_sql_.ptr(),
info_ctx.normalized_sql_.length()); // actual is crc32
tmp_stmt_info.set_ps_stmt_checksum(ps_stmt_checksum);
if (OB_FAIL(schema_guard.get_schema_version(tenant_id_, tenant_version))) {
LOG_WARN("fail to get tenant version", K(ret), K(tenant_id_));
} else if (FALSE_IT(tmp_stmt_info.set_tenant_version(tenant_version))) {
// do nothing
} else if (OB_FAIL(tmp_stmt_info.assign_no_param_sql(info_ctx.no_param_sql_))) {
LOG_WARN("fail to assign no param sql", K(ret), K(info_ctx.no_param_sql_));
} else if (OB_FAIL(tmp_stmt_info.assign_raw_sql(info_ctx.raw_sql_))) {
LOG_WARN("fail to assign rule name", K(ret), K(info_ctx.raw_sql_));
} else if (OB_FAIL(tmp_stmt_info.assign_fixed_raw_params(*info_ctx.fixed_param_idx_,
*info_ctx.raw_params_))) {
LOG_WARN("fail to assign raw params failed", KPC(info_ctx.fixed_param_idx_), K(ret));
} else if (OB_FAIL(fill_ps_stmt_info(result, info_ctx.param_cnt_,
tmp_stmt_info, info_ctx.num_of_returning_into_))) {
LOG_WARN("fill ps stmt info failed", K(ret));
} else if (OB_FAIL(add_stmt_info(*ps_item, tmp_stmt_info, ref_ps_info))) {
LOG_WARN("add stmt info failed", K(ret), K(*ps_item), K(tmp_stmt_info));
}
} else {
LOG_WARN("fail to get stmt info", K(ret), K(*ps_item));
}
}
return ret;
}
// may parallel execute by multi_thread
int ObPsCache::erase_stmt_item(ObPsStmtId stmt_id, const ObPsSqlKey &ps_key)
{
int ret = OB_SUCCESS;
ObPsStmtItem *ps_item = NULL;
/*
* Thread A Thread B
* Get stmt_item successfully Get stmt_item successfully
*
* Check stmt_info expired
* Check stmt_info expired
*
* Delete stmt_item by key(db_id, ps_sql)
*
*
*
* Add stmt_item with (db_id, ps_sql) as key
*
* Delete stmt_item by key(db_id, ps_sql)
* The item added in the previous step of thread A is deleted
*
*
*/
ObPsStmtItemEraseAtomicOp op(stmt_id);
if (OB_FAIL(stmt_id_map_.read_atomic(ps_key, op))) {
if (OB_HASH_NOT_EXIST == ret) {
LOG_INFO("erased by others", K(ret), K(ps_key));
ret = OB_SUCCESS;
} else {
LOG_WARN("failed to get ps stmt item", K(ps_key), K(ret));
}
} else if (op.need_erase()) {
if (OB_FAIL(stmt_id_map_.erase_refactored(ps_key, &ps_item))) {
if (OB_HASH_NOT_EXIST == ret) {
LOG_INFO("erased by others", K(ret), K(ps_key));
ret = OB_SUCCESS;
} else {
LOG_WARN("fail to erase stmt info", K(ps_key), K(ret));
}
} else {
ps_item->dec_ref_count_check_erase();
}
}
return ret;
}
int ObPsCache::get_all_stmt_id(ObIArray<ObPsStmtId> *id_array)
{
int ret = OB_SUCCESS;
ObGetAllStmtIdOp op(id_array);
if (OB_ISNULL(id_array)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("id_array is NULL", K(ret));
} else if (!is_inited()) {
ret = OB_NOT_INIT;
LOG_WARN("ps_cache is not init yet", K(ret));
} else if (OB_FAIL(stmt_info_map_.foreach_refactored(op))) {
LOG_WARN("traverse stmt_info_map_ failed", K(ret));
} else if (OB_FAIL(op.get_callback_ret())) {
LOG_WARN("traverse stmt_info_map_ failed", K(ret));
}
return ret;
}
int ObPsCache::fill_ps_stmt_info(const ObResultSet &result,
int64_t param_cnt,
ObPsStmtInfo &ps_stmt_info,
int32_t returning_into_parm_num) const
{
int ret = OB_SUCCESS;
const ParamsFieldIArray *params = result.get_param_fields();
const ColumnsFieldIArray *columns = result.get_field_columns();
const ObSqlCtx *sql_ctx = result.get_exec_context().get_sql_ctx();
if (OB_ISNULL(params) || OB_ISNULL(columns) || OB_ISNULL(sql_ctx)
|| OB_ISNULL(ps_stmt_info.get_inner_allocator())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(params), K(columns), K(sql_ctx), K(ret));
} else if (OB_FAIL(ps_stmt_info.reserve_ps_meta_fields(params->count(),
columns->count()))) {
LOG_WARN("fail to reserve ps meta field", K(ret));
}
for (int i = 0; OB_SUCC(ret) && i < params->count(); ++i) {
if (OB_FAIL(ps_stmt_info.add_param_field(params->at(i)))) {
LOG_WARN("add param field failed", K(ret), K(i));
}
}
for (int i = 0; OB_SUCC(ret) && i < columns->count(); ++i) {
if (OB_FAIL(ps_stmt_info.add_column_field(columns->at(i)))) {
LOG_WARN("add filed column failed", K(ret), K(i));
}
}
if (OB_SUCC(ret) && result.get_ref_objects().count() > 0) {
int64_t size = result.get_ref_objects().count() * sizeof(ObSchemaObjVersion);
char *buf = NULL;
if (NULL == (buf = (char*)ps_stmt_info.get_inner_allocator()->alloc(size))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to alloc memory", K(ret), K(size));
} else {
for (int64_t i = 0; i < result.get_ref_objects().count(); i++) {
ObSchemaObjVersion *obj = new(buf+i * sizeof(ObSchemaObjVersion))ObSchemaObjVersion();
*obj = result.get_ref_objects().at(i);
}
ps_stmt_info.set_dep_objs(reinterpret_cast<ObSchemaObjVersion*>(buf),
result.get_ref_objects().count());
}
}
if (OB_SUCC(ret)) {
ps_stmt_info.set_question_mark_count(param_cnt);
// only used when returning into
ps_stmt_info.set_num_of_returning_into(returning_into_parm_num);
}
if (OB_SUCC(ret)) {
if (OB_ISNULL(ps_stmt_info.get_ps_sql().ptr())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sql should already be init", K(ret), K(ps_stmt_info));
}
}
if (OB_SUCC(ret)) {
int64_t info_size = 0;
if (OB_FAIL(ps_stmt_info.get_convert_size(info_size))) {
LOG_WARN("fail to get convert size", K(ret));
} else {
int64_t item_size = ps_stmt_info.get_ps_sql().length() + sizeof(ObPsStmtItem) + 1
+ ps_stmt_info.get_no_param_sql().length() + 1;
ps_stmt_info.set_item_and_info_size(item_size + info_size);
}
}
LOG_INFO("fill ps stmt info", KPC(columns), KPC(params), K(result), K(ps_stmt_info), K(param_cnt));
return ret;
}
int ObPsCache::add_stmt_info(const ObPsStmtItem &ps_item,
const ObPsStmtInfo &ps_info,
ObPsStmtInfo *&ref_ps_info)
{
int ret = OB_SUCCESS;
ObPsStmtInfo *new_info_value = NULL;
if (!ps_info.is_valid()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("stmt_id is invalid", K(ret), K(ps_item), K(ps_info));
} else if (!is_inited()) {
ret = OB_NOT_INIT;
LOG_WARN("not init", K(ret));
} else {
//will deep copy
WITH_CONTEXT(mem_context_) {
if (OB_FAIL(ObPsSqlUtils::alloc_new_var(*inner_allocator_, ps_info, new_info_value))) { // ref_count == 1
LOG_WARN("alloc new var failed", K(ret));
} else if (OB_ISNULL(new_info_value)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("new_ps_item_value should not be null");
}
}
}
if (OB_SUCC(ret)) {
const ObPsStmtId stmt_id = ps_item.get_ps_stmt_id();
new_info_value->check_erase_inc_ref_count(); //inc ref count for ps cache, ignore ret;
ret = stmt_info_map_.set_refactored(stmt_id, new_info_value);
if (OB_SUCC(ret)) {
ref_ps_info = new_info_value;
LOG_INFO("succ to add stmt info", K(ps_item), K(*new_info_value), K(ret));
} else if (OB_HASH_EXIST == ret) {
ret = OB_SUCCESS;
ObPsStmtInfo *tmp_stmt_info = NULL;
//may be other session has set
if (OB_FAIL(ref_stmt_info(ps_item.get_ps_stmt_id(), tmp_stmt_info))) {
LOG_INFO("fail to ref stmt info, set again", K(ret), K(ps_item));
if (OB_HASH_NOT_EXIST == ret) {
if (OB_FAIL(add_stmt_info(ps_item, ps_info, ref_ps_info))) {
LOG_WARN("fail to add stmt info", K(ret));
}
} else {
LOG_WARN("fail to add stmt info", K(ret), K(ps_item), K(ps_info));
}
} else {
if (OB_ISNULL(tmp_stmt_info)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("stmt info should not be null", K(ret), K(ps_item));
} else {
ref_ps_info = tmp_stmt_info;
LOG_INFO("succ to ref stmt info", K(ps_item), K(*tmp_stmt_info), K(ret));
}
}
new_info_value->~ObPsStmtInfo();
inner_allocator_->free(new_info_value);
} else {
LOG_WARN("add new stmt info failed", K(ret), K(ps_item), K(*new_info_value));
new_info_value->~ObPsStmtInfo();
inner_allocator_->free(new_info_value);
}
}
return ret;
}
int ObPsCache::ref_stmt_info(const ObPsStmtId stmt_id, ObPsStmtInfo *&ps_stmt_info)
{
int ret = OB_SUCCESS;
int callback_ret = OB_SUCCESS;
ObPsStmtInfoRefAtomicOp op;
int64_t MAX_RETRY_CNT = 2000;
int64_t retry_cnt = 0;
do {
ps_stmt_info = NULL;
ret = stmt_info_map_.read_atomic(stmt_id, op);
switch(ret) {
case OB_SUCCESS: {
if (OB_SUCCESS != (callback_ret = op.get_callback_ret())) {
ret = callback_ret;
if (OB_EAGAIN == ret) {
LOG_INFO("try again", K(ret), K(stmt_id), K(retry_cnt));
ob_usleep(static_cast<uint32_t>(500)); //sleep 500us
}
} else if (OB_FAIL(op.get_value(ps_stmt_info))) {
LOG_WARN("failed to get ps_stmt_info", K(ret), K(ps_stmt_info));
}
break;
}
case OB_HASH_NOT_EXIST: {
LOG_WARN_IGNORE_PS_NOTFOUND(ret, "entry not exist", K(ret), K(stmt_id));
break;
}
case OB_EAGAIN: {
LOG_INFO("try again", K(ret), K(stmt_id), K(retry_cnt));
ob_usleep(static_cast<uint32_t>(500)); //sleep 500us
break;
}
default: {
LOG_WARN("failed to get ps stmt info", K(ret), K(stmt_id));
}
}
retry_cnt++;
} while(OB_EAGAIN == ret && retry_cnt < MAX_RETRY_CNT);
return ret;
}
int ObPsCache::deref_stmt_info(const ObPsStmtId stmt_id)
{
int ret = OB_SUCCESS;
ObPsStmtInfoDerefAtomicOp op;
ObPsStmtInfo *ps_info = NULL;
ObIAllocator *allocator = NULL;
if (OB_FAIL(stmt_info_map_.read_atomic(stmt_id, op))) {
LOG_WARN("deref stmt info failed", K(stmt_id), K(ret));
} else if (op.get_ret() != OB_SUCCESS) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("deref stmt info failed", K(ret));
} else if (op.is_erase()) {
if (OB_FAIL(stmt_info_map_.erase_refactored(stmt_id, &ps_info))) {
LOG_WARN("fail to erase stmt info", K(stmt_id), K(ret));
} else if (OB_ISNULL(ps_info)) {
LOG_INFO("ps info is null in map", K(stmt_id));
// do nothing
} else if (OB_ISNULL(allocator = ps_info->get_external_allocator())) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(allocator), K(ret));
} else {
LOG_INFO("free ps info", K(*ps_info), K(stmt_id));
ps_info->~ObPsStmtInfo();
allocator->free(ps_info);
}
LOG_INFO("erase stmt info", K(stmt_id), K(ret));
}
return ret;
}
struct ObDumpPsItem
{
int operator()(common::hash::HashMapPair<ObPsSqlKey, ObPsStmtItem *>&entry)
{
int ret = common::OB_SUCCESS;
if (OB_ISNULL(entry.second)) {
LOG_INFO("ps item is null", "stmt_key", entry.first);
} else {
LOG_INFO("dump ps item", "ps_item", *entry.second);
}
return ret;
}
};
struct ObDumpPsInfo
{
int operator()(common::hash::HashMapPair<ObPsStmtId, ObPsStmtInfo *>&entry)
{
int ret = common::OB_SUCCESS;
if (OB_ISNULL(entry.second)) {
LOG_INFO("ps info is null", "stmt_id", entry.first);
} else {
LOG_INFO("dump ps info", "stmt_id", entry.first, "ps_info", *entry.second);
}
return ret;
}
};
class PsTimeCmp
{
public:
bool operator() (std::pair<ObPsStmtId, int64_t> left, std::pair<ObPsStmtId, int64_t> right)
{
return left.second < right.second;
}
};
int ObPsCache::cache_evict()
{
return inner_cache_evict(false/*is_evict_all*/);
}
int ObPsCache::cache_evict_all_ps()
{
return inner_cache_evict(true/*is_evict_all*/);
}
int ObPsCache::inner_cache_evict(bool is_evict_all)
{
int ret = OB_SUCCESS;
// There is a concurrency problem between the regularly triggered ps cache evict task and
// the manually triggered flush ps cache evict task. There is also a concurrency problem between
// the flush ps cache evict tasks triggered at the same time in different sessions.
// The same ps stmt may be added to the closed list by different ps cache evict tasks
// at the same time, so mutex is used here to make all flush ps cache evict tasks execute serially
lib::ObMutexGuard guard(mutex_);
SMART_VARS_2((PsIdClosedTimePairs, expired_stmt_ids),
(PsIdClosedTimePairs, closed_stmt_ids)) {
ObGetClosedStmtIdOp op(&expired_stmt_ids, &closed_stmt_ids);
if (!is_inited()) {
ret = OB_NOT_INIT;
LOG_WARN("ps_cache is not init yet", K(ret));
} else if (OB_FAIL(stmt_info_map_.foreach_refactored(op))) {
LOG_WARN("traverse stmt_info_map_ failed", K(ret));
} else if (OB_FAIL(op.get_callback_ret())) {
LOG_WARN("traverse stmt_info_map_ failed", K(ret));
} else {
LOG_INFO("ps cache evict", K_(tenant_id), K(stmt_id_map_.size()), K(stmt_info_map_.size()),
K(op.get_used_size()), K(get_mem_high()), K(expired_stmt_ids.count()),
K(closed_stmt_ids.count()));
// evict expired ps
for (int64_t i = 0; i < expired_stmt_ids.count(); ++i) { //ignore ret
LOG_TRACE("ps close time", K(i), K(expired_stmt_ids.at(i).first), K(expired_stmt_ids.at(i).second));
if (OB_FAIL(deref_ps_stmt(expired_stmt_ids.at(i).first, true/*erase_stmt*/))) {
LOG_WARN("fail to evict ps stmt", K(ret),
K(expired_stmt_ids.at(i).first), K(expired_stmt_ids.count()));
}
}
if (is_evict_all) {
for (int64_t i = 0; i < closed_stmt_ids.count(); ++i) { //ignore ret
LOG_TRACE("ps close time", K(i), K(closed_stmt_ids.at(i).first), K(closed_stmt_ids.at(i).second));
if (OB_FAIL(deref_ps_stmt(closed_stmt_ids.at(i).first, true/*erase_stmt*/))) {
LOG_WARN("fail to evict ps stmt", K(ret),
K(closed_stmt_ids.at(i).first), K(closed_stmt_ids.count()));
}
}
} else {
if (op.get_used_size() > get_mem_high()) {
std::sort(closed_stmt_ids.begin(), closed_stmt_ids.end(), PsTimeCmp());
for (int64_t i = 0; i < closed_stmt_ids.count() / 2; ++i) { //ignore ret
LOG_TRACE("ps close time", K(i), K(closed_stmt_ids.at(i).first), K(closed_stmt_ids.at(i).second));
if (OB_FAIL(deref_ps_stmt(closed_stmt_ids.at(i).first, true/*erase_stmt*/))) {
LOG_WARN("fail to evict ps stmt", K(ret),
K(closed_stmt_ids.at(i).first), K(closed_stmt_ids.count()));
}
}
}
}
LOG_TRACE("ps cache evict end");
}
}
return ret;
}
void ObPsCache::dump_ps_cache()
{
int ret = OB_SUCCESS;
LOG_INFO("================= ps cache info ========", K_(tenant_id), K(stmt_id_map_.size()), K(stmt_info_map_.size()));
ObDumpPsItem dump_ps_item_op;
ObDumpPsInfo dump_ps_info_op;
if (OB_FAIL(stmt_id_map_.foreach_refactored(dump_ps_item_op))) {
LOG_WARN("fail to dump ps item");
} // ignore ret
if (OB_FAIL(stmt_info_map_.foreach_refactored(dump_ps_info_op))) {
LOG_WARN("fail to dump ps info", K(ret));
} // ignore ret
}
int ObPsCache::mem_total(int64_t &mem_total) const
{
int ret = OB_SUCCESS;
mem_total = 0;
if (true == is_inited()) {
if (OB_ISNULL(inner_allocator_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("inner_allocator_ is NULL", K(ret));
} else {
mem_total = inner_allocator_->total();
}
} else {
LOG_DEBUG("ps cache is not init", K(ret), K(is_inited()));
}
return ret;
}
int ObPsCache::check_schema_version(ObSchemaGetterGuard &schema_guard,
ObPsStmtInfo &stmt_info,
bool &is_expired)
{
int ret = OB_SUCCESS;
is_expired = false;
int64_t new_tenant_version = OB_INVALID_VERSION;
if (OB_FAIL(schema_guard.get_schema_version(tenant_id_, new_tenant_version))) {
LOG_WARN("fail to get tenant version", K(ret), K(tenant_id_));
} else if (new_tenant_version != stmt_info.get_tenant_version()) {
LOG_TRACE("tenant version change", K(stmt_info), K(new_tenant_version), K(tenant_id_));
for (int64_t i = 0; OB_SUCC(ret) && !is_expired && i < stmt_info.get_dep_objs_cnt(); i++) {
ObSchemaObjVersion &obj_version = stmt_info.get_dep_objs()[i];
int64_t new_version = OB_INVALID_VERSION;
if (OB_FAIL(schema_guard.get_schema_version(obj_version.get_schema_type(),
tenant_id_,
obj_version.object_id_,
new_version))) {
LOG_WARN("fail to get schema version", K(ret), K(tenant_id_), K(obj_version));
} else if (new_version != obj_version.version_) {
LOG_INFO("ps cache is expired", K(ret), K(stmt_info), K(tenant_id_),
K(new_version), K(obj_version), KP(&stmt_info));
is_expired = true;
}
}
}
return ret;
}
void ObPsCacheEliminationTask::runTimerTask()
{
ps_cache_->update_memory_conf();
ps_cache_->cache_evict();
}
int ObPsCache::update_memory_conf()
{
int ret = OB_SUCCESS;
ObPCMemPctConf pc_mem_conf;
const char* conf_names[3] =
{
"ob_plan_cache_percentage",
"ob_plan_cache_evict_low_percentage",
"ob_plan_cache_evict_high_percentage"
};
int64_t *conf_values[3] =
{
&pc_mem_conf.limit_pct_,
&pc_mem_conf.low_pct_,
&pc_mem_conf.high_pct_
};
ObArenaAllocator alloc;
ObObj obj_val;
if (tenant_id_ > OB_SYS_TENANT_ID && tenant_id_ <= OB_MAX_RESERVED_TENANT_ID) {
// tenant id between (OB_SYS_TENANT_ID, OB_MAX_RESERVED_TENANT_ID) is a virtual tenant,
// virtual tenants do not have schema
// do nothing
} else {
for (int32_t i = 0; i < 3 && OB_SUCC(ret); ++i) {
if (OB_FAIL(ObBasicSessionInfo::get_global_sys_variable(tenant_id_,
alloc,
ObDataTypeCastParams(),
ObString(conf_names[i]), obj_val))) {
} else if (OB_FAIL(obj_val.get_int(*conf_values[i]))) {
LOG_WARN("failed to get int", K(ret), K(obj_val));
} else if (*conf_values[i] < 0 || *conf_values[i] > 100) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid value of plan cache conf", K(obj_val));
}
} // end for
if (OB_SUCC(ret)) {
if (OB_FAIL(set_mem_conf(pc_mem_conf))) {
SQL_PC_LOG(WARN, "fail to update plan cache mem conf", K(ret));
}
}
}
LOG_TRACE("update plan cache memory config",
"ob_plan_cache_percentage", pc_mem_conf.limit_pct_,
"ob_plan_cache_evict_high_percentage", pc_mem_conf.high_pct_,
"ob_plan_cache_evict_low_percentage", pc_mem_conf.low_pct_,
"tenant_id", tenant_id_);
return ret;
}
} //end of sql
} //end of oceanbase