]> arthur.barton.de Git - netdata.git/commitdiff
preparation for multi-host internal database
authorCosta Tsaousis <costa@tsaousis.gr>
Fri, 5 Aug 2016 05:07:40 +0000 (08:07 +0300)
committerCosta Tsaousis <costa@tsaousis.gr>
Fri, 5 Aug 2016 05:07:40 +0000 (08:07 +0300)
src/rrd.c
src/rrd.h
src/rrd2json.c
src/web_client.c

index 47a23ea31e8167642b3d51935afb00fc81377dbc..5a46fb4e12a7781ca66201ea9f912aa360b0c35f 100644 (file)
--- a/src/rrd.c
+++ b/src/rrd.c
@@ -33,12 +33,24 @@ int rrd_delete_unupdated_dimensions = 0;
 
 int rrd_update_every = UPDATE_EVERY;
 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
-
-RRDSET *rrdset_root = NULL;
-pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
-
 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
 
+static int rrdset_compare(void* a, void* b);
+static int rrdset_compare_name(void* a, void* b);
+
+RRDHOST localhost = {
+               .hostname = "localhost",
+               .rrdset_root = NULL,
+               .rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER,
+        .rrdset_root_index = {
+            { NULL, rrdset_compare },
+            AVL_LOCK_INITIALIZER
+        },
+        .rrdset_root_index_name = {
+            { NULL, rrdset_compare_name },
+            AVL_LOCK_INITIALIZER
+        }
+};
 
 // ----------------------------------------------------------------------------
 // RRDSET index
@@ -49,20 +61,15 @@ static int rrdset_compare(void* a, void* b) {
        else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
 }
 
-avl_tree_lock rrdset_root_index = {
-               { NULL, rrdset_compare },
-               AVL_LOCK_INITIALIZER
-};
+#define rrdset_index_add(host, st) avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
+#define rrdset_index_del(host, st) avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
 
-#define rrdset_index_add(st) avl_insert_lock(&rrdset_root_index, (avl *)(st))
-#define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
-
-static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
+static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
        RRDSET tmp;
        strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
        tmp.hash = (hash)?hash:simple_hash(tmp.id);
 
-       return (RRDSET *)avl_search_lock(&(rrdset_root_index), (avl *) &tmp);
+       return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
 }
 
 // ----------------------------------------------------------------------------
@@ -81,26 +88,21 @@ static int rrdset_compare_name(void* a, void* b) {
        else return strcmp(A->name, B->name);
 }
 
-avl_tree_lock rrdset_root_index_name = {
-               { NULL, rrdset_compare_name },
-               AVL_LOCK_INITIALIZER
-};
-
-RRDSET *rrdset_index_add_name(RRDSET *st) {
+RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
        // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
-       return (RRDSET *)avl_insert_lock(&rrdset_root_index_name, (avl *) (&st->avlname));
+       return (RRDSET *)avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
 }
 
-#define rrdset_index_del_name(st) avl_remove_lock(&rrdset_root_index_name, (avl *)(&st->avlname))
+#define rrdset_index_del_name(host, st) avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname))
 
-static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
+static RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
        void *result = NULL;
        RRDSET tmp;
        tmp.name = name;
        tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
 
        // fprintf(stderr, "SEARCHING: %s\n", name);
-       result = avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)));
+       result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
        if(result) {
                RRDSET *st = rrdset_from_avlname(result);
                if(strcmp(st->magic, RRDSET_MAGIC))
@@ -256,7 +258,7 @@ void rrdset_set_name(RRDSET *st, const char *name)
 {
        debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
 
-       if(st->name) rrdset_index_del_name(st);
+       if(st->name) rrdset_index_del_name(&localhost, st);
 
        char b[CONFIG_MAX_VALUE + 1];
        char n[RRD_ID_LENGTH_MAX + 1];
@@ -266,7 +268,7 @@ void rrdset_set_name(RRDSET *st, const char *name)
        st->name = config_get(st->id, "name", b);
        st->hash_name = simple_hash(st->name);
 
-       rrdset_index_add_name(st);
+       rrdset_index_add_name(&localhost, st);
 }
 
 // ----------------------------------------------------------------------------
@@ -447,7 +449,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
        avl_init_lock(&st->dimensions_index, rrddim_compare);
 
        pthread_rwlock_init(&st->rwlock, NULL);
-       pthread_rwlock_wrlock(&rrdset_root_rwlock);
+       pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
 
        if(name && *name) rrdset_set_name(st, name);
        else rrdset_set_name(st, id);
@@ -458,12 +460,12 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
                st->title = config_get(st->id, "title", varvalue);
        }
 
-       st->next = rrdset_root;
-       rrdset_root = st;
+       st->next = localhost.rrdset_root;
+    localhost.rrdset_root = st;
 
-       rrdset_index_add(st);
+       rrdset_index_add(&localhost, st);
 
-       pthread_rwlock_unlock(&rrdset_root_rwlock);
+       pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
 
        return(st);
 }
@@ -650,13 +652,13 @@ void rrdset_free_all(void)
        info("Freeing all memory...");
 
        RRDSET *st;
-       for(st = rrdset_root; st ;) {
+       for(st = localhost.rrdset_root; st ;) {
                RRDSET *next = st->next;
 
                while(st->dimensions)
                        rrddim_free(st, st->dimensions);
 
-               rrdset_index_del(st);
+               rrdset_index_del(&localhost, st);
 
                if(st->mapped == RRD_MEMORY_MODE_SAVE) {
                        debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
@@ -674,7 +676,7 @@ void rrdset_free_all(void)
 
                st = next;
        }
-       rrdset_root = NULL;
+    localhost.rrdset_root = NULL;
 
        info("Memory cleanup completed...");
 }
@@ -685,8 +687,8 @@ void rrdset_save_all(void) {
        RRDSET *st;
        RRDDIM *rd;
 
-       pthread_rwlock_wrlock(&rrdset_root_rwlock);
-       for(st = rrdset_root; st ; st = st->next) {
+       pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
+       for(st = localhost.rrdset_root; st ; st = st->next) {
                pthread_rwlock_wrlock(&st->rwlock);
 
                if(st->mapped == RRD_MEMORY_MODE_SAVE) {
@@ -703,7 +705,7 @@ void rrdset_save_all(void) {
 
                pthread_rwlock_unlock(&st->rwlock);
        }
-       pthread_rwlock_unlock(&rrdset_root_rwlock);
+       pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
 }
 
 
@@ -711,7 +713,7 @@ RRDSET *rrdset_find(const char *id)
 {
        debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
 
-       RRDSET *st = rrdset_index_find(id, 0);
+       RRDSET *st = rrdset_index_find(&localhost, id, 0);
        return(st);
 }
 
@@ -733,7 +735,7 @@ RRDSET *rrdset_find_byname(const char *name)
 {
        debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
 
-       RRDSET *st = rrdset_index_find_name(name, 0);
+       RRDSET *st = rrdset_index_find_name(&localhost, name, 0);
        return(st);
 }
 
index 83ab35b69938cebbfebb0316811a58ff2a65e50f..78ad7e87c965f2123872604591f2dd6ca705c7c2 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
@@ -254,8 +254,32 @@ struct rrdset {
 };
 typedef struct rrdset RRDSET;
 
-extern RRDSET *rrdset_root;
-extern pthread_rwlock_t rrdset_root_rwlock;
+// ----------------------------------------------------------------------------
+// RRD CONTEXT
+
+struct rrdcontext {
+    avl avl;
+
+    char *context;
+};
+typedef struct rrdcontext RRDCONTEXT;
+
+
+// ----------------------------------------------------------------------------
+// RRD HOST
+
+struct rrdhost {
+    char *hostname;
+
+    RRDSET *rrdset_root;
+    pthread_rwlock_t rrdset_root_rwlock;
+
+    avl_tree_lock rrdset_root_index;
+    avl_tree_lock rrdset_root_index_name;
+};
+typedef struct rrdhost RRDHOST;
+
+extern RRDHOST localhost;
 
 // ----------------------------------------------------------------------------
 // RRD SET functions
index 0afb4531bc2e2e383603edab0f878c9e29450891..5da41736bdddc41f8eaa2ded47ebfec4f285fad8 100644 (file)
@@ -96,8 +96,8 @@ void rrd_stats_api_v1_charts(BUFFER *wb)
                , rrd_default_history_entries
                );
 
-       pthread_rwlock_rdlock(&rrdset_root_rwlock);
-       for(st = rrdset_root, c = 0; st ; st = st->next) {
+       pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
+       for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
                if(st->enabled && st->dimensions) {
                        if(c) buffer_strcat(wb, ",");
                        buffer_strcat(wb, "\n\t\t\"");
@@ -107,7 +107,7 @@ void rrd_stats_api_v1_charts(BUFFER *wb)
                        c++;
                }
        }
-       pthread_rwlock_unlock(&rrdset_root_rwlock);
+       pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
 
        buffer_strcat(wb, "\n\t}\n}\n");
 }
@@ -237,15 +237,15 @@ void rrd_stats_all_json(BUFFER *wb)
 
        buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
 
-       pthread_rwlock_rdlock(&rrdset_root_rwlock);
-       for(st = rrdset_root, c = 0; st ; st = st->next) {
+       pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
+       for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
                if(st->enabled && st->dimensions) {
                        if(c) buffer_strcat(wb, ",\n");
                        memory += rrd_stats_one_json(st, NULL, wb);
                        c++;
                }
        }
-       pthread_rwlock_unlock(&rrdset_root_rwlock);
+       pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
 
        buffer_sprintf(wb, "\n\t],\n"
                "\t\"hostname\": \"%s\",\n"
index 4714ef8d979754d036944c53bbf703779aed344b..ea4a942e78d404913950a97d1fa94f25d5132a04 100644 (file)
@@ -1852,7 +1852,7 @@ void web_client_process(struct web_client *w) {
                                        debug(D_WEB_CLIENT_ACCESS, "%llu: Sending list of RRD_STATS...", w->id);
 
                                        buffer_flush(w->response.data);
-                                       RRDSET *st = rrdset_root;
+                                       RRDSET *st = localhost.rrdset_root;
 
                                        for ( ; st ; st = st->next )
                                                buffer_sprintf(w->response.data, "%s\n", st->name);