Skip to content

Commit cec3a97

Browse files
committed
Warn when multiple *pool calls are made with the same parameters
If this is not done, the power manager will have to make decisions on how to prioritize requests from such pools. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 5b4cb32 commit cec3a97

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from __future__ import annotations
1212

13+
import logging
1314
import sys
1415
import typing
1516
from collections import abc
@@ -44,6 +45,8 @@
4445
from ..timeseries.logical_meter import LogicalMeter
4546
from ..timeseries.producer import Producer
4647

48+
_logger = logging.getLogger(__name__)
49+
4750

4851
_REQUEST_RECV_BUFFER_SIZE = 500
4952
"""The maximum number of requests that can be queued in the request receiver.
@@ -112,6 +115,13 @@ def __init__(
112115
self._frequency_instance: GridFrequency | None = None
113116
self._voltage_instance: VoltageStreamer | None = None
114117

118+
self._known_pool_keys: set[str] = set()
119+
"""A set of keys for corresponding to created EVChargerPool instances.
120+
121+
This is used to warn the user if they try to create a new EVChargerPool instance
122+
for the same set of component IDs, and with the same priority.
123+
"""
124+
115125
def frequency(self) -> GridFrequency:
116126
"""Return the grid frequency measuring point."""
117127
if self._frequency_instance is None:
@@ -199,6 +209,21 @@ def ev_charger_pool(
199209
if ev_charger_ids is not None:
200210
ref_store_key = frozenset(ev_charger_ids)
201211

212+
pool_key = f"{ref_store_key}-{priority}"
213+
if pool_key in self._known_pool_keys:
214+
_logger.warning(
215+
"An EVChargerPool instance was already created for ev_charger_ids=%s "
216+
"and priority=%s using `microgrid.ev_charger_pool(...)`."
217+
"\n Hint: If the multiple instances are created from the same actor, "
218+
"consider reusing the same instance."
219+
"\n Hint: If the instances are created from different actors, "
220+
"consider using different priorities to distinguish them.",
221+
ev_charger_ids,
222+
priority,
223+
)
224+
else:
225+
self._known_pool_keys.add(pool_key)
226+
202227
if ref_store_key not in self._ev_charger_pool_reference_stores:
203228
self._ev_charger_pool_reference_stores[ref_store_key] = (
204229
EVChargerPoolReferenceStore(
@@ -265,6 +290,21 @@ def battery_pool(
265290
if battery_ids is not None:
266291
ref_store_key = frozenset(battery_ids)
267292

293+
pool_key = f"{ref_store_key}-{priority}"
294+
if pool_key in self._known_pool_keys:
295+
_logger.warning(
296+
"A BatteryPool instance was already created for battery_ids=%s and "
297+
"priority=%s using `microgrid.battery_pool(...)`."
298+
"\n Hint: If the multiple instances are created from the same actor, "
299+
"consider reusing the same instance."
300+
"\n Hint: If the instances are created from different actors, "
301+
"consider using different priorities to distinguish them.",
302+
battery_ids,
303+
priority,
304+
)
305+
else:
306+
self._known_pool_keys.add(pool_key)
307+
268308
if ref_store_key not in self._battery_pool_reference_stores:
269309
self._battery_pool_reference_stores[ref_store_key] = (
270310
BatteryPoolReferenceStore(

0 commit comments

Comments
 (0)