forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphys.c
1446 lines (1237 loc) · 47 KB
/
phys.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.
*/
/*
phys : physical layer
this file will contain every physical primitive.
each physical primitive is a physical transaction consisting of
3 things: 1) a logical UNDO record. 2) a call to a berk primitive.
3) locks necessary to prevent dirty reads
the logical undo record in a physical transaction must be able to stand
alone, with no context. it must be able to completely undo the physical
transaction. it must also contain enough information for locks to be
aquired based on genid.
More on this below.
Control flow here is confusing, so here's an overview. add.c/upd.c/del.c
routines call ll.c to do some underlying low level operation (like a put
or a del). The ll.c code either does the operation directly, or calls
phys.c to wrap the operation in a new (physical) transaction. So there's
three levels of operations:
Logical - Multiple operations like adding records and their
corresponding keys are done under a logical
transaction.
Physical - A single ("real") operation is wrapped in a
physical transaction. A physical transaction contains
a berkdb operation (see below) wrapped in a transaction
along with a log record that contains enough information
to undo the operation.
Real berkdb) - A real call to berkeley to do something.
So for a transaction with row locks that inserts a record, the call
sequence is:
Description Calls
----------- -----
Begin logical transaction (tran.c)
Add dta (add.c) bdb_prim_allocdta_int
Add a record (ll.c) ll_dta_add
Begin physical transaction (phys.c) phys_dta_add
Add the record (ll.c) ll_dta_add !!!!
(called with different transaction level)
Add log record (custom_recover.c) bdb_llog_add_dta
Commit physical transaction (phys.c)
Add key (add.c) bdb_prim_addk_int
Add key (ll.c) ll_key_add
Begin physical transaction (phys.c) phys_key_add
Add the key (ll.c) ll_key_add (see above)
Add log key (phys.c) bdb_llog_add_ix
Commit physical transaction (phys.c)
Add blob (add.c)
Blobs are records, same case as for record (same code)
Write logical commit record (tran.c)
For a transaction without row locks, the flow is much like before,
with an additional ll layer instead of direct berkeley calls.
It's likely better this way in any case.
Begin real transaction (tran.c)
Add dta (add.c) bdb_prim_allocdta_int
Add a record (ll.c) ll_dta_add
Add the record (ll.c) ll_dta_add (same call)
Add key (ll.c) bdb_prim_addk_int
Add key (ll.c) ll_key_add
Add the key ll_key_add (same call)
*/
#include <errno.h>
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/poll.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 <fsnap.h>
#include <ctrace.h>
#include "flibc.h"
#include "net.h"
#include "bdb_int.h"
#include "locks.h"
#include <dbinc/db_swap.h>
#include "logmsg.h"
/* There are two problems that berkeley solves for us that we need to
re-solve with rowlocks:
1) applications seeing records that are written by transactions
that have not yet committed.
2) applications not seeing records that were deleted by transactions
that have not yet committed.
*/
static int start_physical_transaction(bdb_state_type *bdb_state,
tran_type *logical_tran,
tran_type **outtran)
{
tran_type *physical_tran;
int rc;
physical_tran = bdb_tran_begin_phys(bdb_state, logical_tran);
if (physical_tran == NULL) {
logmsg(LOGMSG_ERROR, "%s:%d begin trans failed\n", __FILE__, __LINE__);
return DB_LOCK_DEADLOCK;
}
if (!logical_tran->committed_begin_record &&
(bdb_state->repinfo->myhost == bdb_state->repinfo->master_host)) {
rc = bdb_llog_start(bdb_state, logical_tran, physical_tran->tid);
if (rc) {
logmsg(LOGMSG_ERROR, "%s:%d begin bdb_llog_start rc %d\n", __FILE__,
__LINE__, rc);
bdb_tran_abort_phys(bdb_state, physical_tran);
return rc;
}
logical_tran->wrote_begin_record = 1;
}
*outtran = physical_tran;
return 0;
}
#include <epochlib.h>
extern int gbl_rowlocks_commit_on_waiters;
extern int gbl_locks_check_waiters;
int get_physical_transaction(bdb_state_type *bdb_state, tran_type *logical_tran,
tran_type **outtran, int force_commit)
{
extern unsigned long long check_waiters_skip_count;
extern unsigned long long check_waiters_commit_count;
int rc = 0;
if (!logical_tran->single_physical_transaction &&
logical_tran->physical_tran) {
int do_commit = 0;
if (force_commit || logical_tran->micro_commit)
do_commit = 1;
else if (gbl_locks_check_waiters && gbl_rowlocks_commit_on_waiters) {
rc = bdb_state->dbenv->lock_id_has_waiters(
bdb_state->dbenv, logical_tran->physical_tran->tid->txnid);
if (rc == 0)
check_waiters_skip_count++;
else if (rc == 1) {
check_waiters_commit_count++;
do_commit = 1;
} else
logmsg(LOGMSG_ERROR, "%s: lock_id_has_waiters returns %d\n",
__func__, rc);
}
if (do_commit) {
bdb_tran_commit_phys(bdb_state, logical_tran->physical_tran);
assert(!logical_tran->physical_tran);
}
}
if (!logical_tran->physical_tran) {
rc = start_physical_transaction(bdb_state, logical_tran, outtran);
if (rc != 0) {
int ismaster =
(bdb_state->repinfo->myhost == bdb_state->repinfo->master_host);
if (!ismaster && !bdb_state->in_recovery) {
logmsg(LOGMSG_ERROR,
"Master change while getting physical tran.\n");
return BDBERR_READONLY;
}
return rc;
}
if (logical_tran->single_physical_transaction &&
logical_tran->schema_change_txn && gbl_rowlocks) {
int bdberr = 0;
logical_tran->sc_parent_tran = logical_tran->physical_tran;
logical_tran->physical_tran = bdb_tran_begin(
bdb_state, logical_tran->sc_parent_tran, &bdberr);
logical_tran->physical_tran->logical_tran = logical_tran;
logical_tran->physical_tran->tranclass = TRANCLASS_PHYSICAL;
if (logical_tran->physical_tran == NULL) {
logmsg(LOGMSG_ERROR,
"%s:%d failed to start child tran for sc, bdberr=%d\n",
__func__, __LINE__, bdberr);
return -1;
}
}
}
*outtran = logical_tran->physical_tran;
return 0;
}
static inline int micro_retry_check(bdb_state_type *bdb_state, tran_type *tran)
{
extern int gbl_micro_retry_on_deadlock;
if (gbl_rowlocks && gbl_micro_retry_on_deadlock && tran->micro_commit &&
!tran->single_physical_transaction &&
!(gbl_locks_check_waiters && gbl_rowlocks_commit_on_waiters))
return 1;
else
return 0;
}
extern unsigned long long gbl_rowlocks_deadlock_retries;
static inline int is_deadlock(int rc)
{
switch (rc) {
case BDBERR_DEADLOCK_ROWLOCK:
case BDBERR_DEADLOCK:
case DB_LOCK_DEADLOCK:
return 1;
break;
default:
return 0;
break;
}
}
static inline void deadlock_trace(const char *func, tran_type *tran, int rc)
{
extern int gbl_rowlocks_deadlock_trace;
if (is_deadlock(rc) && gbl_rowlocks_deadlock_trace) {
logmsg(LOGMSG_ERROR, "ltranid %llu %s returning deadlock %d\n",
tran->logical_tranid, func, rc);
}
}
int phys_dta_add(bdb_state_type *bdb_state, tran_type *logical_tran,
unsigned long long genid, DB *dbp, int dtafile, int dtastripe,
DBT *dbt_key, DBT *dbt_data, int odhready)
{
int rc, micro_retry, retry = 0;
int retry_count = bdb_state->attr->pagedeadlock_retries;
int max_poll = bdb_state->attr->pagedeadlock_maxpoll;
tran_type *physical_tran = NULL;
DBT *addlkptr = NULL;
DB_LOCK *addrowlk = NULL;
DB_LSN last_regop_lsn = logical_tran->last_regop_lsn;
/* Start transaction */
rc = get_physical_transaction(bdb_state, logical_tran, &physical_tran, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n", __func__,
rc);
goto done;
}
if (gbl_rowlocks && dtafile == 0) {
int did_commit = 0;
rc = tran_allocate_rlptr(logical_tran, &addlkptr, &addrowlk);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to allocate rlptr rc %d\n",
__func__, rc);
goto done;
}
if (log_compare(&last_regop_lsn, &logical_tran->last_regop_lsn))
did_commit = 1;
rc = bdb_lock_row_write_getlock(bdb_state, logical_tran, -1, genid,
addrowlk, addlkptr, !did_commit);
if (rc == DB_LOCK_NOTGRANTED && !did_commit) {
/* trylock failed, let's commit here and wait again */
tran_deallocate_pop(logical_tran, 1);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 1);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n",
__func__, rc);
goto done;
}
tran_allocate_rlptr(logical_tran, &addlkptr, &addrowlk);
rc = bdb_lock_row_write_getlock(bdb_state, logical_tran, -1, genid,
addrowlk, addlkptr, 0);
}
if (rc) {
logmsg(LOGMSG_ERROR,
"%s failed to lock row write genid %llx rc %d\n", __func__,
genid, rc);
goto done;
}
}
micro_retry = micro_retry_check(bdb_state, logical_tran);
do {
if (gbl_rowlocks) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)
goto done;
}
/* Insert row */
rc = ll_dta_add(bdb_state, genid, dbp, physical_tran, dtafile,
dtastripe, dbt_key, dbt_data, DB_NOOVERWRITE, odhready);
if (micro_retry && --retry_count > 0 &&
(rc == BDBERR_DEADLOCK || rc == DB_LOCK_DEADLOCK)) {
retry = 1;
bdb_tran_abort_phys_retry(bdb_state, physical_tran);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 0);
if (rc)
goto done;
if (max_poll > 0)
poll(NULL, 0, rand() % max_poll);
gbl_rowlocks_deadlock_retries++;
} else {
retry = 0;
}
} while (retry);
deadlock_trace(__func__, logical_tran, rc);
if (rc)
goto done;
if (gbl_rowlocks) {
/* Logical log on success */
rc = bdb_llog_add_dta_lk(bdb_state, physical_tran, genid, dtafile,
dtastripe);
if (rc)
goto done;
rc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, physical_tran->tid, physical_tran->tid->txnid,
physical_tran->logical_tran->last_logical_lsn);
}
done:
/* Normalize deadlock rcode */
if (rc == BDBERR_DEADLOCK_ROWLOCK || rc == BDBERR_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
return rc;
}
int phys_dta_del(bdb_state_type *bdb_state, tran_type *logical_tran, int rrn,
unsigned long long genid, DB *dbp, int dtafile, int dtastripe)
{
int rc, micro_retry, retry = 0;
int retry_count = bdb_state->attr->pagedeadlock_retries;
int max_poll = bdb_state->attr->pagedeadlock_maxpoll;
tran_type *physical_tran = NULL;
DBT dbt_dta;
/* Delete row */
DBT *dellkptr = NULL;
DB_LOCK *delrowlk = NULL;
DB_LSN last_regop_lsn = logical_tran->last_regop_lsn;
/* Start my transaction */
rc = get_physical_transaction(bdb_state, logical_tran, &physical_tran, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n", __func__,
rc);
goto done;
}
if (gbl_rowlocks && dtafile == 0) {
int did_commit = 0;
rc = tran_allocate_rlptr(logical_tran, &dellkptr, &delrowlk);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to allocate rlptr rc %d\n",
__func__, rc);
goto done;
}
if (log_compare(&last_regop_lsn, &logical_tran->last_regop_lsn))
did_commit = 1;
rc = bdb_lock_row_write_getlock(bdb_state, logical_tran, -1, genid,
delrowlk, dellkptr, !did_commit);
if (rc == DB_LOCK_NOTGRANTED && !did_commit) {
/* trylock failed, let's commit here and wait again */
tran_deallocate_pop(logical_tran, 1);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 1);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n",
__func__, rc);
goto done;
}
tran_allocate_rlptr(logical_tran, &dellkptr, &delrowlk);
rc = bdb_lock_row_write_getlock(bdb_state, logical_tran, -1, genid,
delrowlk, dellkptr, 0);
}
if (rc) {
logmsg(LOGMSG_ERROR,
"%s failed to lock row write genid %llx rc %d\n", __func__,
genid, rc);
goto done;
}
}
micro_retry = micro_retry_check(bdb_state, logical_tran);
do {
if (gbl_rowlocks) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)
goto done;
}
/* Call dta-del */
rc = ll_dta_del(bdb_state, physical_tran, rrn, genid, dbp, dtafile,
dtastripe, &dbt_dta);
if (micro_retry && --retry_count > 0 &&
(rc == BDBERR_DEADLOCK || rc == DB_LOCK_DEADLOCK)) {
retry = 1;
bdb_tran_abort_phys_retry(bdb_state, physical_tran);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 0);
if (rc)
goto done;
if (max_poll > 0)
poll(NULL, 0, rand() % max_poll);
gbl_rowlocks_deadlock_retries++;
} else {
retry = 0;
}
} while (retry);
deadlock_trace(__func__, logical_tran, rc);
if (rc)
goto done;
if (gbl_rowlocks) {
/* Logical log successful delete */
rc = bdb_llog_del_dta_lk(bdb_state, physical_tran, genid, &dbt_dta,
dtafile, dtastripe);
if (!rc)
rc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, physical_tran->tid, physical_tran->tid->txnid,
physical_tran->logical_tran->last_logical_lsn);
}
if (dbt_dta.size)
free(dbt_dta.data);
done:
/* Normalize deadlock rcode */
if (rc == BDBERR_DEADLOCK_ROWLOCK || rc == BDBERR_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
return rc;
}
int phys_dta_upd(bdb_state_type *bdb_state, int rrn,
unsigned long long oldgenid, unsigned long long *newgenid,
DB *dbp, tran_type *logical_tran, int dtafile, int dtastripe,
DBT *verify_dta, DBT *dta, int odhready)
{
int rc, micro_retry, retry = 0;
int retry_count = bdb_state->attr->pagedeadlock_retries;
int max_poll = bdb_state->attr->pagedeadlock_maxpoll;
tran_type *physical_tran = NULL;
DBT old_dta;
/* Old row */
DBT *oldlkptr = NULL;
DB_LOCK *oldrowlk = NULL;
/* Masked genids */
unsigned long long orignew = *newgenid;
DB_LSN last_regop_lsn = logical_tran->last_regop_lsn;
/* Start my transaction */
rc = get_physical_transaction(bdb_state, logical_tran, &physical_tran, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n", __func__,
rc);
goto done;
}
if (gbl_rowlocks && dtafile == 0) {
int did_commit = 0;
rc = tran_allocate_rlptr(logical_tran, &oldlkptr, &oldrowlk);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to allocate rlptr rc %d\n",
__func__, rc);
goto done;
}
if (log_compare(&last_regop_lsn, &logical_tran->last_regop_lsn))
did_commit = 1;
/* trylock first if physical tran wasn't committed before */
rc = bdb_lock_row_write_getlock(bdb_state, logical_tran, -1, oldgenid,
oldrowlk, oldlkptr, !did_commit);
if (rc == DB_LOCK_NOTGRANTED && !did_commit) {
/* trylock failed, let's commit here and wait again */
tran_deallocate_pop(logical_tran, 1);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 1);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n",
__func__, rc);
goto done;
}
tran_allocate_rlptr(logical_tran, &oldlkptr, &oldrowlk);
rc = bdb_lock_row_write_getlock(bdb_state, logical_tran, -1,
oldgenid, oldrowlk, oldlkptr, 0);
}
if (rc) {
logmsg(LOGMSG_ERROR,
"%s failed to lock row write genid %llx rc %d\n", __func__,
oldgenid, rc);
goto done;
}
}
/*
* The only rowlock I need is for the original row: any new changes will be
* hidden from the sql sessions: they won't see the new row, and they'll
* never need to be blocked by a wall lock in the old row. Writes (other
* updates or * deletes) should block on the old row until a logical
* transaction commits.
*/
micro_retry = micro_retry_check(bdb_state, logical_tran);
do {
if (gbl_rowlocks) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)
goto done;
}
*newgenid = orignew;
/* Returns wall genid, wall rowlock, new genid & new rowlock */
rc = ll_dta_upd(bdb_state, rrn, oldgenid, newgenid, dbp, physical_tran,
dtafile, dtastripe, 0 /*participantstripid*/,
0 /*use_new_genid*/, verify_dta, dta, &old_dta,
odhready);
if (micro_retry && --retry_count > 0 &&
(rc == BDBERR_DEADLOCK || rc == DB_LOCK_DEADLOCK)) {
retry = 1;
bdb_tran_abort_phys_retry(bdb_state, physical_tran);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 0);
if (rc)
goto done;
if (max_poll > 0)
poll(NULL, 0, rand() % max_poll);
gbl_rowlocks_deadlock_retries++;
} else {
retry = 0;
}
} while (retry);
deadlock_trace(__func__, logical_tran, rc);
if (rc)
goto done;
if (gbl_rowlocks) {
/* Write the logical log for this update */
rc = bdb_llog_upd_dta_lk(bdb_state, physical_tran, oldgenid, *newgenid,
dtafile, dtastripe, &old_dta);
if (!rc)
rc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, physical_tran->tid, physical_tran->tid->txnid,
physical_tran->logical_tran->last_logical_lsn);
}
if (old_dta.data)
free(old_dta.data);
done:
/* Normalize deadlock rcode */
if (rc == BDBERR_DEADLOCK_ROWLOCK || rc == BDBERR_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
return rc;
}
int phys_key_add(bdb_state_type *bdb_state, tran_type *logical_tran,
unsigned long long genid, int ixnum, DBT *dbt_key,
DBT *dbt_data)
{
int rc, micro_retry, retry = 0;
int retry_count = bdb_state->attr->pagedeadlock_retries;
int max_poll = bdb_state->attr->pagedeadlock_maxpoll;
tran_type *physical_tran = NULL;
/* New row */
DBT *newlkptr = NULL;
DB_LOCK *newrowlk = NULL;
DB_LSN last_regop_lsn = logical_tran->last_regop_lsn;
/* Physical tran */
rc = get_physical_transaction(bdb_state, logical_tran, &physical_tran, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n", __func__,
rc);
goto done;
}
/* Master-only locks unique ix value to ensure we aren't colliding with a
* delete */
if (gbl_rowlocks && !bdb_state->ixdups[ixnum]) {
int did_commit = 0;
rc = tran_allocate_rlptr(logical_tran, &newlkptr, &newrowlk);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to allocate rlptr rc %d\n",
__func__, rc);
goto done;
}
if (log_compare(&last_regop_lsn, &logical_tran->last_regop_lsn))
did_commit = 1;
rc = bdb_lock_ix_value_write(bdb_state, logical_tran, ixnum, dbt_key,
newrowlk, newlkptr, !did_commit);
if (rc == DB_LOCK_NOTGRANTED && !did_commit) {
/* trylock failed, let's commit here and wait again */
tran_deallocate_pop(logical_tran, 1);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 1);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n",
__func__, rc);
goto done;
}
tran_allocate_rlptr(logical_tran, &newlkptr, &newrowlk);
rc = bdb_lock_ix_value_write(bdb_state, logical_tran, ixnum,
dbt_key, newrowlk, newlkptr, 0);
}
if (rc) {
logmsg(LOGMSG_ERROR,
"%s failed to lock row index write genid %llx rc %d\n",
__func__, genid, rc);
goto done;
}
}
micro_retry = micro_retry_check(bdb_state, logical_tran);
/* Add key */
do {
if (gbl_rowlocks) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)
goto done;
}
rc = ll_key_add(bdb_state, genid, physical_tran, ixnum, dbt_key,
dbt_data);
if (micro_retry && --retry_count > 0 &&
(rc == BDBERR_DEADLOCK || rc == DB_LOCK_DEADLOCK)) {
retry = 1;
bdb_tran_abort_phys_retry(bdb_state, physical_tran);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 0);
if (rc)
goto done;
if (max_poll > 0)
poll(NULL, 0, rand() % max_poll);
gbl_rowlocks_deadlock_retries++;
} else {
retry = 0;
}
} while (retry);
deadlock_trace(__func__, logical_tran, rc);
if (rc)
goto done;
if (gbl_rowlocks) {
/* Logical log on success */
rc = bdb_llog_add_ix_lk(bdb_state, physical_tran, ixnum, genid, dbt_key,
dbt_data->size);
if (rc)
goto done;
rc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, physical_tran->tid, physical_tran->tid->txnid,
physical_tran->logical_tran->last_logical_lsn);
}
done:
/* The value-lock for master-only rowlocks is txn duration:
* Otherwise, we could return incorrect dups for subsequent
* inserts. */
/* Normalize deadlock rcode */
if (rc == BDBERR_DEADLOCK_ROWLOCK || rc == BDBERR_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
return rc;
}
int phys_key_del(bdb_state_type *bdb_state, tran_type *logical_tran,
unsigned long long genid, int ixnum, DBT *key)
{
int rc, micro_retry, retry = 0;
int retry_count = bdb_state->attr->pagedeadlock_retries;
int max_poll = bdb_state->attr->pagedeadlock_maxpoll;
tran_type *physical_tran = NULL;
int payloadsz;
/* Delete row */
DBT *dellkptr = NULL;
DB_LOCK *delrowlk;
DB_LSN last_regop_lsn = logical_tran->last_regop_lsn;
/* Start my transaction */
rc = get_physical_transaction(bdb_state, logical_tran, &physical_tran, 0);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n", __func__,
rc);
goto done;
}
/* Master-only locks unique ix values to prevent colliding inserts from
* making this delete un-abortable */
if (gbl_rowlocks && !bdb_state->ixdups[ixnum]) {
int did_commit = 0;
rc = tran_allocate_rlptr(logical_tran, &dellkptr, &delrowlk);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to allocate rlptr rc %d\n",
__func__, rc);
goto done;
}
if (log_compare(&last_regop_lsn, &logical_tran->last_regop_lsn))
did_commit = 1;
rc = bdb_lock_ix_value_write(bdb_state, logical_tran, ixnum, key,
delrowlk, dellkptr, !did_commit);
if (rc == DB_LOCK_NOTGRANTED && !did_commit) {
/* trylock failed, let's commit here and wait again */
tran_deallocate_pop(logical_tran, 1);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 1);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed get physical tran rc %d\n",
__func__, rc);
goto done;
}
tran_allocate_rlptr(logical_tran, &dellkptr, &delrowlk);
rc = bdb_lock_ix_value_write(bdb_state, logical_tran, ixnum, key,
delrowlk, dellkptr, 0);
}
if (rc) {
logmsg(LOGMSG_ERROR,
"%s failed to lock row index write genid %llx rc %d\n",
__func__, genid, rc);
goto done;
}
}
micro_retry = micro_retry_check(bdb_state, logical_tran);
/* Call key-delete */
do {
if (gbl_rowlocks) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)
goto done;
}
rc = ll_key_del(bdb_state, physical_tran, ixnum, key->data, key->size,
2, genid, &payloadsz);
if (micro_retry && --retry_count > 0 &&
(rc == BDBERR_DEADLOCK || rc == DB_LOCK_DEADLOCK)) {
retry = 1;
bdb_tran_abort_phys_retry(bdb_state, physical_tran);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 0);
if (rc)
goto done;
if (max_poll > 0)
poll(NULL, 0, rand() % max_poll);
gbl_rowlocks_deadlock_retries++;
} else {
retry = 0;
}
} while (retry);
deadlock_trace(__func__, logical_tran, rc);
if (rc)
goto done;
if (gbl_rowlocks) {
/* Logical log successful delete */
rc = bdb_llog_del_ix_lk(bdb_state, physical_tran, ixnum, genid, key,
payloadsz);
if (rc)
goto done;
rc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, physical_tran->tid, physical_tran->tid->txnid,
physical_tran->logical_tran->last_logical_lsn);
}
done:
/* Normalize deadlock rcode */
if (rc == BDBERR_DEADLOCK_ROWLOCK || rc == BDBERR_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
return rc;
}
/* This doesn't need a lock wall because it doesn't move in the btree. */
int phys_key_upd(bdb_state_type *bdb_state, tran_type *logical_tran,
char *table_name, unsigned long long oldgenid,
unsigned long long newgenid, void *key, int ix, int keylen,
void *dta, int dtalen, int llog_payload_len)
{
int rc, micro_retry, retry = 0;
int retry_count = bdb_state->attr->pagedeadlock_retries;
int max_poll = bdb_state->attr->pagedeadlock_maxpoll;
tran_type *physical_tran = NULL;
if (flibc_ntohll(oldgenid) >= flibc_ntohll(newgenid))
abort();
/* Start my transaction */
rc = get_physical_transaction(bdb_state, logical_tran, &physical_tran, 0);
if (rc)
goto done;
/* No rowlocks for master only: the key value hasn't changed
* I have to do this to prevent it from being deleted, don't i?
* No .. the code will block trying to delete the data table row first */
micro_retry = micro_retry_check(bdb_state, logical_tran);
do {
if (gbl_rowlocks) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)
goto done;
}
/* Call into ll */
rc = ll_key_upd(bdb_state, physical_tran, table_name, oldgenid,
newgenid, key, ix, keylen, dta, dtalen);
if (micro_retry && --retry_count > 0 &&
(rc == BDBERR_DEADLOCK || rc == DB_LOCK_DEADLOCK)) {
retry = 1;
bdb_tran_abort_phys_retry(bdb_state, physical_tran);
rc = get_physical_transaction(bdb_state, logical_tran,
&physical_tran, 0);
if (rc)
goto done;
if (max_poll > 0)
poll(NULL, 0, rand() % max_poll);
gbl_rowlocks_deadlock_retries++;
} else {
retry = 0;
}
} while (retry);
deadlock_trace(__func__, logical_tran, rc);
if (rc)
goto done;
if (gbl_rowlocks) {
/* Rowlocks logical logging */
rc = bdb_llog_upd_ix_lk(bdb_state, physical_tran, table_name, key,
keylen, ix, llog_payload_len, oldgenid,
newgenid);
if (rc)
goto done;
rc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, physical_tran->tid, physical_tran->tid->txnid,
physical_tran->logical_tran->last_logical_lsn);
}
done:
/* Normalize deadlock rcode */
if (rc == BDBERR_DEADLOCK_ROWLOCK || rc == BDBERR_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
return rc;
}
int ll_undo_add_ix_lk(bdb_state_type *bdb_state, tran_type *tran,
char *table_name, int ixnum, void *key, int keylen,
DB_LSN *undolsn)
{
int rc;
DB *dbp;
DBT dbt_key = {0};
bdb_state_type *table;
tran_type *physical_tran = NULL;
table = bdb_get_table_by_name(bdb_state, table_name);
if (table == NULL) {
logmsg(LOGMSG_ERROR, "%s unknown table %s\n", __func__, table_name);
return -1;
}
dbt_key.data = key;
dbt_key.size = keylen;
rc = get_physical_transaction(bdb_state, tran, &physical_tran, 0);
if (rc)
goto done;
dbp = table->dbp_ix[ixnum];
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)
goto done;
rc = dbp->del(dbp, physical_tran->tid, &dbt_key, 0);
if (rc)
goto done;
rc = bdb_llog_comprec(bdb_state, physical_tran, undolsn);
rc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, physical_tran->tid, physical_tran->tid->txnid,
physical_tran->logical_tran->last_logical_lsn);
done:
return rc;
}
/* undolsn is the LSN of the record we are undoing with this call */
int ll_undo_add_dta_lk(bdb_state_type *bdb_state, tran_type *tran,
char *table_name, unsigned long long genid,
DB_LSN *undolsn, int dtafile, int dtastripe)
{
int rc;
unsigned long long search_genid;
DB *dbp;
DBT dbt_genid = {0};
bdb_state_type *table;
tran_type *physical_tran = NULL;
table = bdb_get_table_by_name(bdb_state, table_name);
if (table == NULL) {
logmsg(LOGMSG_ERROR, "%s unknown table %s\n", __func__, table_name);
return -1;
}
search_genid = get_search_genid(table, genid);
if (search_genid != genid)
assert(dtafile != 0);
dbt_genid.data = &search_genid;
dbt_genid.size = sizeof(unsigned long long);
rc = get_physical_transaction(bdb_state, tran, &physical_tran, 0);
if (rc)
goto done;
/* TODO: should this call ll.c? */
dbp = table->dbp_data[dtafile][dtastripe];
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, physical_tran->tid->txnid);
if (rc)