Skip to content

added Exception handling for devices - #550

Merged
DiamondJoseph merged 17 commits into
mainfrom
device-resilience
May 30, 2024
Merged

DiamondJoseph merged 17 commits into
mainfrom
device-resilience

Conversation

@ZohebShaikh

@ZohebShaikh ZohebShaikh commented May 16, 2024 •

Copy link
Copy Markdown
Contributor

closes DiamondLightSource/blueapi#444

The main change in this PR is that make_all_devices returns tuple of (all_devices, exception_devices)

Instructions to reviewer on how to test:

  1. Checkout branch device-resilience in dodal
  2. In a common python virtual environment install both blueapi and dodal
    pip install -e ../dodal[dev] pip install -e .[dev]
  3. Test device does not fail when exception is occurred

Checks for reviewer

@ZohebShaikh
ZohebShaikh marked this pull request as ready for review May 16, 2024 16:45
Comment thread tests/test_utils.py Outdated
devices = make_all_devices(fake_beamline)
assert not devices
assert not devices[0]
for exception in devices[1]:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had a look at this, the device fake_beamlines don't create exceptions so I will need to add some devices in the fake_beamline that raises a exception when it does not connect

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need the following test cases:

  • All devices pass
  • All devices fail
  • Mixture
    If you have to make a few small fake beamline modules to cover those then go for it

@callumforrester callumforrester left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just the tests and a couple of nitpicks

Comment on lines +71 to +72
devices = make_all_devices(i03, fake_with_ophyd_sim=True)
assert len(beamline_utils.ACTIVE_DEVICES) == len(devices.keys())
assert len(beamline_utils.ACTIVE_DEVICES) == len(devices[0].keys())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You could instead have

devices, exceptions = make_all_devices(i03, fake_with_ophyd_sim=True)
assert len(beamline_utils.ACTIVE_DEVICES) == len(devices.keys()) and len(exceptions) == 0

...which is more readable, ditto for the other tests

Comment thread src/dodal/utils.py Resolved
Comment thread tests/test_utils.py Outdated
devices = make_all_devices(fake_beamline)
assert not devices
assert not devices[0]
for exception in devices[1]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need the following test cases:

  • All devices pass
  • All devices fail
  • Mixture
    If you have to make a few small fake beamline modules to cover those then go for it

Comment thread src/dodal/utils.py Outdated
) -> Dict[str, AnyDevice]:
) -> Tuple[Dict[str, AnyDevice], Dict[str, Exception]]:
devices: dict[str, AnyDevice] = {}
exception_devices: dict[str, Exception] = {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think this makes more sense

Suggested change
exception_devices: dict[str, Exception] = {}
device_exceptions: dict[str, Exception] = {}

Comment thread src/dodal/utils.py Outdated
all_devices = {
device.name: device
for device in devices.values()
if not isinstance(device, Exception)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this line

@callumforrester

Copy link
Copy Markdown
Contributor

I'm taking over this PR in @ZohebShaikh's absence

@callumforrester
callumforrester dismissed their stale review May 20, 2024 11:59

Taking over PR

@callumforrester
callumforrester force-pushed the device-resilience branch 3 times, most recently from 3aaad38 to a2b7316 Compare May 24, 2024 10:43

@DiamondJoseph DiamondJoseph left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good just some edge cases and minor optimisations

Comment thread src/dodal/utils.py Outdated
device
for device, device_dependencies in dependencies.items()
if device not in devices.keys()
if (device not in devices.keys() and device not in exceptions.keys())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
if (device not in devices.keys() and device not in exceptions.keys())
if device not in devices and device not in exceptions

a in dict == a in dict.keys()

for _, device in make_all_devices(i03, fake_with_ophyd_sim=True).items()
]
devices, _ = make_all_devices(i03, fake_with_ophyd_sim=True)
return [id(device) for _, device in devices.items()]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return [id(device) for _, device in devices.items()]
return [id(device) for device in devices.values()]

The view on the values should be sorted the same as items() or keys() (order of insertion)

devices, _ = make_all_devices(i03, fake_with_ophyd_sim=True)
return [id(device) for _, device in devices.items()]

ids_1 = [_make_devices_and_get_id()]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to the change, but this is list[list[id]] ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, could be simplified, separate "good first issue"?

@@ -0,0 +1,21 @@
from bluesky.protocols import Readable

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

"rasie" in filename

Comment on lines +12 to +21
def device_a() -> Readable:
raise DestroyedError


def device_b() -> EpicsMotor:
raise DisconnectedError


def device_c() -> Cryo:
raise UnknownStatusFailure

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently one Protocol, 2 ophyd devices, add an ophyd-async device class?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we are moving in the direction of exclusive ophyd-async (and therefore hopefully dodal's handling of ophyd devices can go away one day) I might just make it ophyd-async exclusively

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to see both tested until there are no ophyd devices, given they are different execution paths

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have reverted cryostream

@callumforrester
callumforrester force-pushed the device-resilience branch 2 times, most recently from d74afb7 to da4e441 Compare May 28, 2024 08:20

@DominicOram DominicOram left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ta, I think mostly just need to fix the cli

Comment thread src/dodal/utils.py
def make_all_devices(
module: str | ModuleType | None = None, include_skipped: bool = False, **kwargs
) -> Dict[str, AnyDevice]:
) -> Tuple[Dict[str, AnyDevice], Dict[str, Exception]]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must: This breaks connect with the error:

Traceback (most recent call last):
  File "/scratch/ffv81422/hyperion/hyperion/.venv/bin/dodal", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/scratch/ffv81422/hyperion/hyperion/.venv/lib/python3.11/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/ffv81422/hyperion/hyperion/.venv/lib/python3.11/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/scratch/ffv81422/hyperion/hyperion/.venv/lib/python3.11/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/ffv81422/hyperion/hyperion/.venv/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/ffv81422/hyperion/hyperion/.venv/lib/python3.11/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/scratch/ffv81422/hyperion/dodal/src/dodal/cli.py", line 62, in connect
    print("\n".join([f"\t{key}" for key in devices.keys()]))
                                           ^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'keys'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, concerning that this didn't make any tests fail, I'll investigate

Comment thread src/dodal/utils.py Resolved
@stan-dot
stan-dot force-pushed the device-resilience branch from da4e441 to ff43ad9 Compare May 28, 2024 14:19
@callumforrester
callumforrester force-pushed the device-resilience branch 2 times, most recently from 91a0098 to f23ec12 Compare May 29, 2024 11:51

@DominicOram DominicOram left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you, happy for this to be merged now but if you want to do the dataclass change then that's fine too

@DiamondJoseph
DiamondJoseph merged commit 76be228 into main May 30, 2024
@DiamondJoseph
DiamondJoseph deleted the device-resilience branch May 30, 2024 15:44
callumforrester added a commit to DiamondLightSource/blueapi that referenced this pull request May 31, 2024
closes #444

With PR for dodal at
DiamondLightSource/dodal#550

---------

Co-authored-by: Callum Forrester <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't Fall Over if a Device Does not Connect

4 participants