Add the ability to perform a data query using an offline node id (#12650)
* Add the ability to build a host structure by node id to execute queries for archived hosts * Add the ability to execute queries from the cloud for archived hosts by node id * Add free_temporary_host function
Stelios Fragkakis committed
Apr 19, 2022 at 11:32 UTC
3e1ed14d8e8fd30864ddde6a236467dc4919ed13
6 files changed
+51
-16
aclk/aclk_query.c
+11
-6
@@ -111,6 +111,7 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
111
w->tv_in = query->created_tv;
112
now_realtime_timeval(&w->tv_ready);
113
114
+ RRDHOST *temp_host = NULL;
115
if (!strncmp(query->data.http_api_v2.query, NODE_ID_QUERY, strlen(NODE_ID_QUERY))) {
116
char *node_uuid = query->data.http_api_v2.query + strlen(NODE_ID_QUERY);
117
char nodeid[UUID_STR_LEN];
@@ -125,11 +126,14 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
126
127
query_host = node_id_2_rrdhost(nodeid);
128
if (!query_host) {
128
- error_report("Host with node_id \"%s\" not found! Returning 404 to Cloud!", nodeid);
129
- retval = 1;
130
- w->response.code = 404;
131
- aclk_http_msg_v2_err(query_thr->client, query->callback_topic, query->msg_id, w->response.code, CLOUD_EC_NODE_NOT_FOUND, CLOUD_EMSG_NODE_NOT_FOUND, NULL, 0);
132
- goto cleanup;
129
+ temp_host = sql_create_host_by_uuid(nodeid);
130
+ if (!temp_host) {
131
+ error_report("Host with node_id \"%s\" not found! Returning 404 to Cloud!", nodeid);
132
+ retval = 1;
133
+ w->response.code = 404;
134
+ aclk_http_msg_v2_err(query_thr->client, query->callback_topic, query->msg_id, w->response.code, CLOUD_EC_NODE_NOT_FOUND, CLOUD_EMSG_NODE_NOT_FOUND, NULL, 0);
135
+ goto cleanup;
136
+ }
137
}
138
}
139
@@ -150,7 +154,8 @@ static int http_api_v2(struct aclk_query_thread *query_thr, aclk_query_t query)
154
}
155
156
// execute the query
153
- t = aclk_web_api_v1_request(query_host, w, mysep ? mysep + 1 : "noop");
157
+ t = aclk_web_api_v1_request(query_host ? query_host : temp_host, w, mysep ? mysep + 1 : "noop");
158
+ free_temporary_host(temp_host);
159
size = (w->mode == WEB_CLIENT_MODE_FILECOPY) ? w->response.rlen : w->response.data->len;
160
sent = size;
161
database/sqlite/sqlite_functions.c
+17
-1
@@ -1160,8 +1160,24 @@ failed:
1160
return;
1161
}
1162
1163
+void free_temporary_host(RRDHOST *host)
1164
+{
1165
+ if (host) {
1166
+ freez(host->hostname);
1167
+ freez((char *)host->os);
1168
+ freez((char *)host->tags);
1169
+ freez((char *)host->timezone);
1170
+ freez(host->program_name);
1171
+ freez(host->program_version);
1172
+ freez(host->registry_hostname);
1173
+ freez(host->system_info);
1174
+ freez(host);
1175
+ }
1176
+}
1177
+
1178
#define SELECT_HOST "select host_id, registry_hostname, update_every, os, timezone, tags from host where hostname = @hostname order by rowid desc;"
1164
-#define SELECT_HOST_BY_UUID "select host_id, registry_hostname, update_every, os, timezone, tags from host where host_id = @host_id ;"
1179
+#define SELECT_HOST_BY_UUID "select h.host_id, h.registry_hostname, h.update_every, h.os, h.timezone, h.tags from host h, node_instance ni " \
1180
+ "where (ni.host_id = @host_id or ni.node_id = @host_id) AND ni.host_id = h.host_id;"
1181
1182
RRDHOST *sql_create_host_by_uuid(char *hostname)
1183
{
database/sqlite/sqlite_functions.h
+1
@@ -100,4 +100,5 @@ extern void sql_load_node_id(RRDHOST *host);
100
extern void compute_chart_hash(RRDSET *st);
101
extern int sql_set_dimension_option(uuid_t *dim_uuid, char *option);
102
char *get_hostname_by_node_id(char *node_id);
103
+void free_temporary_host(RRDHOST *host);
104
#endif //NETDATA_SQLITE_FUNCTIONS_H
web/api/tests/valid_urls.c
+11
@@ -8,6 +8,17 @@
8
#include <cmocka.h>
9
#include <stdbool.h>
10
11
+void free_temporary_host(RRDHOST *host)
12
+{
13
+ (void) host;
14
+}
15
+
16
+void *__wrap_free_temporary_host(RRDHOST *host)
17
+{
18
+ (void) host;
19
+}
20
+
21
+
22
RRDHOST *sql_create_host_by_uuid(char *hostname)
23
{
24
(void) hostname;
web/api/tests/web_api.c
+10
@@ -8,6 +8,16 @@
8
#include <cmocka.h>
9
#include <stdbool.h>
10
11
+void free_temporary_host(RRDHOST *host)
12
+{
13
+ (void) host;
14
+}
15
+
16
+void *__wrap_free_temporary_host(RRDHOST *host)
17
+{
18
+ (void) host;
19
+}
20
+
21
RRDHOST *sql_create_host_by_uuid(char *hostname)
22
{
23
(void) hostname;
web/server/web_client.c
+1
-9
@@ -1309,15 +1309,7 @@ static inline int web_client_switch_host(RRDHOST *host, struct web_client *w, ch
1309
host = sql_create_host_by_uuid(tok);
1310
if (likely(host)) {
1311
int rc = web_client_process_url(host, w, url);
1312
- freez(host->hostname);
1313
- freez((char *)host->os);
1314
- freez((char *)host->tags);
1315
- freez((char *)host->timezone);
1316
- freez(host->program_name);
1317
- freez(host->program_version);
1318
- freez(host->registry_hostname);
1319
- freez(host->system_info);
1320
- freez(host);
1312
+ free_temporary_host(host);
1313
return rc;
1314
}
1315
}