Code

plugin: Reset context on some error conditions.
[sysdb.git] / src / core / plugin.c
index ecd84b5f9c5f5f525017872ecdf54fe234a6267f..76fc2092d022ff8588a2b5f11739e8653fa5c1c4 100644 (file)
@@ -276,8 +276,13 @@ typedef struct {
        sdb_object_t super;
        sdb_store_writer_t *w;
        sdb_object_t *ud;
+       sdb_query_opts_t opts;
 } query_writer_t;
-#define QUERY_WRITER_INIT(w, ud) { SDB_OBJECT_INIT, (w), (ud) }
+#define QUERY_WRITER_INIT(w, ud) { \
+       SDB_OBJECT_INIT, \
+       (w), (ud), \
+       SDB_DEFAULT_QUERY_OPTS \
+}
 #define QUERY_WRITER(obj) ((query_writer_t *)(obj))
 
 static int
@@ -305,8 +310,11 @@ query_store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
        int status;
        size_t i;
 
+       if (! qw->opts.describe_timeseries)
+               /* nothing further to do */
+               return qw->w->store_metric(metric, qw->ud);
+
        for (i = 0; i < metric->stores_num; i++) {
-               /* TODO: Make this optional using query options. */
                sdb_metric_store_t *s = stores + i;
                *s = metric->stores[i];
                infos[i] = sdb_plugin_describe_timeseries(s->type, s->id);
@@ -718,6 +726,7 @@ module_load(const char *basedir, const char *name,
 
        if ((status = module_init(name, lh, &ctx->info))) {
                sdb_object_deref(SDB_OBJ(ctx));
+               ctx_set(NULL);
                return status;
        }
 
@@ -875,7 +884,7 @@ get_interval(int obj_type, const char *hostname,
        fetch.name = n;
 
        status = sdb_plugin_query(SDB_AST_NODE(&fetch),
-                       &interval_fetcher, SDB_OBJ(&obj), NULL);
+                       &interval_fetcher, SDB_OBJ(&obj), NULL, NULL);
        if ((status < 0) || (lu.obj_type != obj_type) || (lu.last_update == 0)) {
                *interval_out = 0;
                return 0;
@@ -945,13 +954,13 @@ sdb_plugin_load(const char *basedir, const char *name,
                        ctx_t *old_ctx = ctx_set(ctx);
 
                        status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
+                       ctx_set(old_ctx);
                        if (status)
                                return status;
 
                        sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
                                        "'%s' (%s)", ctx->info.plugin_name,
                                        INFO_GET(&ctx->info, description));
-                       ctx_set(old_ctx);
                }
                ++ctx->use_cnt;
                return 0;
@@ -1188,7 +1197,7 @@ sdb_plugin_get_ctx(void)
 
        c = ctx_get();
        if (! c) {
-               sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
+               sdb_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
                                "context outside a plugin");
                return plugin_default_ctx;
        }
@@ -1202,7 +1211,7 @@ sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
 
        c = ctx_get();
        if (! c) {
-               sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
+               sdb_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
                                "context outside a plugin");
                return -1;
        }
@@ -1649,7 +1658,8 @@ sdb_plugin_describe_timeseries(const char *type, const char *id)
 
 int
 sdb_plugin_query(sdb_ast_node_t *ast,
-               sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf)
+               sdb_store_writer_t *w, sdb_object_t *wd,
+               sdb_query_opts_t *opts, sdb_strbuf_t *errbuf)
 {
        query_writer_t qw = QUERY_WRITER_INIT(w, wd);
        reader_t *reader;
@@ -1661,6 +1671,9 @@ sdb_plugin_query(sdb_ast_node_t *ast,
        if (! ast)
                return 0;
 
+       if (opts)
+               qw.opts = *opts;
+
        if ((ast->type != SDB_AST_TYPE_FETCH)
                        && (ast->type != SDB_AST_TYPE_LIST)
                        && (ast->type != SDB_AST_TYPE_LOOKUP)) {