| 1 |
import sys |
| 2 |
from time import monotonic |
| 3 |
from unittest.mock import Mock, patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from celery.concurrency.base import BasePool |
| 8 |
from celery.utils.objects import Bunch |
| 9 |
from celery.worker import autoscale, state |
| 10 |
|
| 11 |
|
| 12 |
class MockPool(BasePool): |
| 13 |
|
| 14 |
shrink_raises_exception = False |
| 15 |
shrink_raises_ValueError = False |
| 16 |
|
| 17 |
def __init__(self, *args, **kwargs): |
| 18 |
super().__init__(*args, **kwargs) |
| 19 |
self._pool = Bunch(_processes=self.limit) |
| 20 |
|
| 21 |
def grow(self, n=1): |
| 22 |
self._pool._processes += n |
| 23 |
|
| 24 |
def shrink(self, n=1): |
| 25 |
if self.shrink_raises_exception: |
| 26 |
raise KeyError('foo') |
| 27 |
if self.shrink_raises_ValueError: |
| 28 |
raise ValueError('foo') |
| 29 |
self._pool._processes -= n |
| 30 |
|
| 31 |
@property |
| 32 |
def num_processes(self): |
| 33 |
return self._pool._processes |
| 34 |
|
| 35 |
|
| 36 |
class test_WorkerComponent: |
| 37 |
|
| 38 |
def test_register_with_event_loop(self): |
| 39 |
parent = Mock(name='parent') |
| 40 |
parent.autoscale = True |
| 41 |
parent.consumer.on_task_message = set() |
| 42 |
w = autoscale.WorkerComponent(parent) |
| 43 |
assert parent.autoscaler is None |
| 44 |
assert w.enabled |
| 45 |
|
| 46 |
hub = Mock(name='hub') |
| 47 |
w.create(parent) |
| 48 |
w.register_with_event_loop(parent, hub) |
| 49 |
assert (parent.autoscaler.maybe_scale in |
| 50 |
parent.consumer.on_task_message) |
| 51 |
hub.call_repeatedly.assert_called_with( |
| 52 |
parent.autoscaler.keepalive, parent.autoscaler.maybe_scale, |
| 53 |
) |
| 54 |
|
| 55 |
parent.hub = hub |
| 56 |
hub.on_init = [] |
| 57 |
w.instantiate = Mock() |
| 58 |
w.register_with_event_loop(parent, Mock(name='loop')) |
| 59 |
assert parent.consumer.on_task_message |
| 60 |
|
| 61 |
def test_info_without_event_loop(self): |
| 62 |
parent = Mock(name='parent') |
| 63 |
parent.autoscale = True |
| 64 |
parent.max_concurrency = '10' |
| 65 |
parent.min_concurrency = '2' |
| 66 |
parent.use_eventloop = False |
| 67 |
w = autoscale.WorkerComponent(parent) |
| 68 |
w.create(parent) |
| 69 |
info = w.info(parent) |
| 70 |
assert 'autoscaler' in info |
| 71 |
assert parent.autoscaler_cls().info.called |
| 72 |
|
| 73 |
|
| 74 |
class test_Autoscaler: |
| 75 |
|
| 76 |
def setup_method(self): |
| 77 |
self.pool = MockPool(3) |
| 78 |
|
| 79 |
def test_stop(self): |
| 80 |
|
| 81 |
class Scaler(autoscale.Autoscaler): |
| 82 |
alive = True |
| 83 |
joined = False |
| 84 |
|
| 85 |
def is_alive(self): |
| 86 |
return self.alive |
| 87 |
|
| 88 |
def join(self, timeout=None): |
| 89 |
self.joined = True |
| 90 |
|
| 91 |
worker = Mock(name='worker') |
| 92 |
x = Scaler(self.pool, 10, 3, worker=worker) |
| 93 |
# Don't allow thread joining or event waiting to block the test |
| 94 |
with patch("threading.Thread.join"), patch("threading.Event.wait"): |
| 95 |
x.stop() |
| 96 |
assert x.joined |
| 97 |
x.joined = False |
| 98 |
x.alive = False |
| 99 |
with patch("threading.Thread.join"), patch("threading.Event.wait"): |
| 100 |
x.stop() |
| 101 |
assert not x.joined |
| 102 |
|
| 103 |
@pytest.mark.sleepdeprived_patched_module(autoscale) |
| 104 |
def test_body(self, sleepdeprived): |
| 105 |
worker = Mock(name='worker') |
| 106 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 107 |
x.body() |
| 108 |
assert x.pool.num_processes == 3 |
| 109 |
_keep = [Mock(name=f'req{i}') for i in range(20)] |
| 110 |
[state.task_reserved(m) for m in _keep] |
| 111 |
x.body() |
| 112 |
x.body() |
| 113 |
assert x.pool.num_processes == 10 |
| 114 |
state.reserved_requests.clear() |
| 115 |
x.body() |
| 116 |
assert x.pool.num_processes == 10 |
| 117 |
x._last_scale_up = monotonic() - 10000 |
| 118 |
x.body() |
| 119 |
assert x.pool.num_processes == 3 |
| 120 |
|
| 121 |
def test_run(self): |
| 122 |
|
| 123 |
class Scaler(autoscale.Autoscaler): |
| 124 |
scale_called = False |
| 125 |
|
| 126 |
def body(self): |
| 127 |
self.scale_called = True |
| 128 |
getattr(self, "_bgThread__is_shutdown").set() |
| 129 |
|
| 130 |
worker = Mock(name='worker') |
| 131 |
x = Scaler(self.pool, 10, 3, worker=worker) |
| 132 |
x.run() |
| 133 |
assert getattr(x, "_bgThread__is_shutdown").is_set() |
| 134 |
assert getattr(x, "_bgThread__is_stopped").is_set() |
| 135 |
assert x.scale_called |
| 136 |
|
| 137 |
def test_shrink_raises_exception(self): |
| 138 |
worker = Mock(name='worker') |
| 139 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 140 |
x.scale_up(3) |
| 141 |
x.pool.shrink_raises_exception = True |
| 142 |
x._shrink(1) |
| 143 |
|
| 144 |
@patch('celery.worker.autoscale.debug') |
| 145 |
def test_shrink_raises_ValueError(self, debug): |
| 146 |
worker = Mock(name='worker') |
| 147 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 148 |
x.scale_up(3) |
| 149 |
x._last_scale_up = monotonic() - 10000 |
| 150 |
x.pool.shrink_raises_ValueError = True |
| 151 |
x.scale_down(1) |
| 152 |
assert debug.call_count |
| 153 |
|
| 154 |
def test_update(self): |
| 155 |
worker = Mock(name='worker') |
| 156 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 157 |
x.worker.consumer.prefetch_multiplier = 1 |
| 158 |
x.keepalive = -1 |
| 159 |
assert x.processes == 3 |
| 160 |
x.scale_up(5) |
| 161 |
x.update(7, None) |
| 162 |
assert x.processes == 7 |
| 163 |
assert x.max_concurrency == 7 |
| 164 |
x.scale_down(4) |
| 165 |
x.update(None, 6) |
| 166 |
assert x.processes == 6 |
| 167 |
assert x.min_concurrency == 6 |
| 168 |
|
| 169 |
x.update(max=300, min=10) |
| 170 |
x.update(max=300, min=2) |
| 171 |
x.update(max=None, min=None) |
| 172 |
|
| 173 |
def test_prefetch_count_on_updates(self): |
| 174 |
worker = Mock(name='worker') |
| 175 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 176 |
x.worker.consumer.prefetch_multiplier = 1 |
| 177 |
x.update(5, None) |
| 178 |
worker.consumer._update_prefetch_count.assert_called_with(-5) |
| 179 |
x.update(15, 7) |
| 180 |
worker.consumer._update_prefetch_count.assert_called_with(10) |
| 181 |
|
| 182 |
def test_prefetch_count_on_updates_prefetch_multiplier_gt_one(self): |
| 183 |
worker = Mock(name='worker') |
| 184 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 185 |
x.worker.consumer.prefetch_multiplier = 4 |
| 186 |
x.update(5, None) |
| 187 |
worker.consumer._update_prefetch_count.assert_called_with(-5) |
| 188 |
x.update(15, 7) |
| 189 |
worker.consumer._update_prefetch_count.assert_called_with(10) |
| 190 |
|
| 191 |
def test_info(self): |
| 192 |
worker = Mock(name='worker') |
| 193 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 194 |
info = x.info() |
| 195 |
assert info['max'] == 10 |
| 196 |
assert info['min'] == 3 |
| 197 |
assert info['current'] == 3 |
| 198 |
|
| 199 |
@patch('os._exit') |
| 200 |
def test_thread_crash(self, _exit): |
| 201 |
|
| 202 |
class _Autoscaler(autoscale.Autoscaler): |
| 203 |
|
| 204 |
def body(self): |
| 205 |
getattr(self, "_bgThread__is_shutdown").set() |
| 206 |
raise OSError('foo') |
| 207 |
worker = Mock(name='worker') |
| 208 |
x = _Autoscaler(self.pool, 10, 3, worker=worker) |
| 209 |
|
| 210 |
stderr = Mock() |
| 211 |
p, sys.stderr = sys.stderr, stderr |
| 212 |
try: |
| 213 |
x.run() |
| 214 |
finally: |
| 215 |
sys.stderr = p |
| 216 |
_exit.assert_called_with(1) |
| 217 |
stderr.write.assert_called() |
| 218 |
|
| 219 |
@pytest.mark.sleepdeprived_patched_module(autoscale) |
| 220 |
def test_no_negative_scale(self, sleepdeprived): |
| 221 |
total_num_processes = [] |
| 222 |
worker = Mock(name='worker') |
| 223 |
x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) |
| 224 |
x.body() # the body func scales up or down |
| 225 |
|
| 226 |
_keep = [Mock(name=f'req{i}') for i in range(35)] |
| 227 |
for req in _keep: |
| 228 |
state.task_reserved(req) |
| 229 |
x.body() |
| 230 |
total_num_processes.append(self.pool.num_processes) |
| 231 |
|
| 232 |
for req in _keep: |
| 233 |
state.task_ready(req) |
| 234 |
x.body() |
| 235 |
total_num_processes.append(self.pool.num_processes) |
| 236 |
|
| 237 |
assert all(x.min_concurrency <= i <= x.max_concurrency |
| 238 |
for i in total_num_processes) |
| 239 |
|
| 240 |
def test_disable_prefetch_respects_max_concurrency(self): |
| 241 |
"""Test that disable_prefetch respects autoscale max_concurrency setting""" |
| 242 |
from celery.worker.consumer.tasks import Tasks |
| 243 |
|
| 244 |
# Create a mock consumer with autoscale and disable_prefetch enabled |
| 245 |
consumer = Mock() |
| 246 |
consumer.app = Mock() |
| 247 |
consumer.app.conf.worker_disable_prefetch = True |
| 248 |
consumer.pool = Mock() |
| 249 |
consumer.pool.num_processes = 10 |
| 250 |
consumer.controller = Mock() |
| 251 |
consumer.controller.max_concurrency = 5 # Lower than pool processes |
| 252 |
|
| 253 |
# Mock task consumer setup |
| 254 |
consumer.task_consumer = Mock() |
| 255 |
consumer.task_consumer.channel = Mock() |
| 256 |
consumer.task_consumer.channel.qos = Mock() |
| 257 |
consumer.task_consumer.channel.qos.can_consume = Mock(return_value=True) |
| 258 |
|
| 259 |
# Mock the connection and other required attributes |
| 260 |
consumer.connection = Mock() |
| 261 |
consumer.connection.default_channel = Mock() |
| 262 |
consumer.connection.transport = Mock() |
| 263 |
consumer.connection.transport.driver_type = 'redis' |
| 264 |
consumer.initial_prefetch_count = 20 |
| 265 |
consumer.update_strategies = Mock() |
| 266 |
consumer.on_decode_error = Mock() |
| 267 |
|
| 268 |
# Mock the amqp TaskConsumer |
| 269 |
consumer.app.amqp = Mock() |
| 270 |
consumer.app.amqp.TaskConsumer = Mock(return_value=consumer.task_consumer) |
| 271 |
|
| 272 |
tasks_instance = Tasks(consumer) |
| 273 |
|
| 274 |
# Mock 5 reserved requests (at autoscale limit of 5) |
| 275 |
mock_requests = [Mock() for _ in range(5)] |
| 276 |
with patch('celery.worker.state.reserved_requests', mock_requests): |
| 277 |
tasks_instance.start(consumer) |
| 278 |
|
| 279 |
# Should not be able to consume when at autoscale limit |
| 280 |
assert consumer.task_consumer.channel.qos.can_consume() is False |
| 281 |
|
| 282 |
# Test with 4 reserved requests (under autoscale limit of 5) |
| 283 |
mock_requests = [Mock() for _ in range(4)] |
| 284 |
with patch('celery.worker.state.reserved_requests', mock_requests): |
| 285 |
tasks_instance.start(consumer) |
| 286 |
|
| 287 |
# Should be able to consume when under autoscale limit |
| 288 |
assert consumer.task_consumer.channel.qos.can_consume() is True |