Code

frontend/grammar: Added 'STORE host|service|metric [attribute]' commands.
authorSebastian Harl <sh@tokkee.org>
Sat, 10 Jan 2015 13:02:31 +0000 (14:02 +0100)
committerSebastian Harl <sh@tokkee.org>
Sat, 10 Jan 2015 13:02:31 +0000 (14:02 +0100)
This implements the STORE command in SysQL.

src/frontend/analyzer.c
src/frontend/connection-private.h
src/frontend/grammar.y
src/frontend/query.c
src/frontend/scanner.l
src/include/frontend/proto.h

index 8d228c47e17d2b30d8ffb5ca95813cb8dc740e9e..894cc22f706376ac5df8fff483803a612ea07284 100644 (file)
@@ -322,10 +322,20 @@ sdb_fe_analyze(sdb_conn_node_t *node, sdb_strbuf_t *errbuf)
                        filter = CONN_LOOKUP(node)->filter->matcher;
                context = CONN_LOOKUP(node)->type;
        }
-       else if (node->cmd == SDB_CONNECTION_TIMESERIES)
+       else if ((node->cmd == SDB_CONNECTION_STORE_HOST)
+                       || (node->cmd == SDB_CONNECTION_STORE_SERVICE)
+                       || (node->cmd == SDB_CONNECTION_STORE_METRIC)
+                       || (node->cmd == SDB_CONNECTION_STORE_ATTRIBUTE)) {
                return 0;
-       else
+       }
+       else if (node->cmd == SDB_CONNECTION_TIMESERIES) {
+               return 0;
+       }
+       else {
+               sdb_strbuf_sprintf(errbuf,
+                               "Don't know how to analyze command %#x", node->cmd);
                return -1;
+       }
 
        if (analyze_matcher(context, m, errbuf))
                status = -1;
index 1e5e9e53fee775f452ed85454ef8abea79fc1bb4..361af166867e5ce230415c64475af57812952723 100644 (file)
@@ -117,6 +117,42 @@ typedef struct {
 } conn_lookup_t;
 #define CONN_LOOKUP(obj) ((conn_lookup_t *)(obj))
 
+typedef struct {
+       sdb_conn_node_t super;
+       char *name;
+       sdb_time_t last_update;
+} conn_store_host_t;
+#define CONN_STORE_HOST(obj) ((conn_store_host_t *)(obj))
+
+typedef struct {
+       sdb_conn_node_t super;
+       char *hostname;
+       char *name;
+       sdb_time_t last_update;
+} conn_store_svc_t;
+#define CONN_STORE_SVC(obj) ((conn_store_svc_t *)(obj))
+
+typedef struct {
+       sdb_conn_node_t super;
+       char *hostname;
+       char *name;
+       char *store_type; /* optional */
+       char *store_id;   /* optional */
+       sdb_time_t last_update;
+} conn_store_metric_t;
+#define CONN_STORE_METRIC(obj) ((conn_store_metric_t *)(obj))
+
+typedef struct {
+       sdb_conn_node_t super;
+       int parent_type;
+       char *hostname; /* optional */
+       char *parent;
+       char *key;
+       sdb_data_t value;
+       sdb_time_t last_update;
+} conn_store_attr_t;
+#define CONN_STORE_ATTR(obj) ((conn_store_attr_t *)(obj))
+
 typedef struct {
        sdb_conn_node_t super;
        char *hostname;
@@ -164,6 +200,61 @@ conn_lookup_destroy(sdb_object_t *obj)
        sdb_object_deref(SDB_OBJ(CONN_LOOKUP(obj)->filter));
 } /* conn_lookup_destroy */
 
+static void __attribute__((unused))
+conn_store_host_destroy(sdb_object_t *obj)
+{
+       conn_store_host_t *host = CONN_STORE_HOST(obj);
+       if (host->name)
+               free((void *)host->name);
+       host->name = NULL;
+} /* conn_store_host_destroy */
+
+static void __attribute__((unused))
+conn_store_svc_destroy(sdb_object_t *obj)
+{
+       conn_store_svc_t *svc = CONN_STORE_SVC(obj);
+       if (svc->hostname)
+               free((void *)svc->hostname);
+       svc->hostname = NULL;
+       if (svc->name)
+               free((void *)svc->name);
+       svc->name = NULL;
+} /* conn_store_svc_destroy */
+
+static void __attribute__((unused))
+conn_store_metric_destroy(sdb_object_t *obj)
+{
+       conn_store_metric_t *metric = CONN_STORE_METRIC(obj);
+       if (metric->hostname)
+               free((void *)metric->hostname);
+       metric->hostname = NULL;
+       if (metric->name)
+               free((void *)metric->name);
+       metric->name = NULL;
+       if (metric->store_type)
+               free((void *)metric->store_type);
+       metric->store_type = NULL;
+       if (metric->store_id)
+               free((void *)metric->store_id);
+       metric->store_id = NULL;
+} /* conn_store_metric_destroy */
+
+static void __attribute__((unused))
+conn_store_attr_destroy(sdb_object_t *obj)
+{
+       conn_store_attr_t *attr = CONN_STORE_ATTR(obj);
+       if (attr->hostname)
+               free((void *)attr->hostname);
+       attr->hostname = NULL;
+       if (attr->parent)
+               free((void *)attr->parent);
+       attr->parent = NULL;
+       if (attr->key)
+               free((void *)attr->key);
+       attr->key = NULL;
+       sdb_data_free_datum(&attr->value);
+} /* conn_store_attr_destroy */
+
 static void __attribute__((unused))
 conn_ts_destroy(sdb_object_t *obj)
 {
index 187f3599c08e2514596bd5046d653c8f03c26842..87184a4e15ef3effafe1810c11e368b529b14ea8 100644 (file)
@@ -104,6 +104,8 @@ sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...);
 
        sdb_store_matcher_t *m;
        sdb_store_expr_t *expr;
+
+       sdb_metric_store_t metric_store;
 }
 
 %start statements
@@ -119,12 +121,14 @@ sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...);
 %token ATTRIBUTE_T ATTRIBUTES_T
 %token NAME_T LAST_UPDATE_T AGE_T INTERVAL_T BACKEND_T
 
+%token LAST UPDATE
+
 %token START END
 
 /* NULL token */
 %token NULL_T
 
-%token FETCH LIST LOOKUP TIMESERIES
+%token FETCH LIST LOOKUP STORE TIMESERIES
 
 %token <str> IDENTIFIER STRING
 
@@ -153,6 +157,7 @@ sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...);
        fetch_statement
        list_statement
        lookup_statement
+       store_statement
        timeseries_statement
        matching_clause
        filter_clause
@@ -175,6 +180,9 @@ sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...);
 
 %type <datetime> datetime
        start_clause end_clause
+       last_update_clause
+
+%type <metric_store> metric_store_clause
 
 %destructor { free($$); } <str>
 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
@@ -265,6 +273,8 @@ statement:
        |
        lookup_statement
        |
+       store_statement
+       |
        timeseries_statement
        |
        /* empty */
@@ -345,6 +355,95 @@ filter_clause:
        |
        /* empty */ { $$ = NULL; }
 
+/*
+ * STORE <type> <name>|<host>.<name> [LAST UPDATE <datetime>];
+ * STORE METRIC <host>.<name> STORE <type> <id> [LAST UPDATE <datetime>];
+ * STORE <type> ATTRIBUTE <parent>.<key> <datum> [LAST UPDATE <datetime>];
+ *
+ * Store or update an object in the database.
+ */
+store_statement:
+       STORE HOST_T STRING last_update_clause
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_store_host_t, conn_store_host_destroy));
+                       CONN_STORE_HOST($$)->name = $3;
+                       CONN_STORE_HOST($$)->last_update = $4;
+                       $$->cmd = SDB_CONNECTION_STORE_HOST;
+               }
+       |
+       STORE SERVICE_T STRING '.' STRING last_update_clause
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_store_svc_t, conn_store_svc_destroy));
+                       CONN_STORE_SVC($$)->hostname = $3;
+                       CONN_STORE_SVC($$)->name = $5;
+                       CONN_STORE_SVC($$)->last_update = $6;
+                       $$->cmd = SDB_CONNECTION_STORE_SERVICE;
+               }
+       |
+       STORE METRIC_T STRING '.' STRING metric_store_clause last_update_clause
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_store_metric_t, conn_store_metric_destroy));
+                       CONN_STORE_METRIC($$)->hostname = $3;
+                       CONN_STORE_METRIC($$)->name = $5;
+                       CONN_STORE_METRIC($$)->store_type = $6.type;
+                       CONN_STORE_METRIC($$)->store_id = $6.id;
+                       CONN_STORE_METRIC($$)->last_update = $7;
+                       $$->cmd = SDB_CONNECTION_STORE_METRIC;
+               }
+       |
+       STORE HOST_T ATTRIBUTE_T STRING '.' STRING data last_update_clause
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_store_attr_t, conn_store_attr_destroy));
+                       CONN_STORE_ATTR($$)->parent_type = SDB_HOST;
+                       CONN_STORE_ATTR($$)->hostname = NULL;
+                       CONN_STORE_ATTR($$)->parent = $4;
+                       CONN_STORE_ATTR($$)->key = $6;
+                       CONN_STORE_ATTR($$)->value = $7;
+                       CONN_STORE_ATTR($$)->last_update = $8;
+                       $$->cmd = SDB_CONNECTION_STORE_ATTRIBUTE;
+               }
+       |
+       STORE SERVICE_T ATTRIBUTE_T STRING '.' STRING '.' STRING data last_update_clause
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_store_attr_t, conn_store_attr_destroy));
+                       CONN_STORE_ATTR($$)->parent_type = SDB_SERVICE;
+                       CONN_STORE_ATTR($$)->hostname = $4;
+                       CONN_STORE_ATTR($$)->parent = $6;
+                       CONN_STORE_ATTR($$)->key = $8;
+                       CONN_STORE_ATTR($$)->value = $9;
+                       CONN_STORE_ATTR($$)->last_update = $10;
+                       $$->cmd = SDB_CONNECTION_STORE_ATTRIBUTE;
+               }
+       |
+       STORE METRIC_T ATTRIBUTE_T STRING '.' STRING '.' STRING data last_update_clause
+               {
+                       $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+                                               conn_store_attr_t, conn_store_attr_destroy));
+                       CONN_STORE_ATTR($$)->parent_type = SDB_METRIC;
+                       CONN_STORE_ATTR($$)->hostname = $4;
+                       CONN_STORE_ATTR($$)->parent = $6;
+                       CONN_STORE_ATTR($$)->key = $8;
+                       CONN_STORE_ATTR($$)->value = $9;
+                       CONN_STORE_ATTR($$)->last_update = $10;
+                       $$->cmd = SDB_CONNECTION_STORE_ATTRIBUTE;
+               }
+       ;
+
+last_update_clause:
+       LAST UPDATE datetime { $$ = $3; }
+       |
+       /* empty */ { $$ = sdb_gettime(); }
+
+metric_store_clause:
+       STORE STRING STRING { $$.type = $2; $$.id = $3; }
+       |
+       /* empty */ { $$.type = $$.id = NULL; }
+
 /*
  * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
  *
index 63ec226c6dc17cea869c3187a66908f28b26117f..cc107c6ed46f7f1eb38a1ae3efea3ea94d8d4e2a 100644 (file)
@@ -250,6 +250,36 @@ sdb_fe_exec(sdb_conn_t *conn, sdb_conn_node_t *node)
                                filter = CONN_LOOKUP(node)->filter->matcher;
                        return sdb_fe_exec_lookup(conn,
                                        CONN_LOOKUP(node)->type, m, filter);
+               case SDB_CONNECTION_STORE_HOST:
+               {
+                       conn_store_host_t *n = CONN_STORE_HOST(node);
+                       sdb_proto_host_t host = { n->last_update, n->name };
+                       return sdb_fe_store_host(conn, &host);
+               }
+               case SDB_CONNECTION_STORE_SERVICE:
+               {
+                       conn_store_svc_t *n = CONN_STORE_SVC(node);
+                       sdb_proto_service_t svc = { n->last_update, n->hostname, n->name };
+                       return sdb_fe_store_service(conn, &svc);
+               }
+               case SDB_CONNECTION_STORE_METRIC:
+               {
+                       conn_store_metric_t *n = CONN_STORE_METRIC(node);
+                       sdb_proto_metric_t metric = {
+                               n->last_update, n->hostname, n->name,
+                               n->store_type, n->store_id
+                       };
+                       return sdb_fe_store_metric(conn, &metric);
+               }
+               case SDB_CONNECTION_STORE_ATTRIBUTE:
+               {
+                       conn_store_attr_t *n = CONN_STORE_ATTR(node);
+                       sdb_proto_attribute_t attr = {
+                               n->last_update, n->parent_type, n->hostname, n->parent,
+                               n->key, n->value
+                       };
+                       return sdb_fe_store_attribute(conn, &attr);
+               }
                case SDB_CONNECTION_TIMESERIES:
                        return sdb_fe_exec_timeseries(conn,
                                        CONN_TS(node)->hostname, CONN_TS(node)->metric,
index 6781122c3c2b830547b66a0fe82a7e57b3f15cb4..3985bf87433553545a8ee7022cfb926370ab3b5b 100644 (file)
@@ -59,6 +59,7 @@ static struct {
        { "FILTER",      FILTER },
        { "IN",          IN },
        { "IS",          IS },
+       { "LAST",        LAST },
        { "LIST",        LIST },
        { "LOOKUP",      LOOKUP },
        { "MATCHING",    MATCHING },
@@ -66,7 +67,9 @@ static struct {
        { "NULL",        NULL_T },
        { "OR",          OR },
        { "START",       START },
+       { "STORE",       STORE },
        { "TIMESERIES",  TIMESERIES },
+       { "UPDATE",      UPDATE },
 
        /* object types */
        { "host",        HOST_T },
index 99e2a8df23bbff58d4417792efd092806d232c55..52b50d197ebc868438b5d1394c41edb05ecf3188 100644 (file)
@@ -277,6 +277,12 @@ typedef enum {
         */
        SDB_CONNECTION_STORE = 50,
 
+       /* Only used internally: */
+       SDB_CONNECTION_STORE_HOST,
+       SDB_CONNECTION_STORE_SERVICE,
+       SDB_CONNECTION_STORE_METRIC,
+       SDB_CONNECTION_STORE_ATTRIBUTE,
+
        /*
         * Command subcomponents.
         */