-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathosqlsqlthr.c
More file actions
2109 lines (1843 loc) · 73.6 KB
/
Copy pathosqlsqlthr.c
File metadata and controls
2109 lines (1843 loc) · 73.6 KB
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.
*/
/**
*
* Interface with sqlglue.c
*
* Each sqlthread executing an osql request has associated an osqlstate_t
* structure. The structure is registered in a checkboard during the
* osql request lifetime.
*
* Sqlthreads will call insrec/updrec/delrec functions.
* - For socksql mode, each call results in a net transfer
* to the master.
* - For recom, snapisol and serial mode, each call will update the local
*shadow tables (which
* will be transferred to the master once the transaction is committed).
*
*/
#include <poll.h>
#include <strings.h>
#include <util.h>
#include <unistd.h>
#include "sql.h"
#include "osqlsqlthr.h"
#include "osqlshadtbl.h"
#include "osqlcomm.h"
#include "osqlcheckboard.h"
#include <bdb_api.h>
#include "comdb2.h"
#include "genid.h"
#include "comdb2uuid.h"
#include <bpfunc.h>
#include "debug_switches.h"
#include <net_types.h>
#include <trigger.h>
#include <logmsg.h>
#include "views.h"
#include <dbinc/queue.h>
#include "osqlsqlnet.h"
#include "schemachange.h"
#include "db_access.h"
#include "fdb_fend.h"
extern int gbl_partial_indexes;
extern int gbl_expressions_indexes;
extern int gbl_reorder_socksql_no_deadlock;
int gbl_allow_bplog_restarts = 600;
int gbl_master_retry_poll_ms = 100;
int gbl_noleader_retry_duration_ms = 50 * 1000; /* wait up to 50 seconds for a new leader */
int gbl_noleader_retry_poll_ms = 10;
static int osql_send_usedb_logic(struct BtCursor *pCur, struct sql_thread *thd,
int nettype);
static int osql_send_delidx_logic(struct BtCursor *pCur,
struct sqlclntstate *clnt, int nettype);
static int osql_send_insidx_logic(struct BtCursor *pCur,
struct sqlclntstate *clnt, int nettype);
static int osql_send_qblobs_logic(struct BtCursor *pCur, osqlstate_t *osql,
int *updCols, blob_buffer_t *blobs,
int nettype);
static int osql_send_recordgenid_logic(struct BtCursor *pCur,
struct sql_thread *thd,
unsigned long long genid, int nettype);
static int osql_send_commit_logic(struct sqlclntstate *clnt, int is_retry,
int nettype);
static int osql_send_abort_logic(struct sqlclntstate *clnt, int nettype);
static int check_osql_capacity(struct sql_thread *thd);
static int osql_begin(struct sqlclntstate *clnt, int type, int keep_rqid);
static int osql_end(struct sqlclntstate *clnt);
static int osql_wait(struct sqlclntstate *clnt);
static int osql_sock_restart(struct sqlclntstate *clnt, int maxretries, int keep_session, int is_final);
#ifdef DEBUG_REORDER
#define DEBUG_PRINT_NUMOPS() \
do { \
uuidstr_t us; \
DEBUGMSG("uuid=%s, replicant_numops=%d\n", \
comdb2uuidstr(osql->uuid, us), osql->replicant_numops); \
} while (0)
#else
#define DEBUG_PRINT_NUMOPS()
#endif
/*
artificially limit the size of the transaction;
apparently this avoids some bugs and makes the db happy
- less deadlocks
- no timeouts
- no replication bugout on 4.2
*/
int g_osql_max_trans = 50000;
/**
* Set maximum osql transaction size
*
*/
inline void set_osql_maxtransfer(int limit)
{
g_osql_max_trans = limit;
}
/**
* Get maximum osql transaction size
*
*/
inline int get_osql_maxtransfer(void)
{
return g_osql_max_trans;
}
int gbl_osql_random_restart = 0;
static inline int osql_should_restart(struct sqlclntstate *clnt, int rc,
int keep_rqid)
{
// Modsnap is excluded because snapshot modes do not verifyretry.
if (clnt->dist_txnid) {
return 0;
}
if (rc == OSQL_SEND_ERROR_WRONGMASTER &&
(clnt->dbtran.mode == TRANLEVEL_SOSQL ||
clnt->dbtran.mode == TRANLEVEL_RECOM
)) {
return 1;
}
if (gbl_osql_random_restart && (rand() % 100) == 0) {
uuidstr_t us;
snap_uid_t snap = {{0}};
get_cnonce(clnt, &snap);
logmsg(LOGMSG_USER,
"Forcing random-restart of uuid=%s cnonce=%*s after nops=%d keep_rqid=%d\n",
comdb2uuidstr(clnt->osql.uuid, us), snap.keylen, snap.key,
clnt->osql.replicant_numops, keep_rqid);
return 1;
}
return 0;
}
#define RESTART_SOCKSQL_KEEP_RQID(retries) \
do { \
restarted = 0; \
if (osql_should_restart(clnt, rc, retries)) { \
int final = (retries >= gbl_allow_bplog_restarts); \
rc = osql_sock_restart(clnt, gbl_allow_bplog_restarts, retries, final); \
if (rc) { \
logmsg(LOGMSG_ERROR, "%s: failed to restart socksql session rc=%d\n", __func__, rc); \
} else { \
restarted = 1; \
} \
} \
if (rc) { \
if (rc != SQLITE_DDL_MISUSE) \
logmsg(LOGMSG_ERROR, \
"%s: error writting record to master in offload mode " \
"rc=%d!\n", \
__func__, rc); \
if (rc != SQLITE_TOOBIG && rc != ERR_SC && rc != SQLITE_DDL_MISUSE) \
rc = SQLITE_INTERNAL; \
} else { \
rc = SQLITE_OK; \
} \
} while (0)
#define RESTART_SOCKSQL RESTART_SOCKSQL_KEEP_RQID(0)
#define START_SOCKSQL \
do { \
if (!clnt->osql.sock_started) { \
rc = osql_sock_start(clnt, OSQL_SOCK_REQ, 0, 0); \
if (rc) { \
logmsg(LOGMSG_ERROR, "%s: failed to start socksql transaction rc=%d\n", __func__, rc); \
if (rc != SQLITE_ABORT) \
rc = SQLITE_CLIENT_CHANGENODE; \
return rc; \
} \
uuidstr_t us; \
sql_debug_logf(clnt, __func__, __LINE__, "osql_sock_start returns rqid %llu uuid %s\n", clnt->osql.rqid, \
comdb2uuidstr(clnt->osql.uuid, us), rc); \
} \
} while (0)
/* see below */
enum { OSQL_START_KEEP_RQID = 1, OSQL_START_NO_REORDER = 2, OSQL_START_IS_FINAL = 4 };
extern int gbl_debug_disttxn_trace;
static int osql_sock_start_int(struct sqlclntstate *clnt, int type,
int start_flags)
{
struct sql_thread *thd = pthread_getspecific(query_info_key);
osqlstate_t *osql = &clnt->osql;
int flags = 0;
int rc = 0;
const int poll_ms = gbl_noleader_retry_poll_ms;
int retries = 0;
const int max_retries = gbl_noleader_retry_duration_ms / poll_ms;
int keep_rqid = start_flags & OSQL_START_KEEP_RQID;
int is_final = start_flags & OSQL_START_IS_FINAL;
/* new id */
if (!keep_rqid) {
osql->rqid = OSQL_RQID_USE_UUID;
comdb2uuid(osql->uuid);
if (gbl_debug_disttxn_trace) {
uuidstr_t us;
logmsg(LOGMSG_USER, "DISTTXN REPL %s %s starting uuid %s\n", __func__,
clnt->dist_txnid ? clnt->dist_txnid : "(nodisttxn)", comdb2uuidstr(osql->uuid, us));
}
} else {
if (gbl_debug_disttxn_trace) {
uuidstr_t us;
logmsg(LOGMSG_USER, "DISTTXN REPL %s %s reusing uuid %s\n", __func__,
clnt->dist_txnid ? clnt->dist_txnid : "(nodisttxn)", comdb2uuidstr(osql->uuid, us));
}
}
osql->is_reorder_on = start_flags & OSQL_START_NO_REORDER
? 0
: gbl_reorder_socksql_no_deadlock;
/* lets reset error, this could be a retry */
osql->xerr.errval = 0;
osql->xerr.errstr[0] = '\0';
retry:
rc = clnt_check_bdb_lock_desired(clnt);
if (rc) {
logmsg(LOGMSG_ERROR, "recover_deadlock returned %d\n", rc);
/* keep_rqid: session is being restarted, don't zero rqid */
if (!keep_rqid) {
rc = osql_end(clnt);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to end osql %d\n", __func__, rc);
}
}
return SQLITE_BUSY;
}
rc = osql_begin(clnt, type, keep_rqid);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to start osql rc %d\n", __func__, rc);
return SQLITE_BUSY;
}
/* protect against no master */
if (osql->target.host == NULL || osql->target.host == db_eid_invalid) {
if (retries < max_retries) {
retries++;
if (retries % (1000 / poll_ms) == 0) { /* reduce spew - once a second */
uuidstr_t us;
comdb2uuidstr(osql->uuid, us);
logmsg(LOGMSG_WARN, "Retrying to find the master retries=%d uuid:%s\n", retries, us);
}
poll(NULL, 0, poll_ms);
goto retry;
} else {
uuidstr_t us;
comdb2uuidstr(osql->uuid, us);
logmsg(LOGMSG_ERROR, "%s: no master for uuid:%s\n", __func__, us);
errstat_set_rc(&osql->xerr, ERR_NOMASTER);
errstat_set_str(&osql->xerr, "No master available");
return SQLITE_ABORT;
}
}
/* socksql: check if this is a verify retry, and if we got enough of those
to trigger a self-deadlock check on the master */
if ((type == OSQL_SOCK_REQ || type == OSQL_SOCK_REQ_COST) &&
clnt->verify_retries > gbl_osql_verify_ext_chk)
flags |= OSQL_FLAGS_CHECK_SELFLOCK;
else
flags = 0;
if (osql->is_reorder_on)
flags |= OSQL_FLAGS_REORDER_ON;
if (is_final)
flags |= OSQL_FLAGS_FINAL;
/* send request to blockprocessor */
rc = osql_comm_send_socksqlreq(&osql->target, clnt->sql,
strlen(clnt->sql) + 1, osql->rqid,
osql->uuid, clnt->tzname, type, flags);
if (rc != 0 && retries < max_retries) {
retries++;
if (retries % (1000 / poll_ms) == 0) { /* reduce spew */
uuidstr_t us;
comdb2uuidstr(osql->uuid, us);
logmsg(LOGMSG_WARN, "Retrying to find the master (2) retries=%d uuid:%s\n", retries, us);
}
poll(NULL, 0, poll_ms);
goto retry;
}
if (rc) {
sql_debug_logf(
clnt, __func__, __LINE__,
"Tried %d times and failed rc %d returning SQLITE_ABORT\n", retries,
rc);
errstat_set_rc(&osql->xerr, ERR_NOMASTER);
errstat_set_str(&osql->xerr, "No leader available");
rc = SQLITE_ABORT;
}
if (rc == 0) {
if (clnt->client_understands_query_stats)
osql_query_dbglog(thd, clnt->queryid);
if (clnt->is_participant)
osql_begin_participant(thd);
osql->sock_started = 1;
} else if (!keep_rqid) {
int irc = osql_end(clnt);
if (irc) {
logmsg(LOGMSG_ERROR, "%s failed to end osql rc %d irc %d\n",
__func__, rc, irc);
}
}
return rc;
}
/**
* This is called on the replicant node and starts a sosql session,
* which creates a blockprocessor peer on the master node
* Returns ok if the packet is sent successful to the master
* If keep_rqid, this is a retry and we want to keep the same rqid
*/
int osql_sock_start(struct sqlclntstate *clnt, int type, int keep_id, int is_final)
{
int flags = (keep_id ? OSQL_START_KEEP_RQID : 0) | (is_final ? OSQL_START_IS_FINAL : 0);
return osql_sock_start_int(clnt, type, flags);
}
/* TODO: disable this on the master; if dbq deletes are there */
int osql_sock_start_no_reorder(struct sqlclntstate *clnt, int type, int keep_id, int is_final)
{
int flags = OSQL_START_NO_REORDER;
flags |= (keep_id ? OSQL_START_KEEP_RQID : 0 | (is_final ? OSQL_START_IS_FINAL : 0));
return osql_sock_start_int(clnt, type, flags);
}
int osql_sock_start_deferred(struct sqlclntstate *clnt)
{
int rc;
if (clnt->dbtran.mode == TRANLEVEL_SOSQL)
START_SOCKSQL;
return 0;
}
static int osql_begin(struct sqlclntstate *clnt, int type, int keep_rqid)
{
/* note: custom interface can still delegate to osql over net */
if (clnt->begin) {
if (!clnt->begin(clnt, type, keep_rqid))
return 0;
}
/* default */
return osql_begin_net(clnt, type, keep_rqid);
}
static int osql_end(struct sqlclntstate *clnt)
{
/* note: custom interface can still delegate to osql over net */
if (clnt->end)
if (!clnt->end(clnt))
return 0;
/* default */
return osql_end_net(clnt);
}
static int osql_wait(struct sqlclntstate *clnt)
{
int timeout;
osqlstate_t *osql = &clnt->osql;
errstat_t dummy = {0};
/* if this is a 2pc participant, we don't need to wait here */
if (clnt->is_participant)
return 0;
/* If an error is set (e.g., selectv error from range check), latch it. */
errstat_t *err = (osql->xerr.errval == 0) ? &osql->xerr : &dummy;
if (osql->running_ddl)
timeout = bdb_attr_get(thedb->bdb_attr, BDB_ATTR_SOSQL_DDL_MAX_COMMIT_WAIT_SEC);
else
timeout = bdb_attr_get(thedb->bdb_attr, BDB_ATTR_SOSQL_MAX_COMMIT_WAIT_SEC);
if (clnt->wait)
if (!clnt->wait(clnt, timeout, err))
return 0;
// return osql_chkboard_wait_commitrc(osql->rqid, osql->uuid, timeout, err);
int startms = comdb2_time_epochms();
int rc = osql_chkboard_wait_commitrc(osql->rqid, osql->uuid, timeout, err);
int endms = comdb2_time_epochms();
if (gbl_debug_disttxn_trace) {
uuidstr_t us;
logmsg(LOGMSG_USER, "DISTTXN REPL %s %s took %d ms to commit rqid=%llu uuid=%s\n", __func__,
clnt->dist_txnid ? clnt->dist_txnid : "(nodisttxn)", (endms - startms), osql->rqid,
comdb2uuidstr(osql->uuid, us));
}
return rc;
}
/**
* Process the actual sending of the delrec
*/
static int osql_send_del_logic(struct BtCursor *pCur, struct sql_thread *thd)
{
struct sqlclntstate *clnt = thd->clnt;
osqlstate_t *osql = &clnt->osql;
int rc = osql_send_usedb_logic(pCur, thd, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK)
return rc;
if (osql->is_reorder_on) {
rc = osql_send_delrec(
&osql->target, osql->rqid, osql->uuid, pCur->genid,
(gbl_partial_indexes && pCur->db->ix_partial) ? clnt->del_keys
: -1ULL,
NET_OSQL_SOCK_RPL);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
if (gbl_expressions_indexes && pCur->db->ix_expr) {
rc = osql_send_delidx_logic(pCur, thd->clnt, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
if (!osql->is_reorder_on) {
rc = osql_send_delrec(
&osql->target, osql->rqid, osql->uuid, pCur->genid,
(gbl_partial_indexes && pCur->db->ix_partial) ? clnt->del_keys
: -1ULL,
NET_OSQL_SOCK_RPL);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
osql->replicant_numops++;
DEBUG_PRINT_NUMOPS();
return SQLITE_OK;
}
/**
* Process a sqlite delete row request
* BtCursor points to the record to be deleted.
* Returns SQLITE_OK if successful.
*
*/
int osql_delrec(struct BtCursor *pCur, struct sql_thread *thd)
{
struct sqlclntstate *clnt = thd->clnt;
int restarted;
int rc = 0;
if ((rc = access_control_check_sql_write(pCur, thd)))
return rc;
/* limit transaction*/
if ((rc = check_osql_capacity(thd)))
return rc;
if (clnt->dbtran.mode == TRANLEVEL_SOSQL) {
START_SOCKSQL;
do {
rc = osql_send_del_logic(pCur, thd);
RESTART_SOCKSQL;
} while (restarted);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql delrec rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
if (gbl_expressions_indexes && pCur->db->ix_expr) {
rc = osql_save_index(pCur, thd, 0 /* isupd */, 1 /*isdel*/);
if (rc != SQLITE_OK)
return rc;
}
return osql_save_delrec(pCur, thd);
}
/**
* Process an insert or update to sqlite_stat1.
* This is handled specially to allow us to save the previous data to llmeta.
* Returns SQLITE_OK if successul.
*
*/
int osql_updstat(struct BtCursor *pCur)
{
int rc, restarted;
struct sqlclntstate *clnt = pCur->clnt;
if (clnt->dbtran.mode == TRANLEVEL_SOSQL) {
START_SOCKSQL;
do {
rc = osql_send_updstat(&clnt->osql);
RESTART_SOCKSQL;
} while (restarted);
if (rc) {
logmsg(LOGMSG_ERROR, "%s: rc:%d\n", __func__, rc);
return rc;
}
}
osql_save_updstat(&clnt->osql);
return 0;
}
/**
* Process the sending part for insrec
*/
static int osql_send_ins_logic(struct BtCursor *pCur, struct sql_thread *thd,
char *pData, int nData, blob_buffer_t *blobs,
int maxblobs, int flags)
{
osqlstate_t *osql = &thd->clnt->osql;
int rc = osql_send_usedb_logic(pCur, thd, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK)
return rc;
if (osql->is_reorder_on) {
rc = osql_send_insrec(
&osql->target, osql->rqid, osql->uuid, pCur->genid,
(gbl_partial_indexes && pCur->db->ix_partial) ? thd->clnt->ins_keys
: -1ULL,
pData, nData, NET_OSQL_SOCK_RPL, flags);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
if (gbl_expressions_indexes && pCur->db->ix_expr) {
rc = osql_send_insidx_logic(pCur, thd->clnt, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
rc = osql_send_qblobs_logic(pCur, osql, NULL, blobs, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK) {
logmsg(LOGMSG_ERROR, "%s:%d %s - failed to send socksql row rc=%d\n",
__FILE__, __LINE__, __func__, rc);
return rc;
}
if (!osql->is_reorder_on) {
rc = osql_send_insrec(
&osql->target, osql->rqid, osql->uuid, pCur->genid,
(gbl_partial_indexes && pCur->db->ix_partial) ? thd->clnt->ins_keys
: -1ULL,
pData, nData, NET_OSQL_SOCK_RPL, flags);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
osql->replicant_numops++;
DEBUG_PRINT_NUMOPS();
return SQLITE_OK;
}
/* When gbl_replicate_local=1, mem_to_ondisk generates a fresh seqno via
* get_unique_longlong() each time it encounters a NULL comdb2_seqno value.
* This is called once for the data record (storing seqno1 in pCur->ondisk_buf)
* and independently once per index key containing comdb2_seqno (storing a
* different seqno2 in clnt->idxInsert[i]). The mismatch means DELETE later
* tries to remove a key with seqno1 (read from the data record) but finds
* seqno2 in the index -> DB_NOTFOUND -> rc 304.
*
* Fix: rebuild each comdb2_seqno index key from the ondisk data record so
* that the stored key and any future delete key use the same seqno value. */
static void fixup_replicate_local_seqno(struct BtCursor *pCur, const char *ondisk_record, struct sqlclntstate *clnt)
{
if (!gbl_replicate_local || !gbl_expressions_indexes || !pCur->db->ix_expr || !clnt->idxInsert)
return;
for (int i = 0; i < pCur->db->nix; i++) {
if (!clnt->idxInsert[i])
continue;
struct schema *ixsc = get_schema(pCur->db, i);
if (!ixsc || find_field_idx_in_tag(ixsc, "comdb2_seqno") < 0)
continue;
char correct_key[MAXKEYLEN];
if (stag_ondisk_to_ix(pCur->db, i, ondisk_record, correct_key) == 0)
memcpy(clnt->idxInsert[i], correct_key, pCur->db->ix_keylen[i]);
}
}
/**
* Process a sqlite insert row request
* Row is provided by (pData, nData, blobs, maxblobs)
* Returns SQLITE_OK if successful.
*
*/
int osql_insrec(struct BtCursor *pCur, struct sql_thread *thd, char *pData,
int nData, blob_buffer_t *blobs, int maxblobs, int flags)
{
struct sqlclntstate *clnt = thd->clnt;
int restarted;
int rc = 0;
if ((rc = access_control_check_sql_write(pCur, thd)))
return rc;
/* limit transaction*/
if ((rc = check_osql_capacity(thd)))
return rc;
fixup_replicate_local_seqno(pCur, pData, clnt);
if (clnt->dbtran.mode == TRANLEVEL_SOSQL) {
START_SOCKSQL;
do {
rc = osql_send_ins_logic(pCur, thd, pData, nData, blobs, maxblobs,
flags);
RESTART_SOCKSQL;
} while (restarted);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql insrec rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
if (gbl_expressions_indexes && pCur->db->ix_expr) {
rc = osql_save_index(pCur, thd, 0 /* isupd */, 0 /*isdel*/);
if (rc != SQLITE_OK)
return rc;
}
rc = osql_save_qblobs(pCur, thd, blobs, maxblobs, 0);
if (rc != SQLITE_OK)
return rc;
rc = osql_save_insrec(pCur, thd, pData, nData, flags);
return rc;
}
/**
* process the sending of updrec
*/
static int osql_send_upd_logic(struct BtCursor *pCur, struct sql_thread *thd,
char *pData, int nData, int *updCols,
blob_buffer_t *blobs, int maxblobs, int flags)
{
osqlstate_t *osql = &thd->clnt->osql;
int rc = osql_send_usedb_logic(pCur, thd, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK)
return rc;
if (osql->is_reorder_on) {
rc = osql_send_updrec(
&osql->target, osql->rqid, osql->uuid, pCur->genid,
(gbl_partial_indexes && pCur->db->ix_partial) ? thd->clnt->ins_keys
: -1ULL,
(gbl_partial_indexes && pCur->db->ix_partial) ? thd->clnt->del_keys
: -1ULL,
pData, nData, NET_OSQL_SOCK_RPL);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
if (gbl_expressions_indexes && pCur->db->ix_expr) {
rc = osql_send_delidx_logic(pCur, thd->clnt, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
rc = osql_send_insidx_logic(pCur, thd->clnt, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
rc = osql_send_qblobs_logic(pCur, osql, updCols, blobs, NET_OSQL_SOCK_RPL);
if (rc != SQLITE_OK) {
logmsg(LOGMSG_ERROR, "%s:%d %s - failed to send socksql row rc=%d\n",
__FILE__, __LINE__, __func__, rc);
return rc;
}
if (updCols) {
rc = osql_send_updcols(&osql->target, osql->rqid, osql->uuid,
pCur->genid, NET_OSQL_SOCK_RPL, &updCols[1],
updCols[0]);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
osql->replicant_numops++;
DEBUG_PRINT_NUMOPS();
}
if (!osql->is_reorder_on) {
rc = osql_send_updrec(
&osql->target, osql->rqid, osql->uuid, pCur->genid,
(gbl_partial_indexes && pCur->db->ix_partial) ? thd->clnt->ins_keys
: -1ULL,
(gbl_partial_indexes && pCur->db->ix_partial) ? thd->clnt->del_keys
: -1ULL,
pData, nData, NET_OSQL_SOCK_RPL);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql row rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
osql->replicant_numops++;
DEBUG_PRINT_NUMOPS();
return SQLITE_OK;
}
/**
* Process a sqlite update row request
* New row is provided by (pData, nData, blobs, maxblobs)
* BtCursor points to the old row.
* Returns SQLITE_OK if successful.
*
*/
int osql_updrec(struct BtCursor *pCur, struct sql_thread *thd, char *pData,
int nData, int *updCols, blob_buffer_t *blobs, int maxblobs,
int flags)
{
struct sqlclntstate *clnt = thd->clnt;
int restarted;
int rc = 0;
if ((rc = access_control_check_sql_write(pCur, thd)))
return rc;
/* limit transaction*/
if ((rc = check_osql_capacity(thd)))
return rc;
if (clnt->dbtran.mode == TRANLEVEL_SOSQL) {
START_SOCKSQL;
do {
rc = osql_send_upd_logic(pCur, thd, pData, nData, updCols, blobs,
maxblobs, flags);
RESTART_SOCKSQL;
} while (restarted);
if (rc) {
logmsg(LOGMSG_ERROR,
"%s:%d %s - failed to send socksql updrec rc=%d\n", __FILE__,
__LINE__, __func__, rc);
return rc;
}
}
if (gbl_expressions_indexes && pCur->db->ix_expr) {
rc = osql_save_index(pCur, thd, 1 /* isupd */, 1 /*isdel*/);
if (rc != SQLITE_OK)
return rc;
rc = osql_save_index(pCur, thd, 1 /* isupd */, 0 /*isdel*/);
if (rc != SQLITE_OK)
return rc;
}
rc = osql_save_qblobs(pCur, thd, blobs, maxblobs, 1);
if (rc != SQLITE_OK)
return rc;
rc = osql_save_updcols(pCur, thd, updCols);
if (rc != SQLITE_OK)
return rc;
return osql_save_updrec(pCur, thd, pData, nData, flags);
}
/**
* Process a sqlite clear table request
* Currently not implemented due to non-transactional vs slowness unresolved
* issues.
*
*/
int osql_cleartable(struct sql_thread *thd, char *dbname)
{
logmsg(LOGMSG_ERROR, "cleartable not implemented!\n");
return -1;
}
int nettypetouuidnettype(int nettype)
{
switch (nettype) {
case NET_OSQL_SOCK_RPL:
return NET_OSQL_SOCK_RPL_UUID;
case NET_OSQL_SERIAL_RPL:
return NET_OSQL_SERIAL_RPL_UUID;
default:
logmsg(LOGMSG_ERROR, "Unsupported nettype %d\n", nettype);
return nettype;
}
}
int osql_serial_send_readset(struct sqlclntstate *clnt, int nettype)
{
osqlstate_t *osql = &clnt->osql;
int rc = 0;
if (nettype == NET_OSQL_SERIAL_RPL) {
if (clnt->arr->size == 0)
return 0;
} else {
if (clnt->selectv_arr->size == 0)
return 0;
}
if (osql->rqid == OSQL_RQID_USE_UUID)
nettype = nettypetouuidnettype(nettype);
CurRangeArr *arr_ptr;
if (nettype == NET_OSQL_SERIAL_RPL || nettype == NET_OSQL_SERIAL_RPL_UUID)
arr_ptr = clnt->arr;
else
arr_ptr = clnt->selectv_arr;
rc = osql_send_serial(&osql->target, osql->rqid, osql->uuid, arr_ptr,
arr_ptr->file, arr_ptr->offset, nettype);
osql->replicant_numops++;
DEBUG_PRINT_NUMOPS();
return rc;
}
int gbl_master_swing_sock_restart_sleep = 0;
/**
* Restart a broken socksql connection by opening a new blockproc on the
* provided master and sending the cache rows to resume the current.
* If keep_session is set, the same rqid is used for the replay
*/
static int osql_sock_restart(struct sqlclntstate *clnt, int maxretries, int keep_session, int is_final)
{
osqlstate_t *osql = &clnt->osql;
struct sql_thread *thd = pthread_getspecific(query_info_key);
uuidstr_t us;
int rc = 0;
int retries = 0;
int bdberr = 0;
int sentops = 0;
if (gbl_debug_disttxn_trace) {
logmsg(LOGMSG_USER, "DISTTXN REPL %s %s restarting uuid=%s keep-session=%d\n", __func__,
clnt->dist_txnid ? clnt->dist_txnid : "(nodisttxn)", comdb2uuidstr(clnt->osql.uuid, us), keep_session);
}
if (!thd) {
logmsg(LOGMSG_ERROR, "%s:%d Bug, not sql thread !\n", __func__, __LINE__);
cheap_stack_trace();
}
do {
/* we need to check if we need bdb write lock here to prevent a master
upgrade blockade */
rc = clnt_check_bdb_lock_desired(clnt);
if (rc) {
logmsg(LOGMSG_ERROR, "recover_deadlock returned %d\n", rc);
rc = osql_end(clnt);
if (rc)
logmsg(LOGMSG_ERROR, "%s: failed to end clnt rc=%d\n", __func__,
rc);
return ERR_RECOVER_DEADLOCK;
}
retries++;
/* if we're shaking really badly, back off */
if (retries > 1)
usleep(retries * 10000); // sleep for a multiple of 10ms
sentops = 0;
osql->replicant_numops = 0;
osql->fingerprint_sent = 0; /* re-send fingerprint for the new session */
if (osql->tablename) {
free(osql->tablename);
osql->tablename = NULL;
osql->tablenamelen = 0;
}
if (!keep_session) {
if (gbl_master_swing_osql_verbose)
logmsg(LOGMSG_USER,
"0x%p Starting new session rqid=%llx, uuid=%s\n",
(void*)pthread_self(), clnt->osql.rqid,
comdb2uuidstr(clnt->osql.uuid, us));
rc = osql_end(clnt);
if (rc) {
logmsg(LOGMSG_ERROR, "%s failed to end osql rc %d\n", __func__,
rc);
return rc;
}
} else if (gbl_master_swing_osql_verbose) {
snap_uid_t snap = {{0}};
get_cnonce(clnt, &snap);
logmsg(LOGMSG_USER, "0x%p Restarting rqid=%llx uuid=%s against %s\n",
(void*)pthread_self(), clnt->osql.rqid,
comdb2uuidstr(clnt->osql.uuid, us), thedb->master);
}
rc = osql_sock_start(clnt, (clnt->dbtran.mode == TRANLEVEL_SOSQL) ? OSQL_SOCK_REQ : OSQL_RECOM_REQ,
keep_session, is_final);
if (rc) {
sql_debug_logf(clnt, __func__, __LINE__,
"osql_sock_start returns %d\n", rc);
logmsg(LOGMSG_ERROR, "osql_sock_start error rc=%d, retries=%d\n",
rc, retries);
if (rc == SQLITE_ABORT)
break;
continue;
}
/* process messages from cache */
rc = osql_shadtbl_process(clnt, &sentops, &bdberr, 1);
if (rc == SQLITE_TOOBIG) {
logmsg(LOGMSG_ERROR, "%s: transaction too big %d\n", __func__, sentops);
return rc;
}
if (rc == ERR_SC) {
logmsg(LOGMSG_ERROR, "%s: schema change error\n", __func__);
return rc;
}
if (keep_session && gbl_master_swing_sock_restart_sleep) {
sleep(gbl_master_swing_sock_restart_sleep);
}