forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tran.c
2711 lines (2235 loc) · 84.7 KB
/
tran.c
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 2015 Bloomberg Finance L.P.
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.
*/
/*
* Transaction code. 1172 1325 1372 1440 1581 1659 1698
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stddef.h>
#include <build/db.h>
#include <epochlib.h>
#include <lockmacros.h>
#include <ctrace.h>
#include <net.h>
#include "bdb_cursor.h"
#include "bdb_int.h"
#include "locks.h"
#include "locks_wrap.h"
#include "bdb_osqltrn.h"
#include "bdb_osqlcur.h"
#include "llog_auto.h"
#include "llog_ext.h"
#include "missing.h"
#include <alloca.h>
#include <assert.h>
#include "nodemap.h"
#include "logmsg.h"
#include "txn_properties.h"
#include <build/db.h>
static unsigned int curtran_counter = 0;
extern int gbl_debug_txn_sleep;
extern int __txn_getpriority(DB_TXN *txnp, int *priority);
#if 0
int __lock_dump_region_lockerid __P((DB_ENV *, const char *, FILE *, u_int32_t lockerid));
#endif
int bdb_tran_clear_request_ack(tran_type *tran)
{
tran->request_ack = 0;
return 0;
}
int bdb_tran_set_request_ack(tran_type *tran)
{
tran->request_ack = 1;
return 0;
}
tran_type *bdb_tran_begin_logical_norowlocks_int(bdb_state_type *bdb_state,
unsigned long long tranid,
int trak, int *bdberr)
{
tran_type *tran;
int rc;
unsigned int flags = 0;
/* One day this will change. One day.
We really want a bdb_env_type and a bdb_table_type.
bdb_state_type is making me feel all stabby. */
if (bdb_state->parent)
bdb_state = bdb_state->parent;
*bdberr = BDBERR_NOERROR;
tran = mymalloc(sizeof(tran_type));
bzero(tran, sizeof(tran_type));
tran->tranclass = TRANCLASS_LOGICAL_NOROWLOCKS;
tran->threadid = pthread_self();
tran->wrote_begin_record = 0;
tran->committed_begin_record = 0;
tran->get_schema_lock = 0;
if (tranid == 0)
tran->logical_tranid = get_id(bdb_state);
else {
tran->logical_tranid = tranid;
}
/* LSN_NOT_LOGGED */
tran->last_logical_lsn.file = 0;
tran->last_logical_lsn.offset = 1;
if (!bdb_state->attr->synctransactions)
flags |= DB_TXN_NOSYNC;
rc = bdb_state->dbenv->txn_begin(bdb_state->dbenv, NULL, &tran->tid, flags);
if (rc) {
logmsg(LOGMSG_ERROR, "can't start locker transaction, tid %016llx\n",
tran->logical_tranid);
/* we can retry this from the top level if need be */
*bdberr = BDBERR_DEADLOCK;
myfree(tran);
return NULL;
}
return tran;
}
extern int gbl_update_startlsn_printstep;
int maxstep = 0;
void comdb2_cheapstack(FILE *f);
/* Update the startlsn of an outstanding logical transaction - you are holding
* the lock*/
int bdb_update_startlsn_lk(bdb_state_type *bdb_state, struct tran_tag *intran,
DB_LSN *firstlsn)
{
static int lastpr = 0;
int countstep = 0, now;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
struct tran_tag *ltran = NULL, *tmp = NULL;
int inserted = 0;
/* Remove from the transactions list */
listc_rfl(&bdb_state->logical_transactions_list, intran);
/* Update the startlsn */
intran->startlsn = *firstlsn;
/* Add this in startlsn order. This is cheap: in the non-recovery case
* we're guaranteed that these will be added in order, so they'll break
* out of this loop after the first comparison. */
LISTC_FOR_EACH_SAFE(&bdb_state->logical_transactions_list, ltran, tmp,
tranlist_lnk)
{
countstep++;
if (log_compare(&intran->startlsn, <ran->startlsn) <= 0 ||
ltran->startlsn.file == 0) {
listc_add_before(&bdb_state->logical_transactions_list, intran,
ltran);
inserted = 1;
break;
}
}
if (gbl_update_startlsn_printstep) {
if (countstep > maxstep) {
maxstep = countstep;
if ((now = comdb2_time_epoch()) - lastpr) {
logmsg(LOGMSG_USER, "Maxstep was %d\n", maxstep);
comdb2_cheapstack(stderr);
maxstep = 0;
lastpr = now;
}
}
}
/* Make this the last element */
if (!inserted) {
listc_abl(&bdb_state->logical_transactions_list, intran);
}
/* Grab top lsn */
DB_LSN *top = &bdb_state->logical_transactions_list.top->startlsn;
/* Assert that this didn't go backwards */
if (bdb_state->lwm.file && 0 == bdb_state->in_recovery) {
/* This can happen on the replicant if the commit records are processed
* out of order. I don't think this matters .. */
/*
assert(log_compare(top, &bdb_state->lwm) >= 0);
*/
}
/* Update lwm while under lock. */
if (top->file) {
bdb_state->lwm.file = top->file;
bdb_state->lwm.offset = top->offset;
}
return 0;
}
/* This allows the scdone 'rep-transaction' to gather locks using gbl_rep_lockid
*/
void bdb_set_tran_lockerid(tran_type *tran, uint32_t lockerid)
{
if (tran->tranclass == TRANCLASS_LOGICAL) {
tran->logical_lid = lockerid;
} else {
tran->tid->txnid = lockerid;
}
}
void bdb_get_tran_lockerid(tran_type *tran, uint32_t *lockerid)
{
if (tran->tranclass == TRANCLASS_LOGICAL) {
(*lockerid) = tran->logical_lid;
} else {
(*lockerid) = tran->tid->txnid;
}
}
void *bdb_get_physical_tran(tran_type *ltran)
{
assert(ltran->tranclass == TRANCLASS_LOGICAL);
return ltran->physical_tran;
}
void bdb_reset_physical_tran(tran_type *ltran)
{
assert(ltran->tranclass == TRANCLASS_LOGICAL);
ltran->physical_tran = NULL;
}
void *bdb_get_sc_parent_tran(tran_type *ltran)
{
assert(ltran->tranclass == TRANCLASS_LOGICAL);
return ltran->sc_parent_tran;
}
void bdb_ltran_get_schema_lock(tran_type *ltran)
{
ltran->get_schema_lock = 1;
ltran->single_physical_transaction = 1;
ltran->schema_change_txn = 1;
}
void bdb_ltran_put_schema_lock(tran_type *ltran)
{
ltran->get_schema_lock = 0;
}
/* Create a txn and add to the txn list. Called directly from the replication
* stream. */
static tran_type *bdb_start_ltran(bdb_state_type *bdb_state,
unsigned long long ltranid, void *firstlsn)
{
tran_type *tran;
int bdberr = 0;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
#if DEBUG_TXN_LIST
LOCK(&bdb_state->translist_lk)
{
/* Should not be in the hash */
assert(NULL ==
hash_find(bdb_state->logical_transactions_hash, <ranid));
}
UNLOCK(&bdb_state->translist_lk);
#endif
tran = bdb_tran_start_logical(bdb_state, ltranid, 0, &bdberr);
if (firstlsn)
bdb_update_startlsn(tran, firstlsn);
return tran;
}
/* Logical start function to register with berkley replication */
int berkdb_start_logical(DB_ENV *dbenv, void *state, uint64_t ltranid,
DB_LSN *lsn)
{
tran_type *txn;
bdb_state_type *bdb_state;
bdb_state = (bdb_state_type *)state;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
#ifdef DEBUG_TXN_LIST
LOCK(&bdb_state->translist_lk)
{
txn = hash_find(bdb_state->logical_transactions_hash, <ranid);
}
UNLOCK(&bdb_state->translist_lk);
assert(!txn);
#endif
txn = bdb_start_ltran(bdb_state, ltranid, lsn);
return txn ? 0 : -1;
}
int logical_commit_replicant(bdb_state_type *bdb_state,
unsigned long long ltranid);
/* Logical commit function to register with berkley replication */
int berkdb_commit_logical(DB_ENV *dbenv, void *state, uint64_t ltranid,
DB_LSN *lsn)
{
int rc;
bdb_state_type *bdb_state;
bdb_state = (bdb_state_type *)state;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
#ifdef DEBUG_TXN_LIST
tran_type *txn;
LOCK(&bdb_state->translist_lk)
{
txn = hash_find(bdb_state->logical_transactions_hash, <ranid);
}
UNLOCK(&bdb_state->translist_lk);
assert(txn);
#endif
rc = logical_commit_replicant(bdb_state, ltranid);
return rc;
}
/* Release all of the locks held by this logical transaction */
int bdb_release_ltran_locks(bdb_state_type *bdb_state, struct tran_tag *ltran,
int lockerid)
{
DB_LOCKREQ rq = {0};
int rc;
if (!lockerid) {
assert(bdb_state->in_recovery);
return 0;
}
/* Release all rowlocks */
rq.op = DB_LOCK_PUT_ALL;
/* Release all the locks */
rc =
bdb_state->dbenv->lock_vec(bdb_state->dbenv, lockerid, 0, &rq, 1, NULL);
/* Shouldn't fail */
assert(0 == rc);
if (rc)
logmsg(LOGMSG_WARN, "%s:%d rc = %d\n", __func__, __LINE__, rc);
/* Make sure this is NULL */
assert(NULL == ltran->tid);
/* Release my locker id */
rc = bdb_state->dbenv->lock_id_free(bdb_state->dbenv, lockerid);
/* Shouldn't fail */
assert(0 == rc);
if (rc)
logmsg(LOGMSG_WARN, "%s:%d rc = %d\n", __func__, __LINE__, rc);
/* Invalid lockerid */
ltran->logical_lid = 0;
return 0;
}
/* Aborts anything waiting on locks held by the logical transactions. Used
* before blocking on the bdb writelock so that the replication thread doesn't
* deadlock on a reader which is blocked on a lock that it holds. */
int bdb_abort_logical_waiters(bdb_state_type *bdb_state)
{
struct tran_tag *ltran = NULL, *tmp = NULL;
int rc;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
LOCK(&bdb_state->translist_lk)
{
LISTC_FOR_EACH_SAFE(&bdb_state->logical_transactions_list, ltran, tmp,
tranlist_lnk)
{
if (ltran->logical_lid) {
rc = bdb_state->dbenv->lock_abort_waiters(bdb_state->dbenv, ltran->logical_lid, DB_LOCK_ABORT_LOGICAL);
if (rc)
abort();
}
}
}
UNLOCK(&bdb_state->translist_lk);
return 0;
}
/* Purge the logical txn list of any startlsn's equal to or greater than
* trunclsn.
* This is for the rep_verify truncate case. */
int bdb_purge_logical_transactions(void *statearg, DB_LSN *trunclsn)
{
/* Release rowlocks for each aborted logical transaction. The pages
* should be rolled back already. */
bdb_state_type *bdb_state = (bdb_state_type *)statearg;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
LOCK(&bdb_state->translist_lk)
{
struct tran_tag *ltran = NULL, *tmp = NULL;
LISTC_FOR_EACH_SAFE(&bdb_state->logical_transactions_list, ltran, tmp,
tranlist_lnk)
{
if (ltran->startlsn.file > 0 &&
log_compare(trunclsn, <ran->startlsn) < 0) {
/* Remove from hash */
hash_del(bdb_state->logical_transactions_hash, ltran);
/* Remove from list */
listc_rfl(&bdb_state->logical_transactions_list, ltran);
pool_free(ltran->rc_pool);
myfree(ltran->rc_list);
myfree(ltran->rc_locks);
#if DEBUG_TXN_LIST
ctrace("Removing logical_tranid 0x%llx func %s line %d - "
"ltran->startlsn is %d:%d, trunclsn is %d:%d\n",
ltran->logical_tranid, __func__, __LINE__,
ltran->startlsn.file, ltran->startlsn.offset,
trunclsn->file, trunclsn->offset);
#endif
/* Release locks */
bdb_release_ltran_locks(bdb_state, ltran, ltran->logical_lid);
/* Free this */
free(ltran);
}
}
/* Don't have to touch the lwm: if it was removed the list is empty */
}
UNLOCK(&bdb_state->translist_lk);
return 0;
}
void print_logical_commits_starts(FILE *f);
int bdb_dump_logical_tranlist(void *state, FILE *f)
{
bdb_state_type *bdb_state = (bdb_state_type *)state;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
LOCK(&bdb_state->translist_lk)
{
struct tran_tag *ltran = NULL, *tmp = NULL;
LISTC_FOR_EACH_SAFE(&bdb_state->logical_transactions_list, ltran, tmp,
tranlist_lnk)
{
logmsg(LOGMSG_USER, "ltranid: %llx startlsn: %d:%d lllsn: %d:%d\n",
ltran->logical_tranid, ltran->startlsn.file,
ltran->startlsn.offset, ltran->last_logical_lsn.file,
ltran->last_logical_lsn.offset);
}
}
UNLOCK(&bdb_state->translist_lk);
print_logical_commits_starts(f);
return 0;
}
/* Update the startlsn from berkeley */
int bdb_update_startlwm_berk(void *statearg, unsigned long long ltranid,
DB_LSN *firstlsn)
{
bdb_state_type *bdb_state = (bdb_state_type *)statearg;
tran_type *ltran;
int rc;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
LOCK(&bdb_state->translist_lk)
{
/* Find it in my hash */
ltran = hash_find(bdb_state->logical_transactions_hash, <ranid);
/* Should always find this */
assert(ltran);
/* Now reorder */
rc = bdb_update_startlsn_lk(bdb_state, ltran, firstlsn);
}
UNLOCK(&bdb_state->translist_lk);
return rc;
}
/* Made to be called in berkeley land */
int bdb_update_startlsn(struct tran_tag *intran, DB_LSN *firstlsn)
{
bdb_state_type *bdb_state = intran->parent_state;
int rc;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
LOCK(&bdb_state->translist_lk)
{
rc = bdb_update_startlsn_lk(bdb_state, intran, firstlsn);
}
UNLOCK(&bdb_state->translist_lk);
return rc;
}
int tran_reset_rowlist(tran_type *tran)
{
pool_relall(tran->rc_pool);
tran->rc_count = 0;
return (0);
}
/* We don't need the top 'N' locks that we've just allocated */
int tran_deallocate_pop(tran_type *tran, int count)
{
assert(tran->rc_count >= count);
while (count--) {
pool_relablk(tran->rc_pool, tran->rc_list[--tran->rc_count]);
}
return (0);
}
int tran_allocate_rlptr(tran_type *tran, DBT **rlptr, DB_LOCK **rlock)
{
int ret = 0, step = tran->parent_state->attr->rllist_step;
step = step > 0 ? step : 10;
if (tran->rc_count >= tran->rc_max) {
void *new_rc_list, *new_rc_locks;
new_rc_list =
myrealloc(tran->rc_list, sizeof(DBT *) * (tran->rc_max + step));
if (new_rc_list == NULL) {
logmsg(LOGMSG_FATAL, "%s line %d out of memory.\n", __func__, __LINE__);
abort();
}
new_rc_locks =
myrealloc(tran->rc_locks, sizeof(DB_LOCK) * (tran->rc_max + step));
if (new_rc_locks == NULL) {
logmsg(LOGMSG_FATAL, "%s line %d out of memory.\n", __func__, __LINE__);
abort();
}
tran->rc_list = new_rc_list;
tran->rc_locks = new_rc_locks;
tran->rc_max += step;
}
if (!(tran->rc_list[tran->rc_count] = pool_getablk(tran->rc_pool))) {
logmsg(LOGMSG_FATAL, "%s line %d out of memory.\n", __func__, __LINE__);
abort();
}
tran->rc_list[tran->rc_count]->data =
((u_int8_t *)tran->rc_list[tran->rc_count]) + sizeof(DBT);
tran->rc_list[tran->rc_count]->size = 0;
(*rlock) = &tran->rc_locks[tran->rc_count];
(*rlptr) = tran->rc_list[tran->rc_count++];
return (ret);
}
static tran_type *bdb_tran_begin_logical_int(bdb_state_type *bdb_state,
unsigned long long logical_tranid,
int trak, int got_bdb_lock,
int reptxn, uint32_t logical_lid,
int *bdberr)
{
tran_type *tran;
int step;
int ismaster;
/* One day this will change. One day.
We really want a bdb_env_type and a bdb_table_type.
bdb_state_type is making me feel all stabby. */
if (bdb_state->parent)
bdb_state = bdb_state->parent;
*bdberr = BDBERR_NOERROR;
tran = mymalloc(sizeof(tran_type));
bzero(tran, sizeof(tran_type));
tran->tranclass = TRANCLASS_LOGICAL;
tran->threadid = pthread_self();
tran->wrote_begin_record = 0;
tran->committed_begin_record = 0;
tran->get_schema_lock = 0;
tran->is_about_to_commit = 0;
tran->micro_commit =
gbl_rowlocks ? bdb_state->attr->rowlocks_micro_commit : 0;
tran->logical_tranid = logical_tranid;
/* LSN_NOT_LOGGED */
tran->last_logical_lsn.file = 0;
tran->last_logical_lsn.offset = 1;
tran->last_physical_commit_lsn.file = 0;
tran->last_physical_commit_lsn.offset = 1;
tran->last_regop_lsn.file = 0;
tran->last_regop_lsn.offset = 1;
/* Start at 0 */
tran->startlsn.file = 0;
tran->startlsn.offset = 1;
tran->reptxn = reptxn;
tran->logical_lid = logical_lid; /* 0 for replication */
tran->is_rowlocks_trans = 1;
tran->is_about_to_commit = 0;
tran->master = 1; /* logical transactions don't have subtransactions */
tran->trak = trak;
tran->last_logical_lsn.file = 0;
tran->last_logical_lsn.offset = 1;
tran->parent_state = bdb_state;
/* Put this tran in seqnum_info->key */
Pthread_setspecific(bdb_state->seqnum_info->key, tran);
tran->got_bdb_lock = got_bdb_lock;
ismaster = (bdb_state->repinfo->myhost == bdb_state->repinfo->master_host);
if (ismaster) {
step = bdb_state->attr->rllist_step > 0 ? bdb_state->attr->rllist_step
: 10;
tran->rc_pool = pool_setalloc_init(sizeof(DBT) + MINMAX_KEY_SIZE, step,
malloc, free);
tran->rc_list = mymalloc(sizeof(DBT *) * step);
tran->rc_locks = mymalloc(sizeof(DB_LOCK) * step);
tran->rc_max = step;
tran->rc_count = 0;
}
/* Unset startlsn's are added to the back of this list */
LOCK(&bdb_state->translist_lk)
{
listc_abl(&bdb_state->logical_transactions_list, tran);
hash_add(bdb_state->logical_transactions_hash, tran);
#if defined DEBUG_TXN_LIST
ctrace("Adding logical_tranid 0x%llx func %s line %d\n",
tran->logical_tranid, __func__, __LINE__);
#endif
}
UNLOCK(&bdb_state->translist_lk);
if (tran->trak)
logmsg(LOGMSG_USER, "TRK_TRAN: began transaction %p (logical, class=%d)\n",
tran, tran->tranclass);
return tran;
}
#define GET_TRANID(tranid) (tranid) ? (tranid) : get_id(bdb_state)
extern int gbl_extended_sql_debug_trace;
int bdb_tran_get_start_file_offset(bdb_state_type *bdb_state, tran_type *tran,
int *file, int *offset)
{
if (gbl_new_snapisol_asof) {
if (tran && tran->asof_lsn.file) {
*file = tran->asof_lsn.file;
*offset = tran->asof_lsn.offset;
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "%s line %d using asof lsn [%d][%d]\n",
__func__, __LINE__, *file, *offset);
}
return 0;
} else if (tran && tran->birth_lsn.file) {
*file = tran->birth_lsn.file;
*offset = tran->birth_lsn.offset;
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "%s line %d using birth lsn [%d][%d]\n",
__func__, __LINE__, *file, *offset);
}
return 0;
} else
return -1;
} else {
if (tran && tran->snapy_commit_lsn.file) {
*file = tran->snapy_commit_lsn.file;
*offset = tran->snapy_commit_lsn.offset;
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER,
"%s line %d using snappy-commit lsn [%d][%d]\n",
__func__, __LINE__, *file, *offset);
}
return 0;
}
else
{
// If we are not in a transaction, return nothing
if (tran) {
bdb_get_current_lsn(bdb_state, (unsigned int *)file,
(unsigned int *)offset);
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "%s line %d using current lsn[%d][%d], tran is %p\n",
__func__, __LINE__, *file, *offset, tran);
}
}
else {
*file = 0;
*offset = 0;
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "%s line %d using ZERO lsn[%d][%d], tran is NULL\n",
__func__, __LINE__, *file, *offset);
}
}
return 0;
}
}
return -1;
}
/* only should be called from phys.c */
tran_type *bdb_tran_begin_phys(bdb_state_type *bdb_state,
tran_type *logical_tran)
{
int rc, flags = 0;
extern int gbl_locks_check_waiters, gbl_rowlocks_commit_on_waiters;
tran_type *tran;
if (logical_tran->tranclass != TRANCLASS_LOGICAL) {
logmsg(LOGMSG_FATAL, "Tried to start a physical transaction with a "
"non-logical parent\n");
abort();
}
if (logical_tran->physical_tran) {
logmsg(LOGMSG_FATAL, "Tried to start a physical transaction with another "
"physical transaction active\n");
abort();
}
tran = mymalloc(sizeof(tran_type));
bzero(tran, sizeof(tran_type));
logical_tran->physical_tran = tran;
tran->tranclass = TRANCLASS_PHYSICAL;
tran->threadid = pthread_self();
tran->logical_tran = logical_tran;
if (!bdb_state->attr->synctransactions)
flags |= DB_TXN_NOSYNC;
rc = bdb_state->dbenv->txn_begin(bdb_state->dbenv, NULL, &tran->tid, flags);
if (rc) {
logmsg(LOGMSG_ERROR,
"Can't start physical transaction for ltranid %016llx: rc %d\n",
logical_tran->logical_tranid, rc);
free(tran);
return NULL;
}
/* Make the logical tran the 'parent' for deadlock detection purposes (but
* this will commit independantly). */
if (!logical_tran->micro_commit ||
(gbl_locks_check_waiters && gbl_rowlocks_commit_on_waiters)) {
rc = bdb_state->dbenv->lock_add_child_locker(
bdb_state->dbenv, logical_tran->logical_lid, tran->tid->txnid);
if (rc) {
logmsg(LOGMSG_ERROR, "Error setting physical child locker for logical txn\n");
tran->tid->abort(tran->tid);
free(tran);
return NULL;
}
}
/* Set the 'logical_abort' flag in the physical transactions lockerid */
/* XXX the set_abort_flag_in_locker check is for debugging. This code
* should always be run */
if (!bdb_state->in_recovery && logical_tran->aborted &&
bdb_state->attr->set_abort_flag_in_locker) {
rc = bdb_state->dbenv->lock_id_set_logical_abort(
bdb_state->dbenv, logical_tran->logical_lid);
if (rc) {
logmsg(LOGMSG_ERROR, "Error setting logical-abort flag in the physical "
"transaction\n");
tran->tid->abort(tran->tid);
free(tran);
return NULL;
}
}
if (tran->trak)
logmsg(LOGMSG_USER, "TRK_TRAN: began transaction %p (physical class=%d) "
"for %p (logical, class=%d)\n",
tran, tran->tranclass, logical_tran, logical_tran->tranclass);
return tran;
}
static inline unsigned long long lag_bytes(bdb_state_type *bdb_state)
{
const char *connlist[REPMAX];
int count, i;
char *master_host = bdb_state->repinfo->master_host;
uint64_t lagbytes;
DB_LSN minlsn, masterlsn;
/* MAX_LSN */
minlsn.file = minlsn.offset = 0xffffffff;
count = net_get_all_nodes_connected(bdb_state->repinfo->netinfo, connlist);
if (count == 0)
return 0;
for (i = 0; i < count; i++) {
if (!is_incoherent(bdb_state, connlist[i]) &&
log_compare(
&bdb_state->seqnum_info->seqnums[nodeix(connlist[i])].lsn,
&minlsn) < 0) {
minlsn = bdb_state->seqnum_info->seqnums[nodeix(connlist[i])].lsn;
}
}
masterlsn = bdb_state->seqnum_info->seqnums[nodeix(master_host)].lsn;
if (log_compare(&minlsn, &masterlsn) >= 0)
return 0;
lagbytes = subtract_lsn(bdb_state, &masterlsn, &minlsn);
return lagbytes;
}
static int bdb_tran_commit_phys_getlsn_flags(bdb_state_type *bdb_state,
tran_type *tran, DB_LSN *inlsn,
int flags)
{
extern int gbl_replicate_rowlocks;
DB_LSN llsn, *lsn = (NULL == inlsn ? &llsn : inlsn);
DBT rldbt = {0};
int rc_count, rc, iirc;
if (tran->logical_tran->physical_tran != tran) {
logmsg(LOGMSG_FATAL, "Transaction mismatch: expected 0x%p got 0x%p\n", tran,
tran->logical_tran->physical_tran);
abort();
}
if (!tran->logical_tran->committed_begin_record)
flags |= DB_TXN_LOGICAL_BEGIN;
if (tran->logical_tran->is_about_to_commit)
flags |= DB_TXN_LOGICAL_COMMIT;
if (tran->logical_tran->get_schema_lock)
flags |= DB_TXN_SCHEMA_LOCK;
tran->logical_tran->committed_begin_record = 1;
tran->logical_tran->physical_tran = NULL;
if (!bdb_state->attr->synctransactions)
flags |= DB_TXN_NOSYNC;
if (!gbl_replicate_rowlocks) {
rldbt.data = NULL;
rldbt.size = 0;
rc_count = 0;
} else {
/* This is only called for a logical commit or abort. Pass in the
* logical
* transaction so that the replication code knows to flush it. */
rldbt.data = tran->logical_tran->rc_list;
rldbt.size = sizeof(DBT *) * tran->logical_tran->rc_count;
rc_count = tran->logical_tran->rc_count;
}
bdb_osql_trn_repo_lock();
iirc = update_shadows_beforecommit(
bdb_state, &tran->logical_tran->last_logical_lsn, NULL, 1);
bdb_osql_trn_repo_unlock();
if (iirc) {
logmsg(LOGMSG_ERROR, "%s:update_shadows_beforecommit returns %d\n", __func__,
iirc);
return -1;
}
/* Update last committed logical lsn */
tran->logical_tran->last_physical_commit_lsn =
tran->logical_tran->last_logical_lsn;
assert(lsn == &tran->logical_tran->last_regop_lsn);
/* A little terrible - it serializes commits on this lock ..
* maybe pass in a flag instead? There's a race here if we
* don't lock: this can attempt to update-pagelogs for a
* shadow-trans that is exiting .. */
/* XXX This doesn't seem to be locking .. XXX */
u_int64_t logbytes = 0;
rc = tran->tid->commit_rowlocks(tran->tid, flags, tran->logical_tran->logical_tranid,
tran->logical_tran->logical_lid, &tran->logical_tran->last_regop_lsn, &rldbt,
tran->logical_tran->rc_locks, rc_count, &logbytes, &tran->logical_tran->begin_lsn,
lsn, tran->logical_tran);
tran_reset_rowlist(tran->logical_tran);
if (lsn->file && flags & DB_TXN_REP_ACK) {
int timeoutms = -1;
seqnum_type seqnum = {{0}};
memcpy(&seqnum.lsn, lsn, sizeof(*lsn));
bdb_state->dbenv->get_rep_gen(bdb_state->dbenv, &seqnum.generation);
bdb_wait_for_seqnum_from_all_adaptive_newcoh(bdb_state, &seqnum, 0,
&timeoutms);
}
tran->logbytes += logbytes;
if (tran->trak)
logmsg(LOGMSG_USER, "TRK_TRAN: committed transaction %p (physical "
"class=%d) for %p (logical, class=%d)\n",
tran, tran->tranclass, tran->logical_tran,
tran->logical_tran->tranclass);
if (tran->table_version_cache)
free(tran->table_version_cache);
tran->table_version_cache = NULL;
free(tran);
return rc;
}
int bdb_tran_commit_phys_getlsn(bdb_state_type *bdb_state, tran_type *tran,
DB_LSN *lsn)
{
return bdb_tran_commit_phys_getlsn_flags(bdb_state, tran, lsn,
DB_TXN_REP_ACK);
}
int bdb_tran_commit_phys(bdb_state_type *bdb_state, tran_type *tran)
{
return bdb_tran_commit_phys_getlsn_flags(
bdb_state, tran, &tran->logical_tran->last_regop_lsn, 0);
}
static int bdb_tran_abort_phys_int(bdb_state_type *bdb_state, tran_type *tran,
int reset_rowlist)
{
int rc;
if (tran->logical_tran->physical_tran != tran) {
logmsg(LOGMSG_FATAL, "Transaction mismatch: expected 0x%p got 0x%p\n", tran,