| 1 |
|
| /* --- freelist for cb_queue entries + send_queue entries --- |
| 2 |
|
| * |
| 3 |
|
| * Both use a singly-linked-list-in-the-record-itself stash: the first |
| 4 |
|
| * sizeof(void*) bytes of a released entry are repurposed as the "next |
| 5 |
|
| * free" pointer. Reuse the same struct without rebuilding it from |
| 6 |
|
| * scratch. |
| 7 |
|
| * |
| 8 |
|
| * Also lives here: the singleton sentinels (keepalive_noop_cb, |
| 9 |
|
| * iter_timeout_cb) and the small helpers that munge per-query |
| 10 |
|
| * settings into per-send fields. |
| 11 |
|
| * |
| 12 |
|
| * This file is #include'd from ClickHouse.xs as part of the single |
| 13 |
|
| * translation unit; symbols stay file-local-to-the-TU. |
| 14 |
|
| */ |
| 15 |
|
| |
| 16 |
|
| static ev_ch_cb_t *cbt_freelist = NULL; |
| 17 |
|
| |
| 18 |
49 |
| static ev_ch_cb_t* alloc_cbt(void) { |
| 19 |
|
| ev_ch_cb_t *cbt; |
| 20 |
49 |
▶ | if (cbt_freelist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cbt_freelist) { |
44 |
5 |
49 |
|
| 21 |
44 |
| cbt = cbt_freelist; |
| 22 |
44 |
| cbt_freelist = *(ev_ch_cb_t **)cbt; |
| 23 |
|
| } else { |
| 24 |
5 |
| Newx(cbt, 1, ev_ch_cb_t); |
| 25 |
|
| } |
| 26 |
|
| /* Reset all fields — freelist may have stale values. */ |
| 27 |
49 |
| cbt->cb = NULL; |
| 28 |
49 |
| cbt->raw = 0; |
| 29 |
49 |
| cbt->on_data = NULL; |
| 30 |
49 |
| cbt->on_complete = NULL; |
| 31 |
49 |
| cbt->query_timeout = 0; |
| 32 |
49 |
| return cbt; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
49 |
| static void release_cbt(ev_ch_cb_t *cbt) { |
| 36 |
49 |
| *(ev_ch_cb_t **)cbt = cbt_freelist; |
| 37 |
49 |
| cbt_freelist = cbt; |
| 38 |
49 |
| } |
| 39 |
|
| |
| 40 |
|
| static ev_ch_send_t *send_freelist = NULL; |
| 41 |
|
| |
| 42 |
|
| /* Iterator timeout watcher cb: just break the loop the iterator drove. */ |
| 43 |
0 |
| static void iter_timeout_cb(EV_P_ ev_timer *w, int revents) { |
| 44 |
|
| (void)w; (void)revents; |
| 45 |
0 |
| ev_break(EV_A, EVBREAK_ONE); |
| 46 |
0 |
| } |
| 47 |
|
| |
| 48 |
|
| /* No-op CV reference used as the callback for HTTP keepalive pings; |
| 49 |
|
| * initialised once at BOOT and shared by all connections. */ |
| 50 |
|
| static SV *keepalive_noop_cb = NULL; |
| 51 |
|
| |
| 52 |
55 |
| static ev_ch_send_t* alloc_send(void) { |
| 53 |
|
| ev_ch_send_t *s; |
| 54 |
55 |
▶ | if (send_freelist) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (send_freelist) { |
47 |
8 |
55 |
|
| 55 |
47 |
| s = send_freelist; |
| 56 |
47 |
| send_freelist = *(ev_ch_send_t **)s; |
| 57 |
|
| } else { |
| 58 |
8 |
| Newx(s, 1, ev_ch_send_t); |
| 59 |
|
| } |
| 60 |
55 |
| s->data = NULL; |
| 61 |
55 |
| s->data_len = 0; |
| 62 |
55 |
| s->cb = NULL; |
| 63 |
55 |
| s->insert_data = NULL; |
| 64 |
55 |
| s->insert_data_len = 0; |
| 65 |
55 |
| s->insert_av = NULL; |
| 66 |
55 |
| s->insert_empty_as_default = 0; |
| 67 |
55 |
| s->insert_defaults = NULL; |
| 68 |
55 |
| s->raw = 0; |
| 69 |
55 |
| s->on_data = NULL; |
| 70 |
55 |
| s->on_complete = NULL; |
| 71 |
55 |
| s->query_timeout = 0; |
| 72 |
55 |
| s->query_id = NULL; |
| 73 |
55 |
| s->session_tz = NULL; |
| 74 |
55 |
| s->deferred_sql = NULL; |
| 75 |
55 |
| s->deferred_sql_len = 0; |
| 76 |
55 |
| s->deferred_defaults = NULL; |
| 77 |
55 |
| s->deferred_overrides = NULL; |
| 78 |
55 |
| s->deferred_ext = NULL; |
| 79 |
55 |
| s->deferred_ext_len = 0; |
| 80 |
55 |
| return s; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| /* Drop retained pre-connect build inputs. Idempotent (slots NULLed). */ |
| 84 |
55 |
| static void free_send_deferred(ev_ch_send_t *s) { |
| 85 |
55 |
▶ | CLEAR_STR(s->deferred_sql); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(s->deferred_sql); |
4 |
51 |
55 |
|
| 86 |
55 |
| s->deferred_sql_len = 0; |
| 87 |
55 |
▶ | CLEAR_SV(s->deferred_defaults); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(s->deferred_defaults); |
0 |
55 |
55 |
|
| 88 |
55 |
▶ | CLEAR_SV(s->deferred_overrides); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(s->deferred_overrides); |
0 |
55 |
55 |
|
| 89 |
55 |
▶ | CLEAR_STR(s->deferred_ext); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(s->deferred_ext); |
0 |
55 |
55 |
|
| 90 |
55 |
| s->deferred_ext_len = 0; |
| 91 |
55 |
| } |
| 92 |
|
| |
| 93 |
55 |
| static void release_send(ev_ch_send_t *s) { |
| 94 |
55 |
▶ | CLEAR_STR(s->query_id); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(s->query_id); |
0 |
55 |
55 |
|
| 95 |
55 |
▶ | CLEAR_STR(s->session_tz); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(s->session_tz); |
0 |
55 |
55 |
|
| 96 |
55 |
▶ | CLEAR_SV(s->insert_defaults); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(s->insert_defaults); |
0 |
55 |
55 |
|
| 97 |
55 |
| free_send_deferred(s); |
| 98 |
55 |
| *(ev_ch_send_t **)s = send_freelist; |
| 99 |
55 |
| send_freelist = s; |
| 100 |
55 |
| } |
| 101 |
|
| |
| 102 |
|
| /* Retain pre-connect native build inputs in the send entry as owned |
| 103 |
|
| * snapshots (dispatch builds the packet once server_revision is |
| 104 |
|
| * known). Transfers ext_data and *settings_copy_p ownership. */ |
| 105 |
4 |
| static void stash_send_deferred(pTHX_ ev_ch_send_t *s, const char *sql, |
| 106 |
|
| size_t sql_len, HV *defaults, HV *settings, |
| 107 |
|
| HV **settings_copy_p, char *ext_data, |
| 108 |
|
| size_t ext_len) { |
| 109 |
4 |
| Newx(s->deferred_sql, sql_len + 1, char); |
| 110 |
4 |
| Copy(sql, s->deferred_sql, sql_len, char); |
| 111 |
4 |
| s->deferred_sql[sql_len] = '\0'; |
| 112 |
4 |
| s->deferred_sql_len = sql_len; |
| 113 |
4 |
▶ | s->deferred_defaults = defaults ? newHVhv(defaults) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| s->deferred_defaults = defaults ? newHVhv(defaults) : NULL; |
0 |
4 |
4 |
|
| 114 |
4 |
▶ | if (*settings_copy_p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*settings_copy_p) { |
0 |
4 |
4 |
|
| 115 |
0 |
| s->deferred_overrides = *settings_copy_p; |
| 116 |
0 |
| *settings_copy_p = NULL; |
| 117 |
|
| } else { |
| 118 |
4 |
▶ | s->deferred_overrides = settings ? newHVhv(settings) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| s->deferred_overrides = settings ? newHVhv(settings) : NULL; |
0 |
4 |
4 |
|
| 119 |
|
| } |
| 120 |
4 |
| s->deferred_ext = ext_data; |
| 121 |
4 |
| s->deferred_ext_len = ext_len; |
| 122 |
4 |
| } |
| 123 |
|
| |
| 124 |
|
| /* Copy settings->{query_id} into s->query_id and apply query_timeout. */ |
| 125 |
2 |
| static void send_apply_settings(ev_ch_send_t *s, HV *settings) { |
| 126 |
2 |
| SV **svp = hv_fetch(settings, "query_id", 8, 0); |
| 127 |
2 |
▶ | if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
Branch: 3 branches
| Branch | True | False | Total |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
2 |
0 |
2 |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
2 |
0 |
2 |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
0 |
2 |
2 |
|
| 128 |
2 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
2 |
0 |
2 |
| if (svp && SvOK(*svp)) { |
2 |
0 |
2 |
|
| 129 |
|
| STRLEN qlen; |
| 130 |
2 |
| const char *qstr = SvPV_nomg(*svp, qlen); |
| 131 |
2 |
| Newx(s->query_id, qlen + 1, char); |
| 132 |
2 |
| Copy(qstr, s->query_id, qlen, char); |
| 133 |
2 |
| s->query_id[qlen] = '\0'; |
| 134 |
|
| } |
| 135 |
2 |
| svp = hv_fetch(settings, "query_timeout", 13, 0); |
| 136 |
2 |
▶ | if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
Branch: 3 branches
| Branch | True | False | Total |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
0 |
2 |
2 |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
0 |
0 |
0 |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
0 |
0 |
0 |
|
| 137 |
2 |
▶ | if (svp && SvOK(*svp)) s->query_timeout = SvNV_nomg(*svp); |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) s->query_timeout = SvNV_nomg(*svp); |
0 |
2 |
2 |
| if (svp && SvOK(*svp)) s->query_timeout = SvNV_nomg(*svp); |
0 |
0 |
0 |
|
| 138 |
2 |
| } |
| 139 |
|
| |
| 140 |
|
| /* If settings has params => { x => 1 }, return a new HV* copy with the |
| 141 |
|
| * param keys flattened to param_x => '1'. Caller owns the returned HV |
| 142 |
|
| * (SvREFCNT_dec it). Returns NULL if no params key — caller continues |
| 143 |
|
| * to use the original settings hashref. */ |
| 144 |
2 |
| static HV* expand_params(pTHX_ HV *settings) { |
| 145 |
2 |
| SV **svp = hv_fetch(settings, "params", 6, 0); |
| 146 |
2 |
▶ | if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
Branch: 3 branches
| Branch | True | False | Total |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
0 |
2 |
2 |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
0 |
0 |
0 |
| if (svp && *svp && SvGMAGICAL(*svp)) mg_get(*svp); |
0 |
0 |
0 |
|
| 147 |
2 |
▶ | if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) |
0 |
2 |
2 |
| if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) |
0 |
0 |
0 |
| if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) |
0 |
0 |
0 |
|
| 148 |
2 |
| return NULL; |
| 149 |
0 |
| HV *phv = (HV *)SvRV(*svp); |
| 150 |
0 |
| HV *copy = newHVhv(settings); |
| 151 |
|
| HE *pe; |
| 152 |
0 |
| hv_iterinit(phv); |
| 153 |
0 |
▶ | while ((pe = hv_iternext(phv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((pe = hv_iternext(phv))) { |
0 |
0 |
0 |
|
| 154 |
|
| I32 pklen; |
| 155 |
0 |
| char *pkey = hv_iterkey(pe, &pklen); |
| 156 |
0 |
| SV *pval = hv_iterval(phv, pe); |
| 157 |
|
| char *prefixed; |
| 158 |
0 |
| Newx(prefixed, pklen + 7, char); |
| 159 |
0 |
| Copy("param_", prefixed, 6, char); |
| 160 |
0 |
| Copy(pkey, prefixed + 6, pklen, char); |
| 161 |
0 |
| (void)hv_store(copy, prefixed, pklen + 6, newSVsv(pval), 0); |
| 162 |
0 |
| Safefree(prefixed); |
| 163 |
|
| } |
| 164 |
0 |
| return copy; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* Append send entry to queue and dispatch if idle. */ |
| 168 |
55 |
| static void enqueue_send(ev_clickhouse_t *self, ev_ch_send_t *s) { |
| 169 |
55 |
| ngx_queue_insert_tail(&self->send_queue, &s->queue); |
| 170 |
55 |
| self->pending_count++; |
| 171 |
55 |
▶ | if (self->connected && self->callback_depth == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connected && self->callback_depth == 0) |
49 |
6 |
55 |
| if (self->connected && self->callback_depth == 0) |
6 |
43 |
49 |
|
| 172 |
6 |
| pipeline_advance(self); |
| 173 |
55 |
| } |