@cryptotaxi247 / netdata-1 / commits / d93101b97

Fix label copy (#16297)

Fix label copy, more details in unittest

Stelios Fragkakis committed Oct 29, 2023 at 22:46 UTC d93101b972cacd8673d88be66acc7ad4cf92f70e
1 file changed +46 -9
database/rrdlabels.c
+46 -9
@@ -1040,8 +1040,6 @@ void rrdlabels_copy(RRDLABELS *dst, RRDLABELS *src)
1040 lfe_start_nolock(src, label, ls)
1041 {
1042 RRDLABEL *old_label_with_key = rrdlabels_find_label_with_key_unsafe(dst, label);
1043 - if (old_label_with_key)
1044 - continue;
1043
1044 Pvoid_t *PValue = JudyLIns(&dst->JudyL, (Word_t)label, PJE0);
1045 if(unlikely(!PValue || PValue == PJERR))
@@ -1486,6 +1484,36 @@ int rrdlabels_unittest_double_check() {
1484 return errors;
1485 }
1486
1487 +static int rrdlabels_walkthrough_index_read(RRDLABELS *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, size_t index, void *data), void *data)
1488 +{
1489 + int ret = 0;
1490 +
1491 + if(unlikely(!labels || !callback)) return 0;
1492 +
1493 + RRDLABEL *lb;
1494 + RRDLABEL_SRC ls;
1495 + size_t index = 0;
1496 + lfe_start_read(labels, lb, ls)
1497 + {
1498 + ret = callback(string2str(lb->index.key), string2str(lb->index.value), ls, index, data);
1499 + if (ret < 0)
1500 + break;
1501 + index++;
1502 + }
1503 + lfe_done(labels);
1504 +
1505 + return ret;
1506 +}
1507 +
1508 +static int unittest_dump_labels(const char *name, const char *value, RRDLABEL_SRC ls, size_t index, void *data __maybe_unused)
1509 +{
1510 + if (!index && data) {
1511 + fprintf(stderr, "%s\n", (char *) data);
1512 + }
1513 + fprintf(stderr, "LABEL \"%s\" = %d \"%s\"\n", name, ls & (~RRDLABEL_FLAG_INTERNAL), value);
1514 + return 1;
1515 +}
1516 +
1517 int rrdlabels_unittest_migrate_check() {
1518 fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
1519
@@ -1520,27 +1548,36 @@ int rrdlabels_unittest_migrate_check() {
1548 labels2 = rrdlabels_create();
1549
1550 rrdlabels_add(labels1, "key1", "value1", RRDLABEL_SRC_CONFIG);
1523 - rrdlabels_add(labels1, "key2", "value1", RRDLABEL_SRC_CONFIG);
1524 - rrdlabels_add(labels1, "key3", "value1", RRDLABEL_SRC_CONFIG);
1525 - rrdlabels_add(labels1, "key4", "value1", RRDLABEL_SRC_CONFIG); // 4 keys
1551 + rrdlabels_add(labels1, "key2", "value2", RRDLABEL_SRC_CONFIG);
1552 + rrdlabels_add(labels1, "key3", "value3", RRDLABEL_SRC_CONFIG);
1553 + rrdlabels_add(labels1, "key4", "value4", RRDLABEL_SRC_CONFIG); // 4 keys
1554 + rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1");
1555
1527 - rrdlabels_add(labels2, "key1", "value10", RRDLABEL_SRC_CONFIG);
1528 - rrdlabels_add(labels2, "key2", "value1", RRDLABEL_SRC_CONFIG);
1529 - rrdlabels_add(labels2, "key0", "value1", RRDLABEL_SRC_CONFIG);
1556 + rrdlabels_add(labels2, "key1", "value1_new", RRDLABEL_SRC_CONFIG);
1557 + rrdlabels_add(labels2, "key2", "value2", RRDLABEL_SRC_CONFIG);
1558 + rrdlabels_add(labels2, "key0", "value0", RRDLABEL_SRC_CONFIG);
1559 + rrdlabels_walkthrough_index_read(labels2, unittest_dump_labels, "\nlabels2");
1560
1561 rrdlabels_copy(labels1, labels2); // labels1 should have 5 keys
1562 + rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1 after copy from labels2");
1563
1564 entries = rrdlabels_entries(labels1);
1565 fprintf(stderr, "labels1 (copied) entries found %zu (should be 5)\n", rrdlabels_entries(labels1));
1566 if (entries != 5)
1567 return 1;
1568
1538 - rrdlabels_add(labels1, "key100", "value1", RRDLABEL_SRC_CONFIG);
1569 + rrdlabels_add(labels1, "key100", "value100", RRDLABEL_SRC_CONFIG);
1570 + rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1 now after key100");
1571 +
1572 rrdlabels_copy(labels2, labels1); // labels2 should have 6 keys
1573 entries = rrdlabels_entries(labels1);
1574
1575 fprintf(stderr, "labels2 (copied) entries found %zu (should be 6)\n", rrdlabels_entries(labels1));
1576
1577 + rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels2 after labels1 copy");
1578 +
1579 + rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nfinal labels1");
1580 +
1581 rrdlabels_destroy(labels1);
1582 rrdlabels_destroy(labels2);
1583