X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Ffrontend%2Fquery.c;h=7fcdbe2c338fb8c43f46d96ac22194c735d4b275;hp=d6bd76a95ebb3c23ba06c291f324215233801ff2;hb=29aa1f75c4840710671488420208a2e029a308a9;hpb=69ba5856d2f9e396ce1956c24ed0194075800463 diff --git a/src/frontend/query.c b/src/frontend/query.c index d6bd76a..7fcdbe2 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -1,6 +1,6 @@ /* * SysDB - src/frontend/query.c - * Copyright (C) 2013 Sebastian 'tokkee' Harl + * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -29,7 +29,8 @@ #include "core/store.h" #include "frontend/connection-private.h" -#include "frontend/parser.h" +#include "parser/ast.h" +#include "parser/parser.h" #include "utils/error.h" #include "utils/proto.h" #include "utils/strbuf.h" @@ -41,28 +42,21 @@ * private helper functions */ -typedef struct { - sdb_strbuf_t *buf; - sdb_store_matcher_t *filter; - - size_t last_len; -} tojson_data_t; - static int -lookup_tojson(sdb_store_obj_t *obj, void *user_data) +list_tojson(sdb_store_obj_t *obj, + sdb_store_matcher_t __attribute__((unused)) *filter, + void *user_data) { - tojson_data_t *data = user_data; - int status; - - if (data->filter && (! sdb_store_matcher_matches(data->filter, obj, NULL))) - return 0; + sdb_store_json_formatter_t *f = user_data; + return sdb_store_json_emit(f, obj); +} /* list_tojson */ - if (sdb_strbuf_len(data->buf) > data->last_len) - sdb_strbuf_append(data->buf, ","); - data->last_len = sdb_strbuf_len(data->buf); - status = sdb_store_host_tojson(obj, data->buf, - data->filter, /* flags = */ 0); - return status; +static int +lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter, + void *user_data) +{ + sdb_store_json_formatter_t *f = user_data; + return sdb_store_json_emit_full(f, obj, filter); } /* lookup_tojson */ /* @@ -73,30 +67,33 @@ int sdb_fe_query(sdb_conn_t *conn) { sdb_llist_t *parsetree; - sdb_conn_node_t *node = NULL; + sdb_ast_node_t *ast = NULL; + + sdb_store_query_t *q; + sdb_strbuf_t *buf = NULL; int status = 0; - if ((! conn) || (conn->cmd != CONNECTION_QUERY)) + if ((! conn) || (conn->cmd != SDB_CONNECTION_QUERY)) return -1; - parsetree = sdb_fe_parse(sdb_strbuf_string(conn->buf), - (int)conn->cmd_len); + parsetree = sdb_parser_parse(sdb_strbuf_string(conn->buf), + (int)conn->cmd_len, conn->errbuf); if (! parsetree) { char query[conn->cmd_len + 1]; strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); query[sizeof(query) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s'", - query); + sdb_log(SDB_LOG_ERR, "frontend: Failed to parse query '%s': %s", + query, sdb_strbuf_string(conn->errbuf)); return -1; } switch (sdb_llist_len(parsetree)) { case 0: /* skipping empty command; send back an empty reply */ - sdb_connection_send(conn, CONNECTION_DATA, 0, NULL); + sdb_connection_send(conn, SDB_CONNECTION_DATA, 0, NULL); break; case 1: - node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0)); + ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0)); break; default: @@ -109,38 +106,59 @@ sdb_fe_query(sdb_conn_t *conn) sdb_llist_len(parsetree) - 1, sdb_llist_len(parsetree) == 2 ? "" : "s", query); - node = SDB_CONN_NODE(sdb_llist_get(parsetree, 0)); + ast = SDB_AST_NODE(sdb_llist_get(parsetree, 0)); } } - if (node) { - if (sdb_fe_analyze(node)) { + q = sdb_store_query_prepare(ast); + if (! q) { + /* this shouldn't happen */ + sdb_strbuf_sprintf(conn->errbuf, "failed to compile AST"); + sdb_log(SDB_LOG_ERR, "frontend: failed to compile AST"); + status = -1; + } else { + buf = sdb_strbuf_create(1024); + if (! buf) { + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + sdb_object_deref(SDB_OBJ(q)); + return -1; + } + status = sdb_store_query_execute(q, buf, conn->errbuf); + if (status < 0) { char query[conn->cmd_len + 1]; strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len); query[sizeof(query) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: Failed to verify query '%s'", - query); - status = -1; + sdb_log(SDB_LOG_ERR, "frontend: failed to execute query '%s'", query); } - else - status = sdb_fe_exec(conn, node); - sdb_object_deref(SDB_OBJ(node)); } - + sdb_object_deref(SDB_OBJ(ast)); sdb_llist_destroy(parsetree); - return status; + + if (status < 0) { + sdb_object_deref(SDB_OBJ(q)); + sdb_strbuf_destroy(buf); + return status; + } + + assert(buf); + sdb_connection_send(conn, status, + (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); + + sdb_strbuf_destroy(buf); + sdb_object_deref(SDB_OBJ(q)); + return 0; } /* sdb_fe_query */ int sdb_fe_fetch(sdb_conn_t *conn) { char name[conn->cmd_len + 1]; - int type; + uint32_t type; - if ((! conn) || (conn->cmd != CONNECTION_FETCH)) + if ((! conn) || (conn->cmd != SDB_CONNECTION_FETCH)) return -1; - if (conn->cmd_len < sizeof(type)) { + if (conn->cmd_len < sizeof(uint32_t)) { sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for " "FETCH command", conn->cmd_len); sdb_strbuf_sprintf(conn->errbuf, "FETCH: Invalid command length %d", @@ -148,23 +166,24 @@ sdb_fe_fetch(sdb_conn_t *conn) return -1; } - type = sdb_proto_get_int(conn->buf, 0); - strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(type), - conn->cmd_len - sizeof(type)); + sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type); + strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t), + conn->cmd_len - sizeof(uint32_t)); name[sizeof(name) - 1] = '\0'; - return sdb_fe_exec_fetch(conn, type, name, /* filter = */ NULL); + /* TODO: support other types besides hosts */ + return sdb_fe_exec_fetch(conn, (int)type, name, NULL, /* filter = */ NULL); } /* sdb_fe_fetch */ int sdb_fe_list(sdb_conn_t *conn) { - int type = SDB_HOST; + uint32_t type = SDB_HOST; - if ((! conn) || (conn->cmd != CONNECTION_LIST)) + if ((! conn) || (conn->cmd != SDB_CONNECTION_LIST)) return -1; if (conn->cmd_len == sizeof(uint32_t)) - type = sdb_proto_get_int(conn->buf, 0); + sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type); else if (conn->cmd_len) { sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for " "LIST command", conn->cmd_len); @@ -172,7 +191,7 @@ sdb_fe_list(sdb_conn_t *conn) conn->cmd_len); return -1; } - return sdb_fe_exec_list(conn, type, /* filter = */ NULL); + return sdb_fe_exec_list(conn, (int)type, /* filter = */ NULL); } /* sdb_fe_list */ int @@ -186,116 +205,114 @@ sdb_fe_lookup(sdb_conn_t *conn) int status; conn_matcher_t m_node = { - { SDB_OBJECT_INIT, CONNECTION_MATCHER }, NULL + { SDB_OBJECT_INIT, SDB_CONNECTION_MATCHER }, NULL }; conn_lookup_t node = { - { SDB_OBJECT_INIT, CONNECTION_LOOKUP }, + { SDB_OBJECT_INIT, SDB_CONNECTION_LOOKUP }, -1, &m_node, NULL }; - if ((! conn) || (conn->cmd != CONNECTION_LOOKUP)) + if ((! conn) || (conn->cmd != SDB_CONNECTION_LOOKUP)) return -1; - if (conn->cmd_len < sizeof(type)) { + if (conn->cmd_len < sizeof(uint32_t)) { sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for " "LOOKUP command", conn->cmd_len); sdb_strbuf_sprintf(conn->errbuf, "LOOKUP: Invalid command length %d", conn->cmd_len); return -1; } - type = sdb_proto_get_int(conn->buf, 0); + sdb_proto_unmarshal_int32(SDB_STRBUF_STR(conn->buf), &type); - matcher = sdb_strbuf_string(conn->buf) + sizeof(type); - matcher_len = conn->cmd_len - sizeof(type); - m = sdb_fe_parse_matcher(matcher, (int)matcher_len); + matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t); + matcher_len = conn->cmd_len - sizeof(uint32_t); + m = sdb_fe_parse_matcher(matcher, (int)matcher_len, conn->errbuf); if (! m) { char expr[matcher_len + 1]; strncpy(expr, matcher, sizeof(expr)); expr[sizeof(expr) - 1] = '\0'; sdb_log(SDB_LOG_ERR, "frontend: Failed to parse " - "lookup condition '%s'", expr); + "lookup condition '%s': %s", expr, + sdb_strbuf_string(conn->errbuf)); return -1; } - node.type = type; + node.type = (int)type; m_node.matcher = m; - if (sdb_fe_analyze(SDB_CONN_NODE(&node))) { + /* run analyzer separately; parse_matcher is missing + * the right context to do so */ + if (sdb_fe_analyze(SDB_CONN_NODE(&node), conn->errbuf)) { char expr[matcher_len + 1]; + char err[sdb_strbuf_len(conn->errbuf) + sizeof(expr) + 64]; strncpy(expr, matcher, sizeof(expr)); expr[sizeof(expr) - 1] = '\0'; - sdb_log(SDB_LOG_ERR, "frontend: Failed to verify " - "lookup condition '%s'", expr); + snprintf(err, sizeof(err), "Failed to parse " + "lookup condition '%s': %s", expr, + sdb_strbuf_string(conn->errbuf)); + sdb_strbuf_sprintf(conn->errbuf, "%s", err); status = -1; } else - status = sdb_fe_exec_lookup(conn, type, m, /* filter = */ NULL); + status = sdb_fe_exec_lookup(conn, (int)type, m, /* filter = */ NULL); sdb_object_deref(SDB_OBJ(m)); return status; } /* sdb_fe_lookup */ +/* + * TODO: let the functions above build an AST; then drop sdb_fe_exec_*. + */ + int -sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node) +sdb_fe_exec_fetch(sdb_conn_t *conn, int type, + const char *hostname, const char *name, sdb_store_matcher_t *filter) { - sdb_store_matcher_t *m = NULL, *filter = NULL; - - if (! node) - return -1; - - switch (node->cmd) { - case CONNECTION_FETCH: - if (CONN_FETCH(node)->filter) - filter = CONN_FETCH(node)->filter->matcher; - return sdb_fe_exec_fetch(conn, CONN_FETCH(node)->type, - CONN_FETCH(node)->name, filter); - case CONNECTION_LIST: - if (CONN_LIST(node)->filter) - filter = CONN_LIST(node)->filter->matcher; - return sdb_fe_exec_list(conn, CONN_LIST(node)->type, filter); - case CONNECTION_LOOKUP: - if (CONN_LOOKUP(node)->matcher) - m = CONN_LOOKUP(node)->matcher->matcher; - if (CONN_LOOKUP(node)->filter) - filter = CONN_LOOKUP(node)->filter->matcher; - return sdb_fe_exec_lookup(conn, - CONN_LOOKUP(node)->type, m, filter); - case CONNECTION_TIMESERIES: - return sdb_fe_exec_timeseries(conn, - CONN_TS(node)->hostname, CONN_TS(node)->metric, - &CONN_TS(node)->opts); + uint32_t res_type = htonl(SDB_CONNECTION_FETCH); - default: - sdb_log(SDB_LOG_ERR, "frontend: Unknown command %i", node->cmd); - return -1; - } - return -1; -} /* sdb_fe_exec */ + sdb_store_obj_t *host; + sdb_store_obj_t *obj; -int -sdb_fe_exec_fetch(sdb_conn_t *conn, int type, const char *name, - sdb_store_matcher_t *filter) -{ + sdb_store_json_formatter_t *f; sdb_strbuf_t *buf; - sdb_store_obj_t *host; - uint32_t res_type = htonl(CONNECTION_FETCH); - - /* XXX: support other types */ - if (type != SDB_HOST) { - sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d " - "in FETCH command", type); - sdb_strbuf_sprintf(conn->errbuf, - "FETCH: Invalid object type %d", type); + + if ((! hostname) || ((type == SDB_HOST) && name) + || ((type != SDB_HOST) && (! name))) { + /* This is a programming error, not something the client did wrong */ + sdb_strbuf_sprintf(conn->errbuf, "INTERNAL ERROR: invalid " + "arguments to sdb_fe_exec_fetch(%s, %s, %s)", + SDB_STORE_TYPE_TO_NAME(type), hostname, name); return -1; } - - host = sdb_store_get_host(name); - if (! host) { - sdb_log(SDB_LOG_DEBUG, "frontend: Failed to fetch host '%s': " - "not found", name); - - sdb_strbuf_sprintf(conn->errbuf, "Host %s not found", name); + if (type == SDB_HOST) + name = hostname; + + host = sdb_store_get_host(hostname); + if ((! host) || (filter + && (! sdb_store_matcher_matches(filter, host, NULL)))) { + sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s: " + "host %s not found", SDB_STORE_TYPE_TO_NAME(type), + name, hostname); + sdb_object_deref(SDB_OBJ(host)); return -1; } + if (type == SDB_HOST) { + obj = host; + } + else { + obj = sdb_store_get_child(host, type, name); + if ((! obj) || (filter + && (! sdb_store_matcher_matches(filter, obj, NULL)))) { + sdb_strbuf_sprintf(conn->errbuf, "Failed to fetch %s %s.%s: " + "%s not found", SDB_STORE_TYPE_TO_NAME(type), + hostname, name, name); + if (obj) + sdb_object_deref(SDB_OBJ(obj)); + sdb_object_deref(SDB_OBJ(host)); + return -1; + } + sdb_object_deref(SDB_OBJ(host)); + } + host = NULL; buf = sdb_strbuf_create(1024); if (! buf) { @@ -306,50 +323,50 @@ sdb_fe_exec_fetch(sdb_conn_t *conn, int type, const char *name, sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); sdb_strbuf_destroy(buf); - sdb_object_deref(SDB_OBJ(host)); + sdb_object_deref(SDB_OBJ(obj)); + return -1; + } + f = sdb_store_json_formatter(buf, type, /* flags = */ 0); + if (! f) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create " + "JSON formatter to handle FETCH command: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + sdb_strbuf_destroy(buf); + sdb_object_deref(SDB_OBJ(obj)); return -1; } sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - if (sdb_store_host_tojson(host, buf, filter, /* flags = */ 0)) { + if (sdb_store_json_emit_full(f, obj, filter)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " - "host '%s' to JSON", name); + "%s %s.%s to JSON", SDB_STORE_TYPE_TO_NAME(type), + hostname, name); sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); sdb_strbuf_destroy(buf); - sdb_object_deref(SDB_OBJ(host)); + free(f); + sdb_object_deref(SDB_OBJ(obj)); return -1; } + sdb_store_json_finish(f); - sdb_connection_send(conn, CONNECTION_DATA, + sdb_connection_send(conn, SDB_CONNECTION_DATA, (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); sdb_strbuf_destroy(buf); - sdb_object_deref(SDB_OBJ(host)); + free(f); + sdb_object_deref(SDB_OBJ(obj)); return 0; } /* sdb_fe_exec_fetch */ int sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter) { - sdb_strbuf_t *buf; - uint32_t res_type = htonl(CONNECTION_LIST); - - int flags; + uint32_t res_type = htonl(SDB_CONNECTION_LIST); - if (type == SDB_HOST) - flags = SDB_SKIP_ALL; - else if (type == SDB_SERVICE) - flags = (SDB_SKIP_ALL & (~SDB_SKIP_SERVICES)) - | SDB_SKIP_EMPTY_SERVICES; - else if (type == SDB_METRIC) - flags = (SDB_SKIP_ALL & (~SDB_SKIP_METRICS)) - | SDB_SKIP_EMPTY_METRICS; - else { - sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d " - "for LIST command", type); - sdb_strbuf_sprintf(conn->errbuf, - "LIST: Invalid object type %d", type); - return -1; - } + sdb_store_json_formatter_t *f; + sdb_strbuf_t *buf; buf = sdb_strbuf_create(1024); if (! buf) { @@ -362,19 +379,33 @@ sdb_fe_exec_list(sdb_conn_t *conn, int type, sdb_store_matcher_t *filter) sdb_strbuf_destroy(buf); return -1; } + f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY); + if (! f) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create " + "JSON formatter to handle LIST command: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); + + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + sdb_strbuf_destroy(buf); + return -1; + } sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - if (sdb_store_tojson(buf, filter, flags)) { + if (sdb_store_scan(type, /* m = */ NULL, filter, list_tojson, f)) { sdb_log(SDB_LOG_ERR, "frontend: Failed to serialize " "store to JSON"); sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); sdb_strbuf_destroy(buf); + free(f); return -1; } + sdb_store_json_finish(f); - sdb_connection_send(conn, CONNECTION_DATA, + sdb_connection_send(conn, SDB_CONNECTION_DATA, (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); sdb_strbuf_destroy(buf); + free(f); return 0; } /* sdb_fe_exec_list */ @@ -382,49 +413,50 @@ int sdb_fe_exec_lookup(sdb_conn_t *conn, int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter) { - tojson_data_t data = { NULL, filter, 0 }; - uint32_t res_type = htonl(CONNECTION_LOOKUP); - - /* XXX: support other types */ - if (type != SDB_HOST) { - sdb_log(SDB_LOG_ERR, "frontend: Invalid object type %d " - "in LOOKUP command", type); - sdb_strbuf_sprintf(conn->errbuf, - "LOOKUP: Invalid object type %d", type); - return -1; - } + uint32_t res_type = htonl(SDB_CONNECTION_LOOKUP); - data.buf = sdb_strbuf_create(1024); - if (! data.buf) { + sdb_store_json_formatter_t *f; + sdb_strbuf_t *buf; + + buf = sdb_strbuf_create(1024); + if (! buf) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "frontend: Failed to create " "buffer to handle LOOKUP command: %s", sdb_strerror(errno, errbuf, sizeof(errbuf))); sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); - sdb_strbuf_destroy(data.buf); return -1; } + f = sdb_store_json_formatter(buf, type, SDB_WANT_ARRAY); + if (! f) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "frontend: Failed to create " + "JSON formatter to handle LOOKUP command: %s", + sdb_strerror(errno, errbuf, sizeof(errbuf))); - sdb_strbuf_memcpy(data.buf, &res_type, sizeof(uint32_t)); - sdb_strbuf_append(data.buf, "["); - - /* Let the JSON serializer handle the filter instead of the scanner. Else, - * we'd have to filter twice -- once in the scanner and then again in the - * serializer. */ - data.last_len = sdb_strbuf_len(data.buf); - if (sdb_store_scan(m, /* filter */ NULL, lookup_tojson, &data)) { - sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup hosts"); - sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup hosts"); - sdb_strbuf_destroy(data.buf); + sdb_strbuf_sprintf(conn->errbuf, "Out of memory"); + sdb_strbuf_destroy(buf); return -1; } - sdb_strbuf_append(data.buf, "]"); + sdb_strbuf_memcpy(buf, &res_type, sizeof(uint32_t)); - sdb_connection_send(conn, CONNECTION_DATA, - (uint32_t)sdb_strbuf_len(data.buf), sdb_strbuf_string(data.buf)); - sdb_strbuf_destroy(data.buf); + if (sdb_store_scan(type, m, filter, lookup_tojson, f)) { + sdb_log(SDB_LOG_ERR, "frontend: Failed to lookup %ss", + SDB_STORE_TYPE_TO_NAME(type)); + sdb_strbuf_sprintf(conn->errbuf, "Failed to lookup %ss", + SDB_STORE_TYPE_TO_NAME(type)); + sdb_strbuf_destroy(buf); + free(f); + return -1; + } + sdb_store_json_finish(f); + + sdb_connection_send(conn, SDB_CONNECTION_DATA, + (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); + sdb_strbuf_destroy(buf); + free(f); return 0; } /* sdb_fe_exec_lookup */ @@ -434,7 +466,7 @@ sdb_fe_exec_timeseries(sdb_conn_t *conn, sdb_timeseries_opts_t *opts) { sdb_strbuf_t *buf; - uint32_t res_type = htonl(CONNECTION_TIMESERIES); + uint32_t res_type = htonl(SDB_CONNECTION_TIMESERIES); buf = sdb_strbuf_create(1024); if (! buf) { @@ -455,7 +487,7 @@ sdb_fe_exec_timeseries(sdb_conn_t *conn, return -1; } - sdb_connection_send(conn, CONNECTION_DATA, + sdb_connection_send(conn, SDB_CONNECTION_DATA, (uint32_t)sdb_strbuf_len(buf), sdb_strbuf_string(buf)); sdb_strbuf_destroy(buf); return 0;