diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 11:19:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-07-24 09:53:24 +0000 |
commit | b5f8ee61a7f7e9bd291dd26b0585d03eb686c941 (patch) | |
tree | d4d31289c39fc00da064a825df13a0b98ce95b10 /src/database/sqlite/sqlite_aclk.c | |
parent | Adding upstream version 1.44.3. (diff) | |
download | netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.tar.xz netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.zip |
Adding upstream version 1.46.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/database/sqlite/sqlite_aclk.c (renamed from database/sqlite/sqlite_aclk.c) | 122 |
1 files changed, 52 insertions, 70 deletions
diff --git a/database/sqlite/sqlite_aclk.c b/src/database/sqlite/sqlite_aclk.c index ac574879c..8dc2231b4 100644 --- a/database/sqlite/sqlite_aclk.c +++ b/src/database/sqlite/sqlite_aclk.c @@ -21,6 +21,7 @@ void sanity_check(void) { BUILD_BUG_ON(WORKER_UTILIZATION_MAX_JOB_TYPES < ACLK_MAX_ENUMERATIONS_DEFINED); } +#ifdef ENABLE_ACLK static struct aclk_database_cmd aclk_database_deq_cmd(void) { struct aclk_database_cmd ret; @@ -40,6 +41,7 @@ static struct aclk_database_cmd aclk_database_deq_cmd(void) return ret; } +#endif static void aclk_database_enq_cmd(struct aclk_database_cmd *cmd) { @@ -61,7 +63,6 @@ enum { IDX_UPDATE_EVERY, IDX_OS, IDX_TIMEZONE, - IDX_TAGS, IDX_HOPS, IDX_MEMORY_MODE, IDX_ABBREV_TIMEZONE, @@ -72,6 +73,7 @@ enum { IDX_HEALTH_ENABLED, IDX_LAST_CONNECTED, IDX_IS_EPHEMERAL, + IDX_IS_REGISTERED, }; static int create_host_callback(void *data, int argc, char **argv, char **column) @@ -88,21 +90,27 @@ static int create_host_callback(void *data, int argc, char **argv, char **column time_t age = now_realtime_sec() - last_connected; int is_ephemeral = 0; + int is_registered = 0; if (argv[IDX_IS_EPHEMERAL]) is_ephemeral = str2i(argv[IDX_IS_EPHEMERAL]); + if (argv[IDX_IS_REGISTERED]) + is_registered = str2i(argv[IDX_IS_REGISTERED]); + char guid[UUID_STR_LEN]; - uuid_unparse_lower(*(uuid_t *)argv[IDX_HOST_ID], guid); + uuid_unparse_lower(*(nd_uuid_t *)argv[IDX_HOST_ID], guid); if (is_ephemeral && age > rrdhost_free_ephemeral_time_s) { netdata_log_info( - "Skipping ephemeral hostname \"%s\" with GUID \"%s\", age = %ld seconds (limit %ld seconds)", + "%s ephemeral hostname \"%s\" with GUID \"%s\", age = %ld seconds (limit %ld seconds)", + is_registered ? "Loading registered" : "Skipping unregistered", (const char *)argv[IDX_HOSTNAME], guid, age, rrdhost_free_ephemeral_time_s); - return 0; + if (!is_registered) + return 0; } struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info)); @@ -110,7 +118,7 @@ static int create_host_callback(void *data, int argc, char **argv, char **column system_info->hops = str2i((const char *) argv[IDX_HOPS]); - sql_build_host_system_info((uuid_t *)argv[IDX_HOST_ID], system_info); + sql_build_host_system_info((nd_uuid_t *)argv[IDX_HOST_ID], system_info); RRDHOST *host = rrdhost_find_or_create( (const char *)argv[IDX_HOSTNAME], @@ -120,7 +128,6 @@ static int create_host_callback(void *data, int argc, char **argv, char **column (const char *)argv[IDX_TIMEZONE], (const char *)argv[IDX_ABBREV_TIMEZONE], (int32_t)(argv[IDX_UTC_OFFSET] ? str2uint32_t(argv[IDX_UTC_OFFSET], NULL) : 0), - (const char *)argv[IDX_TAGS], (const char *)(argv[IDX_PROGRAM_NAME] ? argv[IDX_PROGRAM_NAME] : "unknown"), (const char *)(argv[IDX_PROGRAM_VERSION] ? argv[IDX_PROGRAM_VERSION] : "unknown"), argv[IDX_UPDATE_EVERY] ? str2i(argv[IDX_UPDATE_EVERY]) : 1, @@ -152,7 +159,7 @@ static int create_host_callback(void *data, int argc, char **argv, char **column if (is_ephemeral) host->child_disconnected_time = now_realtime_sec(); - host->rrdlabels = sql_load_host_labels((uuid_t *)argv[IDX_HOST_ID]); + host->rrdlabels = sql_load_host_labels((nd_uuid_t *)argv[IDX_HOST_ID]); host->last_connected = last_connected; } @@ -170,34 +177,26 @@ static int create_host_callback(void *data, int argc, char **argv, char **column #ifdef ENABLE_ACLK #define SQL_SELECT_HOST_BY_UUID "SELECT host_id FROM host WHERE host_id = @host_id" -static int is_host_available(uuid_t *host_id) +static int is_host_available(nd_uuid_t *host_id) { sqlite3_stmt *res = NULL; - int rc; + int rc = 0; - if (unlikely(!db_meta)) { - if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) - error_report("Database has not been initialized"); + if (!REQUIRE_DB(db_meta)) return 1; - } - rc = sqlite3_prepare_v2(db_meta, SQL_SELECT_HOST_BY_UUID, -1, &res, 0); - if (unlikely(rc != SQLITE_OK)) { - error_report("Failed to prepare statement to select node instance information for a node"); + if (!PREPARE_STATEMENT(db_meta, SQL_SELECT_HOST_BY_UUID, &res)) return 1; - } - rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC); - if (unlikely(rc != SQLITE_OK)) { - error_report("Failed to bind host_id parameter to check host existence"); - goto failed; - } - rc = sqlite3_step_monitored(res); + int param = 0; + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC)); -failed: - if (unlikely(sqlite3_finalize(res) != SQLITE_OK)) - error_report("Failed to finalize the prepared statement when checking host existence"); + param = 0; + rc = sqlite3_step_monitored(res); +done: + REPORT_BIND_FAIL(res, param); + SQLITE_FINALIZE(res); return (rc == SQLITE_ROW); } @@ -208,7 +207,7 @@ static void sql_delete_aclk_table_list(char *host_guid) char host_str[UUID_STR_LEN]; int rc; - uuid_t host_uuid; + nd_uuid_t host_uuid; if (unlikely(!host_guid)) return; @@ -230,19 +229,15 @@ static void sql_delete_aclk_table_list(char *host_guid) buffer_sprintf(sql,"SELECT 'drop '||type||' IF EXISTS '||name||';' FROM sqlite_schema " \ "WHERE name LIKE 'aclk_%%_%s' AND type IN ('table', 'trigger', 'index')", uuid_str); - rc = sqlite3_prepare_v2(db_meta, buffer_tostring(sql), -1, &res, 0); - if (rc != SQLITE_OK) { - error_report("Failed to prepare statement to clean up aclk tables"); + if (!PREPARE_STATEMENT(db_meta, buffer_tostring(sql), &res)) goto fail; - } + buffer_flush(sql); while (sqlite3_step_monitored(res) == SQLITE_ROW) buffer_strcat(sql, (char *) sqlite3_column_text(res, 0)); - rc = sqlite3_finalize(res); - if (unlikely(rc != SQLITE_OK)) - error_report("Failed to finalize statement to clean up aclk tables, rc = %d", rc); + SQLITE_FINALIZE(res); rc = db_execute(db_meta, buffer_tostring(sql)); if (unlikely(rc)) @@ -256,31 +251,24 @@ fail: static void sql_unregister_node(char *machine_guid) { int rc; - uuid_t host_uuid; + nd_uuid_t host_uuid; if (unlikely(!machine_guid)) return; rc = uuid_parse(machine_guid, host_uuid); - if (rc) { - freez(machine_guid); - return; - } + if (rc) + goto skip; sqlite3_stmt *res = NULL; - rc = sqlite3_prepare_v2(db_meta, "UPDATE node_instance SET node_id = NULL WHERE host_id = @host_id", -1, &res, 0); - if (unlikely(rc != SQLITE_OK)) { - error_report("Failed to prepare statement to remove the host node id"); - freez(machine_guid); - return; - } - - rc = sqlite3_bind_blob(res, 1, &host_uuid, sizeof(host_uuid), SQLITE_STATIC); - if (unlikely(rc != SQLITE_OK)) { - error_report("Failed to bind host_id parameter to remove host node id"); + if (!PREPARE_STATEMENT(db_meta, "UPDATE node_instance SET node_id = NULL WHERE host_id = @host_id", &res)) goto skip; - } + + int param = 0; + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host_uuid, sizeof(host_uuid), SQLITE_STATIC)); + param = 0; + rc = sqlite3_step_monitored(res); if (unlikely(rc != SQLITE_DONE)) { error_report("Failed to execute command to remove host node id"); @@ -290,9 +278,10 @@ static void sql_unregister_node(char *machine_guid) machine_guid = NULL; } +done: + REPORT_BIND_FAIL(res, param); + SQLITE_FINALIZE(res); skip: - if (unlikely(sqlite3_finalize(res) != SQLITE_OK)) - error_report("Failed to finalize statement to remove host node id"); freez(machine_guid); } @@ -346,13 +335,11 @@ static void sql_maint_aclk_sync_database_all(void) static int aclk_config_parameters(void *data __maybe_unused, int argc __maybe_unused, char **argv, char **column __maybe_unused) { char uuid_str[UUID_STR_LEN]; - uuid_unparse_lower(*((uuid_t *) argv[0]), uuid_str); + uuid_unparse_lower(*((nd_uuid_t *) argv[0]), uuid_str); RRDHOST *host = rrdhost_find_by_guid(uuid_str); - if (host == localhost) - return 0; - - sql_create_aclk_table(host, (uuid_t *) argv[0], (uuid_t *) argv[1]); + if (host != localhost) + sql_create_aclk_table(host, (nd_uuid_t *) argv[0], (nd_uuid_t *) argv[1]); return 0; } @@ -389,7 +376,7 @@ static void timer_cb(uv_timer_t *handle) static void aclk_synchronization(void *arg __maybe_unused) { struct aclk_sync_config_s *config = arg; - uv_thread_set_name_np(config->thread, "ACLKSYNC"); + uv_thread_set_name_np("ACLKSYNC"); worker_register("ACLKSYNC"); service_register(SERVICE_THREAD_TYPE_EVENT_LOOP, NULL, NULL, NULL, true); @@ -502,7 +489,7 @@ static void aclk_synchronization_init(void) // ------------------------------------------------------------- -void sql_create_aclk_table(RRDHOST *host __maybe_unused, uuid_t *host_uuid __maybe_unused, uuid_t *node_id __maybe_unused) +void sql_create_aclk_table(RRDHOST *host __maybe_unused, nd_uuid_t *host_uuid __maybe_unused, nd_uuid_t *node_id __maybe_unused) { #ifdef ENABLE_ACLK char uuid_str[UUID_STR_LEN]; @@ -557,11 +544,12 @@ void sql_create_aclk_table(RRDHOST *host __maybe_unused, uuid_t *host_uuid __may #define SQL_FETCH_ALL_HOSTS \ "SELECT host_id, hostname, registry_hostname, update_every, os, " \ - "timezone, tags, hops, memory_mode, abbrev_timezone, utc_offset, program_name, " \ + "timezone, hops, memory_mode, abbrev_timezone, utc_offset, program_name, " \ "program_version, entries, health_enabled, last_connected, " \ "(SELECT CASE WHEN hl.label_value = 'true' THEN 1 ELSE 0 END FROM " \ - "host_label hl WHERE hl.host_id = h.host_id AND hl.label_key = '_is_ephemeral') " \ - "FROM host h WHERE hops > 0" + "host_label hl WHERE hl.host_id = h.host_id AND hl.label_key = '_is_ephemeral'), " \ + "(SELECT CASE WHEN ni.node_id is NULL THEN 0 ELSE 1 END FROM " \ + "node_instance ni WHERE ni.host_id = h.host_id) FROM host h WHERE hops > 0" #define SQL_FETCH_ALL_INSTANCES \ "SELECT ni.host_id, ni.node_id FROM host h, node_instance ni " \ @@ -572,13 +560,7 @@ void sql_aclk_sync_init(void) char *err_msg = NULL; int rc; - if (unlikely(!db_meta)) { - if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) { - return; - } - error_report("Database has not been initialized"); - return; - } + REQUIRE_DB(db_meta); netdata_log_info("Creating archived hosts"); int number_of_children = 0; @@ -675,4 +657,4 @@ void unregister_node(const char *machine_guid) cmd.completion = NULL; aclk_database_enq_cmd(&cmd); } -#endif
\ No newline at end of file +#endif |