1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
from __future__ import annotations
import logging
import pytest
from distributed import Nanny, NannyPlugin
from distributed.protocol.pickle import dumps
from distributed.utils_test import captured_logger, gen_cluster
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_register_worker_plugin_is_deprecated(c, s, a):
class DuckPlugin(NannyPlugin):
def setup(self, nanny):
nanny.foo = 123
def teardown(self, nanny):
pass
n_existing_plugins = len(a.plugins)
assert not hasattr(a, "foo")
with pytest.warns(DeprecationWarning, match="register_worker_plugin.*deprecated"):
await c.register_worker_plugin(DuckPlugin())
assert len(a.plugins) == n_existing_plugins + 1
assert a.foo == 123
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_register_worker_plugin_typing_over_nanny_keyword(c, s, a):
class DuckPlugin(NannyPlugin):
def setup(self, nanny):
nanny.foo = 123
def teardown(self, nanny):
pass
n_existing_plugins = len(a.plugins)
assert not hasattr(a, "foo")
with (
pytest.warns(UserWarning, match="`NannyPlugin` as a worker plugin"),
pytest.warns(DeprecationWarning, match="please use `Client.register_plugin`"),
):
await c.register_worker_plugin(DuckPlugin(), nanny=False)
assert len(a.plugins) == n_existing_plugins + 1
assert a.foo == 123
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_duck_typed_register_nanny_plugin_is_deprecated(c, s, a):
class DuckPlugin:
def setup(self, nanny):
nanny.foo = 123
def teardown(self, nanny):
pass
n_existing_plugins = len(a.plugins)
assert not hasattr(a, "foo")
with (
pytest.warns(DeprecationWarning, match="duck-typed.*NannyPlugin"),
pytest.warns(DeprecationWarning, match="please use `Client.register_plugin`"),
):
await c.register_worker_plugin(DuckPlugin(), nanny=True)
assert len(a.plugins) == n_existing_plugins + 1
assert a.foo == 123
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_register_idempotent_plugin(c, s, a):
class IdempotentPlugin(NannyPlugin):
def __init__(self, instance=None):
self.name = "idempotentplugin"
self.instance = instance
self.idempotent = True
def setup(self, nanny):
if self.instance != "first":
raise RuntimeError(
"Only the first plugin should be started when idempotent is set"
)
first = IdempotentPlugin(instance="first")
await c.register_plugin(first)
assert "idempotentplugin" in a.plugins
second = IdempotentPlugin(instance="second")
await c.register_plugin(second)
assert "idempotentplugin" in a.plugins
assert a.plugins["idempotentplugin"].instance == "first"
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_register_non_idempotent_plugin(c, s, a):
class NonIdempotentPlugin(NannyPlugin):
def __init__(self, instance=None):
self.name = "nonidempotentplugin"
self.instance = instance
first = NonIdempotentPlugin(instance="first")
await c.register_plugin(first)
assert "nonidempotentplugin" in a.plugins
second = NonIdempotentPlugin(instance="second")
await c.register_plugin(second)
assert "nonidempotentplugin" in a.plugins
assert a.plugins["nonidempotentplugin"].instance == "second"
third = NonIdempotentPlugin(instance="third")
with pytest.warns(
FutureWarning,
match="`Scheduler.register_nanny_plugin` now requires `idempotent`",
):
await s.register_nanny_plugin(
comm=None, plugin=dumps(third), name="nonidempotentplugin"
)
assert "nonidempotentplugin" in a.plugins
assert a.plugins["nonidempotentplugin"].instance == "third"
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_register_plugin_with_idempotent_keyword_is_deprecated(c, s, a):
class NonIdempotentPlugin(NannyPlugin):
def __init__(self, instance=None):
self.name = "nonidempotentplugin"
self.instance = instance
# We want to overrule this
self.idempotent = True
first = NonIdempotentPlugin(instance="first")
with pytest.warns(FutureWarning, match="`idempotent` argument is deprecated"):
await c.register_plugin(first, idempotent=False)
assert "nonidempotentplugin" in a.plugins
second = NonIdempotentPlugin(instance="second")
with pytest.warns(FutureWarning, match="`idempotent` argument is deprecated"):
await c.register_plugin(second, idempotent=False)
assert "nonidempotentplugin" in a.plugins
assert a.plugins["nonidempotentplugin"].instance == "second"
class IdempotentPlugin(NannyPlugin):
def __init__(self, instance=None):
self.name = "idempotentplugin"
self.instance = instance
# We want to overrule this
self.idempotent = False
def setup(self, nanny):
if self.instance != "first":
raise RuntimeError(
"Only the first plugin should be started when idempotent is set"
)
first = IdempotentPlugin(instance="first")
with pytest.warns(FutureWarning, match="`idempotent` argument is deprecated"):
await c.register_plugin(first, idempotent=True)
assert "idempotentplugin" in a.plugins
second = IdempotentPlugin(instance="second")
with pytest.warns(FutureWarning, match="`idempotent` argument is deprecated"):
await c.register_plugin(second, idempotent=True)
assert "idempotentplugin" in a.plugins
assert a.plugins["idempotentplugin"].instance == "first"
class BrokenSetupPlugin(NannyPlugin):
def setup(self, nanny):
raise RuntimeError("test error")
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_register_plugin_with_broken_setup_to_existing_nannies_raises(c, s, a):
with pytest.raises(RuntimeError, match="test error"):
with captured_logger("distributed.nanny", level=logging.ERROR) as caplog:
await c.register_plugin(BrokenSetupPlugin(), name="TestPlugin1")
logs = caplog.getvalue()
assert "TestPlugin1 failed to setup" in logs
assert "test error" in logs
@gen_cluster(client=True, nthreads=[])
async def test_plugin_with_broken_setup_on_new_nanny_logs(c, s):
await c.register_plugin(BrokenSetupPlugin(), name="TestPlugin1")
with captured_logger("distributed.nanny", level=logging.ERROR) as caplog:
async with Nanny(s.address):
pass
logs = caplog.getvalue()
assert "TestPlugin1 failed to setup" in logs
assert "test error" in logs
class BrokenTeardownPlugin(NannyPlugin):
def teardown(self, nanny):
raise RuntimeError("test error")
@gen_cluster(client=True, nthreads=[("", 1)], Worker=Nanny)
async def test_unregister_nanny_plugin_with_broken_teardown_raises(c, s, a):
await c.register_plugin(BrokenTeardownPlugin(), name="TestPlugin1")
with pytest.raises(RuntimeError, match="test error"):
with captured_logger("distributed.nanny", level=logging.ERROR) as caplog:
await c.unregister_worker_plugin("TestPlugin1", nanny=True)
logs = caplog.getvalue()
assert "TestPlugin1 failed to teardown" in logs
assert "test error" in logs
@gen_cluster(client=True, nthreads=[])
async def test_nanny_plugin_with_broken_teardown_logs_on_close(c, s):
await c.register_plugin(BrokenTeardownPlugin(), name="TestPlugin1")
with captured_logger("distributed.nanny", level=logging.ERROR) as caplog:
async with Nanny(s.address):
pass
logs = caplog.getvalue()
assert "TestPlugin1 failed to teardown" in logs
assert "test error" in logs
|