forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_sql_trans_control.cpp
More file actions
1637 lines (1538 loc) · 64.3 KB
/
Copy pathob_sql_trans_control.cpp
File metadata and controls
1637 lines (1538 loc) · 64.3 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_EXE
#include "ob_sql_trans_control.h"
#include "storage/tablelock/ob_table_lock_service.h"
#include "sql/executor/ob_task_spliter.h"
#include "observer/ob_server.h"
#include "storage/tx_storage/ob_ls_service.h"
#include "sql/dblink/ob_tm_service.h"
#include "storage/memtable/ob_lock_wait_mgr.h"
#ifdef CHECK_SESSION
#error "redefine macro CHECK_SESSION"
#else
#define CHECK_SESSION(session) \
if (OB_SUCC(ret) && session->is_zombie()) { \
ret = OB_ERR_SESSION_INTERRUPTED; \
LOG_WARN("session has been killed", KR(ret), KPC(session)); \
}
#endif
namespace oceanbase
{
using namespace common;
using namespace transaction;
using namespace share;
using namespace share::schema;
using namespace share::detector;
namespace sql
{
static int get_tx_service(ObBasicSessionInfo *session,
ObTransService *&txs)
{
int ret = OB_SUCCESS;
uint64_t effective_tenant_id = session->get_effective_tenant_id();
if (OB_NOT_NULL(session->get_tx_desc())) {
uint64_t tx_tenant_id = session->get_tx_desc()->get_tenant_id();
if (effective_tenant_id != tx_tenant_id) {
ret = OB_TENANT_ID_NOT_MATCH;
LOG_ERROR("effective_tenant_id not equals to tx_tenant_id", K(ret), K(effective_tenant_id), K(tx_tenant_id), KPC(session));
}
}
if (OB_SUCC(ret)) {
if (OB_ISNULL(txs = MTL_WITH_CHECK_TENANT(ObTransService*, effective_tenant_id))) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("get_tx_service", K(ret), K(effective_tenant_id), K(MTL_ID()));
}
}
return ret;
}
static inline int get_lock_service(uint64_t tenant_id, tablelock::ObTableLockService *&lock_service)
{
int ret = OB_SUCCESS;
lock_service = MTL_WITH_CHECK_TENANT(tablelock::ObTableLockService*, tenant_id);
if (OB_ISNULL(lock_service)) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("get_lock_service", K(ret), K(tenant_id), K(MTL_ID()));
}
return ret;
}
static int get_org_cluster_id_(ObSQLSessionInfo*, int64_t &);
static inline int build_tx_param_(ObSQLSessionInfo *session, ObTxParam &p, const bool *readonly = nullptr)
{
int ret = OB_SUCCESS;
int64_t org_cluster_id = OB_INVALID_ORG_CLUSTER_ID;
OZ (get_org_cluster_id_(session, org_cluster_id));
int64_t tx_timeout_us = 0;
session->get_tx_timeout(tx_timeout_us);
p.timeout_us_ = tx_timeout_us;
p.lock_timeout_us_ = session->get_trx_lock_timeout();
bool ro = OB_NOT_NULL(readonly) ? *readonly : session->get_tx_read_only();
p.access_mode_ = ro ? ObTxAccessMode::RD_ONLY : ObTxAccessMode::RW;
p.isolation_ = session->get_tx_isolation();
p.cluster_id_ = org_cluster_id;
return ret;
}
// NOTE that only for mysql mode
// in mysql xa, the session id is 0 by default
int ObSqlTransControl::create_stash_savepoint(ObExecContext &ctx, const ObString &name)
{
int ret = OB_SUCCESS;
ObTransService *txs = NULL;
ObSQLSessionInfo *session = GET_MY_SESSION(ctx);
CK (OB_NOT_NULL(session));
OZ (get_tx_service(session, txs));
OZ (acquire_tx_if_need_(txs, *session));
OZ (txs->create_stash_savepoint(*session->get_tx_desc(), name));
return ret;
}
int ObSqlTransControl::explicit_start_trans(ObExecContext &ctx, const bool read_only, const ObString hint)
{
int ret = OB_SUCCESS;
ObPhysicalPlanCtx *plan_ctx = GET_PHY_PLAN_CTX(ctx);
if (OB_ISNULL(plan_ctx)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid arguments", K(ctx), K(read_only), K(hint));
} else {
ret = explicit_start_trans(ctx.get_my_session(),
plan_ctx->get_trans_param(),
ctx.get_need_disconnect_for_update(),
read_only,
hint);
}
return ret;
}
int ObSqlTransControl::explicit_start_trans(ObSQLSessionInfo *session,
transaction::ObTxParam &tx_param,
bool &need_disconnect,
const bool read_only,
const ObString hint)
{
int ret = OB_SUCCESS;
ObTransService *txs = NULL;
uint64_t tenant_id = 0;
ObTransID tx_id;
bool cleanup = true;
CK (OB_NOT_NULL(session));
CHECK_SESSION(session);
if (OB_SUCC(ret) && session->is_in_transaction()) {
ret = OB_ERR_UNEXPECTED;
cleanup = false;
LOG_ERROR("nested start transaction not allowed", KR(ret), KP(session));
}
OX (tenant_id = session->get_effective_tenant_id());
OZ (get_tx_service(session, txs), tenant_id);
if (OB_SUCC(ret) && OB_NOT_NULL(session->get_tx_desc())) {
ObSQLSessionInfo::LockGuard data_lock_guard(session->get_thread_data_lock());
ObTxDesc *tx_desc = session->get_tx_desc();
if (tx_desc->get_tenant_id() != tenant_id) {
LOG_ERROR("switch tenant but hold tx_desc", K(tenant_id), KPC(tx_desc));
}
txs->release_tx(*tx_desc);
session->get_tx_desc() = NULL;
}
OZ (build_tx_param_(session, tx_param, &read_only));
OZ (txs->acquire_tx(session->get_tx_desc(), session->get_server_sid(), session->get_sid(), session->get_data_version()));
OZ (txs->start_tx(*session->get_tx_desc(), tx_param), tx_param);
OX (tx_id = session->get_tx_desc()->get_tx_id());
if (OB_FAIL(ret) && cleanup && OB_NOT_NULL(txs) && OB_NOT_NULL(session->get_tx_desc())) {
ObSQLSessionInfo::LockGuard data_lock_guard(session->get_thread_data_lock());
txs->release_tx(*session->get_tx_desc());
session->get_tx_desc() = NULL;
}
OX (set_audit_tx_id_(session));
OX (session->get_raw_audit_record().seq_num_ = ObSequence::get_max_seq_no());
NG_TRACE_EXT(start_trans, OB_ID(ret), ret,
OB_ID(trans_id), tx_id.get_id(),
OB_ID(timeout), tx_param.timeout_us_,
OB_ID(start_time), session ? session->get_query_start_time() : 0);
if (hint.length()) {
LOG_INFO("explicit start trans with hint", "trans_id", tx_id,
K(ret), K(hint), K(read_only), "session_id", (session ? session->get_server_sid() : 0));
}
#ifndef NDEBUG
LOG_INFO("start_trans", K(ret), K(tx_id), KPC(session), K(read_only));
#endif
return ret;
}
int ObSqlTransControl::implicit_end_trans(ObExecContext &exec_ctx,
const bool is_rollback,
ObEndTransAsyncCallback *callback,
bool reset_trans_variable)
{
return end_trans(exec_ctx.get_my_session(),
exec_ctx.get_need_disconnect_for_update(),
exec_ctx.get_trans_state(),
is_rollback,
false,
callback,
reset_trans_variable);
}
int ObSqlTransControl::explicit_end_trans(ObExecContext &exec_ctx, const bool is_rollback, const ObString hint)
{
ObSQLSessionInfo *session = exec_ctx.get_my_session();
ObEndTransAsyncCallback *callback = nullptr;
if (exec_ctx.is_end_trans_async() && OB_NOT_NULL(session)) {
callback = &session->get_end_trans_cb();
}
return end_trans(session,
exec_ctx.get_need_disconnect_for_update(),
exec_ctx.get_trans_state(),
is_rollback,
true,
callback,
true,
hint);
}
int ObSqlTransControl::end_trans(ObSQLSessionInfo *session,
bool &need_disconnect,
TransState &trans_state,
const bool is_rollback,
const bool is_explicit,
ObEndTransAsyncCallback *callback,
bool reset_trans_variable,
const ObString hint)
{
int ret = OB_SUCCESS;
bool sync = false;
int64_t tx_id = 0;
#ifndef NDEBUG
LOG_INFO("end_trans", K(session->is_in_transaction()),
K(session->has_explicit_start_trans()),
KPC(session->get_tx_desc()),
K(is_explicit),
KP(callback));
#endif
FLTSpanGuard(end_transaction);
if (OB_ISNULL(session)) {
ret = OB_INVALID_ARGUMENT;
LOG_ERROR("invalid argument", K(ret), KPC(session));
} else if (!is_explicit && !session->is_inner() && session->associated_xa()) {
// NOTE that not support dblink trans in this interface
// PLEASE handle implicit cases for dblink trans instead of this interface
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("executing do end trans in xa", K(ret), K(session->get_xid()));
} else {
if (OB_NOT_NULL(callback)) {
callback->set_is_need_rollback(is_rollback);
callback->set_end_trans_type(is_explicit ?
ObExclusiveEndTransCallback::END_TRANS_TYPE_EXPLICIT :
ObExclusiveEndTransCallback::END_TRANS_TYPE_IMPLICIT);
}
if (OB_NOT_NULL(session->get_tx_desc())) {
tx_id = session->get_tx_id().get_id();
}
}
if (OB_FAIL(ret)) {
} else if (!session->is_in_transaction()) {
if (!is_rollback && OB_NOT_NULL(callback)) {
if (OB_FAIL(inc_session_ref(session))) {
LOG_WARN("fail to inc session ref", K(ret));
} else {
callback->handout();
}
callback->callback(OB_SUCCESS);
} else {
reset_session_tx_state(session, true, reset_trans_variable);
need_disconnect = false;
}
} else {
// add tx id to AuditRecord
set_audit_tx_id_(session);
int64_t expire_ts = get_stmt_expire_ts(NULL, *session);
if (OB_FAIL(do_end_trans_(session,
is_rollback,
is_explicit,
expire_ts,
callback))) {
}
ObSQLUtils::check_if_need_disconnect_after_end_trans(ret,
is_rollback,
is_explicit,
need_disconnect);
if (is_rollback || OB_FAIL(ret) || !callback) {
bool reuse_tx = OB_SUCCESS == ret
|| OB_TRANS_COMMITED == ret
|| OB_TRANS_ROLLBACKED == ret;
reset_session_tx_state(session, reuse_tx, reset_trans_variable);
}
}
if (callback && !is_rollback) {
trans_state.set_end_trans_executed(OB_SUCC(ret));
}
OX (session->get_raw_audit_record().seq_num_ = ObSequence::get_max_seq_no());
if (is_explicit && hint.length()) {
LOG_INFO("explicit end trans with hint",
"trans_id", tx_id, "action", (is_rollback ? "ROLLBACK" : "COMMIT"),
K(ret), K(hint), "session_id", session->get_server_sid());
}
FLT_SET_TAG(trans_id, tx_id);
return ret;
}
int ObSqlTransControl::end_trans_before_cmd_execute(ObSQLSessionInfo &session,
bool &need_disconnect,
TransState &trans_state,
const int cmd_type)
{
int ret = OB_SUCCESS;
// implicit end transaction and start transaction will not clear next scope transaction settings by:
// a. set by `set transaction read only`
// b. set by `set transaction isolation level XXX`
bool keep_trans_variable = (cmd_type == stmt::T_START_TRANS);
if (OB_FAIL(ObSqlTransControl::end_trans(&session,
need_disconnect,
trans_state,
false, // is_rollback
false, // is_explicit
nullptr, // callback
!keep_trans_variable))) {
LOG_WARN("implicit end trans fail", KR(ret), K(cmd_type), K(need_disconnect));
} else if (session.need_recheck_txn_readonly() && session.get_tx_read_only()) {
ret = OB_ERR_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION;
LOG_WARN("cmd can not execute because txn is read only", K(ret));
}
return ret;
}
int ObSqlTransControl::kill_query_session(ObSQLSessionInfo &session,
const ObSQLSessionState &status)
{
int ret = OB_SUCCESS;
if (session.get_in_transaction()) {
ObTxDesc *tx_desc = session.get_tx_desc();
uint64_t tx_tenant_id = tx_desc->get_tenant_id();
MTL_SWITCH(tx_tenant_id) {
ObTransService *txs = NULL;
CK(OB_NOT_NULL(txs = MTL_WITH_CHECK_TENANT(ObTransService*, tx_tenant_id)));
OZ(txs->interrupt(*tx_desc, OB_ERR_QUERY_INTERRUPTED),
tx_desc->get_tx_id(), status);
LOG_INFO("kill_query_session", K(ret), K(session), K(tx_desc->get_tx_id()),
"session_status", status);
}
}
return ret;
}
int ObSqlTransControl::kill_idle_timeout_tx(ObSQLSessionInfo *session)
{
int ret = OB_SUCCESS;
ret = kill_tx(session, OB_TRANS_IDLE_TIMEOUT);
return ret;
}
int ObSqlTransControl::kill_tx(ObSQLSessionInfo *session, int cause)
{
int ret = OB_SUCCESS;
if (!session->get_is_deserialized() && session->is_in_transaction()) {
uint32_t session_id = session->get_server_sid();
if (cause >= 0) {
LOG_INFO("begin to kill tx", "caused_by", ObTxAbortCauseNames::of(cause), K(cause), K(session_id), KPC(session));
} else {
LOG_INFO("begin to kill tx", "caused_by", common::ob_error_name(cause), K(cause), K(session_id), KPC(session));
}
ObTxDesc *tx_desc = session->get_tx_desc();
uint64_t tx_tenant_id = tx_desc->get_tenant_id();
const ObTransID tx_id = tx_desc->get_tx_id();
MTL_SWITCH(tx_tenant_id) {
ObSQLSessionInfo::LockGuard data_lock_guard(session->get_thread_data_lock());
ObTransService *txs = NULL;
CK(OB_NOT_NULL(txs = MTL_WITH_CHECK_TENANT(ObTransService*, tx_tenant_id)));
OZ(txs->abort_tx(*tx_desc, cause), *session, tx_desc->get_tx_id());
// NOTE that the tx_desc is set to NULL in xa case, DO NOT print anything in tx_desc
LOG_INFO("kill tx done", K(ret), K(cause), K(session_id), K(tx_id));
}
}
return ret;
}
int ObSqlTransControl::rollback_trans(ObSQLSessionInfo *session,
bool &need_disconnect)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(session)) {
ret = OB_INVALID_ARGUMENT;
LOG_ERROR("", K(ret), K(session));
} else if (OB_NOT_NULL(session->get_tx_desc())) {
need_disconnect = false;
if (OB_FAIL(do_end_trans_(session, true, false, INT64_MAX, NULL))) {
LOG_WARN("fail rollback trans", K(ret), KPC(session->get_tx_desc()));
ObSQLUtils::check_if_need_disconnect_after_end_trans(
ret, true, false, need_disconnect);
}
reset_session_tx_state(session);
} else {
reset_session_tx_state(session);
}
return ret;
}
ERRSIM_POINT_DEF(SQL_DO_END_TX_FAIL)
int ObSqlTransControl::do_end_trans_(ObSQLSessionInfo *session,
const bool is_rollback,
const bool is_explicit,
const int64_t expire_ts,
ObEndTransAsyncCallback *callback)
{
int ret = OB_SUCCESS;
ObTxDesc *&tx_ptr = session->get_tx_desc();
bool is_detector_exist = false;
int tmp_ret = OB_SUCCESS;
const int64_t lcl_op_interval = GCONF._lcl_op_interval;
if (lcl_op_interval <= 0) {
// do nothing
} else if (OB_ISNULL(MTL(share::detector::ObDeadLockDetectorMgr*))) {
tmp_ret = OB_BAD_NULL_ERROR;
DETECT_LOG(WARN, "MTL ObDeadLockDetectorMgr is NULL", K(tmp_ret), K(tx_ptr->tid()));
} else if (OB_TMP_FAIL(MTL(share::detector::ObDeadLockDetectorMgr*)->
check_detector_exist(tx_ptr->tid(), is_detector_exist))) {
DETECT_LOG(WARN, "fail to check detector exist, may causing detector leak", K(tmp_ret),
K(tx_ptr->tid()));
} else if (is_detector_exist) {
ObTransDeadlockDetectorAdapter::unregister_from_deadlock_detector(tx_ptr->tid(),
ObTransDeadlockDetectorAdapter::UnregisterPath::DO_END_TRANS);
}
if (!session->is_inner() && session->associated_xa() && !is_explicit) {
ret = OB_TRANS_XA_RMFAIL;
LOG_ERROR("executing do end trans in xa", K(ret), K(session->get_xid()), KPC(tx_ptr));
} else if (OB_FAIL(SQL_DO_END_TX_FAIL)) {
LOG_WARN("do end trans failed", K(ret));
} else {
/*
* normal transaction control
*
* call convention:
* if trans_service.end_trans failed:
* 1) tx will be aborted (if tx exist and not terminated)
* 2) the callback will not been called
*/
ObTransService *txs = NULL;
uint64_t tenant_id = session->get_effective_tenant_id();
const common::ObString &trace_info = session->get_ob_trace_info();
if (OB_FAIL(get_tx_service(session, txs))) {
LOG_ERROR("fail to get trans service", K(ret), K(tenant_id));
} else if (is_rollback) {
ret = txs->rollback_tx(*tx_ptr);
} else if (callback) {
if (OB_FAIL(inc_session_ref(session))) {
LOG_WARN("fail to inc session ref", K(ret));
} else {
callback->handout();
ObDiagnosticInfo *di = ObLocalDiagnosticInfo::get();
if (OB_NOT_NULL(di)) {
di->get_ash_stat().in_committing_ = true;
di->begin_wait_event(ObWaitEventIds::ASYNC_COMMITTING_WAIT);
}
callback->set_diagnostic_info(di);
if(OB_FAIL(txs->submit_commit_tx(*tx_ptr, expire_ts, *callback, &trace_info))) {
LOG_WARN("submit commit tx fail", K(ret), KP(callback), K(expire_ts), KPC(tx_ptr));
callback->reset_diagnostic_info();
GCTX.session_mgr_->revert_session(session);
if (OB_NOT_NULL(di)) {
di->get_ash_stat().in_committing_ = false;
di->end_wait_event(ObWaitEventIds::ASYNC_COMMITTING_WAIT);
}
callback->handin();
}
}
} else {
ACTIVE_SESSION_FLAG_SETTER_GUARD(in_committing);
if (OB_FAIL(txs->commit_tx(*tx_ptr, expire_ts, &trace_info))) {
LOG_WARN("sync commit tx fail", K(ret), K(expire_ts), KPC(tx_ptr));
}
}
}
bool print_log = OB_FAIL(ret);
#ifndef NDEBUG
print_log = true;
#endif
if (print_log) {
LOG_INFO("do_end_trans", K(ret),
KPC(tx_ptr),
K(is_rollback),
K(expire_ts),
K(is_explicit),
KP(callback));
}
return ret;
}
int ObSqlTransControl::decide_trans_read_interface_specs(
const ObConsistencyLevel &sql_consistency_level,
ObTxConsistencyType &trans_consistency_type)
{
int ret = OB_SUCCESS;
if (sql_consistency_level == STRONG) {
trans_consistency_type = ObTxConsistencyType::CURRENT_READ;
} else if (sql_consistency_level == WEAK || sql_consistency_level == FROZEN){
trans_consistency_type = ObTxConsistencyType::BOUNDED_STALENESS_READ;
} else {
ret = OB_INVALID_ARGUMENT;
SQL_LOG(ERROR, "invalid consistency_level", K(sql_consistency_level));
}
return ret;
}
int ObSqlTransControl::start_stmt(ObExecContext &exec_ctx)
{
int ret = OB_SUCCESS;
memtable::advance_tlocal_request_lock_wait_stat(rpc::RequestLockWaitStat::RequestStat::START);
ObSQLSessionInfo *session = GET_MY_SESSION(exec_ctx);
ObPhysicalPlanCtx *plan_ctx = GET_PHY_PLAN_CTX(exec_ctx);
const ObPhysicalPlan *plan = plan_ctx->get_phy_plan();
ObDASCtx &das_ctx = DAS_CTX(exec_ctx);
ObTransService *txs = NULL;
uint64_t tenant_id = 0;
CK (OB_NOT_NULL(session), OB_NOT_NULL(plan_ctx), OB_NOT_NULL(plan));
OX (tenant_id = session->get_effective_tenant_id());
OX (session->get_trans_result().reset());
OZ (get_tx_service(session, txs), tenant_id);
OZ (acquire_tx_if_need_(txs, *session));
OZ (stmt_sanity_check_(session, plan, plan_ctx));
bool start_hook = false;
if (!ObSQLUtils::is_nested_sql(&exec_ctx)) {
OZ (txs->sql_stmt_start_hook(session->get_xid(), *session->get_tx_desc(), session->get_server_sid(), get_real_session_id(*session)));
if (OB_SUCC(ret)) {
start_hook = true;
OX (session->get_tx_desc()->clear_interrupt());
}
}
uint32_t session_id = 0;
ObTxDesc *tx_desc = NULL;
bool is_plain_select = false;
int64_t nested_level = 0;
OX (nested_level = exec_ctx.get_nested_level());
OX (session_id = session->get_server_sid());
OX (tx_desc = session->get_tx_desc());
OX (is_plain_select = plan->is_plain_select());
OX (tx_desc->clear_interrupt());
if (OB_SUCC(ret) && !is_plain_select) {
OZ (stmt_setup_savepoint_(session, das_ctx, plan_ctx, txs, nested_level), session_id, *tx_desc);
}
OZ (stmt_setup_snapshot_(session, das_ctx, plan, plan_ctx, txs, exec_ctx), session_id, *tx_desc);
// add snapshot info to AuditRecord
if (OB_SUCC(ret)) {
ObAuditRecordData &audit_record = session->get_raw_audit_record();
ObTxReadSnapshot &snapshot = das_ctx.get_snapshot();
#ifdef ENABLE_DEBUG_LOG
(void)snapshot.format_source_for_display(audit_record.snapshot_source_, sizeof(audit_record.snapshot_source_));
#endif
audit_record.snapshot_ = {
.version_ = snapshot.core_.version_,
.tx_id_ = snapshot.core_.tx_id_.get_id(),
.scn_ = static_cast<int64_t>(snapshot.core_.scn_.cast_to_int()),
.source_ = audit_record.snapshot_source_
};
audit_record.seq_num_ = ObSequence::get_max_seq_no();
}
if (OB_SUCC(ret) && !session->has_start_stmt()) {
OZ (session->set_start_stmt());
}
if (OB_FAIL(ret) && start_hook) {
int tmp_ret = txs->sql_stmt_end_hook(session->get_xid(), *session->get_tx_desc());
if (OB_SUCCESS != tmp_ret) {
LOG_WARN("call sql stmt end hook fail", K(tmp_ret));
}
}
if (OB_SUCC(ret)
&& !ObSQLUtils::is_nested_sql(&exec_ctx)
&& das_ctx.get_snapshot().core_.version_.is_valid()) {
// maintain the read snapshot version on session for multi-version garbage
// colloecor. It is maintained for all cases except remote exection with ac
// = 1. So we need carefully design the version for the corner case.
session->set_reserved_snapshot_version(das_ctx.get_snapshot().core_.version_);
}
bool print_log = false;
#ifndef NDEBUG
print_log = true;
#else
if (OB_FAIL(ret)) { print_log = true; }
#endif
if (print_log) {
bool auto_commit = false;
session->get_autocommit(auto_commit);
ObPhyPlanType plan_type = plan->get_location_type();
stmt::StmtType stmt_type = plan->get_stmt_type();
bool has_for_update = plan->has_for_update();
bool use_das = plan->use_das();
ObTxExecResult &trans_result = session->get_trans_result();
int64_t query_start_time = session->get_query_start_time();
ObTxReadSnapshot &snapshot = das_ctx.get_snapshot();
ObTxSEQ savepoint = das_ctx.get_savepoint();
LOG_INFO("start stmt", K(ret),
K(auto_commit),
K(session_id),
K(snapshot),
K(savepoint),
KPC(tx_desc),
K(plan_type),
K(stmt_type),
K(has_for_update),
K(query_start_time),
K(use_das),
K(nested_level),
KPC(session),
K(plan),
"consistency_level_in_plan_ctx", plan_ctx->get_consistency_level(),
K(trans_result));
}
return ret;
}
int ObSqlTransControl::stmt_sanity_check_(ObSQLSessionInfo *session,
const ObPhysicalPlan *plan,
ObPhysicalPlanCtx *plan_ctx)
{
int ret = OB_SUCCESS;
ObConsistencyLevel current_consist_level = plan_ctx->get_consistency_level();
CK (current_consist_level != ObConsistencyLevel::INVALID_CONSISTENCY);
const bool contain_inner_table = plan->is_contain_inner_table();
// adjust stmt's consistency level
if (OB_SUCC(ret)) {
// Weak read statement with inner table should be converted to strong read.
// For example, schema refresh statement;
if (contain_inner_table ||
(!plan->is_plain_select() && current_consist_level != ObConsistencyLevel::STRONG)) {
plan_ctx->set_consistency_level(ObConsistencyLevel::STRONG);
}
}
if (OB_SUCC(ret) && session->is_in_transaction()) {
// check consistency type volatile
ObConsistencyLevel current_consist_level = plan_ctx->get_consistency_level();
if (current_consist_level == ObConsistencyLevel::WEAK) {
// read write transaction
if (!session->get_tx_desc()->is_clean()) {
plan_ctx->set_consistency_level(ObConsistencyLevel::STRONG);
}
}
// check isolation with consistency type
ObTxIsolationLevel iso = session->get_tx_desc()->get_isolation_level();
ObConsistencyLevel cl = plan_ctx->get_consistency_level();
if (ObConsistencyLevel::WEAK == cl && is_isolation_RR_or_SE(iso)) {
ret = OB_NOT_SUPPORTED;
TRANS_LOG(ERROR, "statement of weak consistency is not allowed under SERIALIZABLE isolation",
KR(ret), "trans_id", session->get_tx_id(), "consistency_level", cl);
LOG_USER_ERROR(OB_NOT_SUPPORTED, "weak consistency under SERIALIZABLE and REPEATABLE-READ isolation level");
}
}
if (OB_SUCC(ret)
&& !plan_ctx->check_consistency_level_validation(contain_inner_table)) {
ret = OB_ERR_UNEXPECTED;
TRANS_LOG(ERROR, "unexpected consistency level", K(ret), K(contain_inner_table),
"current_level", current_consist_level,
"plan_ctx_level", plan_ctx->get_consistency_level());
}
return ret;
}
int ObSqlTransControl::stmt_setup_snapshot_(ObSQLSessionInfo *session,
ObDASCtx &das_ctx,
const ObPhysicalPlan *plan,
const ObPhysicalPlanCtx *plan_ctx,
ObTransService *txs,
ObExecContext &exec_ctx)
{
int ret = OB_SUCCESS;
ObConsistencyLevel cl = plan_ctx->get_consistency_level();
ObTxReadSnapshot &snapshot = das_ctx.get_snapshot();
bool can_plain_insert = false;
if (cl == ObConsistencyLevel::WEAK || cl == ObConsistencyLevel::FROZEN) {
SCN snapshot_version = SCN::min_scn();
const bool local_single_ls = plan->is_local_plan() &&
OB_PHY_PLAN_LOCAL == plan->get_location_type();
if (OB_FAIL(txs->get_weak_read_snapshot_version(session->get_ob_max_read_stale_time(),
local_single_ls,
snapshot_version))) {
TRANS_LOG(WARN, "get weak read snapshot fail", KPC(txs));
int64_t stale_time = session->get_ob_max_read_stale_time();
int64_t refresh_interval = GCONF.weak_read_version_refresh_interval;
if (stale_time > 0 && refresh_interval > stale_time) {
TRANS_LOG(WARN, "weak_read_version_refresh_interval is larger than ob_max_read_stale_time ",
K(refresh_interval), K(stale_time), KPC(txs));
}
} else {
snapshot.init_weak_read(snapshot_version);
}
// 1) acquire snapshot version when insert operator is executed
// 2) don't resolve RR and SERIALIZABLE isolation scenario temporarily, because of remote stmt plan
} else if (!plan->is_plain_select() &&
OB_FAIL(can_do_plain_insert(session, plan, exec_ctx, can_plain_insert))) {
TRANS_LOG(WARN, "check can do plain insert failed", KPC(txs));
} else if (can_plain_insert) {
ObTxDesc &tx_desc = *session->get_tx_desc();
das_ctx.set_use_gts_opt(true);
snapshot.init_none_read();
snapshot.core_.tx_id_ = tx_desc.get_tx_id();
snapshot.core_.scn_ = tx_desc.get_tx_seq();
} else {
ObTxDesc &tx_desc = *session->get_tx_desc();
int64_t stmt_expire_ts = get_stmt_expire_ts(plan_ctx, *session);
share::ObLSID first_ls_id;
bool local_single_ls_plan = false;
bool is_single_tablet = false;
const bool local_single_ls_plan_maybe = plan->is_local_plan() &&
OB_PHY_PLAN_LOCAL == plan->get_location_type();
if (local_single_ls_plan_maybe) {
if (OB_FAIL(get_first_lsid(das_ctx, first_ls_id, is_single_tablet))) {
} else if (!first_ls_id.is_valid()) {
// do nothing
// get_ls_read_snapshot may degenerate into get_gts, so it can be used even if the ls is not local.
// This is mainly to solve the problem of strong reading performance in some single-tablet scenarios.
} else if (OB_FAIL(txs->get_ls_read_snapshot(tx_desc,
session->get_tx_isolation(),
first_ls_id,
stmt_expire_ts,
snapshot))) {
} else if (is_single_tablet && snapshot.snapshot_ls_role_ != ObRole::FOLLOWER) {
// performance for single tablet scenario
local_single_ls_plan = true;
} else {
local_single_ls_plan = has_same_lsid(das_ctx, snapshot, first_ls_id);
}
}
// per-opt: set read elr for DML stmt
if (OB_SUCC(ret) && local_single_ls_plan && !plan->is_plain_select() && txs->get_tx_elr_util().is_can_tenant_elr()) {
snapshot.try_set_read_elr();
}
if (OB_SUCC(ret) && !local_single_ls_plan) {
ret = txs->get_read_snapshot(tx_desc,
session->get_tx_isolation(),
stmt_expire_ts,
snapshot);
}
if (OB_FAIL(ret)) {
LOG_WARN("fail to get snapshot", K(ret), K(local_single_ls_plan), K(first_ls_id), KPC(session));
}
}
return ret;
}
int ObSqlTransControl::stmt_refresh_snapshot(ObExecContext &exec_ctx) {
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = GET_MY_SESSION(exec_ctx);
ObDASCtx &das_ctx = DAS_CTX(exec_ctx);
ObPhysicalPlanCtx *plan_ctx = GET_PHY_PLAN_CTX(exec_ctx);
const ObPhysicalPlan *plan = plan_ctx->get_phy_plan();
ObTransService *txs = NULL;
if (sql::stmt::T_INSERT == plan->get_stmt_type()) {
//NOTE: oracle insert and insert all stmt can't see the evaluated results of before stmt trigger, no need to refresh snapshot
} else if (OB_FAIL(get_tx_service(session, txs))) {
LOG_WARN("failed to get transaction service", K(ret));
} else if (OB_FAIL(stmt_setup_snapshot_(session, das_ctx, plan, plan_ctx, txs, exec_ctx))) {
LOG_WARN("failed to set snapshot", K(ret));
}
return ret;
}
int ObSqlTransControl::set_fk_check_snapshot(ObExecContext &exec_ctx)
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = GET_MY_SESSION(exec_ctx);
ObDASCtx &das_ctx = DAS_CTX(exec_ctx);
ObPhysicalPlanCtx *plan_ctx = GET_PHY_PLAN_CTX(exec_ctx);
const ObPhysicalPlan *plan = plan_ctx->get_phy_plan();
// insert stmt does not set snapshot by default, set snapshopt for foreign key check induced by insert heres
if (plan->is_plain_insert()) {
ObTransService *txs = NULL;
ObTxReadSnapshot &snapshot = das_ctx.get_snapshot();
ObTxDesc &tx_desc = *session->get_tx_desc();
int64_t stmt_expire_ts = get_stmt_expire_ts(plan_ctx, *session);
share::ObLSID local_ls_id;
bool local_single_ls_plan = plan->is_local_plan()
&& OB_PHY_PLAN_LOCAL == plan->get_location_type()
&& has_same_lsid(das_ctx, snapshot, local_ls_id);
if (OB_FAIL(get_tx_service(session, txs))) {
LOG_WARN("failed to get transaction service", K(ret));
} else {
if (local_single_ls_plan) {
ret = txs->get_ls_read_snapshot(tx_desc,
session->get_tx_isolation(),
local_ls_id,
stmt_expire_ts,
snapshot);
} else {
ret = txs->get_read_snapshot(tx_desc,
session->get_tx_isolation(),
stmt_expire_ts,
snapshot);
}
if (OB_FAIL(ret)) {
LOG_WARN("fail to get snapshot", K(ret), K(local_ls_id), KPC(session));
}
}
}
return ret;
}
int ObSqlTransControl::can_do_plain_insert(ObSQLSessionInfo *session,
const ObPhysicalPlan *plan,
ObExecContext &exec_ctx,
bool &can_plain_insert)
{
int ret = OB_SUCCESS;
can_plain_insert = false;
int last_query_retry_err = session->get_retry_info().get_last_query_retry_err();
if (OB_ISNULL(session) || OB_ISNULL(plan)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected null ptr", K(ret), KPC(session), KPC(plan));
} else if (plan->get_need_serial_exec()
|| ObSQLUtils::is_nested_sql(&exec_ctx)
|| last_query_retry_err == OB_TRANSACTION_SET_VIOLATION
|| session->get_tx_isolation() != ObTxIsolationLevel::RC) {
LOG_TRACE("can't support plain insert", K(plan->get_need_serial_exec()), K(last_query_retry_err),
K(ObSQLUtils::is_nested_sql(&exec_ctx)), K(session->get_tx_isolation()));
} else if (plan->is_plain_insert()) {
can_plain_insert = true;
} else if (session->enable_insertup_replace_gts_opt() && plan->get_insertup_can_do_gts_opt()) {
if (session->is_user_session()) {
can_plain_insert = true;
}
}
if (OB_SUCC(ret)) {
if (plan->get_insertup_can_do_gts_opt()) {
LOG_TRACE("whether can do_batch_insert", K(can_plain_insert), K(plan->is_plain_insert()),
K(last_query_retry_err), K(plan->get_insertup_can_do_gts_opt()),
K(plan->get_need_serial_exec()), K(session->is_user_session()),
K(session->get_tx_isolation()), K(ObSQLUtils::is_nested_sql(&exec_ctx)),
K(session->enable_insertup_replace_gts_opt()));
}
}
return ret;
}
int ObSqlTransControl::get_read_snapshot(ObSQLSessionInfo *session,
ObPhysicalPlanCtx *plan_ctx,
transaction::ObTxReadSnapshot &snapshot)
{
int ret = OB_SUCCESS;
ObTxIsolationLevel isolation = session->get_tx_isolation();
const ObPhysicalPlan *plan = plan_ctx->get_phy_plan();
int64_t expire_ts = get_stmt_expire_ts(plan_ctx, *session);
transaction::ObTransService *txs = NULL;
transaction::ObTxDesc &tx_desc = *session->get_tx_desc();
if (OB_FAIL(get_tx_service(session, txs))) {
LOG_WARN("failed to get transaction service", K(ret));
} else if (OB_FAIL(txs->get_read_snapshot(tx_desc, isolation, expire_ts, snapshot))) {
LOG_WARN("failed to set snapshot", K(ret));
} else if (!snapshot.is_valid()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected invalid snapshot", K(ret), K(tx_desc), K(isolation));
}
return ret;
}
int ObSqlTransControl::get_ls_read_snapshot(ObSQLSessionInfo *session,
ObPhysicalPlanCtx *plan_ctx,
const share::ObLSID &local_ls_id,
transaction::ObTxReadSnapshot &snapshot)
{
int ret = OB_SUCCESS;
ObTxIsolationLevel isolation = session->get_tx_isolation();
const ObPhysicalPlan *plan = plan_ctx->get_phy_plan();
int64_t expire_ts = get_stmt_expire_ts(plan_ctx, *session);
transaction::ObTransService *txs = NULL;
transaction::ObTxDesc &tx_desc = *session->get_tx_desc();
if (OB_FAIL(get_tx_service(session, txs))) {
LOG_WARN("failed to get transaction service", K(ret));
} else if (OB_FAIL(txs->get_ls_read_snapshot(tx_desc, isolation, local_ls_id, expire_ts, snapshot))) {
LOG_WARN("failed to set snapshot", K(ret));
} else if (!snapshot.is_valid()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected invalid snapshot", K(ret), K(tx_desc), K(isolation), K(local_ls_id));
}
return ret;
}
int ObSqlTransControl::stmt_setup_savepoint_(ObSQLSessionInfo *session,
ObDASCtx &das_ctx,
ObPhysicalPlanCtx *plan_ctx,
ObTransService* txs,
const int64_t nested_level)
{
int ret = OB_SUCCESS;
ObTxParam &tx_param = plan_ctx->get_trans_param();
OZ (build_tx_param_(session, tx_param));
ObTxDesc &tx = *session->get_tx_desc();
ObTxSEQ savepoint;
OZ (txs->create_implicit_savepoint(tx, tx_param, savepoint, nested_level == 0), tx, tx_param);
OX (das_ctx.set_savepoint(savepoint));
return ret;
}
#define CHECK_DBLINK_DEFAULT_SAVEPOINTNAME_ALLOWED(sp_name) \
if (OB_SUCC(ret) && (sp_name == DBLINK_DEFAULT_SAVEPOINT || sp_name == PL_DBLINK_DEFAULT_SAVEPOINT)) { \
ret = OB_ERR_INVALID_CHARACTER_STRING; \
LOG_WARN("this savepoint name is not allowed", K(ret), K(sp_name)); \
}
int ObSqlTransControl::create_savepoint(ObExecContext &exec_ctx,
const ObString &sp_name,
const bool user_create)
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = GET_MY_SESSION(exec_ctx);
ObTransService *txs = NULL;
CK (OB_NOT_NULL(session));
CHECK_SESSION (session);
OZ (get_tx_service(session, txs));
OZ (acquire_tx_if_need_(txs, *session));
// for dblink trans
// create savepoint before start stmt to avoid start stmt repeatly
if (OB_SUCC(ret) && session->is_in_transaction()) {
ObTxDesc *&tx_desc = session->get_tx_desc();
if (OB_ISNULL(tx_desc)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected error", K(sp_name), KP(tx_desc));
} else {
// check in dblink trans
const transaction::ObXATransID xid = session->get_xid();
const transaction::ObGlobalTxType global_tx_type = tx_desc->get_global_tx_type(xid);
if (global_tx_type == transaction::ObGlobalTxType::DBLINK_TRANS) {
// only check savepoint name in dblink trans
CHECK_DBLINK_DEFAULT_SAVEPOINTNAME_ALLOWED(sp_name);
OZ (ObTMService::tm_create_savepoint(exec_ctx, sp_name));
}
}
}
bool start_hook = false;
OZ(start_hook_if_need_(*session, txs, start_hook));
OZ (txs->create_explicit_savepoint(*session->get_tx_desc(), sp_name, get_real_session_id(*session), user_create), sp_name);
if (start_hook) {
int tmp_ret = txs->sql_stmt_end_hook(session->get_xid(), *session->get_tx_desc());
if (OB_SUCCESS != tmp_ret) {
LOG_WARN("call sql stmt end hook fail", K(tmp_ret));
ret = COVER_SUCC(tmp_ret);
}
}
if (user_create) {
OX(session->get_raw_audit_record().seq_num_ = ObSequence::get_max_seq_no());
}
return ret;
}
uint32_t ObSqlTransControl::get_real_session_id(ObSQLSessionInfo &session)
{
return session.get_xid().empty() ? 0 : (session.get_proxy_sessid() != 0 ? session.get_proxy_sessid() : session.get_server_sid());
}
int ObSqlTransControl::get_first_lsid(const ObDASCtx &das_ctx, share::ObLSID &first_lsid, bool &is_single_tablet)
{
int ret = OB_SUCCESS;
const DASTableLocList &table_locs = das_ctx.get_table_loc_list();
if (!table_locs.empty()) {
const ObDASTableLoc *first_table_loc = table_locs.get_first();
const DASTabletLocList &tablet_locs = first_table_loc->get_tablet_locs();
if (!tablet_locs.empty()) {
const ObDASTabletLoc *tablet_loc = tablet_locs.get_first();
first_lsid = tablet_loc->ls_id_;
}
is_single_tablet = (1 == table_locs.size() && 1 == tablet_locs.size());
}
return ret;
}
bool ObSqlTransControl::has_same_lsid(const ObDASCtx &das_ctx,
const ObTxReadSnapshot &snapshot,
share::ObLSID &first_lsid)
{
int ret = OB_SUCCESS;
bool bret = true;
ObLSHandle ls_handle;
const share::SCN snapshot_version = snapshot.core_.version_;
const DASTableLocList &table_locs = das_ctx.get_table_loc_list();
FOREACH_X(table_node, table_locs, bret) {
ObDASTableLoc *table_loc = *table_node;
for (DASTabletLocListIter tablet_node = table_loc->tablet_locs_begin();
bret && tablet_node != table_loc->tablet_locs_end(); ++tablet_node) {
ObDASTabletLoc *tablet_loc = *tablet_node;
const ObTabletID tablet_id = tablet_loc->tablet_id_;
if (first_lsid != tablet_loc->ls_id_) {
bret = false;
}
if (bret && !ls_handle.is_valid()) {
ObLSService *ls_svr = NULL;
if (OB_ISNULL(ls_svr = MTL(ObLSService *))) {
bret = false;