338d95a75c6351343a6a2b248b3f5bc2a61cdafa
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))) {
212 char expr[matcher_len + 1];
213 strncpy(expr, matcher, sizeof(expr));
214 expr[sizeof(expr) - 1] = '\0';
215 sdb_strbuf_sprintf(conn->errbuf, "Failed to verify "
216 "lookup condition '%s'", expr);
217 status = -1;
218 }
219 else
220 status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL);
221 sdb_object_deref(SDB_OBJ(m));
222 return status;
223 } /* sdb_fe_lookup */
225 int
226 sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
227 {
228 sdb_store_matcher_t *m = NULL, *filter = NULL;
230 if (! node)
231 return -1;
233 switch (node->cmd) {
234 case CONNECTION_FETCH:
235 if (CONN_FETCH(node)->filter)
236 filter = CONN_FETCH(node)->filter->matcher;
237 return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type,
238 CONN_FETCH(node)->host, CONN_FETCH(node)->name, filter);
239 case CONNECTION_LIST:
240 if (CONN_LIST(node)->filter)
241 filter = CONN_LIST(node)->filter->matcher;
242 return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter);
243 case CONNECTION_LOOKUP:
244 if (CONN_LOOKUP(node)->matcher)
245 m = CONN_LOOKUP(node)->matcher->matcher;
246 if (CONN_LOOKUP(node)->filter)
247 filter = CONN_LOOKUP(node)->filter->matcher;
248 return sdb_fe_exec_lookup(conn,
249 CONN_LOOKUP(node)->type, m, filter);
250 case CONNECTION_TIMESERIES:
251 return sdb_fe_exec_timeseries(conn,
252 CONN_TS(node)->hostname, CONN_TS(node)->metric,
253 &CONN_TS(node)->opts);
255 default:
256 sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd);
257 return -1;
258 }
259 return -1;
260 } /* sdb_fe_exec */
262 int
263 sdb_fe_exec_fetch(sdb_conn_t *conn, int type,
264 const char *hostname, const char *name, sdb_store_matcher_t *filter)
265 {
266 uint32_t res_type = htonl(CONNECTION_FETCH);
268 sdb_store_obj_t *host;
269 sdb_store_obj_t *obj;
271 sdb_store_json_formatter_t *f;
272 sdb_strbuf_t *buf;
274 if ((! hostname) || ((type == SDB_HOST) && name)
275 || ((type != SDB_HOST) && (! name))) {
276 /* This is a programming error, not something the client did wrong */
277 sdb_strbuf_sprintf(conn->errbuf, "INTERNAL ERROR: invalid "
278 "arguments to sdb_fe_exec_fetch(%s, %s, %s)",
279 SDB_STORE_TYPE_TO_NAME(type), hostname, name);
280 return -1;
281 }
283 host = sdb_store_get_host(hostname);
284 if ((! host) || (filter
285 && (! sdb_store_matcher_matches(filter, host, NULL)))) {
286 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: "
287 "host %s not found", SDB_STORE_TYPE_TO_NAME(type),
288 name, hostname);
289 return -1;
290 }
291 if (type == SDB_HOST) {
292 obj = host;
293 }
294 else {
295 obj = sdb_store_get_child(host, type, name);
296 if ((! obj) || (filter
297 && (! sdb_store_matcher_matches(filter, obj, NULL)))) {
298 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: "
299 "%s not found", SDB_STORE_TYPE_TO_NAME(type),
300 hostname, name, name);
301 return -1;
302 }
303 sdb_object_deref(SDB_OBJ(host));
304 }
306 buf = sdb_strbuf_create(1024);
307 if (! buf) {
308 char errbuf[1024];
309 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
310 "buffer to handle FETCH command: %s",
311 sdb_strerror(errno, errbuf, sizeof(errbuf)));
313 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
314 sdb_strbuf_destroy(buf);
315 sdb_object_deref(SDB_OBJ(obj));
316 return -1;
317 }
318 f = sdb_store_json_formatter(buf, type, /* flags = */ 0);
319 if (! f) {
320 char errbuf[1024];
321 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
322 "JSON formatter to handle FETCH command: %s",
323 sdb_strerror(errno, errbuf, sizeof(errbuf)));
325 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
326 sdb_strbuf_destroy(buf);
327 sdb_object_deref(SDB_OBJ(obj));
328 return -1;
329 }
331 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
332 if (sdb_store_json_emit_full(f, obj, filter)) {
333 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
334 "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type),
335 hostname, name);
336 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
337 sdb_strbuf_destroy(buf);
338 free(f);
339 sdb_object_deref(SDB_OBJ(obj));
340 return -1;
341 }
342 sdb_store_json_finish(f);
344 sdb_connection_send(conn, CONNECTION_DATA,
345 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
346 sdb_strbuf_destroy(buf);
347 free(f);
348 sdb_object_deref(SDB_OBJ(obj));
349 return 0;
350 } /* sdb_fe_exec_fetch */
352 int
353 sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter)
354 {
355 uint32_t res_type = htonl(CONNECTION_LIST);
357 sdb_store_json_formatter_t *f;
358 sdb_strbuf_t *buf;
360 buf = sdb_strbuf_create(1024);
361 if (! buf) {
362 char errbuf[1024];
363 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
364 "buffer to handle LIST command: %s",
365 sdb_strerror(errno, errbuf, sizeof(errbuf)));
367 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
368 sdb_strbuf_destroy(buf);
369 return -1;
370 }
371 f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
372 if (! f) {
373 char errbuf[1024];
374 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
375 "JSON formatter to handle LIST command: %s",
376 sdb_strerror(errno, errbuf, sizeof(errbuf)));
378 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
379 sdb_strbuf_destroy(buf);
380 return -1;
381 }
383 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
384 if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) {
385 sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize "
386 "store to JSON");
387 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
388 sdb_strbuf_destroy(buf);
389 free(f);
390 return -1;
391 }
392 sdb_store_json_finish(f);
394 sdb_connection_send(conn, CONNECTION_DATA,
395 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
396 sdb_strbuf_destroy(buf);
397 free(f);
398 return 0;
399 } /* sdb_fe_exec_list */
401 int
402 sdb_fe_exec_lookup(sdb_conn_t *conn, int type,
403 sdb_store_matcher_t *m, sdb_store_matcher_t *filter)
404 {
405 uint32_t res_type = htonl(CONNECTION_LOOKUP);
407 sdb_store_json_formatter_t *f;
408 sdb_strbuf_t *buf;
410 /* XXX: support other types */
411 if (type != SDB_HOST) {
412 sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d "
413 "in LOOKUP command", type);
414 sdb_strbuf_sprintf(conn->errbuf,
415 "LOOKUP: Invalid object type %d", type);
416 return -1;
417 }
419 buf = sdb_strbuf_create(1024);
420 if (! buf) {
421 char errbuf[1024];
422 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
423 "buffer to handle LOOKUP command: %s",
424 sdb_strerror(errno, errbuf, sizeof(errbuf)));
426 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
427 return -1;
428 }
429 f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY);
430 if (! f) {
431 char errbuf[1024];
432 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
433 "JSON formatter to handle LOOKUP command: %s",
434 sdb_strerror(errno, errbuf, sizeof(errbuf)));
436 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
437 sdb_strbuf_destroy(buf);
438 return -1;
439 }
441 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
443 if (sdb_store_scan(SDB_HOST, m, filter, lookup_tojson, f)) {
444 sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts");
445 sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts");
446 sdb_strbuf_destroy(buf);
447 free(f);
448 return -1;
449 }
450 sdb_store_json_finish(f);
452 sdb_connection_send(conn, CONNECTION_DATA,
453 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
454 sdb_strbuf_destroy(buf);
455 free(f);
456 return 0;
457 } /* sdb_fe_exec_lookup */
459 int
460 sdb_fe_exec_timeseries(sdb_conn_t *conn,
461 const char *hostname, const char *metric,
462 sdb_timeseries_opts_t *opts)
463 {
464 sdb_strbuf_t *buf;
465 uint32_t res_type = htonl(CONNECTION_TIMESERIES);
467 buf = sdb_strbuf_create(1024);
468 if (! buf) {
469 char errbuf[1024];
470 sdb_log(SDB_LOG_ERR, "frontend: Failed to create "
471 "buffer to handle TIMESERIES command: %s",
472 sdb_strerror(errno, errbuf, sizeof(errbuf)));
474 sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
475 return -1;
476 }
478 sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t));
479 if (sdb_store_fetch_timeseries(hostname, metric, opts, buf)) {
480 sdb_log(SDB_LOG_ERR, "frontend: Failed to fetch time-series");
481 sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch time-series");
482 sdb_strbuf_destroy(buf);
483 return -1;
484 }
486 sdb_connection_send(conn, CONNECTION_DATA,
487 (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf));
488 sdb_strbuf_destroy(buf);
489 return 0;
490 } /* sdb_fe_exec_timeseries */
492 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */