@cryptotaxi247 / netdata-1 / commits / ebdaffa1e

preparation for multi-host internal database

Costa Tsaousis committed Aug 5, 2016 at 08:07 UTC ebdaffa1e65bae092c8cb488cc0caec981454c71
4 files changed +73 -47
src/rrd.c
+40 -38
@@ -33,12 +33,24 @@ int rrd_delete_unupdated_dimensions = 0;
33
34 int rrd_update_every = UPDATE_EVERY;
35 int rrd_default_history_entries = RRD_DEFAULT_HISTORY_ENTRIES;
36 -
37 -RRDSET *rrdset_root = NULL;
38 -pthread_rwlock_t rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER;
39 -
36 int rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
37
38 +static int rrdset_compare(void* a, void* b);
39 +static int rrdset_compare_name(void* a, void* b);
40 +
41 +RRDHOST localhost = {
42 + .hostname = "localhost",
43 + .rrdset_root = NULL,
44 + .rrdset_root_rwlock = PTHREAD_RWLOCK_INITIALIZER,
45 + .rrdset_root_index = {
46 + { NULL, rrdset_compare },
47 + AVL_LOCK_INITIALIZER
48 + },
49 + .rrdset_root_index_name = {
50 + { NULL, rrdset_compare_name },
51 + AVL_LOCK_INITIALIZER
52 + }
53 +};
54
55 // ----------------------------------------------------------------------------
56 // RRDSET index
@@ -49,20 +61,15 @@ static int rrdset_compare(void* a, void* b) {
61 else return strcmp(((RRDSET *)a)->id, ((RRDSET *)b)->id);
62 }
63
52 -avl_tree_lock rrdset_root_index = {
53 - { NULL, rrdset_compare },
54 - AVL_LOCK_INITIALIZER
55 -};
64 +#define rrdset_index_add(host, st) avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
65 +#define rrdset_index_del(host, st) avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
66
57 -#define rrdset_index_add(st) avl_insert_lock(&rrdset_root_index, (avl *)(st))
58 -#define rrdset_index_del(st) avl_remove_lock(&rrdset_root_index, (avl *)(st))
59 -
60 -static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
67 +static RRDSET *rrdset_index_find(RRDHOST *host, const char *id, uint32_t hash) {
68 RRDSET tmp;
69 strncpyz(tmp.id, id, RRD_ID_LENGTH_MAX);
70 tmp.hash = (hash)?hash:simple_hash(tmp.id);
71
65 - return (RRDSET *)avl_search_lock(&(rrdset_root_index), (avl *) &tmp);
72 + return (RRDSET *)avl_search_lock(&(host->rrdset_root_index), (avl *) &tmp);
73 }
74
75 // ----------------------------------------------------------------------------
@@ -81,26 +88,21 @@ static int rrdset_compare_name(void* a, void* b) {
88 else return strcmp(A->name, B->name);
89 }
90
84 -avl_tree_lock rrdset_root_index_name = {
85 - { NULL, rrdset_compare_name },
86 - AVL_LOCK_INITIALIZER
87 -};
88 -
89 -RRDSET *rrdset_index_add_name(RRDSET *st) {
91 +RRDSET *rrdset_index_add_name(RRDHOST *host, RRDSET *st) {
92 // fprintf(stderr, "ADDING: %s (name: %s)\n", st->id, st->name);
91 - return (RRDSET *)avl_insert_lock(&rrdset_root_index_name, (avl *) (&st->avlname));
93 + return (RRDSET *)avl_insert_lock(&host->rrdset_root_index_name, (avl *) (&st->avlname));
94 }
95
94 -#define rrdset_index_del_name(st) avl_remove_lock(&rrdset_root_index_name, (avl *)(&st->avlname))
96 +#define rrdset_index_del_name(host, st) avl_remove_lock(&((host)->rrdset_root_index_name), (avl *)(&st->avlname))
97
96 -static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
98 +static RRDSET *rrdset_index_find_name(RRDHOST *host, const char *name, uint32_t hash) {
99 void *result = NULL;
100 RRDSET tmp;
101 tmp.name = name;
102 tmp.hash_name = (hash)?hash:simple_hash(tmp.name);
103
104 // fprintf(stderr, "SEARCHING: %s\n", name);
103 - result = avl_search_lock(&(rrdset_root_index_name), (avl *) (&(tmp.avlname)));
105 + result = avl_search_lock(&host->rrdset_root_index_name, (avl *) (&(tmp.avlname)));
106 if(result) {
107 RRDSET *st = rrdset_from_avlname(result);
108 if(strcmp(st->magic, RRDSET_MAGIC))
@@ -256,7 +258,7 @@ void rrdset_set_name(RRDSET *st, const char *name)
258 {
259 debug(D_RRD_CALLS, "rrdset_set_name() old: %s, new: %s", st->name, name);
260
259 - if(st->name) rrdset_index_del_name(st);
261 + if(st->name) rrdset_index_del_name(&localhost, st);
262
263 char b[CONFIG_MAX_VALUE + 1];
264 char n[RRD_ID_LENGTH_MAX + 1];
@@ -266,7 +268,7 @@ void rrdset_set_name(RRDSET *st, const char *name)
268 st->name = config_get(st->id, "name", b);
269 st->hash_name = simple_hash(st->name);
270
269 - rrdset_index_add_name(st);
271 + rrdset_index_add_name(&localhost, st);
272 }
273
274 // ----------------------------------------------------------------------------
@@ -447,7 +449,7 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
449 avl_init_lock(&st->dimensions_index, rrddim_compare);
450
451 pthread_rwlock_init(&st->rwlock, NULL);
450 - pthread_rwlock_wrlock(&rrdset_root_rwlock);
452 + pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
453
454 if(name && *name) rrdset_set_name(st, name);
455 else rrdset_set_name(st, id);
@@ -458,12 +460,12 @@ RRDSET *rrdset_create(const char *type, const char *id, const char *name, const
460 st->title = config_get(st->id, "title", varvalue);
461 }
462
461 - st->next = rrdset_root;
462 - rrdset_root = st;
463 + st->next = localhost.rrdset_root;
464 + localhost.rrdset_root = st;
465
464 - rrdset_index_add(st);
466 + rrdset_index_add(&localhost, st);
467
466 - pthread_rwlock_unlock(&rrdset_root_rwlock);
468 + pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
469
470 return(st);
471 }
@@ -650,13 +652,13 @@ void rrdset_free_all(void)
652 info("Freeing all memory...");
653
654 RRDSET *st;
653 - for(st = rrdset_root; st ;) {
655 + for(st = localhost.rrdset_root; st ;) {
656 RRDSET *next = st->next;
657
658 while(st->dimensions)
659 rrddim_free(st, st->dimensions);
660
659 - rrdset_index_del(st);
661 + rrdset_index_del(&localhost, st);
662
663 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
664 debug(D_RRD_CALLS, "Saving stats '%s' to '%s'.", st->name, st->cache_filename);
@@ -674,7 +676,7 @@ void rrdset_free_all(void)
676
677 st = next;
678 }
677 - rrdset_root = NULL;
679 + localhost.rrdset_root = NULL;
680
681 info("Memory cleanup completed...");
682 }
@@ -685,8 +687,8 @@ void rrdset_save_all(void) {
687 RRDSET *st;
688 RRDDIM *rd;
689
688 - pthread_rwlock_wrlock(&rrdset_root_rwlock);
689 - for(st = rrdset_root; st ; st = st->next) {
690 + pthread_rwlock_wrlock(&localhost.rrdset_root_rwlock);
691 + for(st = localhost.rrdset_root; st ; st = st->next) {
692 pthread_rwlock_wrlock(&st->rwlock);
693
694 if(st->mapped == RRD_MEMORY_MODE_SAVE) {
@@ -703,7 +705,7 @@ void rrdset_save_all(void) {
705
706 pthread_rwlock_unlock(&st->rwlock);
707 }
706 - pthread_rwlock_unlock(&rrdset_root_rwlock);
708 + pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
709 }
710
711
@@ -711,7 +713,7 @@ RRDSET *rrdset_find(const char *id)
713 {
714 debug(D_RRD_CALLS, "rrdset_find() for chart %s", id);
715
714 - RRDSET *st = rrdset_index_find(id, 0);
716 + RRDSET *st = rrdset_index_find(&localhost, id, 0);
717 return(st);
718 }
719
@@ -733,7 +735,7 @@ RRDSET *rrdset_find_byname(const char *name)
735 {
736 debug(D_RRD_CALLS, "rrdset_find_byname() for chart %s", name);
737
736 - RRDSET *st = rrdset_index_find_name(name, 0);
738 + RRDSET *st = rrdset_index_find_name(&localhost, name, 0);
739 return(st);
740 }
741
src/rrd.h
+26 -2
@@ -254,8 +254,32 @@ struct rrdset {
254 };
255 typedef struct rrdset RRDSET;
256
257 -extern RRDSET *rrdset_root;
258 -extern pthread_rwlock_t rrdset_root_rwlock;
257 +// ----------------------------------------------------------------------------
258 +// RRD CONTEXT
259 +
260 +struct rrdcontext {
261 + avl avl;
262 +
263 + char *context;
264 +};
265 +typedef struct rrdcontext RRDCONTEXT;
266 +
267 +
268 +// ----------------------------------------------------------------------------
269 +// RRD HOST
270 +
271 +struct rrdhost {
272 + char *hostname;
273 +
274 + RRDSET *rrdset_root;
275 + pthread_rwlock_t rrdset_root_rwlock;
276 +
277 + avl_tree_lock rrdset_root_index;
278 + avl_tree_lock rrdset_root_index_name;
279 +};
280 +typedef struct rrdhost RRDHOST;
281 +
282 +extern RRDHOST localhost;
283
284 // ----------------------------------------------------------------------------
285 // RRD SET functions
src/rrd2json.c
+6 -6
@@ -96,8 +96,8 @@ void rrd_stats_api_v1_charts(BUFFER *wb)
96 , rrd_default_history_entries
97 );
98
99 - pthread_rwlock_rdlock(&rrdset_root_rwlock);
100 - for(st = rrdset_root, c = 0; st ; st = st->next) {
99 + pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
100 + for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
101 if(st->enabled && st->dimensions) {
102 if(c) buffer_strcat(wb, ",");
103 buffer_strcat(wb, "\n\t\t\"");
@@ -107,7 +107,7 @@ void rrd_stats_api_v1_charts(BUFFER *wb)
107 c++;
108 }
109 }
110 - pthread_rwlock_unlock(&rrdset_root_rwlock);
110 + pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
111
112 buffer_strcat(wb, "\n\t}\n}\n");
113 }
@@ -237,15 +237,15 @@ void rrd_stats_all_json(BUFFER *wb)
237
238 buffer_strcat(wb, RRD_GRAPH_JSON_HEADER);
239
240 - pthread_rwlock_rdlock(&rrdset_root_rwlock);
241 - for(st = rrdset_root, c = 0; st ; st = st->next) {
240 + pthread_rwlock_rdlock(&localhost.rrdset_root_rwlock);
241 + for(st = localhost.rrdset_root, c = 0; st ; st = st->next) {
242 if(st->enabled && st->dimensions) {
243 if(c) buffer_strcat(wb, ",\n");
244 memory += rrd_stats_one_json(st, NULL, wb);
245 c++;
246 }
247 }
248 - pthread_rwlock_unlock(&rrdset_root_rwlock);
248 + pthread_rwlock_unlock(&localhost.rrdset_root_rwlock);
249
250 buffer_sprintf(wb, "\n\t],\n"
251 "\t\"hostname\": \"%s\",\n"
src/web_client.c
+1 -1
@@ -1852,7 +1852,7 @@ void web_client_process(struct web_client *w) {
1852 debug(D_WEB_CLIENT_ACCESS, "%llu: Sending list of RRD_STATS...", w->id);
1853
1854 buffer_flush(w->response.data);
1855 - RRDSET *st = rrdset_root;
1855 + RRDSET *st = localhost.rrdset_root;
1856
1857 for ( ; st ; st = st->next )
1858 buffer_sprintf(w->response.data, "%s\n", st->name);