Skip to content

Commit b7d97eb

Browse files
committed
Attempt to add caching
1 parent 5edc200 commit b7d97eb

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

os_capacity/prometheus.py

Lines changed: 16 additions & 8 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,14 +124,20 @@ def get_resource_provider_info(compute_client, placement_client):
122124
# skip checking every resource provider for their aggregates
123125
continue
124126

125-
# TODO(johngarbutt): add a cache in here?
126-
response = placement_client.get(
127-
f"/resource_providers/{raw_rp.id}/aggregates",
128-
headers={"OpenStack-API-Version": "placement 1.19"},
129-
)
130-
response.raise_for_status()
131-
aggs = response.json()
132-
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+
133141
for agg_id in rp["aggregates"]:
134142
if agg_id in azones:
135143
rp["az"] = azones[agg_id]

0 commit comments

Comments
 (0)