]> arthur.barton.de Git - netdata.git/commitdiff
work in progress: registry
authorCosta Tsaousis <costa@tsaousis.gr>
Sat, 7 May 2016 01:52:03 +0000 (04:52 +0300)
committerCosta Tsaousis <costa@tsaousis.gr>
Sat, 7 May 2016 01:52:03 +0000 (04:52 +0300)
configure.ac
src/dictionary.c
src/dictionary.h
src/log.h
src/registry.c [new file with mode: 0644]

index 177b6ddbd54266624aa2c78a96687742c138d1a7..7979bfbfbe231bafe5b17b13be82839b35abd495 100644 (file)
@@ -92,6 +92,10 @@ if test -z "${MATH_LIBS}"; then
        )
 fi
 
+AC_CHECK_LIB([uuid], [uuid_generate_time_safe], [], \
+             [AC_MSG_ERROR([Function uuid_generate_time_safe was not found in libuuid.
+Is uuid-dev installed? Try running 'sudo apt-get install uuid-dev'.])])
+
 if test "${enable_plugin_nfacct}" = "yes"; then
        PKG_CHECK_MODULES(
                [NFACCT],
index 7c6a8342d04b49906e8938ce057a97389d76b689..c794d0865433625424c4a46bdff743d308f23131 100644 (file)
@@ -1,6 +1,7 @@
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
+
 #include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
@@ -58,9 +59,13 @@ static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const c
        NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
        if(unlikely(!nv)) fatal("Cannot allocate name_value of size %z", sizeof(NAME_VALUE));
 
-       nv->name = strdup(name);
-       if(unlikely(!nv->name))
-               fatal("Cannot allocate name_value.name of size %z", strlen(name));
+       if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
+               nv->name = (char *)name;
+       else {
+               nv->name = strdup(name);
+               if (unlikely(!nv->name))
+                       fatal("Cannot allocate name_value.name of size %z", strlen(name));
+       }
 
        nv->hash = (hash)?hash:simple_hash(nv->name);
 
index e9edab4c231d0d54fa2954d86862c54a74e21479..56e6e144b8d1d7758fab6b9c7a888596feae2c4e 100644 (file)
@@ -30,6 +30,7 @@ typedef struct dictionary {
 #define DICTIONARY_FLAG_DEFAULT                                        0x00000000
 #define DICTIONARY_FLAG_SINGLE_THREADED                        0x00000001
 #define DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE  0x00000002
+#define DICTIONARY_FLAG_NAME_LINK_DONT_CLONE   0x00000004
 
 extern DICTIONARY *dictionary_create(uint32_t flags);
 extern void dictionary_destroy(DICTIONARY *dict);
index 1607826e6748b1ac81d226e2056772c3538d2257..3f811d9fbe93bf3160a14049590b0ceb8356607f 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -26,6 +26,7 @@
 #define D_DICTIONARY           0x00040000
 #define D_MEMORY                       0x00080000
 #define D_CGROUP            0x00100000
+#define D_REGISTRY                     0x00200000
 
 //#define DEBUG (D_WEB_CLIENT_ACCESS|D_LISTENER|D_RRD_STATS)
 //#define DEBUG 0xffffffff
diff --git a/src/registry.c b/src/registry.c
new file mode 100644 (file)
index 0000000..6fbaade
--- /dev/null
@@ -0,0 +1,242 @@
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+// gcc -O3 -Wall -Wextra -I ../src/ -I ../ -o registry ../src/registry.c ../src/dictionary.o ../src/log.o ../src/avl.o ../src/common.o -pthread -luuid -DHAVE_CONFIG_H
+
+#include <uuid/uuid.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "log.h"
+#include "common.h"
+#include "dictionary.h"
+
+struct machine {
+       char guid[36 + 1];
+
+       time_t first_t;
+       time_t last_t;
+       size_t usages;
+       
+       DICTIONARY *urls;
+};
+typedef struct machine MACHINE;
+
+
+struct person {
+       char guid[36 + 1];
+
+       time_t first_t;
+       time_t last_t;
+       size_t usages;
+
+       DICTIONARY *urls;
+};
+typedef struct person PERSON;
+
+
+struct url {
+       MACHINE *machine;
+       PERSON *person;
+
+       time_t first_t;
+       time_t last_t;
+       size_t usages;
+
+       size_t url_length;
+       char url[];
+};
+typedef struct url URL;
+
+
+struct registry {
+       DICTIONARY *persons;
+       DICTIONARY *machines;
+} registry;
+
+
+// ----------------------------------------------------------------------------
+// MACHINE
+
+MACHINE *registry_machine_load(const char *machine_guid) {
+       (void)machine_guid;
+
+       return NULL;
+}
+
+int registry_machine_save(MACHINE *m) {
+       (void)m;
+
+       return -1;
+}
+
+MACHINE *registry_machine_find(const char *machine_guid) {
+       MACHINE *m = dictionary_get(registry.machines, machine_guid);
+       if(!m) m = registry_machine_load(machine_guid);
+       return m;
+}
+
+
+MACHINE *registry_machine_get(const char *machine_guid) {
+       MACHINE *m = registry_machine_find(machine_guid);
+       if(!m) {
+               debug(D_REGISTRY, "Registry: creating new machine '%s'", machine_guid);
+
+               m = calloc(1, sizeof(MACHINE));
+               if(!m) fatal("Registry: cannot allocate memory for new machine '%s'", machine_guid);
+
+               strncpy(m->guid, machine_guid, 36);
+
+               dictionary_set(registry.machines, m->guid, m, sizeof(MACHINE));
+               
+               m->first_t = time(NULL);
+               m->usages = 0;
+       }
+
+       return m;
+}
+
+
+// ----------------------------------------------------------------------------
+// PERSON
+
+PERSON *registry_person_load(const char *person_guid) {
+       (void)person_guid;
+
+       return NULL;
+}
+
+int registry_person_save(PERSON *p) {
+       (void)p;
+
+       return -1;
+}
+
+PERSON *registry_person_find(const char *person_guid) {
+       PERSON *p = dictionary_get(registry.persons, person_guid);
+       if(!p) p = registry_person_load(person_guid);
+       return p;
+}
+
+PERSON *registry_person_get(const char *person_guid) {
+       PERSON *p = NULL;
+
+       if(person_guid && *person_guid)
+               p = registry_person_find(person_guid);
+
+       if(!p) {
+               if(person_guid && *person_guid)
+                       error("Registry: discarding unknown person guid '%s'. Will give a new PERSONID.", person_guid);
+
+               debug(D_REGISTRY, "Registry: creating new person");
+
+               p = calloc(1, sizeof(PERSON));
+               if(!p) fatal("Registry: cannot allocate memory for new person.");
+
+               uuid_t uuid;
+               if(uuid_generate_time_safe(uuid) == -1)
+                       info("Registry: uuid_generate_time_safe() reports UUID generation is not safe for uniqueness.");
+
+               uuid_unparse_lower(uuid, p->guid);
+
+               dictionary_set(registry.persons, p->guid, p, sizeof(PERSON));
+
+               p->first_t = time(NULL);
+               p->usages = 0;
+       }
+
+       return p;
+}
+
+
+// ----------------------------------------------------------------------------
+// URL
+
+URL *registry_url_update(PERSON *p, MACHINE *m, const char *url) {
+       URL *pu = dictionary_get(p->urls, url);
+       URL *mu = dictionary_get(m->urls, url);
+       URL *u = NULL;
+
+       if(pu != mu || pu == NULL || mu == NULL)
+               error("Registry: person/machine discrepancy on url '%s', for person '%s' and machine '%s'", url, p->guid, m->guid);
+       else
+               u = pu;
+
+       if(!u) {
+               size_t len = strlen(url);
+
+               URL *u = calloc(1, sizeof(URL) + len);
+               if(!u) fatal("Registry: cannot allocate memory for person '%s', machine '%s', url '%s'.", p->guid, m->guid, url);
+
+               strcpy(u->url, url);
+               u->url_length = len;
+               u->person = p;
+               u->machine = m;
+               u->first_t = time(NULL);
+               u->usages = 1;
+
+               dictionary_set(p->urls, url, u, sizeof(URL) + u->url_length);
+               dictionary_set(m->urls, url, u, sizeof(URL) + u->url_length);
+               if(pu) free(pu);
+               if(mu) free(mu);
+       }
+       else
+               u->usages++;
+
+       p->usages++;
+       m->usages++;
+       p->last_t = m->last_t = u->last_t = time(NULL);
+
+       return u;
+}
+
+
+// ----------------------------------------------------------------------------
+// REGISTRY
+
+int registry_save(void) {
+       return -1;
+}
+
+char *registry_request(const char *person_guid, const char *machine_guid, const char *url) {
+       PERSON *p = NULL;
+       MACHINE *m = NULL;
+
+       // --- PERSON ---
+       p = registry_person_get(person_guid);
+       person_guid = p->guid;
+
+       // --- MACHINE ---
+       m = registry_machine_get(machine_guid);
+       machine_guid = m->guid;
+
+       // --- URL ---
+       registry_url_update(p, m, url);
+
+       registry_person_save(p);
+       registry_machine_save(m);
+       registry_save();
+
+       return NULL;
+}
+
+void registry_init(void) {
+       registry.persons = dictionary_create(DICTIONARY_FLAG_DEFAULT);
+       if(!registry.persons)
+               fatal("Registry: cannot create persons registry");
+
+       registry.machines = dictionary_create(DICTIONARY_FLAG_DEFAULT);
+       if(!registry.machines)
+               fatal("Registry: cannot create machines registry");
+}
+
+int main(int argc, char **argv) {
+       (void)argc;
+       (void)argv;
+
+       registry_init();
+
+       return 0;
+}