Code

Let the front-end handle STORE commands.
authorSebastian Harl <sh@tokkee.org>
Mon, 24 Aug 2015 19:16:06 +0000 (21:16 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 24 Aug 2015 19:16:06 +0000 (21:16 +0200)
… rather than requiring each reader plugin to do so which doesn't make any
sense at all.

src/core/plugin.c
src/core/store_exec.c
src/frontend/query.c

index 6b1560336b311a659c35566333d78a147d8d39c2..235b8c7721ba97d08dc1b165c6ac0812ed7c6292 100644 (file)
@@ -1433,6 +1433,17 @@ sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
        if (! ast)
                return 0;
 
        if (! ast)
                return 0;
 
+       if ((ast->type != SDB_AST_TYPE_FETCH)
+                       && (ast->type != SDB_AST_TYPE_LIST)
+                       && (ast->type != SDB_AST_TYPE_LOOKUP)
+                       && (ast->type != SDB_AST_TYPE_TIMESERIES)) {
+               sdb_log(SDB_LOG_ERR, "core: Cannot execute query of type %s",
+                               SDB_AST_TYPE_TO_STRING(ast));
+               sdb_strbuf_sprintf(errbuf, "Cannot execute query of type %s",
+                               SDB_AST_TYPE_TO_STRING(ast));
+               return -1;
+       }
+
        if (n != 1) {
                char *msg = (n > 0)
                        ? "Cannot execute query: multiple readers not supported"
        if (n != 1) {
                char *msg = (n > 0)
                        ? "Cannot execute query: multiple readers not supported"
index 5daaa143105558c133f6649d8d44e7c24e0f9307..ee1f8d046f47ad49768c4aa2a2b89a0bff9bbd80 100644 (file)
@@ -35,7 +35,6 @@
 #include <errno.h>
 
 #include <arpa/inet.h>
 #include <errno.h>
 
 #include <arpa/inet.h>
-#include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
 
@@ -60,12 +59,6 @@ lookup_tojson(sdb_store_obj_t *obj, sdb_store_matcher_t *filter,
        return sdb_store_json_emit_full(f, obj, filter);
 } /* lookup_tojson */
 
        return sdb_store_json_emit_full(f, obj, filter);
 } /* lookup_tojson */
 
-static size_t
-sstrlen(const char *s)
-{
-       return s ? strlen(s) : 0;
-} /* sstrlen */
-
 /*
  * query implementations
  */
 /*
  * query implementations
  */
@@ -219,91 +212,6 @@ exec_lookup(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
        return SDB_CONNECTION_DATA;
 } /* exec_lookup */
 
        return SDB_CONNECTION_DATA;
 } /* exec_lookup */
 
-static int
-exec_store(sdb_strbuf_t *buf, sdb_strbuf_t *errbuf, sdb_ast_store_t *st)
-{
-       char name[sstrlen(st->hostname) + sstrlen(st->parent) + sstrlen(st->name) + 3];
-       sdb_metric_store_t metric_store;
-       int type = st->obj_type, status = -1;
-
-       switch (st->obj_type) {
-       case SDB_HOST:
-               strncpy(name, st->name, sizeof(name));
-               status = sdb_plugin_store_host(st->name, st->last_update);
-               break;
-
-       case SDB_SERVICE:
-               snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
-               status = sdb_plugin_store_service(st->hostname, st->name, st->last_update);
-               break;
-
-       case SDB_METRIC:
-               snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
-               metric_store.type = st->store_type;
-               metric_store.id = st->store_id;
-               status = sdb_plugin_store_metric(st->hostname, st->name,
-                               &metric_store, st->last_update);
-               break;
-
-       case SDB_ATTRIBUTE:
-               type |= st->parent_type;
-
-               if (st->parent)
-                       snprintf(name, sizeof(name), "%s.%s.%s",
-                                       st->hostname, st->parent, st->name);
-               else
-                       snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
-
-               switch (st->parent_type) {
-               case 0:
-                       type |= SDB_HOST;
-                       status = sdb_plugin_store_attribute(st->hostname,
-                                       st->name, &st->value, st->last_update);
-                       break;
-
-               case SDB_SERVICE:
-                       status = sdb_plugin_store_service_attribute(st->hostname, st->parent,
-                                       st->name, &st->value, st->last_update);
-                       break;
-
-               case SDB_METRIC:
-                       status = sdb_plugin_store_metric_attribute(st->hostname, st->parent,
-                                       st->name, &st->value, st->last_update);
-                       break;
-
-               default:
-                       sdb_log(SDB_LOG_ERR, "store: Invalid parent type in STORE: %s",
-                                       SDB_STORE_TYPE_TO_NAME(st->parent_type));
-                       return -1;
-               }
-               break;
-
-       default:
-               sdb_log(SDB_LOG_ERR, "store: Invalid object type in STORE: %s",
-                               SDB_STORE_TYPE_TO_NAME(st->obj_type));
-               return -1;
-       }
-
-       if (status < 0) {
-               sdb_strbuf_sprintf(errbuf, "STORE: Failed to store %s object",
-                               SDB_STORE_TYPE_TO_NAME(type));
-               return -1;
-       }
-
-       if (! status) {
-               sdb_strbuf_sprintf(buf, "Successfully stored %s %s",
-                               SDB_STORE_TYPE_TO_NAME(type), name);
-       }
-       else {
-               char type_str[32];
-               strncpy(type_str, SDB_STORE_TYPE_TO_NAME(type), sizeof(type_str));
-               type_str[0] = (char)toupper((int)type_str[0]);
-               sdb_strbuf_sprintf(buf, "%s %s already up to date", type_str, name);
-       }
-
-       return SDB_CONNECTION_OK;
-} /* exec_store */
-
 static int
 exec_timeseries(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
                const char *hostname, const char *metric,
 static int
 exec_timeseries(sdb_store_t *store, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
                const char *hostname, const char *metric,
@@ -354,14 +262,6 @@ sdb_store_query_execute(sdb_store_t *store, sdb_store_query_t *q,
                return exec_lookup(store, buf, errbuf, SDB_AST_LOOKUP(ast)->obj_type,
                                q->matcher, q->filter);
 
                return exec_lookup(store, buf, errbuf, SDB_AST_LOOKUP(ast)->obj_type,
                                q->matcher, q->filter);
 
-       case SDB_AST_TYPE_STORE:
-               if (ast->type != SDB_AST_TYPE_STORE) {
-                       sdb_log(SDB_LOG_ERR, "store: Invalid AST node for STORE command: %s",
-                                       SDB_AST_TYPE_TO_STRING(ast));
-                       return -1;
-               }
-               return exec_store(buf, errbuf, SDB_AST_STORE(ast));
-
        case SDB_AST_TYPE_TIMESERIES:
                ts_opts.start = SDB_AST_TIMESERIES(ast)->start;
                ts_opts.end = SDB_AST_TIMESERIES(ast)->end;
        case SDB_AST_TYPE_TIMESERIES:
                ts_opts.start = SDB_AST_TIMESERIES(ast)->start;
                ts_opts.end = SDB_AST_TIMESERIES(ast)->end;
index 724144aafb91a63369efb98cd0c97a55b5a7f8a9..79886105bd3d68ed0dd099114af5d7d8913983cf 100644 (file)
@@ -40,6 +40,7 @@
 #include "utils/strbuf.h"
 
 #include <errno.h>
 #include "utils/strbuf.h"
 
 #include <errno.h>
+#include <ctype.h>
 #include <string.h>
 
 /*
 #include <string.h>
 
 /*
@@ -52,8 +53,99 @@ sstrdup(const char *s)
        return s ? strdup(s) : NULL;
 } /* sstrdup */
 
        return s ? strdup(s) : NULL;
 } /* sstrdup */
 
+static size_t
+sstrlen(const char *s)
+{
+       return s ? strlen(s) : 0;
+} /* sstrlen */
+
+static int
+exec_store(sdb_ast_store_t *st, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
+{
+       char name[sstrlen(st->hostname) + sstrlen(st->parent) + sstrlen(st->name) + 3];
+       sdb_metric_store_t metric_store;
+       int type = st->obj_type, status = -1;
+
+       switch (st->obj_type) {
+       case SDB_HOST:
+               strncpy(name, st->name, sizeof(name));
+               status = sdb_plugin_store_host(st->name, st->last_update);
+               break;
+
+       case SDB_SERVICE:
+               snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
+               status = sdb_plugin_store_service(st->hostname, st->name, st->last_update);
+               break;
+
+       case SDB_METRIC:
+               snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
+               metric_store.type = st->store_type;
+               metric_store.id = st->store_id;
+               status = sdb_plugin_store_metric(st->hostname, st->name,
+                               &metric_store, st->last_update);
+               break;
+
+       case SDB_ATTRIBUTE:
+               type |= st->parent_type;
+
+               if (st->parent)
+                       snprintf(name, sizeof(name), "%s.%s.%s",
+                                       st->hostname, st->parent, st->name);
+               else
+                       snprintf(name, sizeof(name), "%s.%s", st->hostname, st->name);
+
+               switch (st->parent_type) {
+               case 0:
+                       type |= SDB_HOST;
+                       status = sdb_plugin_store_attribute(st->hostname,
+                                       st->name, &st->value, st->last_update);
+                       break;
+
+               case SDB_SERVICE:
+                       status = sdb_plugin_store_service_attribute(st->hostname, st->parent,
+                                       st->name, &st->value, st->last_update);
+                       break;
+
+               case SDB_METRIC:
+                       status = sdb_plugin_store_metric_attribute(st->hostname, st->parent,
+                                       st->name, &st->value, st->last_update);
+                       break;
+
+               default:
+                       sdb_log(SDB_LOG_ERR, "store: Invalid parent type in STORE: %s",
+                                       SDB_STORE_TYPE_TO_NAME(st->parent_type));
+                       return -1;
+               }
+               break;
+
+       default:
+               sdb_log(SDB_LOG_ERR, "store: Invalid object type in STORE: %s",
+                               SDB_STORE_TYPE_TO_NAME(st->obj_type));
+               return -1;
+       }
+
+       if (status < 0) {
+               sdb_strbuf_sprintf(errbuf, "STORE: Failed to store %s object",
+                               SDB_STORE_TYPE_TO_NAME(type));
+               return -1;
+       }
+
+       if (! status) {
+               sdb_strbuf_sprintf(buf, "Successfully stored %s %s",
+                               SDB_STORE_TYPE_TO_NAME(type), name);
+       }
+       else {
+               char type_str[32];
+               strncpy(type_str, SDB_STORE_TYPE_TO_NAME(type), sizeof(type_str));
+               type_str[0] = (char)toupper((int)type_str[0]);
+               sdb_strbuf_sprintf(buf, "%s %s already up to date", type_str, name);
+       }
+
+       return SDB_CONNECTION_OK;
+} /* exec_store */
+
 static int
 static int
-query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast)
+exec_query(sdb_conn_t *conn, sdb_ast_node_t *ast)
 {
        sdb_strbuf_t *buf;
        int status;
 {
        sdb_strbuf_t *buf;
        int status;
@@ -68,7 +160,10 @@ query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast)
                sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
                return -1;
        }
                sdb_strbuf_sprintf(conn->errbuf, "Out of memory");
                return -1;
        }
-       status = sdb_plugin_query(ast, buf, conn->errbuf);
+       if (ast->type == SDB_AST_TYPE_STORE)
+               status = exec_store(SDB_AST_STORE(ast), buf, conn->errbuf);
+       else
+               status = sdb_plugin_query(ast, buf, conn->errbuf);
        if (status < 0) {
                char query[conn->cmd_len + 1];
                strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
        if (status < 0) {
                char query[conn->cmd_len + 1];
                strncpy(query, sdb_strbuf_string(conn->buf), conn->cmd_len);
@@ -81,7 +176,7 @@ query_exec(sdb_conn_t *conn, sdb_ast_node_t *ast)
 
        sdb_strbuf_destroy(buf);
        return status < 0 ? status : 0;
 
        sdb_strbuf_destroy(buf);
        return status < 0 ? status : 0;
-} /* query_exec */
+} /* exec_query */
 
 /*
  * public API
 
 /*
  * public API
@@ -132,7 +227,7 @@ sdb_conn_query(sdb_conn_t *conn)
        }
 
        if (ast) {
        }
 
        if (ast) {
-               status = query_exec(conn, ast);
+               status = exec_query(conn, ast);
                sdb_object_deref(SDB_OBJ(ast));
        }
        sdb_llist_destroy(parsetree);
                sdb_object_deref(SDB_OBJ(ast));
        }
        sdb_llist_destroy(parsetree);
@@ -171,7 +266,7 @@ sdb_conn_fetch(sdb_conn_t *conn)
                        hostname[0] ? strdup(hostname) : NULL,
                        name[0] ? strdup(name) : NULL,
                        /* filter = */ NULL);
                        hostname[0] ? strdup(hostname) : NULL,
                        name[0] ? strdup(name) : NULL,
                        /* filter = */ NULL);
-       status = query_exec(conn, ast);
+       status = exec_query(conn, ast);
        sdb_object_deref(SDB_OBJ(ast));
        return status;
 } /* sdb_conn_fetch */
        sdb_object_deref(SDB_OBJ(ast));
        return status;
 } /* sdb_conn_fetch */
@@ -197,7 +292,7 @@ sdb_conn_list(sdb_conn_t *conn)
        }
 
        ast = sdb_ast_list_create((int)type, /* filter = */ NULL);
        }
 
        ast = sdb_ast_list_create((int)type, /* filter = */ NULL);
-       status = query_exec(conn, ast);
+       status = exec_query(conn, ast);
        sdb_object_deref(SDB_OBJ(ast));
        return status;
 } /* sdb_conn_list */
        sdb_object_deref(SDB_OBJ(ast));
        return status;
 } /* sdb_conn_list */
@@ -249,7 +344,7 @@ sdb_conn_lookup(sdb_conn_t *conn)
                status = -1;
        }
        else
                status = -1;
        }
        else
-               status = query_exec(conn, ast);
+               status = exec_query(conn, ast);
        if (! ast)
                sdb_object_deref(SDB_OBJ(m));
        sdb_object_deref(SDB_OBJ(ast));
        if (! ast)
                sdb_object_deref(SDB_OBJ(m));
        sdb_object_deref(SDB_OBJ(ast));
@@ -356,7 +451,7 @@ sdb_conn_store(sdb_conn_t *conn)
 
        status = sdb_parser_analyze(ast, conn->errbuf);
        if (! status)
 
        status = sdb_parser_analyze(ast, conn->errbuf);
        if (! status)
-               status = query_exec(conn, ast);
+               status = exec_query(conn, ast);
        sdb_object_deref(SDB_OBJ(ast));
        return status;
 } /* sdb_conn_store */
        sdb_object_deref(SDB_OBJ(ast));
        return status;
 } /* sdb_conn_store */