Use correct hop count if host is already in memory (#11785)
* Use correct hop count if host in memory * Add locking to be safe when using host lookup * Update the live state correctly
Stelios Fragkakis committed
Nov 17, 2021 at 11:19 UTC
577f8c0e43d15238f9cdecf036fa889605136d66
1 file changed
+6
-2
database/sqlite/sqlite_functions.c
+6
-2
@@ -1791,6 +1791,7 @@ struct node_instance_list *get_node_list(void)
1791
node_list = callocz(row + 1, sizeof(*node_list));
1792
int max_rows = row;
1793
row = 0;
1794
+ rrd_wrlock();
1795
while (sqlite3_step(res) == SQLITE_ROW) {
1796
if (sqlite3_column_bytes(res, 0) == sizeof(uuid_t))
1797
uuid_copy(node_list[row].node_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
@@ -1799,8 +1800,10 @@ struct node_instance_list *get_node_list(void)
1800
uuid_copy(node_list[row].host_id, *host_id);
1801
node_list[row].queryable = 1;
1802
uuid_unparse_lower(*host_id, host_guid);
1802
- node_list[row].live = rrdhost_find_by_guid(host_guid, 0) ? 1 : 0;
1803
- node_list[row].hops = uuid_compare(*host_id, localhost->host_uuid) ? 1 : 0;
1803
+ RRDHOST *host = rrdhost_find_by_guid(host_guid, 0);
1804
+ node_list[row].live = host && (host == localhost || host->receiver) ? 1 : 0;
1805
+ node_list[row].hops = (host && host->system_info) ? host->system_info->hops :
1806
+ uuid_compare(*host_id, localhost->host_uuid) ? 1 : 0;
1807
node_list[row].hostname =
1808
sqlite3_column_bytes(res, 2) ? strdupz((char *)sqlite3_column_text(res, 2)) : NULL;
1809
}
@@ -1808,6 +1811,7 @@ struct node_instance_list *get_node_list(void)
1811
if (row == max_rows)
1812
break;
1813
}
1814
+ rrd_unlock();
1815
1816
failed:
1817
if (unlikely(sqlite3_finalize(res) != SQLITE_OK))