forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_sql_session_info.cpp
More file actions
1269 lines (1195 loc) · 45.2 KB
/
ob_sql_session_info.cpp
File metadata and controls
1269 lines (1195 loc) · 45.2 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) 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_SESSION
#include "sql/session/ob_sql_session_info.h"
#include "lib/trace/ob_trace_event.h"
#include "lib/alloc/alloc_func.h"
#include "lib/string/ob_sql_string.h"
#include "lib/rc/ob_rc.h"
#include "sql/plan_cache/ob_plan_cache_manager.h"
#include "sql/ob_sql_utils.h"
#include "sql/ob_sql_trans_control.h"
#include "sql/session/ob_sql_session_mgr.h"
#include "io/easy_io.h"
#include "rpc/ob_rpc_define.h"
#include "observer/omt/ob_tenant_config_mgr.h"
#include "observer/ob_server_struct.h"
#include "observer/mysql/ob_mysql_request_manager.h"
#include "observer/ob_server.h"
#include "share/rc/ob_context.h"
#include "share/rc/ob_tenant_base.h"
#include "sql/resolver/ddl/ob_ddl_stmt.h"
#include "observer/omt/ob_tenant_config_mgr.h"
#include "share/schema/ob_schema_struct.h"
#include "sql/resolver/ddl/ob_create_synonym_stmt.h"
#include "sql/resolver/ddl/ob_drop_synonym_stmt.h"
#include "sql/engine/expr/ob_datum_cast.h"
#include "lib/checksum/ob_crc64.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
using namespace oceanbase::share::schema;
using namespace oceanbase::share;
using namespace oceanbase::obmysql;
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_INFO("revert schema guard success by sql",
"session_id",
schema_guard_.get_session_id(),
K_(tenant_id),
K_(schema_version));
reset();
}
}
}
ObSQLSessionInfo::ObSQLSessionInfo()
: ObVersionProvider(),
ObBasicSessionInfo(),
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),
enable_early_lock_release_(false),
trans_type_(transaction::ObTransType::UNKNOWN),
version_provider_(NULL),
config_provider_(NULL),
plan_cache_manager_(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),
pl_can_retry_(true),
pl_attach_session_id_(0),
last_plan_id_(OB_INVALID_ID),
pl_sync_pkg_vars_(NULL),
inner_conn_(NULL),
use_static_typing_engine_(false),
ctx_mem_context_(nullptr),
enable_role_array_(),
in_definer_named_proc_(false),
priv_user_id_(OB_INVALID_ID),
xa_end_timeout_seconds_(DEFAULT_XA_END_TIMEOUT_SECONDS),
is_external_consistent_(false),
enable_batched_multi_statement_(false),
saved_tenant_info_(0),
sort_area_size_(128 * 1024 * 1024),
last_check_ec_ts_(0),
xa_last_result_(OB_SUCCESS),
prelock_(false),
proxy_version_(0),
min_proxy_version_ps_(0),
is_ignore_stmt_(false)
{}
ObSQLSessionInfo::~ObSQLSessionInfo()
{
if (NULL != plan_cache_) {
plan_cache_->dec_ref_count();
plan_cache_ = NULL;
}
destroy(false);
}
int ObSQLSessionInfo::init(uint32_t version, uint32_t sessid, uint64_t proxy_sessid,
common::ObIAllocator* bucket_allocator, const ObTZInfoMap* tz_info, int64_t sess_create_time, uint64_t tenant_id)
{
UNUSED(tenant_id);
int ret = OB_SUCCESS;
static const int64_t PS_BUCKET_NUM = 64;
if (OB_FAIL(ObBasicSessionInfo::init(version, 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(ps_session_info_map_.create(hash::cal_next_prime(PS_BUCKET_NUM),
ObModIds::OB_HASH_BUCKET_PS_SESSION_INFO,
ObModIds::OB_HASH_NODE_PS_SESSION_INFO))) {
LOG_WARN("create ps session info map failed", K(ret));
} else if (!is_acquire_from_pool() && OB_FAIL(ps_name_id_map_.create(hash::cal_next_prime(PS_BUCKET_NUM),
ObModIds::OB_HASH_BUCKET_PS_SESSION_INFO,
ObModIds::OB_HASH_NODE_PS_SESSION_INFO))) {
LOG_WARN("create ps name id map failed", K(ret));
} else if (!is_acquire_from_pool() &&
OB_FAIL(sequence_currval_map_.create(
hash::cal_next_prime(32), ObModIds::OB_HASH_BUCKET, ObModIds::OB_HASH_NODE))) {
LOG_WARN("create sequence current value map failed", K(ret));
} else {
if (is_obproxy_mode()) {
sess_create_time_ = sess_create_time;
} else {
sess_create_time_ = ObTimeUtility::current_time();
}
const char* sup_proxy_min_version = "1.8.4";
min_proxy_version_ps_ = 0;
if (OB_FAIL(ObClusterVersion::get_version(sup_proxy_min_version, min_proxy_version_ps_))) {
LOG_WARN("failed to get version", K(ret));
} else {
is_inited_ = true;
refresh_temp_tables_sess_active_time();
}
}
get_trans_desc().set_audit_record(&audit_record_);
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;
if (OB_FAIL(ObBasicSessionInfo::test_init(version, 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();
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;
trans_type_ = transaction::ObTransType::UNKNOWN;
version_provider_ = NULL;
config_provider_ = NULL;
plan_cache_manager_ = NULL;
if (NULL != ps_cache_) {
ps_cache_->dec_ref_count();
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();
next_client_ps_stmt_id_ = 0;
is_remote_session_ = true;
session_type_ = INVALID_TYPE;
sequence_currval_map_.reuse();
pl_can_retry_ = true;
pl_attach_session_id_ = 0;
last_plan_id_ = OB_INVALID_ID;
inner_conn_ = NULL;
session_stat_.reset();
pl_sync_pkg_vars_ = NULL;
ObBasicSessionInfo::reset(skip_sys_var);
if (ctx_mem_context_ != nullptr) {
DESTROY_CONTEXT(ctx_mem_context_);
ctx_mem_context_ = nullptr;
}
cached_schema_guard_info_.reset();
enable_role_array_.reset();
in_definer_named_proc_ = false;
priv_user_id_ = OB_INVALID_ID;
xa_end_timeout_seconds_ = DEFAULT_XA_END_TIMEOUT_SECONDS;
is_external_consistent_ = false;
enable_batched_multi_statement_ = false;
saved_tenant_info_ = 0;
sort_area_size_ = 128 * 1024 * 1024;
last_check_ec_ts_ = 0;
xa_last_result_ = OB_SUCCESS;
prelock_ = false;
proxy_version_ = 0;
min_proxy_version_ps_ = 0;
}
}
void ObSQLSessionInfo::clean_status()
{
ObBasicSessionInfo::clean_status();
}
bool ObSQLSessionInfo::is_encrypt_tenant()
{
bool ret = false;
return ret;
}
void ObSQLSessionInfo::destroy(bool skip_sys_var)
{
if (is_inited_) {
int ret = OB_SUCCESS;
if (rpc::is_io_thread()) {
LOG_WARN("free session at IO thread",
"sessid",
get_sessid(),
"proxy_sessid",
get_proxy_sessid(),
"version",
get_version());
}
if (false == get_is_deserialized()) {
if (false == ObSchemaService::g_liboblog_mode_) {
set_query_start_time(ObTimeUtility::current_time());
bool has_called_txs_end_trans = false;
if (OB_FAIL(ObSqlTransControl::end_trans(GCTX.par_ser_, this, has_called_txs_end_trans))) {
LOG_WARN("fail to rollback transaction",
K(get_sessid()),
"proxy_sessid",
get_proxy_sessid(),
K(has_called_txs_end_trans),
K(ret));
} else if (false == inner_flag_ && false == is_remote_session_) {
LOG_INFO("end trans successfully",
"sessid",
get_sessid(),
"proxy_sessid",
get_proxy_sessid(),
"version",
get_version(),
"trans id",
get_trans_desc().get_trans_id(),
K(has_called_txs_end_trans));
}
}
}
if (false == get_is_deserialized()) {
drop_temp_tables();
refresh_temp_tables_sess_active_time();
}
if (OB_SUCC(ret)) {
if (OB_FAIL(close_all_ps_stmt())) {
LOG_WARN("failed to close all stmt", K(ret));
}
}
reset(skip_sys_var);
is_inited_ = false;
}
}
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_sessid(), 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_sessid(), 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_sessid(), K(ret));
}
// release resource of ps stmt in any case.
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_sessid(), 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;
PsSessionInfoMap::iterator iter = ps_session_info_map_.begin();
if (OB_ISNULL(ps_cache_)) {
// do nothing, session no ps
} else {
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_map_.reuse();
}
return ret;
}
int ObSQLSessionInfo::delete_from_oracle_temp_tables(const obrpc::ObDropTableArg& const_drop_table_arg)
{
int ret = OB_SUCCESS;
common::ObSqlString sql;
int64_t affect_rows = 0;
common::ObMySQLProxy* sql_proxy = GCTX.sql_proxy_;
common::ObCommonSqlProxy* user_sql_proxy;
common::ObOracleSqlProxy oracle_sql_proxy;
ObSchemaGetterGuard schema_guard;
const ObDatabaseSchema* database_schema = NULL;
ObSEArray<const ObSimpleTableSchemaV2*, 512> table_schemas;
obrpc::ObDropTableArg& drop_table_arg = const_cast<obrpc::ObDropTableArg&>(const_drop_table_arg);
const share::schema::ObTableType table_type = drop_table_arg.table_type_;
const uint64_t tenant_id = drop_table_arg.tenant_id_;
if (OB_FAIL(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_ISNULL(sql_proxy)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sql proxy is null", K(ret));
} else if (OB_FAIL(oracle_sql_proxy.init(sql_proxy->get_pool()))) {
LOG_WARN("init oracle sql proxy failed", K(ret));
} else if (OB_FAIL(schema_guard.get_table_schemas_in_tenant(tenant_id, table_schemas))) {
LOG_WARN("fail to get table schema", K(ret), K(tenant_id));
} else {
user_sql_proxy = &oracle_sql_proxy;
for (int64_t i = 0; i < table_schemas.count() && OB_SUCC(ret); i++) {
const ObSimpleTableSchemaV2* table_schema = table_schemas.at(i);
if (OB_ISNULL(table_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("got invalid schema", K(ret), K(i));
} else if (tenant_id != table_schema->get_tenant_id()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("tenant_id not match", K(ret), K(tenant_id), "table_id", table_schema->get_table_id());
} else if ((TMP_TABLE_ORA_SESS == table_type && table_schema->is_oracle_tmp_table()) ||
(TMP_TABLE_ORA_TRX == table_type && table_schema->is_oracle_trx_tmp_table())) {
database_schema = NULL;
if (OB_FAIL(schema_guard.get_database_schema(table_schema->get_database_id(), database_schema))) {
LOG_WARN("failed to get database schema", K(ret));
} else if (OB_ISNULL(database_schema)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("database schema is null", K(ret));
} else if (database_schema->is_in_recyclebin() || table_schema->is_in_recyclebin()) {
LOG_DEBUG("skip table schema in recyclebin", K(*table_schema));
} else {
if (0 == drop_table_arg.sess_create_time_) {
ret = sql.assign_fmt("DELETE FROM \"%.*s\".\"%.*s\" WHERE %s = %ld",
database_schema->get_database_name_str().length(),
database_schema->get_database_name_str().ptr(),
table_schema->get_table_name_str().length(),
table_schema->get_table_name_str().ptr(),
OB_HIDDEN_SESSION_ID_COLUMN_NAME,
drop_table_arg.session_id_);
} else {
ret = sql.assign_fmt("DELETE FROM \"%.*s\".\"%.*s\" WHERE %s = %ld AND %s <> %ld",
database_schema->get_database_name_str().length(),
database_schema->get_database_name_str().ptr(),
table_schema->get_table_name_str().length(),
table_schema->get_table_name_str().ptr(),
OB_HIDDEN_SESSION_ID_COLUMN_NAME,
drop_table_arg.session_id_,
OB_HIDDEN_SESS_CREATE_TIME_COLUMN_NAME,
drop_table_arg.sess_create_time_);
}
if (OB_SUCC(ret)) {
// THIS_WORKER.set_timeout_ts(get_query_start_time() + 2 * 1000L * 1000L);
int64_t cur_timeout_backup = THIS_WORKER.get_timeout_ts();
THIS_WORKER.set_timeout_ts(ObTimeUtility::current_time() + OB_MAX_USER_SPECIFIED_TIMEOUT);
if (OB_FAIL(user_sql_proxy->write(tenant_id, sql.ptr(), affect_rows))) {
LOG_WARN("execute sql failed", K(ret), K(sql));
} else {
LOG_INFO("succeed to delete rows in oracle temporary table", K(sql), K(affect_rows));
}
THIS_WORKER.set_timeout_ts(cur_timeout_backup);
}
}
}
}
}
return ret;
}
int ObSQLSessionInfo::drop_temp_tables(const bool is_sess_disconn_const)
{
int ret = OB_SUCCESS;
bool ac = false;
bool is_sess_disconn = is_sess_disconn_const;
obrpc::ObCommonRpcProxy* common_rpc_proxy = NULL;
if (OB_FAIL(get_autocommit(ac))) {
LOG_WARN("get autocommit error", K(ret), K(ac));
} else if ((get_has_temp_table_flag() || get_trans_desc().is_trx_level_temporary_table_involved()) &&
(!get_is_deserialized() || ac)) {
bool need_drop_temp_table = false;
if (!is_oracle_mode()) {
if (false == is_obproxy_mode() && is_sess_disconn) {
need_drop_temp_table = true;
}
} else {
if (false == is_sess_disconn || false == is_obproxy_mode()) {
need_drop_temp_table = true;
if (is_sess_disconn && get_is_deserialized() && ac) {
is_sess_disconn = false;
}
}
}
if (need_drop_temp_table) {
obrpc::ObDropTableArg drop_table_arg;
drop_table_arg.if_exist_ = true;
drop_table_arg.to_recyclebin_ = false;
if (false == is_sess_disconn) {
drop_table_arg.table_type_ = share::schema::TMP_TABLE_ORA_TRX;
} else if (is_oracle_mode()) {
drop_table_arg.table_type_ = share::schema::TMP_TABLE_ORA_SESS;
} else {
drop_table_arg.table_type_ = share::schema::TMP_TABLE;
}
drop_table_arg.session_id_ = get_sessid_for_table();
drop_table_arg.tenant_id_ = get_login_tenant_id();
common_rpc_proxy = GCTX.rs_rpc_proxy_;
if (is_oracle_mode() && OB_FAIL(delete_from_oracle_temp_tables(drop_table_arg))) {
LOG_WARN("failed to delete from oracle temporary table", K(drop_table_arg), K(ret));
} else if (!is_oracle_mode() && OB_FAIL(common_rpc_proxy->drop_table(drop_table_arg))) {
LOG_WARN("failed to drop temporary table", K(drop_table_arg), K(ret));
} else {
LOG_DEBUG("temporary tables dropped due to connection disconnected", K(is_sess_disconn), K(drop_table_arg));
}
}
}
return ret;
}
int ObSQLSessionInfo::drop_reused_oracle_temp_tables()
{
int ret = OB_SUCCESS;
// obrpc::ObCommonRpcProxy *common_rpc_proxy = NULL;
if (false == get_is_deserialized() && !GCTX.is_standby_cluster()) {
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_ORA_SESS;
drop_table_arg.session_id_ = get_sessid_for_table();
drop_table_arg.tenant_id_ = get_login_tenant_id();
drop_table_arg.sess_create_time_ = get_sess_create_time();
// common_rpc_proxy = GCTX.rs_rpc_proxy_;
if (OB_FAIL(delete_from_oracle_temp_tables(drop_table_arg))) {
// if (OB_FAIL(common_rpc_proxy->drop_table(drop_table_arg))) {
LOG_WARN("failed to drop reused temporary table", K(drop_table_arg), K(ret));
} else {
LOG_DEBUG("succeed to delete old rows for oracle temporary table", K(drop_table_arg));
}
}
return ret;
}
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() && !is_oracle_mode()) {
int64_t now = ObTimeUtility::current_time();
obrpc::ObAlterTableRes res;
if (now - get_last_refresh_temp_table_time() >= REFRESH_INTERVAL) {
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_;
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(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());
}
}
}
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, lib::is_oracle_mode()), error_code);
} else if (OB_SUCCESS == error_code) {
warnings_buf_.reset_err();
}
show_warnings_buf_ = warnings_buf_;
}
void ObSQLSessionInfo::get_session_priv_info(share::schema::ObSessionPrivInfo& session_priv) const
{
session_priv.tenant_id_ = get_priv_tenant_id();
session_priv.user_id_ = get_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_;
session_priv.enable_role_id_array_.assign(enable_role_array_);
}
ObPlanCache* ObSQLSessionInfo::get_plan_cache()
{
if (OB_LIKELY(NULL != plan_cache_manager_)) {
if (OB_NOT_NULL(plan_cache_) && plan_cache_->is_valid()) {
// do nothing
} else {
// release old plancache and get new
if (NULL != plan_cache_) {
plan_cache_->dec_ref_count();
}
ObPCMemPctConf pc_mem_conf;
if (OB_SUCCESS != get_pc_mem_conf(pc_mem_conf)) {
LOG_ERROR("fail to get pc mem conf");
plan_cache_ = NULL;
} else {
uint64_t tenant_id = lib::current_resource_owner_id();
if (tenant_id > OB_SYS_TENANT_ID && tenant_id <= OB_MAX_RESERVED_TENANT_ID) {
// all virtual tenants use sys tenant's plan cache
tenant_id = OB_SYS_TENANT_ID;
}
plan_cache_ = plan_cache_manager_->get_or_create_plan_cache(tenant_id, pc_mem_conf);
if (NULL == plan_cache_) {
LOG_WARN("failed to get plan cache");
}
}
}
} else {
LOG_WARN("Invalid status", K(plan_cache_), K(plan_cache_manager_));
}
return plan_cache_;
}
ObPsCache* ObSQLSessionInfo::get_ps_cache()
{
if (OB_ISNULL(plan_cache_manager_)) {
LOG_WARN("invalid status", K_(ps_cache), K_(plan_cache_manager));
} else if (OB_NOT_NULL(ps_cache_) && ps_cache_->is_valid()) {
// do nothing
} else {
int ret = OB_SUCCESS;
if (NULL != ps_cache_) {
ps_cache_->dec_ref_count();
}
ObPCMemPctConf pc_mem_conf;
if (OB_FAIL(get_pc_mem_conf(pc_mem_conf))) {
LOG_ERROR("failed to get pc mem conf");
ps_cache_ = NULL;
} else {
ps_cache_ = plan_cache_manager_->get_or_create_ps_cache(lib::current_resource_owner_id(), pc_mem_conf);
if (OB_ISNULL(ps_cache_)) {
LOG_WARN("failed to get ps plan cache");
}
}
}
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() && read_only) {
/** session1 session2
* insert into xxx;
* set @@global.read_only = 1;
* update xxx (should fail)
* create (should fail)
* ... (all write stmt should fail)
*/
if (!sql_traits.is_readonly_stmt_) {
if (sql_traits.is_modify_tenant_stmt_) {
ret = OB_ERR_NO_PRIVILEGE;
LOG_USER_ERROR(OB_ERR_NO_PRIVILEGE, "SUPER");
LOG_WARN("Access denied; you need (at least one of)"
"the SUPER privilege(s) for this operation");
} else {
ret = OB_ERR_OPTION_PREVENTS_STATEMENT;
LOG_WARN("the server is running with read_only, cannot execute stmt");
}
} else {
/** session1 session2 session3
* begin begin;
* insert into xxx; (without write stmt)
* set @@global.read_only = 1;
* commit; (should fail) commit; (should success)
*/
if (sql_traits.is_commit_stmt_ && trans_flags_.has_exec_write_stmt()) {
ret = OB_ERR_OPTION_PREVENTS_STATEMENT;
LOG_WARN("the server is running with read_only, cannot execute stmt");
}
}
}
return ret;
}
int ObSQLSessionInfo::remove_prepare(const ObString& ps_name)
{
int ret = OB_SUCCESS;
ObPsStmtId ps_id = OB_INVALID_ID;
if (OB_FAIL(ps_name_id_map_.erase_refactored(ps_name, &ps_id))) {
LOG_WARN("ps session info not exist", K(ps_name));
} else if (OB_INVALID_ID == ps_id) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session_info is null", K(ret));
} else { /*do nothing*/
}
return ret;
}
int ObSQLSessionInfo::get_prepare_id(const ObString& ps_name, ObPsStmtId& ps_id) const
{
int ret = OB_SUCCESS;
ps_id = OB_INVALID_ID;
if (OB_FAIL(ps_name_id_map_.get_refactored(ps_name, ps_id))) {
LOG_WARN("get ps session info failed", K(ps_name));
if (ret == OB_HASH_NOT_EXIST) {
ret = OB_EER_UNKNOWN_STMT_HANDLER;
}
} else if (OB_INVALID_ID == ps_id) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ps info is null", K(ret), K(ps_name));
} else { /*do nothing*/
}
return ret;
}
int ObSQLSessionInfo::add_prepare(const ObString& ps_name, ObPsStmtId ps_id)
{
int ret = OB_SUCCESS;
ObString stored_name;
ObPsStmtId exist_ps_id = OB_INVALID_ID;
if (OB_FAIL(name_pool_.write_string(ps_name, &stored_name))) {
LOG_WARN("failed to copy name", K(ps_name), K(ps_id), K(ret));
} else if (OB_FAIL(ps_name_id_map_.get_refactored(stored_name, exist_ps_id))) {
if (OB_HASH_NOT_EXIST == ret) {
if (OB_FAIL(ps_name_id_map_.set_refactored(stored_name, ps_id))) {
LOG_WARN("fail insert ps id to hash map", K(stored_name), K(ps_id), K(ret));
}
} else {
LOG_WARN("fail to search ps name hash id map", K(stored_name), K(ret));
}
} else if (ps_id != exist_ps_id) {
LOG_DEBUG("exist ps id is diff", K(ps_id), K(exist_ps_id), K(ps_name), K(stored_name));
if (OB_FAIL(remove_prepare(stored_name))) {
LOG_WARN("failed to remove prepare", K(stored_name), K(ret));
} else if (OB_FAIL(remove_ps_session_info(exist_ps_id))) {
LOG_WARN("failed to remove prepare sesion info", K(exist_ps_id), K(stored_name), K(ret));
} else if (OB_FAIL(ps_name_id_map_.set_refactored(stored_name, ps_id))) {
LOG_WARN("fail insert ps id to hash map", K(stored_name), K(ps_id), K(ret));
}
}
return ret;
}
int ObSQLSessionInfo::get_ps_session_info(const ObPsStmtId stmt_id, ObPsSessionInfo*& ps_session_info) const
{
int ret = OB_SUCCESS;
ps_session_info = NULL;
if (OB_FAIL(ps_session_info_map_.get_refactored(stmt_id, ps_session_info))) {
LOG_WARN("get ps session info failed", K(stmt_id), K(get_sessid()));
if (ret == OB_HASH_NOT_EXIST) {
ret = OB_EER_UNKNOWN_STMT_HANDLER;
}
} else if (OB_ISNULL(ps_session_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ps session info is null", K(ret), K(stmt_id));
}
return ret;
}
int ObSQLSessionInfo::remove_ps_session_info(const ObPsStmtId stmt_id)
{
int ret = OB_SUCCESS;
ObPsSessionInfo* session_info = NULL;
if (OB_FAIL(ps_session_info_map_.erase_refactored(stmt_id, &session_info))) {
LOG_WARN("ps session info not exist", K(stmt_id));
} else if (OB_ISNULL(session_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session_info is null", K(ret));
} else {
LOG_TRACE("remove ps session info", K(ret), K(stmt_id), K(get_sessid()));
session_info->~ObPsSessionInfo();
ps_session_info_allocator_.free(session_info);
session_info = NULL;
}
return ret;
}
int ObSQLSessionInfo::prepare_ps_stmt(const ObPsStmtId inner_stmt_id, const ObPsStmtInfo* stmt_info,
ObPsStmtId& client_stmt_id, bool& already_exists, bool is_inner_sql)
{
int ret = OB_SUCCESS;
ObPsSessionInfo* session_info = NULL;
const bool is_new_proxy = ((is_obproxy_mode() && proxy_version_ >= min_proxy_version_ps_) || !is_obproxy_mode());
if (is_new_proxy && !is_inner_sql) {
client_stmt_id = ++next_client_ps_stmt_id_;
} else {
client_stmt_id = inner_stmt_id;
}
already_exists = false;
if (is_inner_sql) {
LOG_TRACE("is inner sql no need to add session info",
K(proxy_version_),
K(min_proxy_version_ps_),
K(inner_stmt_id),
K(client_stmt_id),
K(next_client_ps_stmt_id_),
K(is_new_proxy),
K(is_inner_sql));
} else {
ret = ps_session_info_map_.get_refactored(client_stmt_id, session_info);
LOG_TRACE("will add session info",
K(proxy_version_),
K(min_proxy_version_ps_),
K(inner_stmt_id),
K(client_stmt_id),
K(next_client_ps_stmt_id_),
K(is_new_proxy),
K(ret),
K(is_inner_sql));
if (OB_SUCC(ret)) {
if (OB_ISNULL(session_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session_info is NULL", K(ret), K(inner_stmt_id), K(client_stmt_id));
} else {
already_exists = true;
session_info->inc_ref_count();
}
} else if (OB_HASH_NOT_EXIST == ret) {
ret = OB_SUCCESS;
char* buf = static_cast<char*>(ps_session_info_allocator_.alloc(sizeof(ObPsSessionInfo)));
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else if (OB_ISNULL(stmt_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt info is null", K(ret), K(stmt_info));
} else {
session_info = new (buf) ObPsSessionInfo(stmt_info->get_num_of_param());
session_info->set_stmt_id(client_stmt_id);
session_info->set_stmt_type(stmt_info->get_stmt_type());
session_info->set_ps_stmt_checksum(stmt_info->get_ps_stmt_checksum());
session_info->set_inner_stmt_id(inner_stmt_id);
LOG_TRACE("add ps session info",
K(stmt_info->get_ps_sql()),
K(stmt_info->get_ps_stmt_checksum()),
K(client_stmt_id),
K(inner_stmt_id),
K(get_sessid()));
}
if (OB_SUCC(ret) && stmt::T_CALL_PROCEDURE == stmt_info->get_stmt_type() && stmt_info->has_complex_argument()) {
ret = OB_NOT_SUPPORTED;
}
if (OB_SUCC(ret)) {
session_info->inc_ref_count();
if (OB_FAIL(ps_session_info_map_.set_refactored(client_stmt_id, session_info))) {
// OB_HASH_EXIST cannot be here, no need to handle
LOG_WARN("push back ps_session info failed", K(ret), K(client_stmt_id));
} else {
LOG_TRACE("add ps session info success", K(client_stmt_id), K(get_sessid()));
}
}
if (OB_FAIL(ret) && OB_NOT_NULL(session_info)) {
session_info->~ObPsSessionInfo();
ps_session_info_allocator_.free(session_info);
session_info = NULL;
buf = NULL;
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get ps session failed", K(ret), K(client_stmt_id), K(inner_stmt_id));
}
}
return ret;
}
int ObSQLSessionInfo::get_inner_ps_stmt_id(ObPsStmtId cli_stmt_id, ObPsStmtId& inner_stmt_id)
{
int ret = OB_SUCCESS;
ObPsSessionInfo* ps_session_info = NULL;
if (OB_FAIL(ps_session_info_map_.get_refactored(cli_stmt_id, ps_session_info))) {
LOG_WARN("get inner ps stmt id failed", K(ret), K(cli_stmt_id), K(lbt()));
} else if (OB_ISNULL(ps_session_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ps session info is null", K(cli_stmt_id), "session_id", get_sessid(), K(ret));
} else {
inner_stmt_id = ps_session_info->get_inner_stmt_id();
}
return ret;
}
int ObSQLSessionInfo::check_read_only_privilege(const bool read_only, const ObSqlTraits& sql_traits)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_global_read_only_privilege(read_only, sql_traits))) {
LOG_WARN("failed to check global read_only privilege!", K(ret));
} else if (OB_FAIL(check_tx_read_only_privilege(sql_traits))) {
LOG_WARN("failed to check tx_read_only privilege!", K(ret));
}
return ret;
}
ObTraceEventRecorder* ObSQLSessionInfo::get_trace_buf()
{
if (NULL == trace_recorder_) {
void* ptr = name_pool_.alloc(sizeof(ObTraceEventRecorder));
if (NULL == ptr) {
LOG_WARN("fail to alloc trace recorder");
} else {
trace_recorder_ = new (ptr) ObTraceEventRecorder(false, ObLatchIds::SESSION_TRACE_RECORDER_LOCK);
}
}
return trace_recorder_;
}
void ObSQLSessionInfo::clear_trace_buf()
{
if (NULL != trace_recorder_) {
trace_recorder_->reset();
}
}
OB_SERIALIZE_MEMBER((ObSQLSessionInfo, ObBasicSessionInfo), thread_data_.cur_query_start_time_, user_priv_set_,
db_priv_set_, trans_type_, global_sessid_, inner_flag_, is_max_availability_mode_, session_type_,
has_temp_table_flag_, enable_early_lock_release_, use_static_typing_engine_, enable_role_array_,
in_definer_named_proc_, priv_user_id_, xa_end_timeout_seconds_, prelock_, proxy_version_, min_proxy_version_ps_);
int ObSQLSessionInfo::get_collation_type_of_names(const ObNameTypeClass type_class, ObCollationType& cs_type) const
{
int ret = OB_SUCCESS;
ObNameCaseMode case_mode = OB_NAME_CASE_INVALID;
cs_type = CS_TYPE_INVALID;
if (OB_TABLE_NAME_CLASS == type_class) {
if (OB_FAIL(get_name_case_mode(case_mode))) {
LOG_WARN("fail to get name case mode", K(ret));
} else if (OB_ORIGIN_AND_SENSITIVE == case_mode) {
cs_type = CS_TYPE_UTF8MB4_BIN;
} else if (OB_ORIGIN_AND_INSENSITIVE == case_mode || OB_LOWERCASE_AND_INSENSITIVE == case_mode) {
cs_type = CS_TYPE_UTF8MB4_GENERAL_CI;
}
} else if (OB_COLUMN_NAME_CLASS == type_class) {
cs_type = CS_TYPE_UTF8MB4_GENERAL_CI;
} else if (OB_USER_NAME_CLASS == type_class) {
cs_type = CS_TYPE_UTF8MB4_BIN;
}
return ret;
}
int ObSQLSessionInfo::name_case_cmp(
const ObString& name, const ObString& name_other, const ObNameTypeClass type_class, bool& is_equal) const
{
int ret = OB_SUCCESS;
ObCollationType cs_type = CS_TYPE_INVALID;
if (OB_FAIL(get_collation_type_of_names(type_class, cs_type))) {
LOG_WARN("fail to get collation type of name", K(name), K(name_other), K(type_class), K(ret));
} else {
if (0 == ObCharset::strcmp(cs_type, name, name_other)) {
is_equal = true;
} else {
is_equal = false;
}
}
return ret;
}
int ObSQLSessionInfo::kill_query()
{
ObSQLSessionInfo::LockGuard lock_guard(get_thread_data_lock());