Code

e5a1fc43263b925653f71539ca369a5f677646b7
[sysdb.git] / src / frontend / query.c
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);
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'",
82                                 query);
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)
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)
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)
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);
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'", expr);
202                 return -1;
203         }
205         node.type = type;
206         m_node.matcher = m;
208         /* run analyzer separately; parse_matcher is missing
209          * the right context to do so */
210         if (sdb_fe_analyze(SDB_CONN_NODE(&node))) {
211                 char expr[matcher_len + 1];
212                 strncpy(expr, matcher, sizeof(expr));
213                 expr[sizeof(expr) - 1] = '\0';
214                 sdb_log(SDB_LOG_ERR, "frontend: Failed to verify "
215                                 "lookup condition '%s'", expr);
216                 status = -1;
217         }
218         else
219                 status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
220         sdb_object_deref(SDB_OBJ(m));
221         return status;
222 } /* sdb_fe_lookup */
224 int
225 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
227         sdb_store_matcher_t *m = NULL, *filter = NULL;
229         if (! node)
230                 return -1;
232         switch (node->cmd) {
233                 case CONNECTION_FETCH:
234                         if (CONN_FETCH(node)->filter)
235                                 filter = CONN_FETCH(node)->filter->matcher;
236                         return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type,
237                                         CONN_FETCH(node)->host, CONN_FETCH(node)->name, filter);
238                 case CONNECTION_LIST:
239                         if (CONN_LIST(node)->filter)
240                                 filter = CONN_LIST(node)->filter->matcher;
241                         return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
242                 case CONNECTION_LOOKUP:
243                         if (CONN_LOOKUP(node)->matcher)
244                                 m = CONN_LOOKUP(node)->matcher->matcher;
245                         if (CONN_LOOKUP(node)->filter)
246                                 filter = CONN_LOOKUP(node)->filter->matcher;
247                         return sdb_fe_exec_lookup(conn,
248                                         CONN_LOOKUP(node)->type, m, filter);
249                 case CONNECTION_TIMESERIES:
250                         return sdb_fe_exec_timeseries(conn,
251                                         CONN_TS(node)->hostname, CONN_TS(node)->metric,
252                                         &CONN_TS(node)->opts);
254                 default:
255                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
256                         return -1;
257         }
258         return -1;
259 } /* sdb_fe_exec */
261 int
262 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
263                 const char *hostname, const char *name, sdb_store_matcher_t *filter)
265         uint32_t res_type = htonl(CONNECTION_FETCH);
267         sdb_store_obj_t *host;
268         sdb_store_obj_t *obj;
270         sdb_store_json_formatter_t *f;
271         sdb_strbuf_t *buf;
273         if ((! hostname) || ((type == SDB_HOST) && name)
274                         || ((type != SDB_HOST) && (! name))) {
275                 /* This is a programming error, not something the client did wrong */
276                 sdb_strbuf_sprintf(conn->errbuf, "INTERNAL ERROR: invalid "
277                                 "arguments to sdb_fe_exec_fetch(%s, %s, %s)",
278                                 SDB_STORE_TYPE_TO_NAME(type), hostname, name);
279                 return -1;
280         }
282         host = sdb_store_get_host(hostname);
283         if ((! host) || (filter
284                                 && (! sdb_store_matcher_matches(filter, host, NULL)))) {
285                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: "
286                                 "host %s not found", SDB_STORE_TYPE_TO_NAME(type),
287                                 name, hostname);
288                 return -1;
289         }
290         if (type == SDB_HOST) {
291                 obj = host;
292         }
293         else {
294                 obj = sdb_store_get_child(host, type, name);
295                 if ((! obj) || (filter
296                                         && (! sdb_store_matcher_matches(filter, obj, NULL)))) {
297                         sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: "
298                                         "%s not found", SDB_STORE_TYPE_TO_NAME(type),
299                                         hostname, name, name);
300                         return -1;
301                 }
302                 sdb_object_deref(SDB_OBJ(host));
303         }
305         buf = sdb_strbuf_create(1024);
306         if (! buf) {
307                 char errbuf[1024];
308                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
309                                 "buffer to handle FETCH command: %s",
310                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
312                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
313                 sdb_strbuf_destroy(buf);
314                 sdb_object_deref(SDB_OBJ(obj));
315                 return -1;
316         }
317         f = sdb_store_json_formatter(buf, type, /* flags = */ 0);
318         if (! f) {
319                 char errbuf[1024];
320                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
321                                 "JSON formatter to handle FETCH command: %s",
322                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
324                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
325                 sdb_strbuf_destroy(buf);
326                 sdb_object_deref(SDB_OBJ(obj));
327                 return -1;
328         }
330         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
331         if (sdb_store_json_emit_full(f, obj, filter)) {
332                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
333                                 "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type),
334                                 hostname, name);
335                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
336                 sdb_strbuf_destroy(buf);
337                 free(f);
338                 sdb_object_deref(SDB_OBJ(obj));
339                 return -1;
340         }
341         sdb_store_json_finish(f);
343         sdb_connection_send(conn, CONNECTION_DATA,
344                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
345         sdb_strbuf_destroy(buf);
346         free(f);
347         sdb_object_deref(SDB_OBJ(obj));
348         return 0;
349 } /* sdb_fe_exec_fetch */
351 int
352 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
354         uint32_t res_type = htonl(CONNECTION_LIST);
356         sdb_store_json_formatter_t *f;
357         sdb_strbuf_t *buf;
359         buf = sdb_strbuf_create(1024);
360         if (! buf) {
361                 char errbuf[1024];
362                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
363                                 "buffer to handle LIST command: %s",
364                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
366                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
367                 sdb_strbuf_destroy(buf);
368                 return -1;
369         }
370         f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
371         if (! f) {
372                 char errbuf[1024];
373                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
374                                 "JSON formatter to handle LIST command: %s",
375                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
377                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
378                 sdb_strbuf_destroy(buf);
379                 return -1;
380         }
382         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
383         if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) {
384                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
385                                 "store to JSON");
386                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
387                 sdb_strbuf_destroy(buf);
388                 free(f);
389                 return -1;
390         }
391         sdb_store_json_finish(f);
393         sdb_connection_send(conn, CONNECTION_DATA,
394                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
395         sdb_strbuf_destroy(buf);
396         free(f);
397         return 0;
398 } /* sdb_fe_exec_list */
400 int
401 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
402                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
404         uint32_t res_type = htonl(CONNECTION_LOOKUP);
406         sdb_store_json_formatter_t *f;
407         sdb_strbuf_t *buf;
409         /* XXX: support other types */
410         if (type != SDB_HOST) {
411                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
412                                 "in LOOKUP command", type);
413                 sdb_strbuf_sprintf(conn->errbuf,
414                                 "LOOKUP: Invalid object type %d", type);
415                 return -1;
416         }
418         buf = sdb_strbuf_create(1024);
419         if (! buf) {
420                 char errbuf[1024];
421                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
422                                 "buffer to handle LOOKUP command: %s",
423                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
425                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
426                 return -1;
427         }
428         f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
429         if (! f) {
430                 char errbuf[1024];
431                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
432                                 "JSON formatter to handle LOOKUP command: %s",
433                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
435                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
436                 sdb_strbuf_destroy(buf);
437                 return -1;
438         }
440         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
442         if (sdb_store_scan(SDB_HOST, m, filter, lookup_tojson, f)) {
443                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
444                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
445                 sdb_strbuf_destroy(buf);
446                 free(f);
447                 return -1;
448         }
449         sdb_store_json_finish(f);
451         sdb_connection_send(conn, CONNECTION_DATA,
452                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
453         sdb_strbuf_destroy(buf);
454         free(f);
455         return 0;
456 } /* sdb_fe_exec_lookup */
458 int
459 sdb_fe_exec_timeseries(sdb_conn_t *conn,
460                 const char *hostname, const char *metric,
461                 sdb_timeseries_opts_t *opts)
463         sdb_strbuf_t *buf;
464         uint32_t res_type = htonl(CONNECTION_TIMESERIES);
466         buf = sdb_strbuf_create(1024);
467         if (! buf) {
468                 char errbuf[1024];
469                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
470                                 "buffer to handle TIMESERIES command: %s",
471                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
473                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
474                 return -1;
475         }
477         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
478         if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
479                 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
480                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
481                 sdb_strbuf_destroy(buf);
482                 return -1;
483         }
485         sdb_connection_send(conn, CONNECTION_DATA,
486                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
487         sdb_strbuf_destroy(buf);
488         return 0;
489 } /* sdb_fe_exec_timeseries */
491 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */