forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll.c
1539 lines (1322 loc) · 53.5 KB
/
ll.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.
*/
/* ll : low level.
add/del/upd instead of making direct berk calls, are to make calls to ll.
ll provides routines that are easily callable instead, taking close to the
same arguments when possible. goal is MINIMAL changes to add/upd/del code
logic whenever possible.
based on whether the transaction is "BERK" or "LOGICAL", the ll routines
make the switch between either directly calling the berk primitive
(that would have been directly called at various places in
add/del/upd) or instead calling out to the physical routines in phys.c
Routines in this file should return unmolested berkdb return code.
Calling code should be responsible for renaming return codes to bdb if
needed.
*/
#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 <limits.h>
#include <assert.h>
#include <alloca.h>
#include <build/db.h>
#include <epochlib.h>
#include <ctrace.h>
#include <net.h>
#include "bdb_int.h"
#include "locks.h"
#include "llog_auto.h"
#include "missing.h"
#include <list.h>
#include "genid.h"
#include "logmsg.h"
/* This file should contains primitives for adding/deleting data.
All code in add.c/upd.c/del.c should call this instead of relying
on berkeley ->put/->del/etc calls. */
#define RCCHECK(rc, close) \
if (rc) { \
if (rc != BDBERR_DEADLOCK && rc != BDBERR_DEADLOCK_ROWLOCK) \
logmsg(LOGMSG_ERROR, "%s:%d %s get rc %d\n", __FILE__, __LINE__, \
__func__, rc); \
if (close) { \
crc = dbcp->c_close(dbcp); \
if (crc == DB_LOCK_DEADLOCK) \
rc = DB_LOCK_DEADLOCK; \
} \
return rc; \
}
/* Grab the rowlock for a record in this index */
static int get_row_lock_ix(bdb_state_type *bdb_state, DBC *dbcp,
tran_type *tran, int ixnum, unsigned long long genid,
DB_LOCK *rlk, DBT *lkname)
{
/* Acquire rowlocks using this lid */
int locker_lid = tran->logical_tran->logical_lid;
/* Make sure this is NULL */
assert(NULL == tran->logical_tran->tid);
/* Call lock primitive */
return bdb_get_row_lock(bdb_state, locker_lid, ixnum, dbcp, genid, rlk,
lkname, BDB_LOCK_WRITE);
}
/* Grab min or max lock while the cursor is not positioned on a page */
static int get_row_lock_ix_minmaxlk(bdb_state_type *bdb_state, DBC *dbcp,
int ixnum, tran_type *tran, int minmax,
DB_LOCK *rlk, DBT *lkname)
{
/* Acquire rowlocks using this lid */
int locker_lid = tran->logical_tran->logical_lid;
/* Make sure this is NULL */
assert(NULL == tran->logical_tran->tid);
/* Call lock primitive */
return bdb_get_row_lock_minmaxlk(bdb_state, locker_lid, dbcp, ixnum, -1,
minmax, rlk, lkname, BDB_LOCK_WRITE);
}
/* Grab a row-lock for this genid. dbcp is positioned in a data file */
static int get_row_lock_dta(bdb_state_type *bdb_state, DBC *dbcp,
tran_type *tran, unsigned long long genid,
DB_LOCK *rlk, DBT *lkname)
{
/* Locker id */
int locker_lid = tran->logical_tran->logical_lid;
/* Make sure this is NULL */
assert(NULL == tran->logical_tran->tid);
/* Call lock primitive */
return bdb_get_row_lock(bdb_state, locker_lid, -1, dbcp, genid, rlk, lkname,
BDB_LOCK_WRITE);
}
/* Grab min-lock for this stripe. 'genid' should be the first record in the
* table. */
static int get_row_lock_dta_minlk(bdb_state_type *bdb_state, DBC *dbcp,
int stripe, tran_type *tran,
unsigned long long genid, DB_LOCK *rlk,
DBT *lkname)
{
/* Locker id */
int locker_lid = tran->logical_tran->logical_lid;
/* Should be NULL */
assert(NULL == tran->logical_tran->tid);
/* Call lock primitive */
return bdb_get_row_lock_minmaxlk(bdb_state, locker_lid, dbcp, -1, stripe, 0,
rlk, lkname, BDB_LOCK_WRITE);
}
extern bdb_state_type *gbl_bdb_state;
extern int gbl_snapisol;
extern int gbl_logical_live_sc;
int bdb_logical_logging_enabled()
{
if ((gbl_bdb_state && gbl_bdb_state->attr->snapisol) || gbl_snapisol ||
gbl_rowlocks || gbl_logical_live_sc)
return 1;
return 0;
}
extern int gbl_is_physical_replicant;
int add_snapisol_logging(bdb_state_type *bdb_state, tran_type *tran)
{
/* physical_replicants:
* handle_truncation->vrfy_match->do_rcvry->reload_schemas->commit
* reload_schemas opens btrees transactionally. We call commit, but this
* won't produce a log record if no other record has been written. */
if ((bdb_state->attr->snapisol || bdb_state->logical_live_sc) &&
!gbl_rowlocks && !gbl_is_physical_replicant) {
if (bdb_state->logical_live_sc) {
if (tran->parent)
tran = tran->parent;
tran->force_logical_commit = 1;
if (tran->dirty_table_hash == NULL) {
tran->dirty_table_hash =
hash_init_strptr(offsetof(bdb_state_type, name));
if (tran->dirty_table_hash == NULL) {
logmsg(LOGMSG_FATAL,
"%s: failed to init dirty table hash\n", __func__);
abort();
}
}
if (hash_find_readonly(tran->dirty_table_hash,
&(bdb_state->name)) == NULL) {
hash_add(tran->dirty_table_hash, bdb_state);
}
}
return 1;
} else {
return 0;
}
}
/* Why do we pass this both dtafile/dtastripe and a dbp? Because there's
some, erm, interesting logic at various points for figuring out which
file to write to, and I don't want to clone it here. */
int ll_dta_add(bdb_state_type *bdb_state, unsigned long long genid, DB *dbp,
tran_type *tran, int dtafile, int dtastripe, DBT *dbt_key,
DBT *dbt_data, int flags, int odhready)
{
int outrc = 0, rc;
int tran_flags;
tran_flags = tran ? 0 : DB_AUTO_COMMIT;
tran_flags |= flags;
switch (tran->tranclass) {
case TRANCLASS_BERK:
if (tran->logical_tran) {
logmsg(LOGMSG_FATAL, "BUG: dtaadd: got a TRANCLASS_BERK transaction"
" in rowlocks mode\n");
abort();
}
/* fall through */
case TRANCLASS_PHYSICAL:
if (add_snapisol_logging(bdb_state, tran)) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, tran->tid->txnid);
if (rc) {
logmsg(LOGMSG_FATAL,
"%s:%d error clearing tracked writelocks: %d\n",
__FILE__, __LINE__, rc);
abort();
}
}
outrc = bdb_put_pack(bdb_state, dtafile > 0 ? 1 : 0, dbp,
tran ? tran->tid : NULL, dbt_key, dbt_data,
tran_flags, odhready);
if (!outrc && add_snapisol_logging(bdb_state, tran)) {
tran_type *parent = (tran->parent) ? tran->parent : tran;
DBT dbt_tbl = {0};
int iirc;
/* prepare tablename and lsn */
dbt_tbl.data = bdb_state->name;
dbt_tbl.size = strlen(bdb_state->name) + 1;
iirc = llog_undo_add_dta_log(
bdb_state->parent->dbenv, tran->tid, &parent->last_logical_lsn,
0, &dbt_tbl, dtafile, dtastripe, genid, parent->logical_tranid,
&parent->last_logical_lsn, NULL);
if (iirc)
abort();
iirc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, tran->tid, tran->tid->txnid,
parent->last_logical_lsn);
if (iirc)
abort();
}
/* Grab row locks surrounding the record we are modifying. This
protects
us against dirty reads and undeleted records. */
break;
case TRANCLASS_LOGICAL:
outrc = phys_dta_add(bdb_state, tran, genid, dbp, dtafile, dtastripe,
dbt_key, dbt_data, odhready);
break;
default:
logmsg(LOGMSG_ERROR, "ll_dta_add called with unknown tran type %d\n",
tran->tranclass);
outrc = -1;
}
return outrc;
}
int ll_dta_del(bdb_state_type *bdb_state, tran_type *tran, int rrn,
unsigned long long genid, DB *dbp, int dtafile, int dtastripe,
DBT *dta_out)
{
int rc;
DBT dbt_key = {0};
DBT dta_out_si = {0};
DBC *dbcp = NULL;
unsigned long long search_genid;
int crc;
int is_blob = 0;
/* Verify updateid here for ondisk data as in rowlocks mode, we dont have
* the luxury of letting ix_find* to protect the row from changing because
* we may have released the page lock before getting the row lock. So the
* row may have been changed in this gap and we need to verify here again */
int verify_updateid = ((tran->verify_updateid || tran->logical_tran) && dtafile == 0);
if (dta_out) {
bzero(dta_out, sizeof(DBT));
dta_out->flags = DB_DBT_MALLOC;
}
/* Always delete a masked genid for blobs. */
if (dtafile > 0)
is_blob = 1;
/* NOTE: for logical transactions we are called with a row lock NOT
acquired, the physical code ends up acquiring the lock and calling
us with a berkeley transaction with the lock held. */
switch (tran->tranclass) {
case TRANCLASS_BERK:
if (tran->logical_tran) {
logmsg(LOGMSG_FATAL, "BUG: dtadel: got a TRANCLASS_BERK transaction"
" in rowlocks mode\n");
abort();
}
/* fall through */
case TRANCLASS_PHYSICAL: {
if (add_snapisol_logging(bdb_state, tran)) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, tran->tid->txnid);
if (rc) {
logmsg(LOGMSG_FATAL,
"%s:%d error clearing tracked writelocks: %d\n",
__FILE__, __LINE__, rc);
abort();
}
}
rc = dbp->cursor(dbp, tran->tid, &dbcp, 0);
if (rc) {
if (rc != DB_LOCK_DEADLOCK)
logmsg(LOGMSG_ERROR, "%s:%d %s cursor rc %d\n", __FILE__, __LINE__,
__func__, rc);
return rc;
}
/* 1) form the key
2) position the cursor to the key we are trying to delete */
dbt_key.flags = DB_DBT_USERMEM;
dbt_key.ulen = sizeof(unsigned long long);
/* Retrieve a maximum of eight bytes. This is enough to check the
genid. Unless
we are deleting as part of a logical transaction, in which case we
need to log
the entire record in case the transaction aborts later, so we need to
get the
whole record payload. */
/* If the calling code needs the record value to log an undo,
fetch it */
if (dta_out || bdb_state->attr->snapisol ||
bdb_state->logical_live_sc || is_blob) {
/* This codepath does a direct lookup on a masked genid. */
int updateid = 0;
int od_updateid = 0;
search_genid = get_search_genid(bdb_state, genid);
dbt_key.data = &search_genid;
dbt_key.size = sizeof(search_genid);
/* Tell Berkley to allocate memory if we need it. */
if (dta_out || verify_updateid ||
add_snapisol_logging(bdb_state, tran)) {
dta_out_si.flags = DB_DBT_MALLOC;
} else {
dta_out_si.ulen = 0;
dta_out_si.flags = DB_DBT_PARTIAL | DB_DBT_USERMEM;
}
/*
* Use a real cursor so we pick up the ondisk headers. The
* data will still be 'packed'. Use the packed-size for the
* logical log, and return a packed record.
*/
if (0 != (rc = dbcp->c_get(dbcp, &dbt_key, &dta_out_si,
DB_SET | DB_RMW))) {
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = crc;
goto done;
}
if (verify_updateid) {
/* Verify updateid */
updateid = get_updateid_from_genid(bdb_state, genid);
od_updateid = bdb_retrieve_updateid(bdb_state, dta_out_si.data,
dta_out_si.size);
if (od_updateid >= 0) {
if (dtafile >= 1) {
/* blobs */
if (od_updateid > updateid)
rc = DB_NOTFOUND;
} else {
/* data */
if (od_updateid != updateid)
rc = DB_NOTFOUND;
}
} else {
logmsg(LOGMSG_ERROR,
"%s:%d failed to get updateid from odh for genid "
"%llx\n",
__FILE__, __LINE__, genid);
rc = DB_ODH_CORRUPT;
}
}
/* Copy the pointers if the user wanted the record. */
if (dta_out) {
dta_out->data = dta_out_si.data;
dta_out->size = dta_out_si.size;
} else if (dta_out_si.data) {
/* Logical logging only needs the record size. */
free(dta_out_si.data);
dta_out_si.data = NULL;
}
} /* dta_out || snapisol || is_blob */
else {
/* Use the normal genid. */
dbt_key.data = &genid;
dbt_key.size = sizeof(genid);
rc = bdb_cposition(bdb_state, dbcp, &dbt_key, DB_SET | DB_RMW);
}
if (rc != 0) {
/* this could get a deadlock, and if it does, we
need to percolate it up */
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = crc;
goto done;
}
rc = dbcp->c_del(dbcp, 0);
if (dtafile != 0 && rc == DB_NOTFOUND)
rc = 0;
else if (rc) {
if (rc != DB_LOCK_DEADLOCK)
logmsg(LOGMSG_ERROR, "%s:%d %s c_del rc %d\n", __FILE__, __LINE__,
__func__, rc);
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = crc;
goto done;
}
rc = dbcp->c_close(dbcp);
if (!rc && add_snapisol_logging(bdb_state, tran)) {
tran_type *parent = (tran->parent) ? tran->parent : tran;
DBT dbt_tbl = {0};
int iirc;
dbt_tbl.size = strlen(bdb_state->name) + 1;
dbt_tbl.data = bdb_state->name;
iirc = llog_undo_del_dta_log(bdb_state->parent->dbenv, tran->tid,
&parent->last_logical_lsn, 0, &dbt_tbl,
genid, parent->logical_tranid,
&parent->last_logical_lsn, dtafile,
dtastripe, dta_out_si.size, NULL);
if (iirc)
abort();
iirc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, tran->tid, tran->tid->txnid,
parent->last_logical_lsn);
if (iirc)
abort();
}
} break;
case TRANCLASS_LOGICAL:
rc = phys_dta_del(bdb_state, tran, rrn, genid, dbp, dtafile, dtastripe);
break;
default:
logmsg(LOGMSG_ERROR, "ll_dta_del called with unknown tranclass %d\n",
tran->tranclass);
rc = -1;
break;
}
done:
return rc;
}
int ll_key_del(bdb_state_type *bdb_state, tran_type *tran, int ixnum, void *key,
int keylen, int rrn, unsigned long long genid, int *payloadsz)
{
int rc = 0;
DB *dbp;
DBC *dbcp;
DBT dbt_key = {0};
DBT dbt_dta = {0};
unsigned long long *found_genid;
int keydata[3]; /* genid or rrn + genid for verification */
int crc;
int payloadsz_si = 0;
dbp = bdb_state->dbp_ix[ixnum];
dbt_key.data = key;
dbt_key.size = keylen;
found_genid = (unsigned long long *)(keydata);
switch (tran->tranclass) {
case TRANCLASS_BERK:
if (tran->logical_tran) {
logmsg(LOGMSG_FATAL, "BUG: keydel: got a TRANCLASS_BERK "
"transaction in rowlocks mode\n");
abort();
}
/* fall through */
case TRANCLASS_PHYSICAL:
if (add_snapisol_logging(bdb_state, tran)) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, tran->tid->txnid);
if (rc) {
logmsg(LOGMSG_FATAL,
"%s:%d error clearing tracked writelocks: %d\n",
__FILE__, __LINE__, rc);
abort();
}
}
if ((bdb_state->attr->snapisol || bdb_state->logical_live_sc) &&
!payloadsz)
payloadsz = &payloadsz_si;
/* open a cursor on the index, find exact key to be deleted
(if key doesn't allow dupes we need to verify the genid),
then delete */
rc = dbp->cursor(dbp, tran ? tran->tid : NULL, &dbcp, 0);
if (rc) {
if (rc != DB_LOCK_DEADLOCK)
logmsg(LOGMSG_ERROR, "%s:%d %s cursor rc %d\n", __FILE__, __LINE__,
__func__, rc);
goto done;
}
if (bdb_state->ixdta[ixnum]) {
dbt_dta.data = keydata;
dbt_dta.ulen = sizeof(int) * 3;
dbt_dta.dlen = dbt_dta.ulen;
dbt_dta.flags = DB_DBT_USERMEM;
/* Get the full record to get the size. This will fail with ENOMEM
for most calls since we didn't allocate enough room - this is
intentional. All we want is the size. The call a few lines
further down will fetch the genid with a partial find. */
rc = dbcp->c_get(dbcp, &dbt_key, &dbt_dta, DB_SET | DB_RMW);
if (rc && rc != ENOMEM) {
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
goto done;
}
dbt_dta.data = keydata;
dbt_dta.ulen = sizeof(int) * 3;
dbt_dta.dlen = dbt_dta.ulen;
dbt_dta.doff = 0;
dbt_dta.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
if (payloadsz)
*payloadsz = dbt_dta.size;
} else {
dbt_dta.data = keydata;
dbt_dta.ulen = sizeof(int) * 3;
dbt_dta.dlen = dbt_dta.ulen;
dbt_dta.doff = 0;
dbt_dta.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
if (payloadsz)
*payloadsz = sizeof(unsigned long long);
}
/* call cget here in favor of bdb_cget_unpack since keys aren't
packed and unpack routines don't support partial finds. */
rc = dbcp->c_get(dbcp, &dbt_key, &dbt_dta, DB_SET | DB_RMW);
if (rc) {
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
goto done;
}
/* see if the genid matches */
if (*found_genid != genid) {
rc = DB_NOTFOUND;
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
goto done;
}
rc = dbcp->c_del(dbcp, 0);
if (rc != 0) {
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
goto done;
}
/* now close our cursor */
rc = dbcp->c_close(dbcp);
if (!rc && add_snapisol_logging(bdb_state, tran)) {
tran_type *parent = (tran->parent) ? tran->parent : tran;
DBT dbt_tbl = {0};
int iirc;
dbt_tbl.size = strlen(bdb_state->name) + 1;
dbt_tbl.data = bdb_state->name;
iirc = llog_undo_del_ix_log(
bdb_state->parent->dbenv, tran->tid, &parent->last_logical_lsn,
0, &dbt_tbl, genid, ixnum, parent->logical_tranid,
&parent->last_logical_lsn, NULL, keylen, *payloadsz);
if (iirc)
abort();
iirc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, tran->tid, tran->tid->txnid,
parent->last_logical_lsn);
if (iirc)
abort();
}
break;
case TRANCLASS_LOGICAL:
rc = phys_key_del(bdb_state, tran, genid, ixnum, &dbt_key);
break;
default:
logmsg(LOGMSG_ERROR, "ll_key_del called with unknown tran type %d\n",
tran->tranclass);
rc = -1;
}
done:
return rc;
}
int ll_key_upd(bdb_state_type *bdb_state, tran_type *tran, char *table_name,
unsigned long long oldgenid, unsigned long long genid, void *key,
int ixnum, int keylen, void *dta, int dtalen)
{
int rc = 0;
DB *dbp;
DBC *dbcp;
DBT dbt_key = {0};
DBT dbt_dta = {0};
int crc;
const int genid_sz = sizeof(unsigned long long);
unsigned char dtacopy_payload[MAXRECSZ + ODH_SIZE_RESERVE + genid_sz];
int dtacopy_payload_len = 0;
unsigned char keydata[MAXKEYSZ];
int llog_payload_len = 8;
dbp = bdb_state->dbp_ix[ixnum];
bzero(&dbt_key, sizeof(DBT));
dbt_key.data = key;
dbt_key.size = keylen;
if (dta) {
/* copy the genid */
memcpy(dtacopy_payload, &genid, genid_sz);
/* we really need to do this only for datacopy row */
if (bdb_state->ixdta[ixnum] && bdb_state->ondisk_header &&
bdb_state->datacopy_odh) {
/* put odh and dta */
struct odh odh;
void *rec = NULL;
uint32_t recsize = 0;
void *freeptr = NULL;
int pd_index = bdb_state->ixdtalen[ixnum] > 0 ? ixnum : -1; // partial datacopy
init_odh(bdb_state, &odh, dta, dtalen, 0);
bdb_pack(bdb_state, &odh, dtacopy_payload + genid_sz,
MAXRECSZ + ODH_SIZE_RESERVE, &rec, &recsize, &freeptr, pd_index);
llog_payload_len = dtacopy_payload_len = recsize + genid_sz;
} else {
/* put dta only */
memcpy(dtacopy_payload + genid_sz, dta, dtalen);
llog_payload_len = dtacopy_payload_len = dtalen + genid_sz;
}
}
switch (tran->tranclass) {
case TRANCLASS_BERK:
if (tran->logical_tran) {
logmsg(LOGMSG_FATAL, "BUG: keydel: got a TRANCLASS_BERK "
"transaction in rowlocks mode\n");
abort();
}
/* fall through */
case TRANCLASS_PHYSICAL:
if (add_snapisol_logging(bdb_state, tran)) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, tran->tid->txnid);
if (rc) {
logmsg(LOGMSG_FATAL,
"%s:%d error clearing tracked writelocks: %d\n",
__FILE__, __LINE__, rc);
abort();
}
}
/* open a cursor on the index, find exact key to be deleted
(if key doesn't allow dupes we need to verify the genid),
then delete */
rc = dbp->cursor(dbp, tran ? tran->tid : NULL, &dbcp, 0);
if (rc) {
if (rc != DB_LOCK_DEADLOCK)
logmsg(LOGMSG_ERROR, "%s:%d %s cursor rc %d\n", __FILE__, __LINE__,
__func__, rc);
goto done;
}
bzero(&dbt_key, sizeof(DBT));
bzero(&dbt_dta, sizeof(DBT));
/* lookup by key */
dbt_key.data = key;
dbt_key.size = keylen;
dbt_key.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
/* retrieve genid */
dbt_dta.data = keydata;
dbt_dta.ulen = sizeof(unsigned long long);
dbt_dta.dlen = sizeof(unsigned long long);
dbt_dta.doff = 0;
dbt_dta.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
/* do a partial find to support dupe indexes which have a genid
appended on the right side of the key. */
/* call cget here in favor of bdb_cget_unpack since keys aren't
packed and unpack routines don't support partial finds. */
rc = dbcp->c_get(dbcp, &dbt_key, &dbt_dta, DB_SET | DB_RMW);
if (rc) {
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
goto done;
}
/* now do an update to update the genid. if we're using dtacopy
then update the genid + changed data payload. (it must have changed
if we're updating something). */
if (dta) {
dbt_dta.data = dtacopy_payload;
dbt_dta.ulen = dtacopy_payload_len;
dbt_dta.dlen = dtacopy_payload_len;
dbt_dta.size = dtacopy_payload_len;
} else {
dbt_dta.data = &genid;
dbt_dta.ulen = sizeof(unsigned long long);
dbt_dta.dlen = sizeof(unsigned long long);
}
dbt_dta.doff = 0;
dbt_dta.flags = DB_DBT_USERMEM;
rc = dbcp->c_put(dbcp, &dbt_key, &dbt_dta, DB_CURRENT);
if (rc != 0) {
crc = dbcp->c_close(dbcp);
if (crc == DB_LOCK_DEADLOCK)
rc = DB_LOCK_DEADLOCK;
goto done;
}
/* now close our cursor */
rc = dbcp->c_close(dbcp);
if (!rc && add_snapisol_logging(bdb_state, tran)) {
tran_type *parent = (tran->parent) ? tran->parent : tran;
DBT dbt_tbl = {0};
int iirc;
dbt_tbl.size = strlen(bdb_state->name) + 1;
dbt_tbl.data = bdb_state->name;
{
/* Send key with our logical-log: we can't get to it from the
* berkley logs. */
iirc = llog_undo_upd_ix_log(
bdb_state->parent->dbenv, tran->tid,
&parent->last_logical_lsn, 0, &dbt_tbl, oldgenid, genid,
parent->logical_tranid, &parent->last_logical_lsn, ixnum,
&dbt_key, dtalen);
/*
DB_LSN crp = parent->last_logical_lsn;
fprintf( stderr, "%s:%d upd ix LLSN %d:%d -> %d:%d\n",
__FILE__, __LINE__, crp.file, crp.offset,
parent->last_logical_lsn.file,
parent->last_logical_lsn.offset);
*/
if (iirc)
abort();
}
iirc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, tran->tid, tran->tid->txnid,
parent->last_logical_lsn);
if (iirc)
abort();
}
break;
case TRANCLASS_LOGICAL:
rc = phys_key_upd(bdb_state, tran, bdb_state->name, oldgenid, genid,
key, ixnum, keylen, dta, dtalen, llog_payload_len);
break;
default:
logmsg(LOGMSG_ERROR, "ll_key_upd called with unknown tran type %d\n",
tran->tranclass);
rc = -1;
}
done:
return rc;
}
int ll_key_add(bdb_state_type *bdb_state, unsigned long long ingenid,
tran_type *tran, int ixnum, DBT *dbt_key, DBT *dbt_data)
{
int rc;
switch (tran->tranclass) {
case TRANCLASS_BERK:
if (tran->logical_tran) {
logmsg(LOGMSG_FATAL, "BUG: keyadd: got a TRANCLASS_BERK transaction"
" in rowlocks mode\n");
abort();
}
/* fall through */
case TRANCLASS_PHYSICAL:
if (add_snapisol_logging(bdb_state, tran)) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, tran->tid->txnid);
if (rc) {
logmsg(LOGMSG_FATAL,
"%s:%d error clearing tracked writelocks: %d\n",
__FILE__, __LINE__, rc);
abort();
}
}
rc = bdb_state->dbp_ix[ixnum]->put(bdb_state->dbp_ix[ixnum], tran->tid,
dbt_key, dbt_data, DB_NOOVERWRITE);
if (rc) {
return rc;
}
if (!rc && add_snapisol_logging(bdb_state, tran)) {
tran_type *parent = (tran->parent) ? tran->parent : tran;
DBT dbt_tbl = {0};
int iirc;
/* prepare tablename and lsn */
dbt_tbl.data = bdb_state->name;
dbt_tbl.size = strlen(bdb_state->name) + 1;
iirc = llog_undo_add_ix_log(
bdb_state->parent->dbenv, tran->tid, &parent->last_logical_lsn,
0, &dbt_tbl, ixnum, ingenid, parent->logical_tranid,
&parent->last_logical_lsn, dbt_key->size, dbt_data->size);
if (iirc)
abort();
iirc = bdb_state->dbenv->lock_update_tracked_writelocks_lsn(
bdb_state->dbenv, tran->tid, tran->tid->txnid,
parent->last_logical_lsn);
if (iirc)
abort();
}
break;
case TRANCLASS_LOGICAL:
rc = phys_key_add(bdb_state, tran, ingenid, ixnum, dbt_key, dbt_data);
break;
default:
logmsg(LOGMSG_ERROR, "ll_key_add called with unknown tran type %d\n",
tran->tranclass);
rc = -1;
}
return rc;
}
static int ll_dta_upd_int(bdb_state_type *bdb_state, int rrn,
unsigned long long oldgenid,
unsigned long long *newgenid, DB *dbp,
tran_type *tran, int dtafile, int dtastripe,
int participantstripid, int use_new_genid,
DBT *verify_dta, DBT *dta, DBT *old_dta_out,
int is_blob, int has_blob_update_optimization,
int keep_genid_intact, int odhready)
{
int rc = 0;
int inplace = 0;
int updateid = 0;
DBC *dbcp = NULL;
DBT dbt_key;
DBT dbt_data;
unsigned long long search_genid;
bdb_state_type *parent;
void *malloceddta = NULL;
void *recptr = NULL;
int bdberr = 0;
int crc;
struct odh odh;
void *freeptr = NULL;
void *freedtaptr = NULL;
DB *dbp_add;
int add_blob = 0;
DBT old_dta_out_lcl;
char *oldrec = NULL;
void *formatted_record = NULL;
uint32_t formatted_record_len;
int formatted_record_needsfree = 0;
int oldsz = -1;
int newstripe = 0;
/* Verify updateid for ondisk data in rowlocks mode.
* see ll_dta_del() */
int verify_updateid = (tran->logical_tran && dtafile == 0);
if (bdb_state->parent)
parent = bdb_state->parent;
else
parent = bdb_state;
dbp_add = NULL;
if (old_dta_out) {
old_dta_out->size = 0;
old_dta_out->data = NULL;
}
switch (tran->tranclass) {
case TRANCLASS_BERK:
if (tran->logical_tran) {
logmsg(LOGMSG_FATAL, "BUG: datupd: got a TRANCLASS_BERK "
"transaction in rowlocks mode\n");
abort();
}
/* fall through */
case TRANCLASS_PHYSICAL:
if (add_snapisol_logging(bdb_state, tran)) {
rc = bdb_state->dbenv->lock_clear_tracked_writelocks(
bdb_state->dbenv, tran->tid->txnid);
if (rc) {
logmsg(LOGMSG_FATAL,
"%s:%d error clearing tracked writelocks: %d\n",
__FILE__, __LINE__, rc);
abort();
}
}
/* open a cursor */
rc = dbp->cursor(dbp, tran->tid, &dbcp, 0);
if (rc != 0) {
bdb_cursor_error(bdb_state, tran->tid, rc, &bdberr, "ll_dta_upd");
goto done;
}
/* Possibly allocate a genid for this update */
if (keep_genid_intact) {
inplace = 1;
} else if (0 == *newgenid) {
if (ip_updates_enabled(bdb_state)) {
int newupd = (1 + get_updateid_from_genid(bdb_state, oldgenid));
if (newupd <= max_updateid(bdb_state)) {
*newgenid = set_updateid(bdb_state, newupd, oldgenid);
inplace = 1;
}
}
if (!inplace) {
if (bdb_state->attr->disable_update_stripe_change) {
/* old style that prevents update missing rows */
newstripe = get_dtafile_from_genid(oldgenid);
} else {
newstripe = bdb_get_active_stripe_int(bdb_state);
}