| 1160 | } |
| 1161 | |
| 1162 | int dohsql_distribute(dohsql_node_t *node) |
| 1163 | { |
| 1164 | GET_CLNT; |
| 1165 | dohsql_t *conns; |
| 1166 | int i, rc; |
| 1167 | int clnt_nparams; |
| 1168 | int flags = 0; |
| 1169 | |
| 1170 | if (gbl_dohsql_max_threads && node->nnodes > gbl_dohsql_max_threads) { |
| 1171 | return SHARD_ERR_TOOMANYTHR; |
| 1172 | } |
| 1173 | |
| 1174 | clnt_nparams = param_count(clnt); |
| 1175 | if (clnt_nparams != node->nparams) { |
| 1176 | return SHARD_ERR_PARAMS; |
| 1177 | } |
| 1178 | |
| 1179 | rc = _add_parallel_load(clnt); |
| 1180 | if (rc) |
| 1181 | return rc; |
| 1182 | |
| 1183 | /* setup communication queue */ |
| 1184 | conns = (dohsql_t *)calloc( |
| 1185 | 1, sizeof(dohsql_t) + node->nnodes * sizeof(dohsql_connector_t)); |
| 1186 | if (!conns) |
| 1187 | return SHARD_ERR_MALLOC; |
| 1188 | conns->conns = (dohsql_connector_t *)(conns + 1); |
| 1189 | conns->nconns = node->nnodes; |
| 1190 | conns->ncols = node->ncols; |
| 1191 | conns->nparams = node->nparams; |
| 1192 | |
| 1193 | if (node->order_size) { |
| 1194 | if (order_init(conns, node)) { |
| 1195 | free(conns); |
| 1196 | return SHARD_ERR_MALLOC; |
| 1197 | } |
| 1198 | flags = THDPOOL_FORCE_DISPATCH; |
| 1199 | } |
| 1200 | /* there is a slack to allow non-coordinator tasks to drain; |
| 1201 | * it is still possible to fill the sql queue; force the |
| 1202 | * worker shards on the queue in any case |
| 1203 | */ |
| 1204 | flags |= THDPOOL_FORCE_QUEUE; |
| 1205 | clnt->conns = conns; |
| 1206 | /* augment interface */ |
| 1207 | _master_clnt_set(clnt); |
| 1208 | |
| 1209 | /* start peers */ |
| 1210 | for (i = 0; i < conns->nconns; i++) { |
| 1211 | struct param_data *params; |
| 1212 | int nparams; |
| 1213 | _save_params(node->nodes[i], ¶ms, &nparams); |
| 1214 | if ((rc = _shard_connect(clnt, &conns->conns[i], node->nodes[i]->sql, |
| 1215 | nparams, params)) != 0) |
| 1216 | return rc; |
| 1217 | |
| 1218 | if (i > 0) { |
| 1219 | struct string_ref *sr = create_string_ref(node->nodes[i]->sql); |
no test coverage detected