Code

frontend: Added basic support for semantic AST analysis.
[sysdb.git] / src / frontend / query.c
1 /*
2  * SysDB - src/frontend/query.c
3  * Copyright (C) 2013 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 typedef struct {
45         sdb_strbuf_t *buf;
46         sdb_store_matcher_t *filter;
48         size_t last_len;
49 } tojson_data_t;
51 static int
52 lookup_tojson(sdb_store_obj_t *obj, void *user_data)
53 {
54         tojson_data_t *data = user_data;
55         int status;
57         if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL)))
58                 return 0;
60         if (sdb_strbuf_len(data->buf) > data->last_len)
61                 sdb_strbuf_append(data->buf, ",");
62         data->last_len = sdb_strbuf_len(data->buf);
63         status = sdb_store_host_tojson(obj, data->buf,
64                         data->filter, /* flags = */ 0);
65         return status;
66 } /* lookup_tojson */
68 /*
69  * public API
70  */
72 int
73 sdb_fe_query(sdb_conn_t *conn)
74 {
75         sdb_llist_t *parsetree;
76         sdb_conn_node_t *node = NULL;
77         int status = 0;
79         if ((! conn) || (conn->cmd != CONNECTION_QUERY))
80                 return -1;
82         parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf),
83                         (int)conn->cmd_len);
84         if (! parsetree) {
85                 char query[conn->cmd_len + 1];
86                 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
87                 query[sizeof(query) - 1] = '\0';
88                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'",
89                                 query);
90                 return -1;
91         }
93         switch (sdb_llist_len(parsetree)) {
94                 case 0:
95                         /* skipping empty command; send back an empty reply */
96                         sdb_connection_send(conn, CONNECTION_DATA, 0, NULL);
97                         break;
98                 case 1:
99                         node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
100                         break;
102                 default:
103                         {
104                                 char query[conn->cmd_len + 1];
105                                 strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
106                                 query[sizeof(query) - 1] = '\0';
107                                 sdb_log(SDB_LOG_WARNING, "frontend: Ignoring %zu command%s "
108                                                 "in multi-statement query '%s'",
109                                                 sdb_llist_len(parsetree) - 1,
110                                                 sdb_llist_len(parsetree) == 2 ? "" : "s",
111                                                 query);
112                                 node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0));
113                         }
114         }
116         if (node) {
117                 if (sdb_fe_analyze(node)) {
118                         char query[conn->cmd_len + 1];
119                         strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
120                         query[sizeof(query) - 1] = '\0';
121                         sdb_log(SDB_LOG_ERR, "frontend: Failed to verify query '%s'",
122                                         query);
123                         status = -1;
124                 }
125                 else
126                         status = sdb_fe_exec(conn, node);
127                 sdb_object_deref(SDB_OBJ(node));
128         }
130         sdb_llist_destroy(parsetree);
131         return status;
132 } /* sdb_fe_query */
134 int
135 sdb_fe_fetch(sdb_conn_t *conn)
137         char name[conn->cmd_len + 1];
138         int type;
140         if ((! conn) || (conn->cmd != CONNECTION_FETCH))
141                 return -1;
143         if (conn->cmd_len < sizeof(type)) {
144                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
145                                 "FETCH command", conn->cmd_len);
146                 sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d",
147                                 conn->cmd_len);
148                 return -1;
149         }
151         type = sdb_proto_get_int(conn->buf, 0);
152         strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(type),
153                         conn->cmd_len - sizeof(type));
154         name[sizeof(name) - 1] = '\0';
155         return sdb_fe_exec_fetch(conn, type, name, /* filter = */ NULL);
156 } /* sdb_fe_fetch */
158 int
159 sdb_fe_list(sdb_conn_t *conn)
161         int type = SDB_HOST;
163         if ((! conn) || (conn->cmd != CONNECTION_LIST))
164                 return -1;
166         if (conn->cmd_len == sizeof(uint32_t))
167                 type = sdb_proto_get_int(conn->buf, 0);
168         else if (conn->cmd_len) {
169                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
170                                 "LIST command", conn->cmd_len);
171                 sdb_strbuf_sprintf(conn->errbuf, "LIST: Invalid command length %d",
172                                 conn->cmd_len);
173                 return -1;
174         }
175         return sdb_fe_exec_list(conn, type, /* filter = */ NULL);
176 } /* sdb_fe_list */
178 int
179 sdb_fe_lookup(sdb_conn_t *conn)
181         sdb_store_matcher_t *m;
182         const char *matcher;
183         size_t matcher_len;
185         uint32_t type;
186         int status;
188         conn_matcher_t m_node = {
189                 { SDB_OBJECT_INIT, CONNECTION_MATCHER }, NULL
190         };
191         conn_lookup_t node = {
192                 { SDB_OBJECT_INIT, CONNECTION_LOOKUP },
193                 -1, &m_node, NULL
194         };
196         if ((! conn) || (conn->cmd != CONNECTION_LOOKUP))
197                 return -1;
199         if (conn->cmd_len < sizeof(type)) {
200                 sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for "
201                                 "LOOKUP command", conn->cmd_len);
202                 sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d",
203                                 conn->cmd_len);
204                 return -1;
205         }
206         type = sdb_proto_get_int(conn->buf, 0);
208         matcher = sdb_strbuf_string(conn->buf) + sizeof(type);
209         matcher_len = conn->cmd_len - sizeof(type);
210         m = sdb_fe_parse_matcher(matcher, (int)matcher_len);
211         if (! m) {
212                 char expr[matcher_len + 1];
213                 strncpy(expr, matcher, sizeof(expr));
214                 expr[sizeof(expr) - 1] = '\0';
215                 sdb_log(SDB_LOG_ERR, "frontend: Failed to parse "
216                                 "lookup condition '%s'", expr);
217                 return -1;
218         }
220         node.type = type;
221         m_node.matcher = m;
223         if (sdb_fe_analyze(SDB_CONN_NODE(&node))) {
224                 char expr[matcher_len + 1];
225                 strncpy(expr, matcher, sizeof(expr));
226                 expr[sizeof(expr) - 1] = '\0';
227                 sdb_log(SDB_LOG_ERR, "frontend: Failed to verify "
228                                 "lookup condition '%s'", expr);
229                 status = -1;
230         }
231         else
232                 status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
233         sdb_object_deref(SDB_OBJ(m));
234         return status;
235 } /* sdb_fe_lookup */
237 int
238 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
240         sdb_store_matcher_t *m = NULL, *filter = NULL;
242         if (! node)
243                 return -1;
245         switch (node->cmd) {
246                 case CONNECTION_FETCH:
247                         if (CONN_FETCH(node)->filter)
248                                 filter = CONN_FETCH(node)->filter->matcher;
249                         return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type,
250                                         CONN_FETCH(node)->name, filter);
251                 case CONNECTION_LIST:
252                         if (CONN_LIST(node)->filter)
253                                 filter = CONN_LIST(node)->filter->matcher;
254                         return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
255                 case CONNECTION_LOOKUP:
256                         if (CONN_LOOKUP(node)->matcher)
257                                 m = CONN_LOOKUP(node)->matcher->matcher;
258                         if (CONN_LOOKUP(node)->filter)
259                                 filter = CONN_LOOKUP(node)->filter->matcher;
260                         return sdb_fe_exec_lookup(conn,
261                                         CONN_LOOKUP(node)->type, m, filter);
262                 case CONNECTION_TIMESERIES:
263                         return sdb_fe_exec_timeseries(conn,
264                                         CONN_TS(node)->hostname, CONN_TS(node)->metric,
265                                         &CONN_TS(node)->opts);
267                 default:
268                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
269                         return -1;
270         }
271         return -1;
272 } /* sdb_fe_exec */
274 int
275 sdb_fe_exec_fetch(sdb_conn_t *conn, int type, const char *name,
276                 sdb_store_matcher_t *filter)
278         sdb_strbuf_t *buf;
279         sdb_store_obj_t *host;
280         uint32_t res_type = htonl(CONNECTION_FETCH);
282         /* XXX: support other types */
283         if (type != SDB_HOST) {
284                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
285                                 "in FETCH command", type);
286                 sdb_strbuf_sprintf(conn->errbuf,
287                                 "FETCH: Invalid object type %d", type);
288                 return -1;
289         }
291         host = sdb_store_get_host(name);
292         if (! host) {
293                 sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': "
294                                 "not found", name);
296                 sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name);
297                 return -1;
298         }
300         buf = sdb_strbuf_create(1024);
301         if (! buf) {
302                 char errbuf[1024];
303                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
304                                 "buffer to handle FETCH command: %s",
305                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
307                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
308                 sdb_strbuf_destroy(buf);
309                 sdb_object_deref(SDB_OBJ(host));
310                 return -1;
311         }
313         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
314         if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) {
315                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
316                                 "host '%s' to JSON", name);
317                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
318                 sdb_strbuf_destroy(buf);
319                 sdb_object_deref(SDB_OBJ(host));
320                 return -1;
321         }
323         sdb_connection_send(conn, CONNECTION_DATA,
324                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
325         sdb_strbuf_destroy(buf);
326         sdb_object_deref(SDB_OBJ(host));
327         return 0;
328 } /* sdb_fe_exec_fetch */
330 int
331 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
333         sdb_strbuf_t *buf;
334         uint32_t res_type = htonl(CONNECTION_LIST);
336         int flags;
338         if (type == SDB_HOST)
339                 flags = SDB_SKIP_ALL;
340         else if (type == SDB_SERVICE)
341                 flags = (SDB_SKIP_ALL & (~SDB_SKIP_SERVICES))
342                         | SDB_SKIP_EMPTY_SERVICES;
343         else if (type == SDB_METRIC)
344                 flags = (SDB_SKIP_ALL & (~SDB_SKIP_METRICS))
345                         | SDB_SKIP_EMPTY_METRICS;
346         else {
347                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
348                                 "for LIST command", type);
349                 sdb_strbuf_sprintf(conn->errbuf,
350                                 "LIST: Invalid object type %d", type);
351                 return -1;
352         }
354         buf = sdb_strbuf_create(1024);
355         if (! buf) {
356                 char errbuf[1024];
357                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
358                                 "buffer to handle LIST command: %s",
359                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
361                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
362                 sdb_strbuf_destroy(buf);
363                 return -1;
364         }
366         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
367         if (sdb_store_tojson(buf, filter, flags)) {
368                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
369                                 "store to JSON");
370                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
371                 sdb_strbuf_destroy(buf);
372                 return -1;
373         }
375         sdb_connection_send(conn, CONNECTION_DATA,
376                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
377         sdb_strbuf_destroy(buf);
378         return 0;
379 } /* sdb_fe_exec_list */
381 int
382 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
383                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
385         tojson_data_t data = { NULL, filter, 0 };
386         uint32_t res_type = htonl(CONNECTION_LOOKUP);
388         /* XXX: support other types */
389         if (type != SDB_HOST) {
390                 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
391                                 "in LOOKUP command", type);
392                 sdb_strbuf_sprintf(conn->errbuf,
393                                 "LOOKUP: Invalid object type %d", type);
394                 return -1;
395         }
397         data.buf = sdb_strbuf_create(1024);
398         if (! data.buf) {
399                 char errbuf[1024];
400                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
401                                 "buffer to handle LOOKUP command: %s",
402                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
404                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
405                 sdb_strbuf_destroy(data.buf);
406                 return -1;
407         }
409         sdb_strbuf_memcpy(data.buf, &res_type, sizeof(uint32_t));
410         sdb_strbuf_append(data.buf, "[");
412         /* Let the JSON serializer handle the filter instead of the scanner. Else,
413          * we'd have to filter twice -- once in the scanner and then again in the
414          * serializer. */
415         data.last_len = sdb_strbuf_len(data.buf);
416         if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) {
417                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
418                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
419                 sdb_strbuf_destroy(data.buf);
420                 return -1;
421         }
423         sdb_strbuf_append(data.buf, "]");
425         sdb_connection_send(conn, CONNECTION_DATA,
426                         (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf));
427         sdb_strbuf_destroy(data.buf);
428         return 0;
429 } /* sdb_fe_exec_lookup */
431 int
432 sdb_fe_exec_timeseries(sdb_conn_t *conn,
433                 const char *hostname, const char *metric,
434                 sdb_timeseries_opts_t *opts)
436         sdb_strbuf_t *buf;
437         uint32_t res_type = htonl(CONNECTION_TIMESERIES);
439         buf = sdb_strbuf_create(1024);
440         if (! buf) {
441                 char errbuf[1024];
442                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
443                                 "buffer to handle TIMESERIES command: %s",
444                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
446                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
447                 return -1;
448         }
450         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
451         if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
452                 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
453                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
454                 sdb_strbuf_destroy(buf);
455                 return -1;
456         }
458         sdb_connection_send(conn, CONNECTION_DATA,
459                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
460         sdb_strbuf_destroy(buf);
461         return 0;
462 } /* sdb_fe_exec_timeseries */
464 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */