Drop an unused index from aclk_alert table (#16242)
* Drop unused aclk_alert index * Log messages only when compiled with NETDATA_INTERNAL_CHECKS
Stelios Fragkakis committed
Oct 20, 2023 at 10:23 UTC
243c5cdfbc7e6fc87641f11833f7b5e5a1c683eb
6 files changed
+32
-24
daemon/unit_test.c
-7
@@ -1681,13 +1681,6 @@ int test_sqlite(void) {
1681
rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1682
if (rc != SQLITE_OK)
1683
goto error;
1684
- buffer_flush(sql);
1685
-
1686
- buffer_sprintf(sql, INDEX_ACLK_ALERT, uuid_str, uuid_str);
1687
- rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1688
- if (rc != SQLITE_OK)
1689
- goto error;
1690
- buffer_flush(sql);
1684
1685
buffer_free(sql);
1686
fprintf(stderr,"SQLite is OK\n");
database/sqlite/sqlite_aclk.c
-5
@@ -486,11 +486,6 @@ void sql_create_aclk_table(RRDHOST *host __maybe_unused, uuid_t *host_uuid __may
486
if (unlikely(rc))
487
error_report("Failed to create ACLK alert table for host %s", host ? rrdhost_hostname(host) : host_guid);
488
else {
489
- snprintfz(sql, ACLK_SYNC_QUERY_SIZE -1, INDEX_ACLK_ALERT, uuid_str, uuid_str);
490
- rc = db_execute(db_meta, sql);
491
- if (unlikely(rc))
492
- error_report("Failed to create ACLK alert table index for host %s", host ? string2str(host->hostname) : host_guid);
493
-
489
snprintfz(sql, ACLK_SYNC_QUERY_SIZE -1, INDEX_ACLK_ALERT1, uuid_str, uuid_str);
490
rc = db_execute(db_meta, sql);
491
if (unlikely(rc))
database/sqlite/sqlite_aclk_alert.c
-7
@@ -292,13 +292,6 @@ void aclk_push_alert_event(struct aclk_sync_host_config *wc)
292
rc = db_execute(db_meta, buffer_tostring(sql_fix));
293
if (unlikely(rc))
294
error_report("Failed to create ACLK alert table for host %s", rrdhost_hostname(wc->host));
295
-
296
- else {
297
- buffer_flush(sql_fix);
298
- buffer_sprintf(sql_fix, INDEX_ACLK_ALERT, wc->uuid_str, wc->uuid_str);
299
- if (unlikely(db_execute(db_meta, buffer_tostring(sql_fix))))
300
- error_report("Failed to create ACLK alert table for host %s", rrdhost_hostname(wc->host));
301
- }
295
buffer_free(sql_fix);
296
297
// Try again
database/sqlite/sqlite_db_migration.c
+28
-1
@@ -368,6 +368,33 @@ static int do_migration_v11_v12(sqlite3 *database)
368
return rc;
369
}
370
371
+static int do_migration_v14_v15(sqlite3 *database)
372
+{
373
+ char sql[256];
374
+
375
+ int rc;
376
+ sqlite3_stmt *res = NULL;
377
+ snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type = \"index\" AND name LIKE \"aclk_alert_index@_%%\" ESCAPE \"@\"");
378
+ rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
379
+ if (rc != SQLITE_OK) {
380
+ error_report("Failed to prepare statement to drop unused indices");
381
+ return 1;
382
+ }
383
+
384
+ BUFFER *wb = buffer_create(128, NULL);
385
+ while (sqlite3_step_monitored(res) == SQLITE_ROW)
386
+ buffer_sprintf(wb, "DROP INDEX IF EXISTS %s;", (char *) sqlite3_column_text(res, 0));
387
+
388
+ rc = sqlite3_finalize(res);
389
+ if (unlikely(rc != SQLITE_OK))
390
+ error_report("Failed to finalize statement when dropping unused indices, rc = %d", rc);
391
+
392
+ (void) db_execute(database, buffer_tostring(wb));
393
+
394
+ buffer_free(wb);
395
+ return 0;
396
+}
397
+
398
static int do_migration_v12_v13(sqlite3 *database)
399
{
400
int rc = 0;
@@ -446,7 +473,6 @@ static int migrate_database(sqlite3 *database, int target_version, char *db_name
473
}
474
}
475
return target_version;
449
-
476
}
477
478
DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
@@ -464,6 +490,7 @@ DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
490
{.name = "v11 to v12", .func = do_migration_v11_v12},
491
{.name = "v12 to v13", .func = do_migration_v12_v13},
492
{.name = "v13 to v14", .func = do_migration_v13_v14},
493
+ {.name = "v14 to v15", .func = do_migration_v14_v15},
494
// the terminator of this array
495
{.name = NULL, .func = NULL}
496
};
database/sqlite/sqlite_functions.c
+1
-1
@@ -4,7 +4,7 @@
4
#include "sqlite3recover.h"
5
#include "sqlite_db_migration.h"
6
7
-#define DB_METADATA_VERSION 14
7
+#define DB_METADATA_VERSION 15
8
9
const char *database_config[] = {
10
"CREATE TABLE IF NOT EXISTS host(host_id BLOB PRIMARY KEY, hostname TEXT NOT NULL, "
database/sqlite/sqlite_metadata.c
+3
-3
@@ -872,7 +872,7 @@ static void check_dimension_metadata(struct metadata_wc *wc)
872
next_execution_t = now + METADATA_DIM_CHECK_INTERVAL;
873
}
874
875
- netdata_log_info(
875
+ internal_error(true,
876
"METADATA: Dimensions checked %u, deleted %u. Checks will %s in %lld seconds",
877
total_checked,
878
total_deleted,
@@ -939,7 +939,7 @@ static void check_chart_metadata(struct metadata_wc *wc)
939
next_execution_t = now + METADATA_CHART_CHECK_INTERVAL;
940
}
941
942
- netdata_log_info(
942
+ internal_error(true,
943
"METADATA: Charts checked %u, deleted %u. Checks will %s in %lld seconds",
944
total_checked,
945
total_deleted,
@@ -1008,7 +1008,7 @@ static void check_label_metadata(struct metadata_wc *wc)
1008
next_execution_t = now + METADATA_LABEL_CHECK_INTERVAL;
1009
}
1010
1011
- netdata_log_info(
1011
+ internal_error(true,
1012
"METADATA: Chart labels checked %u, deleted %u. Checks will %s in %lld seconds",
1013
total_checked,
1014
total_deleted,