|
| 1 | +# |
| 2 | +# Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending |
| 3 | +# |
| 4 | +"""This module allows user to enable/disable Deephaven systemic object marking. When enabled, Deephaven will mark |
| 5 | +all objects created in the current thread as systemic. These systemic objects will be tracked and if errors occur to |
| 6 | +them, the errors are deemed to be systemic and fatal. |
| 7 | +""" |
| 8 | +import contextlib |
| 9 | + |
| 10 | +import jpy |
| 11 | +from deephaven import DHError |
| 12 | + |
| 13 | +_JSystemicObjectTracker = jpy.get_type("io.deephaven.engine.util.systemicmarking.SystemicObjectTracker") |
| 14 | + |
| 15 | + |
| 16 | +def is_systemic_object_marking_enabled() -> bool: |
| 17 | + """Returns True if the systemic object marking is enabled. When enabled, the current thread can be marked as |
| 18 | + systemic or not systemic. |
| 19 | + """ |
| 20 | + return _JSystemicObjectTracker.isSystemicObjectMarkingEnabled() |
| 21 | + |
| 22 | + |
| 23 | +def is_systemic() -> bool: |
| 24 | + """Returns whether the current thread is systemic. If true, objects created on this thread are treated as |
| 25 | + systemic.""" |
| 26 | + return _JSystemicObjectTracker.isSystemicThread() |
| 27 | + |
| 28 | + |
| 29 | +def set_systemic(systemic: bool) -> None: |
| 30 | + """Sets whether the current thread is systemic. If true, objects created on this thread are treated as systemic. |
| 31 | +
|
| 32 | + Args: |
| 33 | + systemic (bool): True to mark the current thread as systemic, False to mark it as not systemic. |
| 34 | +
|
| 35 | + Raises: |
| 36 | + DHError: If the systemic object marking is not enabled. |
| 37 | + """ |
| 38 | + if is_systemic_object_marking_enabled(): |
| 39 | + if systemic: |
| 40 | + _JSystemicObjectTracker.markThreadSystemic() |
| 41 | + else: |
| 42 | + _JSystemicObjectTracker.markThreadNotSystemic() |
| 43 | + else: |
| 44 | + raise DHError(message="Systemic object marking is not enabled.") |
| 45 | + |
| 46 | + |
| 47 | +@contextlib.contextmanager |
| 48 | +def systemic_object_marking() -> None: |
| 49 | + """A Context manager to ensure the current thread is marked as systemic for the execution of the enclosed code |
| 50 | + block. On exit, the thread is restored to its previous systemic state. |
| 51 | +
|
| 52 | + Raises: |
| 53 | + DHError: If the systemic object marking is not enabled. |
| 54 | + """ |
| 55 | + if is_systemic_object_marking_enabled(): |
| 56 | + if not is_systemic(): |
| 57 | + try: |
| 58 | + _JSystemicObjectTracker.markThreadSystemic() |
| 59 | + yield |
| 60 | + finally: |
| 61 | + _JSystemicObjectTracker.markThreadNotSystemic() |
| 62 | + else: |
| 63 | + yield |
| 64 | + else: |
| 65 | + raise DHError(message="Systemic object marking is not enabled.") |
| 66 | + |
| 67 | + |
| 68 | +@contextlib.contextmanager |
| 69 | +def no_systemic_object_marking() -> None: |
| 70 | + """A Context manager to ensure the current thread is marked as not systemic for the execution of the enclosed code |
| 71 | + block. On exit, the thread is restored to its previous systemic state. |
| 72 | +
|
| 73 | + Raises: |
| 74 | + DHError: If the systemic object marking is not enabled. |
| 75 | + """ |
| 76 | + if is_systemic_object_marking_enabled(): |
| 77 | + if is_systemic(): |
| 78 | + try: |
| 79 | + _JSystemicObjectTracker.markThreadNotSystemic() |
| 80 | + yield |
| 81 | + finally: |
| 82 | + _JSystemicObjectTracker.markThreadSystemic() |
| 83 | + else: |
| 84 | + yield |
| 85 | + else: |
| 86 | + raise DHError(message="Systemic object marking is not enabled.") |
0 commit comments