@cryptotaxi247 / netdata-1 / commits / 9e89ac730

Find the chart and dimension UUID from the context (#13868)

* Add functions to find chart uuid and dimension UUIDs from context * Remove old functions that access the sqlite database directly * Use new functions to fetch the UUIDs for chart and dimensions * Remove unused function

Stelios Fragkakis committed Oct 26, 2022 at 12:19 UTC 9e89ac73077e269df5662b2b56cb39843a03d4fa
6 files changed +61 -110
database/rrdcontext.c
+57
@@ -1486,6 +1486,63 @@ void rrdcontext_host_child_connected(RRDHOST *host) {
1486 ;
1487 }
1488
1489 +int rrdcontext_find_dimension_uuid(RRDSET *st, const char *id, uuid_t *store_uuid) {
1490 + if(!st->rrdhost) return 1;
1491 + if(!st->context) return 2;
1492 +
1493 + RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item((DICTIONARY *)st->rrdhost->rrdctx, string2str(st->context));
1494 + if(!rca) return 3;
1495 +
1496 + RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
1497 +
1498 + RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *)dictionary_get_and_acquire_item(rc->rrdinstances, string2str(st->id));
1499 + if(!ria) {
1500 + rrdcontext_release(rca);
1501 + return 4;
1502 + }
1503 +
1504 + RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
1505 +
1506 + RRDMETRIC_ACQUIRED *rma = (RRDMETRIC_ACQUIRED *)dictionary_get_and_acquire_item(ri->rrdmetrics, id);
1507 + if(!rma) {
1508 + rrdinstance_release(ria);
1509 + rrdcontext_release(rca);
1510 + return 5;
1511 + }
1512 +
1513 + RRDMETRIC *rm = rrdmetric_acquired_value(rma);
1514 +
1515 + uuid_copy(*store_uuid, rm->uuid);
1516 +
1517 + rrdmetric_release(rma);
1518 + rrdinstance_release(ria);
1519 + rrdcontext_release(rca);
1520 + return 0;
1521 +}
1522 +
1523 +int rrdcontext_find_chart_uuid(RRDSET *st, uuid_t *store_uuid) {
1524 + if(!st->rrdhost) return 1;
1525 + if(!st->context) return 2;
1526 +
1527 + RRDCONTEXT_ACQUIRED *rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item((DICTIONARY *)st->rrdhost->rrdctx, string2str(st->context));
1528 + if(!rca) return 3;
1529 +
1530 + RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
1531 +
1532 + RRDINSTANCE_ACQUIRED *ria = (RRDINSTANCE_ACQUIRED *)dictionary_get_and_acquire_item(rc->rrdinstances, string2str(st->id));
1533 + if(!ria) {
1534 + rrdcontext_release(rca);
1535 + return 4;
1536 + }
1537 +
1538 + RRDINSTANCE *ri = rrdinstance_acquired_value(ria);
1539 + uuid_copy(*store_uuid, ri->uuid);
1540 +
1541 + rrdinstance_release(ria);
1542 + rrdcontext_release(rca);
1543 + return 0;
1544 +}
1545 +
1546 void rrdcontext_host_child_disconnected(RRDHOST *host) {
1547 rrdcontext_recalculate_host_retention(host, RRD_FLAG_UPDATE_REASON_DISCONNECTED_CHILD, false);
1548 }
database/rrdcontext.h
+2
@@ -78,6 +78,7 @@ void rrdcontext_updated_rrddim_multiplier(RRDDIM *rd);
78 void rrdcontext_updated_rrddim_divisor(RRDDIM *rd);
79 void rrdcontext_updated_rrddim_flags(RRDDIM *rd);
80 void rrdcontext_collected_rrddim(RRDDIM *rd);
81 +int rrdcontext_find_dimension_uuid(RRDSET *st, const char *id, uuid_t *store_uuid);
82
83 // ----------------------------------------------------------------------------
84 // public API for rrdsets
@@ -87,6 +88,7 @@ void rrdcontext_removed_rrdset(RRDSET *st);
88 void rrdcontext_updated_rrdset_name(RRDSET *st);
89 void rrdcontext_updated_rrdset_flags(RRDSET *st);
90 void rrdcontext_collected_rrdset(RRDSET *st);
91 +int rrdcontext_find_chart_uuid(RRDSET *st, uuid_t *store_uuid);
92
93 // ----------------------------------------------------------------------------
94 // public API for ACLK
database/rrddim.c
+1 -1
@@ -82,7 +82,7 @@ static void rrddim_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, v
82
83 rd->rrd_memory_mode = ctr->memory_mode;
84
85 - if (unlikely(find_dimension_uuid(st, rd, &(rd->metric_uuid)))) {
85 + if (unlikely(rrdcontext_find_dimension_uuid(st, rrddim_id(rd), &(rd->metric_uuid)))) {
86 uuid_generate(rd->metric_uuid);
87 }
88
database/rrdset.c
+1 -1
@@ -367,7 +367,7 @@ static void rrdset_react_callback(const DICTIONARY_ITEM *item __maybe_unused, vo
367
368 if(ctr->react_action & (RRDSET_REACT_NEW | RRDSET_REACT_PLUGIN_UPDATED | RRDSET_REACT_MODULE_UPDATED)) {
369 if (ctr->react_action & RRDSET_REACT_NEW) {
370 - if (find_chart_uuid(host, string2str(st->parts.type), string2str(st->parts.id), &st->chart_uuid)) {
370 + if(unlikely(rrdcontext_find_chart_uuid(st, &st->chart_uuid))) {
371 uuid_generate(st->chart_uuid);
372 }
373 }
database/sqlite/sqlite_functions.c
-106
@@ -474,112 +474,6 @@ void sql_close_database(void)
474 error_report("Error %d while closing the SQLite database, %s", rc, sqlite3_errstr(rc));
475 }
476
477 -
478 -// Expect a UUID in column 0
479 -// Return 0 and store in store_uuid
480 -// Return 1 if it fails (store_uuid is left unchanged)
481 -static int expect_uuid_column0(sqlite3_stmt *res, uuid_t *store_uuid)
482 -{
483 - int rc = sqlite3_step_monitored(res);
484 - if (likely(rc == SQLITE_ROW)) {
485 - uuid_copy(*store_uuid, *((uuid_t *)sqlite3_column_blob(res, 0)));
486 - return 0;
487 - }
488 - return 1;
489 -}
490 -
491 -
492 -// Do a database lookup to find the uuid of a dimension
493 -// Return 0 if uuid is found and store it in *store_uuid
494 -// 1 if no uuid is found (needs to be created)
495 -//
496 -
497 -#define SQL_FIND_DIMENSION_UUID \
498 - "SELECT dim_id FROM dimension WHERE chart_id=@chart AND id=@id AND name=@name AND LENGTH(dim_id)=16;"
499 -int find_dimension_uuid(RRDSET *st, RRDDIM *rd, uuid_t *store_uuid)
500 -{
501 - static __thread sqlite3_stmt *res = NULL;
502 - int rc;
503 - int status = 1;
504 -
505 - if (unlikely(!db_meta) && default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
506 - return 1;
507 -
508 - if (unlikely(!res)) {
509 - rc = prepare_statement(db_meta, SQL_FIND_DIMENSION_UUID, &res);
510 - if (rc != SQLITE_OK) {
511 - error_report("Failed to prepare statement to lookup dimension UUID in the database");
512 - return 1;
513 - }
514 - }
515 -
516 - rc = sqlite3_bind_blob(res, 1, &st->chart_uuid, sizeof(st->chart_uuid), SQLITE_STATIC);
517 - if (unlikely(rc != SQLITE_OK))
518 - goto skip;
519 -
520 - rc = sqlite3_bind_text(res, 2, rrddim_id(rd), -1, SQLITE_STATIC);
521 - if (unlikely(rc != SQLITE_OK))
522 - goto skip;
523 -
524 - rc = sqlite3_bind_text(res, 3, rrddim_name(rd), -1, SQLITE_STATIC);
525 - if (unlikely(rc != SQLITE_OK))
526 - goto skip;
527 -
528 - status = expect_uuid_column0(res, store_uuid);
529 -
530 -skip:
531 - rc = sqlite3_reset(res);
532 - if (unlikely(rc != SQLITE_OK))
533 - error_report("Failed to reset statement find dimension uuid, rc = %d", rc);
534 - return status;
535 -}
536 -
537 -
538 -/*
539 - * Do a database lookup to find the UUID of a chart
540 - *
541 - */
542 -
543 -#define SQL_FIND_CHART_UUID "SELECT chart_id FROM chart WHERE host_id = @host AND type=@type AND id=@id AND chart_id IS NOT NULL;"
544 -
545 -int find_chart_uuid(RRDHOST *host, const char *type, const char *id, uuid_t *store_uuid)
546 -{
547 - static __thread sqlite3_stmt *res = NULL;
548 - int rc;
549 - int status = 1;
550 -
551 - if (unlikely(!db_meta) && default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
552 - return 1;
553 -
554 - if (unlikely(!res)) {
555 - rc = prepare_statement(db_meta, SQL_FIND_CHART_UUID, &res);
556 - if (rc != SQLITE_OK) {
557 - error_report("Failed to prepare statement to lookup chart UUID in the database");
558 - return 1;
559 - }
560 - }
561 -
562 - rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
563 - if (unlikely(rc != SQLITE_OK))
564 - goto skip;
565 -
566 - rc = sqlite3_bind_text(res, 2, type, -1, SQLITE_STATIC);
567 - if (unlikely(rc != SQLITE_OK))
568 - goto skip;
569 -
570 - rc = sqlite3_bind_text(res, 3, id, -1, SQLITE_STATIC);
571 - if (unlikely(rc != SQLITE_OK))
572 - goto skip;
573 -
574 - status = expect_uuid_column0(res, store_uuid);
575 -
576 -skip:
577 - rc = sqlite3_reset(res);
578 - if (unlikely(rc != SQLITE_OK))
579 - error_report("Failed to reset statement when searching for a chart UUID, rc = %d", rc);
580 - return status;
581 -}
582 -
477 int exec_statement_with_uuid(const char *sql, uuid_t *uuid)
478 {
479 int rc, result = 1;
database/sqlite/sqlite_functions.h
-2
@@ -60,8 +60,6 @@ void add_migrated_file(char *path, uint64_t file_size);
60 void db_execute(const char *cmd);
61
62 // Look up functions
63 -int find_dimension_uuid(RRDSET *st, RRDDIM *rd, uuid_t *store_uuid);
64 -int find_chart_uuid(RRDHOST *host, const char *type, const char *id, uuid_t *store_uuid);
63 int get_node_id(uuid_t *host_id, uuid_t *node_id);
64 int get_host_id(uuid_t *node_id, uuid_t *host_id);
65 struct node_instance_list *get_node_list(void);