@cryptotaxi247 / netdata-1 / commits / 9396b862a

Add additional fail reason and source during database initialization (#16794)

Stelios Fragkakis committed Jan 15, 2024 at 22:32 UTC 9396b862ae0732d3b15a1f4981513e39257829f9
5 files changed +53 -33
database/sqlite/sqlite_context.c
+4 -4
@@ -52,7 +52,7 @@ int sql_init_context_database(int memory)
52 if (likely(!memory))
53 target_version = perform_context_database_migration(db_context_meta, DB_CONTEXT_METADATA_VERSION);
54
55 - if (configure_sqlite_database(db_context_meta, target_version))
55 + if (configure_sqlite_database(db_context_meta, target_version, "context_config"))
56 return 1;
57
58 if (likely(!memory))
@@ -60,12 +60,12 @@ int sql_init_context_database(int memory)
60 else
61 snprintfz(buf, sizeof(buf) - 1, "ATTACH DATABASE ':memory:' as meta");
62
63 - if(init_database_batch(db_context_meta, list)) return 1;
63 + if(init_database_batch(db_context_meta, list, "context")) return 1;
64
65 - if (init_database_batch(db_context_meta, &database_context_config[0]))
65 + if (init_database_batch(db_context_meta, &database_context_config[0], "context_init"))
66 return 1;
67
68 - if (init_database_batch(db_context_meta, &database_context_cleanup[0]))
68 + if (init_database_batch(db_context_meta, &database_context_cleanup[0], "context_cleanup"))
69 return 1;
70
71 return 0;
database/sqlite/sqlite_db_migration.c
+11 -11
@@ -156,14 +156,14 @@ const char *database_migrate_v13_v14[] = {
156 static int do_migration_v1_v2(sqlite3 *database)
157 {
158 if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "hops"))
159 - return init_database_batch(database, &database_migrate_v1_v2[0]);
159 + return init_database_batch(database, &database_migrate_v1_v2[0], "meta_migrate");
160 return 0;
161 }
162
163 static int do_migration_v2_v3(sqlite3 *database)
164 {
165 if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "memory_mode"))
166 - return init_database_batch(database, &database_migrate_v2_v3[0]);
166 + return init_database_batch(database, &database_migrate_v2_v3[0], "meta_migrate");
167 return 0;
168 }
169
@@ -198,12 +198,12 @@ static int do_migration_v3_v4(sqlite3 *database)
198
199 static int do_migration_v4_v5(sqlite3 *database)
200 {
201 - return init_database_batch(database, &database_migrate_v4_v5[0]);
201 + return init_database_batch(database, &database_migrate_v4_v5[0], "meta_migrate");
202 }
203
204 static int do_migration_v5_v6(sqlite3 *database)
205 {
206 - return init_database_batch(database, &database_migrate_v5_v6[0]);
206 + return init_database_batch(database, &database_migrate_v5_v6[0], "meta_migrate");
207 }
208
209 static int do_migration_v6_v7(sqlite3 *database)
@@ -341,14 +341,14 @@ static int do_migration_v8_v9(sqlite3 *database)
341 static int do_migration_v9_v10(sqlite3 *database)
342 {
343 if (table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "chart_labels"))
344 - return init_database_batch(database, &database_migrate_v9_v10[0]);
344 + return init_database_batch(database, &database_migrate_v9_v10[0], "meta_migrate");
345 return 0;
346 }
347
348 static int do_migration_v10_v11(sqlite3 *database)
349 {
350 if (table_exists_in_database(database, "health_log") && !column_exists_in_table(database, "health_log", "chart_name"))
351 - return init_database_batch(database, &database_migrate_v10_v11[0]);
351 + return init_database_batch(database, &database_migrate_v10_v11[0], "meta_migrate");
352
353 return 0;
354 }
@@ -360,7 +360,7 @@ static int do_migration_v11_v12(sqlite3 *database)
360
361 if (table_exists_in_database(database, "health_log_detail") && !column_exists_in_table(database, "health_log_detail", "summary") &&
362 table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "summary"))
363 - rc = init_database_batch(database, &database_migrate_v11_v12[0]);
363 + rc = init_database_batch(database, &database_migrate_v11_v12[0], "meta_migrate");
364
365 if (!rc)
366 sqlite3_exec_monitored(database, MIGR_11_12_UPD_HEALTH_LOG_DETAIL, 0, 0, NULL);
@@ -400,12 +400,12 @@ static int do_migration_v12_v13(sqlite3 *database)
400 int rc = 0;
401
402 if (table_exists_in_database(database, "health_log_detail") && !column_exists_in_table(database, "health_log_detail", "summary")) {
403 - rc = init_database_batch(database, &database_migrate_v12_v13_detail[0]);
403 + rc = init_database_batch(database, &database_migrate_v12_v13_detail[0], "meta_migrate");
404 sqlite3_exec_monitored(database, MIGR_11_12_UPD_HEALTH_LOG_DETAIL, 0, 0, NULL);
405 }
406
407 if (table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "summary"))
408 - rc = init_database_batch(database, &database_migrate_v12_v13_hash[0]);
408 + rc = init_database_batch(database, &database_migrate_v12_v13_hash[0], "meta_migrate");
409
410 return rc;
411 }
@@ -413,7 +413,7 @@ static int do_migration_v12_v13(sqlite3 *database)
413 static int do_migration_v13_v14(sqlite3 *database)
414 {
415 if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "last_connected"))
416 - return init_database_batch(database, &database_migrate_v13_v14[0]);
416 + return init_database_batch(database, &database_migrate_v13_v14[0], "meta_migrate");
417
418 return 0;
419 }
@@ -431,7 +431,7 @@ const char *database_ml_migrate_v1_v2[] = {
431 static int do_ml_migration_v1_v2(sqlite3 *database)
432 {
433 if (get_auto_vaccum(database) != 2)
434 - return init_database_batch(database, &database_ml_migrate_v1_v2[0]);
434 + return init_database_batch(database, &database_ml_migrate_v1_v2[0], "ml_migrate");
435 return 0;
436 }
437
database/sqlite/sqlite_functions.c
+35 -15
@@ -119,7 +119,7 @@ SQLITE_API int sqlite3_step_monitored(sqlite3_stmt *stmt) {
119 break;
120 case SQLITE_BUSY:
121 case SQLITE_LOCKED:
122 - global_statistics_sqlite3_query_completed(rc == SQLITE_DONE, rc == SQLITE_BUSY, rc == SQLITE_LOCKED);
122 + global_statistics_sqlite3_query_completed(false, rc == SQLITE_BUSY, rc == SQLITE_LOCKED);
123 usleep(SQLITE_INSERT_DELAY * USEC_PER_MS);
124 continue;
125 default:
@@ -201,7 +201,7 @@ int execute_insert(sqlite3_stmt *res)
201 return rc;
202 }
203
204 -int configure_sqlite_database(sqlite3 *database, int target_version)
204 +int configure_sqlite_database(sqlite3 *database, int target_version, const char *description)
205 {
206 char buf[1024 + 1] = "";
207 const char *list[2] = { buf, NULL };
@@ -209,42 +209,42 @@ int configure_sqlite_database(sqlite3 *database, int target_version)
209 // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
210 // PRAGMA schema.auto_vacuum = 0 | NONE | 1 | FULL | 2 | INCREMENTAL;
211 snprintfz(buf, sizeof(buf) - 1, "PRAGMA auto_vacuum=%s", config_get(CONFIG_SECTION_SQLITE, "auto vacuum", "INCREMENTAL"));
212 - if (init_database_batch(database, list))
212 + if (init_database_batch(database, list, description))
213 return 1;
214
215 // https://www.sqlite.org/pragma.html#pragma_synchronous
216 // PRAGMA schema.synchronous = 0 | OFF | 1 | NORMAL | 2 | FULL | 3 | EXTRA;
217 snprintfz(buf, sizeof(buf) - 1, "PRAGMA synchronous=%s", config_get(CONFIG_SECTION_SQLITE, "synchronous", "NORMAL"));
218 - if (init_database_batch(database, list))
218 + if (init_database_batch(database, list, description))
219 return 1;
220
221 // https://www.sqlite.org/pragma.html#pragma_journal_mode
222 // PRAGMA schema.journal_mode = DELETE | TRUNCATE | PERSIST | MEMORY | WAL | OFF
223 snprintfz(buf, sizeof(buf) - 1, "PRAGMA journal_mode=%s", config_get(CONFIG_SECTION_SQLITE, "journal mode", "WAL"));
224 - if (init_database_batch(database, list))
224 + if (init_database_batch(database, list, description))
225 return 1;
226
227 // https://www.sqlite.org/pragma.html#pragma_temp_store
228 // PRAGMA temp_store = 0 | DEFAULT | 1 | FILE | 2 | MEMORY;
229 snprintfz(buf, sizeof(buf) - 1, "PRAGMA temp_store=%s", config_get(CONFIG_SECTION_SQLITE, "temp store", "MEMORY"));
230 - if (init_database_batch(database, list))
230 + if (init_database_batch(database, list, description))
231 return 1;
232
233 // https://www.sqlite.org/pragma.html#pragma_journal_size_limit
234 // PRAGMA schema.journal_size_limit = N ;
235 snprintfz(buf, sizeof(buf) - 1, "PRAGMA journal_size_limit=%lld", config_get_number(CONFIG_SECTION_SQLITE, "journal size limit", 16777216));
236 - if (init_database_batch(database, list))
236 + if (init_database_batch(database, list, description))
237 return 1;
238
239 // https://www.sqlite.org/pragma.html#pragma_cache_size
240 // PRAGMA schema.cache_size = pages;
241 // PRAGMA schema.cache_size = -kibibytes;
242 snprintfz(buf, sizeof(buf) - 1, "PRAGMA cache_size=%lld", config_get_number(CONFIG_SECTION_SQLITE, "cache size", -2000));
243 - if (init_database_batch(database, list))
243 + if (init_database_batch(database, list, description))
244 return 1;
245
246 snprintfz(buf, sizeof(buf) - 1, "PRAGMA user_version=%d", target_version);
247 - if (init_database_batch(database, list))
247 + if (init_database_batch(database, list, description))
248 return 1;
249
250 return 0;
@@ -315,7 +315,21 @@ int prepare_statement(sqlite3 *database, const char *query, sqlite3_stmt **state
315 return rc;
316 }
317
318 -int init_database_batch(sqlite3 *database, const char *batch[])
318 +char *get_database_extented_error(sqlite3 *database, int i, const char *description)
319 +{
320 + const char *err = sqlite3_errstr(sqlite3_extended_errcode(database));
321 +
322 + if (!err)
323 + return NULL;
324 +
325 + size_t len = strlen(err)+ strlen(description) + 32;
326 + char *full_err = mallocz(len);
327 +
328 + snprintfz(full_err, len - 1, "%s: %d: %s", description, i, err);
329 + return full_err;
330 +}
331 +
332 +int init_database_batch(sqlite3 *database, const char *batch[], const char *description)
333 {
334 int rc;
335 char *err_msg = NULL;
@@ -324,8 +338,11 @@ int init_database_batch(sqlite3 *database, const char *batch[])
338 if (rc != SQLITE_OK) {
339 error_report("SQLite error during database initialization, rc = %d (%s)", rc, err_msg);
340 error_report("SQLite failed statement %s", batch[i]);
327 - analytics_set_data_str(&analytics_data.netdata_fail_reason, sqlite3_errstr(sqlite3_extended_errcode(database)));
341 + char *error_str = get_database_extented_error(database, i, description);
342 + if (error_str)
343 + analytics_set_data_str(&analytics_data.netdata_fail_reason, error_str);
344 sqlite3_free(err_msg);
345 + freez(error_str);
346 if (SQLITE_CORRUPT == rc) {
347 if (mark_database_to_recover(NULL, database))
348 error_report("Database is corrupted will attempt to fix");
@@ -408,7 +425,10 @@ int sql_init_database(db_check_action_type_t rebuild, int memory)
425 rc = sqlite3_open(sqlite_database, &db_meta);
426 if (rc != SQLITE_OK) {
427 error_report("Failed to initialize database at %s, due to \"%s\"", sqlite_database, sqlite3_errstr(rc));
411 - analytics_set_data_str(&analytics_data.netdata_fail_reason, sqlite3_errstr(sqlite3_extended_errcode(db_meta)));
428 + char *error_str = get_database_extented_error(db_meta, 0, "meta_open");
429 + if (error_str)
430 + analytics_set_data_str(&analytics_data.netdata_fail_reason, error_str);
431 + freez(error_str);
432 sqlite3_close(db_meta);
433 db_meta = NULL;
434 return 1;
@@ -447,13 +467,13 @@ int sql_init_database(db_check_action_type_t rebuild, int memory)
467 if (likely(!memory))
468 target_version = perform_database_migration(db_meta, DB_METADATA_VERSION);
469
450 - if (configure_sqlite_database(db_meta, target_version))
470 + if (configure_sqlite_database(db_meta, target_version, "meta_config"))
471 return 1;
472
453 - if (init_database_batch(db_meta, &database_config[0]))
473 + if (init_database_batch(db_meta, &database_config[0], "meta_init"))
474 return 1;
475
456 - if (init_database_batch(db_meta, &database_cleanup[0]))
476 + if (init_database_batch(db_meta, &database_cleanup[0], "meta_cleanup"))
477 return 1;
478
479 netdata_log_info("SQLite database initialization completed");
database/sqlite/sqlite_functions.h
+2 -2
@@ -47,10 +47,10 @@ SQLITE_API int sqlite3_exec_monitored(
47 );
48
49 // Initialization and shutdown
50 -int init_database_batch(sqlite3 *database, const char *batch[]);
50 +int init_database_batch(sqlite3 *database, const char *batch[], const char *description);
51 int sql_init_database(db_check_action_type_t rebuild, int memory);
52 void sql_close_database(void);
53 -int configure_sqlite_database(sqlite3 *database, int target_version);
53 +int configure_sqlite_database(sqlite3 *database, int target_version, const char *description);
54
55 // Helpers
56 int bind_text_null(sqlite3_stmt *res, int position, const char *text, bool can_be_null);
ml/ml.cc
+1 -1
@@ -1787,7 +1787,7 @@ void ml_init()
1787 // create table
1788 if (db) {
1789 int target_version = perform_ml_database_migration(db, ML_METADATA_VERSION);
1790 - if (configure_sqlite_database(db, target_version)) {
1790 + if (configure_sqlite_database(db, target_version, "ml_config")) {
1791 error_report("Failed to setup ML database");
1792 sqlite3_close(db);
1793 db = NULL;