forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constraints.c
2108 lines (1903 loc) · 78.1 KB
/
constraints.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, 2018, 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.
*/
#include "limit_fortify.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <alloca.h>
#include <memory_sync.h>
#include "comdb2.h"
#include <schemachange.h>
#include "tag.h"
#include "types.h"
#include "block_internal.h"
#include <assert.h>
#include "logmsg.h"
#include "views.h"
#include "osqlsqlthr.h"
static void *get_constraint_table_cursor(void *table);
static int close_constraint_table_cursor(void *cursor);
static char *get_temp_ct_dbname(long long *);
static int is_update_op(int op);
static int is_delete_op(int op);
extern int gbl_partial_indexes;
/**
* Checks to see if there are any cascading deleletes/updates pointing to this
* table
* @param db pointer to the db to check for cascading constraints
* @param op optype used to determine whether we care about update or delete
* constraints
* @return 0 if there are no cascading constraints !0 otherwise
*/
int has_cascading_reverse_constraints(struct dbtable *db)
{
int i;
/* for each of the constraints pointing to this db */
for (i = 0; i < db->n_rev_constraints; i++) {
constraint_t *cnstrt = db->rev_constraints[i];
if ((cnstrt->flags & CT_DEL_CASCADE) == CT_DEL_CASCADE)
return 1;
if ((cnstrt->flags & CT_UPD_CASCADE) == CT_UPD_CASCADE)
return 1;
}
return 0;
}
static int insert_add_index(struct ireq *iq, unsigned long long genid)
{
char key[MAXKEYLEN];
void *foundkey = NULL;
void *cur = NULL;
int err = 0;
int *pixnum;
int i;
int rc = 0;
if (iq->idxInsert == NULL)
return 0;
struct thread_info *thdinfo = pthread_getspecific(unique_tag_key);
if (thdinfo == NULL) {
logmsg(LOGMSG_ERROR, "%s: no thdinfo\n", __func__);
return -1;
}
cur = get_constraint_table_cursor(thdinfo->ct_add_index);
if (cur == NULL) {
logmsg(LOGMSG_ERROR, "%s : no cursor???\n", __func__);
return -1;
}
memcpy(key, &genid, sizeof(unsigned long long));
pixnum = (int *)(key + sizeof(unsigned long long));
*pixnum = 0;
bdb_temp_table_find(thedb->bdb_env, cur, key,
sizeof(unsigned long long) + sizeof(int), NULL, &err);
foundkey = bdb_temp_table_key(cur);
if (foundkey && !memcmp(foundkey, &genid, sizeof(unsigned long long)))
goto out;
for (i = 0; i < iq->usedb->nix; i++) {
if (iq->idxInsert[i] == NULL)
continue;
*pixnum = i;
rc = bdb_temp_table_insert(
thedb->bdb_env, cur, key, sizeof(unsigned long long) + sizeof(int),
iq->idxInsert[i], getkeysize(iq->usedb, i), &err);
if (rc != 0) {
logmsg(LOGMSG_ERROR, "%s: bdb_temp_table_insert rc = %d\n", __func__,
rc);
goto out;
}
}
out:
close_constraint_table_cursor(cur);
return rc;
}
void free_cached_idx(uint8_t **cached_idx);
static int cache_delayed_indexes(struct ireq *iq, unsigned long long genid)
{
char key[MAXKEYLEN];
void *foundkey = NULL;
void *cur = NULL;
int err = 0;
int *pixnum;
if (iq->idxInsert || iq->idxDelete) {
free_cached_idx(iq->idxInsert);
free_cached_idx(iq->idxDelete);
free(iq->idxInsert);
free(iq->idxDelete);
iq->idxInsert = iq->idxDelete = NULL;
}
struct thread_info *thdinfo = pthread_getspecific(unique_tag_key);
if (thdinfo == NULL) {
logmsg(LOGMSG_ERROR, "%s: no thdinfo\n", __func__);
return -1;
}
cur = get_constraint_table_cursor(thdinfo->ct_add_index);
if (cur == NULL) {
logmsg(LOGMSG_ERROR, "%s : no cursor???\n", __func__);
return -1;
}
memcpy(key, &genid, sizeof(unsigned long long));
pixnum = (int *)(key + sizeof(unsigned long long));
*pixnum = 0;
bdb_temp_table_find(thedb->bdb_env, cur, key,
sizeof(unsigned long long) + sizeof(int), NULL, &err);
foundkey = bdb_temp_table_key(cur);
if (!foundkey || memcmp(foundkey, &genid, sizeof(unsigned long long))) {
close_constraint_table_cursor(cur);
return 0;
} else {
iq->idxInsert = calloc(MAXINDEX, sizeof(uint8_t *));
iq->idxDelete = calloc(MAXINDEX, sizeof(uint8_t *));
if (!iq->idxInsert || !iq->idxDelete) {
logmsg(LOGMSG_ERROR, "%s failed to allocated indexes\n", __func__);
close_constraint_table_cursor(cur);
return -1;
}
}
while (1) {
pixnum = (int *)((char *)foundkey + sizeof(unsigned long long));
iq->idxInsert[*pixnum] = malloc(getkeysize(iq->usedb, *pixnum));
if (iq->idxInsert[*pixnum] == NULL) {
logmsg(LOGMSG_ERROR, "%s failed to allocated indexes\n", __func__);
close_constraint_table_cursor(cur);
return -1;
}
memcpy(iq->idxInsert[*pixnum], bdb_temp_table_data(cur),
getkeysize(iq->usedb, *pixnum));
bdb_temp_table_next(thedb->bdb_env, cur, &err);
foundkey = bdb_temp_table_key(cur);
if (!foundkey || memcmp(foundkey, &genid, sizeof(unsigned long long)))
break;
}
close_constraint_table_cursor(cur);
return 0;
}
static inline void free_cached_delayed_indexes(struct ireq *iq)
{
if (iq->idxInsert || iq->idxDelete) {
free_cached_idx(iq->idxInsert);
free_cached_idx(iq->idxDelete);
free(iq->idxInsert);
free(iq->idxDelete);
iq->idxInsert = iq->idxDelete = NULL;
}
}
enum ct_etype { CTE_ADD = 1, CTE_DEL, CTE_UPD };
struct forward_ct {
unsigned long long genid;
unsigned long long ins_keys;
struct dbtable *usedb;
int blkpos;
int ixnum;
int rrn;
int optype;
int flags;
};
struct backward_ct {
struct dbtable *srcdb;
struct dbtable *dstdb;
int blkpos;
int optype;
char key[MAXKEYLEN];
char newkey[MAXKEYLEN];
int sixlen;
int sixnum;
int dixnum;
int nonewrefs;
int flags;
};
typedef struct cttbl_entry {
int ct_type;
union {
struct forward_ct fwdct;
struct backward_ct bwdct;
} ctop;
} cte;
int insert_add_op(struct ireq *iq, int optype, int rrn, int ixnum,
unsigned long long genid, unsigned long long ins_keys,
int blkpos, int flags)
{
block_state_t *blkstate = iq->blkstate;
int type = CTE_ADD, rc = 0;
char key[MAXKEYLEN];
cte cte_record;
int err = 0;
struct thread_info *thdinfo = pthread_getspecific(unique_tag_key);
if (thdinfo == NULL) {
logmsg(LOGMSG_ERROR, "insert_add_op: no thdinfo\n");
return -1;
}
void *cur = get_constraint_table_cursor(thdinfo->ct_add_table);
if (cur == NULL) {
logmsg(LOGMSG_ERROR, "insert_add_op: no cursor???\n");
return -1;
}
memcpy(key, &type, sizeof(type));
memcpy(key + sizeof(type), &blkstate->ct_id_key,
sizeof(blkstate->ct_id_key));
cte_record.ct_type = CTE_ADD;
struct forward_ct *fwdct = &cte_record.ctop.fwdct;
fwdct->genid = genid;
fwdct->ins_keys = ins_keys;
fwdct->usedb = iq->usedb;
fwdct->blkpos = blkpos;
fwdct->ixnum = ixnum;
fwdct->rrn = rrn;
fwdct->optype = optype;
fwdct->flags = flags;
rc = bdb_temp_table_insert(thedb->bdb_env, cur, key,
sizeof(int) + sizeof(long long), &cte_record,
sizeof(cte), &err);
close_constraint_table_cursor(cur);
if (rc != 0) {
logmsg(LOGMSG_ERROR, "insert_add_op: bdb_temp_table_insert rc = %d\n", rc);
return -1;
}
rc = insert_add_index(iq, genid);
if (rc != 0) {
logmsg(LOGMSG_ERROR, "insert_add_op: insert_add_index rc = %d\n", rc);
return -1;
}
blkstate->ct_id_key++;
return 0;
}
static int insert_del_op(block_state_t *blkstate, struct dbtable *srcdb,
struct dbtable *dstdb, int optype, int blkpos,
void *inkey, void *innewkey, int keylen, int sixnum,
int dixnum, int nonewrefs, int flags)
{
void *cur = NULL;
int type = CTE_DEL, rc = 0;
char key[MAXKEYLEN];
cte cte_record;
int err = 0;
struct thread_info *thdinfo = pthread_getspecific(unique_tag_key);
if (thdinfo == NULL)
return -1;
cur = get_constraint_table_cursor(thdinfo->ct_del_table);
if (cur == NULL)
return -1;
memcpy(key, &type, sizeof(type));
memcpy(key + sizeof(type), &blkstate->ct_id_key,
sizeof(blkstate->ct_id_key));
cte_record.ct_type = CTE_DEL;
struct backward_ct *bwdct = &cte_record.ctop.bwdct;
bwdct->srcdb = srcdb;
bwdct->dstdb = dstdb;
bwdct->blkpos = blkpos;
bwdct->sixlen = keylen;
bwdct->sixnum = sixnum;
bwdct->dixnum = dixnum;
bwdct->optype = optype;
bwdct->nonewrefs = nonewrefs;
bwdct->flags = flags;
memcpy(bwdct->key, inkey, keylen);
if (innewkey == NULL) {
memset(bwdct->newkey, 0, sizeof(bwdct->newkey));
} else {
/* always non-null in case of updates */
memcpy(bwdct->newkey, innewkey, keylen);
}
rc = bdb_temp_table_insert(thedb->bdb_env, cur, key,
sizeof(int) + sizeof(long long), &cte_record,
sizeof(cte), &err);
close_constraint_table_cursor(cur);
if (rc != 0)
return -1;
blkstate->ct_id_key++;
return 0;
}
static int should_skip_constraint_for_index(struct ireq *iq, int ixnum, int nulls)
{
return (nulls && (gbl_nullfkey || iq->usedb->ix_nullsallowed[ixnum]));
}
/* rec_dta is in .ONDISK format..we have it from 'delete' operation in block
* loop */
int check_delete_constraints(struct ireq *iq, void *trans,
block_state_t *blkstate, int op, void *rec_dta,
unsigned long long del_keys, int *errout)
{
return check_update_constraints(iq, trans, blkstate, op, rec_dta, NULL,
del_keys, errout);
}
int check_update_constraints(struct ireq *iq, void *trans,
block_state_t *blkstate, int op, void *rec_dta,
void *newrec_dta, unsigned long long del_keys,
int *errout)
{
int i = 0, rc = 0;
for (i = 0; i < iq->usedb->n_rev_constraints; i++) {
int j = 0;
constraint_t *cnstrt = iq->usedb->rev_constraints[i];
char rkey[MAXKEYLEN];
int rixnum = 0, rixlen = 0;
char rondisk_tag[MAXTAGLEN];
for (j = 0; j < cnstrt->nrules; j++) {
char ondisk_tag[MAXTAGLEN];
int ixnum = 0, ixlen = 0;
char lkey[MAXKEYLEN];
char nkey[MAXKEYLEN];
char rnkey[MAXKEYLEN];
int nornrefs = 0;
if (strcasecmp(cnstrt->table[j], iq->usedb->tablename)) {
continue;
}
rc = getidxnumbyname(cnstrt->table[j], cnstrt->keynm[j], &ixnum);
if (rc) {
if (iq->debug)
reqprintf(iq, "RTNKYCNSTRT: UNKNOWN KEYTAG %s",
cnstrt->keynm[j]);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_KEY,
"key constraint: unknown keytag '%s'",
cnstrt->keynm[j]);
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
return ERR_CONVERT_IX;
}
if (gbl_partial_indexes && iq->usedb->ix_partial &&
!(del_keys & (1ULL << ixnum))) {
continue;
}
ixlen = getkeysize(iq->usedb, ixnum);
snprintf(ondisk_tag, MAXTAGLEN, ".ONDISK_IX_%d", ixnum);
/*
rc=stag_to_stag_buf(iq->usedb->tablename, ".ONDISK",rec_dta,
ondisk_tag, lkey, NULL);
*/
if (iq->idxDelete)
rc = create_key_from_ireq(iq, ixnum, 1, NULL, NULL, NULL,
rec_dta, 0 /* not needed */, lkey);
else
rc = create_key_from_ondisk(
iq->usedb, ixnum, NULL, NULL, NULL, ".ONDISK", rec_dta,
0 /* not needed */, ondisk_tag, lkey, NULL, NULL);
if (rc == -1) {
if (iq->debug)
reqprintf(iq,
"RTNKYCNSTRT CANT FORM DST TBL %s INDEX %d (%s)",
iq->usedb->tablename, ixnum, cnstrt->keynm[j]);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_IDX,
"key constraint cannot form destination table '%s' "
"index %d (%s)",
iq->usedb->tablename, ixnum, cnstrt->keynm[j]);
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
return ERR_CONVERT_IX;
}
/* this part is for update */
if (newrec_dta != NULL) {
/*
rc=stag_to_stag_buf(iq->usedb->tablename, ".ONDISK",newrec_dta,
ondisk_tag, nkey, NULL);
*/
if (iq->idxInsert)
rc = create_key_from_ireq(iq, ixnum, 0, NULL, NULL, NULL,
newrec_dta, 0 /* not needed */,
nkey);
else
rc = create_key_from_ondisk(iq->usedb, ixnum, NULL, NULL,
NULL, ".ONDISK", newrec_dta,
0 /* not needed */, ondisk_tag,
nkey, NULL, NULL);
if (rc == -1) {
if (iq->debug)
reqprintf(
iq,
"RTNKYCNSTRT NEWDTA CANT FORM TBL %s INDEX %d (%s)",
iq->usedb->tablename, ixnum, cnstrt->keynm[j]);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_IDX,
"key constraint: new data cannot form table '%s' "
"index %d (%s)",
iq->usedb->tablename, ixnum, cnstrt->keynm[j]);
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
return ERR_CONVERT_IX;
}
/* if new key matches old key, we don't need to check this
delete constraint,
because we know that whatever records depend on this rule,
will still hold
after update */
if (!memcmp(lkey, nkey, ixlen)) {
if (iq->debug)
reqprintf(iq, "RTNKYCNSTRT SKIP CNSTRT CHECK DUE TO "
"SAME KEY DATA. TBL %s INDEX %d (%s)",
iq->usedb->tablename, ixnum,
cnstrt->keynm[j]);
continue;
}
}
/* here we convert the key into return db format */
rc = getidxnumbyname(cnstrt->lcltable->tablename,
cnstrt->lclkeyname, &rixnum);
if (rc) {
if (iq->debug)
reqprintf(iq, "RTNKYCNSTRT: UNKNOWN TABLE %s KEYTAG %s",
cnstrt->lcltable->tablename, cnstrt->lclkeyname);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_KEY,
"key constraint: unknown table '%s' keytag '%s'",
cnstrt->lcltable->tablename, cnstrt->lclkeyname);
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
return ERR_CONVERT_IX;
}
snprintf(rondisk_tag, MAXTAGLEN, ".ONDISK_IX_%d", rixnum);
int nulls = 0;
rixlen = rc = stag_to_stag_buf_ckey(
iq->usedb->tablename, ondisk_tag, lkey,
cnstrt->lcltable->tablename, rondisk_tag, rkey, &nulls, PK2FK);
if (rc == -1) {
#if 0
if (iq->debug)
reqprintf(iq,
"RTNKYCNSTRT CANT FORM SRC TBL %s INDEX %d (%s)",
cnstrt->lcltable->tablename, rixnum,cnstrt->lclkeyname);
*errout=OP_FAILED_INTERNAL + ERR_FORM_KEY;
return ERR_CONVERT_IX;
#endif
/* we cant form the key for source table/key pair in the
constraint.
This means that this record is not referenced..just let it
through!*/
if (iq->debug)
reqprintf(iq, "RTNKYCNSTRT SRC TBL %s INDEX %d (%s). "
"SKIPPING RULE CHECK.",
cnstrt->lcltable->tablename, rixnum,
cnstrt->lclkeyname);
continue; /* just move on, there should be nothing to check */
}
if (should_skip_constraint_for_index(iq, rixnum, nulls)) {
if (iq->debug)
reqprintf(iq, "RTNKYCNSTRT NULL COLUMN PREVENTS FOREIGN "
"REF %s INDEX %d (%s). SKIPPING RULE CHECK.",
cnstrt->lcltable->tablename, rixnum,
cnstrt->lclkeyname);
continue; /* just move on, there should be nothing to check */
}
if (cnstrt->lcltable->ix_collattr[rixnum]) {
rc = extract_decimal_quantum(cnstrt->lcltable, rixnum, rkey, NULL,
0, NULL);
if (rc) {
abort(); /* called doesn't return error for these arguments,
at least not now */
}
}
if (newrec_dta != NULL) {
/*
this conversion is for new key data, in case we'll need to
cascade
and update any other records on the way. this key is the
source of
new data for everyone else.
*/
rixlen = rc =
stag_to_stag_buf_ckey(iq->usedb->tablename, ondisk_tag,
nkey, cnstrt->lcltable->tablename,
rondisk_tag, rnkey, NULL, PK2FK);
if (rc == -1) {
/* same thing as for delete. If the key cannot be formed,
it is not a failure, no one's referencing us..so,
just add empty key to be verified recursively */
#if 1
nornrefs = 1;
/* something could be found, we'll postpone additional check
* until verify time */
if (iq->debug)
reqprintf(iq, "RTNKYCNSTRT CANT FORM NEW SRC TBL %s "
"INDEX %d (%s). PENDING RULE CHECK.",
cnstrt->lcltable->tablename, rixnum,
cnstrt->lclkeyname);
memcpy(rnkey, nkey, ixlen);
rixlen = ixlen;
#endif
#if 0
if (iq->debug)
reqprintf(iq,
"RTNKYCNSTRT CANT FORM NEW SRC TBL %s INDEX %d (%s)",
cnstrt->lcltable->tablename, rixnum,cnstrt->lclkeyname);
*errout=OP_FAILED_INTERNAL + ERR_FORM_KEY;
return ERR_CONVERT_IX;
#endif
} else if (cnstrt->lcltable->ix_collattr[rixnum]) {
rc = extract_decimal_quantum(cnstrt->lcltable, rixnum, rnkey,
NULL, 0, NULL);
if (rc) {
abort(); /* called doesn't return error for these
arguments, at least not now */
}
}
}
rc = insert_del_op(blkstate, cnstrt->lcltable, iq->usedb, op, 0,
rkey, (newrec_dta == NULL) ? NULL : rnkey,
rixlen, rixnum, ixnum, nornrefs, cnstrt->flags);
if (rc != 0) {
if (iq->debug)
reqprintf(iq, "RTNKYCNSTRT CANT MALLOC\n");
*errout = OP_FAILED_INTERNAL + ERR_INTERNAL;
return ERR_INTERNAL;
}
}
}
return 0;
}
/* FOR UPDATES/DELETES, MUST VERIFY AGAINST DELETED RECORD'S TABLE TO SEE IF
* THERE'RE ANY KEYS WITH SAME VALUE. IT IS OK TO DELETE IF THATS THE CASE */
int verify_del_constraints(struct javasp_trans_state *javasp_trans_handle,
struct ireq *iq, block_state_t *blkstate,
void *trans, blob_buffer_t *blobs, int *errout)
{
int rc = 0, fndrrn = 0, err = 0;
int keylen;
char key[MAXKEYLEN];
struct thread_info *thdinfo = pthread_getspecific(unique_tag_key);
if (thdinfo == NULL) {
if (iq->debug)
reqprintf(iq, "VERKYCNSTRT CANNOT GET DEL LIST CURSOR");
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_CURSOR,
"verify constraint cannot get del list cursor");
*errout = OP_FAILED_INTERNAL;
return ERR_INTERNAL;
}
void *cur = get_constraint_table_cursor(thdinfo->ct_del_table);
if (cur == NULL) {
if (iq->debug)
reqprintf(iq, "VERKYCNSTRT CANNOT GET DEL LIST CURSOR");
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_CURSOR,
"verify constraint cannot get del list cursor");
*errout = OP_FAILED_INTERNAL;
return ERR_INTERNAL;
}
rc = bdb_temp_table_first(thedb->bdb_env, cur, &err);
if (rc != 0) {
close_constraint_table_cursor(cur);
if (rc == IX_EMPTY) {
if (iq->debug)
reqprintf(iq,
"VERKYCNSTRT FOUND NOTHING TO VERIFY IN DEL LIST");
return 0;
}
if (iq->debug)
reqprintf(iq, "VERKYCNSTRT ERROR GETTING FINDING IN DEL LIST");
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_REC,
"verify key constraint error getting finding in del list");
*errout = OP_FAILED_INTERNAL;
return ERR_INTERNAL;
}
while (rc == 0) {
cte *ctrq = (cte *)bdb_temp_table_data(cur);
if (ctrq == NULL) {
if (iq->debug)
reqprintf(iq, "VERKYCNSTRT CANNOT GET DEL LIST RECORD DATA");
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_REC,
"verify key constraint cannot get del list record data");
*errout = OP_FAILED_INTERNAL;
close_constraint_table_cursor(cur);
return ERR_INTERNAL;
}
unsigned long long genid = 0LL, fndgenid = 0LL;
int rrn = 0;
int del_cascade = 0;
int upd_cascade = 0;
struct backward_ct *bct = &ctrq->ctop.bwdct;
struct dbtable *currdb = iq->usedb; /* make a copy */
char *skey = bct ? bct->key : "";
if (is_delete_op(bct->optype) && (bct->flags & CT_DEL_CASCADE))
del_cascade = 1;
else if (is_update_op(bct->optype) && (bct->flags & CT_UPD_CASCADE))
upd_cascade = 1;
/* verify against source table...must be not found */
iq->usedb = bct->srcdb;
rc = ix_find_by_key_tran(iq, skey, bct->sixlen, bct->sixnum, key, &rrn,
&genid, NULL, NULL, 0, trans);
iq->usedb = currdb;
if (rc == RC_INTERNAL_RETRY) {
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
close_constraint_table_cursor(cur);
return rc;
}
if (rc != IX_FND && rc != IX_FNDMORE) {
// key was not found on source table, so nothing to do
if (iq->debug) {
reqprintf(
iq,
"VERBKYCNSTRT VERIFIED TBL %s IX %d AGAINST TBL %s IX %d ",
bct->dstdb->tablename, bct->dixnum, bct->srcdb->tablename,
bct->sixnum);
reqdumphex(iq, bct->key, bct->sixlen);
reqmoref(iq, " RC %d", rc);
}
rc = bdb_temp_table_next(thedb->bdb_env, cur, &err);
continue;
}
/* Key was found, check the dependee (parent table) of the constraint.
* If we find another key there with the same value, then we dont need
* to do anything (if also key length and key are exactly the same).
* If we dont find key, we need to cascade the delete. */
char ondisk_tag[MAXTAGLEN], dondisk_tag[MAXTAGLEN];
char dkey[MAXKEYLEN];
if (bct->nonewrefs) {
if (iq->debug)
reqprintf(iq,
"VERBKYCNSTRT CANT FORM NEW DATA TBL %s "
"INDEX %d FROM %s INDEX %d ",
bct->dstdb->tablename, bct->dixnum,
bct->srcdb->tablename, bct->sixnum);
reqmoref(iq, " RC %d", rc);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_DTA,
"verify key constraint cannot form new data table "
"'%s' index %d from %s index %d ",
bct->dstdb->tablename, bct->dixnum, bct->srcdb->tablename,
bct->sixnum);
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
close_constraint_table_cursor(cur);
return ERR_CONVERT_IX;
}
snprintf(ondisk_tag, sizeof(ondisk_tag) - 1, ".ONDISK_IX_%d",
bct->sixnum);
snprintf(dondisk_tag, sizeof(dondisk_tag) - 1, ".ONDISK_IX_%d",
bct->dixnum);
int nullck = 0;
keylen = rc = stag_to_stag_buf_ckey(bct->srcdb->tablename, ondisk_tag,
skey, bct->dstdb->tablename,
dondisk_tag, dkey, &nullck, FK2PK);
if (rc == -1) {
if (iq->debug)
reqprintf(iq,
"VERBKYCNSTRT CANT FORM TBL %s INDEX %d FROM "
"%s INDEX %d KEY ",
bct->dstdb->tablename, bct->dixnum,
bct->srcdb->tablename, bct->sixnum);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_DTA,
"verify key constraint cannot form table '%s' index "
"%d from %s index %d key '%s",
bct->dstdb->tablename, bct->dixnum, bct->srcdb->tablename,
bct->sixnum,
get_keynm_from_db_idx(bct->srcdb, bct->sixnum));
reqdumphex(iq, skey, bct->sixlen);
reqmoref(iq, " RC %d", rc);
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
close_constraint_table_cursor(cur);
return ERR_CONVERT_IX;
}
/* Ignore records with null columns if nullfkey is set */
if (should_skip_constraint_for_index(iq, bct->sixnum, nullck)) {
if (iq->debug) {
reqprintf(iq,
"VERBKYCNSTRT NULL COLUMN PREVENTS FOREIGN "
"REF %s INDEX %d.",
bct->srcdb->tablename, bct->sixnum);
}
continue; /* TODO: why do we not get next? */
}
if (bct->dstdb->ix_collattr[bct->dixnum]) {
rc = extract_decimal_quantum(bct->dstdb, bct->dixnum, dkey, NULL, 0,
NULL);
if (rc) {
abort();
}
}
iq->usedb = bct->dstdb;
rc = ix_find_by_key_tran(iq, dkey, keylen, bct->dixnum, key, &fndrrn,
&fndgenid, NULL, NULL, 0, trans);
iq->usedb = currdb;
if (rc == RC_INTERNAL_RETRY) {
*errout = OP_FAILED_INTERNAL + ERR_FORM_KEY;
close_constraint_table_cursor(cur);
return rc;
}
/* key was found in parent tbl, no need to delete */
if (rc == IX_FND || rc == IX_FNDMORE) {
if (iq->debug) {
reqprintf(iq,
"VERBKYCNSTRT VERIFIED TBL %s IX %d AGAINST "
"TBL %s IX %d ",
bct->dstdb->tablename, bct->dixnum,
bct->srcdb->tablename, bct->sixnum);
reqdumphex(iq, bct->key, bct->sixlen);
reqmoref(iq, " RC %d", rc);
}
rc = bdb_temp_table_next(thedb->bdb_env, cur, &err);
continue;
}
/* key was not found in parent tbl, will need to delete from this tbl */
if (del_cascade) {
/* do cascade logic here */
int err = 0, idx = 0;
if (iq->debug) {
reqprintf(iq, "VERBKYCNSTRT CASCADE DELETE TBL %s RRN %d ",
bct->srcdb->tablename, rrn);
}
iq->usedb = bct->srcdb;
if (iq->debug)
reqpushprefixf(iq, "VERBKYCNSTRT CASCADE DEL:");
/* TODO verify we have proper schema change locks */
rc = del_record(iq, trans, NULL, rrn, genid, -1ULL, &err, &idx,
BLOCK2_DELKL, RECFLAGS_DONT_LOCK_TBL);
if (iq->debug)
reqpopprefixes(iq, 1);
iq->usedb = currdb;
if (rc != 0) {
if (iq->debug) {
reqprintf(iq,
"VERBKYCNSTRT CANT CASCADE DELETE "
"TBL %s RRN %d RC %d ",
bct->srcdb->tablename, rrn, rc);
}
if (rc == ERR_TRAN_TOO_BIG) {
reqerrstr(iq, COMDB2_CSTRT_RC_CASCADE,
"cascaded delete exceeds max writes");
*errout = OP_FAILED_INTERNAL + ERR_TRAN_TOO_BIG;
} else {
reqerrstr(iq, COMDB2_CSTRT_RC_CASCADE,
"verify key constraint cannot cascade delete "
"table '%s' rc %d",
bct->srcdb->tablename, rc);
*errout = OP_FAILED_INTERNAL + ERR_FIND_CONSTRAINT;
}
close_constraint_table_cursor(cur);
return rc;
}
/* here, we need to retry to verify the constraint */
/* sub 1 to go to current constraint again */
continue;
} else if (upd_cascade) {
int err = 0, idx = 0;
unsigned long long newgenid;
int newkeylen;
iq->usedb = bct->srcdb;
newkeylen = getkeysize(iq->usedb, bct->sixnum);
if (gbl_fk_allow_superset_keys && newkeylen > bct->sixlen) {
memcpy(bct->newkey + bct->sixlen, key + bct->sixlen,
newkeylen - bct->sixlen);
} else {
newkeylen = bct->sixlen;
}
if (iq->debug)
reqpushprefixf(iq, "VERBKYCNSTRT CASCADE UPD:");
/* TODO verify we have proper schema change locks */
rc = upd_record(
iq, trans, NULL, /*primkey*/
rrn, genid, (const unsigned char *)ondisk_tag, /*.ONDISK_IX_0*/
(const unsigned char *)ondisk_tag + strlen(ondisk_tag),
(unsigned char *)bct->newkey, /*p_buf_rec*/
(const unsigned char *)bct->newkey + newkeylen,
NULL /*p_buf_vrec*/, NULL /*p_buf_vrec_end*/,
NULL, /*fldnullmap*/
NULL, /*updCols*/
NULL, /*blobs*/
0, /*maxblobs*/
&newgenid, -1ULL, -1ULL, &err, &idx, BLOCK2_UPDKL, 0, /*blkpos*/
UPDFLAGS_CASCADE | RECFLAGS_DONT_LOCK_TBL);
if (iq->debug)
reqpopprefixes(iq, 1);
iq->usedb = currdb;
if (rc != 0) {
if (iq->debug) {
reqprintf(iq,
"VERBKYCNSTRT CANT CASCADE UPDATE "
"TBL %s RRN %d RC %d ",
bct->srcdb->tablename, rrn, rc);
}
if (rc == ERR_TRAN_TOO_BIG) {
reqerrstr(iq, COMDB2_CSTRT_RC_CASCADE,
"cascaded update exceeds max writes");
*errout = OP_FAILED_INTERNAL + ERR_TRAN_TOO_BIG;
} else {
reqerrstr(iq, COMDB2_CSTRT_RC_CASCADE,
"verify key constraint cannot cascade update "
"table '%s' rc %d",
bct->srcdb->tablename, rc);
*errout = OP_FAILED_INTERNAL + ERR_FIND_CONSTRAINT;
}
close_constraint_table_cursor(cur);
return rc;
}
/* here, we need to retry to verify the constraint */
continue;
} else { /* key was not found in parent tbl and we are not cascading */
if (iq->debug) {
reqprintf(iq,
"VERBKYCNSTRT CANT RESOLVE CONSTRAINT TBL %s "
"IDX '%d' KEY -> TBL %s IDX '%d' ",
bct->dstdb->tablename, bct->dixnum,
bct->srcdb->tablename, bct->sixnum);
reqdumphex(iq, bct->key, bct->sixlen);
reqmoref(iq, " RC %d", rc);
}
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_KEY,
"verify key constraint cannot resolve constraint "
"table '%s' index '%d' key '%s' -> table '%s' index "
"'%d' ",
bct->dstdb->tablename, bct->dixnum,
get_keynm_from_db_idx(bct->dstdb, bct->dixnum),
bct->srcdb->tablename, bct->sixnum);
*errout = OP_FAILED_INTERNAL + ERR_FIND_CONSTRAINT;
close_constraint_table_cursor(cur);
return ERR_BADREQ;
}
/* get next record from table */
rc = bdb_temp_table_next(thedb->bdb_env, cur, &err);
}
close_constraint_table_cursor(cur);
if (rc == IX_EMPTY || rc == IX_PASTEOF) {
return 0;
}
if (iq->debug)
reqprintf(iq, "VERKYCNSTRT ERROR READING DEL TABLE");
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_TBL,
"verify key constraint error reading del table");
*errout = OP_FAILED_INTERNAL;
return ERR_INTERNAL;
}
/* this is called twice so putting here to avoid mess */
#define LIVE_SC_DELAYED_KEY_ADDS(LAST) \
do { \
/* its ok to fail adding to newbtree indices -- SC will abort */ \
int lrc = live_sc_delayed_key_adds(iq, trans, genid, od_dta, ins_keys, \
ondisk_size); \
if (lrc == RC_INTERNAL_RETRY) { \
logmsg(LOGMSG_ERROR, "%s: deadlock add2idx genid 0x%llx\n", \
__func__, genid); \
/* if we failed to add due to deadlock, need to redo */ \
*errout = OP_FAILED_INTERNAL; \
if (!LAST) \
close_constraint_table_cursor(cur); \
return lrc; \
} else if (lrc == ERR_NOMASTER) { \
logmsg(LOGMSG_ERROR, "%s:%d: live sc downgrading\n", __func__, \
__LINE__); \
return ERR_NOMASTER; \
} else if (lrc) \
logmsg(LOGMSG_USER, \
"%s:%d: ERROR: failed add2idx rc %d genid 0x%llx\n", \
__func__, __LINE__, lrc, genid); \
} while (0);
int delayed_key_adds(struct ireq *iq, block_state_t *blkstate, void *trans,
int *blkpos, int *ixout, int *errout)
{
int rc = 0, fndlen = 0, err = 0, limit = 0;
int idx = 0, ixkeylen = -1;
void *od_dta = NULL;
char ondisk_tag[MAXTAGLEN];
char key[MAXKEYLEN];
char *od_dta_tail = NULL;
int od_tail_len = 0;
char mangled_key[MAXKEYLEN];
od_dta = alloca(20 * 1024 + 8);
if (od_dta == NULL) {
if (iq->debug)
reqprintf(iq, "%p:ADDKYCNSTRT FAILED MALLOC", trans);
reqerrstr(iq, COMDB2_CSTRT_RC_ALLOC,
"add key constraint failed malloc");
*errout = OP_FAILED_INTERNAL;
*blkpos = 0;
return ERR_INTERNAL;
}
struct thread_info *thdinfo = pthread_getspecific(unique_tag_key);
if (thdinfo == NULL) {
if (iq->debug)
reqprintf(iq, "%p:VERKYCNSTRT CANNOT GET ADD LIST CURSOR", trans);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_CURSOR,
"verify key constraint: cannot get add list cursor");
*errout = OP_FAILED_INTERNAL;
return ERR_INTERNAL;
}
void *cur = get_constraint_table_cursor(thdinfo->ct_add_table);
if (cur == NULL) {
if (iq->debug)
reqprintf(iq, "%p:VERKYCNSTRT CANNOT GET ADD LIST CURSOR", trans);
reqerrstr(iq, COMDB2_CSTRT_RC_INVL_CURSOR,
"verify key constraint cannot get add list cursor");
*errout = OP_FAILED_INTERNAL;
return ERR_INTERNAL;
}
*ixout = -1;
*blkpos = -1;
int ondisk_size = 0;
unsigned long long genid = 0LL;
unsigned long long cached_index_genid = genid;
unsigned long long ins_keys = 0ULL;
int flags = 0;
rc = bdb_temp_table_first(thedb->bdb_env, cur, &err);
if (rc != 0) {
close_constraint_table_cursor(cur);