forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor_rowlocks.c
2756 lines (2213 loc) · 76.2 KB
/
cursor_rowlocks.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.
*/
/**
* Similar to cursor_ll.c, but the cursors defined here have two additional
* properties:
* 1) They only hold transient page locks - just enough to position the
* cursor. A cursor that's not in the middle of a find/next/prev/
* last call does not hold a page lock.
* 2) They hold row locks for the duration of a cursor. When a cursor moves
* to a different record, gets a row lock on the next row and relinquishes
* the lock on the previous row.
*
* In rowlocks mode, the replication thread has 2 types of transactions active
* at a time: (1) a long duration transaction that only holds row locks, and
* (2) a * short duration transaction that only holds page locks. If our
* cursors hold both page and row locks we can get into a non-detectable
* cycle with the replication thread. To avoid this we use strict ordering:
* we never unconditionally wait for a row locks while holding a page lock. A
* positioned cursor remembers the page_lsn, page number, slot number, key
* value and row lock name of the last record it was on.
*
* Can I use bulk if I'll be releasing the pagelock every time? Nope - don't
* release the pagelock when you use bulk.
*
* TODO (priority order):
*
* odh (X)
* should work on linux (X)
* all tag apis should fail against a rowlocks database ( )
* pause/restore instead of close/open/refind (X)
* bulk ( )
* replace key->lastkey memcpy with switching buffers? ( )
* retry on page operation deadlocks (X)
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <string.h>
#include <assert.h>
#include <strings.h>
#include <build/db.h>
#include "bdb_cursor.h"
#include "bdb_int.h"
#include "cursor_ll.h"
#include "bdb_osqlcur.h"
#include "bdb_osqllog.h"
#include <thread_malloc.h>
#include "locks.h"
#include "tohex.h"
/* For pthread self debug trace */
#include <pthread.h>
#include <logmsg.h>
/* Switch into and out-of the no-rowlocks mode */
enum { CURSOR_MODE_ROWLOCKS = 0, CURSOR_MODE_NO_ROWLOCKS = 1 };
/* Debug variable for this module */
static int rowlocks_debug = 0;
/* Enable debug trace */
static inline int debug_trace(bdb_berkdb_t *pberkdb)
{
return (pberkdb->impl->trak || rowlocks_debug);
}
/* Print nice looking debug trace */
static inline const char *curtypetostr(int type)
{
switch (type) {
default:
case BDBC_UN:
return "unknown?????";
case BDBC_IX:
return "index";
case BDBC_DT:
return "dta";
case BDBC_SK:
return "shadow";
case BDBC_BL:
return "blob";
}
}
/* Consolidated lock & cursor positioning */
static inline int close_pagelock_cursor(bdb_berkdb_t *berkdb)
{
bdb_rowlocks_tag_t *r;
int rc;
/* Pointer to last row structure */
r = &berkdb->impl->u.row;
/* Return good rcode if cursor is already closed */
if (!r->pagelock_cursor)
return 0;
/* Close my cursor */
rc = r->pagelock_cursor->c_close(r->pagelock_cursor);
/* Heavy handed debug assert */
assert(0 == rc || DB_LOCK_DEADLOCK == rc);
/* Nilify cursor */
r->pagelock_cursor = NULL;
/* This is no longer paused */
r->paused = 0;
return rc;
}
/* Nullify cursor after a failed unpause */
static inline int stale_pagelock_cursor(bdb_berkdb_t *berkdb)
{
bdb_rowlocks_tag_t *r;
/* Pointer to last row structure */
r = &berkdb->impl->u.row;
/* Zero nexts */
berkdb->impl->num_nexts_since_stale = 0;
/* Return good rcode if cursor is already null */
if (!r->pagelock_cursor)
return 0;
/* Print debug trace */
if (debug_trace(berkdb)) {
logmsg(LOGMSG_ERROR, "Cur %p nullify stale pagelock cursor\n", berkdb);
}
/* Nilify cursor */
r->pagelock_cursor = NULL;
return 0;
}
/* Get the pagelsn */
static inline int cur_pagelsn(bdb_berkdb_t *berkdb, DB_LSN *lsn)
{
bdb_rowlocks_tag_t *r = &berkdb->impl->u.row;
/* Retrieve if unset */
if (0 == r->pagelsn.file && r->pagelock_cursor) {
r->pagelock_cursor->c_get_pagelsn(r->pagelock_cursor, &r->pagelsn);
}
/* Copy */
*lsn = r->pagelsn;
return 0;
}
/* Unpack data from either a datafile or an index. Switch to bulk if
* appropriate */
static inline int cur_cget(bdb_berkdb_t *berkdb, DBT *dbtkey, DBT *dbtdta,
int flags)
{
bdb_rowlocks_tag_t *r = &berkdb->impl->u.row;
bdb_cursor_impl_t *cur = berkdb->impl->cur;
bdb_state_type *bdb_state = berkdb->impl->bdb_state;
int rc;
/* Logic for accessing data file */
if (BDBC_DT == cur->type && !r->use_bulk) {
rc = bdb_cget_unpack(bdb_state, r->pagelock_cursor, dbtkey, dbtdta,
&r->ver, flags);
if (0 == rc)
cur->ver = r->ver;
}
/* Logic for accessing index file */
else {
rc = r->pagelock_cursor->c_get(r->pagelock_cursor, dbtkey, dbtdta,
flags);
}
/* Unset pagelsn */
r->pagelsn.file = 0;
r->pagelsn.offset = 1;
return rc;
}
/* Snap active cursor back to its original position. Returns cursor
to it's steady state suitable for repositioning. */
static inline int return_cursor(bdb_berkdb_t *berkdb)
{
bdb_rowlocks_tag_t *r;
bdb_cursor_impl_t *cur;
bdb_state_type *bdb_state;
int rc;
u_int32_t curflags = 0;
DBT dbtkey = {0};
DBT dbtdta = {0};
int bdberr;
DB *db = NULL;
r = &berkdb->impl->u.row;
cur = berkdb->impl->cur;
bdb_state = berkdb->impl->bdb_state;
if (cur->type == BDBC_DT)
db = bdb_state->dbp_data[0][cur->idx];
else
db = cur->state->dbp_ix[cur->idx];
if (!r->positioned) {
if (debug_trace(berkdb)) {
logmsg(LOGMSG_ERROR, "Cur %p return_cursor on unpositioned cursor?\n",
berkdb);
}
return 0;
}
/* Set pageorder options. */
if (r->pagelock_cursor == NULL) {
if (cur->pageorder) {
curflags |= DB_PAGE_ORDER;
if (cur->discardpages)
curflags |= DB_DISCARD_PAGES;
}
/* All of these cursors must be pausible */
curflags |= DB_PAUSIBLE;
r->pagelock_cursor = get_cursor_for_cursortran_flags(
berkdb->impl->cur->curtran, db, curflags, &bdberr);
}
if (r->pagelock_cursor == NULL) {
if (debug_trace(berkdb)) {
logmsg(LOGMSG_ERROR, "Cur %p return_cursor error creating cursor\n",
cur);
}
return -1;
}
dbtkey.data = r->lastkey;
dbtkey.size = r->keylen;
dbtdta.data = NULL;
dbtdta.size = 0;
dbtdta.doff = 0;
dbtdta.dlen = 0;
dbtdta.flags |= DB_DBT_MALLOC;
if (debug_trace(berkdb)) {
char *srcmemp;
char *srckeyp;
srcmemp = alloca((2 * r->keylen) + 2);
srckeyp = util_tohex(srcmemp, r->lastkey, r->keylen);
logmsg(LOGMSG_USER, "Cur %p %s tbl %s %s return_key='%s'\n", berkdb,
__func__, bdb_state->name, curtypetostr(cur->type), srckeyp);
}
rc = cur_cget(berkdb, &dbtkey, &dbtdta, DB_SET | DB_DBT_PARTIAL);
if (dbtdta.data)
free(dbtdta.data);
if (rc == DB_REP_HANDLE_DEAD)
rc = DB_LOCK_DEADLOCK;
if (debug_trace(berkdb)) {
logmsg(LOGMSG_USER, "Cur %p return-cursor cursor rc=%d\n", cur, rc);
}
return rc;
}
/* Wrapper for getting a rowlock */
static inline int get_row_lock(bdb_berkdb_t *berkdb, bdb_state_type *bdb_state,
int rowlock_lid, int idx, DBC *dbcp,
unsigned long long genid, DB_LOCK *rlk,
DBT *lkname, int how)
{
bdb_cursor_impl_t *cur = berkdb->impl->cur;
/* Get the rowlock & pause all pagelock cursors if this blocks. */
return bdb_get_row_lock_pfunc(bdb_state, rowlock_lid, idx, dbcp,
cur->ifn->pauseall, cur->ifn->pausearg, genid,
rlk, lkname, how);
}
/* Wrapper for getting a minmax lock */
static inline int get_row_lock_minmaxlk(bdb_berkdb_t *berkdb,
bdb_state_type *bdb_state,
int rowlock_lid, DBC *dbcp, int ixnum,
int stripe, int minmax, DB_LOCK *rlk,
DBT *lkname, int how)
{
bdb_cursor_impl_t *cur = berkdb->impl->cur;
/* Get the endlock & pause all pagelock cursors if this blocks. */
return bdb_get_row_lock_minmaxlk_pfunc(
bdb_state, rowlock_lid, dbcp, cur->ifn->pauseall, cur->ifn->pausearg,
ixnum, stripe, minmax, rlk, lkname, how);
}
/* Open a cursor */
static inline int open_pagelock_cursor(bdb_berkdb_t *berkdb)
{
bdb_rowlocks_tag_t *r;
u_int32_t curflags = 0;
bdb_cursor_impl_t *cur;
int bdberr = 0;
DB *db = NULL;
/* Pointer to last row structure */
r = &berkdb->impl->u.row;
/* Cursor */
cur = berkdb->impl->cur;
/* Shouldn't be here if we got a cursor */
assert(NULL == r->pagelock_cursor);
/* Resolve db object */
db = (BDBC_DT == cur->type) ? cur->state->dbp_data[0][cur->idx]
: cur->state->dbp_ix[cur->idx];
/* Set pageorder options. */
if (cur->pageorder) {
curflags |= DB_PAGE_ORDER;
/* Set discard pages options */
if (cur->discardpages)
curflags |= DB_DISCARD_PAGES;
}
/* All of these cursors must be pausible */
curflags |= DB_PAUSIBLE;
/* Get a cursor */
r->pagelock_cursor = get_cursor_for_cursortran_flags(
berkdb->impl->cur->curtran, db, curflags, &bdberr);
/* Check for error */
if (NULL == r->pagelock_cursor) {
logmsg(LOGMSG_ERROR, "%s line %d: error getting cursor, bdberr is %d.\n",
__func__, __LINE__, bdberr);
return bdberr;
}
/* No nexts */
berkdb->impl->num_nexts_since_open = 0;
/* This is not paused */
r->paused = 0;
/* Cursors are opened in rowlocks mode */
r->lock_mode = CURSOR_MODE_ROWLOCKS;
return 0;
}
/* Unlock routine for rowlocks */
int bdb_berkdb_rowlocks_unlock(bdb_berkdb_t *pberkdb, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_state_type *bdb_state = berkdb->bdb_state;
bdb_rowlocks_tag_t *r = &pberkdb->impl->u.row;
int rc = 0;
/* Reset bdberr */
*bdberr = 0;
/* If you're not rowlocks you shouldn't be here */
assert(BERKDB_REAL_ROWLOCKS == berkdb->type);
/* Close your cursor if you have it */
if (r->pagelock_cursor) {
rc = close_pagelock_cursor(pberkdb);
if (rc) {
if (DB_LOCK_DEADLOCK == rc || DB_REP_HANDLE_DEAD == rc)
*bdberr = BDBERR_DEADLOCK;
else
*bdberr = rc;
rc = -1;
}
}
/* Release rowlock if I have it */
if (r->have_lock) {
bdb_release_row_lock(bdb_state, &r->lk);
r->have_lock = 0;
}
/* Debug trace */
if (debug_trace(pberkdb)) {
bdb_cursor_impl_t *cur = pberkdb->impl->cur;
logmsg(LOGMSG_USER, "Cur %p rowlocks unlock rc=%d", cur, rc);
}
return rc;
}
/* Recreate a cursor */
int bdb_berkdb_rowlocks_lock(bdb_berkdb_t *pberkdb, struct cursor_tran *curtran,
int *bdberr)
{
int rc = 0;
#ifndef NDEBUG
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
/* If you're not rowlocks you shouldn't be here */
assert(BERKDB_REAL_ROWLOCKS == berkdb->type);
bdb_rowlocks_tag_t *r = &pberkdb->impl->u.row;
/* Cursor better be null */
assert(NULL == r->pagelock_cursor);
#endif
/* Open a cursor */
rc = open_pagelock_cursor(pberkdb);
/* Debug trace */
if (debug_trace(pberkdb)) {
bdb_cursor_impl_t *cur = pberkdb->impl->cur;
logmsg(LOGMSG_USER, "Cur %p rowlocks lock rc=%d", cur, rc);
}
return rc;
}
/* Find the first or last record of a table or index */
static inline int bdb_berkdb_rowlocks_firstlast_int(bdb_berkdb_t *berkdb,
int how, int *bdberr)
{
/* Rcode */
int rc;
/* Page index */
int pg, idx;
/* New lock */
DB_LOCK newlk;
/* End lock */
DB_LOCK endlk;
/* Light a flag if we got a record */
int gotrecord = 0;
/* bdb_state */
bdb_state_type *bdb_state;
/* Pointer to genid */
unsigned long long *fgenid;
/* Genid copy */
unsigned long long cgenid = 0;
/* Locker id */
int cursor_lid;
/* Key and Data */
DBT key = {0};
DBT data = {0};
/* Debug variable which counts retries */
int trycnt = 0;
/* Got the endlock flag */
int got_endlk = 0;
/* Work on both data and idx cursors */
int curidx;
int curstp;
/* Pointers to cursors */
bdb_rowlocks_tag_t *r;
bdb_cursor_impl_t *cur;
/* Local reference to bdb_state */
bdb_state = berkdb->impl->bdb_state;
/* Reference to cursor info */
r = &berkdb->impl->u.row;
cur = berkdb->impl->cur;
/* Get lockerid */
cursor_lid = bdb_get_lid_from_cursortran(cur->curtran);
/* This should not be holding any locks */
assert(!r->have_lock);
/* Index & stripe settings for index file */
if (BDBC_IX == cur->type) {
curidx = cur->idx;
curstp = -1;
}
/* Index & stripe settings for data file */
else {
curidx = -1;
curstp = cur->idx;
}
/* Set key */
key.data = r->keymem;
key.size = key.ulen = r->keylen;
key.flags = DB_DBT_USERMEM;
r->key = r->keymem;
/* Set data */
data.data = r->dtamem;
data.size =
r->dtalen + (BDBC_DT == cur->type && bdb_state->ondisk_header ? 7 : 0);
data.ulen = data.size;
data.flags = DB_DBT_USERMEM;
r->dta = r->dtamem;
/* Grab the endlock */
if (!got_endlk) {
/* Get minimum or maximum lock */
rc = get_row_lock_minmaxlk(berkdb, bdb_state, cursor_lid, NULL, curidx,
curstp, DB_FIRST == how ? 0 : 1, &endlk,
NULL, BDB_LOCK_READ);
/* Break out if this failed */
if (rc) {
goto err_rc;
}
/* Endlock flag */
got_endlk = 1;
}
/* Clear the eof flag */
r->eof = 0;
again:
/* Increment debug-try counter */
trycnt++;
/* If this is null then we are in a retry */
if (NULL == r->pagelock_cursor) {
rc = open_pagelock_cursor(berkdb);
if (0 != rc) {
goto err_rc;
}
}
/* Genid pointer */
if (BDBC_DT == cur->type) {
fgenid = (unsigned long long *)r->key;
} else {
fgenid = (unsigned long long *)r->dta;
}
/* Fetch the record */
rc = cur_cget(berkdb, &key, &data, how);
/* Not found case */
if (DB_NOTFOUND == rc) {
rc = IX_EMPTY;
r->eof = 1;
goto done;
}
/* Found a row */
else if (0 == rc) {
r->lastdtasize = data.size;
r->lastkeysize = key.size;
/* Grab rowlock */
rc = get_row_lock(berkdb, bdb_state, cursor_lid, curidx,
r->pagelock_cursor, *fgenid, &newlk, NULL,
BDB_LOCK_READ);
/* Success */
if (0 == rc) {
/* Light the gotrecord flag */
gotrecord = 1;
/* Copy the genid */
cgenid = *fgenid;
/* Grab page of current record */
rc = r->pagelock_cursor->c_get_pageindex(r->pagelock_cursor, &pg,
&idx);
if (rc)
goto err_rc;
/* Stash page and index in cursor */
r->page = pg;
r->index = idx;
goto done;
}
/* Retry on stale cursor */
else if (DB_CUR_STALE == rc) {
/* Cursor is closed automatically by unpause */
stale_pagelock_cursor(berkdb);
/* Try again */
goto again;
} else {
if (BDBERR_DEADLOCK_ROWLOCK != rc) {
logmsg(LOGMSG_ERROR, "%s:%d Bad rcode from get_row_lock_ix %d\n",
__func__, __LINE__, rc);
}
goto err_rc;
}
}
/* Error */
else {
goto err_rc;
}
err_rc:
if (DB_LOCK_DEADLOCK == rc || BDBERR_DEADLOCK_ROWLOCK == rc ||
BDBERR_DEADLOCK == rc || DB_REP_HANDLE_DEAD == rc) {
*bdberr = BDBERR_DEADLOCK;
rc = DB_LOCK_DEADLOCK;
} else {
*bdberr = rc;
rc = -1;
}
done:
/* Release endlock if I got it */
if (got_endlk) {
bdb_release_row_lock(bdb_state, &endlk);
}
/* Cursor is in 'gather-rowlocks' mode */
r->lock_mode = CURSOR_MODE_ROWLOCKS;
/* Got a record. Update cursor internals */
if (gotrecord) {
/* Copy new lock */
r->lk = newlk;
/* We have a new genid */
r->genid = cgenid;
/* We have a lock */
r->have_lock = 1;
/* Copy lastkey */
memcpy(r->lastkey, r->key, r->keylen);
/* Positioned after lastkey copy */
r->positioned = 1;
}
return rc;
}
/* Test to enable bulk mode */
static inline int switch_bulk_test(bdb_berkdb_t *berkdb, int dir)
{
/* bdb_state */
bdb_state_type *bdb_state;
/* Cursors */
bdb_rowlocks_tag_t *r;
/* Bulk size */
int bulksz;
/* Convenience vars */
bdb_state = berkdb->impl->bdb_state;
r = &berkdb->impl->u.row;
/* Already enabled */
if (r->use_bulk)
return 0;
/* Not enabled */
if (!bdb_state->attr->bulk_sql_mode)
return 0;
if (!bdb_state->attr->bulk_sql_rowlocks)
return 0;
/* Wrong mode */
if (CURSOR_MODE_NO_ROWLOCKS != r->lock_mode)
return 0;
/* Wrong direction */
if (DB_NEXT != dir)
return 0;
/* Didn't pass threshold */
if (berkdb->impl->num_nexts < bdb_state->attr->bulk_sql_threshold)
return 0;
/* Not worth it */
if ((bulksz = bdb_state->attr->sqlbulksz) < (bdb_state->lrl << 1))
return 0;
/* Good to enable */
return bulksz;
}
/* Return 1 if there are no outstanding logical transactions */
static inline int update_cursor_lwm(bdb_berkdb_t *pberkdb,
bdb_state_type *bdb_state,
bdb_rowlocks_tag_t *r, int lineno)
{
DB_LSN lwmlsn;
int lwmrc, cmp;
bdb_cursor_impl_t *cur;
lwmrc = bdb_get_lsn_lwm(bdb_state, &lwmlsn);
/* There is an outstanding logical transaction */
if (0 == lwmrc) {
if ((cmp = log_compare(&lwmlsn, &r->lwmlsn)) > 0) {
/* Print trace */
if (debug_trace(pberkdb)) {
cur = pberkdb->impl->cur;
logmsg(LOGMSG_ERROR, "Cur %p %s line %d tbl %s %s update lwm from "
"%d:%d to %d:%d\n",
pberkdb, __FILE__, lineno, bdb_state->name,
curtypetostr(cur->type), r->lwmlsn.file,
r->lwmlsn.offset, lwmlsn.file, lwmlsn.offset);
}
/* Copy into the cursor */
r->lwmlsn = lwmlsn;
} else if (cmp < 0) {
/* This can happen: the replicant's log records are processed in the
* order of physical commit. You could have a log-stream which
* looks like this:
*
* t1: logical-start
* t2: logical-start
* t2: physical-commit
* Open a read cursor which is tagged with t2's logical start
* t1: physical-commit
*
* We don't have to care if the cursor is marked with the later lsn
* because the first physical write from either must have occurred
* after the lsn that we have anyway.
*/
if (debug_trace(pberkdb)) {
cur = pberkdb->impl->cur;
logmsg(LOGMSG_ERROR, "Cur %p %s line %d tbl %s %s not updating "
"out-of-order lwm %d:%d vs cursor %d:%d\n",
pberkdb, __FILE__, lineno, bdb_state->name,
curtypetostr(cur->type), lwmlsn.file, lwmlsn.offset,
r->lwmlsn.file, r->lwmlsn.offset);
}
}
}
return lwmrc;
}
int __lock_dump_region_lockerid
__P((DB_ENV *, const char *, FILE *, u_int32_t lockerid));
/* Here's the algorithm:
* -
* 1 Call next-or-prev, and get the rowlock.
* 2 If the cursor is stale, then reposition on the original record and retry.
* 3 Release the previous rowlock.
*
* Holding the previous rowlock will keep the row from disappearing. It will
* also protect the next record from disappearing (by holding it, we're
* preventing a lock-wall from being formed).
*
* If we get a deadlock and have to release our locks all bets are off. The
* next 'move' should turn into a find, which can grab the next row and lock.
* The upper layer handles this correctly.
*
* I was worried about missing deleted records which are rolled back. The wall
* locks protect against that. I was also worried about the last-record case.
* We shouldn't have to acquire the endlock for that as the deleted/updated
* record would have been protected by a wall-lock which we've passed.
*/
static inline int bdb_berkdb_rowlocks_nextprev_int(bdb_berkdb_t *berkdb,
int dir, int *bdberr)
{
/* Rcode */
int rc = 0;
/* Bulk sz */
int bulksz;
/* Compare variable */
int cmp;
/* Rcode from get_lsn_lwm. Non-0 means there are no logical txns */
int lwmrc;
/* Switch to no-rowlocks */
int nrlsw = 0;
/* For unpacking bulk data */
struct odh odh;
/* Holds the pagelsn of a page */
DB_LSN pagelsn;
/* New lock */
DB_LOCK newlk;
/* Verify */
int pg, idx;
/* Got record flag */
int gotrecord = 0;
/* Have lock flag */
int have_lock = 0;
/* Debug counter */
int trycnt = 0;
/* Current index */
int curidx;
/* Lid for my cursor */
int cursor_lid;
/* Pointer to genid */
unsigned long long *fgenid = NULL;
/* Genid copy */
unsigned long long cgenid = 0;
/* Reposition-on-deadlock key */
char *repokey;
/* Reposition-on-deadlock key pointer */
char *repokeyptr;
/* bdb_state */
bdb_state_type *bdb_state;
/* Key and Data */
DBT key = {0};
DBT data = {0};
/* Pointers to cursors */
bdb_rowlocks_tag_t *r;
bdb_cursor_impl_t *cur;
/* Local reference to bdb_state */
bdb_state = berkdb->impl->bdb_state;
/* Reference to cursor info */
r = &berkdb->impl->u.row;
cur = berkdb->impl->cur;
/* Get lockerid */
cursor_lid = bdb_get_lid_from_cursortran(cur->curtran);
/* This should be holding a lock for the current row */
assert(r->have_lock || CURSOR_MODE_NO_ROWLOCKS == r->lock_mode);
/* Allocate memory for the reposition key */
repokey = alloca(r->keylen);
/* Copy the repokey */
memcpy(repokey, r->key, r->keylen);
/* A reposition in cursor.c read from here */
repokeyptr = r->key;
/* Bulk test */
if ((bulksz = switch_bulk_test(berkdb, dir)) != 0) {
/* Switching to bulk mode */
r->use_bulk = 1;
/* Reset pointers */
r->bulkptr = r->bulkdata = NULL;
/* Reset other bulk vars */
r->bulkdatasz = r->bulkkeysz = 0;
/* Allocate data */
if (!r->bulk.data) {
r->bulk.data = malloc(bulksz);
r->bulk.ulen = bulksz;
r->bulk.flags = DB_DBT_USERMEM;
}
/* Allocate space for unpacking */
if (BDBC_DT == cur->type && !r->odh_tmp) {
r->odh_tmp = malloc(MAXRECSZ);
}
}
/* Set key */
key.data = r->keymem;
key.size = key.ulen = r->keylen;
key.flags = DB_DBT_USERMEM;
r->key = r->keymem;
/* Setup for normal find */
if (!r->use_bulk) {
/* Set data */
data.data = r->dtamem;
data.size = r->dtalen +
(BDBC_DT == cur->type && bdb_state->ondisk_header ? 7 : 0);
data.ulen = data.size;
data.flags = DB_DBT_USERMEM;
r->dta = r->dtamem;
/* Genid pointer */
if (BDBC_DT == cur->type) {
fgenid = (unsigned long long *)r->key;
} else {
fgenid = (unsigned long long *)r->dta;
}
}
/* Index & stripe settings for index file */
if (BDBC_IX == cur->type) {
curidx = cur->idx;
}
/* Index & stripe settings for data file */
else {
curidx = -1;
}
again:
/* Increment debug-try counter */
trycnt++;
/* Return the cursor to the original position */
if (NULL == r->pagelock_cursor) {
/* Sanity */
assert(!r->use_bulk);
/* Open the cursor */
rc = return_cursor(berkdb);
if (rc) {
/* Shouldn't happen: we have a lock on the row */
if (rc == DB_NOTFOUND)
abort();
goto err_rc;
} else if (!r->pagelock_cursor) {
logmsg(LOGMSG_FATAL,
"Return_cursor has a 0 rc and didn't create a cursor.\n");
abort();
}
}
/* Unset no-rowlocks switch */
nrlsw = 0;
/* Switch into pagelocks if I can */
if (bdb_state->attr->rowlocks_pagelock_optimization &&
CURSOR_MODE_ROWLOCKS == r->lock_mode && !r->force_rowlocks) {
/* Get the current pagelsn */
cur_pagelsn(berkdb, &pagelsn);
/* Reset lwmrc - it will be set if I check the list and there's nothing
* in it. */
lwmrc = 0;
/* Only hit the lock (and refresh cursor lwmlsn) if I have to. */
if (log_compare(&pagelsn, &r->lwmlsn) >= 0) {
lwmrc = update_cursor_lwm(berkdb, bdb_state, r, __LINE__);
}
/* To page-locking if there are no txns or the pagelsn is low enough */
if (lwmrc || (cmp = log_compare(&pagelsn, &r->lwmlsn)) < 0) {
/* 'Switch to no-rowlocks' flag */
nrlsw = 1;
}
}
/* Bulk code */
if (r->use_bulk) {
/* Consume from last */
if (r->bulkptr) {
DB_MULTIPLE_KEY_NEXT(r->bulkptr, &r->bulk, r->bulkkey, r->bulkkeysz,