5
6
#include "sqlite_aclk_node.h"
7
8
+struct aclk_sync_config_s {
9
+ uv_thread_t thread;
10
+ uv_loop_t loop;
11
+ uv_timer_t timer_req;
12
+ time_t cleanup_after; // Start a cleanup after this timestamp
13
+ uv_async_t async;
14
+ /* FIFO command queue */
15
+ uv_mutex_t cmd_mutex;
16
+ uv_cond_t cmd_cond;
17
+ bool initialized;
18
+ volatile unsigned queue_size;
19
+ struct aclk_database_cmdqueue cmd_queue;
20
+} aclk_sync_config = { 0 };
21
+
22
+
23
void sanity_check(void) {
24
// make sure the compiler will stop on misconfigurations
25
BUILD_BUG_ON(WORKER_UTILIZATION_MAX_JOB_TYPES < ACLK_MAX_ENUMERATIONS_DEFINED);
26
}
27
13
-void schedule_node_info_update(RRDHOST *host)
28
+
29
+int aclk_database_enq_cmd_noblock(struct aclk_database_cmd *cmd)
30
{
15
- if (unlikely(!host))
16
- return;
31
+ unsigned queue_size;
32
18
- struct aclk_database_worker_config *wc = host->dbsync_worker;
33
+ /* wait for free space in queue */
34
+ uv_mutex_lock(&aclk_sync_config.cmd_mutex);
35
+ if ((queue_size = aclk_sync_config.queue_size) == ACLK_DATABASE_CMD_Q_MAX_SIZE) {
36
+ uv_mutex_unlock(&aclk_sync_config.cmd_mutex);
37
+ return 1;
38
+ }
39
20
- if (unlikely(!wc))
21
- return;
40
+ fatal_assert(queue_size < ACLK_DATABASE_CMD_Q_MAX_SIZE);
41
+ /* enqueue command */
42
+ aclk_sync_config.cmd_queue.cmd_array[aclk_sync_config.cmd_queue.tail] = *cmd;
43
+ aclk_sync_config.cmd_queue.tail = aclk_sync_config.cmd_queue.tail != ACLK_DATABASE_CMD_Q_MAX_SIZE - 1 ?
44
+ aclk_sync_config.cmd_queue.tail + 1 : 0;
45
+ aclk_sync_config.queue_size = queue_size + 1;
46
+ uv_mutex_unlock(&aclk_sync_config.cmd_mutex);
47
+ return 0;
48
+}
49
23
- struct aclk_database_cmd cmd;
24
- memset(&cmd, 0, sizeof(cmd));
25
- cmd.opcode = ACLK_DATABASE_NODE_STATE;
26
- cmd.completion = NULL;
27
- aclk_database_enq_cmd(wc, &cmd);
50
+static void aclk_database_enq_cmd(struct aclk_database_cmd *cmd)
51
+{
52
+ unsigned queue_size;
53
+
54
+ /* wait for free space in queue */
55
+ uv_mutex_lock(&aclk_sync_config.cmd_mutex);
56
+ while ((queue_size = aclk_sync_config.queue_size) == ACLK_DATABASE_CMD_Q_MAX_SIZE) {
57
+ uv_cond_wait(&aclk_sync_config.cmd_cond, &aclk_sync_config.cmd_mutex);
58
+ }
59
+ fatal_assert(queue_size < ACLK_DATABASE_CMD_Q_MAX_SIZE);
60
+ /* enqueue command */
61
+ aclk_sync_config.cmd_queue.cmd_array[aclk_sync_config.cmd_queue.tail] = *cmd;
62
+ aclk_sync_config.cmd_queue.tail = aclk_sync_config.cmd_queue.tail != ACLK_DATABASE_CMD_Q_MAX_SIZE - 1 ?
63
+ aclk_sync_config.cmd_queue.tail + 1 : 0;
64
+ aclk_sync_config.queue_size = queue_size + 1;
65
+ uv_mutex_unlock(&aclk_sync_config.cmd_mutex);
66
+
67
+ /* wake up event loop */
68
+ int rc = uv_async_send(&aclk_sync_config.async);
69
+ if (unlikely(rc))
70
+ debug(D_ACLK_SYNC, "Failed to wake up event loop");
71
}
72
30
-#ifdef ENABLE_ACLK
31
-static int sql_check_aclk_table(void *data, int argc, char **argv, char **column)
73
+enum {
74
+ IDX_HOST_ID,
75
+ IDX_HOSTNAME,
76
+ IDX_REGISTRY,
77
+ IDX_UPDATE_EVERY,
78
+ IDX_OS,
79
+ IDX_TIMEZONE,
80
+ IDX_TAGS,
81
+ IDX_HOPS,
82
+ IDX_MEMORY_MODE,
83
+ IDX_ABBREV_TIMEZONE,
84
+ IDX_UTC_OFFSET,
85
+ IDX_PROGRAM_NAME,
86
+ IDX_PROGRAM_VERSION,
87
+ IDX_ENTRIES,
88
+ IDX_HEALTH_ENABLED,
89
+};
90
+
91
+static int create_host_callback(void *data, int argc, char **argv, char **column)
92
{
33
- struct aclk_database_worker_config *wc = data;
93
+ UNUSED(data);
94
UNUSED(argc);
95
UNUSED(column);
96
37
- debug(D_ACLK_SYNC,"Scheduling aclk sync table check for node %s", (char *) argv[0]);
38
- struct aclk_database_cmd cmd;
39
- memset(&cmd, 0, sizeof(cmd));
40
- cmd.opcode = ACLK_DATABASE_DELETE_HOST;
41
- cmd.data = strdupz((char *) argv[0]);
42
- aclk_database_enq_cmd_noblock(wc, &cmd);
43
- return 0;
44
-}
97
+ char guid[UUID_STR_LEN];
98
+ uuid_unparse_lower(*(uuid_t *)argv[IDX_HOST_ID], guid);
99
46
-#define SQL_SELECT_ACLK_ACTIVE_LIST "SELECT REPLACE(SUBSTR(name,19),'_','-') FROM sqlite_schema " \
47
- "WHERE name LIKE 'aclk_chart_latest_%' AND type IN ('table');"
100
+ struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info));
101
+ __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(struct rrdhost_system_info), __ATOMIC_RELAXED);
102
49
-static void sql_check_aclk_table_list(struct aclk_database_worker_config *wc)
50
-{
51
- char *err_msg = NULL;
52
- debug(D_ACLK_SYNC,"Cleaning tables for nodes that do not exist");
53
- int rc = sqlite3_exec_monitored(db_meta, SQL_SELECT_ACLK_ACTIVE_LIST, sql_check_aclk_table, (void *) wc, &err_msg);
54
- if (rc != SQLITE_OK) {
55
- error_report("Query failed when trying to check for obsolete ACLK sync tables, %s", err_msg);
56
- sqlite3_free(err_msg);
57
- }
103
+ system_info->hops = str2i((const char *) argv[IDX_HOPS]);
104
+
105
+ sql_build_host_system_info((uuid_t *)argv[IDX_HOST_ID], system_info);
106
+
107
+ RRDHOST *host = rrdhost_find_or_create(
108
+ (const char *) argv[IDX_HOSTNAME]
109
+ , (const char *) argv[IDX_REGISTRY]
110
+ , guid
111
+ , (const char *) argv[IDX_OS]
112
+ , (const char *) argv[IDX_TIMEZONE]
113
+ , (const char *) argv[IDX_ABBREV_TIMEZONE]
114
+ , (int32_t) (argv[IDX_UTC_OFFSET] ? str2uint32_t(argv[IDX_UTC_OFFSET], NULL) : 0)
115
+ , (const char *) argv[IDX_TAGS]
116
+ , (const char *) (argv[IDX_PROGRAM_NAME] ? argv[IDX_PROGRAM_NAME] : "unknown")
117
+ , (const char *) (argv[IDX_PROGRAM_VERSION] ? argv[IDX_PROGRAM_VERSION] : "unknown")
118
+ , argv[IDX_UPDATE_EVERY] ? str2i(argv[IDX_UPDATE_EVERY]) : 1
119
+ , argv[IDX_ENTRIES] ? str2i(argv[IDX_ENTRIES]) : 0
120
+ , default_rrd_memory_mode
121
+ , 0 // health
122
+ , 0 // rrdpush enabled
123
+ , NULL //destination
124
+ , NULL // api key
125
+ , NULL // send charts matching
126
+ , false // rrdpush_enable_replication
127
+ , 0 // rrdpush_seconds_to_replicate
128
+ , 0 // rrdpush_replication_step
129
+ , system_info
130
+ , 1
131
+ );
132
+ if (likely(host))
133
+ host->rrdlabels = sql_load_host_labels((uuid_t *)argv[IDX_HOST_ID]);
134
+
135
+#ifdef NETDATA_INTERNAL_CHECKS
136
+ char node_str[UUID_STR_LEN] = "<none>";
137
+ if (likely(host->node_id))
138
+ uuid_unparse_lower(*host->node_id, node_str);
139
+ internal_error(true, "Adding archived host \"%s\" with GUID \"%s\" node id = \"%s\"", rrdhost_hostname(host), host->machine_guid, node_str);
140
+#endif
141
+ return 0;
142
}
143
60
-static void sql_maint_aclk_sync_database(struct aclk_database_worker_config *wc, struct aclk_database_cmd cmd)
144
+#ifdef ENABLE_ACLK
145
+static struct aclk_database_cmd aclk_database_deq_cmd(void)
146
{
62
- UNUSED(cmd);
63
-
64
- debug(D_ACLK, "Checking database for %s", wc->host_guid);
147
+ struct aclk_database_cmd ret;
148
+ unsigned queue_size;
149
66
- BUFFER *sql = buffer_create(ACLK_SYNC_QUERY_SIZE, &netdata_buffers_statistics.buffers_sqlite);
150
+ uv_mutex_lock(&aclk_sync_config.cmd_mutex);
151
+ queue_size = aclk_sync_config.queue_size;
152
+ if (queue_size == 0) {
153
+ memset(&ret, 0, sizeof(ret));
154
+ ret.opcode = ACLK_DATABASE_NOOP;
155
+ ret.completion = NULL;
156
68
- buffer_sprintf(sql,"DELETE FROM aclk_alert_%s WHERE date_submitted IS NOT NULL AND "
69
- "CAST(date_cloud_ack AS INT) < unixepoch()-%d;", wc->uuid_str, ACLK_DELETE_ACK_ALERTS_INTERNAL);
70
- db_execute(buffer_tostring(sql));
157
+ } else {
158
+ /* dequeue command */
159
+ ret = aclk_sync_config.cmd_queue.cmd_array[aclk_sync_config.cmd_queue.head];
160
+ if (queue_size == 1) {
161
+ aclk_sync_config.cmd_queue.head = aclk_sync_config.cmd_queue.tail = 0;
162
+ } else {
163
+ aclk_sync_config.cmd_queue.head = aclk_sync_config.cmd_queue.head != ACLK_DATABASE_CMD_Q_MAX_SIZE - 1 ?
164
+ aclk_sync_config.cmd_queue.head + 1 : 0;
165
+ }
166
+ aclk_sync_config.queue_size = queue_size - 1;
167
+ /* wake up producers */
168
+ uv_cond_signal(&aclk_sync_config.cmd_cond);
169
+ }
170
+ uv_mutex_unlock(&aclk_sync_config.cmd_mutex);
171
72
- buffer_free(sql);
172
+ return ret;
173
}
174
75
-
175
#define SQL_SELECT_HOST_BY_UUID "SELECT host_id FROM host WHERE host_id = @host_id;"
77
-
176
static int is_host_available(uuid_t *host_id)
177
{
178
sqlite3_stmt *res = NULL;
192
193
rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
194
if (unlikely(rc != SQLITE_OK)) {
97
- error_report("Failed to bind host_id parameter to select node instance information");
195
+ error_report("Failed to bind host_id parameter to check host existence");
196
goto failed;
197
}
198
rc = sqlite3_step_monitored(res);
205
}
206
207
// OPCODE: ACLK_DATABASE_DELETE_HOST
110
-void sql_delete_aclk_table_list(struct aclk_database_worker_config *wc, struct aclk_database_cmd cmd)
208
+static void sql_delete_aclk_table_list(char *host_guid)
209
{
112
- UNUSED(wc);
113
- char uuid_str[GUID_LEN + 1];
114
- char host_str[GUID_LEN + 1];
210
+ char uuid_str[UUID_STR_LEN];
211
+ char host_str[UUID_STR_LEN];
212
213
int rc;
214
uuid_t host_uuid;
118
- char *host_guid = (char *)cmd.data;
215
216
if (unlikely(!host_guid))
217
return;
253
if (unlikely(rc != SQLITE_OK))
254
error_report("Failed to finalize statement to clean up aclk tables, rc = %d", rc);
255
160
- db_execute(buffer_tostring(sql));
256
+ rc = db_execute(buffer_tostring(sql));
257
+ if (unlikely(rc))
258
+ error("Failed to drop unused ACLK tables");
259
260
fail:
261
buffer_free(sql);
262
}
165
-#endif
166
-
167
-uv_mutex_t aclk_async_lock;
168
-struct aclk_database_worker_config *aclk_thread_head = NULL;
169
-
170
-int claimed()
171
-{
172
- int rc;
173
- rrdhost_aclk_state_lock(localhost);
174
- rc = (localhost->aclk_state.claimed_id != NULL);
175
- rrdhost_aclk_state_unlock(localhost);
176
- return rc;
177
-}
263
179
-void aclk_add_worker_thread(struct aclk_database_worker_config *wc)
264
+static int sql_check_aclk_table(void *data __maybe_unused, int argc __maybe_unused, char **argv __maybe_unused, char **column __maybe_unused)
265
{
181
- if (unlikely(!wc))
182
- return;
183
-
184
- uv_mutex_lock(&aclk_async_lock);
185
- if (unlikely(!wc->host)) {
186
- wc->next = aclk_thread_head;
187
- aclk_thread_head = wc;
188
- }
189
- uv_mutex_unlock(&aclk_async_lock);
266
+ debug(D_ACLK_SYNC,"Scheduling aclk sync table check for node %s", (char *) argv[0]);
267
+ struct aclk_database_cmd cmd;
268
+ memset(&cmd, 0, sizeof(cmd));
269
+ cmd.opcode = ACLK_DATABASE_DELETE_HOST;
270
+ cmd.param[0] = strdupz((char *) argv[0]);
271
+ aclk_database_enq_cmd_noblock(&cmd);
272
+ return 0;
273
}
274
192
-void aclk_del_worker_thread(struct aclk_database_worker_config *wc)
193
-{
194
- if (unlikely(!wc))
195
- return;
196
-
197
- uv_mutex_lock(&aclk_async_lock);
198
- struct aclk_database_worker_config **tmp = &aclk_thread_head;
199
- while (*tmp && (*tmp) != wc)
200
- tmp = &(*tmp)->next;
201
- if (*tmp)
202
- *tmp = wc->next;
203
- uv_mutex_unlock(&aclk_async_lock);
204
-}
275
+#define SQL_SELECT_ACLK_ACTIVE_LIST "SELECT REPLACE(SUBSTR(name,19),'_','-') FROM sqlite_schema " \
276
+ "WHERE name LIKE 'aclk_chart_latest_%' AND type IN ('table');"
277
206
-int aclk_worker_thread_exists(char *guid)
278
+static void sql_check_aclk_table_list(void)
279
{
208
- int rc = 0;
209
- uv_mutex_lock(&aclk_async_lock);
210
-
211
- struct aclk_database_worker_config *tmp = aclk_thread_head;
212
-
213
- while (tmp && !rc) {
214
- rc = strcmp(tmp->uuid_str, guid) == 0;
215
- tmp = tmp->next;
280
+ char *err_msg = NULL;
281
+ debug(D_ACLK_SYNC,"Cleaning tables for nodes that do not exist");
282
+ int rc = sqlite3_exec_monitored(db_meta, SQL_SELECT_ACLK_ACTIVE_LIST, sql_check_aclk_table, NULL, &err_msg);
283
+ if (rc != SQLITE_OK) {
284
+ error_report("Query failed when trying to check for obsolete ACLK sync tables, %s", err_msg);
285
+ sqlite3_free(err_msg);
286
}
217
- uv_mutex_unlock(&aclk_async_lock);
218
- return rc;
287
}
288
221
-void aclk_database_init_cmd_queue(struct aclk_database_worker_config *wc)
222
-{
223
- wc->cmd_queue.head = wc->cmd_queue.tail = 0;
224
- wc->queue_size = 0;
225
- fatal_assert(0 == uv_cond_init(&wc->cmd_cond));
226
- fatal_assert(0 == uv_mutex_init(&wc->cmd_mutex));
227
-}
289
+#define SQL_ALERT_CLEANUP "DELETE FROM aclk_alert_%s WHERE date_submitted IS NOT NULL AND CAST(date_cloud_ack AS INT) < unixepoch()-%d;"
290
229
-int aclk_database_enq_cmd_noblock(struct aclk_database_worker_config *wc, struct aclk_database_cmd *cmd)
291
+static int sql_maint_aclk_sync_database(void *data __maybe_unused, int argc __maybe_unused, char **argv, char **column __maybe_unused)
292
{
231
- unsigned queue_size;
232
-
233
- /* wait for free space in queue */
234
- uv_mutex_lock(&wc->cmd_mutex);
235
- if ((queue_size = wc->queue_size) == ACLK_DATABASE_CMD_Q_MAX_SIZE || wc->is_shutting_down) {
236
- uv_mutex_unlock(&wc->cmd_mutex);
237
- return 1;
238
- }
239
-
240
- fatal_assert(queue_size < ACLK_DATABASE_CMD_Q_MAX_SIZE);
241
- /* enqueue command */
242
- wc->cmd_queue.cmd_array[wc->cmd_queue.tail] = *cmd;
243
- wc->cmd_queue.tail = wc->cmd_queue.tail != ACLK_DATABASE_CMD_Q_MAX_SIZE - 1 ?
244
- wc->cmd_queue.tail + 1 : 0;
245
- wc->queue_size = queue_size + 1;
246
- uv_mutex_unlock(&wc->cmd_mutex);
293
+ char sql[512];
294
+ snprintfz(sql,511, SQL_ALERT_CLEANUP, (char *) argv[0], ACLK_DELETE_ACK_ALERTS_INTERNAL);
295
+ if (unlikely(db_execute(sql)))
296
+ error_report("Failed to clean stale ACLK alert entries");
297
return 0;
298
}
299
250
-void aclk_database_enq_cmd(struct aclk_database_worker_config *wc, struct aclk_database_cmd *cmd)
251
-{
252
- unsigned queue_size;
253
-
254
- /* wait for free space in queue */
255
- uv_mutex_lock(&wc->cmd_mutex);
256
- if (wc->is_shutting_down) {
257
- uv_mutex_unlock(&wc->cmd_mutex);
258
- return;
259
- }
260
- while ((queue_size = wc->queue_size) == ACLK_DATABASE_CMD_Q_MAX_SIZE) {
261
- uv_cond_wait(&wc->cmd_cond, &wc->cmd_mutex);
262
- }
263
- fatal_assert(queue_size < ACLK_DATABASE_CMD_Q_MAX_SIZE);
264
- /* enqueue command */
265
- wc->cmd_queue.cmd_array[wc->cmd_queue.tail] = *cmd;
266
- wc->cmd_queue.tail = wc->cmd_queue.tail != ACLK_DATABASE_CMD_Q_MAX_SIZE - 1 ?
267
- wc->cmd_queue.tail + 1 : 0;
268
- wc->queue_size = queue_size + 1;
269
- uv_mutex_unlock(&wc->cmd_mutex);
270
-
271
- /* wake up event loop */
272
- int rc = uv_async_send(&wc->async);
273
- if (unlikely(rc))
274
- debug(D_ACLK_SYNC, "Failed to wake up event loop");
275
-}
276
-
277
-struct aclk_database_cmd aclk_database_deq_cmd(struct aclk_database_worker_config* wc)
278
-{
279
- struct aclk_database_cmd ret;
280
- unsigned queue_size;
281
-
282
- uv_mutex_lock(&wc->cmd_mutex);
283
- queue_size = wc->queue_size;
284
- if (queue_size == 0 || wc->is_shutting_down) {
285
- memset(&ret, 0, sizeof(ret));
286
- ret.opcode = ACLK_DATABASE_NOOP;
287
- ret.completion = NULL;
288
- if (wc->is_shutting_down)
289
- uv_cond_signal(&wc->cmd_cond);
290
- } else {
291
- /* dequeue command */
292
- ret = wc->cmd_queue.cmd_array[wc->cmd_queue.head];
293
- if (queue_size == 1) {
294
- wc->cmd_queue.head = wc->cmd_queue.tail = 0;
295
- } else {
296
- wc->cmd_queue.head = wc->cmd_queue.head != ACLK_DATABASE_CMD_Q_MAX_SIZE - 1 ?
297
- wc->cmd_queue.head + 1 : 0;
298
- }
299
- wc->queue_size = queue_size - 1;
300
- /* wake up producers */
301
- uv_cond_signal(&wc->cmd_cond);
302
- }
303
- uv_mutex_unlock(&wc->cmd_mutex);
300
305
- return ret;
306
-}
301
+#define SQL_SELECT_ACLK_ALERT_LIST "SELECT SUBSTR(name,12) FROM sqlite_schema WHERE name LIKE 'aclk_alert_%' AND type IN ('table');"
302
308
-struct aclk_database_worker_config *find_inactive_wc_by_node_id(char *node_id)
303
+static void sql_maint_aclk_sync_database_all(void)
304
{
310
- if (unlikely(!node_id))
311
- return NULL;
312
-
313
- uv_mutex_lock(&aclk_async_lock);
314
- struct aclk_database_worker_config *wc = aclk_thread_head;
315
-
316
- while (wc) {
317
- if (!strcmp(wc->node_id, node_id))
318
- break;
319
- wc = wc->next;
305
+ char *err_msg = NULL;
306
+ debug(D_ACLK_SYNC,"Cleaning tables for nodes that do not exist");
307
+ int rc = sqlite3_exec_monitored(db_meta, SQL_SELECT_ACLK_ALERT_LIST, sql_maint_aclk_sync_database, NULL, &err_msg);
308
+ if (rc != SQLITE_OK) {
309
+ error_report("Query failed when trying to check for obsolete ACLK sync tables, %s", err_msg);
310
+ sqlite3_free(err_msg);
311
}
321
- uv_mutex_unlock(&aclk_async_lock);
322
-
323
- return (wc);
312
}
313
326
-void aclk_sync_exit_all()
327
-{
328
- rrd_rdlock();
329
- RRDHOST *host;
330
- rrdhost_foreach_read(host) {
331
- struct aclk_database_worker_config *wc = host->dbsync_worker;
332
- if (wc) {
333
- wc->is_shutting_down = 1;
334
- (void) aclk_database_deq_cmd(wc);
335
- uv_cond_signal(&wc->cmd_cond);
336
- }
337
- }
338
- rrd_unlock();
339
-
340
- uv_mutex_lock(&aclk_async_lock);
341
- struct aclk_database_worker_config *wc = aclk_thread_head;
342
- while (wc) {
343
- wc->is_shutting_down = 1;
344
- wc = wc->next;
345
- }
346
- uv_mutex_unlock(&aclk_async_lock);
347
-}
348
-
349
-enum {
350
- IDX_HOST_ID,
351
- IDX_HOSTNAME,
352
- IDX_REGISTRY,
353
- IDX_UPDATE_EVERY,
354
- IDX_OS,
355
- IDX_TIMEZONE,
356
- IDX_TAGS,
357
- IDX_HOPS,
358
- IDX_MEMORY_MODE,
359
- IDX_ABBREV_TIMEZONE,
360
- IDX_UTC_OFFSET,
361
- IDX_PROGRAM_NAME,
362
- IDX_PROGRAM_VERSION,
363
- IDX_ENTRIES,
364
- IDX_HEALTH_ENABLED,
365
-};
366
-
367
-static int create_host_callback(void *data, int argc, char **argv, char **column)
368
-{
369
- UNUSED(data);
370
- UNUSED(argc);
371
- UNUSED(column);
372
-
373
- char guid[UUID_STR_LEN];
374
- uuid_unparse_lower(*(uuid_t *)argv[IDX_HOST_ID], guid);
375
-
376
- struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info));
377
- __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(struct rrdhost_system_info), __ATOMIC_RELAXED);
378
-
379
- system_info->hops = str2i((const char *) argv[IDX_HOPS]);
380
-
381
- sql_build_host_system_info((uuid_t *)argv[IDX_HOST_ID], system_info);
382
-
383
- RRDHOST *host = rrdhost_find_or_create(
384
- (const char *) argv[IDX_HOSTNAME]
385
- , (const char *) argv[IDX_REGISTRY]
386
- , guid
387
- , (const char *) argv[IDX_OS]
388
- , (const char *) argv[IDX_TIMEZONE]
389
- , (const char *) argv[IDX_ABBREV_TIMEZONE]
390
- , argv[IDX_UTC_OFFSET] ? str2uint32_t(argv[IDX_UTC_OFFSET], NULL) : 0
391
- , (const char *) argv[IDX_TAGS]
392
- , (const char *) (argv[IDX_PROGRAM_NAME] ? argv[IDX_PROGRAM_NAME] : "unknown")
393
- , (const char *) (argv[IDX_PROGRAM_VERSION] ? argv[IDX_PROGRAM_VERSION] : "unknown")
394
- , argv[3] ? str2i(argv[IDX_UPDATE_EVERY]) : 1
395
- , argv[13] ? str2i(argv[IDX_ENTRIES]) : 0
396
- , default_rrd_memory_mode
397
- , 0 // health
398
- , 0 // rrdpush enabled
399
- , NULL //destination
400
- , NULL // api key
401
- , NULL // send charts matching
402
- , false // rrdpush_enable_replication
403
- , 0 // rrdpush_seconds_to_replicate
404
- , 0 // rrdpush_replication_step
405
- , system_info
406
- , 1
407
- );
408
- if (likely(host))
409
- host->rrdlabels = sql_load_host_labels((uuid_t *)argv[IDX_HOST_ID]);
410
-
411
-#ifdef NETDATA_INTERNAL_CHECKS
412
- char node_str[UUID_STR_LEN] = "<none>";
413
- if (likely(host->node_id))
414
- uuid_unparse_lower(*host->node_id, node_str);
415
- internal_error(true, "Adding archived host \"%s\" with GUID \"%s\" node id = \"%s\"", rrdhost_hostname(host), host->machine_guid, node_str);
416
-#endif
417
- return 0;
418
-}
419
-
420
-#ifdef ENABLE_ACLK
421
-static int aclk_start_sync_thread(void *data, int argc, char **argv, char **column)
314
+static int aclk_config_parameters(void *data __maybe_unused, int argc __maybe_unused, char **argv, char **column __maybe_unused)
315
{
316
char uuid_str[GUID_LEN + 1];
424
- UNUSED(data);
425
- UNUSED(argc);
426
- UNUSED(column);
427
-
317
uuid_unparse_lower(*((uuid_t *) argv[0]), uuid_str);
318
319
RRDHOST *host = rrdhost_find_by_guid(uuid_str);
323
sql_create_aclk_table(host, (uuid_t *) argv[0], (uuid_t *) argv[1]);
324
return 0;
325
}
437
-#endif
438
-void sql_aclk_sync_init(void)
439
-{
440
- char *err_msg = NULL;
441
- int rc;
442
-
443
- if (unlikely(!db_meta)) {
444
- if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) {
445
- return;
446
- }
447
- error_report("Database has not been initialized");
448
- return;
449
- }
450
-
451
- info("Creating archived hosts");
452
- rc = sqlite3_exec_monitored(db_meta, "SELECT host_id, hostname, registry_hostname, update_every, os, "
453
- "timezone, tags, hops, memory_mode, abbrev_timezone, utc_offset, program_name, "
454
- "program_version, entries, health_enabled FROM host WHERE hops >0;",
455
- create_host_callback, NULL, &err_msg);
456
- if (rc != SQLITE_OK) {
457
- error_report("SQLite error when loading archived hosts, rc = %d (%s)", rc, err_msg);
458
- sqlite3_free(err_msg);
459
- }
326
461
-#ifdef ENABLE_ACLK
462
- fatal_assert(0 == uv_mutex_init(&aclk_async_lock));
463
- rc = sqlite3_exec_monitored(db_meta, "SELECT ni.host_id, ni.node_id FROM host h, node_instance ni WHERE "
464
- "h.host_id = ni.host_id AND ni.node_id IS NOT NULL;", aclk_start_sync_thread, NULL, &err_msg);
465
- if (rc != SQLITE_OK) {
466
- error_report("SQLite error when starting ACLK sync threads, rc = %d (%s)", rc, err_msg);
467
- sqlite3_free(err_msg);
468
- }
469
- info("ACLK sync initialization completed");
470
-#endif
471
-}
472
-#ifdef ENABLE_ACLK
327
static void async_cb(uv_async_t *handle)
328
{
329
uv_stop(handle->loop);
330
uv_update_time(handle->loop);
477
- debug(D_ACLK_SYNC, "%s called, active=%d.", __func__, uv_is_active((uv_handle_t *)handle));
331
}
332
333
#define TIMER_PERIOD_MS (1000)
334
482
-static void timer_cb(uv_timer_t* handle)
335
+static void timer_cb(uv_timer_t *handle)
336
{
337
uv_stop(handle->loop);
338
uv_update_time(handle->loop);
339
487
- struct aclk_database_worker_config *wc = handle->data;
340
+ struct aclk_sync_config_s *config = handle->data;
341
struct aclk_database_cmd cmd;
342
memset(&cmd, 0, sizeof(cmd));
490
- cmd.opcode = ACLK_DATABASE_TIMER;
491
- aclk_database_enq_cmd_noblock(wc, &cmd);
343
344
time_t now = now_realtime_sec();
345
495
- if (wc->cleanup_after && wc->cleanup_after < now) {
346
+ if (config->cleanup_after && config->cleanup_after < now) {
347
cmd.opcode = ACLK_DATABASE_CLEANUP;
497
- if (!aclk_database_enq_cmd_noblock(wc, &cmd))
498
- wc->cleanup_after += ACLK_DATABASE_CLEANUP_INTERVAL;
348
+ if (!aclk_database_enq_cmd_noblock(&cmd))
349
+ config->cleanup_after += ACLK_DATABASE_CLEANUP_INTERVAL;
350
}
351
352
if (aclk_connected) {
502
- if (wc->alert_updates && !wc->pause_alert_updates) {
503
- cmd.opcode = ACLK_DATABASE_PUSH_ALERT;
504
- cmd.count = ACLK_MAX_ALERT_UPDATES;
505
- aclk_database_enq_cmd_noblock(wc, &cmd);
506
- }
353
+ cmd.opcode = ACLK_DATABASE_PUSH_ALERT;
354
+ aclk_database_enq_cmd_noblock(&cmd);
355
+
356
+ aclk_check_node_info_and_collectors();
357
}
358
}
359
510
-static void aclk_database_worker(void *arg)
360
+static void aclk_synchronization(void *arg __maybe_unused)
361
{
512
- service_register(SERVICE_THREAD_TYPE_EVENT_LOOP, NULL, NULL, NULL, true);
362
+ struct aclk_sync_config_s *config = arg;
363
+ uv_thread_set_name_np(config->thread, "ACLKSYNC");
364
worker_register("ACLKSYNC");
365
+ service_register(SERVICE_THREAD_TYPE_EVENT_LOOP, NULL, NULL, NULL, true);
366
+
367
worker_register_job_name(ACLK_DATABASE_NOOP, "noop");
515
- worker_register_job_name(ACLK_DATABASE_ORPHAN_HOST, "node orphan");
368
worker_register_job_name(ACLK_DATABASE_ALARM_HEALTH_LOG, "alert log");
369
worker_register_job_name(ACLK_DATABASE_CLEANUP, "cleanup");
370
worker_register_job_name(ACLK_DATABASE_DELETE_HOST, "node delete");
519
- worker_register_job_name(ACLK_DATABASE_NODE_INFO, "node info");
371
worker_register_job_name(ACLK_DATABASE_NODE_STATE, "node state");
521
- worker_register_job_name(ACLK_DATABASE_NODE_COLLECTORS, "node collectors");
372
worker_register_job_name(ACLK_DATABASE_PUSH_ALERT, "alert push");
373
worker_register_job_name(ACLK_DATABASE_PUSH_ALERT_CONFIG, "alert conf push");
374
worker_register_job_name(ACLK_DATABASE_PUSH_ALERT_SNAPSHOT, "alert snapshot");
375
worker_register_job_name(ACLK_DATABASE_QUEUE_REMOVED_ALERTS, "alerts check");
376
worker_register_job_name(ACLK_DATABASE_TIMER, "timer");
377
528
- struct aclk_database_worker_config *wc = arg;
529
- uv_loop_t *loop;
530
- int ret;
531
- enum aclk_database_opcode opcode;
532
- uv_timer_t timer_req;
533
- struct aclk_database_cmd cmd;
534
-
535
- char threadname[NETDATA_THREAD_NAME_MAX+1];
536
- if (wc->host)
537
- snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "ACLK[%s]", rrdhost_hostname(wc->host));
538
- else {
539
- snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "ACLK[%s]", wc->uuid_str);
540
- threadname[11] = '\0';
541
- }
542
- uv_thread_set_name_np(wc->thread, threadname);
543
-
544
- loop = wc->loop = mallocz(sizeof(uv_loop_t));
545
- ret = uv_loop_init(loop);
546
- if (ret) {
547
- error("uv_loop_init(): %s", uv_strerror(ret));
548
- goto error_after_loop_init;
549
- }
550
- loop->data = wc;
551
-
552
- ret = uv_async_init(wc->loop, &wc->async, async_cb);
553
- if (ret) {
554
- error("uv_async_init(): %s", uv_strerror(ret));
555
- goto error_after_async_init;
556
- }
557
- wc->async.data = wc;
558
-
559
- ret = uv_timer_init(loop, &timer_req);
560
- if (ret) {
561
- error("uv_timer_init(): %s", uv_strerror(ret));
562
- goto error_after_timer_init;
563
- }
564
- timer_req.data = wc;
565
- fatal_assert(0 == uv_timer_start(&timer_req, timer_cb, TIMER_PERIOD_MS, TIMER_PERIOD_MS));
566
-
567
- wc->node_info_send = 1;
568
- info("Starting ACLK sync thread for host %s -- scratch area %lu bytes", wc->host_guid, (unsigned long int) sizeof(*wc));
378
+ uv_loop_t *loop = &config->loop;
379
+ fatal_assert(0 == uv_loop_init(loop));
380
+ fatal_assert(0 == uv_async_init(loop, &config->async, async_cb));
381
570
- memset(&cmd, 0, sizeof(cmd));
382
+ fatal_assert(0 == uv_timer_init(loop, &config->timer_req));
383
+ config->timer_req.data = config;
384
+ fatal_assert(0 == uv_timer_start(&config->timer_req, timer_cb, TIMER_PERIOD_MS, TIMER_PERIOD_MS));
385
572
- wc->startup_time = now_realtime_sec();
573
- wc->cleanup_after = wc->startup_time + ACLK_DATABASE_CLEANUP_FIRST;
386
+ info("Starting ACLK synchronization thread");
387
575
- debug(D_ACLK_SYNC,"Node %s reports pending message count = %u", wc->node_id, wc->chart_payload_count);
388
+ config->cleanup_after = now_realtime_sec() + ACLK_DATABASE_CLEANUP_FIRST;
389
+ config->initialized = true;
390
577
- while (likely(!netdata_exit)) {
391
+ while (likely(service_running(SERVICE_ACLKSYNC))) {
392
+ enum aclk_database_opcode opcode;
393
worker_is_idle();
394
uv_run(loop, UV_RUN_DEFAULT);
395
396
/* wait for commands */
397
do {
583
- cmd = aclk_database_deq_cmd(wc);
398
+ struct aclk_database_cmd cmd = aclk_database_deq_cmd();
399
585
- if (netdata_exit)
400
+ if (unlikely(!service_running(SERVICE_ACLKSYNC)))
401
break;
402
403
opcode = cmd.opcode;
409
case ACLK_DATABASE_NOOP:
410
/* the command queue was empty, do nothing */
411
break;
597
-
412
// MAINTENANCE
413
case ACLK_DATABASE_CLEANUP:
600
- debug(D_ACLK_SYNC, "Database cleanup for %s", wc->host_guid);
601
-
602
- if (wc->startup_time + ACLK_DATABASE_CLEANUP_FIRST + 2 < now_realtime_sec() && claimed() && aclk_connected) {
603
- cmd.opcode = ACLK_DATABASE_NODE_INFO;
604
- cmd.completion = NULL;
605
- (void) aclk_database_enq_cmd_noblock(wc, &cmd);
606
- }
607
-
608
- sql_maint_aclk_sync_database(wc, cmd);
609
- if (wc->host == localhost)
610
- sql_check_aclk_table_list(wc);
414
+ // Scan all aclk_alert_ tables and cleanup as needed
415
+ sql_maint_aclk_sync_database_all();
416
+ sql_check_aclk_table_list();
417
break;
418
419
case ACLK_DATABASE_DELETE_HOST:
614
- debug(D_ACLK_SYNC,"Cleaning ACLK tables for %s", (char *) cmd.data);
615
- sql_delete_aclk_table_list(wc, cmd);
420
+ sql_delete_aclk_table_list(cmd.param[0]);
421
+ break;
422
+// NODE STATE
423
+ case ACLK_DATABASE_NODE_STATE:;
424
+ RRDHOST *host = cmd.param[0];
425
+ int live = (host == localhost || host->receiver || !(rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN))) ? 1 : 0;
426
+ struct aclk_sync_host_config *ahc = host->aclk_sync_host_config;
427
+ if (unlikely(!ahc))
428
+ sql_create_aclk_table(host, &host->host_uuid, host->node_id);
429
+ aclk_host_state_update(host, live);
430
break;
617
-
431
// ALERTS
432
case ACLK_DATABASE_PUSH_ALERT_CONFIG:
620
- debug(D_ACLK_SYNC,"Pushing chart config info to the cloud for %s", wc->host_guid);
621
- aclk_push_alert_config_event(wc, cmd);
433
+ aclk_push_alert_config_event(cmd.param[0], cmd.param[1]);
434
break;
435
case ACLK_DATABASE_PUSH_ALERT:
624
- debug(D_ACLK_SYNC, "Pushing alert info to the cloud for %s", wc->host_guid);
625
- aclk_push_alert_event(wc, cmd);
436
+ aclk_push_alert_events_for_all_hosts();
437
break;
438
case ACLK_DATABASE_ALARM_HEALTH_LOG:
628
- debug(D_ACLK_SYNC, "Pushing alarm health log to the cloud for %s", wc->host_guid);
629
- aclk_push_alarm_health_log(wc, cmd);
439
+ aclk_push_alarm_health_log(cmd.param[0]);
440
break;
631
- case ACLK_DATABASE_PUSH_ALERT_SNAPSHOT:
632
- debug(D_ACLK_SYNC, "Pushing alert snapshot to the cloud for node %s", wc->host_guid);
633
- aclk_push_alert_snapshot_event(wc, cmd);
441
+ case ACLK_DATABASE_PUSH_ALERT_SNAPSHOT:;
442
+ aclk_push_alert_snapshot_event(cmd.param[0]);
443
break;
444
case ACLK_DATABASE_QUEUE_REMOVED_ALERTS:
636
- debug(D_ACLK_SYNC, "Queueing removed alerts for node %s", wc->host_guid);
637
- sql_process_queue_removed_alerts_to_aclk(wc, cmd);
638
- break;
639
-// NODE OPERATIONS
640
- case ACLK_DATABASE_NODE_STATE:
641
- debug(D_ACLK_SYNC,"Sending state update for %s", wc->uuid_str);
642
- if (wc->host) {
643
- RRDHOST *host = wc->host;
644
- int live = (host == localhost || host->receiver || !(rrdhost_flag_check(host, RRDHOST_FLAG_ORPHAN))) ? 1 : 0;
645
- aclk_host_state_update(wc->host, live);
646
- }
647
- break;
648
- case ACLK_DATABASE_NODE_INFO:
649
- debug(D_ACLK_SYNC,"Sending node info for %s", wc->uuid_str);
650
- sql_build_node_info(wc, cmd);
651
- break;
652
- case ACLK_DATABASE_NODE_COLLECTORS:
653
- debug(D_ACLK_SYNC,"Sending node collectors info for %s", wc->uuid_str);
654
- sql_build_node_collectors(wc);
655
- break;
656
-// NODE_INSTANCE DETECTION
657
- case ACLK_DATABASE_ORPHAN_HOST:
658
- wc->host = NULL;
659
- wc->is_orphan = 1;
660
- aclk_add_worker_thread(wc);
661
- break;
662
- case ACLK_DATABASE_TIMER:
663
- if (unlikely(localhost && !wc->host && !wc->is_orphan)) {
664
- if (claimed()) {
665
- wc->host = rrdhost_find_by_guid(wc->host_guid);
666
- if (wc->host) {
667
- info("HOST %s (%s) detected as active", rrdhost_hostname(wc->host), wc->host_guid);
668
- snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "ACLK[%s]", rrdhost_hostname(wc->host));
669
- uv_thread_set_name_np(wc->thread, threadname);
670
- wc->host->dbsync_worker = wc;
671
- if (unlikely(!wc->hostname))
672
- wc->hostname = strdupz(rrdhost_hostname(wc->host));
673
- aclk_del_worker_thread(wc);
674
- wc->node_info_send = 1;
675
- }
676
- }
677
- }
678
- if (wc->node_info_send && localhost && claimed() && aclk_connected) {
679
- cmd.opcode = ACLK_DATABASE_NODE_INFO;
680
- cmd.completion = NULL;
681
- wc->node_info_send = aclk_database_enq_cmd_noblock(wc, &cmd);
682
- }
683
- if (wc->node_collectors_send && wc->node_collectors_send + 30 < now_realtime_sec()) {
684
- cmd.opcode = ACLK_DATABASE_NODE_COLLECTORS;
685
- cmd.completion = NULL;
686
- wc->node_collectors_send = aclk_database_enq_cmd_noblock(wc, &cmd);
687
- }
688
- if (localhost == wc->host)
689
- (void) sqlite3_wal_checkpoint(db_meta, NULL);
445
+ sql_process_queue_removed_alerts_to_aclk(cmd.param[0]);
446
break;
447
default:
448
debug(D_ACLK_SYNC, "%s: default.", __func__);
449
break;
450
}
451
if (cmd.completion)
696
- aclk_complete(cmd.completion);
452
+ completion_mark_complete(cmd.completion);
453
} while (opcode != ACLK_DATABASE_NOOP);
454
}
455
700
- if (!uv_timer_stop(&timer_req))
701
- uv_close((uv_handle_t *)&timer_req, NULL);
702
-
703
- /* cleanup operations of the event loop */
704
- //info("Shutting down ACLK sync event loop for %s", wc->host_guid);
705
-
706
- /*
707
- * uv_async_send after uv_close does not seem to crash in linux at the moment,
708
- * it is however undocumented behaviour we need to be aware if this becomes
709
- * an issue in the future.
710
- */
711
- uv_close((uv_handle_t *)&wc->async, NULL);
712
- uv_run(loop, UV_RUN_DEFAULT);
713
-
714
- info("Shutting down ACLK sync event loop complete for host %s", wc->host_guid);
715
- /* TODO: don't let the API block by waiting to enqueue commands */
716
- uv_cond_destroy(&wc->cmd_cond);
456
+ if (!uv_timer_stop(&config->timer_req))
457
+ uv_close((uv_handle_t *)&config->timer_req, NULL);
458
718
- int rc;
719
- do {
720
- rc = uv_loop_close(loop);
721
- } while (rc != UV_EBUSY);
459
+ uv_close((uv_handle_t *)&config->async, NULL);
460
+// uv_close((uv_handle_t *)&config->async_exit, NULL);
461
+ uv_cond_destroy(&config->cmd_cond);
462
+ (void) uv_loop_close(loop);
463
723
- freez(loop);
464
+ worker_unregister();
465
+ service_exits();
466
+ info("ACLK SYNC: Shutting down ACLK synchronization event loop");
467
+}
468
725
- rrd_rdlock();
726
- if (likely(wc->host))
727
- wc->host->dbsync_worker = NULL;
728
- freez(wc->hostname);
729
- freez(wc);
730
- rrd_unlock();
469
+static void aclk_synchronization_init(void)
470
+{
471
+ aclk_sync_config.cmd_queue.head = aclk_sync_config.cmd_queue.tail = 0;
472
+ aclk_sync_config.queue_size = 0;
473
+ fatal_assert(0 == uv_cond_init(&aclk_sync_config.cmd_cond));
474
+ fatal_assert(0 == uv_mutex_init(&aclk_sync_config.cmd_mutex));
475
732
- worker_unregister();
733
- return;
734
-
735
-error_after_timer_init:
736
- uv_close((uv_handle_t *)&wc->async, NULL);
737
-error_after_async_init:
738
- fatal_assert(0 == uv_loop_close(loop));
739
-error_after_loop_init:
740
- freez(loop);
741
- worker_unregister();
476
+ fatal_assert(0 == uv_thread_create(&aclk_sync_config.thread, aclk_synchronization, &aclk_sync_config));
477
}
478
#endif
479
480
// -------------------------------------------------------------
481
747
-void sql_create_aclk_table(RRDHOST *host, uuid_t *host_uuid, uuid_t *node_id)
482
+void sql_create_aclk_table(RRDHOST *host __maybe_unused, uuid_t *host_uuid __maybe_unused, uuid_t *node_id __maybe_unused)
483
{
484
#ifdef ENABLE_ACLK
485
char uuid_str[GUID_LEN + 1];
486
char host_guid[GUID_LEN + 1];
487
+ int rc;
488
489
uuid_unparse_lower_fix(host_uuid, uuid_str);
754
-
755
- if (aclk_worker_thread_exists(uuid_str))
756
- return;
757
-
490
uuid_unparse_lower(*host_uuid, host_guid);
491
760
- BUFFER *sql = buffer_create(ACLK_SYNC_QUERY_SIZE, &netdata_buffers_statistics.buffers_sqlite);
761
-
762
- buffer_sprintf(sql, TABLE_ACLK_ALERT, uuid_str);
763
- db_execute(buffer_tostring(sql));
764
- buffer_flush(sql);
765
-
766
- buffer_sprintf(sql, INDEX_ACLK_ALERT, uuid_str, uuid_str);
767
- db_execute(buffer_tostring(sql));
492
+ char sql[ACLK_SYNC_QUERY_SIZE];
493
769
- buffer_free(sql);
494
+ snprintfz(sql, ACLK_SYNC_QUERY_SIZE-1, TABLE_ACLK_ALERT, uuid_str);
495
+ rc = db_execute(sql);
496
+ if (unlikely(rc))
497
+ error_report("Failed to create ACLK alert table for host %s", host ? rrdhost_hostname(host) : host_guid);
498
+ else {
499
+ snprintfz(sql, ACLK_SYNC_QUERY_SIZE -1, INDEX_ACLK_ALERT, uuid_str, uuid_str);
500
+ rc = db_execute(sql);
501
+ if (unlikely(rc))
502
+ error_report("Failed to create ACLK alert table index for host %s", host ? string2str(host->hostname) : host_guid);
503
+ }
504
+ if (likely(host) && unlikely(host->aclk_sync_host_config))
505
+ return;
506
771
- if (likely(host) && unlikely(host->dbsync_worker))
507
+ if (unlikely(!host))
508
return;
509
774
- struct aclk_database_worker_config *wc = callocz(1, sizeof(struct aclk_database_worker_config));
510
+ struct aclk_sync_host_config *wc = callocz(1, sizeof(struct aclk_sync_host_config));
511
if (node_id && !uuid_is_null(*node_id))
512
uuid_unparse_lower(*node_id, wc->node_id);
777
- if (likely(host)) {
778
- host->dbsync_worker = (void *)wc;
779
- wc->hostname = strdupz(rrdhost_hostname(host));
780
- if (node_id && !host->node_id) {
781
- host->node_id = mallocz(sizeof(*host->node_id));
782
- uuid_copy(*host->node_id, *node_id);
783
- }
513
+
514
+ host->aclk_sync_host_config = (void *)wc;
515
+ if (node_id && !host->node_id) {
516
+ host->node_id = mallocz(sizeof(*host->node_id));
517
+ uuid_copy(*host->node_id, *node_id);
518
}
785
- else
786
- wc->hostname = get_hostname_by_node_id(wc->node_id);
519
+
520
wc->host = host;
521
strcpy(wc->uuid_str, uuid_str);
789
- strcpy(wc->host_guid, host_guid);
522
wc->alert_updates = 0;
791
- aclk_database_init_cmd_queue(wc);
792
- aclk_add_worker_thread(wc);
793
- fatal_assert(0 == uv_thread_create(&(wc->thread), aclk_database_worker, wc));
794
-#else
795
- UNUSED(host);
796
- UNUSED(host_uuid);
797
- UNUSED(node_id);
523
+ wc->node_info_send = 1;
524
#endif
799
-}
\ No newline at end of file
525
+}
526
+
527
+#define SQL_FETCH_ALL_HOSTS "SELECT host_id, hostname, registry_hostname, update_every, os, " \
528
+ "timezone, tags, hops, memory_mode, abbrev_timezone, utc_offset, program_name, " \
529
+ "program_version, entries, health_enabled FROM host WHERE hops >0;"
530
+
531
+#define SQL_FETCH_ALL_INSTANCES "SELECT ni.host_id, ni.node_id FROM host h, node_instance ni " \
532
+ "WHERE h.host_id = ni.host_id AND ni.node_id IS NOT NULL; "
533
+void sql_aclk_sync_init(void)
534
+{
535
+ char *err_msg = NULL;
536
+ int rc;
537
+
538
+ if (unlikely(!db_meta)) {
539
+ if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) {
540
+ return;
541
+ }
542
+ error_report("Database has not been initialized");
543
+ return;
544
+ }
545
+
546
+ info("Creating archived hosts");
547
+ rc = sqlite3_exec_monitored(db_meta, SQL_FETCH_ALL_HOSTS, create_host_callback, NULL, &err_msg);
548
+
549
+ if (rc != SQLITE_OK) {
550
+ error_report("SQLite error when loading archived hosts, rc = %d (%s)", rc, err_msg);
551
+ sqlite3_free(err_msg);
552
+ }
553
+ // Trigger host context load for hosts that have been created
554
+ metadata_queue_load_host_context(NULL);
555
+
556
+#ifdef ENABLE_ACLK
557
+ rc = sqlite3_exec_monitored(db_meta, SQL_FETCH_ALL_INSTANCES,aclk_config_parameters, NULL,&err_msg);
558
+
559
+ if (rc != SQLITE_OK) {
560
+ error_report("SQLite error when configuring host ACLK synchonization parameters, rc = %d (%s)", rc, err_msg);
561
+ sqlite3_free(err_msg);
562
+ }
563
+ aclk_synchronization_init();
564
+
565
+ info("ACLK sync initialization completed");
566
+#endif
567
+}
568
+
569
+// Public
570
+
571
+static inline void queue_aclk_sync_cmd(enum aclk_database_opcode opcode, const void *param0, const void *param1)
572
+{
573
+ struct aclk_database_cmd cmd;
574
+ cmd.opcode = opcode;
575
+ cmd.param[0] = (void *) param0;
576
+ cmd.param[1] = (void *) param1;
577
+ cmd.completion = NULL;
578
+ aclk_database_enq_cmd(&cmd);
579
+}
580
+
581
+// Public
582
+void aclk_push_alert_config(const char *node_id, const char *config_hash)
583
+{
584
+ if (unlikely(!aclk_sync_config.initialized))
585
+ return;
586
+
587
+ queue_aclk_sync_cmd(ACLK_DATABASE_PUSH_ALERT_CONFIG, node_id, config_hash);
588
+}
589
+
590
+void aclk_push_node_alert_snapshot(const char *node_id)
591
+{
592
+ if (unlikely(!aclk_sync_config.initialized))
593
+ return;
594
+
595
+ queue_aclk_sync_cmd(ACLK_DATABASE_PUSH_ALERT_SNAPSHOT, strdupz(node_id), NULL);
596
+}
597
+
598
+
599
+void aclk_push_node_health_log(const char *node_id)
600
+{
601
+ if (unlikely(!aclk_sync_config.initialized))
602
+ return;
603
+
604
+ queue_aclk_sync_cmd(ACLK_DATABASE_ALARM_HEALTH_LOG, strdupz(node_id), NULL);
605
+}
606
+
607
+void aclk_push_node_removed_alerts(const char *node_id)
608
+{
609
+ if (unlikely(!aclk_sync_config.initialized))
610
+ return;
611
+
612
+ queue_aclk_sync_cmd(ACLK_DATABASE_QUEUE_REMOVED_ALERTS, strdupz(node_id), NULL);
613
+}
614
+
615
+void schedule_node_info_update(RRDHOST *host __maybe_unused)
616
+{
617
+#ifdef ENABLE_ACLK
618
+ if (unlikely(!host))
619
+ return;
620
+
621
+ struct aclk_database_cmd cmd;
622
+ memset(&cmd, 0, sizeof(cmd));
623
+ cmd.opcode = ACLK_DATABASE_NODE_STATE;
624
+ cmd.param[0] = host;
625
+ cmd.completion = NULL;
626
+ aclk_database_enq_cmd(&cmd);
627
+#endif
628
+}