@cryptotaxi247 / netdata-1 / commits / aa430dc76

Metadata cleanup improvements (#15462)

* Cleanup improvements Cleanup for charts and chart labels Code Formatting Run health cleanup every hour Generic cleanup function with appropriate callbacks * Cleanup and better logging * Start metadata cleanup job faster * Improve logging message * Do cleanup after storing metadata as needed * First check after 30 minutes * First check after 30 minutes Cleanup

Stelios Fragkakis committed Aug 25, 2023 at 12:20 UTC aa430dc76a17482e038c2aa9d9a8ed0288c2524b
2 files changed +346 -91
database/sqlite/sqlite_functions.c
-1
@@ -63,7 +63,6 @@ const char *database_config[] = {
63 };
64
65 const char *database_cleanup[] = {
66 - "DELETE FROM chart WHERE chart_id NOT IN (SELECT chart_id FROM dimension);",
66 "DELETE FROM host WHERE host_id NOT IN (SELECT host_id FROM chart);",
67 "DELETE FROM node_instance WHERE host_id NOT IN (SELECT host_id FROM host);",
68 "DELETE FROM host_info WHERE host_id NOT IN (SELECT host_id FROM host);",
database/sqlite/sqlite_metadata.c
+346 -90
@@ -36,6 +36,8 @@
36 "VALUES (@dim_id, @chart_id, @id, @name, @multiplier, @divisor, @algorithm, @options);"
37
38 #define SELECT_DIMENSION_LIST "SELECT dim_id, rowid FROM dimension WHERE rowid > @row_id"
39 +#define SELECT_CHART_LIST "SELECT chart_id, rowid FROM chart WHERE rowid > @row_id"
40 +#define SELECT_CHART_LABEL_LIST "SELECT chart_id, rowid FROM chart_label WHERE rowid > @row_id"
41
42 #define SQL_STORE_HOST_SYSTEM_INFO_VALUES "INSERT OR REPLACE INTO host_info (host_id, system_key, system_value, date_created) VALUES " \
43 "(@uuid, @name, @value, unixepoch())"
@@ -47,13 +49,18 @@
49
50 #define METADATA_CMD_Q_MAX_SIZE (1024) // Max queue size; callers will block until there is room
51 #define METADATA_MAINTENANCE_FIRST_CHECK (1800) // Maintenance first run after agent startup in seconds
50 -#define METADATA_MAINTENANCE_RETRY (60) // Retry run if already running or last run did actual work
51 -#define METADATA_MAINTENANCE_INTERVAL (3600) // Repeat maintenance after latest successful
52 +#define METADATA_MAINTENANCE_REPEAT (60) // Repeat if last run for dimensions, charts, labels needs more work
53 +#define METADATA_HEALTH_LOG_INTERVAL (3600) // Repeat maintenance for health
54 +#define METADATA_DIM_CHECK_INTERVAL (3600) // Repeat maintenance for dimensions
55 +#define METADATA_CHART_CHECK_INTERVAL (3600) // Repeat maintenance for charts
56 +#define METADATA_LABEL_CHECK_INTERVAL (3600) // Repeat maintenance for labels
57 +#define METADATA_RUNTIME_THRESHOLD (5) // Run time threshold for cleanup task
58
59 #define METADATA_HOST_CHECK_FIRST_CHECK (5) // First check for pending metadata
60 #define METADATA_HOST_CHECK_INTERVAL (30) // Repeat check for pending metadata
61 #define METADATA_HOST_CHECK_IMMEDIATE (5) // Repeat immediate run because we have more metadata to write
56 -
62 +#define METADATA_FREE_PAGES_THRESHOLD_PC (5) // Percentage of free pages to trigger vacuum
63 +#define METADATA_FREE_PAGES_VACUUM_PC (10) // Percentage of free pages to vacuum
64 #define MAX_METADATA_CLEANUP (500) // Maximum metadata write operations (e.g deletes before retrying)
65 #define METADATA_MAX_BATCH_SIZE (512) // Maximum commands to execute before running the event loop
66
@@ -87,23 +94,18 @@ struct metadata_database_cmdqueue {
94 };
95
96 typedef enum {
90 - METADATA_FLAG_CLEANUP = (1 << 0), // Cleanup is running
91 - METADATA_FLAG_SCANNING_HOSTS = (1 << 1), // Scanning of hosts in worker thread
92 - METADATA_FLAG_SHUTDOWN = (1 << 2), // Shutting down
97 + METADATA_FLAG_PROCESSING = (1 << 0), // store or cleanup
98 + METADATA_FLAG_SHUTDOWN = (1 << 1), // Shutting down
99 } METADATA_FLAG;
100
95 -#define METADATA_WORKER_BUSY (METADATA_FLAG_CLEANUP | METADATA_FLAG_SCANNING_HOSTS)
96 -
101 struct metadata_wc {
102 uv_thread_t thread;
103 uv_loop_t *loop;
104 uv_async_t async;
105 uv_timer_t timer_req;
102 - time_t check_metadata_after;
103 - time_t check_hosts_after;
106 + time_t metadata_check_after;
107 volatile unsigned queue_size;
108 METADATA_FLAG flags;
106 - uint64_t row_id;
109 struct completion init_complete;
110 /* FIFO command queue */
111 uv_mutex_t cmd_mutex;
@@ -252,7 +254,7 @@ failed:
254 return rc != SQLITE_DONE;
255 }
256
255 -static void delete_dimension_uuid(uuid_t *dimension_uuid)
257 +static void delete_dimension_uuid(uuid_t *dimension_uuid, bool flag __maybe_unused)
258 {
259 static __thread sqlite3_stmt *res = NULL;
260 int rc;
@@ -650,7 +652,7 @@ bind_fail:
652 return 1;
653 }
654
653 -static bool dimension_can_be_deleted(uuid_t *dim_uuid __maybe_unused)
655 +static bool dimension_can_be_deleted(uuid_t *dim_uuid __maybe_unused, bool flag __maybe_unused)
656 {
657 #ifdef ENABLE_DBENGINE
658 if(dbengine_enabled) {
@@ -675,8 +677,134 @@ static bool dimension_can_be_deleted(uuid_t *dim_uuid __maybe_unused)
677 #endif
678 }
679
680 +int get_pragma_value(sqlite3 *database, const char *sql)
681 +{
682 + sqlite3_stmt *res = NULL;
683 + int rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
684 + if (unlikely(rc != SQLITE_OK))
685 + return -1;
686 +
687 + int result = -1;
688 + rc = sqlite3_step_monitored(res);
689 + if (likely(rc == SQLITE_ROW))
690 + result = sqlite3_column_int(res, 0);
691 +
692 + rc = sqlite3_finalize(res);
693 + (void) rc;
694 +
695 + return result;
696 +}
697 +
698 +
699 +int get_free_page_count(sqlite3 *database)
700 +{
701 + return get_pragma_value(database, "PRAGMA freelist_count");
702 +}
703 +
704 +int get_database_page_count(sqlite3 *database)
705 +{
706 + return get_pragma_value(database, "PRAGMA page_count");
707 +}
708 +
709 +static bool run_cleanup_loop(
710 + sqlite3_stmt *res,
711 + struct metadata_wc *wc,
712 + bool (*check_cb)(uuid_t *uuid, bool check_flag __maybe_unused),
713 + void (*action_cb)(uuid_t *uuid, bool action_flag __maybe_unused),
714 + uint32_t *total_checked,
715 + uint32_t *total_deleted,
716 + uint32_t run_threshold,
717 + uint32_t cleanup_threshold,
718 + uint64_t *row_id,
719 + bool check_flag,
720 + bool action_flag)
721 +{
722 +
723 + if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
724 + return true;
725 +
726 + int rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) *row_id);
727 + if (unlikely(rc != SQLITE_OK))
728 + return true;
729 +
730 + time_t start_running = now_monotonic_sec();
731 + bool time_expired = false;
732 + while (!time_expired && sqlite3_step_monitored(res) == SQLITE_ROW && *total_deleted < cleanup_threshold) {
733 + if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
734 + break;
735 +
736 + *row_id = sqlite3_column_int64(res, 1);
737 + rc = check_cb((uuid_t *)sqlite3_column_blob(res, 0), check_flag);
738 +
739 + if (rc == true) {
740 + action_cb((uuid_t *)sqlite3_column_blob(res, 0), action_flag);
741 + (*total_deleted)++;
742 + }
743 +
744 + (*total_checked)++;
745 + time_expired = ((now_monotonic_sec() - start_running) > run_threshold);
746 + }
747 + return time_expired;
748 +}
749 +
750 +
751 +#define SQL_CHECK_CHART_EXISTENCE_IN_DIMENSION "SELECT count(1) FROM dimension WHERE chart_id = @chart_id"
752 +#define SQL_CHECK_CHART_EXISTENCE_IN_CHART "SELECT count(1) FROM chart WHERE chart_id = @chart_id"
753 +
754 +static bool chart_can_be_deleted(uuid_t *chart_uuid, bool check_in_dimension)
755 +{
756 + int rc, result = 1;
757 + sqlite3_stmt *res = NULL;
758 +
759 + if (check_in_dimension)
760 + rc = sqlite3_prepare_v2(db_meta, SQL_CHECK_CHART_EXISTENCE_IN_DIMENSION, -1, &res, 0);
761 + else
762 + rc = sqlite3_prepare_v2(db_meta, SQL_CHECK_CHART_EXISTENCE_IN_CHART, -1, &res, 0);
763 + if (unlikely(rc != SQLITE_OK)) {
764 + error_report("Failed to prepare statement to check for chart existence, rc = %d", rc);
765 + return 0;
766 + }
767 +
768 + rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
769 + if (unlikely(rc != SQLITE_OK)) {
770 + error_report("Failed to bind chart uuid parameter, rc = %d", rc);
771 + goto skip;
772 + }
773 +
774 + rc = sqlite3_step_monitored(res);
775 + if (likely(rc == SQLITE_ROW))
776 + result = sqlite3_column_int(res, 0);
777 +
778 +skip:
779 + rc = sqlite3_finalize(res);
780 + if (unlikely(rc != SQLITE_OK))
781 + error_report("Failed to finalize statement that checks chart uuid existence rc = %d", rc);
782 + return result == 0;
783 +}
784 +
785 +#define SQL_DELETE_CHART_BY_UUID "DELETE FROM chart WHERE chart_id = @chart_id"
786 +#define SQL_DELETE_CHART_LABEL_BY_UUID "DELETE FROM chart_label WHERE chart_id = @chart_id"
787 +
788 +static void delete_chart_uuid(uuid_t(*chart_uuid), bool label_only)
789 +{
790 + if (label_only == false)
791 + (void) exec_statement_with_uuid(SQL_DELETE_CHART_BY_UUID, chart_uuid);
792 + (void) exec_statement_with_uuid(SQL_DELETE_CHART_LABEL_BY_UUID, chart_uuid);
793 +}
794 +
795 static void check_dimension_metadata(struct metadata_wc *wc)
796 {
797 + static time_t next_execution_t = 0;
798 + static uint64_t last_row_id = 0;
799 +
800 + time_t now = now_realtime_sec();
801 +
802 + if (!next_execution_t)
803 + next_execution_t = now + METADATA_MAINTENANCE_FIRST_CHECK;
804 +
805 + if (next_execution_t && next_execution_t > now)
806 + return;
807 +
808 int rc;
809 sqlite3_stmt *res = NULL;
810
@@ -686,47 +814,178 @@ static void check_dimension_metadata(struct metadata_wc *wc)
814 return;
815 }
816
689 - rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) wc->row_id);
817 + uint32_t total_checked = 0;
818 + uint32_t total_deleted = 0;
819 +
820 + internal_error(true, "METADATA: Checking dimensions starting after row %"PRIu64, last_row_id);
821 +
822 + bool runtime_exceeded = run_cleanup_loop(
823 + res,
824 + wc,
825 + dimension_can_be_deleted,
826 + delete_dimension_uuid,
827 + &total_checked,
828 + &total_deleted,
829 + METADATA_RUNTIME_THRESHOLD,
830 + MAX_METADATA_CLEANUP,
831 + &last_row_id,
832 + false,
833 + false);
834 +
835 + now = now_realtime_sec();
836 + if (total_deleted > 0 || runtime_exceeded)
837 + next_execution_t = now + METADATA_MAINTENANCE_REPEAT;
838 + else {
839 + last_row_id = 0;
840 + next_execution_t = now + METADATA_DIM_CHECK_INTERVAL;
841 + }
842 +
843 + netdata_log_info(
844 + "METADATA: Dimensions checked %u, deleted %u. Checks will %s in %lld seconds",
845 + total_checked,
846 + total_deleted,
847 + last_row_id ? "resume" : "restart",
848 + (long long)(next_execution_t - now));
849 +
850 + rc = sqlite3_finalize(res);
851 + if (unlikely(rc != SQLITE_OK))
852 + error_report("Failed to finalize the prepared statement to check dimensions");
853 +}
854 +
855 +static void check_chart_metadata(struct metadata_wc *wc)
856 +{
857 + static time_t next_execution_t = 0;
858 + static uint64_t last_row_id = 0;
859 +
860 + time_t now = now_realtime_sec();
861 +
862 + if (!next_execution_t)
863 + next_execution_t = now + METADATA_MAINTENANCE_FIRST_CHECK;
864 +
865 + if (next_execution_t && next_execution_t > now)
866 + return;
867 +
868 + sqlite3_stmt *res = NULL;
869 +
870 + int rc = sqlite3_prepare_v2(db_meta, SELECT_CHART_LIST, -1, &res, 0);
871 if (unlikely(rc != SQLITE_OK)) {
691 - error_report("Failed to row parameter");
692 - goto skip_run;
872 + error_report("Failed to prepare statement to fetch charts");
873 + return;
874 }
875
876 uint32_t total_checked = 0;
877 uint32_t total_deleted= 0;
697 - uint64_t last_row_id = wc->row_id;
878
699 - netdata_log_info("METADATA: Checking dimensions starting after row %"PRIu64, wc->row_id);
879 + internal_error(true, "METADATA: Checking charts starting after row %"PRIu64, last_row_id);
880 +
881 + bool runtime_exceeded = run_cleanup_loop(
882 + res,
883 + wc,
884 + chart_can_be_deleted,
885 + delete_chart_uuid,
886 + &total_checked,
887 + &total_deleted,
888 + METADATA_RUNTIME_THRESHOLD,
889 + MAX_METADATA_CLEANUP,
890 + &last_row_id,
891 + true,
892 + false);
893 +
894 + now = now_realtime_sec();
895 + if (total_deleted > 0 || runtime_exceeded)
896 + next_execution_t = now + METADATA_MAINTENANCE_REPEAT;
897 + else {
898 + last_row_id = 0;
899 + next_execution_t = now + METADATA_CHART_CHECK_INTERVAL;
900 + }
901
701 - while (sqlite3_step_monitored(res) == SQLITE_ROW && total_deleted < MAX_METADATA_CLEANUP) {
702 - if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
703 - break;
902 + netdata_log_info(
903 + "METADATA: Charts checked %u, deleted %u. Checks will %s in %lld seconds",
904 + total_checked,
905 + total_deleted,
906 + last_row_id ? "resume" : "restart",
907 + (long long)(next_execution_t - now));
908 +
909 + rc = sqlite3_finalize(res);
910 + if (unlikely(rc != SQLITE_OK))
911 + error_report("Failed to finalize the prepared statement when reading charts");
912 +}
913 +
914 +static void check_label_metadata(struct metadata_wc *wc)
915 +{
916 + static time_t next_execution_t = 0;
917 + static uint64_t last_row_id = 0;
918
705 - last_row_id = sqlite3_column_int64(res, 1);
706 - rc = dimension_can_be_deleted((uuid_t *)sqlite3_column_blob(res, 0));
707 - if (rc == true) {
708 - delete_dimension_uuid((uuid_t *)sqlite3_column_blob(res, 0));
709 - total_deleted++;
710 - }
711 - total_checked++;
712 - }
713 - wc->row_id = last_row_id;
919 time_t now = now_realtime_sec();
715 - if (total_deleted > 0) {
716 - wc->check_metadata_after = now + METADATA_MAINTENANCE_RETRY;
717 - } else
718 - wc->row_id = 0;
719 - netdata_log_info("METADATA: Checked %u, deleted %u -- will resume after row %"PRIu64" in %lld seconds", total_checked, total_deleted, wc->row_id,
720 - (long long)(wc->check_metadata_after - now));
721 -
722 -skip_run:
920 +
921 + if (!next_execution_t)
922 + next_execution_t = now + METADATA_MAINTENANCE_FIRST_CHECK;
923 +
924 + if (next_execution_t && next_execution_t > now)
925 + return;
926 +
927 + int rc;
928 + sqlite3_stmt *res = NULL;
929 +
930 + rc = sqlite3_prepare_v2(db_meta, SELECT_CHART_LABEL_LIST, -1, &res, 0);
931 + if (unlikely(rc != SQLITE_OK)) {
932 + error_report("Failed to prepare statement to fetch charts");
933 + return;
934 + }
935 +
936 + uint32_t total_checked = 0;
937 + uint32_t total_deleted= 0;
938 +
939 + internal_error(true,"METADATA: Checking charts labels starting after row %"PRIu64, last_row_id);
940 +
941 + bool runtime_exceeded = run_cleanup_loop(
942 + res,
943 + wc,
944 + chart_can_be_deleted,
945 + delete_chart_uuid,
946 + &total_checked,
947 + &total_deleted,
948 + METADATA_RUNTIME_THRESHOLD,
949 + MAX_METADATA_CLEANUP,
950 + &last_row_id,
951 + false,
952 + true);
953 +
954 + now = now_realtime_sec();
955 + if (total_deleted > 0 || runtime_exceeded)
956 + next_execution_t = now + METADATA_MAINTENANCE_REPEAT;
957 + else {
958 + last_row_id = 0;
959 + next_execution_t = now + METADATA_LABEL_CHECK_INTERVAL;
960 + }
961 +
962 + netdata_log_info(
963 + "METADATA: Chart labels checked %u, deleted %u. Checks will %s in %lld seconds",
964 + total_checked,
965 + total_deleted,
966 + last_row_id ? "resume" : "restart",
967 + (long long)(next_execution_t - now));
968 +
969 rc = sqlite3_finalize(res);
970 if (unlikely(rc != SQLITE_OK))
725 - error_report("Failed to finalize the prepared statement when reading dimensions");
971 + error_report("Failed to finalize the prepared statement when checking charts");
972 }
973
728 -static void cleanup_health_log(void)
974 +
975 +static void cleanup_health_log(struct metadata_wc *wc)
976 {
977 + static time_t next_execution_t = 0;
978 +
979 + time_t now = now_realtime_sec();
980 +
981 + if (!next_execution_t)
982 + next_execution_t = now + METADATA_MAINTENANCE_FIRST_CHECK;
983 +
984 + if (next_execution_t && next_execution_t > now)
985 + return;
986 +
987 + next_execution_t = now + METADATA_HEALTH_LOG_INTERVAL;
988 +
989 RRDHOST *host;
990
991 bool is_claimed = claimed();
@@ -734,8 +993,16 @@ static void cleanup_health_log(void)
993 if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
994 continue;
995 sql_health_alarm_log_cleanup(host, is_claimed);
996 + if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
997 + break;
998 }
999 dfe_done(host);
1000 +
1001 + if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
1002 + return;
1003 +
1004 + (void) db_execute(db_meta,"DELETE FROM health_log WHERE host_id NOT IN (SELECT host_id FROM host)");
1005 + (void) db_execute(db_meta,"DELETE FROM health_log_detail WHERE health_log_id NOT IN (SELECT health_log_id FROM health_log)");
1006 }
1007
1008 //
@@ -867,37 +1134,40 @@ static void timer_cb(uv_timer_t* handle)
1134
1135 time_t now = now_realtime_sec();
1136
870 - if (wc->check_metadata_after && wc->check_metadata_after < now) {
871 - cmd.opcode = METADATA_MAINTENANCE;
872 - if (!metadata_enq_cmd_noblock(wc, &cmd))
873 - wc->check_metadata_after = now + METADATA_MAINTENANCE_INTERVAL;
874 - }
875 -
876 - if (wc->check_hosts_after && wc->check_hosts_after < now) {
1137 + if (wc->metadata_check_after && wc->metadata_check_after < now) {
1138 cmd.opcode = METADATA_SCAN_HOSTS;
1139 if (!metadata_enq_cmd_noblock(wc, &cmd))
879 - wc->check_hosts_after = now + METADATA_HOST_CHECK_INTERVAL;
1140 + wc->metadata_check_after = now + METADATA_HOST_CHECK_INTERVAL;
1141 }
1142 }
1143
883 -static void after_metadata_cleanup(uv_work_t *req, int status)
1144 +void run_metadata_cleanup(struct metadata_wc *wc)
1145 {
885 - UNUSED(status);
1146 + if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
1147 + return;
1148
887 - struct metadata_wc *wc = req->data;
888 - metadata_flag_clear(wc, METADATA_FLAG_CLEANUP);
889 -}
1149 + check_dimension_metadata(wc);
1150 + check_chart_metadata(wc);
1151 + check_label_metadata(wc);
1152 + cleanup_health_log(wc);
1153
891 -static void start_metadata_cleanup(uv_work_t *req)
892 -{
893 - register_libuv_worker_jobs();
1154 + if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SHUTDOWN)))
1155 + return;
1156 +
1157 + int free_pages = get_free_page_count(db_meta);
1158 + int total_pages = get_database_page_count(db_meta);
1159 +
1160 + if (free_pages > (total_pages * METADATA_FREE_PAGES_THRESHOLD_PC / 100)) {
1161 +
1162 + int do_free_pages = (int) (free_pages * METADATA_FREE_PAGES_VACUUM_PC / 100);
1163 + netdata_log_info("METADATA: Freeing %d database pages", do_free_pages);
1164 +
1165 + char sql[128];
1166 + snprintfz(sql, 127, "PRAGMA incremental_vacuum(%d)", do_free_pages);
1167 + (void) db_execute(db_meta, sql);
1168 + }
1169
895 - worker_is_busy(UV_EVENT_METADATA_CLEANUP);
896 - struct metadata_wc *wc = req->data;
897 - check_dimension_metadata(wc);
898 - cleanup_health_log();
1170 (void) sqlite3_wal_checkpoint(db_meta, NULL);
900 - worker_is_idle();
1171 }
1172
1173 struct scan_metadata_payload {
@@ -1029,7 +1299,7 @@ static void after_metadata_hosts(uv_work_t *req, int status __maybe_unused)
1299 struct scan_metadata_payload *data = req->data;
1300 struct metadata_wc *wc = data->wc;
1301
1032 - metadata_flag_clear(wc, METADATA_FLAG_SCANNING_HOSTS);
1302 + metadata_flag_clear(wc, METADATA_FLAG_PROCESSING);
1303 internal_error(true, "METADATA: scanning hosts complete");
1304 if (unlikely(data->completion)) {
1305 completion_mark_complete(data->completion);
@@ -1217,9 +1487,11 @@ static void start_metadata_hosts(uv_work_t *req __maybe_unused)
1487 (double)(all_ended_ut - all_started_ut) / USEC_PER_MS);
1488
1489 if (unlikely(run_again))
1220 - wc->check_hosts_after = now_realtime_sec() + METADATA_HOST_CHECK_IMMEDIATE;
1221 - else
1222 - wc->check_hosts_after = now_realtime_sec() + METADATA_HOST_CHECK_INTERVAL;
1490 + wc->metadata_check_after = now_realtime_sec() + METADATA_HOST_CHECK_IMMEDIATE;
1491 + else {
1492 + wc->metadata_check_after = now_realtime_sec() + METADATA_HOST_CHECK_INTERVAL;
1493 + run_metadata_cleanup(wc);
1494 + }
1495 worker_is_idle();
1496 }
1497
@@ -1239,10 +1511,8 @@ static void metadata_event_loop(void *arg)
1511 unsigned cmd_batch_size;
1512 struct metadata_wc *wc = arg;
1513 enum metadata_opcode opcode;
1242 - uv_work_t metadata_cleanup_worker;
1514
1515 uv_thread_set_name_np(wc->thread, "METASYNC");
1245 -// service_register(SERVICE_THREAD_TYPE_EVENT_LOOP, NULL, NULL, NULL, true);
1516 loop = wc->loop = mallocz(sizeof(uv_loop_t));
1517 ret = uv_loop_init(loop);
1518 if (ret) {
@@ -1270,19 +1540,16 @@ static void metadata_event_loop(void *arg)
1540
1541 struct metadata_cmd cmd;
1542 memset(&cmd, 0, sizeof(cmd));
1273 - metadata_flag_clear(wc, METADATA_FLAG_CLEANUP);
1274 - metadata_flag_clear(wc, METADATA_FLAG_SCANNING_HOSTS);
1543 + metadata_flag_clear(wc, METADATA_FLAG_PROCESSING);
1544
1276 - wc->check_metadata_after = now_realtime_sec() + METADATA_MAINTENANCE_FIRST_CHECK;
1277 - wc->check_hosts_after = now_realtime_sec() + METADATA_HOST_CHECK_FIRST_CHECK;
1545 + wc->metadata_check_after = now_realtime_sec() + METADATA_HOST_CHECK_FIRST_CHECK;
1546
1547 int shutdown = 0;
1280 - wc->row_id = 0;
1548 completion_mark_complete(&wc->init_complete);
1549 BUFFER *work_buffer = buffer_create(1024, &netdata_buffers_statistics.buffers_sqlite);
1550 struct scan_metadata_payload *data;
1551
1285 - while (shutdown == 0 || (wc->flags & METADATA_WORKER_BUSY)) {
1552 + while (shutdown == 0 || (wc->flags & METADATA_FLAG_PROCESSING)) {
1553 uuid_t *uuid;
1554 RRDHOST *host = NULL;
1555
@@ -1320,8 +1587,8 @@ static void metadata_event_loop(void *arg)
1587 }
1588 case METADATA_DEL_DIMENSION:
1589 uuid = (uuid_t *) cmd.param[0];
1323 - if (likely(dimension_can_be_deleted(uuid)))
1324 - delete_dimension_uuid(uuid);
1590 + if (likely(dimension_can_be_deleted(uuid, false)))
1591 + delete_dimension_uuid(uuid, false);
1592 freez(uuid);
1593 break;
1594 case METADATA_STORE_CLAIM_ID:
@@ -1334,7 +1601,7 @@ static void metadata_event_loop(void *arg)
1601 store_host_and_system_info(host, NULL);
1602 break;
1603 case METADATA_SCAN_HOSTS:
1337 - if (unlikely(metadata_flag_check(wc, METADATA_FLAG_SCANNING_HOSTS)))
1604 + if (unlikely(metadata_flag_check(wc, METADATA_FLAG_PROCESSING)))
1605 break;
1606
1607 if (unittest_running)
@@ -1353,7 +1620,7 @@ static void metadata_event_loop(void *arg)
1620 else
1621 data->max_count = 5000;
1622
1356 - metadata_flag_set(wc, METADATA_FLAG_SCANNING_HOSTS);
1623 + metadata_flag_set(wc, METADATA_FLAG_PROCESSING);
1624 if (unlikely(
1625 uv_queue_work(loop,&data->request,
1626 start_metadata_hosts,
@@ -1361,7 +1628,7 @@ static void metadata_event_loop(void *arg)
1628 // Failed to launch worker -- let the event loop handle completion
1629 cmd.completion = data->completion;
1630 freez(data);
1364 - metadata_flag_clear(wc, METADATA_FLAG_SCANNING_HOSTS);
1631 + metadata_flag_clear(wc, METADATA_FLAG_PROCESSING);
1632 }
1633 break;
1634 case METADATA_LOAD_HOST_CONTEXT:;
@@ -1377,17 +1644,6 @@ static void metadata_event_loop(void *arg)
1644 freez(data);
1645 }
1646 break;
1380 - case METADATA_MAINTENANCE:
1381 - if (unlikely(metadata_flag_check(wc, METADATA_FLAG_CLEANUP)))
1382 - break;
1383 -
1384 - metadata_cleanup_worker.data = wc;
1385 - metadata_flag_set(wc, METADATA_FLAG_CLEANUP);
1386 - if (unlikely(
1387 - uv_queue_work(loop, &metadata_cleanup_worker, start_metadata_cleanup, after_metadata_cleanup))) {
1388 - metadata_flag_clear(wc, METADATA_FLAG_CLEANUP);
1389 - }
1390 - break;
1647 case METADATA_UNITTEST:;
1648 struct thread_unittest *tu = (struct thread_unittest *) cmd.param[0];
1649 sleep_usec(1000); // processing takes 1ms
@@ -1461,7 +1717,7 @@ void metadata_sync_shutdown_prepare(void)
1717
1718 netdata_log_info("METADATA: Sending a scan host command");
1719 uint32_t max_wait_iterations = 2000;
1464 - while (unlikely(metadata_flag_check(&metasync_worker, METADATA_FLAG_SCANNING_HOSTS)) && max_wait_iterations--) {
1720 + while (unlikely(metadata_flag_check(&metasync_worker, METADATA_FLAG_PROCESSING)) && max_wait_iterations--) {
1721 if (max_wait_iterations == 1999)
1722 netdata_log_info("METADATA: Current worker is running; waiting to finish");
1723 sleep_usec(1000);