forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdohsql.c
1893 lines (1644 loc) · 56.3 KB
/
dohsql.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 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 <poll.h>
#include "comdb2.h"
#include "sql.h"
#include "shard_range.h"
#include "sqliteInt.h"
#include "queue.h"
#include "dohsql.h"
#include "sqlinterfaces.h"
#include "memcompare.c"
int gbl_dohsql_disable = 0;
int gbl_dohsql_verbose = 0;
int gbl_dohsql_max_queued_kb_highwm = 10000000; /* 10 GB */
int gbl_dohsql_full_queue_poll_msec = 10; /* 10msec */
/* for now we keep this tunning "private */
static int gbl_dohsql_track_stats = 1;
static int gbl_dohsql_que_free_highwm = 10;
static int gbl_dohsql_que_free_lowwm = 5;
static int gbl_dohsql_max_queued_kb_lowwm = 1000; /* 1 GB */
struct col {
int type;
char *name;
};
typedef struct col my_col_t;
enum doh_status { DOH_RUNNING = 0, DOH_MASTER_DONE = 1, DOH_CLIENT_DONE = 2 };
struct dohsql_connector_stats {
int max_queue_len; /* over the execution time */
int max_free_queue_len; /* -- " -- */
long long max_queue_bytes; /* -- " -- */
};
typedef struct dohsql_connector_stats dohsql_connector_stats_t;
struct dohsql_connector {
struct sqlclntstate *clnt;
queue_type *que; /* queue to caller */
queue_type *que_free; /* de-queued rows come here to be freed */
pthread_mutex_t mtx; /* mutex for queueing operations and related counts */
char *thr_where; /* cached where status */
my_col_t *cols; /* cached cols values */
int ncols; /* number of columns */
int rc;
int nrows; /* current total queued rows */
enum doh_status status; /* caller is done */
long long queue_size; /* size of queue in bytes */
int nparams; /* parameters for the child */
struct param_data *params;
dohsql_connector_stats_t stats;
};
typedef struct dohsql_connector dohsql_connector_t;
typedef Mem row_t;
enum {
ILIMIT_MEM_IDX = 0,
ILIMIT_SAVED_MEM_IDX = 1,
IOFFSET_MEM_IDX = 2,
IOFFSETLIMIT_MEM_IDX = 3,
IOFFSET_SAVED_MEM_IDX = 4,
MAX_MEM_IDX = 5
};
struct dohsql_req_stats {
int max_queue_len; /* among all shards */
int max_free_queue_len; /* -- " -- */
long long max_queue_bytes; /* -- " -- */
};
typedef struct dohsql_req_stats dohsql_req_stats_t;
struct dohsql {
int nconns;
dohsql_connector_t *conns;
my_col_t *cols;
int ncols;
row_t *row;
int row_src;
int rc;
int child_err; /* if a child error occurred, which one?*/
/* LIMIT support */
int limitRegs[MAX_MEM_IDX]; /* sqlite engine limit registers*/
int limit; /* any limit */
int nrows; /* sent rows so far */
/* OFFSET support */
int offset; /* any offset */
int skipped; /* how many rows where skipped so far */
/* ORDER BY support */
int filling;
int active;
int *order;
int top_idx;
int order_size;
int *order_dir;
int nparams;
/* stats */
dohsql_req_stats_t stats;
struct plugin_callbacks backup;
};
typedef struct dohsql dohsql_t;
struct dohsql_stats {
long long num_reqs;
int max_distribution;
int max_queue_len;
int max_free_queue_len;
long long max_queue_bytes;
};
typedef struct dohsql_stats dohsql_stats_t;
pthread_mutex_t dohsql_stats_mtx = PTHREAD_MUTEX_INITIALIZER;
dohsql_stats_t gbl_dohsql_stats; /* updated only on request completion */
dohsql_stats_t gbl_dohsql_stats_dirty; /* updated dynamically, unlocked */
static int gbl_plugin_api_debug = 0;
static void sqlengine_work_shard_pp(struct thdpool *pool, void *work,
void *thddata, int op);
static void sqlengine_work_shard(struct thdpool *pool, void *work,
void *thddata);
static int order_init(dohsql_t *conns, dohsql_node_t *node);
static int dohsql_dist_next_row_ordered(struct sqlclntstate *clnt,
sqlite3_stmt *stmt);
static int _param_index(dohsql_connector_t *conn, const char *b, int64_t *c);
static int _param_value(dohsql_connector_t *conn, struct param_data *b, int c,
const char *src);
static void sqlengine_work_shard_pp(struct thdpool *pool, void *work,
void *thddata, int op)
{
struct sqlclntstate *clnt = work;
switch (op) {
case THD_RUN:
sqlengine_setup_temp_table_mtx(clnt);
sqlengine_work_shard(pool, work, thddata);
break;
case THD_FREE:
sqlengine_cleanup_temp_table_mtx(clnt);
/* error, we are done */
clnt->query_rc = -1;
clnt->done = 1;
break;
}
}
void handle_child_error(struct sqlclntstate *clnt, int errcode)
{
dohsql_connector_t *conn = (dohsql_connector_t *)clnt->plugin.state;
if (conn) {
pthread_mutex_lock(&conn->mtx);
conn->rc = -1;
pthread_mutex_unlock(&conn->mtx);
}
}
static void sqlengine_work_shard(struct thdpool *pool, void *work,
void *thddata)
{
struct sqlthdstate *thd = thddata;
struct sqlclntstate *clnt = (struct sqlclntstate *)work;
int rc;
thr_set_user("shard thread", clnt->appsock_id);
rdlock_schema_lk();
sqlengine_prepare_engine(thd, clnt, 0);
unlock_schema_lk();
reqlog_set_origin(thd->logger, "%s", clnt->origin);
rc = get_curtran(thedb->bdb_env, clnt);
if (rc) {
handle_child_error(clnt, rc);
return;
}
/* assign this query a unique id */
sql_get_query_id(thd->sqlthd);
/*
Review: read committed and friends
// Set whatever mode this client needs
rc = sql_set_transaction_mode(thd->sqldb, clnt, clnt_parent->dbtran.mode);
osql_shadtbl_begin_query(thedb->bdb_env, clnt);
//expanded execute_sql_query
query_stats_setup(thd, clnt);
*/
clnt->query_rc = handle_sqlite_requests(thd, clnt);
if (clnt->query_rc != SQLITE_OK) {
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "XXX: client %p returned error %d\n",
clnt->plugin.state, clnt->query_rc);
handle_child_error(clnt, clnt->query_rc);
}
sql_reset_sqlthread(thd->sqlthd);
if (put_curtran(thedb->bdb_env, clnt)) {
logmsg(LOGMSG_ERROR, "%s: unable to destroy a CURSOR transaction!\n",
__func__);
}
clnt->osql.timings.query_finished = osql_log_time();
osql_log_time_done(clnt);
/*thrman_setid(thrman_self(), "[done]");*/
/* after this clnt is toast */
((dohsql_connector_t *)clnt->plugin.state)->status = DOH_CLIENT_DONE;
}
static int inner_type(sqlite3_stmt *stmt, int col)
{
int type = sqlite3_column_type(stmt, col);
if (type == SQLITE_NULL) {
type = typestr_to_type(sqlite3_column_decltype(stmt, col));
}
if (type == SQLITE_DECIMAL) {
type = SQLITE_TEXT;
}
return type;
}
static int inner_columns(struct sqlclntstate *clnt, sqlite3_stmt *stmt)
{
dohsql_connector_t *conn = (dohsql_connector_t *)clnt->plugin.state;
int ncols, i;
ncols = sqlite3_column_count(stmt);
if (!conn->cols || conn->ncols < ncols) {
conn->cols = (my_col_t *)realloc(conn->cols, ncols * sizeof(my_col_t));
if (!conn->cols)
return -1;
}
conn->ncols = ncols;
for (i = 0; i < ncols; i++) {
conn->cols[i].type = inner_type(stmt, i);
}
return 0;
}
static void trimQue(dohsql_connector_t *conn, sqlite3_stmt *stmt,
queue_type *que, int limit)
{
row_t *row;
long long row_size;
while (queue_count(que) > limit) {
row = queue_next(que);
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "XXX: %p freed older row limit %d\n", que,
limit);
sqlite3CloneResultFree(stmt, &row, &row_size);
if (gbl_dohsql_max_queued_kb_highwm) {
conn->queue_size -= row_size;
}
}
}
/* locked */
static void _track_que_free(dohsql_connector_t *conn)
{
if (unlikely(!gbl_dohsql_track_stats))
return;
int len = queue_count(conn->que_free);
if (conn->stats.max_free_queue_len < len) {
conn->stats.max_free_queue_len = len;
if (gbl_dohsql_stats_dirty.max_free_queue_len < len)
gbl_dohsql_stats_dirty.max_free_queue_len = len;
}
}
/* conn is locked */
static void _que_limiter(dohsql_connector_t *conn, sqlite3_stmt *stmt,
int row_size)
{
if (gbl_dohsql_max_queued_kb_highwm) {
conn->queue_size += row_size;
}
cleanup:
/* inline cleanup */
if (queue_count(conn->que_free) > gbl_dohsql_que_free_highwm) {
_track_que_free(conn);
trimQue(conn, stmt, conn->que_free, gbl_dohsql_que_free_lowwm);
}
if (gbl_dohsql_max_queued_kb_highwm) {
if (conn->queue_size > gbl_dohsql_max_queued_kb_highwm * 1000) {
if ((conn->queue_size > gbl_dohsql_max_queued_kb_lowwm * 1000) &&
conn->status != DOH_MASTER_DONE) {
pthread_mutex_unlock(&conn->mtx);
poll(NULL, 0, gbl_dohsql_full_queue_poll_msec);
pthread_mutex_lock(&conn->mtx);
goto cleanup;
}
}
}
if (likely(gbl_dohsql_track_stats)) {
int tmp = queue_count(conn->que);
if (conn->stats.max_queue_len < tmp) {
conn->stats.max_queue_len = tmp;
if (gbl_dohsql_stats_dirty.max_queue_len < tmp)
gbl_dohsql_stats_dirty.max_queue_len = tmp;
}
if (conn->stats.max_queue_bytes < conn->queue_size) {
conn->stats.max_queue_bytes = conn->queue_size;
if (gbl_dohsql_stats_dirty.max_queue_bytes < conn->queue_size)
gbl_dohsql_stats_dirty.max_queue_bytes = conn->queue_size;
}
}
}
static int inner_row(struct sqlclntstate *clnt, struct response_data *resp,
int postpone)
{
dohsql_connector_t *conn = (dohsql_connector_t *)clnt->plugin.state;
sqlite3_stmt *stmt = resp->stmt;
long long row_size;
row_t *row;
row_t *oldrow;
oldrow = NULL;
pthread_mutex_lock(&conn->mtx);
if (conn->status == DOH_MASTER_DONE) {
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "%lx %s master done q %d qf %d\n",
pthread_self(), __func__, queue_count(conn->que),
queue_count(conn->que_free));
/* work is done, need to clean-up */
_track_que_free(conn);
trimQue(conn, stmt, conn->que, 0);
trimQue(conn, stmt, conn->que_free, 0);
conn->rc = SQLITE_DONE; /* signal master this is clear */
pthread_mutex_unlock(&conn->mtx);
return SQLITE_DONE; /* any != 0 will do, this impersonates a normal end
*/
}
/* try to steal an old row */
if (queue_count(conn->que_free) > 0) {
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "%lx %s retrieved older row\n", pthread_self(),
__func__);
oldrow = queue_next(conn->que_free);
}
pthread_mutex_unlock(&conn->mtx);
row = sqlite3CloneResult(stmt, oldrow, &row_size);
if (!row)
return SHARD_ERR_GENERIC;
pthread_mutex_lock(&conn->mtx);
conn->rc = SQLITE_ROW;
if (queue_add(conn->que, row))
abort();
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "%lx XXX: %p added new row\n", pthread_self(),
conn);
_que_limiter(conn, stmt, row_size);
pthread_mutex_unlock(&conn->mtx);
return SHARD_NOERR;
}
static int inner_row_last(struct sqlclntstate *clnt)
{
dohsql_connector_t *conn = (dohsql_connector_t *)clnt->plugin.state;
pthread_mutex_lock(&conn->mtx);
conn->rc = SQLITE_DONE;
pthread_mutex_unlock(&conn->mtx);
return SHARD_NOERR;
}
/* override sqlite engine */
static int dohsql_dist_column_count(struct sqlclntstate *clnt, sqlite3_stmt *_)
{
return clnt->conns->ncols;
}
#define FUNC_COLUMN_TYPE(ret, type) \
static ret dohsql_dist_column_##type(struct sqlclntstate *clnt, \
sqlite3_stmt *stmt, int iCol) \
{ \
dohsql_t *conns = clnt->conns; \
if (conns->row_src == 0) \
return sqlite3_column_##type(stmt, iCol); \
return sqlite3_value_##type(&conns->row[iCol]); \
}
FUNC_COLUMN_TYPE(int, type)
FUNC_COLUMN_TYPE(sqlite_int64, int64)
FUNC_COLUMN_TYPE(double, double)
FUNC_COLUMN_TYPE(int, bytes)
FUNC_COLUMN_TYPE(const unsigned char *, text)
FUNC_COLUMN_TYPE(const void *, blob)
FUNC_COLUMN_TYPE(const dttz_t *, datetime)
static const intv_t *dohsql_dist_column_interval(struct sqlclntstate *clnt,
sqlite3_stmt *stmt, int iCol,
int type)
{
dohsql_t *conns = clnt->conns;
if (conns->row_src == 0)
return sqlite3_column_interval(stmt, iCol, type);
return sqlite3_value_interval(&conns->row[iCol], type);
}
static sqlite3_value *dohsql_dist_column_value(struct sqlclntstate *clnt,
sqlite3_stmt *stmt, int i)
{
dohsql_t *conns = clnt->conns;
if (conns->row_src == 0)
return sqlite3_column_value(stmt, i);
return &conns->row[i];
}
#define Q_LOCK(x) pthread_mutex_lock(&conns->conns[x].mtx)
#define Q_UNLOCK(x) pthread_mutex_unlock(&conns->conns[x].mtx)
static int dohsql_dist_sqlite_error(struct sqlclntstate *clnt,
sqlite3_stmt *stmt, const char **errstr)
{
dohsql_t *conns = clnt->conns;
int errcode;
int src = conns->row_src;
if (src == 0) {
return sqlite_stmt_error(stmt, errstr);
}
Q_LOCK(src);
*errstr = NULL;
errcode = conns->conns[src].rc;
if (errcode != SQLITE_ROW && errcode != SQLITE_DONE)
*errstr = (const char *)conns->row;
Q_UNLOCK(src);
return errcode;
}
static int dohsql_dist_param_count(struct sqlclntstate *clnt)
{
return clnt->conns->conns[0].nparams;
}
static int dohsql_dist_param_index(struct sqlclntstate *clnt, const char *name,
int64_t *index)
{
/* coordinator param subset */
return _param_index(&clnt->conns->conns[0], name, index);
}
static int dohsql_dist_param_value(struct sqlclntstate *clnt,
struct param_data *param, int n)
{
/* coordinator param subset */
return _param_value(&clnt->conns->conns[0], param, n, __func__);
}
static void add_row(dohsql_t *conns, int i, row_t *row)
{
if (conns->row && conns->row_src) {
/* put the used row in the free list */
if (i != conns->row_src)
pthread_mutex_lock(&conns->conns[conns->row_src].mtx);
if (queue_add(conns->conns[conns->row_src].que_free, conns->row))
abort();
if (i != conns->row_src)
pthread_mutex_unlock(&conns->conns[conns->row_src].mtx);
}
/* new row */
conns->row = row;
conns->row_src = i;
}
#define CHILD_DONE(kid) \
(queue_count(conns->conns[(kid)].que) == 0 && \
conns->conns[(kid)].rc == SQLITE_DONE)
#define CHILD_ERROR(kid) \
(conns->conns[(kid)].rc != SQLITE_ROW && \
conns->conns[(kid)].rc != SQLITE_DONE)
static void _signal_children_master_is_done(dohsql_t *conns)
{
int child_num;
for (child_num = 1; child_num < conns->nconns; child_num++) {
pthread_mutex_lock(&conns->conns[child_num].mtx);
if (conns->conns[child_num].status != DOH_CLIENT_DONE) {
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "%s: signalling client done, ignoring\n",
__func__);
conns->conns[child_num].status = DOH_MASTER_DONE;
}
pthread_mutex_unlock(&conns->conns[child_num].mtx);
}
}
static int _get_a_parallel_row(dohsql_t *conns, row_t **prow, int *error_child)
{
int child_num;
int rc = SQLITE_DONE;
*prow = NULL;
for (child_num = 1; child_num < conns->nconns && (*prow) == NULL;
child_num++) {
Q_LOCK(child_num);
/* done */
if (CHILD_DONE(child_num)) {
Q_UNLOCK(child_num);
continue;
}
/* error */
if (CHILD_ERROR(child_num)) {
/* debatable if we wanna clear cached rows before check for error */
rc = conns->conns[child_num].rc;
if (error_child)
*error_child = child_num;
Q_UNLOCK(child_num);
/* we could envision a case when child is retried for cut 2*/
/* for now, signal all children that we are done and pass error
to caller */
_signal_children_master_is_done(conns);
return rc;
}
rc = SQLITE_OK;
if (queue_count(conns->conns[child_num].que) > 0) {
*prow = queue_next(conns->conns[child_num].que);
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "XXX: %p retrieved row\n",
&conns->conns[child_num]);
add_row(conns, child_num, *prow);
rc = SQLITE_ROW;
}
Q_UNLOCK(child_num);
}
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "XXX: parallel row rc = %d\n", rc);
return rc;
}
static int init_next_row(struct sqlclntstate *clnt, sqlite3_stmt *stmt)
{
dohsql_t *conns = clnt->conns;
int rc;
rc = sqlite3_step(stmt);
if (gbl_dohsql_verbose) {
logmsg(LOGMSG_DEBUG, "%lx %s: sqlite3_step rc %d\n", pthread_self(),
__func__, rc);
if (conns->limitRegs[ILIMIT_SAVED_MEM_IDX] > 0)
logmsg(LOGMSG_DEBUG,
"%lx clnt %p conns %p limitMem %d:%d limit %d "
"offsetMem %d:%d offset %d\n",
pthread_self(), clnt, conns,
conns->limitRegs[ILIMIT_MEM_IDX],
conns->limitRegs[ILIMIT_SAVED_MEM_IDX], conns->limit,
conns->limitRegs[IOFFSET_MEM_IDX],
conns->limitRegs[IOFFSET_SAVED_MEM_IDX], conns->offset);
}
if (rc == SQLITE_ROW)
return rc;
if (rc == SQLITE_DONE) {
conns->conns[0].rc = SQLITE_DONE;
return SQLITE_DONE;
}
_signal_children_master_is_done(conns);
return rc;
}
static int _check_limit(sqlite3_stmt *stmt, dohsql_t *conns)
{
if (conns->limitRegs[ILIMIT_SAVED_MEM_IDX] > 0 && conns->limit >= 0 &&
conns->nrows >= conns->limit) {
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "%lx REACHED LIMIT rc =%d!\n", pthread_self(),
conns->conns[0].rc);
if (conns->conns[0].rc != SQLITE_DONE) {
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "RESET STMT!\n");
sqlite3_reset(stmt);
}
_signal_children_master_is_done(conns);
return SQLITE_DONE;
}
return SQLITE_OK;
}
static int _check_offset(dohsql_t *conns)
{
if (conns->limitRegs[IOFFSET_SAVED_MEM_IDX] &&
conns->skipped < conns->offset) {
conns->skipped++;
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG,
"XXX: skipped client %d row skipped %d, offset =%d\n",
conns->row_src, conns->skipped, conns->offset);
/* skip it */
return SQLITE_OK;
}
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "XXX: returned source %d row\n", conns->row_src);
return SQLITE_ROW;
}
/**
* this is a non-ordered merge of N engine outputs
*
*/
static int dohsql_dist_next_row(struct sqlclntstate *clnt, sqlite3_stmt *stmt)
{
dohsql_t *conns = clnt->conns;
row_t *row;
int empty;
int rc;
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "%lx %s: start\n", pthread_self(), __func__);
if (conns->nrows == 0) {
rc = init_next_row(clnt, stmt);
if (rc == SQLITE_ROW) {
add_row(conns, 0, NULL);
goto got_row;
}
if (rc != SQLITE_DONE)
return rc;
}
rc = _check_limit(stmt, conns);
if (rc != SQLITE_OK)
return rc;
wait_for_others:
rc = 0;
empty = 1;
rc = _get_a_parallel_row(conns, &row, &conns->child_err);
if (rc == SQLITE_ROW) {
assert(row);
goto got_row;
}
if (rc == SQLITE_OK)
empty = 0;
else if (rc != SQLITE_DONE) {
/* it seems some shard failed, reset current since we are bailing out */
if (conns->conns[0].rc != SQLITE_DONE) {
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "RESET STMT CLIENT FAILED!\n");
sqlite3_reset(stmt);
}
return rc;
}
/* no row in others (yet) */
if (conns->conns[0].rc != SQLITE_DONE) {
rc = sqlite3_step(stmt);
if (gbl_dohsql_verbose)
logmsg(LOGMSG_DEBUG, "%s: rc =%d\n", __func__, rc);
if (rc == SQLITE_DONE)
conns->conns[0].rc = SQLITE_DONE;
else {
if (rc == SQLITE_ROW) {
add_row(conns, 0, NULL);
goto got_row;
}
_signal_children_master_is_done(conns);
return rc;
}
}
if (!empty) {
poll(NULL, 0, 10);
goto wait_for_others;
}
/* error or done */
return SQLITE_DONE;
got_row:
/* limit support */
rc = _check_offset(conns);
if (rc != SQLITE_ROW)
goto wait_for_others;
conns->nrows++;
return SQLITE_ROW;
}
static int dohsql_write_response(struct sqlclntstate *c, int t, void *a, int i)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s type %d code %d\n", pthread_self(),
__func__, t, i);
switch (t) {
case RESPONSE_COLUMNS:
return inner_columns(c, a);
case RESPONSE_COLUMNS_STR:
return 0 /*newsql_columns_str(c, a, i)*/;
case RESPONSE_DEBUG:
return 0 /*newsql_debug(c, a)*/;
case RESPONSE_ERROR:
c->saved_rc = i;
c->saved_errstr = strdup((char *)a);
return 0;
case RESPONSE_ERROR_ACCESS:
c->saved_rc = CDB2ERR_ACCESS;
c->saved_errstr = strdup((char *)a);
return 0;
case RESPONSE_ERROR_BAD_STATE:
c->saved_rc = CDB2ERR_BADSTATE;
c->saved_errstr = strdup((char *)a);
return 0;
case RESPONSE_ERROR_PREPARE:
c->saved_rc = CDB2ERR_PREPARE_ERROR;
c->saved_errstr = strdup((char *)a);
return 0;
case RESPONSE_ERROR_REJECT:
c->saved_rc = CDB2ERR_REJECTED;
c->saved_errstr = strdup((char *)a);
return 0;
case RESPONSE_FLUSH:
return 0 /*newsql_flush(c)*/;
case RESPONSE_HEARTBEAT:
return 0 /*newsql_heartbeat(c)*/;
case RESPONSE_ROW:
return inner_row(c, a, i);
case RESPONSE_ROW_LAST:
return inner_row_last(c);
case RESPONSE_ROW_LAST_DUMMY:
return 0 /*newsql_row_last_dummy(c)*/;
case RESPONSE_ROW_LUA:
return 0 /*newsql_row_lua(c, a)*/;
case RESPONSE_ROW_STR:
return 0 /*newsql_row_str(c, a, i)*/;
case RESPONSE_TRACE:
return 0 /*newsql_trace(c, a)*/;
/* fastsql only messages */
case RESPONSE_COST:
case RESPONSE_EFFECTS:
case RESPONSE_ERROR_PREPARE_RETRY:
case RESPONSE_COLUMNS_LUA:
return 0;
default:
logmsg(LOGMSG_ERROR, "Unsupported option %d\n", t);
abort();
}
return 0;
}
static int dohsql_read_response(struct sqlclntstate *a, int b, void *c, int d)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s %d\n", pthread_self(), __func__, b);
return -1;
}
static void *dohsql_save_stmt(struct sqlclntstate *clnt, void *arg)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return strdup(clnt->sql);
}
static void *dohsql_restore_stmt(struct sqlclntstate *clnt, void *arg)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
clnt->sql = arg;
return NULL;
}
static void *dohsql_destroy_stmt(struct sqlclntstate *clnt, void *arg)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
free(arg);
return NULL;
}
static void *dohsql_print_stmt(struct sqlclntstate *clnt, void *arg)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return arg;
}
static int dohsql_param_count(struct sqlclntstate *c)
{
dohsql_connector_t *conn = (dohsql_connector_t *)c->plugin.state;
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return conn->nparams;
}
static int _param_index(dohsql_connector_t *conn, const char *b, int64_t *c)
{
int i;
for (i = 0; i < conn->nparams; i++) {
if (!strcasecmp(b, conn->params[i].name)) {
*c = conn->params[i].pos;
return 0;
}
}
return -1;
}
static int dohsql_param_index(struct sqlclntstate *a, const char *b, int64_t *c)
{
dohsql_connector_t *conn = (dohsql_connector_t *)a->plugin.state;
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return _param_index(conn, b, c);
}
static int _param_value(dohsql_connector_t *conn, struct param_data *b, int c,
const char *src)
{
if (c < 0 || c >= conn->nparams)
return -1;
*b = conn->params[c];
if (b->name[0] == '\0') {
/* these are index based that are renamed; use their index position as
their identity, matching the sql query generated */
b->pos = c + 1;
}
return 0;
}
static int dohsql_param_value(struct sqlclntstate *a, struct param_data *b,
int c)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return _param_value((dohsql_connector_t *)a->plugin.state, b, c, __func__);
}
static int dohsql_override_count(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
/* children don't have overrides, they are only available in distributor */
return 0;
}
static int dohsql_override_type(struct sqlclntstate *a, int b)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s TODO\n", pthread_self(), __func__);
return 0;
}
static int dohsql_clr_cnonce(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_has_cnonce(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return 0;
}
static int dohsql_set_cnonce(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_get_cnonce(struct sqlclntstate *a, snap_uid_t *b)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_get_snapshot(struct sqlclntstate *a, int *b, int *c)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_upd_snapshot(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_clr_snapshot(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_has_high_availability(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return 0;
}
static int dohsql_set_high_availability(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_clr_high_availability(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return -1;
}
static int dohsql_get_high_availability(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return 0;
}
static int dohsql_has_parallel_sql(struct sqlclntstate *a)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return 0;
}
static void dohsql_add_steps(struct sqlclntstate *a, double b)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
}
static void dohsql_setup_client_info(struct sqlclntstate *clnt,
struct sqlthdstate *b, char *c)
{
dohsql_connector_t *conn = (dohsql_connector_t *)clnt->plugin.state;
if (conn->thr_where)
thrman_wheref(thrman_self(), "%s", conn->thr_where);
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s %s\n", pthread_self(), __func__,
(conn->thr_where) ? conn->thr_where : "NULL");
}
static int dohsql_skip_row(struct sqlclntstate *a, uint64_t b)
{
if (gbl_plugin_api_debug)
logmsg(LOGMSG_WARN, "%lx %s\n", pthread_self(), __func__);
return 0;
}
static int dohsql_log_context(struct sqlclntstate *a, struct reqlogger *b)