1 /*
2 * SysDB - src/frontend/query.c
3 * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "sysdb.h"
30 #include "core/store.h"
31 #include "frontend/connection-private.h"
32 #include "frontend/parser.h"
33 #include "utils/error.h"
34 #include "utils/proto.h"
35 #include "utils/strbuf.h"
37 #include <errno.h>
38 #include <string.h>
40 /*
41 * private helper functions
42 */
44 static int
45 list_tojson(sdb_store_obj_t *obj,
46 sdb_store_matcher_t __attribute__((unused)) *filter,
47 void *user_data)
48 {
49 sdb_store_json_formatter_t *f = user_data;
50 return sdb_store_json_emit(f, obj);
51 } /* list_tojson */
53 static int
54 lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
55 void *user_data)
56 {
57 sdb_store_json_formatter_t *f = user_data;
58 return sdb_store_json_emit_full(f, obj, filter);
59 } /* lookup_tojson */
61 /*
62 * public API
63 */
65 int
66 sdb_fe_query(sdb_conn_t *conn)
67 {
68 sdb_llist_t *parsetree;
69 sdb_conn_node_t *node = NULL;
70 int status = 0;
72 if ((! conn) || (conn->cmd != CONNECTION_QUERY))
73 return -1;
75 parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
76 (int)conn->cmd_len, conn->errbuf);
77 if (! parsetree) {
78 char query[conn->cmd_len + 1];
79 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
80 query[sizeof(query) - 1] = '\0';
81 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s': %s",
82 query, sdb_strbuf_string(conn->errbuf));
83 return -1;
84 }
86 switch (sdb_llist_len(parsetree)) {
87 case 0:
88 /* skipping empty command; send back an empty reply */
89 sdb_connection_send(conn, CONNECTION_DATA, 0, NULL);
90 break;
91 case 1:
92 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
93 break;
95 default:
96 {
97 char query[conn->cmd_len + 1];
98 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
99 query[sizeof(query) - 1] = '\0';
100 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
101 "in multi-statement query '%s'",
102 sdb_llist_len(parsetree) - 1,
103 sdb_llist_len(parsetree) == 2 ? "" : "s",
104 query);
105 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
106 }
107 }
109 if (node) {
110 status = sdb_fe_exec(conn, node);
111 sdb_object_deref(SDB_OBJ(node));
112 }
114 sdb_llist_destroy(parsetree);
115 return status;
116 } /* sdb_fe_query */
118 int
119 sdb_fe_fetch(sdb_conn_t *conn)
120 {
121 char name[conn->cmd_len + 1];
122 int type;
124 if ((! conn) || (conn->cmd != CONNECTION_FETCH))
125 return -1;
127 if (conn->cmd_len < sizeof(uint32_t)) {
128 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
129 "FETCH command", conn->cmd_len);
130 sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d",
131 conn->cmd_len);
132 return -1;
133 }
135 type = sdb_proto_get_int(conn->buf, 0);
136 strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t),
137 conn->cmd_len - sizeof(uint32_t));
138 name[sizeof(name) - 1] = '\0';
139 /* TODO: support other types besides hosts */
140 return sdb_fe_exec_fetch(conn, type, name, NULL, /* filter = */ NULL);
141 } /* sdb_fe_fetch */
143 int
144 sdb_fe_list(sdb_conn_t *conn)
145 {
146 int type = SDB_HOST;
148 if ((! conn) || (conn->cmd != CONNECTION_LIST))
149 return -1;
151 if (conn->cmd_len == sizeof(uint32_t))
152 type = sdb_proto_get_int(conn->buf, 0);
153 else if (conn->cmd_len) {
154 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
155 "LIST command", conn->cmd_len);
156 sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
157 conn->cmd_len);
158 return -1;
159 }
160 return sdb_fe_exec_list(conn, type, /* filter = */ NULL);
161 } /* sdb_fe_list */
163 int
164 sdb_fe_lookup(sdb_conn_t *conn)
165 {
166 sdb_store_matcher_t *m;
167 const char *matcher;
168 size_t matcher_len;
170 int type;
171 int status;
173 conn_matcher_t m_node = {
174 { SDB_OBJECT_INIT, CONNECTION_MATCHER }, NULL
175 };
176 conn_lookup_t node = {
177 { SDB_OBJECT_INIT, CONNECTION_LOOKUP },
178 -1, &m_node, NULL
179 };
181 if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
182 return -1;
184 if (conn->cmd_len < sizeof(uint32_t)) {
185 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
186 "LOOKUP command", conn->cmd_len);
187 sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d",
188 conn->cmd_len);
189 return -1;
190 }
191 type = sdb_proto_get_int(conn->buf, 0);
193 matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t);
194 matcher_len = conn->cmd_len - sizeof(uint32_t);
195 m = sdb_fe_parse_matcher(matcher, (int)matcher_len, conn->errbuf);
196 if (! m) {
197 char expr[matcher_len + 1];
198 strncpy(expr, matcher, sizeof(expr));
199 expr[sizeof(expr) - 1] = '\0';
200 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse "
201 "lookup condition '%s': %s", expr,
202 sdb_strbuf_string(conn->errbuf));
203 return -1;
204 }
206 node.type = type;
207 m_node.matcher = m;
209 /* run analyzer separately; parse_matcher is missing
210 * the right context to do so */
211 if (sdb_fe_analyze(SDB_CONN_NODE(&node), conn->errbuf)) {
212 char expr[matcher_len + 1];
213 char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64];
214 strncpy(expr, matcher, sizeof(expr));
215 expr[sizeof(expr) - 1] = '\0';
216 snprintf(err, sizeof(err), "Failed to parse "
217 "lookup condition '%s': %s", expr,
218 sdb_strbuf_string(conn->errbuf));
219 sdb_strbuf_sprintf(conn->errbuf, "%s", err);
220 status = -1;
221 }
222 else
223 status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
224 sdb_object_deref(SDB_OBJ(m));
225 return status;
226 } /* sdb_fe_lookup */
228 int
229 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
230 {
231 sdb_store_matcher_t *m = NULL, *filter = NULL;
233 if (! node)
234 return -1;
236 switch (node->cmd) {
237 case CONNECTION_FETCH:
238 if (CONN_FETCH(node)->filter)
239 filter = CONN_FETCH(node)->filter->matcher;
240 return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type,
241 CONN_FETCH(node)->host, CONN_FETCH(node)->name, filter);
242 case CONNECTION_LIST:
243 if (CONN_LIST(node)->filter)
244 filter = CONN_LIST(node)->filter->matcher;
245 return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
246 case CONNECTION_LOOKUP:
247 if (CONN_LOOKUP(node)->matcher)
248 m = CONN_LOOKUP(node)->matcher->matcher;
249 if (CONN_LOOKUP(node)->filter)
250 filter = CONN_LOOKUP(node)->filter->matcher;
251 return sdb_fe_exec_lookup(conn,
252 CONN_LOOKUP(node)->type, m, filter);
253 case CONNECTION_TIMESERIES:
254 return sdb_fe_exec_timeseries(conn,
255 CONN_TS(node)->hostname, CONN_TS(node)->metric,
256 &CONN_TS(node)->opts);
258 default:
259 sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
260 return -1;
261 }
262 return -1;
263 } /* sdb_fe_exec */
265 int
266 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
267 const char *hostname, const char *name, sdb_store_matcher_t *filter)
268 {
269 uint32_t res_type = htonl(CONNECTION_FETCH);
271 sdb_store_obj_t *host;
272 sdb_store_obj_t *obj;
274 sdb_store_json_formatter_t *f;
275 sdb_strbuf_t *buf;
277 if ((! hostname) || ((type == SDB_HOST) && name)
278 || ((type != SDB_HOST) && (! name))) {
279 /* This is a programming error, not something the client did wrong */
280 sdb_strbuf_sprintf(conn->errbuf, "INTERNAL ERROR: invalid "
281 "arguments to sdb_fe_exec_fetch(%s, %s, %s)",
282 SDB_STORE_TYPE_TO_NAME(type), hostname, name);
283 return -1;
284 }
286 host = sdb_store_get_host(hostname);
287 if ((! host) || (filter
288 && (! sdb_store_matcher_matches(filter, host, NULL)))) {
289 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: "
290 "host %s not found", SDB_STORE_TYPE_TO_NAME(type),
291 name, hostname);
292 return -1;
293 }
294 if (type == SDB_HOST) {
295 obj = host;
296 }
297 else {
298 obj = sdb_store_get_child(host, type, name);
299 if ((! obj) || (filter
300 && (! sdb_store_matcher_matches(filter, obj, NULL)))) {
301 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: "
302 "%s not found", SDB_STORE_TYPE_TO_NAME(type),
303 hostname, name, name);
304 return -1;
305 }
306 sdb_object_deref(SDB_OBJ(host));
307 }
309 buf = sdb_strbuf_create(1024);
310 if (! buf) {
311 char errbuf[1024];
312 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
313 "buffer to handle FETCH command: %s",
314 sdb_strerror(errno, errbuf, sizeof(errbuf)));
316 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
317 sdb_strbuf_destroy(buf);
318 sdb_object_deref(SDB_OBJ(obj));
319 return -1;
320 }
321 f = sdb_store_json_formatter(buf, type, /* flags = */ 0);
322 if (! f) {
323 char errbuf[1024];
324 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
325 "JSON formatter to handle FETCH command: %s",
326 sdb_strerror(errno, errbuf, sizeof(errbuf)));
328 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
329 sdb_strbuf_destroy(buf);
330 sdb_object_deref(SDB_OBJ(obj));
331 return -1;
332 }
334 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
335 if (sdb_store_json_emit_full(f, obj, filter)) {
336 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
337 "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type),
338 hostname, name);
339 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
340 sdb_strbuf_destroy(buf);
341 free(f);
342 sdb_object_deref(SDB_OBJ(obj));
343 return -1;
344 }
345 sdb_store_json_finish(f);
347 sdb_connection_send(conn, CONNECTION_DATA,
348 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
349 sdb_strbuf_destroy(buf);
350 free(f);
351 sdb_object_deref(SDB_OBJ(obj));
352 return 0;
353 } /* sdb_fe_exec_fetch */
355 int
356 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
357 {
358 uint32_t res_type = htonl(CONNECTION_LIST);
360 sdb_store_json_formatter_t *f;
361 sdb_strbuf_t *buf;
363 buf = sdb_strbuf_create(1024);
364 if (! buf) {
365 char errbuf[1024];
366 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
367 "buffer to handle LIST command: %s",
368 sdb_strerror(errno, errbuf, sizeof(errbuf)));
370 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
371 sdb_strbuf_destroy(buf);
372 return -1;
373 }
374 f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
375 if (! f) {
376 char errbuf[1024];
377 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
378 "JSON formatter to handle LIST command: %s",
379 sdb_strerror(errno, errbuf, sizeof(errbuf)));
381 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
382 sdb_strbuf_destroy(buf);
383 return -1;
384 }
386 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
387 if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) {
388 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
389 "store to JSON");
390 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
391 sdb_strbuf_destroy(buf);
392 free(f);
393 return -1;
394 }
395 sdb_store_json_finish(f);
397 sdb_connection_send(conn, CONNECTION_DATA,
398 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
399 sdb_strbuf_destroy(buf);
400 free(f);
401 return 0;
402 } /* sdb_fe_exec_list */
404 int
405 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
406 sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
407 {
408 uint32_t res_type = htonl(CONNECTION_LOOKUP);
410 sdb_store_json_formatter_t *f;
411 sdb_strbuf_t *buf;
413 buf = sdb_strbuf_create(1024);
414 if (! buf) {
415 char errbuf[1024];
416 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
417 "buffer to handle LOOKUP command: %s",
418 sdb_strerror(errno, errbuf, sizeof(errbuf)));
420 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
421 return -1;
422 }
423 f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
424 if (! f) {
425 char errbuf[1024];
426 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
427 "JSON formatter to handle LOOKUP command: %s",
428 sdb_strerror(errno, errbuf, sizeof(errbuf)));
430 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
431 sdb_strbuf_destroy(buf);
432 return -1;
433 }
435 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
437 if (sdb_store_scan(type, m, filter, lookup_tojson, f)) {
438 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup %ss",
439 SDB_STORE_TYPE_TO_NAME(type));
440 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup %ss",
441 SDB_STORE_TYPE_TO_NAME(type));
442 sdb_strbuf_destroy(buf);
443 free(f);
444 return -1;
445 }
446 sdb_store_json_finish(f);
448 sdb_connection_send(conn, CONNECTION_DATA,
449 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
450 sdb_strbuf_destroy(buf);
451 free(f);
452 return 0;
453 } /* sdb_fe_exec_lookup */
455 int
456 sdb_fe_exec_timeseries(sdb_conn_t *conn,
457 const char *hostname, const char *metric,
458 sdb_timeseries_opts_t *opts)
459 {
460 sdb_strbuf_t *buf;
461 uint32_t res_type = htonl(CONNECTION_TIMESERIES);
463 buf = sdb_strbuf_create(1024);
464 if (! buf) {
465 char errbuf[1024];
466 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
467 "buffer to handle TIMESERIES command: %s",
468 sdb_strerror(errno, errbuf, sizeof(errbuf)));
470 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
471 return -1;
472 }
474 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
475 if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
476 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
477 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
478 sdb_strbuf_destroy(buf);
479 return -1;
480 }
482 sdb_connection_send(conn, CONNECTION_DATA,
483 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
484 sdb_strbuf_destroy(buf);
485 return 0;
486 } /* sdb_fe_exec_timeseries */
488 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */