Repository navigation
Description
At the moment we have something like:
dodal/src/dodal/beamlines/i03.py
Lines 46 to 57 in 4f646f8
| def dcm(wait_for_connection: bool = True, fake_with_ophyd_sim: bool = False) -> DCM: | |
| """Get the i03 DCM device, instantiate it if it hasn't already been. | |
| If this is called when already instantiated in i03, it will return the existing object. | |
| """ | |
| return device_instantiation( | |
| DCM, | |
| "dcm", | |
| "", | |
| wait_for_connection, | |
| fake_with_ophyd_sim, | |
| daq_configuration_path=DAQ_CONFIGURATION_PATH, | |
| ) |
This allows optional serial connection and creation of fake devices instead of real devices via device_instantiation. The ophyd-async mechanism for this is using the connect method:
async def connect(self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT):
(you could argue that fake would be a better name than sim, so that could also be changed)
I would like to be able to:
- Create all the Devices for a beamline
- Have blueapi know what those Devices are
- Connect those Devices in the fastest possible way so we can get to running a plan quickly
I will make some suggestions and I'd like to encourage a vigorous discussion of the alternatives here!
Suggestion 1: Background idempotent connect
Rather than an explicit connect when creating the Device, add it to a background queue so it will be connected eventually. I would also suggest we make connect() idempotent so that it will complete immediately if it has already been run successfully. If we also use Devices as arguments to plans then blueapi could call connect() on all the Devices that are plan ags (or some other decorator solution a-la hyperion composites).
# ophyd-async
class Device:
# None if connect hasn't started, an Event if it has, a set Event if it's done
_connect_task: Optional[asyncio.Task] = None
async def connect(self, sim: bool = False, timeout: float = DEFAULT_TIMEOUT):
if not self._connect_task or (self._connect_task.done() and self._connect_task.exception()):
# connect never run, or it errored, so try to connect now
coros = {
name: child_device.connect(sim, timeout=timeout)
for name, child_device in self.children()
}
self._connect_task = asyncio.create_task(wait_for_connection(**coros))
# Wait for it to complete
await self._connect_task# blueapi
async def run_plan_sketch(plan, kwargs):
coros = {name: device.connect() for name, device in get_devices(plan, kwargs)}
await wait_for_connection(**coros)
RE(plan(**kwargs))Suggestion 2: Remove device factories
If connect is no longer happening at Device instantiation then it should be safe to always have them created at startup, then we can just import them with the minimum boilerplate. Connection can be done outside this function.
# dodal/beamlines/i22.py
BL = "BL22I"
panda1 = PandAHDF(f"{BL}-EA-PANDA-01")
panda2 = PandAHDF(f"{BL}-EA-PANDA-02")
i0 = TetrammHDF(f"{BL}-EA-TTRM-01")# blueapi
async def connect_and_name(bl, sim, timeout):
coros = {}
for name, obj in inspect.getmembers(bl):
if isinstance(obj, Device):
obj.set_name(name)
coros[name] = obj.connect(sim, timeout)
# Also add the device to some registry
return asyncio.create_task(wait_for_connection(**coros))Not sure if connect_and_name should be done at the bottom of i22.py or not...
Acceptance Criteria
- Dodal can do parallel connect of all devices in abeamline
- Blueapi can fine all those devices
Activity
Tom-Willemsen commentedon Apr 4, 2024
Observation: this connection logic seems like an area with significant hidden complexity/code churn. When we decide on an approach, can we make sure we write an ADR documenting exactly what we considered and why we chose that solution?
I think we need to consider both "optional" and "permanent" devices in this connection logic - where a given device might be optional or not on any given beamline. I previously wrote a similar issue in blueapi, but I think it makes sense to consider it here if we're going to refactor the connection logic.
In fact, I think there might be several cases:
connecton all relevant devices just before running a plan to ensure they exist at the point the plan is being run.Thinking out loud here, I'm thinking of a syntax along the lines of:
This would also leave us scope for different connection behaviours later on if we've missed a use-case. Happy to bikeshed over names and/or what the default/unannotated should be.
Some more specific comments:
I think this is true for hyperion, but less true for i22. For i22, as the plans are in general pretty slow compared to connection times, I'd be willing to trade some performance for better / more ergonomic error reporting.
i22are also significantly more likely to run into these errors due to the nature of all their temporary equipment.At what point would connection errors be reported if the connection is happening in a background queue? When you first try to use that device in a plan?
Hmmm. Thinking about the i22 case for example, let's say I start blueAPI at the start of run. when the linkam is available, and then later unplug it / switch off it's IOC. I probably don't want subsequent calls to
connectto work.Maybe we need separate concepts for
connectversusensure_connected- where the former would be idempotent, but the latter would really check that the device is still available at the point it's run (or try to reconnect if it wasn't previously)? Name bikeshedding welcome.Q: If all the PV connections are happening in parallel via
asyncio, doesconnectfor a device actually take significant time? (I can see how it would take a long time in the sequential, non-aio case)Sorry if this is a bit long... but you did
;)
d-perl commentedon Apr 5, 2024
Yes, I think your example is somewhat likely to be desired, but I think it's fine for that to be case where it's up to you to do the explicit connection, it could hardly be otherwise
In my discussion with @coretl yesterday we imagined something like "at the start of the plan, move required & not-yet-connected devices to the front of the queue and block until they are connected/fail", and having everything be "semi-eager" i.e., BlueApi/Dodal will attempt to connect to everything (eventually), but if it can't, it will only report those errors and not exit. It will then only be an error if you attempt to run a plan which uses a device which failed to connect.
This seemed good from the point of view of having fast application startup with minimal faff, and the required flexibility for optional devices. I guess it could end up with a lot of logs if there are lots of optional devices?
If there are thousands of them (a whole beamline worth) maybe yeah
DominicOram commentedon Apr 8, 2024
Definitely agree with this, this is my 3rd time having this discussion.
As Tom said, we might not want all the devices for a beamline.
I think the 2nd would be better for Hyperion too. In fact I think the GDA behavior that we have on some beamlines of not being able to start if some devices aren't connected is
This isn't actually that necessary for Hyperion either. We don't care if connect takes some time, as long as it's only done once at start up. We shouldn't be restarting Hyperion often so the time it takes to start up is irrelevant. The logic in
dodalis there because we originally were connecting at the start of every plan andconnectwas an operation that took time and slowed us down.Our view on this would probably be that if the check to see if we're connected takes a small amount of time (<100ms for all devices) then we can block on it at the start of a plan and happily sit there and wait for it to be connected. Otherwise we would probably rather proceed without the check and take the hit when we crash later.
I guess circling back to Tom C's original suggestions:
connectidempotent (or fast enough in the happy path that it may as well be). I don't think background connection is completely necessary, we would probably always want it to be blocking the first timeI think I stand by both of those statements still and the suggestion breaks them both.
I do think we're going to struggle to get to a one-size-fits all solution here. I do like Tom's decorators on the devices but we might need more customisability as well
I think this needs broad discussion, probably in person, and a comprehensive final document on it.
coretl commentedon Apr 8, 2024
Agreed, I will set this up, during the hackathon or after?
But I'd like to explore this question first, if
Device.__init__cannot do any I/O, so therefore is always fast and safe to do even if the hardware is not present, why would we not create theDevice? I understand why we wouldn'tconnect()it, but is there a reason not to create it in the first place? If so, then what should control whether it is created or not?DominicOram commentedon Apr 8, 2024
I vote after, there's already enough going on.
Sorry @coretl, I think I updated my comment above answering some of these question more.
DominicOram commentedon Apr 8, 2024
An example of this currently is the OAV. The OAV function takes a config object (based on a file) on
__init__, this file is needed for theOAVto operate correctly so from that Device's perspective it makes sense for it to be a required parameter in theinit, it means that we can be sure for the lifetime of the Device we have some valid config. So to fix this under the suggested change the options are:configurefunction onOAV(which is what we have in GDA), consequences of this are:coretl commentedon Apr 8, 2024
Ok, that is a good example, I think I'm sold on the need for device factories and some variant of @Tom-Willemsen's connection decorators. Will try and propose something in advance of the meeting and we can argue about it then
coretl commentedon Apr 8, 2024
I would like to explore this requirement, as there are tricky interactions with the RunEngine event loop that we need to consider, are these Devices ever used without the RE being present, or can we say that this is a requirement?
DominicOram commentedon Apr 8, 2024
To play devil's advocate. There is a 3rd solution to the OAV issue where it takes a
configurationobject but this doesn't actually do the file I/O until it is triggered in theconnectofOAV. This may solve the issue but it's very much not immediately obvious to a developer why you would do it.I think I basically worry that without the factories future developers will quickly fall into the patterns of work on import or
configurefunctions, especially given that these both exist in GDA already. I think both of those solutions are worse than the factory function (which at the moment my only dislike of is that it has too much boilerplate).I think we can say that the RE is a requirement, as you'll probably be using one anyway. I would basically like to keep the barrier to entry of writing some basic plans as low as possible, and at the moment it is:
Introducing BlueAPI as a dependency here gives nothing to the scientist that wants to try something out quickly
Tom-Willemsen commentedon Apr 8, 2024
I agree with Dom's comment about minimising the barrier of entry to basic plans.
From a support perspective, I'd really like to try and minimise "tricky interactions" - whether that's for devs or users. So if making the
REa hard requirement eliminates those complexities, then I think I'm probably in favour. To be honest I'm struggling to think of a user-facing use case for not having the runengine present, am I missing something?coretl commentedon Apr 8, 2024
This makes sense, and was the only non-blueapi solution I was planning to explicitly support. If we do something like:
Then in the absence of blueapi it will serially connect the device on first call of the factory. We can also provide functions like
connect_all_eager_devices()that blueapi can call to do a parallel connect upfront. The only limitation with this approach is thatfactory()can only do device connection in the main thread. We would have to provide someexplicit_connect()plan or async function if we need to do that in the run engine event loop (like in a plan).coretl commentedon Apr 23, 2024
We had a meeting on this, and based on this I now propose:
Device.connectseems like a good idea as long as we can optionally passforce=Trueto itensure_connected(device, force=False)plan stub that can be used to explicitly make and connect Devices in plansconnect=Trueargument to the device factory that can be passed in for basic plan development usageconnect=TrueargumentThis means we have:
Is everyone happy with this?
coretl commentedon Apr 23, 2024
I guess you could do composites like this:
d-perl commentedon Apr 23, 2024
Re: getting rid of composites, I would like it if it would support
Unpack(https://peps.python.org/pep-0692/#specification) so that we could at least still do something like:to stop having enormous parameter lists when there are a lot of devices and nested plan sections using them
coretl commentedon Apr 23, 2024
Would you actually use
outer_planas an entrypoint though? If would mean callingouter_plan(parameters, backlight=backlight, gonio=gonio, ...)rather than justouter_plan(parameters). Would this work instead?For this to work with typing we'd have to make sure type checkers are happy with this:
d-perl commentedon Apr 23, 2024
the idea is that it is nice shorthand for
so that blueAPI can give them to us - in the case where we explicitly call
ensure_connectedI would rather use a dataclass (for dot access) like we already do - but yes, we can definitely work with that suggestion.coretl commentedon Apr 23, 2024
Or we could keep the composite:
d-perl commentedon Apr 23, 2024
yeah, that's very close to what we do now, seems fine
coretl commentedon Apr 26, 2024
More conversations with @callumforrester and other DAQ members led us to this proposal:
Device.connect(mock, timeout, force=False)be idempotentensure_connected(*devices)plan stubdevice_factory(startup_connect=False)decorator that makes, names, caches and optionally connects a deviceget_device_factories()that returns all device factories and whether they should be connected at startupget_device_factories(), make all the Devices, connect the ones that should be connected at startup in parallel and log those that failensure_connectedon all plan args and defaults that are DevicesWe can then iterate on this if the parallel connect causes a broadcast storm. We could also in future add a monitor to a heartbeat PV per device in
Device.connectso that it would reconnect next time it was called.@DominicOram are you happy with this approach? If so I'll go ahead and make the tickets in the 3 repos for these items and write and ADR in dodal.
coretl commentedon Apr 29, 2024
I've made 3 more targeted issues so closing this one