From 5dd410aab93c4ea5c3c04662585c072172ea5547 Mon Sep 17 00:00:00 2001 From: Johann CAHIER Date: Thu, 16 May 2019 17:35:59 +0200 Subject: Better memory management in chained list key is now managed internally, using a copy. add_elt() / add_key() now provide a 'suffix' parameter. If suffix is given, it is appended to the key. If NULL, or empty string ("") is given, nothing is appended. NOTE : value is still owned by json object. Bug-AGL: SPEC-2416 Change-Id: I624a2dd211801c2d24b2c6739f2c7536a047ea32 Signed-off-by: Johann CAHIER --- src/plugins/influxdb-reader.c | 4 ++-- src/plugins/influxdb-writer.c | 17 ++++++++++------- src/plugins/influxdb.c | 30 +++++++----------------------- src/utils/list.c | 29 +++++++++++++++++++++++------ src/utils/list.h | 10 +++++----- 5 files changed, 47 insertions(+), 43 deletions(-) diff --git a/src/plugins/influxdb-reader.c b/src/plugins/influxdb-reader.c index d7ddac7..c633e9f 100644 --- a/src/plugins/influxdb-reader.c +++ b/src/plugins/influxdb-reader.c @@ -70,10 +70,10 @@ static void fill_key(void *c, json_object *columnJ) struct metrics_list *m_list = (struct metrics_list *)c; if(strncasecmp(&column[length-2], "_t", 2) == 0) { - add_key(&m_list->serie.serie_columns.tags, column); + add_key(&m_list->serie.serie_columns.tags, column, ""); } else if(strncasecmp(&column[length-2], "_f", 2) == 0) { - add_key(&m_list->serie.serie_columns.fields, column); + add_key(&m_list->serie.serie_columns.fields, column, ""); } } diff --git a/src/plugins/influxdb-writer.c b/src/plugins/influxdb-writer.c index 2815b19..a7ce8fc 100644 --- a/src/plugins/influxdb-writer.c +++ b/src/plugins/influxdb-writer.c @@ -136,7 +136,7 @@ CURL *make_curl_write_post(afb_api_t apiHandle, const char *url, json_object *me curl_wrap_prepare_post_unescaped(url, NULL, "\n", (const char * const*)post_data) : NULL; free(serie); for(i = 0; i < lpd; i++) - free(post_data[i]); + if (post_data[i]) free(post_data[i]); free(post_data); return curl; @@ -155,6 +155,7 @@ CTLP_CAPI(write_to_influxdb, source, argsJ, eventJ) const char *port = NULL; const char *host = NULL; CURL *curl_request; + int rc = -1; json_object *req_args = afb_req_json(request), *portJ = NULL, @@ -163,15 +164,17 @@ CTLP_CAPI(write_to_influxdb, source, argsJ, eventJ) if(wrap_json_unpack(req_args, "{s?s,s?o,so!}", "host", &host, "port", &portJ, - "metric", &metric) || ! metric) + "metric", &metric) || ! metric) { afb_req_fail(request, "Failed", "Error processing arguments. Miss metric\ JSON object or malformed"); - else + rc = -1; + } else { port = json_object_is_type(portJ, json_type_null) ? NULL : json_object_to_json_string(portJ); + curl_request = influxdb_write(source->api, host, port, metric); + curl_wrap_do(curl_request, influxdb_write_curl_cb, request); + rc = 0; + } - curl_request = influxdb_write(source->api, host, port, metric); - curl_wrap_do(curl_request, influxdb_write_curl_cb, request); - - return 0; + return rc; } diff --git a/src/plugins/influxdb.c b/src/plugins/influxdb.c index 2cfd551..b8f85d2 100644 --- a/src/plugins/influxdb.c +++ b/src/plugins/influxdb.c @@ -117,28 +117,12 @@ int create_database(afb_req_t request) void unpack_values(void* l, json_object* valuesJ, const char* key) { - struct list** oneList = (struct list**)l; - - /* Append a suffix to be able to differentiate tags and fields at reading - time */ - char* suffixed_key = calloc(1, strlen(key) + 3); - strcpy(suffixed_key, key); - // strcat(suffixed_key, "_f"); - - add_elt(oneList, suffixed_key, valuesJ); + add_elt((struct list**)l, key, "", valuesJ); } void unpack_metadata(void* l, json_object* valuesJ, const char* key) { - struct list** oneList = (struct list**)l; - - /* Append a suffix to be able to differentiate tags and fields at reading - time */ - char* suffixed_key = calloc(1, strlen(key) + 3); - strcpy(suffixed_key, key); - // strcat(suffixed_key, "_t"); - - add_elt(oneList, suffixed_key, valuesJ); + add_elt((struct list**)l, key, "", valuesJ); } void unpacking_from_api(void* s, json_object* valueJ, const char* key) @@ -156,13 +140,13 @@ void unpacking_from_api(void* s, json_object* valueJ, const char* key) else if (strcasecmp("value", key) == 0 || strcasecmp("values", key) == 0) wrap_json_object_for_all(valueJ, unpack_values, (void*)&serie->serie_columns.fields); /* Treat all key looking for tag and field object. Those ones could be find - with the last 2 character. '_t' for tag and '_f' that are the keys that - could be indefinite. Cf influxdb documentation: - https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_reference/ */ + with the last 2 character. '_t' for tag and '_f' that are the keys that + could be indefinite. Cf influxdb documentation: + https://docs.influxdata.com/influxdb/v1.5/write_protocols/line_protocol_reference/ */ else if (strncasecmp(&key[key_length - 2], "_t", 2) == 0) - add_elt(&serie->serie_columns.tags, key, valueJ); + add_elt(&serie->serie_columns.tags, key, "", valueJ); else if (strncasecmp(&key[key_length - 2], "_f", 2) == 0) - add_elt(&serie->serie_columns.fields, key, valueJ); + add_elt(&serie->serie_columns.fields, key, "", valueJ); } int unpack_metric_from_api(json_object* m, struct series_t* serie) diff --git a/src/utils/list.c b/src/utils/list.c index c5152a3..030c759 100644 --- a/src/utils/list.c +++ b/src/utils/list.c @@ -25,14 +25,31 @@ void destroy_list(struct list* l) struct list* save; while (l != NULL) { save = l->next; + free(l->key); free(l); l = save; } } -void add_elt(struct list** l, const char* key, json_object* value) +void add_elt(struct list** l, const char* key, const char* suffix, json_object* value) { - struct list* new_elt; + struct list* new_elt = NULL; + char *suffixed_key = NULL; + size_t key_len = strlen(key), suffix_len = -1; + + if (suffix == NULL) { + suffix = ""; + suffix_len=0; + } else { + suffix_len = strlen(suffix); + } + suffixed_key = malloc(key_len + suffix_len + 1); + if (!suffixed_key) { + fprintf(stderr, "list.c, add_elt() : can't allocate 'suffixed_key' (out of memory)"); + exit(1); + } + strncpy(suffixed_key, key, key_len); + strncpy(suffixed_key+key_len, suffix, suffix_len+1); // search tail new_elt = *l; @@ -44,19 +61,19 @@ void add_elt(struct list** l, const char* key, json_object* value) // alloc new elem new_elt = malloc(sizeof(struct list)); if (!new_elt) { - fprintf(stderr, "OUT of memory"); + fprintf(stderr, "list.c, add_elt() : can't allocate 'new_elt' (out of memory)"); exit(1); } *l = new_elt; - new_elt->key = key; + new_elt->key = suffixed_key; new_elt->value = value; new_elt->next = NULL; } -void add_key(struct list** l, const char* key) +void add_key(struct list** l, const char* key, const char* suffix) { - add_elt(l, key, NULL); + add_elt(l, key, suffix, NULL); } int set_value(struct list* l, json_object* val, int index) diff --git a/src/utils/list.h b/src/utils/list.h index 9f93cb6..c0d559f 100644 --- a/src/utils/list.h +++ b/src/utils/list.h @@ -21,17 +21,17 @@ #include struct list { - const char *key; + char *key; json_object *value; struct list *next; }; void destroy_list(struct list *l); -void add_elt(struct list **l, const char *key, json_object *value); -void add_key(struct list **l, const char *key); +void add_elt(struct list **l, const char *key, const char* suffix, json_object *value); +void add_key(struct list **l, const char *key, const char* suffix); int set_value(struct list *l, json_object *val, int index); struct list *get_elt(struct list *l, int index); -struct list *find_elt_from_key(struct list *l, const char *key); -json_object *find_key_value(struct list *l, const char *key); +struct list *find_elt_from_key(struct list *l, const char *suffixed_key); +json_object *find_key_value(struct list *l, const char *suffixed_key); #endif -- cgit 1.2.3-korg