Skip to content

Commit 72e416e

Browse files
authored
Merge pull request #15 from stackhpc/add-caching
Add caching of aggregates
2 parents b2e977c + b7d97eb commit 72e416e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

os_capacity/prometheus.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import prometheus_client as prom_client
1111
from prometheus_client import core as prom_core
1212

13+
RESOURCE_PROVIDER_AGGREGATE_CACHE = {}
14+
1315

1416
def get_capacity_per_flavor(placement_client, flavors):
1517
capacity_per_flavor = {}
@@ -122,13 +124,20 @@ def get_resource_provider_info(compute_client, placement_client):
122124
# skip checking every resource provider for their aggregates
123125
continue
124126

125-
response = placement_client.get(
126-
f"/resource_providers/{raw_rp.id}/aggregates",
127-
headers={"OpenStack-API-Version": "placement 1.19"},
128-
)
129-
response.raise_for_status()
130-
aggs = response.json()
131-
rp["aggregates"] = aggs["aggregates"]
127+
# TODO(johngarbutt): maybe check if cached aggregate still exists?
128+
aggregates = RESOURCE_PROVIDER_AGGREGATE_CACHE.get(raw_rp.id)
129+
if aggregates is None:
130+
response = placement_client.get(
131+
f"/resource_providers/{raw_rp.id}/aggregates",
132+
headers={"OpenStack-API-Version": "placement 1.19"},
133+
)
134+
response.raise_for_status()
135+
aggs = response.json()
136+
rp["aggregates"] = aggs["aggregates"]
137+
RESOURCE_PROVIDER_AGGREGATE_CACHE[raw_rp.id] = aggs["aggregates"]
138+
else:
139+
rp["aggregates"] = aggregates
140+
132141
for agg_id in rp["aggregates"]:
133142
if agg_id in azones:
134143
rp["az"] = azones[agg_id]

0 commit comments

Comments
 (0)