sqlite_context.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "sqlite_functions.h"
  3. #include "sqlite_context.h"
  4. #include "sqlite_db_migration.h"
  5. #define DB_CONTEXT_METADATA_VERSION 1
  6. const char *database_context_config[] = {
  7. "CREATE TABLE IF NOT EXISTS context (host_id BLOB, id TEXT NOT NULL, version INT NOT NULL, title TEXT NOT NULL, " \
  8. "chart_type TEXT NOT NULL, unit TEXT NOT NULL, priority INT NOT NULL, first_time_t INT NOT NULL, "
  9. "last_time_t INT NOT NULL, deleted INT NOT NULL, "
  10. "family TEXT, PRIMARY KEY (host_id, id));",
  11. NULL
  12. };
  13. const char *database_context_cleanup[] = {
  14. "VACUUM;",
  15. NULL
  16. };
  17. sqlite3 *db_context_meta = NULL;
  18. /*
  19. * Initialize the SQLite database
  20. * Return 0 on success
  21. */
  22. int sql_init_context_database(int memory)
  23. {
  24. char sqlite_database[FILENAME_MAX + 1];
  25. int rc;
  26. if (likely(!memory))
  27. snprintfz(sqlite_database, FILENAME_MAX, "%s/context-meta.db", netdata_configured_cache_dir);
  28. else
  29. strcpy(sqlite_database, ":memory:");
  30. rc = sqlite3_open(sqlite_database, &db_context_meta);
  31. if (rc != SQLITE_OK) {
  32. error_report("Failed to initialize database at %s, due to \"%s\"", sqlite_database, sqlite3_errstr(rc));
  33. sqlite3_close(db_context_meta);
  34. db_context_meta = NULL;
  35. return 1;
  36. }
  37. netdata_log_info("SQLite database %s initialization", sqlite_database);
  38. char buf[1024 + 1] = "";
  39. const char *list[2] = { buf, NULL };
  40. int target_version = DB_CONTEXT_METADATA_VERSION;
  41. if (likely(!memory))
  42. target_version = perform_context_database_migration(db_context_meta, DB_CONTEXT_METADATA_VERSION);
  43. if (configure_sqlite_database(db_context_meta, target_version))
  44. return 1;
  45. if (likely(!memory))
  46. snprintfz(buf, 1024, "ATTACH DATABASE \"%s/netdata-meta.db\" as meta;", netdata_configured_cache_dir);
  47. else
  48. snprintfz(buf, 1024, "ATTACH DATABASE ':memory:' as meta;");
  49. if(init_database_batch(db_context_meta, list)) return 1;
  50. if (init_database_batch(db_context_meta, &database_context_config[0]))
  51. return 1;
  52. if (init_database_batch(db_context_meta, &database_context_cleanup[0]))
  53. return 1;
  54. return 0;
  55. }
  56. /*
  57. * Close the sqlite database
  58. */
  59. void sql_close_context_database(void)
  60. {
  61. int rc;
  62. if (unlikely(!db_context_meta))
  63. return;
  64. netdata_log_info("Closing context SQLite database");
  65. rc = sqlite3_close_v2(db_context_meta);
  66. if (unlikely(rc != SQLITE_OK))
  67. error_report("Error %d while closing the context SQLite database, %s", rc, sqlite3_errstr(rc));
  68. }
  69. //
  70. // Fetching data
  71. //
  72. #define CTX_GET_CHART_LIST "SELECT c.chart_id, c.type||'.'||c.id, c.name, c.context, c.title, c.unit, c.priority, " \
  73. "c.update_every, c.chart_type, c.family FROM meta.chart c WHERE c.host_id = @host_id and c.chart_id is not null; "
  74. void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, void *), void *data)
  75. {
  76. int rc;
  77. static __thread sqlite3_stmt *res = NULL;
  78. if (unlikely(!host_uuid)) {
  79. internal_error(true, "Requesting context chart list without host_id");
  80. return;
  81. }
  82. if (unlikely(!res)) {
  83. rc = prepare_statement(db_context_meta, CTX_GET_CHART_LIST, &res);
  84. if (rc != SQLITE_OK) {
  85. error_report("Failed to prepare statement to fetch chart list");
  86. return;
  87. }
  88. }
  89. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  90. if (unlikely(rc != SQLITE_OK)) {
  91. error_report("Failed to bind host_id to fetch the chart list");
  92. goto skip_load;
  93. }
  94. SQL_CHART_DATA chart_data = { 0 };
  95. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  96. uuid_copy(chart_data.chart_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
  97. chart_data.id = (char *) sqlite3_column_text(res, 1);
  98. chart_data.name = (char *) sqlite3_column_text(res, 2);
  99. chart_data.context = (char *) sqlite3_column_text(res, 3);
  100. chart_data.title = (char *) sqlite3_column_text(res, 4);
  101. chart_data.units = (char *) sqlite3_column_text(res, 5);
  102. chart_data.priority = sqlite3_column_int(res, 6);
  103. chart_data.update_every = sqlite3_column_int(res, 7);
  104. chart_data.chart_type = sqlite3_column_int(res, 8);
  105. chart_data.family = (char *) sqlite3_column_text(res, 9);
  106. dict_cb(&chart_data, data);
  107. }
  108. skip_load:
  109. rc = sqlite3_reset(res);
  110. if (rc != SQLITE_OK)
  111. error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
  112. }
  113. // Dimension list
  114. #define CTX_GET_DIMENSION_LIST "SELECT d.dim_id, d.id, d.name, CASE WHEN INSTR(d.options,\"hidden\") > 0 THEN 1 ELSE 0 END " \
  115. "FROM meta.dimension d WHERE d.chart_id = @id and d.dim_id is not null ORDER BY d.rowid ASC;"
  116. void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DATA *, void *), void *data)
  117. {
  118. int rc;
  119. static __thread sqlite3_stmt *res = NULL;
  120. if (unlikely(!res)) {
  121. rc = prepare_statement(db_context_meta, CTX_GET_DIMENSION_LIST, &res);
  122. if (rc != SQLITE_OK) {
  123. error_report("Failed to prepare statement to fetch chart dimension data");
  124. return;
  125. }
  126. }
  127. rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
  128. if (unlikely(rc != SQLITE_OK)) {
  129. error_report("Failed to bind chart_id to fetch dimension list");
  130. goto failed;
  131. }
  132. SQL_DIMENSION_DATA dimension_data;
  133. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  134. uuid_copy(dimension_data.dim_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
  135. dimension_data.id = (char *) sqlite3_column_text(res, 1);
  136. dimension_data.name = (char *) sqlite3_column_text(res, 2);
  137. dimension_data.hidden = sqlite3_column_int(res, 3);
  138. dict_cb(&dimension_data, data);
  139. }
  140. failed:
  141. rc = sqlite3_reset(res);
  142. if (rc != SQLITE_OK)
  143. error_report("Failed to reset statement that fetches the chart dimension list, rc = %d", rc);
  144. }
  145. // LABEL LIST
  146. #define CTX_GET_LABEL_LIST "SELECT l.label_key, l.label_value, l.source_type FROM meta.chart_label l WHERE l.chart_id = @id;"
  147. void ctx_get_label_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_CLABEL_DATA *, void *), void *data)
  148. {
  149. int rc;
  150. static __thread sqlite3_stmt *res = NULL;
  151. if (unlikely(!res)) {
  152. rc = prepare_statement(db_context_meta, CTX_GET_LABEL_LIST, &res);
  153. if (rc != SQLITE_OK) {
  154. error_report("Failed to prepare statement to fetch chart labels");
  155. return;
  156. }
  157. }
  158. rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
  159. if (unlikely(rc != SQLITE_OK)) {
  160. error_report("Failed to bind chart_id to fetch chart labels");
  161. goto failed;
  162. }
  163. SQL_CLABEL_DATA label_data;
  164. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  165. label_data.label_key = (char *) sqlite3_column_text(res, 0);
  166. label_data.label_value = (char *) sqlite3_column_text(res, 1);
  167. label_data.label_source = sqlite3_column_int(res, 2);
  168. dict_cb(&label_data, data);
  169. }
  170. failed:
  171. rc = sqlite3_reset(res);
  172. if (rc != SQLITE_OK)
  173. error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
  174. }
  175. // CONTEXT LIST
  176. #define CTX_GET_CONTEXT_LIST "SELECT id, version, title, chart_type, unit, priority, first_time_t, " \
  177. "last_time_t, deleted, family FROM context c WHERE c.host_id = @host_id;"
  178. void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_DATA *, void *), void *data)
  179. {
  180. if (unlikely(!host_uuid))
  181. return;
  182. int rc;
  183. static __thread sqlite3_stmt *res = NULL;
  184. if (unlikely(!res)) {
  185. rc = prepare_statement(db_context_meta, CTX_GET_CONTEXT_LIST, &res);
  186. if (rc != SQLITE_OK) {
  187. error_report("Failed to prepare statement to fetch stored context list");
  188. return;
  189. }
  190. }
  191. VERSIONED_CONTEXT_DATA context_data = {0};
  192. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  193. if (unlikely(rc != SQLITE_OK)) {
  194. error_report("Failed to bind host_id to fetch versioned context data");
  195. goto failed;
  196. }
  197. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  198. context_data.id = (char *) sqlite3_column_text(res, 0);
  199. context_data.version = sqlite3_column_int64(res, 1);
  200. context_data.title = (char *) sqlite3_column_text(res, 2);
  201. context_data.chart_type = (char *) sqlite3_column_text(res, 3);
  202. context_data.units = (char *) sqlite3_column_text(res, 4);
  203. context_data.priority = sqlite3_column_int64(res, 5);
  204. context_data.first_time_s = sqlite3_column_int64(res, 6);
  205. context_data.last_time_s = sqlite3_column_int64(res, 7);
  206. context_data.deleted = sqlite3_column_int(res, 8);
  207. context_data.family = (char *) sqlite3_column_text(res, 9);
  208. dict_cb(&context_data, data);
  209. }
  210. failed:
  211. rc = sqlite3_reset(res);
  212. if (rc != SQLITE_OK)
  213. error_report("Failed to reset statement that fetches stored context versioned data, rc = %d", rc);
  214. }
  215. //
  216. // Storing Data
  217. //
  218. #define CTX_STORE_CONTEXT "INSERT OR REPLACE INTO context " \
  219. "(host_id, id, version, title, chart_type, unit, priority, first_time_t, last_time_t, deleted, family) " \
  220. "VALUES (@host_id, @context, @version, @title, @chart_type, @unit, @priority, @first_time_t, @last_time_t, @deleted, @family);"
  221. int ctx_store_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
  222. {
  223. int rc, rc_stored = 1;
  224. sqlite3_stmt *res = NULL;
  225. if (unlikely(!host_uuid || !context_data || !context_data->id))
  226. return 0;
  227. rc = sqlite3_prepare_v2(db_context_meta, CTX_STORE_CONTEXT, -1, &res, 0);
  228. if (unlikely(rc != SQLITE_OK)) {
  229. error_report("Failed to prepare statement to store context");
  230. return 1;
  231. }
  232. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  233. if (unlikely(rc != SQLITE_OK)) {
  234. error_report("Failed to bind host_uuid to store context details");
  235. goto skip_store;
  236. }
  237. rc = bind_text_null(res, 2, context_data->id, 0);
  238. if (unlikely(rc != SQLITE_OK)) {
  239. error_report("Failed to bind context to store details");
  240. goto skip_store;
  241. }
  242. rc = sqlite3_bind_int64(res, 3, (time_t) context_data->version);
  243. if (unlikely(rc != SQLITE_OK)) {
  244. error_report("Failed to bind first_time_t to store context details");
  245. goto skip_store;
  246. }
  247. rc = bind_text_null(res, 4, context_data->title, 0);
  248. if (unlikely(rc != SQLITE_OK)) {
  249. error_report("Failed to bind context to store details");
  250. goto skip_store;
  251. }
  252. rc = bind_text_null(res, 5, context_data->chart_type, 0);
  253. if (unlikely(rc != SQLITE_OK)) {
  254. error_report("Failed to bind context to store details");
  255. goto skip_store;
  256. }
  257. rc = bind_text_null(res, 6, context_data->units, 0);
  258. if (unlikely(rc != SQLITE_OK)) {
  259. error_report("Failed to bind context to store details");
  260. goto skip_store;
  261. }
  262. rc = sqlite3_bind_int64(res, 7, (time_t) context_data->priority);
  263. if (unlikely(rc != SQLITE_OK)) {
  264. error_report("Failed to bind first_time_t to store context details");
  265. goto skip_store;
  266. }
  267. rc = sqlite3_bind_int64(res, 8, (time_t) context_data->first_time_s);
  268. if (unlikely(rc != SQLITE_OK)) {
  269. error_report("Failed to bind first_time_t to store context details");
  270. goto skip_store;
  271. }
  272. rc = sqlite3_bind_int64(res, 9, (time_t) context_data->last_time_s);
  273. if (unlikely(rc != SQLITE_OK)) {
  274. error_report("Failed to bind last_time_t to store context details");
  275. goto skip_store;
  276. }
  277. rc = sqlite3_bind_int(res, 10, context_data->deleted);
  278. if (unlikely(rc != SQLITE_OK)) {
  279. error_report("Failed to bind deleted flag to store context details");
  280. goto skip_store;
  281. }
  282. rc = bind_text_null(res, 11, context_data->family, 1);
  283. if (unlikely(rc != SQLITE_OK)) {
  284. error_report("Failed to bind context to store details");
  285. goto skip_store;
  286. }
  287. rc_stored = execute_insert(res);
  288. if (rc_stored != SQLITE_DONE)
  289. error_report("Failed store context details for context %s, rc = %d", context_data->id, rc_stored);
  290. skip_store:
  291. rc = sqlite3_finalize(res);
  292. if (rc != SQLITE_OK)
  293. error_report("Failed to finalize statement that stores context details, rc = %d", rc);
  294. return (rc_stored != SQLITE_DONE);
  295. }
  296. // Delete a context
  297. #define CTX_DELETE_CONTEXT "DELETE FROM context WHERE host_id = @host_id AND id = @context;"
  298. int ctx_delete_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
  299. {
  300. int rc, rc_stored = 1;
  301. sqlite3_stmt *res = NULL;
  302. if (unlikely(!context_data || !context_data->id))
  303. return 0;
  304. rc = sqlite3_prepare_v2(db_context_meta, CTX_DELETE_CONTEXT, -1, &res, 0);
  305. if (unlikely(rc != SQLITE_OK)) {
  306. error_report("Failed to prepare statement to delete context");
  307. return 1;
  308. }
  309. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  310. if (unlikely(rc != SQLITE_OK)) {
  311. error_report("Failed to bind host_id to delete context data");
  312. goto skip_delete;
  313. }
  314. rc = sqlite3_bind_text(res, 2, context_data->id, -1, SQLITE_STATIC);
  315. if (unlikely(rc != SQLITE_OK)) {
  316. error_report("Failed to bind context id for data deletion");
  317. goto skip_delete;
  318. }
  319. rc_stored = execute_insert(res);
  320. if (rc_stored != SQLITE_DONE)
  321. error_report("Failed to delete context %s, rc = %d", context_data->id, rc_stored);
  322. #ifdef NETDATA_INTERNAL_CHECKS
  323. else {
  324. char host_uuid_str[UUID_STR_LEN];
  325. uuid_unparse_lower(*host_uuid, host_uuid_str);
  326. netdata_log_info("%s: Deleted context %s under host %s", __FUNCTION__, context_data->id, host_uuid_str);
  327. }
  328. #endif
  329. skip_delete:
  330. rc = sqlite3_finalize(res);
  331. if (rc != SQLITE_OK)
  332. error_report("Failed to finalize statement where deleting a context, rc = %d", rc);
  333. return (rc_stored != SQLITE_DONE);
  334. }
  335. int sql_context_cache_stats(int op)
  336. {
  337. int count, dummy;
  338. if (unlikely(!db_context_meta))
  339. return 0;
  340. netdata_thread_disable_cancelability();
  341. sqlite3_db_status(db_context_meta, op, &count, &dummy, 0);
  342. netdata_thread_enable_cancelability();
  343. return count;
  344. }
  345. //
  346. // TESTING FUNCTIONS
  347. //
  348. static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, void *data)
  349. {
  350. (void)data;
  351. netdata_log_info(" Context id = %s "
  352. "version = %"PRIu64" "
  353. "title = %s "
  354. "chart_type = %s "
  355. "units = %s "
  356. "priority = %"PRIu64" "
  357. "first time = %"PRIu64" "
  358. "last time = %"PRIu64" "
  359. "deleted = %d "
  360. "family = %s",
  361. context_data->id,
  362. context_data->version,
  363. context_data->title,
  364. context_data->chart_type,
  365. context_data->units,
  366. context_data->priority,
  367. context_data->first_time_s,
  368. context_data->last_time_s,
  369. context_data->deleted,
  370. context_data->family);
  371. }
  372. int ctx_unittest(void)
  373. {
  374. uuid_t host_uuid;
  375. uuid_generate(host_uuid);
  376. initialize_thread_key_pool();
  377. int rc = sql_init_context_database(1);
  378. if (rc != SQLITE_OK)
  379. return 1;
  380. // Store a context
  381. VERSIONED_CONTEXT_DATA context_data;
  382. context_data.id = strdupz("cpu.cpu");
  383. context_data.title = strdupz("TestContextTitle");
  384. context_data.units= strdupz("TestContextUnits");
  385. context_data.chart_type = strdupz("TestContextChartType");
  386. context_data.family = strdupz("TestContextFamily");
  387. context_data.priority = 50000;
  388. context_data.deleted = 0;
  389. context_data.first_time_s = 1657781000;
  390. context_data.last_time_s = 1657781100;
  391. context_data.version = now_realtime_usec();
  392. if (likely(!ctx_store_context(&host_uuid, &context_data)))
  393. netdata_log_info("Entry %s inserted", context_data.id);
  394. else
  395. netdata_log_info("Entry %s not inserted", context_data.id);
  396. if (likely(!ctx_store_context(&host_uuid, &context_data)))
  397. netdata_log_info("Entry %s inserted", context_data.id);
  398. else
  399. netdata_log_info("Entry %s not inserted", context_data.id);
  400. // This will change end time
  401. context_data.first_time_s = 1657781000;
  402. context_data.last_time_s = 1657782001;
  403. if (likely(!ctx_update_context(&host_uuid, &context_data)))
  404. netdata_log_info("Entry %s updated", context_data.id);
  405. else
  406. netdata_log_info("Entry %s not updated", context_data.id);
  407. netdata_log_info("List context start after insert");
  408. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  409. netdata_log_info("List context end after insert");
  410. // This will change start time
  411. context_data.first_time_s = 1657782000;
  412. context_data.last_time_s = 1657782001;
  413. if (likely(!ctx_update_context(&host_uuid, &context_data)))
  414. netdata_log_info("Entry %s updated", context_data.id);
  415. else
  416. netdata_log_info("Entry %s not updated", context_data.id);
  417. // This will list one entry
  418. netdata_log_info("List context start after insert");
  419. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  420. netdata_log_info("List context end after insert");
  421. netdata_log_info("List context start after insert");
  422. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  423. netdata_log_info("List context end after insert");
  424. // This will delete the entry
  425. if (likely(!ctx_delete_context(&host_uuid, &context_data)))
  426. netdata_log_info("Entry %s deleted", context_data.id);
  427. else
  428. netdata_log_info("Entry %s not deleted", context_data.id);
  429. freez((void *)context_data.id);
  430. freez((void *)context_data.title);
  431. freez((void *)context_data.chart_type);
  432. freez((void *)context_data.family);
  433. freez((void *)context_data.units);
  434. // The list should be empty
  435. netdata_log_info("List context start after delete");
  436. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  437. netdata_log_info("List context end after delete");
  438. sql_close_context_database();
  439. return 0;
  440. }