Code

store: Don't report an error in scan() if the store is still empty.
[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 != SDB_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, SDB_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         uint32_t type;
124         if ((! conn) || (conn->cmd != SDB_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         sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
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, (int)type, name, NULL, /* filter = */ NULL);
141 } /* sdb_fe_fetch */
143 int
144 sdb_fe_list(sdb_conn_t *conn)
146         uint32_t type = SDB_HOST;
148         if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST))
149                 return -1;
151         if (conn->cmd_len == sizeof(uint32_t))
152                 sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
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, (int)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         uint32_t type;
171         int status;
173         conn_matcher_t m_node = {
174                 { SDB_OBJECT_INIT, SDB_CONNECTION_MATCHER }, NULL
175         };
176         conn_lookup_t node = {
177                 { SDB_OBJECT_INIT, SDB_CONNECTION_LOOKUP },
178                 -1, &m_node, NULL
179         };
181         if ((! conn) || (conn->cmd != SDB_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         sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type);
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 = (int)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, (int)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)
231         sdb_store_matcher_t *m = NULL, *filter = NULL;
233         if (! node)
234                 return -1;
236         switch (node->cmd) {
237                 case SDB_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 SDB_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 SDB_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 SDB_CONNECTION_STORE_HOST:
254                 {
255                         conn_store_host_t *n = CONN_STORE_HOST(node);
256                         sdb_proto_host_t host = { n->last_update, n->name };
257                         return sdb_fe_store_host(conn, &host);
258                 }
259                 case SDB_CONNECTION_STORE_SERVICE:
260                 {
261                         conn_store_svc_t *n = CONN_STORE_SVC(node);
262                         sdb_proto_service_t svc = { n->last_update, n->hostname, n->name };
263                         return sdb_fe_store_service(conn, &svc);
264                 }
265                 case SDB_CONNECTION_STORE_METRIC:
266                 {
267                         conn_store_metric_t *n = CONN_STORE_METRIC(node);
268                         sdb_proto_metric_t metric = {
269                                 n->last_update, n->hostname, n->name,
270                                 n->store_type, n->store_id
271                         };
272                         return sdb_fe_store_metric(conn, &metric);
273                 }
274                 case SDB_CONNECTION_STORE_ATTRIBUTE:
275                 {
276                         conn_store_attr_t *n = CONN_STORE_ATTR(node);
277                         sdb_proto_attribute_t attr = {
278                                 n->last_update, n->parent_type, n->hostname, n->parent,
279                                 n->key, n->value
280                         };
281                         return sdb_fe_store_attribute(conn, &attr);
282                 }
283                 case SDB_CONNECTION_TIMESERIES:
284                         return sdb_fe_exec_timeseries(conn,
285                                         CONN_TS(node)->hostname, CONN_TS(node)->metric,
286                                         &CONN_TS(node)->opts);
288                 default:
289                         sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
290                         return -1;
291         }
292         return -1;
293 } /* sdb_fe_exec */
295 int
296 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
297                 const char *hostname, const char *name, sdb_store_matcher_t *filter)
299         uint32_t res_type = htonl(SDB_CONNECTION_FETCH);
301         sdb_store_obj_t *host;
302         sdb_store_obj_t *obj;
304         sdb_store_json_formatter_t *f;
305         sdb_strbuf_t *buf;
307         if ((! hostname) || ((type == SDB_HOST) && name)
308                         || ((type != SDB_HOST) && (! name))) {
309                 /* This is a programming error, not something the client did wrong */
310                 sdb_strbuf_sprintf(conn->errbuf, "INTERNAL ERROR: invalid "
311                                 "arguments to sdb_fe_exec_fetch(%s, %s, %s)",
312                                 SDB_STORE_TYPE_TO_NAME(type), hostname, name);
313                 return -1;
314         }
316         host = sdb_store_get_host(hostname);
317         if ((! host) || (filter
318                                 && (! sdb_store_matcher_matches(filter, host, NULL)))) {
319                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: "
320                                 "host %s not found", SDB_STORE_TYPE_TO_NAME(type),
321                                 name, hostname);
322                 return -1;
323         }
324         if (type == SDB_HOST) {
325                 obj = host;
326         }
327         else {
328                 obj = sdb_store_get_child(host, type, name);
329                 if ((! obj) || (filter
330                                         && (! sdb_store_matcher_matches(filter, obj, NULL)))) {
331                         sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: "
332                                         "%s not found", SDB_STORE_TYPE_TO_NAME(type),
333                                         hostname, name, name);
334                         return -1;
335                 }
336                 sdb_object_deref(SDB_OBJ(host));
337         }
339         buf = sdb_strbuf_create(1024);
340         if (! buf) {
341                 char errbuf[1024];
342                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
343                                 "buffer to handle FETCH command: %s",
344                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
346                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
347                 sdb_strbuf_destroy(buf);
348                 sdb_object_deref(SDB_OBJ(obj));
349                 return -1;
350         }
351         f = sdb_store_json_formatter(buf, type, /* flags = */ 0);
352         if (! f) {
353                 char errbuf[1024];
354                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
355                                 "JSON formatter to handle FETCH command: %s",
356                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
358                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
359                 sdb_strbuf_destroy(buf);
360                 sdb_object_deref(SDB_OBJ(obj));
361                 return -1;
362         }
364         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
365         if (sdb_store_json_emit_full(f, obj, filter)) {
366                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
367                                 "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type),
368                                 hostname, name);
369                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
370                 sdb_strbuf_destroy(buf);
371                 free(f);
372                 sdb_object_deref(SDB_OBJ(obj));
373                 return -1;
374         }
375         sdb_store_json_finish(f);
377         sdb_connection_send(conn, SDB_CONNECTION_DATA,
378                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
379         sdb_strbuf_destroy(buf);
380         free(f);
381         sdb_object_deref(SDB_OBJ(obj));
382         return 0;
383 } /* sdb_fe_exec_fetch */
385 int
386 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
388         uint32_t res_type = htonl(SDB_CONNECTION_LIST);
390         sdb_store_json_formatter_t *f;
391         sdb_strbuf_t *buf;
393         buf = sdb_strbuf_create(1024);
394         if (! buf) {
395                 char errbuf[1024];
396                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
397                                 "buffer to handle LIST command: %s",
398                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
400                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
401                 sdb_strbuf_destroy(buf);
402                 return -1;
403         }
404         f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
405         if (! f) {
406                 char errbuf[1024];
407                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
408                                 "JSON formatter to handle LIST command: %s",
409                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
411                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
412                 sdb_strbuf_destroy(buf);
413                 return -1;
414         }
416         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
417         if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) {
418                 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
419                                 "store to JSON");
420                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
421                 sdb_strbuf_destroy(buf);
422                 free(f);
423                 return -1;
424         }
425         sdb_store_json_finish(f);
427         sdb_connection_send(conn, SDB_CONNECTION_DATA,
428                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
429         sdb_strbuf_destroy(buf);
430         free(f);
431         return 0;
432 } /* sdb_fe_exec_list */
434 int
435 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
436                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
438         uint32_t res_type = htonl(SDB_CONNECTION_LOOKUP);
440         sdb_store_json_formatter_t *f;
441         sdb_strbuf_t *buf;
443         buf = sdb_strbuf_create(1024);
444         if (! buf) {
445                 char errbuf[1024];
446                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
447                                 "buffer to handle LOOKUP command: %s",
448                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
450                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
451                 return -1;
452         }
453         f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
454         if (! f) {
455                 char errbuf[1024];
456                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
457                                 "JSON formatter to handle LOOKUP command: %s",
458                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
460                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
461                 sdb_strbuf_destroy(buf);
462                 return -1;
463         }
465         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
467         if (sdb_store_scan(type, m, filter, lookup_tojson, f)) {
468                 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup %ss",
469                                 SDB_STORE_TYPE_TO_NAME(type));
470                 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup %ss",
471                                 SDB_STORE_TYPE_TO_NAME(type));
472                 sdb_strbuf_destroy(buf);
473                 free(f);
474                 return -1;
475         }
476         sdb_store_json_finish(f);
478         sdb_connection_send(conn, SDB_CONNECTION_DATA,
479                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
480         sdb_strbuf_destroy(buf);
481         free(f);
482         return 0;
483 } /* sdb_fe_exec_lookup */
485 int
486 sdb_fe_exec_timeseries(sdb_conn_t *conn,
487                 const char *hostname, const char *metric,
488                 sdb_timeseries_opts_t *opts)
490         sdb_strbuf_t *buf;
491         uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES);
493         buf = sdb_strbuf_create(1024);
494         if (! buf) {
495                 char errbuf[1024];
496                 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
497                                 "buffer to handle TIMESERIES command: %s",
498                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
500                 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
501                 return -1;
502         }
504         sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
505         if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
506                 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
507                 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
508                 sdb_strbuf_destroy(buf);
509                 return -1;
510         }
512         sdb_connection_send(conn, SDB_CONNECTION_DATA,
513                         (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
514         sdb_strbuf_destroy(buf);
515         return 0;
516 } /* sdb_fe_exec_timeseries */
518 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */