sqlite3 global statistics (#13594)
Costa Tsaousis committed
Aug 31, 2022 at 10:04 UTC
77b0e7bccd7666ad6df609051a2736aec29e2d39
13 files changed
+268
-96
daemon/global_statistics.c
+149
-13
@@ -30,6 +30,14 @@ static struct global_statistics {
30
volatile uint64_t rrdr_queries_made;
31
volatile uint64_t rrdr_db_points_read;
32
volatile uint64_t rrdr_result_points_generated;
33
+
34
+ volatile uint64_t sqlite3_queries_made;
35
+ volatile uint64_t sqlite3_queries_ok;
36
+ volatile uint64_t sqlite3_queries_failed;
37
+ volatile uint64_t sqlite3_queries_failed_busy;
38
+ volatile uint64_t sqlite3_queries_failed_locked;
39
+ volatile uint64_t sqlite3_rows;
40
+
41
} global_statistics = {
42
.connected_clients = 0,
43
.web_requests = 0,
@@ -45,6 +53,27 @@ static struct global_statistics {
53
.rrdr_result_points_generated = 0,
54
};
55
56
+void sqlite3_query_completed(bool success, bool busy, bool locked) {
57
+ __atomic_fetch_add(&global_statistics.sqlite3_queries_made, 1, __ATOMIC_RELAXED);
58
+
59
+ if(success) {
60
+ __atomic_fetch_add(&global_statistics.sqlite3_queries_ok, 1, __ATOMIC_RELAXED);
61
+ }
62
+ else {
63
+ __atomic_fetch_add(&global_statistics.sqlite3_queries_failed, 1, __ATOMIC_RELAXED);
64
+
65
+ if(busy)
66
+ __atomic_fetch_add(&global_statistics.sqlite3_queries_failed_busy, 1, __ATOMIC_RELAXED);
67
+
68
+ if(locked)
69
+ __atomic_fetch_add(&global_statistics.sqlite3_queries_failed_locked, 1, __ATOMIC_RELAXED);
70
+ }
71
+}
72
+
73
+void sqlite3_row_completed(void) {
74
+ __atomic_fetch_add(&global_statistics.sqlite3_rows, 1, __ATOMIC_RELAXED);
75
+}
76
+
77
void rrdr_query_completed(uint64_t db_points_read, uint64_t result_points_generated) {
78
__atomic_fetch_add(&global_statistics.rrdr_queries_made, 1, __ATOMIC_RELAXED);
79
__atomic_fetch_add(&global_statistics.rrdr_db_points_read, db_points_read, __ATOMIC_RELAXED);
@@ -79,24 +108,31 @@ void web_client_disconnected(void) {
108
109
110
static inline void global_statistics_copy(struct global_statistics *gs, uint8_t options) {
82
- gs->connected_clients = __atomic_fetch_add(&global_statistics.connected_clients, 0, __ATOMIC_RELAXED);
83
- gs->web_requests = __atomic_fetch_add(&global_statistics.web_requests, 0, __ATOMIC_RELAXED);
84
- gs->web_usec = __atomic_fetch_add(&global_statistics.web_usec, 0, __ATOMIC_RELAXED);
85
- gs->web_usec_max = __atomic_fetch_add(&global_statistics.web_usec_max, 0, __ATOMIC_RELAXED);
86
- gs->bytes_received = __atomic_fetch_add(&global_statistics.bytes_received, 0, __ATOMIC_RELAXED);
87
- gs->bytes_sent = __atomic_fetch_add(&global_statistics.bytes_sent, 0, __ATOMIC_RELAXED);
88
- gs->content_size = __atomic_fetch_add(&global_statistics.content_size, 0, __ATOMIC_RELAXED);
89
- gs->compressed_content_size = __atomic_fetch_add(&global_statistics.compressed_content_size, 0, __ATOMIC_RELAXED);
90
- gs->web_client_count = __atomic_fetch_add(&global_statistics.web_client_count, 0, __ATOMIC_RELAXED);
91
-
92
- gs->rrdr_queries_made = __atomic_fetch_add(&global_statistics.rrdr_queries_made, 0, __ATOMIC_RELAXED);
93
- gs->rrdr_db_points_read = __atomic_fetch_add(&global_statistics.rrdr_db_points_read, 0, __ATOMIC_RELAXED);
94
- gs->rrdr_result_points_generated = __atomic_fetch_add(&global_statistics.rrdr_result_points_generated, 0, __ATOMIC_RELAXED);
111
+ gs->connected_clients = __atomic_load_n(&global_statistics.connected_clients, __ATOMIC_RELAXED);
112
+ gs->web_requests = __atomic_load_n(&global_statistics.web_requests, __ATOMIC_RELAXED);
113
+ gs->web_usec = __atomic_load_n(&global_statistics.web_usec, __ATOMIC_RELAXED);
114
+ gs->web_usec_max = __atomic_load_n(&global_statistics.web_usec_max, __ATOMIC_RELAXED);
115
+ gs->bytes_received = __atomic_load_n(&global_statistics.bytes_received, __ATOMIC_RELAXED);
116
+ gs->bytes_sent = __atomic_load_n(&global_statistics.bytes_sent, __ATOMIC_RELAXED);
117
+ gs->content_size = __atomic_load_n(&global_statistics.content_size, __ATOMIC_RELAXED);
118
+ gs->compressed_content_size = __atomic_load_n(&global_statistics.compressed_content_size, __ATOMIC_RELAXED);
119
+ gs->web_client_count = __atomic_load_n(&global_statistics.web_client_count, __ATOMIC_RELAXED);
120
+
121
+ gs->rrdr_queries_made = __atomic_load_n(&global_statistics.rrdr_queries_made, __ATOMIC_RELAXED);
122
+ gs->rrdr_db_points_read = __atomic_load_n(&global_statistics.rrdr_db_points_read, __ATOMIC_RELAXED);
123
+ gs->rrdr_result_points_generated = __atomic_load_n(&global_statistics.rrdr_result_points_generated, __ATOMIC_RELAXED);
124
125
if(options & GLOBAL_STATS_RESET_WEB_USEC_MAX) {
126
uint64_t n = 0;
127
__atomic_compare_exchange(&global_statistics.web_usec_max, (uint64_t *) &gs->web_usec_max, &n, 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
128
}
129
+
130
+ gs->sqlite3_queries_made = __atomic_load_n(&global_statistics.sqlite3_queries_made, __ATOMIC_RELAXED);
131
+ gs->sqlite3_queries_ok = __atomic_load_n(&global_statistics.sqlite3_queries_ok, __ATOMIC_RELAXED);
132
+ gs->sqlite3_queries_failed = __atomic_load_n(&global_statistics.sqlite3_queries_failed, __ATOMIC_RELAXED);
133
+ gs->sqlite3_queries_failed_busy = __atomic_load_n(&global_statistics.sqlite3_queries_failed_busy, __ATOMIC_RELAXED);
134
+ gs->sqlite3_queries_failed_locked = __atomic_load_n(&global_statistics.sqlite3_queries_failed_locked, __ATOMIC_RELAXED);
135
+ gs->sqlite3_rows = __atomic_load_n(&global_statistics.sqlite3_rows, __ATOMIC_RELAXED);
136
}
137
138
static void global_statistics_charts(void) {
@@ -443,6 +479,106 @@ static void global_statistics_charts(void) {
479
}
480
481
// ----------------------------------------------------------------
482
+
483
+ if(gs.sqlite3_queries_made) {
484
+ static RRDSET *st_sqlite3_queries = NULL;
485
+ static RRDDIM *rd_queries = NULL;
486
+
487
+ if (unlikely(!st_sqlite3_queries)) {
488
+ st_sqlite3_queries = rrdset_create_localhost(
489
+ "netdata"
490
+ , "sqlite3_queries"
491
+ , NULL
492
+ , "sqlite3"
493
+ , NULL
494
+ , "Netdata SQLite3 Queries"
495
+ , "queries/s"
496
+ , "netdata"
497
+ , "stats"
498
+ , 131100
499
+ , localhost->rrd_update_every
500
+ , RRDSET_TYPE_LINE
501
+ );
502
+
503
+ rd_queries = rrddim_add(st_sqlite3_queries, "queries", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
504
+ }
505
+ else
506
+ rrdset_next(st_sqlite3_queries);
507
+
508
+ rrddim_set_by_pointer(st_sqlite3_queries, rd_queries, (collected_number)gs.sqlite3_queries_made);
509
+
510
+ rrdset_done(st_sqlite3_queries);
511
+ }
512
+
513
+ // ----------------------------------------------------------------
514
+
515
+ if(gs.sqlite3_queries_ok || gs.sqlite3_queries_failed) {
516
+ static RRDSET *st_sqlite3_queries_by_status = NULL;
517
+ static RRDDIM *rd_ok = NULL, *rd_failed = NULL, *rd_busy = NULL, *rd_locked = NULL;
518
+
519
+ if (unlikely(!st_sqlite3_queries_by_status)) {
520
+ st_sqlite3_queries_by_status = rrdset_create_localhost(
521
+ "netdata"
522
+ , "sqlite3_queries_by_status"
523
+ , NULL
524
+ , "sqlite3"
525
+ , NULL
526
+ , "Netdata SQLite3 Queries by status"
527
+ , "queries/s"
528
+ , "netdata"
529
+ , "stats"
530
+ , 131101
531
+ , localhost->rrd_update_every
532
+ , RRDSET_TYPE_LINE
533
+ );
534
+
535
+ rd_ok = rrddim_add(st_sqlite3_queries_by_status, "ok", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
536
+ rd_failed = rrddim_add(st_sqlite3_queries_by_status, "failed", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
537
+ rd_busy = rrddim_add(st_sqlite3_queries_by_status, "busy", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
538
+ rd_locked = rrddim_add(st_sqlite3_queries_by_status, "locked", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
539
+ }
540
+ else
541
+ rrdset_next(st_sqlite3_queries_by_status);
542
+
543
+ rrddim_set_by_pointer(st_sqlite3_queries_by_status, rd_ok, (collected_number)gs.sqlite3_queries_made);
544
+ rrddim_set_by_pointer(st_sqlite3_queries_by_status, rd_failed, (collected_number)gs.sqlite3_queries_failed);
545
+ rrddim_set_by_pointer(st_sqlite3_queries_by_status, rd_busy, (collected_number)gs.sqlite3_queries_failed_busy);
546
+ rrddim_set_by_pointer(st_sqlite3_queries_by_status, rd_locked, (collected_number)gs.sqlite3_queries_failed_locked);
547
+
548
+ rrdset_done(st_sqlite3_queries_by_status);
549
+ }
550
+
551
+ // ----------------------------------------------------------------
552
+
553
+ if(gs.sqlite3_rows) {
554
+ static RRDSET *st_sqlite3_rows = NULL;
555
+ static RRDDIM *rd_rows = NULL;
556
+
557
+ if (unlikely(!st_sqlite3_rows)) {
558
+ st_sqlite3_rows = rrdset_create_localhost(
559
+ "netdata"
560
+ , "sqlite3_rows"
561
+ , NULL
562
+ , "sqlite3"
563
+ , NULL
564
+ , "Netdata SQLite3 Rows"
565
+ , "rows/s"
566
+ , "netdata"
567
+ , "stats"
568
+ , 131102
569
+ , localhost->rrd_update_every
570
+ , RRDSET_TYPE_LINE
571
+ );
572
+
573
+ rd_rows = rrddim_add(st_sqlite3_rows, "ok", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
574
+ }
575
+ else
576
+ rrdset_next(st_sqlite3_rows);
577
+
578
+ rrddim_set_by_pointer(st_sqlite3_rows, rd_rows, (collected_number)gs.sqlite3_rows);
579
+
580
+ rrdset_done(st_sqlite3_rows);
581
+ }
582
}
583
584
static void dbengine_statistics_charts(void) {
daemon/global_statistics.h
+2
@@ -9,6 +9,8 @@
9
// global statistics
10
11
extern void rrdr_query_completed(uint64_t db_points_read, uint64_t result_points_generated);
12
+extern void sqlite3_query_completed(bool success, bool busy, bool locked);
13
+extern void sqlite3_row_completed(void);
14
15
extern void finished_web_request_statistics(uint64_t dt,
16
uint64_t bytes_received,
daemon/unit_test.c
+11
-11
@@ -1527,19 +1527,19 @@ int test_sqlite(void) {
1527
return 1;
1528
}
1529
1530
- rc = sqlite3_exec(db_meta, "CREATE TABLE IF NOT EXISTS mine (id1, id2);", 0, 0, NULL);
1530
+ rc = sqlite3_exec_monitored(db_meta, "CREATE TABLE IF NOT EXISTS mine (id1, id2);", 0, 0, NULL);
1531
if (rc != SQLITE_OK) {
1532
fprintf(stderr,"Failed to test SQLite: Create table failed\n");
1533
return 1;
1534
}
1535
1536
- rc = sqlite3_exec(db_meta, "DELETE FROM MINE LIMIT 1;", 0, 0, NULL);
1536
+ rc = sqlite3_exec_monitored(db_meta, "DELETE FROM MINE LIMIT 1;", 0, 0, NULL);
1537
if (rc != SQLITE_OK) {
1538
fprintf(stderr,"Failed to test SQLite: Delete with LIMIT failed\n");
1539
return 1;
1540
}
1541
1542
- rc = sqlite3_exec(db_meta, "UPDATE MINE SET id1=1 LIMIT 1;", 0, 0, NULL);
1542
+ rc = sqlite3_exec_monitored(db_meta, "UPDATE MINE SET id1=1 LIMIT 1;", 0, 0, NULL);
1543
if (rc != SQLITE_OK) {
1544
fprintf(stderr,"Failed to test SQLite: Update with LIMIT failed\n");
1545
return 1;
@@ -1549,49 +1549,49 @@ int test_sqlite(void) {
1549
char *uuid_str = "0000_000";
1550
1551
buffer_sprintf(sql, TABLE_ACLK_CHART, uuid_str);
1552
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1552
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1553
buffer_flush(sql);
1554
if (rc != SQLITE_OK)
1555
goto error;
1556
1557
buffer_sprintf(sql, TABLE_ACLK_CHART_PAYLOAD, uuid_str);
1558
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1558
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1559
buffer_flush(sql);
1560
if (rc != SQLITE_OK)
1561
goto error;
1562
1563
buffer_sprintf(sql, TABLE_ACLK_CHART_LATEST, uuid_str);
1564
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1564
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1565
if (rc != SQLITE_OK)
1566
goto error;
1567
buffer_flush(sql);
1568
1569
buffer_sprintf(sql, INDEX_ACLK_CHART, uuid_str, uuid_str);
1570
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1570
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1571
if (rc != SQLITE_OK)
1572
goto error;
1573
buffer_flush(sql);
1574
1575
buffer_sprintf(sql, INDEX_ACLK_CHART_LATEST, uuid_str, uuid_str);
1576
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1576
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1577
if (rc != SQLITE_OK)
1578
goto error;
1579
buffer_flush(sql);
1580
1581
buffer_sprintf(sql, TRIGGER_ACLK_CHART_PAYLOAD, uuid_str, uuid_str, uuid_str);
1582
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1582
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1583
if (rc != SQLITE_OK)
1584
goto error;
1585
buffer_flush(sql);
1586
1587
buffer_sprintf(sql, TABLE_ACLK_ALERT, uuid_str);
1588
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1588
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1589
if (rc != SQLITE_OK)
1590
goto error;
1591
buffer_flush(sql);
1592
1593
buffer_sprintf(sql, INDEX_ACLK_ALERT, uuid_str, uuid_str);
1594
- rc = sqlite3_exec(db_meta, buffer_tostring(sql), 0, 0, NULL);
1594
+ rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), 0, 0, NULL);
1595
if (rc != SQLITE_OK)
1596
goto error;
1597
buffer_flush(sql);
database/sqlite/sqlite_aclk.c
+6
-6
@@ -361,7 +361,7 @@ void sql_aclk_sync_init(void)
361
362
for (int i = 0; aclk_sync_config[i]; i++) {
363
debug(D_ACLK_SYNC, "Executing %s", aclk_sync_config[i]);
364
- rc = sqlite3_exec(db_meta, aclk_sync_config[i], 0, 0, &err_msg);
364
+ rc = sqlite3_exec_monitored(db_meta, aclk_sync_config[i], 0, 0, &err_msg);
365
if (rc != SQLITE_OK) {
366
error_report("SQLite error aclk sync initialization setup, rc = %d (%s)", rc, err_msg);
367
error_report("SQLite failed statement %s", aclk_sync_config[i]);
@@ -373,7 +373,7 @@ void sql_aclk_sync_init(void)
373
fatal_assert(0 == uv_mutex_init(&aclk_async_lock));
374
375
if (likely(rrdcontext_enabled == CONFIG_BOOLEAN_YES)) {
376
- rc = sqlite3_exec(db_meta, "SELECT host_id, hostname, registry_hostname, update_every, os, "
376
+ rc = sqlite3_exec_monitored(db_meta, "SELECT host_id, hostname, registry_hostname, update_every, os, "
377
"timezone, tags, hops, memory_mode, abbrev_timezone, utc_offset, program_name, "
378
"program_version, entries, health_enabled FROM host WHERE hops >0;",
379
create_host_callback, NULL, &err_msg);
@@ -383,7 +383,7 @@ void sql_aclk_sync_init(void)
383
}
384
}
385
386
- rc = sqlite3_exec(db_meta, "SELECT ni.host_id, ni.node_id FROM host h, node_instance ni WHERE "
386
+ rc = sqlite3_exec_monitored(db_meta, "SELECT ni.host_id, ni.node_id FROM host h, node_instance ni WHERE "
387
"h.host_id = ni.host_id AND ni.node_id IS NOT NULL;", aclk_start_sync_thread, NULL, &err_msg);
388
if (rc != SQLITE_OK) {
389
error_report("SQLite error when starting ACLK sync threads, rc = %d (%s)", rc, err_msg);
@@ -927,7 +927,7 @@ static int is_host_available(uuid_t *host_id)
927
error_report("Failed to bind host_id parameter to select node instance information");
928
goto failed;
929
}
930
- rc = sqlite3_step(res);
930
+ rc = sqlite3_step_monitored(res);
931
932
failed:
933
if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
@@ -980,7 +980,7 @@ void sql_delete_aclk_table_list(struct aclk_database_worker_config *wc, struct a
980
}
981
buffer_flush(sql);
982
983
- while (sqlite3_step(res) == SQLITE_ROW)
983
+ while (sqlite3_step_monitored(res) == SQLITE_ROW)
984
buffer_strcat(sql, (char *) sqlite3_column_text(res, 0));
985
986
rc = sqlite3_finalize(res);
@@ -1016,7 +1016,7 @@ void sql_check_aclk_table_list(struct aclk_database_worker_config *wc)
1016
{
1017
char *err_msg = NULL;
1018
debug(D_ACLK_SYNC,"Cleaning tables for nodes that do not exist");
1019
- int rc = sqlite3_exec(db_meta, SQL_SELECT_ACLK_ACTIVE_LIST, sql_check_aclk_table, (void *) wc, &err_msg);
1019
+ int rc = sqlite3_exec_monitored(db_meta, SQL_SELECT_ACLK_ACTIVE_LIST, sql_check_aclk_table, (void *) wc, &err_msg);
1020
if (rc != SQLITE_OK) {
1021
error_report("Query failed when trying to check for obsolete ACLK sync tables, %s", err_msg);
1022
sqlite3_free(err_msg);
database/sqlite/sqlite_aclk_alert.c
+7
-7
@@ -24,7 +24,7 @@ time_t removed_when(uint32_t alarm_id, uint32_t before_unique_id, uint32_t after
24
return 0;
25
}
26
27
- rc = sqlite3_step(res);
27
+ rc = sqlite3_step_monitored(res);
28
if (likely(rc == SQLITE_ROW)) {
29
when = (time_t) sqlite3_column_int64(res, 0);
30
}
@@ -70,7 +70,7 @@ int should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
70
return send;
71
}
72
73
- rc = sqlite3_step(res);
73
+ rc = sqlite3_step_monitored(res);
74
if (likely(rc == SQLITE_ROW)) {
75
status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
76
if (sqlite3_column_type(res, 1) != SQLITE_NULL)
@@ -280,7 +280,7 @@ void aclk_push_alert_event(struct aclk_database_worker_config *wc, struct aclk_d
280
static __thread uint64_t log_first_sequence_id = 0;
281
static __thread uint64_t log_last_sequence_id = 0;
282
283
- while (sqlite3_step(res) == SQLITE_ROW) {
283
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
284
struct alarm_log_entry alarm_log;
285
char old_value_string[100 + 1];
286
char new_value_string[100 + 1];
@@ -500,7 +500,7 @@ void aclk_push_alarm_health_log(struct aclk_database_worker_config *wc, struct a
500
last_timestamp.tv_sec = 0;
501
last_timestamp.tv_usec = 0;
502
503
- while (sqlite3_step(res) == SQLITE_ROW) {
503
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
504
first_sequence = sqlite3_column_bytes(res, 0) > 0 ? (uint64_t) sqlite3_column_int64(res, 0) : 0;
505
if (sqlite3_column_bytes(res, 1) > 0) {
506
first_timestamp.tv_sec = sqlite3_column_int64(res, 1);
@@ -603,7 +603,7 @@ int aclk_push_alert_config_event(struct aclk_database_worker_config *wc, struct
603
struct provide_alarm_configuration p_alarm_config;
604
p_alarm_config.cfg_hash = NULL;
605
606
- if (sqlite3_step(res) == SQLITE_ROW) {
606
+ if (sqlite3_step_monitored(res) == SQLITE_ROW) {
607
608
alarm_config.alarm = sqlite3_column_bytes(res, 0) > 0 ? strdupz((char *)sqlite3_column_text(res, 0)) : NULL;
609
alarm_config.tmpl = sqlite3_column_bytes(res, 1) > 0 ? strdupz((char *)sqlite3_column_text(res, 1)) : NULL;
@@ -1029,7 +1029,7 @@ void sql_aclk_alert_clean_dead_entries(RRDHOST *host)
1029
" (select unique_id from health_log_%s); ", uuid_str, uuid_str);
1030
1031
char *err_msg = NULL;
1032
- int rc = sqlite3_exec(db_meta, buffer_tostring(sql), NULL, NULL, &err_msg);
1032
+ int rc = sqlite3_exec_monitored(db_meta, buffer_tostring(sql), NULL, NULL, &err_msg);
1033
if (rc != SQLITE_OK) {
1034
error_report("Failed when trying to clean stale ACLK alert entries from aclk_alert_%s, error message \"%s""",
1035
uuid_str, err_msg);
@@ -1064,7 +1064,7 @@ int get_proto_alert_status(RRDHOST *host, struct proto_alert_status *proto_alert
1064
return 1;
1065
}
1066
1067
- while (sqlite3_step(res) == SQLITE_ROW) {
1067
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
1068
proto_alert_status->pending_min_sequence_id = sqlite3_column_bytes(res, 0) > 0 ? (uint64_t) sqlite3_column_int64(res, 0) : 0;
1069
proto_alert_status->pending_max_sequence_id = sqlite3_column_bytes(res, 1) > 0 ? (uint64_t) sqlite3_column_int64(res, 1) : 0;
1070
proto_alert_status->last_acked_sequence_id = sqlite3_column_bytes(res, 2) > 0 ? (uint64_t) sqlite3_column_int64(res, 2) : 0;
database/sqlite/sqlite_aclk_chart.c
+14
-14
@@ -48,7 +48,7 @@ static time_t payload_sent(char *uuid_str, uuid_t *uuid, void *payload, size_t p
48
if (unlikely(rc != SQLITE_OK))
49
goto bind_fail;
50
51
- while (sqlite3_step(res) == SQLITE_ROW) {
51
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
52
send_status = (time_t) sqlite3_column_int64(res, 0);
53
}
54
@@ -245,7 +245,7 @@ void aclk_process_dimension_deletion(struct aclk_database_worker_config *wc, str
245
goto bind_fail;
246
247
unsigned count = 0;
248
- while (sqlite3_step(res) == SQLITE_ROW) {
248
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
249
(void) aclk_upd_dimension_event(
250
wc,
251
claim_id,
@@ -357,7 +357,7 @@ void aclk_send_chart_event(struct aclk_database_worker_config *wc, struct aclk_d
357
int count = 0;
358
first_sequence = 0;
359
last_sequence = 0;
360
- while (count < limit && sqlite3_step(res) == SQLITE_ROW) {
360
+ while (count < limit && sqlite3_step_monitored(res) == SQLITE_ROW) {
361
size_t payload_size = sqlite3_column_bytes(res, 1);
362
if (payload_list_max_size[count] < payload_size) {
363
freez(payload_list[count]);
@@ -487,7 +487,7 @@ int aclk_send_chart_config(struct aclk_database_worker_config *wc, struct aclk_d
487
struct chart_config_updated chart_config;
488
chart_config.config_hash = NULL;
489
490
- while (sqlite3_step(res) == SQLITE_ROW) {
490
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
491
chart_config.type = strdupz((char *)sqlite3_column_text(res, 0));
492
chart_config.family = strdupz((char *)sqlite3_column_text(res, 1));
493
chart_config.context = strdupz((char *)sqlite3_column_text(res, 2));
@@ -799,7 +799,7 @@ static RRD_MEMORY_MODE sql_get_host_memory_mode(uuid_t *host_id)
799
goto failed;
800
}
801
802
- while (sqlite3_step(res) == SQLITE_ROW) {
802
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
803
memory_mode = (RRD_MEMORY_MODE)sqlite3_column_int(res, 0);
804
}
805
@@ -891,7 +891,7 @@ void aclk_update_retention(struct aclk_database_worker_config *wc)
891
rotate_data.node_id = strdupz(wc->node_id);
892
893
time_t now = now_realtime_sec();
894
- while (sqlite3_step(res) == SQLITE_ROW && dimension_update_count < ACLK_MAX_DIMENSION_CLEANUP) {
894
+ while (sqlite3_step_monitored(res) == SQLITE_ROW && dimension_update_count < ACLK_MAX_DIMENSION_CLEANUP) {
895
if (unlikely(netdata_exit))
896
break;
897
if (!update_every || update_every != (uint32_t)sqlite3_column_int(res, 1)) {
@@ -1022,7 +1022,7 @@ uint32_t sql_get_pending_count(struct aclk_database_worker_config *wc)
1022
return 0;
1023
}
1024
}
1025
- while (sqlite3_step(res) == SQLITE_ROW)
1025
+ while (sqlite3_step_monitored(res) == SQLITE_ROW)
1026
chart_payload_count = (uint32_t) sqlite3_column_int(res, 0);
1027
1028
rc = sqlite3_reset(res);
@@ -1049,7 +1049,7 @@ void sql_get_last_chart_sequence(struct aclk_database_worker_config *wc)
1049
1050
wc->chart_sequence_id = 0;
1051
wc->chart_timestamp = 0;
1052
- while (sqlite3_step(res) == SQLITE_ROW) {
1052
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
1053
wc->chart_sequence_id = (uint64_t)sqlite3_column_int64(res, 0);
1054
wc->chart_timestamp = (time_t)sqlite3_column_int64(res, 1);
1055
}
@@ -1220,37 +1220,37 @@ struct aclk_chart_sync_stats *aclk_get_chart_sync_stats(RRDHOST *host)
1220
return NULL;
1221
}
1222
1223
- rc = sqlite3_step(res);
1223
+ rc = sqlite3_step_monitored(res);
1224
if (rc == SQLITE_ROW) {
1225
aclk_statistics->min_seqid = SQL_SEQ_NULL(res, 0);
1226
aclk_statistics->max_seqid = SQL_SEQ_NULL(res, 1);
1227
}
1228
1229
- rc = sqlite3_step(res);
1229
+ rc = sqlite3_step_monitored(res);
1230
if (rc == SQLITE_ROW) {
1231
aclk_statistics->min_seqid_pend = SQL_SEQ_NULL(res, 0);
1232
aclk_statistics->max_seqid_pend = SQL_SEQ_NULL(res, 1);
1233
}
1234
1235
- rc = sqlite3_step(res);
1235
+ rc = sqlite3_step_monitored(res);
1236
if (rc == SQLITE_ROW) {
1237
aclk_statistics->min_seqid_sent = SQL_SEQ_NULL(res, 0);
1238
aclk_statistics->max_seqid_sent = SQL_SEQ_NULL(res, 1);
1239
}
1240
1241
- rc = sqlite3_step(res);
1241
+ rc = sqlite3_step_monitored(res);
1242
if (rc == SQLITE_ROW) {
1243
aclk_statistics->min_seqid_ack = SQL_SEQ_NULL(res, 0);
1244
aclk_statistics->max_seqid_ack = SQL_SEQ_NULL(res, 1);
1245
}
1246
1247
- rc = sqlite3_step(res);
1247
+ rc = sqlite3_step_monitored(res);
1248
if (rc == SQLITE_ROW) {
1249
aclk_statistics->min_seqid_ack = SQL_SEQ_NULL(res, 0);
1250
aclk_statistics->max_seqid_ack = SQL_SEQ_NULL(res, 1);
1251
}
1252
1253
- rc = sqlite3_step(res);
1253
+ rc = sqlite3_step_monitored(res);
1254
if (rc == SQLITE_ROW) {
1255
aclk_statistics->max_date_created = (time_t) SQL_SEQ_NULL(res, 0);
1256
aclk_statistics->max_date_submitted = (time_t) SQL_SEQ_NULL(res, 1);
database/sqlite/sqlite_context.c
+4
-4
@@ -150,7 +150,7 @@ void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, voi
150
}
151
152
SQL_CHART_DATA chart_data = { 0 };
153
- while (sqlite3_step(res) == SQLITE_ROW) {
153
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
154
uuid_copy(chart_data.chart_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
155
chart_data.id = (char *) sqlite3_column_text(res, 1);
156
chart_data.name = (char *) sqlite3_column_text(res, 2);
@@ -191,7 +191,7 @@ void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DA
191
192
SQL_DIMENSION_DATA dimension_data;
193
194
- while (sqlite3_step(res) == SQLITE_ROW) {
194
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
195
uuid_copy(dimension_data.dim_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
196
dimension_data.id = (char *) sqlite3_column_text(res, 1);
197
dimension_data.name = (char *) sqlite3_column_text(res, 2);
@@ -225,7 +225,7 @@ void ctx_get_label_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_CLABEL_DATA *, v
225
226
SQL_CLABEL_DATA label_data;
227
228
- while (sqlite3_step(res) == SQLITE_ROW) {
228
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
229
label_data.label_key = (char *) sqlite3_column_text(res, 0);
230
label_data.label_value = (char *) sqlite3_column_text(res, 1);
231
label_data.label_source = sqlite3_column_int(res, 2);
@@ -267,7 +267,7 @@ void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_D
267
goto failed;
268
}
269
270
- while (sqlite3_step(res) == SQLITE_ROW) {
270
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
271
context_data.id = (char *) sqlite3_column_text(res, 0);
272
context_data.version = sqlite3_column_int64(res, 1);
273
context_data.title = (char *) sqlite3_column_text(res, 2);
database/sqlite/sqlite_db_migration.c
+5
-5
@@ -21,7 +21,7 @@ static int table_exists_in_database(const char *table)
21
22
snprintf(sql, 127, "select 1 from sqlite_schema where type = 'table' and name = '%s';", table);
23
24
- int rc = sqlite3_exec(db_meta, sql, return_int_cb, (void *) &exists, &err_msg);
24
+ int rc = sqlite3_exec_monitored(db_meta, sql, return_int_cb, (void *) &exists, &err_msg);
25
if (rc != SQLITE_OK) {
26
info("Error checking table existence; %s", err_msg);
27
sqlite3_free(err_msg);
@@ -39,7 +39,7 @@ static int column_exists_in_table(const char *table, const char *column)
39
40
snprintf(sql, 127, "SELECT 1 FROM pragma_table_info('%s') where name = '%s';", table, column);
41
42
- int rc = sqlite3_exec(db_meta, sql, return_int_cb, (void *) &exists, &err_msg);
42
+ int rc = sqlite3_exec_monitored(db_meta, sql, return_int_cb, (void *) &exists, &err_msg);
43
if (rc != SQLITE_OK) {
44
info("Error checking column existence; %s", err_msg);
45
sqlite3_free(err_msg);
@@ -100,11 +100,11 @@ static int do_migration_v3_v4(sqlite3 *database, const char *name)
100
return 1;
101
}
102
103
- while (sqlite3_step(res) == SQLITE_ROW) {
103
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
104
char *table = strdupz((char *) sqlite3_column_text(res, 0));
105
if (!column_exists_in_table(table, "chart_context")) {
106
snprintfz(sql, 255, "ALTER TABLE %s ADD chart_context text", table);
107
- sqlite3_exec(database, sql, 0, 0, NULL);
107
+ sqlite3_exec_monitored(database, sql, 0, 0, NULL);
108
}
109
freez(table);
110
}
@@ -135,7 +135,7 @@ static int migrate_database(sqlite3 *database, int target_version, char *db_name
135
int user_version = 0;
136
char *err_msg = NULL;
137
138
- int rc = sqlite3_exec(database, "PRAGMA user_version;", return_int_cb, (void *) &user_version, &err_msg);
138
+ int rc = sqlite3_exec_monitored(database, "PRAGMA user_version;", return_int_cb, (void *) &user_version, &err_msg);
139
if (rc != SQLITE_OK) {
140
info("Error checking the %s database version; %s", db_name, err_msg);
141
sqlite3_free(err_msg);
database/sqlite/sqlite_functions.c
+53
-28
@@ -88,11 +88,35 @@ pthread_key_t key_pool[MAX_PREPARED_STATEMENTS];
88
89
static uv_mutex_t sqlite_transaction_lock;
90
91
+
92
+SQLITE_API int sqlite3_exec_monitored(
93
+ sqlite3 *db, /* An open database */
94
+ const char *sql, /* SQL to be evaluated */
95
+ int (*callback)(void*,int,char**,char**), /* Callback function */
96
+ void *data, /* 1st argument to callback */
97
+ char **errmsg /* Error msg written here */
98
+) {
99
+ int rc = sqlite3_exec(db, sql, callback, data, errmsg);
100
+ sqlite3_query_completed(rc == SQLITE_OK, rc == SQLITE_BUSY, rc == SQLITE_LOCKED);
101
+ return rc;
102
+}
103
+
104
+SQLITE_API int sqlite3_step_monitored(sqlite3_stmt *stmt) {
105
+ int rc = sqlite3_step(stmt);
106
+
107
+ if(likely(rc == SQLITE_ROW))
108
+ sqlite3_row_completed();
109
+ else
110
+ sqlite3_query_completed(rc == SQLITE_DONE, rc == SQLITE_BUSY, rc == SQLITE_LOCKED);
111
+
112
+ return rc;
113
+}
114
+
115
int execute_insert(sqlite3_stmt *res)
116
{
117
int rc;
118
int cnt = 0;
95
- while ((rc = sqlite3_step(res)) != SQLITE_DONE && ++cnt < SQL_MAX_RETRY && likely(!netdata_exit)) {
119
+ while ((rc = sqlite3_step_monitored(res)) != SQLITE_DONE && ++cnt < SQL_MAX_RETRY && likely(!netdata_exit)) {
120
if (likely(rc == SQLITE_BUSY || rc == SQLITE_LOCKED)) {
121
usleep(SQLITE_INSERT_DELAY * USEC_PER_MS);
122
error_report("Failed to insert/update, rc = %d -- attempt %d", rc, cnt);
@@ -273,7 +297,7 @@ static int check_table_integrity(char *table)
297
strcpy(wstr,"PRAGMA integrity_check;");
298
}
299
276
- int rc = sqlite3_exec(db_meta, wstr, check_table_integrity_cb, (void *) &status, &err_msg);
300
+ int rc = sqlite3_exec_monitored(db_meta, wstr, check_table_integrity_cb, (void *) &status, &err_msg);
301
if (rc != SQLITE_OK) {
302
error_report("SQLite error during database integrity check for %s, rc = %d (%s)",
303
table ? table : "the entire database", rc, err_msg);
@@ -306,7 +330,7 @@ static void rebuild_chart()
330
info("Rebuilding chart table");
331
for (int i = 0; rebuild_chart_commands[i]; i++) {
332
info("Executing %s", rebuild_chart_commands[i]);
309
- rc = sqlite3_exec(db_meta, rebuild_chart_commands[i], 0, 0, &err_msg);
333
+ rc = sqlite3_exec_monitored(db_meta, rebuild_chart_commands[i], 0, 0, &err_msg);
334
if (rc != SQLITE_OK) {
335
error_report("SQLite error during database setup, rc = %d (%s)", rc, err_msg);
336
error_report("SQLite failed statement %s", rebuild_chart_commands[i]);
@@ -339,7 +363,7 @@ void rebuild_dimension()
363
info("Rebuilding dimension table");
364
for (int i = 0; rebuild_dimension_commands[i]; i++) {
365
info("Executing %s", rebuild_dimension_commands[i]);
342
- rc = sqlite3_exec(db_meta, rebuild_dimension_commands[i], 0, 0, &err_msg);
366
+ rc = sqlite3_exec_monitored(db_meta, rebuild_dimension_commands[i], 0, 0, &err_msg);
367
if (rc != SQLITE_OK) {
368
error_report("SQLite error during database setup, rc = %d (%s)", rc, err_msg);
369
error_report("SQLite failed statement %s", rebuild_dimension_commands[i]);
@@ -366,7 +390,7 @@ int init_database_batch(sqlite3 *database, int rebuild, int init_type, const cha
390
char *err_msg = NULL;
391
for (int i = 0; batch[i]; i++) {
392
debug(D_METADATALOG, "Executing %s", batch[i]);
369
- rc = sqlite3_exec(database, batch[i], 0, 0, &err_msg);
393
+ rc = sqlite3_exec_monitored(database, batch[i], 0, 0, &err_msg);
394
if (rc != SQLITE_OK) {
395
error_report("SQLite error during database %s, rc = %d (%s)", init_type ? "cleanup" : "setup", rc, err_msg);
396
error_report("SQLite failed statement %s", batch[i]);
@@ -437,7 +461,7 @@ int sql_init_database(db_check_action_type_t rebuild, int memory)
461
if (rebuild & DB_CHECK_RECLAIM_SPACE) {
462
if (!(rebuild & DB_CHECK_CONT))
463
info("Reclaiming space of %s", sqlite_database);
440
- rc = sqlite3_exec(db_meta, "VACUUM;", 0, 0, &err_msg);
464
+ rc = sqlite3_exec_monitored(db_meta, "VACUUM;", 0, 0, &err_msg);
465
if (rc != SQLITE_OK) {
466
error_report("Failed to execute VACUUM rc = %d (%s)", rc, err_msg);
467
sqlite3_free(err_msg);
@@ -546,7 +570,7 @@ int find_uuid_type(uuid_t *uuid)
570
if (unlikely(rc != SQLITE_OK))
571
goto bind_fail;
572
549
- rc = sqlite3_step(res);
573
+ rc = sqlite3_step_monitored(res);
574
if (likely(rc == SQLITE_ROW))
575
uuid_type = sqlite3_column_int(res, 0);
576
@@ -589,7 +613,7 @@ int find_dimension_uuid(RRDSET *st, RRDDIM *rd, uuid_t *store_uuid)
613
if (unlikely(rc != SQLITE_OK))
614
goto bind_fail;
615
592
- rc = sqlite3_step(res);
616
+ rc = sqlite3_step_monitored(res);
617
if (likely(rc == SQLITE_ROW)) {
618
uuid_copy(*store_uuid, *((uuid_t *) sqlite3_column_blob(res, 0)));
619
status = 0;
@@ -636,7 +660,7 @@ void delete_dimension_uuid(uuid_t *dimension_uuid)
660
if (unlikely(rc != SQLITE_OK))
661
goto bind_fail;
662
639
- rc = sqlite3_step(res);
663
+ rc = sqlite3_step_monitored(res);
664
if (unlikely(rc != SQLITE_DONE))
665
error_report("Failed to delete dimension uuid, rc = %d", rc);
666
@@ -684,7 +708,7 @@ uuid_t *find_chart_uuid(RRDHOST *host, const char *type, const char *id, const c
708
if (unlikely(rc != SQLITE_OK))
709
goto bind_fail;
710
687
- rc = sqlite3_step(res);
711
+ rc = sqlite3_step_monitored(res);
712
if (likely(rc == SQLITE_ROW)) {
713
uuid = mallocz(sizeof(uuid_t));
714
uuid_copy(*uuid, sqlite3_column_blob(res, 0));
@@ -847,7 +871,7 @@ int sql_store_host(
871
if (unlikely(rc != SQLITE_OK))
872
goto bind_fail;
873
850
- int store_rc = sqlite3_step(res);
874
+ int store_rc = sqlite3_step_monitored(res);
875
if (unlikely(store_rc != SQLITE_DONE))
876
error_report("Failed to store host %s, rc = %d", hostname, rc);
877
@@ -954,7 +978,7 @@ int sql_store_host_info(RRDHOST *host)
978
if (unlikely(rc != SQLITE_OK))
979
goto bind_fail;
980
957
- int store_rc = sqlite3_step(res);
981
+ int store_rc = sqlite3_step_monitored(res);
982
if (unlikely(store_rc != SQLITE_DONE))
983
error_report("Failed to store host %s, rc = %d", host->hostname, rc);
984
@@ -1229,7 +1253,7 @@ void sql_rrdim2json(sqlite3_stmt *res_dim, uuid_t *chart_uuid, BUFFER *wb, size_
1253
int dimensions = 0;
1254
buffer_sprintf(wb, "\t\t\t\"dimensions\": {\n");
1255
1232
- while (sqlite3_step(res_dim) == SQLITE_ROW) {
1256
+ while (sqlite3_step_monitored(res_dim) == SQLITE_ROW) {
1257
if (dimensions)
1258
buffer_strcat(wb, ",\n\t\t\t\t\"");
1259
else
@@ -1305,7 +1329,7 @@ void sql_rrdset2json(RRDHOST *host, BUFFER *wb)
1329
size_t c = 0;
1330
size_t dimensions = 0;
1331
1308
- while (sqlite3_step(res_chart) == SQLITE_ROW) {
1332
+ while (sqlite3_step_monitored(res_chart) == SQLITE_ROW) {
1333
char id[512];
1334
sprintf(id, "%s.%s", sqlite3_column_text(res_chart, 3), sqlite3_column_text(res_chart, 1));
1335
RRDSET *st = rrdset_find(host, id);
@@ -1471,7 +1495,7 @@ RRDHOST *sql_create_host_by_uuid(char *hostname)
1495
}
1496
}
1497
1474
- rc = sqlite3_step(res);
1498
+ rc = sqlite3_step_monitored(res);
1499
if (unlikely(rc != SQLITE_ROW)) {
1500
error_report("Failed to find hostname %s", hostname);
1501
goto failed;
@@ -1511,20 +1535,21 @@ void db_execute(const char *cmd)
1535
int cnt = 0;
1536
while (cnt < SQL_MAX_RETRY) {
1537
char *err_msg;
1514
- rc = sqlite3_exec(db_meta, cmd, 0, 0, &err_msg);
1538
+ rc = sqlite3_exec_monitored(db_meta, cmd, 0, 0, &err_msg);
1539
if (rc != SQLITE_OK) {
1540
error_report("Failed to execute '%s', rc = %d (%s) -- attempt %d", cmd, rc, err_msg, cnt);
1541
sqlite3_free(err_msg);
1542
if (likely(rc == SQLITE_BUSY || rc == SQLITE_LOCKED)) {
1543
usleep(SQLITE_INSERT_DELAY * USEC_PER_MS);
1544
}
1521
- else break;
1545
+ else
1546
+ break;
1547
}
1548
else
1549
break;
1550
+
1551
++cnt;
1552
}
1527
- return;
1553
}
1554
1555
void db_lock(void)
@@ -1559,7 +1584,7 @@ int file_is_migrated(char *path)
1584
return 0;
1585
}
1586
1562
- rc = sqlite3_step(res);
1587
+ rc = sqlite3_step_monitored(res);
1588
1589
if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
1590
error_report("Failed to finalize the prepared statement when checking if metadata file is migrated");
@@ -1838,7 +1863,7 @@ void sql_build_context_param_list(ONEWAYALLOC *owa, struct context_param **para
1863
uuid_t rrdeng_uuid;
1864
uuid_t chart_id;
1865
1841
- while (sqlite3_step(res) == SQLITE_ROW) {
1866
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
1867
char id[512];
1868
sprintf(id, "%s.%s", sqlite3_column_text(res, 3), sqlite3_column_text(res, 1));
1869
@@ -2242,7 +2267,7 @@ char *get_hostname_by_node_id(char *node)
2267
goto failed;
2268
}
2269
2245
- rc = sqlite3_step(res);
2270
+ rc = sqlite3_step_monitored(res);
2271
if (likely(rc == SQLITE_ROW))
2272
hostname = strdupz((char *)sqlite3_column_text(res, 0));
2273
@@ -2280,7 +2305,7 @@ int get_host_id(uuid_t *node_id, uuid_t *host_id)
2305
goto failed;
2306
}
2307
2283
- rc = sqlite3_step(res);
2308
+ rc = sqlite3_step_monitored(res);
2309
if (likely(rc == SQLITE_ROW && host_id))
2310
uuid_copy(*host_id, *((uuid_t *) sqlite3_column_blob(res, 0)));
2311
@@ -2316,7 +2341,7 @@ int get_node_id(uuid_t *host_id, uuid_t *node_id)
2341
goto failed;
2342
}
2343
2319
- rc = sqlite3_step(res);
2344
+ rc = sqlite3_step_monitored(res);
2345
if (likely(rc == SQLITE_ROW && node_id))
2346
uuid_copy(*node_id, *((uuid_t *) sqlite3_column_blob(res, 0)));
2347
@@ -2395,7 +2420,7 @@ struct node_instance_list *get_node_list(void)
2420
2421
int row = 0;
2422
char host_guid[37];
2398
- while (sqlite3_step(res) == SQLITE_ROW)
2423
+ while (sqlite3_step_monitored(res) == SQLITE_ROW)
2424
row++;
2425
2426
if (sqlite3_reset(res) != SQLITE_OK) {
@@ -2406,7 +2431,7 @@ struct node_instance_list *get_node_list(void)
2431
int max_rows = row;
2432
row = 0;
2433
rrd_rdlock();
2409
- while (sqlite3_step(res) == SQLITE_ROW) {
2434
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
2435
if (sqlite3_column_bytes(res, 0) == sizeof(uuid_t))
2436
uuid_copy(node_list[row].node_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
2437
if (sqlite3_column_bytes(res, 1) == sizeof(uuid_t)) {
@@ -2461,7 +2486,7 @@ void sql_load_node_id(RRDHOST *host)
2486
goto failed;
2487
}
2488
2464
- rc = sqlite3_step(res);
2489
+ rc = sqlite3_step_monitored(res);
2490
if (likely(rc == SQLITE_ROW)) {
2491
if (likely(sqlite3_column_bytes(res, 0) == sizeof(uuid_t)))
2492
set_host_node_id(host, (uuid_t *)sqlite3_column_blob(res, 0));
@@ -2497,7 +2522,7 @@ void sql_build_host_system_info(uuid_t *host_id, struct rrdhost_system_info *sys
2522
goto skip_loading;
2523
}
2524
2500
- while (sqlite3_step(res) == SQLITE_ROW) {
2525
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
2526
rrdhost_set_system_info_variable(system_info, (char *) sqlite3_column_text(res, 0),
2527
(char *) sqlite3_column_text(res, 1));
2528
}
@@ -2681,7 +2706,7 @@ DICTIONARY *sql_load_host_labels(uuid_t *host_id)
2706
2707
labels = rrdlabels_create();
2708
2684
- while (sqlite3_step(res) == SQLITE_ROW) {
2709
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
2710
rrdlabels_add(
2711
labels,
2712
(const char *)sqlite3_column_text(res, 0),
database/sqlite/sqlite_functions.h
+9
@@ -58,6 +58,15 @@ typedef enum db_check_action_type {
58
return 1; \
59
}
60
61
+extern SQLITE_API int sqlite3_step_monitored(sqlite3_stmt *stmt);
62
+extern SQLITE_API int sqlite3_exec_monitored(
63
+ sqlite3 *db, /* An open database */
64
+ const char *sql, /* SQL to be evaluated */
65
+ int (*callback)(void*,int,char**,char**), /* Callback function */
66
+ void *data, /* 1st argument to callback */
67
+ char **errmsg /* Error msg written here */
68
+ );
69
+
70
extern int sql_init_database(db_check_action_type_t rebuild, int memory);
71
extern void sql_close_database(void);
72
extern int bind_text_null(sqlite3_stmt *res, int position, const char *text, bool can_be_null);
database/sqlite/sqlite_health.c
+6
-6
@@ -24,7 +24,7 @@ int sql_create_health_log_table(RRDHOST *host) {
24
25
snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CREATE_HEALTH_LOG_TABLE(uuid_str));
26
27
- rc = sqlite3_exec(db_meta, command, 0, 0, &err_msg);
27
+ rc = sqlite3_exec_monitored(db_meta, command, 0, 0, &err_msg);
28
if (rc != SQLITE_OK) {
29
error_report("HEALTH [%s]: SQLite error during creation of health log table, rc = %d (%s)", host->hostname, rc, err_msg);
30
sqlite3_free(err_msg);
@@ -389,7 +389,7 @@ void sql_health_alarm_log_cleanup(RRDHOST *host) {
389
return;
390
}
391
392
- rc = sqlite3_step(res);
392
+ rc = sqlite3_step_monitored(res);
393
if (unlikely(rc != SQLITE_DONE))
394
error_report("Failed to cleanup health log table, rc = %d", rc);
395
@@ -428,7 +428,7 @@ void sql_health_alarm_log_count(RRDHOST *host) {
428
return;
429
}
430
431
- rc = sqlite3_step(res);
431
+ rc = sqlite3_step_monitored(res);
432
if (likely(rc == SQLITE_ROW))
433
host->health_log_entries_written = (size_t) sqlite3_column_int64(res, 0);
434
@@ -556,7 +556,7 @@ uint32_t sql_get_max_unique_id (char *uuid_str)
556
return 0;
557
}
558
559
- while (sqlite3_step(res) == SQLITE_ROW) {
559
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
560
max_unique_id = (uint32_t) sqlite3_column_int64(res, 0);
561
}
562
@@ -584,7 +584,7 @@ void sql_check_removed_alerts_state(char *uuid_str)
584
return;
585
}
586
587
- while (sqlite3_step(res) == SQLITE_ROW) {
587
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
588
status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
589
unique_id = (uint32_t) sqlite3_column_int64(res, 1);
590
alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
@@ -634,7 +634,7 @@ void sql_health_alarm_log_load(RRDHOST *host) {
634
635
netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
636
637
- while (sqlite3_step(res) == SQLITE_ROW) {
637
+ while (sqlite3_step_monitored(res) == SQLITE_ROW) {
638
ALARM_ENTRY *ae = NULL;
639
640
// check that we have valid ids
ml/Database.cc
+1
-1
@@ -105,7 +105,7 @@ Database::Database(const std::string &Path) {
105
106
// Create anomaly events table if it does not exist.
107
char *ErrMsg;
108
- RC = sqlite3_exec(Conn, SQL_CREATE_ANOMALIES_TABLE, nullptr, nullptr, &ErrMsg);
108
+ RC = sqlite3_exec_monitored(Conn, SQL_CREATE_ANOMALIES_TABLE, nullptr, nullptr, &ErrMsg);
109
if (RC == SQLITE_OK)
110
return;
111
ml/Database.h
+1
-1
@@ -32,7 +32,7 @@ public:
32
}
33
34
while (true) {
35
- switch (int RC = sqlite3_step(ParsedStmt)) {
35
+ switch (int RC = sqlite3_step_monitored(ParsedStmt)) {
36
case SQLITE_BUSY: case SQLITE_LOCKED:
37
usleep(SQLITE_INSERT_DELAY * USEC_PER_MS);
38
continue;