forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_sql_session_info.cpp
More file actions
4090 lines (3854 loc) · 153 KB
/
Copy pathob_sql_session_info.cpp
File metadata and controls
4090 lines (3854 loc) · 153 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
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) 2025 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define USING_LOG_PREFIX SQL_SESSION
#include "ob_sql_session_info.h"
#include "rpc/ob_rpc_define.h"
#include "pl/ob_pl_package.h"
#include "observer/mysql/obmp_stmt_send_piece_data.h"
#include "observer/ob_server.h"
#include "sql/plan_cache/ob_ps_cache.h"
#include "share/stat/ob_opt_stat_manager.h" // for ObOptStatManager
#include "ob_sess_info_verify.h"
#include "rootserver/ob_tenant_info_loader.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
using namespace oceanbase::share::schema;
using namespace oceanbase::share;
using namespace oceanbase::pl;
using namespace oceanbase::obmysql;
using namespace oceanbase::observer;
static const int64_t DEFAULT_XA_END_TIMEOUT_SECONDS = 60;/*60s*/
const char *state_str[] =
{
"INIT",
"SLEEP",
"ACTIVE",
"QUERY_KILLED",
"SESSION_KILLED",
};
void ObTenantCachedSchemaGuardInfo::reset()
{
schema_guard_.reset();
ref_ts_ = 0;
tenant_id_ = 0;
schema_version_ = 0;
}
int ObTenantCachedSchemaGuardInfo::refresh_tenant_schema_guard(const uint64_t tenant_id)
{
int ret = OB_SUCCESS;
if (OB_FAIL(OBSERVER.get_gctx().schema_service_->get_tenant_schema_guard(tenant_id, schema_guard_))) {
LOG_WARN("get schema guard failed", K(ret), K(tenant_id));
} else if (OB_FAIL(schema_guard_.get_schema_version(tenant_id, schema_version_))) {
LOG_WARN("fail get schema version", K(ret), K(tenant_id));
} else {
ref_ts_ = ObClockGenerator::getClock();
tenant_id_ = tenant_id;
}
return ret;
}
void ObTenantCachedSchemaGuardInfo::try_revert_schema_guard()
{
if (schema_guard_.is_inited()) {
const int64_t MAX_SCHEMA_GUARD_CACHED_TIME = 10 * 1000 * 1000;
if (ObClockGenerator::getClock() - ref_ts_ > MAX_SCHEMA_GUARD_CACHED_TIME) {
LOG_DEBUG("revert schema guard success by sql",
"session_id", schema_guard_.get_session_id(),
K_(tenant_id),
K_(schema_version));
reset();
}
}
}
ObSQLSessionInfo::ObSQLSessionInfo(const uint64_t tenant_id) :
ObVersionProvider(),
ObBasicSessionInfo(tenant_id),
is_inited_(false),
warnings_buf_(),
show_warnings_buf_(),
end_trans_cb_(),
user_priv_set_(),
db_priv_set_(),
curr_trans_start_time_(0),
curr_trans_last_stmt_time_(0),
sess_create_time_(0),
last_refresh_temp_table_time_(0),
has_temp_table_flag_(false),
has_accessed_session_level_temp_table_(false),
enable_early_lock_release_(false),
is_for_trigger_package_(false),
trans_type_(transaction::ObTxClass::USER),
version_provider_(NULL),
config_provider_(NULL),
request_manager_(NULL),
flt_span_mgr_(NULL),
plan_cache_(NULL),
ps_cache_(NULL),
found_rows_(1),
affected_rows_(-1),
global_sessid_(0),
read_uncommited_(false),
trace_recorder_(NULL),
inner_flag_(false),
is_max_availability_mode_(false),
next_client_ps_stmt_id_(0),
is_remote_session_(false),
session_type_(INVALID_TYPE),
curr_session_context_size_(0),
pl_context_(NULL),
pl_can_retry_(true),
plsql_exec_time_(0),
plsql_compile_time_(0),
pl_attach_session_id_(0),
pl_query_sender_(NULL),
pl_ps_protocol_(false),
is_ob20_protocol_(false),
is_session_var_sync_(false),
pl_sync_pkg_vars_(NULL),
inner_conn_(NULL),
enable_role_array_(),
in_definer_named_proc_(false),
priv_user_id_(OB_INVALID_ID),
xa_end_timeout_seconds_(transaction::ObXADefault::OB_XA_TIMEOUT_SECONDS),
xa_last_result_(OB_SUCCESS),
cached_tenant_config_info_(this),
prelock_(false),
proxy_version_(0),
min_proxy_version_ps_(0),
is_ignore_stmt_(false),
ddl_info_(),
is_table_name_hidden_(false),
piece_cache_(NULL),
is_load_data_exec_session_(false),
pl_exact_err_msg_(),
is_varparams_sql_prepare_(false),
got_tenant_conn_res_(false),
got_user_conn_res_(false),
conn_res_user_id_(OB_INVALID_ID),
mem_context_(nullptr),
has_query_executed_(false),
is_latest_sess_info_(false),
cur_exec_ctx_(nullptr),
restore_auto_commit_(false),
sql_req_level_(0),
expect_group_id_(OB_INVALID_ID),
group_id_not_expected_(false),
vid_(OB_INVALID_ID),
vport_(0),
in_bytes_(0),
out_bytes_(0),
client_non_standard_(false),
is_session_sync_support_(false),
job_info_(nullptr),
failover_mode_(false),
service_name_(),
executing_sql_stat_record_(),
unit_gc_min_sup_proxy_version_(0),
has_ccl_rule_(false),
last_update_ccl_cnt_time_(-1)
{
MEMSET(tenant_buff_, 0, sizeof(share::ObTenantSpaceFetcher));
MEMSET(vip_buf_, 0, sizeof(vip_buf_));
}
ObSQLSessionInfo::~ObSQLSessionInfo()
{
plan_cache_ = NULL;
destroy(false);
}
int ObSQLSessionInfo::init(uint32_t sessid, uint64_t proxy_sessid,
common::ObIAllocator *bucket_allocator, const ObTZInfoMap *tz_info, int64_t sess_create_time,
uint64_t tenant_id, int64_t client_create_time)
{
UNUSED(tenant_id);
int ret = OB_SUCCESS;
static const int64_t PS_BUCKET_NUM = 64;
if (OB_FAIL(ObBasicSessionInfo::init(sessid, proxy_sessid, bucket_allocator, tz_info))) {
LOG_WARN("fail to init basic session info", K(ret));
} else if (!is_acquire_from_pool() &&
OB_FAIL(package_state_map_.create(hash::cal_next_prime(4),
ObMemAttr(orig_tenant_id_, "PackStateMap")))) {
LOG_WARN("create package state map failed", K(ret));
} else if (!is_acquire_from_pool() &&
OB_FAIL(sequence_currval_map_.create(hash::cal_next_prime(32),
ObMemAttr(orig_tenant_id_, "SequenceMap")))) {
LOG_WARN("create sequence current value map failed", K(ret));
} else if (!is_acquire_from_pool() &&
OB_FAIL(contexts_map_.create(hash::cal_next_prime(32),
ObMemAttr(orig_tenant_id_, "ContextsMap")))) {
LOG_WARN("create contexts map failed", K(ret));
} else {
curr_session_context_size_ = 0;
if (is_obproxy_mode()) {
sess_create_time_ = sess_create_time;
} else {
sess_create_time_ = ObTimeUtility::current_time();
}
set_client_create_time(client_create_time);
const char *sup_proxy_min_version = "1.8.4";
const char *gc_min_sup_proxy_version = "1.0.0.0";
min_proxy_version_ps_ = 0;
unit_gc_min_sup_proxy_version_ = 0;
if (OB_FAIL(ObClusterVersion::get_version(sup_proxy_min_version, min_proxy_version_ps_))) {
LOG_WARN("failed to get version", K(ret));
} else if (OB_FAIL(ObClusterVersion::get_version(gc_min_sup_proxy_version,
unit_gc_min_sup_proxy_version_))) {
LOG_WARN("failed to get version", K(ret));
} else {
is_inited_ = true;
refresh_temp_tables_sess_active_time();
}
}
if (OB_FAIL(ret)) {
package_state_map_.clear();
sequence_currval_map_.clear();
contexts_map_.clear();
sock_fd_map_.clear();
}
return ret;
}
//for test
int ObSQLSessionInfo::test_init(uint32_t version, uint32_t sessid, uint64_t proxy_sessid,
common::ObIAllocator *bucket_allocator)
{
int ret = OB_SUCCESS;
UNUSED(version);
if (OB_FAIL(ObBasicSessionInfo::test_init(sessid, proxy_sessid, bucket_allocator))) {
LOG_WARN("fail to init basic session info", K(ret));
} else {
is_inited_ = true;
}
return ret;
}
void ObSQLSessionInfo::reset(bool skip_sys_var)
{
if (is_inited_) {
// ObVersionProvider::reset();
reset_all_package_changed_info();
warnings_buf_.reset();
show_warnings_buf_.reset();
end_trans_cb_.reset(),
audit_record_.reset();
user_priv_set_ = 0;
db_priv_set_ = 0;
curr_trans_start_time_ = 0;
curr_trans_last_stmt_time_ = 0;
sess_create_time_ = 0;
last_refresh_temp_table_time_ = 0;
has_temp_table_flag_ = false;
has_accessed_session_level_temp_table_ = false;
is_for_trigger_package_ = false;
trans_type_ = transaction::ObTxClass::USER;
version_provider_ = NULL;
config_provider_ = NULL;
request_manager_ = NULL;
flt_span_mgr_ = NULL;
MEMSET(tenant_buff_, 0, sizeof(share::ObTenantSpaceFetcher));
ps_cache_ = NULL;
found_rows_ = 1;
affected_rows_ = -1;
global_sessid_ = 0;
read_uncommited_ = false;
trace_recorder_ = NULL;
inner_flag_ = false;
is_max_availability_mode_ = false;
enable_early_lock_release_ = false;
ps_session_info_map_.reuse();
ps_name_id_map_.reuse();
in_use_ps_stmt_id_set_.reuse();
next_client_ps_stmt_id_ = 0;
is_remote_session_ = false;
session_type_ = INVALID_TYPE;
package_state_map_.reuse();
sequence_currval_map_.reuse();
sock_fd_map_.reuse();
curr_session_context_size_ = 0;
pl_context_ = NULL;
pl_can_retry_ = true;
plsql_exec_time_ = 0;
plsql_compile_time_ = 0;
pl_attach_session_id_ = 0;
pl_query_sender_ = NULL;
pl_ps_protocol_ = false;
if (pl_cursor_cache_.is_inited()) {
// when select GV$OPEN_CURSOR, we will add get_thread_data_lock to fetch pl_cursor_map_
// so we need get_thread_data_lock there
ObSQLSessionInfo::LockGuard lock_guard(get_thread_data_lock());
pl_cursor_cache_.reset();
}
inner_conn_ = NULL;
session_stat_.reset();
pl_sync_pkg_vars_ = NULL;
cached_schema_guard_info_.reset();
enable_role_array_.reset();
in_definer_named_proc_ = false;
priv_user_id_ = OB_INVALID_ID;
xa_end_timeout_seconds_ = transaction::ObXADefault::OB_XA_TIMEOUT_SECONDS;
xa_last_result_ = OB_SUCCESS;
prelock_ = false;
proxy_version_ = 0;
min_proxy_version_ps_ = 0;
ddl_info_.reset();
if (OB_NOT_NULL(mem_context_)) {
destroy_contexts_map(contexts_map_, mem_context_->get_malloc_allocator());
DESTROY_CONTEXT(mem_context_);
mem_context_ = NULL;
}
contexts_map_.reuse();
cur_exec_ctx_ = nullptr;
plan_cache_ = NULL;
client_app_info_.reset();
has_query_executed_ = false;
flt_control_info_.reset();
is_send_control_info_ = false;
trace_enable_ = false;
auto_flush_trace_ = false;
coninfo_set_by_sess_ = false;
is_ob20_protocol_ = false;
is_session_var_sync_ = false;
is_latest_sess_info_ = false;
int temp_ret = OB_SUCCESS;
sql_req_level_ = 0;
optimizer_tracer_.reset();
expect_group_id_ = OB_INVALID_ID;
flt_control_info_.reset();
group_id_not_expected_ = false;
//call at last time
ObBasicSessionInfo::reset(skip_sys_var);
client_non_standard_ = false;
}
vid_ = OB_INVALID_ID;
vport_ = 0;
in_bytes_ = 0;
out_bytes_ = 0;
MEMSET(vip_buf_, 0, sizeof(vip_buf_));
dblink_sequence_schemas_.reset();
is_session_sync_support_ = false;
need_send_feedback_proxy_info_ = false;
is_lock_session_ = false;
job_info_ = nullptr;
need_send_feedback_proxy_info_ = false;
is_lock_session_ = false;
failover_mode_ = false;
service_name_.reset();
executing_sql_stat_record_.reset();
unit_gc_min_sup_proxy_version_ = 0;
}
void ObSQLSessionInfo::clean_status()
{
reset_all_package_changed_info();
ObBasicSessionInfo::clean_status();
}
int ObSQLSessionInfo::is_force_temp_table_inline(bool &force_inline) const
{
int ret = OB_SUCCESS;
int64_t with_subquery_policy = 0;
force_inline = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
int64_t with_subquery_policy = tenant_config->_with_subquery;
if (2 == with_subquery_policy) {
force_inline = true;
}
}
return ret;
}
int ObSQLSessionInfo::is_force_temp_table_materialize(bool &force_materialize) const
{
int ret = OB_SUCCESS;
int64_t with_subquery_policy = 0;
force_materialize = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
int64_t with_subquery_policy = tenant_config->_with_subquery;
if (1 == with_subquery_policy) {
force_materialize = true;
}
}
return ret;
}
int ObSQLSessionInfo::is_temp_table_transformation_enabled(bool &transformation_enabled) const
{
int ret = OB_SUCCESS;
transformation_enabled = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
transformation_enabled = tenant_config->_xsolapi_generate_with_clause;
}
return ret;
}
int ObSQLSessionInfo::is_groupby_placement_transformation_enabled(bool &transformation_enabled) const
{
int ret = OB_SUCCESS;
transformation_enabled = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
transformation_enabled = tenant_config->_optimizer_group_by_placement;
}
return ret;
}
bool ObSQLSessionInfo::is_in_range_optimization_enabled() const
{
bool bret = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
bret = tenant_config->_enable_in_range_optimization;
}
return bret;
}
int64_t ObSQLSessionInfo::get_inlist_rewrite_threshold() const
{
int64_t threshold = 1000;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
threshold = tenant_config->_inlist_rewrite_threshold;
}
return threshold;
}
int ObSQLSessionInfo::is_better_inlist_enabled(bool &enabled) const
{
int ret = OB_SUCCESS;
enabled = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
enabled = tenant_config->_optimizer_better_inlist_costing;
}
return ret;
}
int ObSQLSessionInfo::is_preserve_order_for_pagination_enabled(bool &enabled) const
{
int ret = OB_SUCCESS;
enabled = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
enabled = tenant_config->_preserve_order_for_pagination;
}
return ret;
}
int ObSQLSessionInfo::is_preserve_order_for_groupby_enabled(bool &enabled) const
{
int ret = OB_SUCCESS;
enabled = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
enabled = tenant_config->_preserve_order_for_groupby;
}
return ret;
}
bool ObSQLSessionInfo::is_pl_prepare_stage() const
{
bool bret = false;
if (OB_NOT_NULL(cur_exec_ctx_) && OB_NOT_NULL(cur_exec_ctx_->get_sql_ctx())) {
bret = cur_exec_ctx_->get_sql_ctx()->is_prepare_stage_;
}
return bret;
}
bool ObSQLSessionInfo::is_index_skip_scan_enabled() const
{
bool bret = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
bret = tenant_config->_optimizer_skip_scan_enabled;
}
return bret;
}
bool ObSQLSessionInfo::is_qualify_filter_enabled() const
{
bool bret = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
bret = tenant_config->_enable_optimizer_qualify_filter;
}
return bret;
}
int ObSQLSessionInfo::is_enable_range_extraction_for_not_in(bool &enabled) const
{
int ret = OB_SUCCESS;
enabled = true;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
enabled = tenant_config->_enable_range_extraction_for_not_in;
}
return ret;
}
bool ObSQLSessionInfo::is_var_assign_use_das_enabled() const
{
bool bret = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
bret = tenant_config->_enable_var_assign_use_das;
}
return bret;
}
bool ObSQLSessionInfo::is_nlj_spf_use_rich_format_enabled() const
{
bool bret = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
bret = tenant_config->_enable_nlj_spf_use_rich_format;
}
return bret;
}
int ObSQLSessionInfo::is_adj_index_cost_enabled(bool &enabled, int64_t &stats_cost_percent) const
{
int ret = OB_SUCCESS;
enabled = false;
stats_cost_percent = 0;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
stats_cost_percent = tenant_config->optimizer_index_cost_adj;
enabled = (0 != stats_cost_percent);
}
return ret;
}
//to control subplan filter and multiple level join group rescan
bool ObSQLSessionInfo::is_spf_mlj_group_rescan_enabled() const
{
bool bret = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
bret = tenant_config->_enable_spf_batch_rescan;
}
return bret;
}
bool ObSQLSessionInfo::enable_parallel_das_dml() const
{
bool bret = false;
int64_t tenant_id = get_effective_tenant_id();
omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
if (tenant_config.is_valid()) {
bret = tenant_config->_enable_parallel_das_dml;
}
return bret;
}
bool ObSQLSessionInfo::is_sqlstat_enabled()
{
bool bret = false;
if (lib::is_diagnose_info_enabled()) {
bret = get_tenant_ob_sqlstat_enable();
// sqlstat has a dependency on the statistics mechanism, so turning off perf event will turn off sqlstat at the same time.
}
return bret;
}
// To avoid frequent ObSchemaMgr access in check_lazy_guard,
// refresh ccl_cnt every 5s
int ObSQLSessionInfo::has_ccl_rules(share::schema::ObSchemaGetterGuard *&schema_guard,
bool &has_ccl_rules)
{
int ret = OB_SUCCESS;
int64_t cur_time = ObTimeUtility::current_time();
if (last_update_ccl_cnt_time_ == -1 || cur_time - last_update_ccl_cnt_time_ > 5 * 1000 * 1000LL) {
uint64_t ccl_cnt = 0;
last_update_ccl_cnt_time_ = cur_time;
if (OB_FAIL(schema_guard->get_ccl_rule_count(get_effective_tenant_id(), ccl_cnt))) {
LOG_WARN("fail to get ccl rule count", K(ret));
}
has_ccl_rule_ = (ccl_cnt > 0);
}
has_ccl_rules = has_ccl_rule_;
return ret;
}
void ObSQLSessionInfo::destroy(bool skip_sys_var)
{
if (is_inited_) {
int ret = OB_SUCCESS;
// The deserialized session should not do end_trans etc cleanup work
// bug:
if (false == get_is_deserialized()) {
if (false == ObSchemaService::g_liboblog_mode_) {
// session disconnects, call ObTransService::end_trans to roll back the transaction,
// Here stmt_timeout = current time + statement query timeout, not the start_time of the last sql, related bug_id : 7961445
set_query_start_time(ObTimeUtility::current_time());
// Here calling end_trans does not require locking, because calling reclaim_value means there is no query concurrently using the session
// Call this function before session.set_session_state(SESSION_KILLED),
bool need_disconnect = false;
// NOTE: only rollback trans if it is started on this node
// otherwise the transaction maybe rollbacked by idle session disconnect
if (is_in_transaction() && (tx_desc_->get_session_id() == get_server_sid())) {
transaction::ObTransID tx_id = get_tx_id();
MAKE_TENANT_SWITCH_SCOPE_GUARD(guard);
// inner session skip check switch tenant, because the inner connection was shared between tenant
if (OB_SUCC(guard.switch_to(get_effective_tenant_id(), !is_inner()))) {
if (OB_FAIL(ObSqlTransControl::rollback_trans(this, need_disconnect))) {
LOG_WARN("fail to rollback transaction", K(get_server_sid()),
"proxy_sessid", get_proxy_sessid(), K(ret));
} else if (false == inner_flag_ && false == is_remote_session_) {
LOG_INFO("end trans successfully",
"sessid", get_server_sid(),
"proxy_sessid", get_proxy_sessid(),
"trans id", tx_id);
}
} else {
LOG_WARN("fail to switch tenant", K(get_effective_tenant_id()), K(ret));
}
}
}
}
// Temporary table cannot be cleaned up when the slave session is destructed
if (false == get_is_deserialized()) {
int temp_ret = drop_temp_tables();
if (OB_UNLIKELY(OB_SUCCESS != temp_ret)) {
LOG_WARN("fail to drop temp tables", K(temp_ret));
}
refresh_temp_tables_sess_active_time();
}
// slave session ps_session_info_map_ is empty, calling close will have no side effects
if (OB_SUCC(ret)) {
if (OB_FAIL(close_all_ps_stmt())) {
LOG_WARN("failed to close all stmt", K(ret));
}
}
//close all cursor
if (pl_cursor_cache_.is_inited()) {
int temp_ret = pl_cursor_cache_.close_all(*this);
if (temp_ret != OB_SUCCESS) {
LOG_WARN("failed to close all cursor", K(ret));
}
}
if (NULL != piece_cache_) {
int temp_ret = piece_cache_->close_all(*this);
if (temp_ret != OB_SUCCESS) {
LOG_WARN("failed to close all piece", K(ret));
}
piece_cache_->~ObPieceCache();
get_session_allocator().free(piece_cache_);
piece_cache_ = NULL;
}
// Non-distributed needs it, distributed also needs it, used for cleaning up the global variable values of package
reset_all_package_state();
reset(skip_sys_var);
is_inited_ = false;
sql_req_level_ = 0;
}
}
int ObSQLSessionInfo::close_ps_stmt(ObPsStmtId client_stmt_id)
{
int ret = OB_SUCCESS;
ObPsSessionInfo *ps_sess_info = NULL;
if (OB_FAIL(get_ps_session_info(client_stmt_id, ps_sess_info))) {
LOG_WARN("fail to get ps session info", K(client_stmt_id), "session_id", get_server_sid(), K(ret));
} else if (OB_ISNULL(ps_sess_info)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("ps session info is null", K(client_stmt_id), "session_id", get_server_sid(), K(ret));
} else {
ObPsStmtId inner_stmt_id = ps_sess_info->get_inner_stmt_id();
ps_sess_info->dec_ref_count();
if (ps_sess_info->need_erase()) {
if (OB_ISNULL(ps_cache_)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("ps cache is null", K(ret));
} else if (OB_FAIL(ps_cache_->deref_ps_stmt(inner_stmt_id))) {
LOG_WARN("close ps stmt failed", K(ret), "session_id", get_server_sid(), K(ret));
}
// Regardless of whether the above was successful, the session info resource needs to be released
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = remove_ps_session_info(client_stmt_id))) {
ret = tmp_ret;
LOG_WARN("remove ps session info failed", K(client_stmt_id),
"session_id", get_server_sid(), K(ret));
}
LOG_TRACE("close ps stmt", K(ret), K(client_stmt_id), K(inner_stmt_id), K(lbt()));
}
}
return ret;
}
int ObSQLSessionInfo::close_all_ps_stmt()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(ps_cache_)) {
// do nothing, session no ps
} else if (!ps_session_info_map_.created()) {
// do nothing, no ps added to map
} else {
PsSessionInfoMap::iterator iter = ps_session_info_map_.begin();
ObPsStmtId inner_stmt_id = OB_INVALID_ID;
for (; iter != ps_session_info_map_.end(); ++iter) { //ignore ret
const ObPsStmtId client_stmt_id = iter->first;
if (OB_FAIL(get_inner_ps_stmt_id(client_stmt_id, inner_stmt_id))) {
LOG_WARN("get_inner_ps_stmt_id failed", K(ret), K(client_stmt_id), K(inner_stmt_id));
} else if (OB_FAIL(ps_cache_->deref_ps_stmt(inner_stmt_id))) {
LOG_WARN("close ps stmt failed", K(ret), K(client_stmt_id), K(inner_stmt_id));
} else if (OB_ISNULL(iter->second)) {
// do nothing
} else {
iter->second->~ObPsSessionInfo();
ps_session_info_allocator_.free(iter->second);
iter->second = NULL;
}
}
ps_session_info_allocator_.reset();
ps_session_info_map_.reuse();
}
return ret;
}
//mysql tenant: If session created temporary tables, direct connection mode: drop temp table when session disconnects;
//oracle tenant, when commit clears data will also call this interface, but only clears transaction-level temporary tables;
// session disconnects then clean up transaction-level and session-level temporary tables;
// Since Oracle temporary tables only clean up data for this session, to avoid RS congestion, do not send to RS and execute by SQL proxy
// For distributed planning, unless ac=1 otherwise hand over to master session for cleanup, deserialized session does nothing
int ObSQLSessionInfo::drop_temp_tables(const bool is_disconn,
const bool is_xa_trans,
const bool is_reset_connection)
{
int ret = OB_SUCCESS;
bool ac = false;
bool is_sess_disconn = is_disconn;
obrpc::ObCommonRpcProxy *common_rpc_proxy = NULL;
if (OB_FAIL(get_autocommit(ac))) {
LOG_WARN("get autocommit error", K(ret), K(ac));
} else if (!(is_inner() && !is_user_session())
&& (get_has_temp_table_flag()
|| has_accessed_session_level_temp_table()
|| has_tx_level_temp_table()
|| is_xa_trans)
&& (!get_is_deserialized() || ac)) {
bool need_drop_temp_table = false;
//mysql: 1. direct connection & session disconnect 2. reset connection
if (OB_SUCC(ret)) {
if ((false == is_obproxy_mode() && is_sess_disconn) || is_reset_connection) {
need_drop_temp_table = true;
}
}
if (need_drop_temp_table) {
LOG_DEBUG("need_drop_temp_table",
K(get_current_query_string()),
K(get_login_tenant_id()),
K(get_effective_tenant_id()),
K(lbt()));
obrpc::ObDDLRes res;
obrpc::ObDropTableArg drop_table_arg;
drop_table_arg.if_exist_ = true;
drop_table_arg.to_recyclebin_ = false;
drop_table_arg.table_type_ = share::schema::TMP_TABLE;
drop_table_arg.session_id_ = get_sessid_for_table();
drop_table_arg.tenant_id_ = get_effective_tenant_id();
drop_table_arg.exec_tenant_id_ = get_effective_tenant_id();
common_rpc_proxy = GCTX.rs_rpc_proxy_;
if (OB_ISNULL(common_rpc_proxy)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("rpc proxy is null", K(ret));
} else {
LOG_INFO("temporary tables dropped due to connection disconnected", K(is_sess_disconn), K(drop_table_arg));
}
}
}
if (OB_FAIL(ret)) {
LOG_WARN("fail to drop temp tables", K(ret),
K(get_effective_tenant_id()), K(get_server_sid()),
K(has_accessed_session_level_temp_table()),
K(is_xa_trans),
K(lbt()));
}
return ret;
}
//proxy mode session creation, disconnection and background scheduled task check:
// If the time since the last update of this session->last_refresh_temp_table_time_ exceeds 1hr
// Then update the last active time of the temporary table created for the session SESSION_ACTIVE_TIME
//oracle temporary table dependency additional __sess_create_time judgment reuse and cleanup, no need to update
void ObSQLSessionInfo::refresh_temp_tables_sess_active_time()
{
int ret = OB_SUCCESS;
const int64_t REFRESH_INTERVAL = 60L * 60L * 1000L * 1000L; // 1hr
obrpc::ObCommonRpcProxy *common_rpc_proxy = NULL;
if (get_has_temp_table_flag() && is_obproxy_mode()) {
int64_t now = ObTimeUtility::current_time();
obrpc::ObAlterTableRes res;
if (now - get_last_refresh_temp_table_time() >= REFRESH_INTERVAL) {
SMART_VAR(obrpc::ObAlterTableArg, alter_table_arg) {
AlterTableSchema *alter_table_schema = &alter_table_arg.alter_table_schema_;
alter_table_arg.session_id_ = get_sessid_for_table();
alter_table_schema->alter_type_ = OB_DDL_ALTER_TABLE;
common_rpc_proxy = GCTX.rs_rpc_proxy_;
alter_table_arg.nls_formats_[ObNLSFormatEnum::NLS_DATE] = ObTimeConverter::COMPAT_OLD_NLS_DATE_FORMAT;
alter_table_arg.nls_formats_[ObNLSFormatEnum::NLS_TIMESTAMP] = ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_FORMAT;
alter_table_arg.nls_formats_[ObNLSFormatEnum::NLS_TIMESTAMP_TZ] = ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_TZ_FORMAT;
alter_table_arg.compat_mode_ = lib::Worker::CompatMode::MYSQL;
if (OB_FAIL(alter_table_schema->alter_option_bitset_.add_member(obrpc::ObAlterTableArg::SESSION_ACTIVE_TIME))) {
LOG_WARN("failed to add member SESSION_ACTIVE_TIME for alter table schema", K(ret));
} else if (OB_FAIL(alter_table_arg.tz_info_wrap_.deep_copy(get_tz_info_wrap()))) {
LOG_WARN("failed to deep copy tz_info_wrap", K(ret));
} else if (OB_FAIL(common_rpc_proxy->alter_table(alter_table_arg, res))) {
LOG_WARN("failed to alter temporary table session active time", K(alter_table_arg), K(ret), K(is_obproxy_mode()));
} else {
LOG_DEBUG("session active time of temporary tables refreshed", K(ret), "last refresh time", get_last_refresh_temp_table_time());
set_last_refresh_temp_table_time(now);
}
}
} else {
LOG_DEBUG("no need to refresh session active time of temporary tables", "last refresh time", get_last_refresh_temp_table_time());
}
}
}
ObMySQLRequestManager* ObSQLSessionInfo::get_request_manager()
{
int ret = OB_SUCCESS;
if (NULL == request_manager_) {
MTL_SWITCH(get_effective_tenant_id()) {
request_manager_ = MTL(obmysql::ObMySQLRequestManager*);
}
}
return request_manager_;
}
void ObSQLSessionInfo::set_show_warnings_buf(int error_code)
{
// if error message didn't insert into THREAD warning buffer,
// insert it into SESSION warning buffer
// if no error at all,
// clear err.
if (OB_SUCCESS != error_code && strlen(warnings_buf_.get_err_msg()) <= 0) {
warnings_buf_.set_error(ob_errpkt_strerror(error_code, false), error_code);
} else if (OB_SUCCESS == error_code) {
warnings_buf_.reset_err();
}
show_warnings_buf_ = warnings_buf_; // show_warnings_buf_ used for show warnings
}
void ObSQLSessionInfo::update_show_warnings_buf()
{
for (int64_t i = 0; i < warnings_buf_.get_readable_warning_count(); i++) {
const ObWarningBuffer::WarningItem *item = warnings_buf_.get_warning_item(i);
if (OB_ISNULL(item)) {
} else if (item->log_level_ == common::ObLogger::UserMsgLevel::USER_WARN) {
show_warnings_buf_.append_warning(item->msg_, item->code_);
} else if (item->log_level_ == common::ObLogger::UserMsgLevel::USER_NOTE) {
show_warnings_buf_.append_note(item->msg_, item->code_);
}
}
}
int ObSQLSessionInfo::get_session_priv_info(share::schema::ObSessionPrivInfo &session_priv) const
{
int ret = OB_SUCCESS;
session_priv.tenant_id_ = get_priv_tenant_id();
session_priv.user_id_ = get_priv_user_id();
session_priv.user_name_ = get_user_name();
session_priv.host_name_ = get_host_name();
session_priv.db_ = get_database_name();
session_priv.user_priv_set_ = user_priv_set_;
session_priv.db_priv_set_ = db_priv_set_;
if (OB_FAIL(get_security_version(session_priv.security_version_))) {
LOG_WARN("failed to get security version", K(ret));
}
return ret;
}
ObPlanCache *ObSQLSessionInfo::get_plan_cache()
{
if (OB_NOT_NULL(plan_cache_)) {
// do nothing
} else {
//release old plancache and get new
ObPCMemPctConf pc_mem_conf;
if (OB_SUCCESS != get_pc_mem_conf(pc_mem_conf)) {
LOG_ERROR_RET(OB_ERR_UNEXPECTED, "fail to get pc mem conf");
plan_cache_ = NULL;
} else {
plan_cache_ = MTL(ObPlanCache*);
if (OB_ISNULL(plan_cache_)) {
LOG_WARN_RET(OB_ERR_UNEXPECTED, "failed to get plan cache");
} else if (MTL_ID() != get_effective_tenant_id()) {
LOG_ERROR_RET(OB_ERR_UNEXPECTED, "unmatched tenant_id", K(MTL_ID()), K(get_effective_tenant_id()));
} else if (plan_cache_->is_inited()) {
// skip update mem conf
} else if (OB_SUCCESS != plan_cache_->set_mem_conf(pc_mem_conf)) {
LOG_ERROR_RET(OB_ERR_UNEXPECTED, "fail to set plan cache memory conf");
}
}
}
return plan_cache_;
}
ObPsCache *ObSQLSessionInfo::get_ps_cache()
{
if (OB_NOT_NULL(ps_cache_)) {
//do nothing
} else {
int ret = OB_SUCCESS;
const uint64_t tenant_id = get_effective_tenant_id();
ObPCMemPctConf pc_mem_conf;
ObMemAttr mem_attr;
mem_attr.label_ = "PsSessionInfo";
mem_attr.tenant_id_ = tenant_id;
mem_attr.ctx_id_ = ObCtxIds::DEFAULT_CTX_ID;
if (OB_FAIL(get_pc_mem_conf(pc_mem_conf))) {
LOG_ERROR("failed to get pc mem conf");
ps_cache_ = NULL;
} else {
ps_cache_ = MTL(ObPsCache*);
if (OB_ISNULL(ps_cache_)) {
// ignore ret
LOG_WARN("failed to get ps cache");
} else if (MTL_ID() != get_effective_tenant_id()) {
LOG_ERROR("unmatched tenant_id", K(MTL_ID()), K(get_effective_tenant_id()));
} else if (!ps_cache_->is_inited() &&
OB_FAIL(ps_cache_->init(common::calculate_scaled_value_by_memory(common::OB_PLAN_CACHE_BUCKET_NUMBER_MIN,
common::OB_PLAN_CACHE_BUCKET_NUMBER), tenant_id))) {
LOG_WARN("failed to init ps cache");
} else {
ps_session_info_allocator_.set_attr(mem_attr);
}
}
}
return ps_cache_;
}
//whether the user has the super privilege
bool ObSQLSessionInfo::has_user_super_privilege() const
{
int ret = false;
if (OB_PRIV_HAS_ANY(user_priv_set_, OB_PRIV_SUPER)) {
ret = true;
}
return ret;
}
//whether the user has the process privilege
bool ObSQLSessionInfo::has_user_process_privilege() const
{
int ret = false;
if (OB_PRIV_HAS_ANY(user_priv_set_, OB_PRIV_PROCESS)) {
ret = true;
}
return ret;
}
//check tenant read_only
int ObSQLSessionInfo::check_global_read_only_privilege(const bool read_only,
const ObSqlTraits &sql_traits)
{
int ret = OB_SUCCESS;
if (!has_user_super_privilege()
&& !is_tenant_changed()
&& read_only) {
/** session1 session2
* insert into xxx;
* set @@global.read_only = 1;
* update xxx (should fail)