forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglue.c
6243 lines (5534 loc) · 206 KB
/
glue.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, 2017, 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.
*/
/* glue front end to db engine */
/* any transactional code needs to return RETRY rcode to upper levels.
This is because the transaction needs to abort, and start over again.
non-transactional can retry within glue code.
*/
#include "limit_fortify.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <inttypes.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <poll.h>
#include <unistd.h>
#include <gettimeofday_ms.h>
#include <ctrace.h>
#include <epochlib.h>
#include <str0.h>
#include <pthread.h>
#include <netinet/in.h>
#include <build/db.h>
#include <portmuxapi.h>
#include <bb_oscompat.h>
#include <list.h>
#include <memory_sync.h>
#include "comdb2.h"
#include "translistener.h"
#include "prefault.h"
#include "util.h"
#include "sql.h"
#include <sbuf2.h>
#include <bdb_api.h>
#include <bdb_cursor.h>
#include <bdb_fetch.h>
#include <bdb_queue.h>
#include <net.h>
#include <net_types.h>
#include "remote.h"
#include <cdb2api.h>
#include <dlmalloc.h>
#include "sqloffload.h"
#include "osqlcomm.h"
#include <flibc.h>
#include <cdb2_constants.h>
#include <autoanalyze.h>
#include "util.h"
#include <schemachange/sc_global.h>
#include "rtcpu.h"
#include <intern_strings.h>
#include "debug_switches.h"
#include <trigger.h>
#include "views.h"
#include <sc_callbacks.h>
#include "views.h"
#include "logmsg.h"
int (*comdb2_ipc_master_set)(char *host) = 0;
/* ixrc != -1 is incorrect. Could be IX_PASTEOF or IX_EMPTY.
* Don't want to vtag those results
*
* Ha! Dont need IX_EMPTY but do need IX_NOTFND and IX_PASTEOF.
* Just use is_good_ix_find_rc() */
#define VTAG(rc, db) \
if (is_good_ix_find_rc((rc))) \
vtag_to_ondisk((db), fnddta, fndlen, args.ver, *genid)
#define VTAG_GENID(rc, db) \
if (is_good_ix_find_rc((rc))) \
vtag_to_ondisk((db), fnddta, fndlen, args.ver, genid)
#define VTAG_PTR(rc, db) \
if (is_good_ix_find_rc((rc))) \
vtag_to_ondisk((db), fnddta, fndlen, args->ver, *genid)
#define VTAG_PTR_GENID(rc, db) \
if (is_good_ix_find_rc((rc))) \
vtag_to_ondisk((db), fnddta, fndlen, args->ver, genid)
extern int verbose_deadlocks;
struct net_new_queue_msg {
bbuint32_t reserved;
bbuint32_t avgitemsz;
char name[MAXTABLELEN + 1];
};
struct net_add_consumer_msg {
bbuint32_t reserved;
bbuint32_t consumern;
char name[MAXTABLELEN + 1];
char method[128];
};
struct new_procedure_op_msg {
bbuint32_t reserved;
bbuint32_t op;
bbuint32_t namelen;
bbuint32_t jarfilelen;
bbuint32_t paramlen;
char text[1];
};
struct net_morestripe_msg {
int32_t reserved0;
int32_t reserved1;
int32_t newdtastripe;
int32_t newblobstripe;
};
extern struct dbenv *thedb;
extern int gbl_lost_master_time;
extern int gbl_check_access_controls;
static int meta_put(struct dbtable *db, void *input_tran, struct metahdr *hdr,
void *data, int dtalen);
static int meta_get(struct dbtable *db, struct metahdr *key, void *dta, int dtalen);
static int meta_get_tran(tran_type *tran, struct dbtable *db, struct metahdr *key1,
void *dta, int dtalen);
static int meta_get_var(struct dbtable *db, struct metahdr *key, void **dta,
int *fndlen);
static int meta_get_var_tran(tran_type *tran, struct dbtable *db,
struct metahdr *key1, void **dta, int *fndlen);
static int put_meta_int(const char *table, void *tran, int rrn, int key,
int value);
static int get_meta_int(const char *table, int rrn, int key);
static int get_meta_int_tran(tran_type *tran, const char *table, int rrn,
int key);
static int ix_find_check_blob_race(struct ireq *iq, char *inbuf, int numblobs,
int *blobnums, void **blobptrs);
/* How many times we became, or ceased to be, master node. */
int gbl_master_changes = 0;
void *get_bdb_handle(struct dbtable *db, int auxdb)
{
void *bdb_handle;
switch (auxdb) {
case AUXDB_NONE:
bdb_handle = db->handle;
break;
case AUXDB_META:
if (!db->meta && db->dbenv->meta)
bdb_handle = db->dbenv->meta;
else
bdb_handle = db->meta;
break;
default:
logmsg(LOGMSG_ERROR, "get_bdb_handle: bad auxdb=%d\n", auxdb);
bdb_handle = NULL;
}
return bdb_handle;
}
static void *get_bdb_handle_ireq(struct ireq *iq, int auxdb)
{
void *bdb_handle = NULL;
if (iq->usedb) {
if (auxdb == AUXDB_NONE)
reqlog_usetable(iq->reqlogger, iq->usedb->tablename);
return get_bdb_handle(iq->usedb, auxdb);
}
switch (auxdb) {
default:
logmsg(LOGMSG_ERROR, "get_bdb_handle_ireq: bad auxdb=%d\n", auxdb);
bdb_handle = NULL;
}
return bdb_handle;
}
static void *bdb_handle_from_ireq(const struct ireq *iq)
{
struct dbtable *db = iq->usedb;
if (db)
return db->handle;
else if (iq->use_handle)
return iq->use_handle;
else {
logmsg(LOGMSG_FATAL, "bdb_handle_from_ireq: ireq has no bdb handle\n");
abort();
return NULL;
}
}
static struct dbenv *dbenv_from_ireq(const struct ireq *iq)
{
struct dbtable *db = iq->usedb;
if (db)
return db->dbenv;
else
return iq->dbenv;
}
void init_fake_ireq_auxdb(struct dbenv *dbenv, struct ireq *iq, int auxdb)
{
memset(iq, 0, sizeof(struct ireq));
iq->transflags = 0;
iq->is_fake = 1;
iq->dbenv = dbenv;
iq->use_handle = get_bdb_handle_ireq(iq, auxdb);
}
void init_fake_ireq(struct dbenv *dbenv, struct ireq *iq)
{
/* region 1 */
const size_t len1 = offsetof(struct ireq, region2);
bzero(iq, len1);
/* region 2 */
iq->corigin[0] = '\0';
iq->debug_buf[0] = '\0';
iq->tzname[0] = '\0';
iq->sqlhistory[0] = '\0';
/* region 3 */
const size_t len3 = sizeof(*iq) - offsetof(struct ireq, region3);
bzero(&iq->region3, len3);
/* Make it fake */
iq->dbenv = dbenv;
iq->use_handle = dbenv->bdb_env;
iq->is_fake = 1;
iq->helper_thread = -1;
}
int set_tran_lowpri(struct ireq *iq, tran_type *tran)
{
void *bdb_handle = bdb_handle_from_ireq(iq);
return bdb_set_tran_lowpri(bdb_handle, tran);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* TRANSACTIONAL STUFF */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
static int trans_start_int_int(struct ireq *iq, tran_type *parent_trans,
tran_type **out_trans, int logical, int sc,
int retries)
{
int bdberr;
void *bdb_handle = bdb_handle_from_ireq(iq);
int rc = 0;
tran_type *physical_tran = NULL;
iq->gluewhere = "bdb_tran_begin";
if (!logical) {
/*
if (retries)
fprintf(stderr, "bdb_tran_begin_set_retries(%d)\n", retries);
*/
*out_trans = bdb_tran_begin_set_retries(bdb_handle, parent_trans,
retries, &bdberr);
} else {
*out_trans = bdb_tran_begin_logical(bdb_handle, 0, &bdberr);
if (iq->tranddl && sc && *out_trans) {
bdb_ltran_get_schema_lock(*out_trans);
int get_physical_transaction(
bdb_state_type * bdb_state, tran_type * logical_tran,
tran_type * *outtran, int force_commit);
rc = get_physical_transaction(bdb_handle, *out_trans,
&physical_tran, 0);
if (rc) {
trans_abort_logical(iq, *out_trans, NULL, 0, NULL, 0);
*out_trans = NULL;
bdberr = rc;
}
}
}
iq->gluewhere = "bdb_tran_begin done";
if (*out_trans == 0) {
/* dbenv->master can change between calling
* bdb_tran_begin and checking it here - in fact, we may get
* upgraded to master in between! ERR_NOMASTER will make the
* proxy retry next second, so that is the simplest fix here.
* Once we're inside a transaction we hold the bdb read lock
* until we've committed or aborted so no need to worry about this
* later on. */
/* struct dbenv *dbenv = dbenv_from_ireq(iq); */
if (bdberr == BDBERR_READONLY /*&& dbenv->master!=gbl_mynode*/) {
/* return NOMASTER so client retries. */
return ERR_NOMASTER;
}
logmsg(LOGMSG_ERROR, "*ERROR* trans_start:failed err %d\n", bdberr);
return ERR_INTERNAL;
}
return 0;
}
int trans_start_int(struct ireq *iq, void *parent_trans, tran_type **out_trans,
int logical, int retries)
{
return trans_start_int_int(iq, parent_trans, out_trans, logical, 0,
retries);
}
int trans_start_logical_sc(struct ireq *iq, tran_type **out_trans)
{
iq->use_handle = thedb->bdb_env;
return trans_start_int_int(iq, NULL, out_trans, 1, 1, 0);
}
int trans_start_logical(struct ireq *iq, tran_type **out_trans)
{
return trans_start_int(iq, NULL, out_trans, 1, 0);
}
int rowlocks_check_commit_physical(bdb_state_type *bdb_state, tran_type *tran,
int blockop_count)
{
return bdb_rowlocks_check_commit_physical(bdb_state, tran, blockop_count);
}
int is_rowlocks_transaction(tran_type *tran)
{
return bdb_is_rowlocks_transaction(tran);
}
int trans_start(struct ireq *iq, tran_type *parent_trans, tran_type **out_trans)
{
if (gbl_rowlocks)
return trans_start_logical(iq, out_trans);
else
return trans_start_int(iq, parent_trans, out_trans, 0, 0);
}
int trans_start_sc(struct ireq *iq, tran_type *parent_trans,
tran_type **out_trans)
{
return trans_start_int(iq, parent_trans, out_trans, 0, 0);
}
int trans_start_set_retries(struct ireq *iq, tran_type *parent_trans,
tran_type **out_trans, int retries)
{
int rc = 0;
if (gbl_rowlocks)
rc = trans_start_logical(iq, out_trans);
else
rc = trans_start_int(iq, parent_trans, out_trans, 0, retries);
if (verbose_deadlocks && retries != 0)
logmsg(LOGMSG_USER, "%s ptran %p tran %p with retries %d\n", __func__,
parent_trans, *out_trans, retries);
return rc;
}
tran_type *trans_start_socksql(struct ireq *iq, int trak)
{
void *bdb_handle = bdb_handle_from_ireq(iq);
tran_type *out_trans = NULL;
int bdberr = 0;
iq->gluewhere = "bdb_tran_begin_socksql";
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "td=%x %s called\n", (int)pthread_self(), __func__);
}
out_trans = bdb_tran_begin_socksql(bdb_handle, trak, &bdberr);
iq->gluewhere = "bdb_tran_begin_socksql done";
if (out_trans == NULL) {
logmsg(LOGMSG_ERROR, "*ERROR* %s:failed err %d\n", __func__, bdberr);
return NULL;
}
return out_trans;
}
tran_type *trans_start_readcommitted(struct ireq *iq, int trak)
{
void *bdb_handle = bdb_handle_from_ireq(iq);
tran_type *out_trans = NULL;
int bdberr = 0;
iq->gluewhere = "bdb_tran_begin_readcommitted";
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "td=%x %s called\n", (int)pthread_self(), __func__);
}
out_trans = bdb_tran_begin_readcommitted(bdb_handle, trak, &bdberr);
iq->gluewhere = "bdb_tran_begin_readcommitted done";
if (out_trans == NULL) {
logmsg(LOGMSG_ERROR, "*ERROR* %s:failed err %d\n", __func__, bdberr);
return NULL;
}
return out_trans;
}
tran_type *trans_start_snapisol(struct ireq *iq, int trak, int epoch, int file,
int offset, int *error, int is_ha_retry)
{
void *bdb_handle = bdb_handle_from_ireq(iq);
tran_type *out_trans = NULL;
*error = 0;
iq->gluewhere = "bdb_tran_begin_snapisol";
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "td=%x %s called with epoch=%d file=%d offset=%d\n",
(int)pthread_self(), __func__, epoch, file, offset);
}
out_trans = bdb_tran_begin_snapisol(bdb_handle, trak, error, epoch, file,
offset, is_ha_retry);
iq->gluewhere = "bdb_tran_begin_snapisol done";
if (out_trans == NULL) {
logmsg(LOGMSG_ERROR, "*ERROR* %s:failed err %d\n", __func__, *error);
return NULL;
}
return out_trans;
}
tran_type *trans_start_serializable(struct ireq *iq, int trak, int epoch,
int file, int offset, int *error,
int is_ha_retry)
{
void *bdb_handle = bdb_handle_from_ireq(iq);
tran_type *out_trans = NULL;
int bdberr = 0;
iq->gluewhere = "bdb_tran_begin";
if (gbl_extended_sql_debug_trace) {
logmsg(LOGMSG_USER, "td=%x %s called with epoch=%d file=%d offset=%d\n",
(int)pthread_self(), __func__, epoch, file, offset);
}
out_trans = bdb_tran_begin_serializable(bdb_handle, trak, &bdberr, epoch,
file, offset, is_ha_retry);
iq->gluewhere = "bdb_tran_begin done";
if (out_trans == NULL) {
logmsg(LOGMSG_ERROR, "*ERROR* %s:failed err %d\n", __func__, bdberr);
*error = bdberr;
return NULL;
}
return out_trans;
}
/**
* Shadow transaction have no berkdb txn and executes (most of the time)
* on replicants;
* There is nothing to wait after.
* I also prefer to pass the bdberr on the higher levels
*
*/
int trans_commit_shadow(void *trans, int *bdberr)
{
int rc = 0;
;
*bdberr = 0;
rc = bdb_tran_commit(thedb->bdb_env, trans, bdberr);
return rc;
}
/**
* Shadow transaction have no berkdb txn and executes (most of the time)
* on replicants;
* There is nothing to wait after.
* I also prefer to pass the bdberr on the higher levels
*
*/
int trans_abort_shadow(void **trans, int *bdberr)
{
int rc = 0;
if (*trans == NULL)
return rc;
*bdberr = 0;
rc = bdb_tran_abort(thedb->bdb_env, *trans, bdberr);
*trans = NULL;
return rc;
}
static int trans_commit_seqnum_int(void *bdb_handle, struct dbenv *dbenv,
struct ireq *iq, void *trans,
db_seqnum_type *seqnum, int logical,
void *blkseq, int blklen, void *blkkey,
int blkkeylen)
{
int bdberr;
iq->gluewhere = "bdb_tran_commit_with_seqnum_size";
if (!logical)
bdb_tran_commit_with_seqnum_size(
bdb_handle, trans, (seqnum_type *)seqnum, &iq->txnsize, &bdberr);
else {
bdb_tran_commit_logical_with_seqnum_size(
bdb_handle, trans, blkseq, blklen, blkkey, blkkeylen,
(seqnum_type *)seqnum, &iq->txnsize, &bdberr);
}
iq->gluewhere = "bdb_tran_commit_with_seqnum_size done";
if (bdberr != 0) {
if (bdberr == BDBERR_DEADLOCK)
return RC_INTERNAL_RETRY;
if (bdberr == BDBERR_READONLY) {
/* I was downgraded in the middle..
return NOMASTER so client retries. */
return ERR_NOMASTER;
} else if (logical && bdberr == BDBERR_ADD_DUPE) {
/*
bdb_tran_commit_logical_with_seqnum_size takes care of aborting
the transaction. I hate this. We've operated under the
assumption that commits never fail. Unfortunately
with logical transactions we don't have nesting, so we can't
have a parent that commits and a child that aborts. It's
ugly, but we need to live with it. Ideas? I am all ears.
*/
return IX_DUP;
}
logmsg(LOGMSG_ERROR, "*ERROR* trans_commit:failed err %d\n", bdberr);
return ERR_INTERNAL;
}
return 0;
}
int trans_commit_seqnum(struct ireq *iq, void *trans, db_seqnum_type *seqnum)
{
void *bdb_handle = bdb_handle_from_ireq(iq);
struct dbenv *dbenv = dbenv_from_ireq(iq);
return trans_commit_seqnum_int(bdb_handle, dbenv, iq, trans, seqnum, 0,
NULL, 0, NULL, 0);
}
static const char *sync_to_str(int sync)
{
switch (sync) {
case REP_SYNC_FULL:
return "SYNC_FULL";
break;
case REP_SYNC_SOURCE:
return "SYNC_SOURCE";
break;
case REP_SYNC_NONE:
return "SYNC_NONE";
break;
case REP_SYNC_ROOM:
return "SYNC_ROOM";
break;
case REP_SYNC_N:
return "SYNC_N";
break;
default:
return "INVALID";
break;
}
}
static int trans_wait_for_seqnum_int(void *bdb_handle, struct dbenv *dbenv,
struct ireq *iq, char *source_node,
int timeoutms, int adaptive,
db_seqnum_type *ss)
{
int rc = 0;
int sync;
int start_ms, end_ms;
if (iq->sc_pending) {
sync = REP_SYNC_FULL;
adaptive = 0;
timeoutms = -1;
} else {
sync = dbenv->rep_sync;
}
/*wait for synchronization, if necessary */
start_ms = comdb2_time_epochms();
switch (sync) {
default:
/*async mode, don't wait at all */
break;
case REP_SYNC_SOURCE:
/*source machine sync, wait for source machine */
if (source_node == gbl_mynode)
break;
iq->gluewhere = "bdb_wait_for_seqnum_from_node";
if (timeoutms == -1)
rc = bdb_wait_for_seqnum_from_node(bdb_handle, (seqnum_type *)ss,
source_node);
else
rc = bdb_wait_for_seqnum_from_node_timeout(
bdb_handle, (seqnum_type *)ss, source_node, timeoutms);
iq->gluewhere = "bdb_wait_for_seqnum_from_node done";
if (rc != 0) {
logmsg(LOGMSG_ERROR, "*WARNING* bdb_wait_seqnum:error syncing node %s rc %d\n",
source_node, rc);
}
break;
case REP_SYNC_FULL:
iq->gluewhere = "bdb_wait_for_seqnum_from_all";
if (adaptive)
rc = bdb_wait_for_seqnum_from_all_adaptive_newcoh(
bdb_handle, (seqnum_type *)ss, iq->txnsize, &iq->timeoutms);
else if (timeoutms == -1)
rc = bdb_wait_for_seqnum_from_all(bdb_handle, (seqnum_type *)ss);
else
rc = bdb_wait_for_seqnum_from_all_timeout(
bdb_handle, (seqnum_type *)ss, timeoutms);
iq->gluewhere = "bdb_wait_for_seqnum_from_all done";
if (rc != 0) {
logmsg(LOGMSG_ERROR, "*WARNING* bdb_wait_seqnum:error syncing all nodes rc %d\n",
rc);
}
if (iq->sc_pending) {
/* TODO: I dont know what to do here. Schema change is already
** commited but one or more replicants didn't get the messages
** to reload table.
*/
logmsg(LOGMSG_INFO, "Schema change scdone sync all nodes, rc %d\n",
rc);
rc = 0;
}
break;
case REP_SYNC_ROOM:
iq->gluewhere = "bdb_wait_for_seqnum_from_room";
rc = bdb_wait_for_seqnum_from_room(bdb_handle, (seqnum_type *)ss);
iq->gluewhere = "bdb_wait_for_seqnum_from_room done";
if (rc != 0) {
logmsg(LOGMSG_ERROR, "*WARNING* bdb_wait_seqnum:error syncing all nodes rc %d\n",
rc);
}
break;
case REP_SYNC_N:
rc = bdb_wait_for_seqnum_from_n(bdb_handle, (seqnum_type *)ss,
thedb->wait_for_N_nodes);
break;
}
if (bdb_attr_get(dbenv->bdb_attr, BDB_ATTR_COHERENCY_LEASE)) {
uint64_t now = gettimeofday_ms(), next_commit = next_commit_timestamp();
if (next_commit > now)
poll(0, 0, next_commit - now);
}
end_ms = comdb2_time_epochms();
iq->reptimems = end_ms - start_ms;
return rc;
}
int trans_wait_for_seqnum(struct ireq *iq, char *source_host,
db_seqnum_type *ss)
{
void *bdb_handle = bdb_handle_from_ireq(iq);
struct dbenv *dbenv = dbenv_from_ireq(iq);
return trans_wait_for_seqnum_int(bdb_handle, dbenv, iq, source_host, -1,
0 /*adaptive*/, ss);
}
int trans_wait_for_last_seqnum(struct ireq *iq, char *source_host)
{
db_seqnum_type seqnum;
void *bdb_handle = bdb_handle_from_ireq(iq);
struct dbenv *dbenv = dbenv_from_ireq(iq);
bdb_get_myseqnum(bdb_handle, (void *)&seqnum);
return trans_wait_for_seqnum_int(bdb_handle, dbenv, iq, source_host, -1,
0 /*adaptive*/, &seqnum);
}
int trans_commit_logical_tran(void *trans, int *bdberr)
{
uint64_t size;
char seq[SIZEOF_SEQNUM];
return bdb_tran_commit_logical_with_seqnum_size(
thedb->bdb_env, trans, NULL, 0, NULL, 0, (seqnum_type *)seq, &size,
bdberr);
}
static int trans_commit_int(struct ireq *iq, void *trans, char *source_host,
int timeoutms, int adaptive, int logical,
void *blkseq, int blklen, void *blkkey,
int blkkeylen)
{
int rc;
db_seqnum_type ss;
char *cnonce = NULL;
int cn_len;
void *bdb_handle = bdb_handle_from_ireq(iq);
struct dbenv *dbenv = dbenv_from_ireq(iq);
memset(&ss, -1, sizeof(ss));
rc = trans_commit_seqnum_int(bdb_handle, dbenv, iq, trans, &ss, logical,
blkseq, blklen, blkkey, blkkeylen);
if (gbl_extended_sql_debug_trace && iq->have_snap_info) {
cn_len = iq->snap_info.keylen;
cnonce = alloca(cn_len + 1);
memcpy(cnonce, iq->snap_info.key, cn_len);
cnonce[cn_len] = '\0';
logmsg(LOGMSG_USER, "%s %s line %d: trans_commit returns %d\n", cnonce,
__func__, __LINE__, rc);
}
if (rc != 0) {
return rc;
}
rc = trans_wait_for_seqnum_int(bdb_handle, dbenv, iq, source_host,
timeoutms, adaptive, &ss);
if (cnonce) {
DB_LSN *lsn = (DB_LSN *)&ss;
logmsg(LOGMSG_USER,
"%s %s line %d: wait_for_seqnum [%d][%d] returns %d\n", cnonce,
__func__, __LINE__, lsn->file, lsn->offset, rc);
}
return rc;
}
int trans_commit_logical(struct ireq *iq, void *trans, char *source_host,
int timeoutms, int adaptive, void *blkseq, int blklen,
void *blkkey, int blkkeylen)
{
return trans_commit_int(iq, trans, source_host, timeoutms, adaptive, 1,
blkseq, blklen, blkkey, blkkeylen);
}
/* XXX i made this be the same as trans_commit_adaptive */
int trans_commit(struct ireq *iq, void *trans, char *source_host)
{
return trans_commit_int(iq, trans, source_host, -1, 1, 0, NULL, 0, NULL, 0);
}
int trans_commit_timeout(struct ireq *iq, void *trans, char *source_host,
int timeoutms)
{
return trans_commit_int(iq, trans, source_host, timeoutms, 0, 0, NULL, 0,
NULL, 0);
}
int trans_commit_adaptive(struct ireq *iq, void *trans, char *source_host)
{
return trans_commit_int(iq, trans, source_host, -1, 1, 0, NULL, 0, NULL, 0);
}
int trans_abort_logical(struct ireq *iq, void *trans, void *blkseq, int blklen,
void *seqkey, int seqkeylen)
{
int bdberr, rc = 0;
void *bdb_handle = bdb_handle_from_ireq(iq);
struct dbenv *dbenv = dbenv_from_ireq(iq);
db_seqnum_type ss;
iq->gluewhere = "bdb_tran_abort";
bdb_tran_abort_logical(bdb_handle, trans, &bdberr, blkseq, blklen, seqkey,
seqkeylen, (seqnum_type *)&ss);
iq->gluewhere = "bdb_tran_abort done";
if (bdberr != 0) {
if (bdberr == BDBERR_ADD_DUPE)
rc = IX_DUP;
else if (bdberr != BDBERR_DEADLOCK)
logmsg(LOGMSG_ERROR, "*ERROR* trans_abort_logical:failed err %d\n", bdberr);
else
rc = ERR_INTERNAL;
}
/* Single phy-txn logical aborts will set ss to 0: check before waiting */
u_int32_t *file = (u_int32_t *)&ss;
if (*file != 0) {
trans_wait_for_seqnum_int(bdb_handle, dbenv, iq, gbl_mynode,
-1 /* timeoutms */, 1 /* adaptive */, &ss);
}
return rc;
}
int trans_abort_int(struct ireq *iq, void *trans, int *priority)
{
int bdberr;
void *bdb_handle = bdb_handle_from_ireq(iq);
iq->gluewhere = "bdb_tran_abort";
bdb_tran_abort_priority(bdb_handle, trans, &bdberr, priority);
iq->gluewhere = "bdb_tran_abort done";
if (bdberr != 0) {
logmsg(LOGMSG_ERROR, "*ERROR* trans_abort:failed err %d\n", bdberr);
return ERR_INTERNAL;
}
return 0;
}
int trans_abort_priority(struct ireq *iq, void *trans, int *priority)
{
return trans_abort_int(iq, trans, priority);
}
int trans_abort(struct ireq *iq, void *trans)
{
return trans_abort_int(iq, trans, NULL);
}
int get_context(struct ireq *iq, unsigned long long *context)
{
struct dbtable *db = iq->usedb;
void *bdb_handle;
bdb_handle = get_bdb_handle(db, AUXDB_NONE);
if (!bdb_handle)
return ERR_NO_AUXDB;
iq->gluewhere = "bdb_get_cmp_context";
*context = bdb_get_cmp_context(bdb_handle);
iq->gluewhere = "bdb_get_cmp_context done";
return 0;
}
/* returns 0 if record is ok in context,
* 1 if it is newer than context (so don't return it)
* else an rcode */
int cmp_context(struct ireq *iq, unsigned long long genid,
unsigned long long context)
{
struct dbtable *db = iq->usedb;
void *bdb_handle;
int rc;
bdb_handle = get_bdb_handle(db, AUXDB_NONE);
if (!bdb_handle)
return ERR_NO_AUXDB;
iq->gluewhere = "bdb_get_cmp_context";
rc = bdb_check_genid_is_older(bdb_handle, genid, context);
iq->gluewhere = "bdb_get_cmp_context done";
/*fprintf(stderr, "cmp_context: %llx %llx %d\n", genid, context, rc);*/
return rc;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* TRANSACTIONAL INDEX ROUTINES */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int ix_isnullk(void *db_table, void *key, int ixnum)
{
struct dbtable *db = db_table;
struct schema *dbixschema;
int ifld;
if (!db || !key || ixnum < 0 || ixnum >= db->nix) {
logmsg(LOGMSG_ERROR,
"ix_isnullk: bad args, db = %p, key = %p, ixnum = %d\n",
db, key, ixnum);
return 0;
}
if (db->ix_dupes[ixnum]) {
return 0;
}
if (!db->ix_nullsallowed[ixnum]) {
return 0;
}
dbixschema = db->ixschema[ixnum];
if (!dbixschema) {
logmsg(LOGMSG_ERROR,
"ix_isnullk: missing schema, db = %p, key = %p, ixnum = %d\n",
db, key, ixnum);
return 0;
}
for (ifld = 0; ifld < dbixschema->nmembers; ifld++) {
struct field *dbixfield = &dbixschema->member[ifld];
if (dbixfield) {
const char *bkey = (const char *)key;
int offset = dbixfield->offset;
if (offset >= 0 && stype_is_null((bkey + offset))) {
/* fprintf(stderr,
"ix_isnullk: found NULL, db = %p, key = %p, ixnum = %d, ifld = %d\n",
db, key, ixnum, ifld); */
return 1;
}
}
}
/* fprintf(stderr,
"ix_isnullk: no NULL, db = %p, key = %p, ixnum = %d\n",
db, key, ixnum); */
return 0;
}
int ix_addk_auxdb(int auxdb, struct ireq *iq, void *trans, void *key, int ixnum,
unsigned long long genid, int rrn, void *dta, int dtalen, int isnull)
{
struct dbtable *db = iq->usedb;
int rc, bdberr;
void *bdb_handle;
if (!auxdb && (iq->usedb->ix_disabled[ixnum] & INDEX_WRITE_DISABLED)) {
if (iq->debug)
reqprintf(iq, "ix_addk_auxdb: ix %d write disabled", ixnum);
return 0;
}
bdb_handle = get_bdb_handle(db, auxdb);
if (!bdb_handle)
return ERR_NO_AUXDB;
iq->gluewhere = "bdb_prim_addkey";
rc = bdb_prim_addkey_genid(bdb_handle, trans, key, ixnum, rrn, genid, dta,
dtalen, isnull,
&bdberr);
iq->gluewhere = "bdb_prim_addkey done";
if (rc == 0)
return 0;
/*translate engine rcodes */
switch (bdberr) {
case BDBERR_ADD_DUPE:
return IX_DUP;
case BDBERR_DEADLOCK:
return RC_INTERNAL_RETRY;
case BDBERR_READONLY:
return ERR_NOMASTER;
/*fall through to default*/
default:
logmsg(LOGMSG_ERROR, "*ERROR* bdb_prim_addkey return unhandled rc %d\n", bdberr);
return ERR_INTERNAL;
}
}
/*index routines */
int ix_addk(struct ireq *iq, void *trans, void *key, int ixnum,
unsigned long long genid, int rrn, void *dta, int dtalen, int isnull)
{
return ix_addk_auxdb(AUXDB_NONE, iq, trans, key, ixnum, genid, rrn, dta,
dtalen, isnull);
}
int ix_upd_key(struct ireq *iq, void *trans, void *key, int keylen, int ixnum,
unsigned long long oldgenid, unsigned long long genid, void *dta,
int dtalen, int isnull)
{
struct dbtable *db = iq->usedb;
int rc, bdberr;
void *bdb_handle;
if (iq->usedb->ix_disabled[ixnum] & INDEX_WRITE_DISABLED) {
if (iq->debug)
reqprintf(iq, "upd_key: ix %d write disabled", ixnum);
return 0;
}
bdb_handle = get_bdb_handle(db, AUXDB_NONE);
if (!bdb_handle)
return ERR_NO_AUXDB;
iq->gluewhere = "bdb_prim_updkey";
rc = bdb_prim_updkey_genid(bdb_handle, trans, key, keylen, ixnum, genid,
oldgenid, dta, dtalen, isnull, &bdberr);