forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_sql_session_mgr.cpp
More file actions
1060 lines (994 loc) · 40.6 KB
/
Copy pathob_sql_session_mgr.cpp
File metadata and controls
1060 lines (994 loc) · 40.6 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
#include "ob_sql_session_mgr.h"
#include "sql/monitor/flt/ob_flt_control_info_mgr.h"
#include "storage/concurrency_control/ob_multi_version_garbage_collector.h"
#include "lib/ash/ob_active_session_guard.h"
#include "sql/engine/dml/ob_trigger_handler.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
using namespace oceanbase::share;
using namespace oceanbase::observer;
ObTenantSQLSessionMgr::SessionPool::SessionPool()
: session_pool_()
{
MEMSET(session_array_, 0, POOL_CAPACIPY * sizeof(ObSQLSessionInfo *));
}
int ObTenantSQLSessionMgr::SessionPool::init(const int64_t capacity)
{
int ret = OB_SUCCESS;
int64_t real_cap = capacity;
if (real_cap > POOL_CAPACIPY) {
real_cap = POOL_CAPACIPY;
}
char *session_buf = reinterpret_cast<char *>(session_array_);
OZ (session_pool_.init(real_cap, session_buf));
return ret;
}
int ObTenantSQLSessionMgr::SessionPool::pop_session(ObSQLSessionInfo *&session)
{
int ret = OB_SUCCESS;
session = NULL;
if (OB_FAIL(session_pool_.pop(session))) {
if (ret != OB_ENTRY_NOT_EXIST) {
LOG_WARN("failed to pop session", K(ret),
K(session_pool_.get_total()), K(session_pool_.get_free()));
} else {
ret = OB_SUCCESS;
LOG_DEBUG("session pool is empty",
K(session_pool_.get_total()), K(session_pool_.get_free()));
}
}
return ret;
}
int ObTenantSQLSessionMgr::SessionPool::push_session(ObSQLSessionInfo *&session)
{
int ret = OB_SUCCESS;
if (OB_NOT_NULL(session)) {
if (OB_FAIL(session_pool_.push(session))) {
if (ret != OB_SIZE_OVERFLOW) {
LOG_WARN("failed to push session", K(ret),
K(session_pool_.get_total()), K(session_pool_.get_free()));
} else {
ret = OB_SUCCESS;
LOG_DEBUG("session pool is full",
K(session_pool_.get_total()), K(session_pool_.get_free()));
}
} else {
session = NULL;
}
}
return ret;
}
int64_t ObTenantSQLSessionMgr::SessionPool::count() const
{
return session_pool_.get_total();
}
ObTenantSQLSessionMgr::ObTenantSQLSessionMgr(const int64_t tenant_id)
: tenant_id_(tenant_id), count_(0),
session_allocator_(lib::ObMemAttr(tenant_id, "SQLSessionInfo"), MTL_CPU_COUNT(), 4)
{}
ObTenantSQLSessionMgr::~ObTenantSQLSessionMgr()
{}
int ObTenantSQLSessionMgr::init()
{
int ret = OB_SUCCESS;
if (OB_FAIL(session_pool_.init(SessionPool::POOL_CAPACIPY))) {
LOG_WARN("fail to init session pool", K(tenant_id_), K(ret));
}
return ret;
}
void ObTenantSQLSessionMgr::destroy()
{
}
int ObTenantSQLSessionMgr::mtl_new(ObTenantSQLSessionMgr *&t_session_mgr)
{
int ret = OB_SUCCESS;
t_session_mgr = OB_NEW(ObTenantSQLSessionMgr, ObMemAttr(MTL_ID(), "TSQLSessionMgr"),
MTL_ID());
if (OB_ISNULL(t_session_mgr)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("failed to alloc tenant session manager", K(ret));
}
return ret;
}
int ObTenantSQLSessionMgr::mtl_init(ObTenantSQLSessionMgr *&t_session_mgr)
{
int ret = OB_SUCCESS;
if (t_session_mgr != NULL) {
if (OB_FAIL(t_session_mgr->init())) {
LOG_WARN("failed to init tenant session manager", K(ret));
}
}
return ret;
}
void ObTenantSQLSessionMgr::mtl_wait(ObTenantSQLSessionMgr *&t_session_mgr)
{
if (t_session_mgr != NULL) {
while (t_session_mgr->count() != 0) {
LOG_WARN_RET(OB_NEED_RETRY, "tenant session mgr should be empty",
K(t_session_mgr->count()));
usleep(1000 * 1000);
}
}
LOG_INFO("success to wait tenant session mgr");
}
void ObTenantSQLSessionMgr::mtl_destroy(ObTenantSQLSessionMgr *&t_session_mgr)
{
if (nullptr != t_session_mgr) {
t_session_mgr->destroy();
OB_DELETE(ObTenantSQLSessionMgr, "unused", t_session_mgr);
t_session_mgr = nullptr;
}
}
ObSQLSessionInfo *ObTenantSQLSessionMgr::alloc_session()
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = NULL;
OX (session_pool_.pop_session(session));
if (OB_ISNULL(session)) {
OX (session = op_instance_alloc_args(&session_allocator_,
ObSQLSessionInfo,
tenant_id_));
if (session != NULL) {
OX (ATOMIC_FAA(&count_, 1));
}
}
OV (OB_NOT_NULL(session));
OX (session->set_tenant_session_mgr(this));
OX (session->set_valid(true));
OX (session->set_shadow(true));
return session;
}
void ObTenantSQLSessionMgr::free_session(ObSQLSessionInfo *session)
{
int ret = OB_SUCCESS;
SessionPool *session_pool = NULL;
// add tracepoint for control session pool.
int64_t code = 0;
code = OB_E(EventTable::EN_SESS_POOL_MGR_CTRL) OB_SUCCESS;
if (ObTenantSQLSessionMgr::is_valid_tenant_id(session->get_login_tenant_id()) &&
session->can_release_to_pool() && code == OB_SUCCESS) {
if (session->is_use_inner_allocator() && !session->is_tenant_killed()) {
session_pool = &session_pool_;
}
}
if (OB_NOT_NULL(session_pool)) {
OX (session->destroy(true));
OX (session->set_acquire_from_pool(true));
OX (session_pool->push_session(session));
}
if (OB_NOT_NULL(session)) {
OX (op_free(session));
OX (ATOMIC_FAA(&count_, -1));
OX (session = NULL);
}
}
void ObTenantSQLSessionMgr::clean_session_pool()
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = NULL;
// Note, here there is no design guarantee that the session pool will be completely emptied, there is a very low probability of a small number of leftover sessions:
// 1. In the production system, deleting a tenant is a very rare operation.
// 2. In the production system, before deleting a tenant, it will ensure that the business layer no longer accesses the tenant.
// Under the above conditions, theoretically this session pool should have no operations.
// 3. Normally stop a certain observer, but even if there are a few sessions missed, they will be released with the process termination.
// 4. unit migration situations, there may be a few sessions not released, but they can be reused when the unit migrates back.
while (session_pool_.count() > 0) {
OX (session_pool_.pop_session(session));
if (OB_NOT_NULL(session)) {
OX (op_free(session));
OX (ATOMIC_FAA(&count_, -1));
OX (session = NULL);
}
}
}
bool ObTenantSQLSessionMgr::is_valid_tenant_id(uint64_t tenant_id) const
{
return ::is_valid_tenant_id(tenant_id) &&
tenant_id != OB_INVALID_ID &&
tenant_id != OB_SYS_TENANT_ID;
}
int ObSQLSessionMgr::ValueAlloc::clean_tenant(uint64_t tenant_id)
{
int ret = OB_SUCCESS;
MTL_SWITCH(tenant_id) {
ObTenantSQLSessionMgr *t_session_mgr = MTL(ObTenantSQLSessionMgr*);
t_session_mgr->clean_session_pool();
} else {
LOG_ERROR("switch tenant failed", K(ret), K(tenant_id));
}
return ret;
}
ObSQLSessionInfo *ObSQLSessionMgr::ValueAlloc::alloc_value(uint64_t tenant_id)
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = NULL;
int64_t alloc_total_count = 0;
// we use OX instead of OZ in operation of upper session pool, because we need acquire
// from lower session pool when not success, no matter which errno we get here.
MTL_SWITCH(tenant_id) {
ObTenantSQLSessionMgr *t_session_mgr = MTL(ObTenantSQLSessionMgr*);
if (OB_ISNULL(session = t_session_mgr->alloc_session())) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to alloc session", K(ret));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(GCTX.session_mgr_->get_sess_hold_map()
.set_refactored(reinterpret_cast<uint64_t>(session), session))) {
LOG_WARN("fail to set session", K(ret), KP(session));
}
}
OX (alloc_total_count = ATOMIC_FAA(&alloc_total_count_, 1));
if (alloc_total_count > 0 && alloc_total_count % 10000 == 0) {
LOG_INFO("alloc_session_count", K(alloc_total_count));
}
} else {
LOG_WARN("switch tenant failed", K(ret), K(tenant_id));
}
return session;
}
void ObSQLSessionMgr::ValueAlloc::free_value(ObSQLSessionInfo *session)
{
if (OB_NOT_NULL(session)) {
int ret = OB_SUCCESS;
uint64_t tenant_id = session->get_login_tenant_id();
int64_t free_total_count = 0;
// delete from hold map, ingore error
int tmp_ret = OB_SUCCESS;
uint32_t server_sessid = INVALID_SESSID;
if (OB_SUCCESS != (tmp_ret = GCTX.session_mgr_->get_sess_hold_map().erase_refactored(
reinterpret_cast<uint64_t>(session)))) {
LOG_WARN("fail to erase session", K(session->get_server_sid()), K(tmp_ret), KP(session));
} else if (session->get_client_sid() != INVALID_SESSID) {
if (OB_SUCCESS != (tmp_ret = GCTX.session_mgr_->get_client_sess_map().get_refactored(
session->get_client_sid(), server_sessid))) {
if (tmp_ret == OB_HASH_NOT_EXIST) {
// no need to display info, if current server no this client session id.
tmp_ret = OB_SUCCESS;
LOG_DEBUG("current client session id not find", K(tmp_ret),
K(session->get_client_sid()));
} else {
COMMON_LOG(WARN, "get session failed", K(tmp_ret), K(session->get_client_sid()));
}
} else if (session->get_server_sid() == server_sessid) {
ObClientSessMapErase client_sess_map_erase(session->get_server_sid());
bool is_erased = false;
if (OB_SUCCESS != (tmp_ret = GCTX.session_mgr_->get_client_sess_map().erase_if(
session->get_client_sid(),client_sess_map_erase, is_erased))) {
LOG_WARN("fail to erase client session", K(session->get_client_sid()),
K(session->get_server_sid()), K(tmp_ret));
} else {
LOG_DEBUG("success to erase cs id", K(session->get_client_sid()),
K(session->get_server_sid()), K(lbt()));
}
} else {
LOG_DEBUG("no need to erase client session", K(session->get_client_sid()),
K(session->get_server_sid()), K(server_sessid), K(tmp_ret),K(lbt()));
}
}
ObTenantSQLSessionMgr *t_session_mgr = session->get_tenant_session_mgr();
if (t_session_mgr != NULL) {
t_session_mgr->free_session(session);
}
OX (free_total_count = ATOMIC_FAA(&free_total_count_, 1));
if (free_total_count > 0 && free_total_count % 10000 == 0) {
LOG_INFO("free_session_count", K(free_total_count));
}
}
}
int ObSQLSessionMgr::init()
{
int ret = OB_SUCCESS;
if (OB_FAIL(sessinfo_map_.init())) {
LOG_WARN("fail to init session map", K(ret));
} else if (OB_FAIL(sessid_sequence_.init(MAX_LOCAL_SEQ))) {
LOG_WARN("init sessid sequence failed", K(ret));
} else if (OB_FAIL(sess_hold_map_.create(BUCKET_COUNT,
SET_USE_500("SessHoldMapBuck"),
SET_USE_500("SessHoldMapNode")))) {
LOG_WARN("failed to init sess_hold_map", K(ret));
} else if (OB_FAIL(client_sess_map_.create(BUCKET_COUNT,
SET_USE_500("ClientSessBuck"),
SET_USE_500("ClientSessNode")))) {
LOG_WARN("failed to init client_sess_map", K(ret));
} else if (OB_FAIL(kill_client_sess_map_.create(BUCKET_COUNT,
SET_USE_500("KillSessMapBuck"),
SET_USE_500("KillSessMapNode")))) {
LOG_WARN("failed to init client_sess_map", K(ret));
}
for (uint32_t i = 1; OB_SUCC(ret) && i <= MAX_LOCAL_SEQ; ++i) {
if (OB_FAIL(sessid_sequence_.push(reinterpret_cast<void*>(i)))) {
LOG_WARN("store sessid sequence failed", K(ret), K(i));
}
}
return ret;
}
void ObSQLSessionMgr::destroy()
{
sessinfo_map_.destroy();
sessid_sequence_.destroy();
client_sess_map_.destroy();
kill_client_sess_map_.destroy();
sess_hold_map_.destroy();
}
uint64_t ObSQLSessionMgr::extract_server_index(uint32_t sessid)
{
uint64_t server_index = sessid >> LOCAL_SEQ_LEN;
return server_index & MAX_SERVER_INDEX;
}
int ObSQLSessionMgr::inc_session_ref(const ObSQLSessionInfo *my_session)
{
int ret = OB_SUCCESS;
if (OB_NOT_NULL(my_session)) {
ObSQLSessionInfo *tmp_session = NULL;
uint32_t sessid = my_session->get_server_sid();
if (OB_FAIL(get_session(sessid, tmp_session))) {
LOG_WARN("fail to get session", K(sessid), K(ret));
}
UNUSED(tmp_session);
}
return ret;
}
// |<---------------------------------32bit---------------------------->|
// 0b 1b 15b 16b
// +----+------------------------------+--------------------------------+
// |Mask| Server Id | Local Seq |
// +----+------------------------------+--------------------------------+
//
//MASK: 1 indicates that the connection id is generated by the server itself,
// 0 indicates a connection id generated by proxy (deprecated, currently used only in the scenario where in_mgr = false);
//Server index: The server ID in the cluster is assigned by RS, unique within the cluster, but may be reused after a server is deleted;
//Local Seq: a server's available connection count, currently a single server can have up to INT16_MAX connections;
//
int ObSQLSessionMgr::create_sessid(uint32_t &sessid, bool in_mgr)
{
int ret = OB_SUCCESS;
int tmp_ret = OB_SUCCESS;
sessid = 0;
const uint64_t server_index = GCTX.get_server_index();
uint32_t local_seq = 0;
static uint32_t abnormal_seq = 0;//used for sessid allocation when server_id == 0
if (server_index > MAX_SERVER_INDEX) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("server index maybe invalid", K(ret), K(server_index));
} else if (!in_mgr) {
sessid = (GETTID() | LOCAL_SESSID_TAG);
sessid |= static_cast<uint32_t>(server_index << LOCAL_SEQ_LEN); // set observer
} else if (0 == server_index) {
local_seq = (ATOMIC_FAA(&abnormal_seq, 1) & MAX_LOCAL_SEQ);
uint32_t max_local_seq = MAX_LOCAL_SEQ;
uint32_t max_server_index = MAX_SERVER_INDEX;
LOG_WARN("server is initiating", K(server_index), K(local_seq), K(max_local_seq), K(max_server_index));
} else if (OB_UNLIKELY(OB_SUCCESS != (ret = tmp_ret = get_avaiable_local_seq(local_seq)))) {
LOG_WARN("fail to get avaiable local_seq", K(local_seq));
} else {/*do nothing*/}
if (OB_SUCC(ret) && in_mgr) {
sessid = local_seq | SERVER_SESSID_TAG;// set observer sessid mark
sessid |= static_cast<uint32_t>(server_index << LOCAL_SEQ_LEN); // set observer
// high bit is reserved for server index
sessid |= static_cast<uint32_t>(1ULL << (LOCAL_SEQ_LEN + SERVER_INDEX_LEN));
}
return ret;
}
// ret == OB_SUCCESS when, ensure sess_info != NULL, need to perform revert_session outside
// ret != OB_SUCCESS when, ensure sess_info == NULL, no need to revert_session outside
int ObSQLSessionMgr::create_session(ObSMConnection *conn, ObSQLSessionInfo *&sess_info)
{
int ret = OB_SUCCESS;
sess_info = NULL;
// In order to be compatible with lower versions,
// the client session id of unsupported versions is INVALID_SESSID.
// proxy mode, client sess id will be passed by proxy.
// direct mode, cs_id same as session id.
conn->client_sessid_ = conn->proxy_cap_flags_.is_client_sessid_support() || (conn->proxy_sessid_ == 0)
? (conn->client_sessid_ == INVALID_SESSID ? conn->sessid_ : conn->client_sessid_) : INVALID_SESSID;
if (OB_ISNULL(conn)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("conn is NULL", K(ret));
} else if (OB_FAIL(create_session(conn->tenant_id_,
conn->sessid_,
conn->proxy_sessid_,
conn->sess_create_time_,
sess_info,
conn->client_sessid_,
conn->client_create_time_))) {
LOG_WARN("create session failed", K(ret));
} else if (OB_ISNULL(sess_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sess_info is null", K(ret));
} else {
GET_DIAGNOSTIC_INFO->get_ash_stat().client_sid_ = conn->client_sessid_;
sess_info->set_vid(conn->vid_);
sess_info->set_vip(conn->vip_buf_);
sess_info->set_vport(conn->vport_);
sess_info->inc_in_bytes(conn->connect_in_bytes_);
}
return ret;
}
int ObSQLSessionMgr::create_session(const uint64_t tenant_id,
const uint32_t sessid,
const uint64_t proxy_sessid,
const int64_t create_time,
ObSQLSessionInfo *&session_info,
const uint32_t client_sessid,
const int64_t client_create_time)
{
ACTIVE_SESSION_FLAG_SETTER_GUARD(in_connection_mgr);
int ret = OB_SUCCESS;
int err = OB_SUCCESS;
session_info = NULL;
ObSQLSessionInfo *tmp_sess = NULL;
if (OB_FAIL(sessinfo_map_.create(tenant_id, Key(sessid, proxy_sessid), tmp_sess))) {
LOG_WARN("fail to create session", K(ret), K(sessid));
if (OB_ENTRY_EXIST == ret) {
ret = OB_SESSION_ENTRY_EXIST;
}
} else if (OB_ISNULL(tmp_sess)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("fail to alloc session info", K(ret), K(sessid), K(proxy_sessid));
} else if (client_sessid != INVALID_SESSID && OB_FAIL(GCTX.session_mgr_->get_client_sess_map()
.set_refactored(client_sessid, sessid))) {
if (OB_HASH_EXIST == ret) {
ret = OB_SUCCESS;
int flag = 1;
ObSQLSessionInfo *last_server_session = NULL;
uint32_t last_sessid = INVALID_SESSID;
if (OB_FAIL(GCTX.session_mgr_->get_client_sess_map()
.get_refactored(client_sessid, last_sessid))) {
ret = OB_SUCCESS;
} else if (OB_FAIL(get_session(last_sessid, last_server_session))) {
ret = OB_SUCCESS;
} else if (OB_ISNULL(last_server_session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("fail to alloc session info", K(last_sessid),
K(client_sessid), K(ret));
} else if (last_server_session->get_session_state() != SESSION_KILLED && proxy_sessid != last_server_session->get_proxy_sessid()) {
LOG_ERROR("conclude same client session", K(client_sessid), K(proxy_sessid),
K(last_server_session->get_proxy_sessid()));
}
if (OB_FAIL(GCTX.session_mgr_->get_client_sess_map()
.set_refactored(client_sessid, sessid, flag))) {
ret = OB_SUCCESS;
LOG_WARN("fail to set client session, no gurantee client session info", K(client_sessid));
} else {
}
if (NULL != last_server_session) {
revert_session(last_server_session);
}
} else {
// revert session behind.
}
} else {
// create session contains a 'get_session' action implicitly
const bool v = GCONF._enable_trace_session_leak;
if (OB_UNLIKELY(v)) {
tmp_sess->on_get_session();
}
}
if (OB_FAIL(ret)) {
if (NULL != tmp_sess) {
if (FALSE_IT(revert_session(tmp_sess))) {
} else if (OB_SUCCESS != (err = sessinfo_map_.del(Key(sessid)))) {
LOG_ERROR("fail to free session", K(err), K(sessid), K(client_sessid));
} else {
LOG_DEBUG("free session successfully in create session", K(err),
K(sessid), K(client_sessid));
}
}
} else if (OB_FAIL(tmp_sess->init(sessid, proxy_sessid, NULL, NULL, create_time,
tenant_id, client_create_time))) {
LOG_WARN("fail to init session", K(ret), K(tmp_sess),
K(sessid), K(proxy_sessid), K(create_time));
if (FALSE_IT(revert_session(tmp_sess))) {
LOG_ERROR("fail to free session", K(err), K(sessid), K(proxy_sessid));
} else if (OB_SUCCESS != (err = sessinfo_map_.del(Key(sessid)))) {
LOG_ERROR("fail to free session", K(err), K(sessid), K(proxy_sessid));
} else {
LOG_DEBUG("free session successfully in create session", K(err),
K(sessid), K(proxy_sessid), K(client_create_time));
}
} else {
// set tenant info to session, if has.
ObFLTControlInfoManager mgr(tenant_id);
if (OB_FAIL(mgr.init())) {
LOG_WARN("failed to init full link control info", K(ret));
if (FALSE_IT(revert_session(tmp_sess))) {
LOG_ERROR("fail to free session", K(err), K(sessid), K(proxy_sessid));
} else if (OB_SUCCESS != (err = sessinfo_map_.del(Key(sessid)))) {
LOG_ERROR("fail to free session", K(err), K(sessid), K(proxy_sessid));
} else {
LOG_DEBUG("free session successfully in create session", K(err),
K(sessid), K(proxy_sessid));
}
} else if (mgr.is_valid_tenant_config()) {
tmp_sess->set_flt_control_info(mgr.get_control_info());
}
if (OB_FAIL(ret)) {
// do nothing
} else {
tmp_sess->update_last_active_time();
session_info = tmp_sess;
}
}
return ret;
}
int ObSQLSessionMgr::free_session(const ObFreeSessionCtx &ctx)
{
ACTIVE_SESSION_FLAG_SETTER_GUARD(in_connection_mgr);
int ret = OB_SUCCESS;
uint32_t sessid = ctx.sessid_;
uint64_t proxy_sessid = ctx.proxy_sessid_;
uint64_t tenant_id = ctx.tenant_id_;
bool has_inc = ctx.has_inc_active_num_;
ObSQLSessionInfo *sess_info = NULL;
sessinfo_map_.get(Key(sessid), sess_info);
if (NULL != sess_info) {
if (OB_UNLIKELY(OB_SUCCESS != sess_info->on_user_disconnect())) {
LOG_WARN("user disconnect failed", K(ret), K(sess_info->get_user_id()));
}
sessinfo_map_.revert(sess_info);
}
if (OB_FAIL(sessinfo_map_.del(Key(sessid)))) {
LOG_WARN("fail to remove session from session map", K(ret), K(sessid), K(proxy_sessid));
} else if (tenant_id != 0 && sessid != 0 && has_inc) {
EVENT_DEC(ACTIVE_SESSIONS);
}
return ret;
}
void ObSQLSessionMgr::try_check_session()
{
int ret = OB_SUCCESS;
CheckSessionFunctor check_timeout(this);
if (OB_FAIL(for_each_session(check_timeout))) {
LOG_WARN("fail to check time out", K(ret));
} else {
if (REACH_TIME_INTERVAL(60000000)) { // 60s
OZ (check_session_leak());
}
}
}
int ObSQLSessionMgr::get_min_active_snapshot_version(share::SCN &snapshot_version)
{
int ret = OB_SUCCESS;
concurrency_control::GetMinActiveSnapshotVersionFunctor min_active_txn_version_getter;
if (OB_FAIL(for_each_session(min_active_txn_version_getter))) {
LOG_WARN("fail to get min active snapshot version", K(ret));
} else {
snapshot_version = min_active_txn_version_getter.get_min_active_snapshot_version();
}
return ret;
}
int ObSQLSessionMgr::check_session_leak()
{
int ret = OB_SUCCESS;
int64_t hold_session_count = sess_hold_map_.size();
int64_t used_session_count = 0;
if (OB_FAIL(get_session_count(used_session_count))) {
LOG_WARN("fail to get session count", K(ret));
} else {
static const int32_t DEFAULT_SESSION_LEAK_COUNT_THRESHOLD = 100;
int64_t session_leak_count_threshold = - EVENT_CALL(EventTable::EN_SESSION_LEAK_COUNT_THRESHOLD);
session_leak_count_threshold = session_leak_count_threshold > 0
? session_leak_count_threshold
: DEFAULT_SESSION_LEAK_COUNT_THRESHOLD;
LOG_INFO("get current session count", K(used_session_count), K(hold_session_count),
K(session_leak_count_threshold));
if (hold_session_count - used_session_count >= session_leak_count_threshold) {
LOG_ERROR("session leak!!!", "leak_count", hold_session_count - used_session_count,
K(used_session_count), K(hold_session_count));
DumpHoldSession dump_session;
OZ(for_each_hold_session(dump_session));
}
}
return ret;
}
void ObSQLSessionMgr::runTimerTask()
{
try_check_session();
traverse_times_++;
// 30s clean kill client session map
if (traverse_times_ == TRAVERSE_MAX_TIMES) {
traverse_times_ = 0;
RecordCleanKillClientSession clean_kill_client_session(this);
for_each_kill_client_session(clean_kill_client_session);
for (int64_t i =0; i< clean_kill_client_session.clean_kill_array_.count(); i++) {
bool is_erased = false;
CleanKillClientSessionFin clean_kill_client_sessionf(this, clean_kill_client_session.clean_kill_array_.at(i).first,
clean_kill_client_session.clean_kill_array_.at(i).second);
GCTX.session_mgr_->get_kill_client_sess_map().erase_if(clean_kill_client_session.clean_kill_array_.at(i).first,
clean_kill_client_sessionf, is_erased);
}
}
}
// just a wrapper
int ObSQLSessionMgr::kill_query(ObSQLSessionInfo &session)
{
return kill_query(session, ObSQLSessionState::QUERY_KILLED);
}
int ObSQLSessionMgr::set_query_deadlocked(ObSQLSessionInfo &session)
{
return kill_query(session, ObSQLSessionState::QUERY_DEADLOCKED);
}
int ObSQLSessionMgr::kill_query(ObSQLSessionInfo &session,
const ObSQLSessionState status)
{
int ret = OB_SUCCESS;
int tmp_ret = OB_SUCCESS;
// If start_stmt/end_stmt gets stuck, at this point, we need to wake up the sql thread first, then set the flag, otherwise kill session will not work
if (OB_SUCCESS != (tmp_ret = ObSqlTransControl::kill_query_session(session, status))) {
LOG_WARN("fail to kill query or session", "ret", tmp_ret, K(session));
}
if (ObSQLSessionState::QUERY_KILLED == status) {
ret = session.kill_query();
} else if (ObSQLSessionState::QUERY_DEADLOCKED == status) {
ret = session.set_query_deadlocked();
} else {
LOG_WARN("unexpected status", K(status));
ret = OB_ERR_UNEXPECTED;
}
return ret;
}
// kill idle timeout transaction on this session
int ObSQLSessionMgr::kill_idle_timeout_tx(ObSQLSessionInfo *session)
{
return ObSqlTransControl::kill_idle_timeout_tx(session);
}
// kill deadlock transaction on this session
int ObSQLSessionMgr::kill_deadlock_tx(ObSQLSessionInfo *session)
{
return ObSqlTransControl::kill_deadlock_tx(session);
}
int ObSQLSessionMgr::kill_session(ObSQLSessionInfo &session)
{
int ret = OB_SUCCESS;
int tmp_ret = OB_SUCCESS;
// If start_stmt/end_stmt gets stuck, at this point, we need to wake up the sql thread first, then set the flag, otherwise kill session will not work
if (OB_SUCCESS != (tmp_ret = ObSqlTransControl::kill_query_session(
session, ObSQLSessionState::SESSION_KILLED))) {
LOG_WARN("fail to kill query or session", "ret", tmp_ret, K(session));
}
session.set_session_state(SESSION_KILLED);
// NOTE: The order of the following two guards cannot be changed, otherwise there is a chance of forming a deadlock
ObSQLSessionInfo::LockGuard query_lock_guard(session.get_query_lock());
ObSQLSessionInfo::LockGuard data_lock_guard(session.get_thread_data_lock());
bool need_disconnect = false;
session.set_query_start_time(ObTimeUtility::current_time());
session.set_mark_killed(true);
if (session.is_in_transaction()) {
if (OB_SUCCESS != (tmp_ret = ObSqlTransControl::kill_tx_on_session_killed(&session))) {
LOG_WARN("fail to rollback transaction", K(session.get_server_sid()),
"proxy_sessid", session.get_proxy_sessid(),
K(tmp_ret), KPC(session.get_tx_desc()),
"query_str", session.get_current_query_string(),
K(need_disconnect));
}
}
session.update_last_active_time();
session.set_disconnect_state(NORMAL_KILL_SESSION);
rpc::ObSqlSockDesc &sock_desc = session.get_sock_desc();
if (OB_LIKELY(NULL != sock_desc.sock_desc_)) {
SQL_REQ_OP.disconnect_by_sql_sock_desc(sock_desc);
// this function will trigger on_close(), and then free the session
LOG_INFO("kill session successfully",
"proxy", session.get_proxy_addr(),
"peer", session.get_peer_addr(),
"real_client_ip", session.get_client_ip(),
"server_sid", session.get_server_sid(),
"client_sid", session.get_client_sid(),
"proxy_sessid", session.get_proxy_sessid(),
"query_str", session.get_current_query_string());
} else {
LOG_WARN("get conn from session info is null", K(session.get_server_sid()),
"proxy_sessid", session.get_proxy_sessid(), K(session.get_magic_num()));
}
return ret;
}
int ObSQLSessionMgr::disconnect_session(ObSQLSessionInfo &session)
{
int ret = OB_SUCCESS;
// NOTE: The order of the following two guards cannot be changed, otherwise there is a chance of forming a deadlock
ObSQLSessionInfo::LockGuard query_lock_guard(session.get_query_lock());
ObSQLSessionInfo::LockGuard data_lock_guard(session.get_thread_data_lock());
bool need_disconnect = false;
session.set_query_start_time(ObTimeUtility::current_time());
// Call this function before session.set_session_state(SESSION_KILLED) is called in ObSMHandler::on_disconnect,
if (session.is_in_transaction()) {
if (OB_FAIL(ObSqlTransControl::kill_tx_on_session_disconnect(&session))) {
LOG_WARN("fail to rollback transaction", K(session.get_server_sid()),
"proxy_sessid", session.get_proxy_sessid(), K(ret),
"query_str", session.get_current_query_string(),
K(need_disconnect));
}
}
session.update_last_active_time();
return ret;
}
int ObSQLSessionMgr::kill_tenant(const uint64_t tenant_id, bool force_kill)
{
int ret = OB_SUCCESS;
KillTenant kt_func(this, tenant_id, force_kill);
OZ (for_each_session(kt_func));
OX (ret = kt_func.get_ret_code());
OZ (sessinfo_map_.clean_tenant(tenant_id));
LOG_INFO("kill tenant", K(tenant_id), K(force_kill));
return ret;
}
int ObSQLSessionMgr::mark_sessid_unused(uint32_t sess_id)
{
int ret = OB_SUCCESS;
uint64_t server_index = extract_server_index(sess_id);
if (0 == server_index) {
// Reference: create_sessid method
// Since server_id == 0, at this time, local_seq is generated by ATOMIC_FAA(&abnormal_seq, 1),
// The reason for using ATOMIC_FAA is unknown (the original author's information description contains no specific details), adopting a conservative modification strategy
// local_seq has never been obtained from the sessid_sequence_ queue, so it does not need to be returned to the queue, otherwise it will cause a bug of queue overflow
// bug details:
} else if (OB_FAIL(sessid_sequence_.push(reinterpret_cast<void*>(sess_id & MAX_LOCAL_SEQ)))) {
LOG_WARN("fail to push sessid to sessid_sequence_", K(sess_id), K(ret));
}
return ret;
}
bool ObSQLSessionMgr::CheckSessionFunctor::operator()(sql::ObSQLSessionMgr::Key key,
ObSQLSessionInfo *sess_info)
{
int ret = OB_SUCCESS;
UNUSED(key);
bool is_timeout = false;
if (OB_ISNULL(sess_info)) {
ret = OB_NOT_INIT;
LOG_WARN("session info is NULL");
} else if (false == sess_info->is_valid()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("session info is not valid", K(ret));
} else {
int callback_retcode = OB_SUCCESS;
transaction::ObITxCallback *commit_cb = NULL;
// NOTE: The order of the following two guards cannot be changed, otherwise there is a chance of forming a deadlock
if (OB_FAIL(sess_info->try_lock_query())) {
if (OB_UNLIKELY(OB_EAGAIN != ret)) {
LOG_WARN("fail to try lock query", K(ret));
} else {
ret = OB_SUCCESS;
}
} else {
if (OB_FAIL(sess_info->try_lock_thread_data())) {
if (OB_UNLIKELY(OB_EAGAIN != ret)) {
LOG_WARN("fail to try lock thread data", K(ret));
} else {
ret = OB_SUCCESS;
}
} else {
if (OB_ISNULL(sess_mgr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session manager point is NULL");
} else if (OB_FAIL(sess_info->is_timeout(is_timeout))) {
LOG_WARN("fail to check is timeout", K(ret));
} else if (true == is_timeout) {
LOG_INFO("session is timeout, kill this session", K(key.sessid_));
ret = sess_mgr_->kill_session(*sess_info);
} else {
//with the help ofsession traversaloffunctionality,tryrevert sessioncacheofschema guard,
// Avoid holding guard for a long time, causing schema mgr slots to be unable to release
sess_info->get_cached_schema_guard_info().try_revert_schema_guard();
// Regularly update tenant-level configuration items to avoid the performance overhead of frequently retrieving tenant-level configuration items
sess_info->refresh_tenant_config();
// send client commit result if txn commit timeout
if (OB_FAIL(sess_info->is_trx_commit_timeout(commit_cb, callback_retcode))) {
LOG_WARN("fail to check transaction commit timeout", K(ret));
} else if (commit_cb) {
LOG_INFO("transaction commit reach timeout", K(callback_retcode), K(key.sessid_));
} else if (OB_FAIL(sess_info->is_trx_idle_timeout(is_timeout))) {
// kill transaction which is idle more than configuration 'ob_trx_idle_timeout'
LOG_WARN("fail to check transaction idle timeout", K(ret));
} else if (true == is_timeout && !sess_info->associated_xa()) {
LOG_INFO("transaction is idle timeout, start to rollback", K(key.sessid_));
int tmp_ret;
if (OB_SUCCESS != (tmp_ret = sess_mgr_->kill_idle_timeout_tx(sess_info))) {
LOG_WARN("fail to kill transaction", K(ret), K(key.sessid_));
}
}
}
(void)sess_info->unlock_thread_data();
}
(void)sess_info->unlock_query();
// NOTE: must execute callback after release query_lock
if (commit_cb) {
commit_cb->callback(callback_retcode);
}
}
}
//detect sql hung
int64_t tmp_ret = (OB_E(EventTable::EN_SQL_HUNG_DETECT) OB_SUCCESS);
int64_t timeout_multiplier = OB_ERROR == tmp_ret ? 2 : std::max(static_cast<int64_t>(1), std::abs(tmp_ret));
if (OB_FAIL(ret)) {
} else if (OB_SUCCESS == tmp_ret) {
//do nothing
} else if (obmysql::COM_QUERY == sess_info->get_mysql_cmd() ||
obmysql::COM_STMT_EXECUTE == sess_info->get_mysql_cmd() ||
obmysql::COM_STMT_PREPARE == sess_info->get_mysql_cmd() ||
obmysql::COM_STMT_PREXECUTE == sess_info->get_mysql_cmd()) {
int64_t cur_time = common::ObTimeUtility::current_time();
int64_t query_timeout = 0;
ObSQLSessionInfo::LockGuard lock_guard(sess_info->get_thread_data_lock());
if ((sess_info->get_stmt_type() != stmt::T_SELECT &&
sess_info->get_stmt_type() != stmt::T_UPDATE &&
sess_info->get_stmt_type() != stmt::T_INSERT &&
sess_info->get_stmt_type() != stmt::T_DELETE &&
sess_info->get_stmt_type() != stmt::T_REPLACE &&
sess_info->get_stmt_type() != stmt::T_EXPLAIN) ||
sess_info->get_ddl_info().is_ddl() ||
OB_NOT_NULL(sess_info->get_pl_context()) ||
!sess_info->is_user_session() ||
sess_info->is_remote_session() ||
sess_info->get_current_trace_id().is_invalid()) {
//filter out DDL, PL and physical restore tenant statements, because they are not subject to query timeout control.
} else if (OB_FAIL(sess_info->get_sys_variable(SYS_VAR_OB_QUERY_TIMEOUT, query_timeout))) {
LOG_WARN("failed to get sesion variable", K(ret));
} else if (sess_info->get_query_start_time() > 0 &&
cur_time - sess_info->get_query_start_time() > timeout_multiplier * query_timeout + 1000000) {
LOG_ERROR("detect sql hung!!!", K(sess_info->get_current_trace_id()),
K(sess_info->get_cur_sql_id()),
K(sess_info->get_thread_id()),
K(cur_time - sess_info->get_query_start_time()),
K(query_timeout), K(timeout_multiplier),
"session_state", ObString::make_string(sess_info->get_session_state_str()),
K(sess_info->get_current_query_string()));
//dump stack of all threads of observer
IGNORE_RETURN faststack();
}
}
return OB_SUCCESS == ret;
}
bool ObSQLSessionMgr::KillTenant::operator() (
sql::ObSQLSessionMgr::Key, ObSQLSessionInfo *sess_info)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(mgr_)) {
ret = OB_NOT_INIT;
LOG_WARN("session mgr_ is NULL", K(mgr_));
} else if (OB_ISNULL(sess_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sess info is NULL", K(sess_info));
} else {
if (sess_info->get_priv_tenant_id() == tenant_id_ ||
sess_info->get_effective_tenant_id() == tenant_id_) {
bool need_kill = true;
if (force_kill_) {
ret = mgr_->kill_session(*sess_info);
} else if (OB_FAIL(sess_info->can_kill_session_immediately(need_kill))) {
LOG_WARN("failed to check can gc immediately");
} else if (!need_kill) {
ret_ = OB_EAGAIN;
LOG_TRACE("unit gc needs to wait", K(ret_));
} else if (need_kill) {
LOG_INFO("force kill session", K(sess_info->get_server_sid()));
ret = mgr_->kill_session(*sess_info);
}
}
}
return OB_SUCCESS == ret;
}
int ObSQLSessionMgr::get_avaiable_local_seq(uint32_t &local_seq)
{
int ret = OB_SUCCESS;
local_seq = 0;
void *ptr = nullptr;
if (OB_FAIL(sessid_sequence_.pop(ptr))) {
LOG_WARN("fail to find and set first zero bit", K(ret));
} else {
int64_t value = reinterpret_cast<int64_t>(ptr);
if (value > MAX_LOCAL_SEQ) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected value from bitset", K(ret), K(value), K(ptr));
} else {
local_seq = static_cast<uint32_t>(value);
}
}
if (OB_ENTRY_NOT_EXIST == ret) {
ret = OB_ERR_CON_COUNT_ERROR;
int64_t sess_count = 0;
(void)get_session_count(sess_count);
LOG_WARN("too many connection", "connection_count", sess_count, K(ret));
}
return ret;
}
int ObSQLSessionMgr::is_need_clear_sessid(const ObSMConnection *conn, bool &is_need)
{
int ret = OB_SUCCESS;
is_need = false;
if (OB_ISNULL(conn)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected parameter", K(conn));
} else if (is_server_sessid(conn->sessid_)
&& ObSMConnection::INITIAL_SESSID != conn->sessid_
&& 0 != extract_server_index(conn->sessid_)
&& GCTX.get_server_index() == extract_server_index(conn->sessid_)
&& conn->is_need_clear_sessid_) {
is_need = true;
} else {/*do nothing*/ }
return ret;
}
int ObSQLSessionMgr::DumpHoldSession::operator()(
common::hash::HashMapPair<uint64_t, ObSQLSessionInfo *> &entry)
{
int ret = common::OB_SUCCESS;
if (OB_ISNULL(entry.second)) {
LOG_INFO("session is null", "sess_ptr", entry.first);
} else {
LOG_INFO("dump session", "sid", entry.second->get_server_sid(),
"ref_count", entry.second->get_sess_ref_cnt(),
"state",ObString::make_string(entry.second->get_session_state_str()),
KP(entry.second),
K(entry.first),
"lbt", entry.second->get_sess_bt());
}
return ret;