9397: Tweak method documentation for Extract()
[arvados.git] / services / nodemanager / tests / test_daemon.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import time
6 import unittest
7
8 import mock
9 import pykka
10
11 import arvnodeman.daemon as nmdaemon
12 from arvnodeman.jobqueue import ServerCalculator
13 from arvnodeman.computenode.dispatch import ComputeNodeMonitorActor
14 from . import testutil
15 import logging
16
17 class NodeManagerDaemonActorTestCase(testutil.ActorTestMixin,
18                                      unittest.TestCase):
19     def mock_node_start(self, **kwargs):
20         # Make sure that every time the daemon starts a setup actor,
21         # it gets a new mock object back.
22         get_cloud_size = mock.MagicMock()
23         get_cloud_size.get.return_value = kwargs["cloud_size"]
24         mock_actor = mock.MagicMock()
25         mock_proxy = mock.NonCallableMock(name='setup_mock_proxy',
26                                           cloud_size=get_cloud_size,
27                                           actor_ref=mock_actor)
28         mock_actor.proxy.return_value = mock_proxy
29         mock_actor.tell_proxy.return_value = mock_proxy
30
31         self.last_setup = mock_proxy
32         return mock_actor
33
34     def mock_node_shutdown(self, **kwargs):
35         # Make sure that every time the daemon starts a shutdown actor,
36         # it gets a new mock object back.
37         get_cloud_node = mock.MagicMock()
38         if "node_monitor" in kwargs:
39             get_cloud_node.get.return_value = kwargs["node_monitor"].proxy().cloud_node.get()
40         mock_actor = mock.MagicMock()
41         mock_proxy = mock.NonCallableMock(name='shutdown_mock_proxy',
42                                           cloud_node=get_cloud_node,
43                                           actor_ref=mock_actor)
44
45         mock_actor.proxy.return_value = mock_proxy
46         self.last_shutdown = mock_proxy
47
48         return mock_actor
49
50     def make_daemon(self, cloud_nodes=[], arvados_nodes=[], want_sizes=[],
51                     avail_sizes=[(testutil.MockSize(1), {"cores": 1})],
52                     min_nodes=0, max_nodes=8,
53                     shutdown_windows=[54, 5, 1],
54                     max_total_price=None):
55         for name in ['cloud_nodes', 'arvados_nodes', 'server_wishlist']:
56             setattr(self, name + '_poller', mock.MagicMock(name=name + '_mock'))
57         self.arv_factory = mock.MagicMock(name='arvados_mock')
58         api_client = mock.MagicMock(name='api_client')
59         api_client.nodes().create().execute.side_effect = [testutil.arvados_node_mock(1),
60                                                            testutil.arvados_node_mock(2)]
61         self.arv_factory.return_value = api_client
62
63         self.cloud_factory = mock.MagicMock(name='cloud_mock')
64         self.cloud_factory().node_start_time.return_value = time.time()
65         self.cloud_updates = mock.MagicMock(name='updates_mock')
66         self.timer = testutil.MockTimer(deliver_immediately=False)
67         self.cloud_factory().node_id.side_effect = lambda node: node.id
68
69         self.node_setup = mock.MagicMock(name='setup_mock')
70         self.node_setup.start.side_effect = self.mock_node_start
71         self.node_setup.reset_mock()
72
73         self.node_shutdown = mock.MagicMock(name='shutdown_mock')
74         self.node_shutdown.start.side_effect = self.mock_node_shutdown
75
76         self.daemon = nmdaemon.NodeManagerDaemonActor.start(
77             self.server_wishlist_poller, self.arvados_nodes_poller,
78             self.cloud_nodes_poller, self.cloud_updates, self.timer,
79             self.arv_factory, self.cloud_factory,
80             shutdown_windows, ServerCalculator(avail_sizes),
81             min_nodes, max_nodes, 600, 1800, 3600,
82             self.node_setup, self.node_shutdown,
83             max_total_price=max_total_price).proxy()
84         if arvados_nodes is not None:
85             self.daemon.update_arvados_nodes(arvados_nodes).get(self.TIMEOUT)
86         if cloud_nodes is not None:
87             self.daemon.update_cloud_nodes(cloud_nodes).get(self.TIMEOUT)
88         if want_sizes is not None:
89             self.daemon.update_server_wishlist(want_sizes).get(self.TIMEOUT)
90
91     def monitor_list(self):
92         return pykka.ActorRegistry.get_by_class(ComputeNodeMonitorActor)
93
94     def monitored_arvados_nodes(self):
95         pairings = []
96         for future in [actor.proxy().arvados_node
97                        for actor in self.monitor_list()]:
98             try:
99                 pairings.append(future.get(self.TIMEOUT))
100             except pykka.ActorDeadError:
101                 pass
102         return pairings
103
104     def alive_monitor_count(self):
105         return len(self.monitored_arvados_nodes())
106
107     def assertShutdownCancellable(self, expected=True):
108         self.assertTrue(self.node_shutdown.start.called)
109         self.assertIs(expected,
110                       self.node_shutdown.start.call_args[1]['cancellable'],
111                       "ComputeNodeShutdownActor incorrectly cancellable")
112
113     def test_easy_node_creation(self):
114         size = testutil.MockSize(1)
115         self.make_daemon(want_sizes=[size])
116         self.stop_proxy(self.daemon)
117         self.assertTrue(self.node_setup.start.called)
118
119     def check_monitors_arvados_nodes(self, *arv_nodes):
120         self.assertItemsEqual(arv_nodes, self.monitored_arvados_nodes())
121
122     def test_node_pairing(self):
123         cloud_node = testutil.cloud_node_mock(1)
124         arv_node = testutil.arvados_node_mock(1)
125         self.make_daemon([cloud_node], [arv_node])
126         self.stop_proxy(self.daemon)
127         self.check_monitors_arvados_nodes(arv_node)
128
129     def test_node_pairing_after_arvados_update(self):
130         cloud_node = testutil.cloud_node_mock(2)
131         self.make_daemon([cloud_node],
132                          [testutil.arvados_node_mock(1, ip_address=None)])
133         arv_node = testutil.arvados_node_mock(2)
134         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
135         self.stop_proxy(self.daemon)
136         self.check_monitors_arvados_nodes(arv_node)
137
138     def test_arvados_node_un_and_re_paired(self):
139         # We need to create the Arvados node mock after spinning up the daemon
140         # to make sure it's new enough to pair with the cloud node.
141         self.make_daemon([testutil.cloud_node_mock(3)], arvados_nodes=None)
142         arv_node = testutil.arvados_node_mock(3)
143         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
144         self.check_monitors_arvados_nodes(arv_node)
145         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
146         self.assertEqual(0, self.alive_monitor_count())
147         self.daemon.update_cloud_nodes([testutil.cloud_node_mock(3)])
148         self.stop_proxy(self.daemon)
149         self.check_monitors_arvados_nodes(arv_node)
150
151     def test_old_arvados_node_not_double_assigned(self):
152         arv_node = testutil.arvados_node_mock(3, age=9000)
153         size = testutil.MockSize(3)
154         self.make_daemon(arvados_nodes=[arv_node], avail_sizes=[(size, {"cores":1})])
155         self.daemon.update_server_wishlist([size]).get(self.TIMEOUT)
156         self.daemon.update_server_wishlist([size, size]).get(self.TIMEOUT)
157         self.stop_proxy(self.daemon)
158         used_nodes = [call[1].get('arvados_node')
159                       for call in self.node_setup.start.call_args_list]
160         self.assertEqual(2, len(used_nodes))
161         self.assertIn(arv_node, used_nodes)
162         self.assertIn(None, used_nodes)
163
164     def test_node_count_satisfied(self):
165         self.make_daemon([testutil.cloud_node_mock()],
166                          want_sizes=[testutil.MockSize(1)])
167         self.stop_proxy(self.daemon)
168         self.assertFalse(self.node_setup.start.called)
169
170     def test_dont_count_missing_as_busy(self):
171         size = testutil.MockSize(1)
172         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock(1),
173                                       testutil.cloud_node_mock(2)],
174                          arvados_nodes=[testutil.arvados_node_mock(1),
175                                       testutil.arvados_node_mock(2,
176                                                                  last_ping_at='1970-01-01T01:02:03.04050607Z')],
177                          want_sizes=[size, size])
178         self.stop_proxy(self.daemon)
179         self.assertTrue(self.node_setup.start.called)
180
181     def test_missing_counts_towards_max(self):
182         size = testutil.MockSize(1)
183         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock(1),
184                                       testutil.cloud_node_mock(2)],
185                          arvados_nodes=[testutil.arvados_node_mock(1),
186                                         testutil.arvados_node_mock(2, last_ping_at='1970-01-01T01:02:03.04050607Z')],
187                          want_sizes=[size, size],
188                          max_nodes=2)
189         self.stop_proxy(self.daemon)
190         self.assertFalse(self.node_setup.start.called)
191
192     def test_excess_counts_missing(self):
193         size = testutil.MockSize(1)
194         cloud_nodes = [testutil.cloud_node_mock(1), testutil.cloud_node_mock(2)]
195         self.make_daemon(cloud_nodes=cloud_nodes,
196                          arvados_nodes=[testutil.arvados_node_mock(1),
197                                         testutil.arvados_node_mock(2, last_ping_at='1970-01-01T01:02:03.04050607Z')],
198                          want_sizes=[size])
199         self.assertEqual(2, self.alive_monitor_count())
200         for mon_ref in self.monitor_list():
201             self.daemon.node_can_shutdown(mon_ref.proxy()).get(self.TIMEOUT)
202         self.assertEqual(1, self.node_shutdown.start.call_count)
203
204     def test_missing_shutdown_not_excess(self):
205         size = testutil.MockSize(1)
206         cloud_nodes = [testutil.cloud_node_mock(1), testutil.cloud_node_mock(2)]
207         self.make_daemon(cloud_nodes=cloud_nodes,
208                          arvados_nodes=[testutil.arvados_node_mock(1),
209                                         testutil.arvados_node_mock(2, last_ping_at='1970-01-01T01:02:03.04050607Z')],
210                          want_sizes=[size])
211
212         get_cloud_node = mock.MagicMock(name="get_cloud_node")
213         get_cloud_node.get.return_value = cloud_nodes[1]
214         mock_node_monitor = mock.MagicMock()
215         mock_node_monitor.proxy.return_value = mock.NonCallableMock(cloud_node=get_cloud_node)
216         mock_shutdown = self.node_shutdown.start(node_monitor=mock_node_monitor)
217
218         self.daemon.cloud_nodes.get()[cloud_nodes[1].id].shutdown_actor = mock_shutdown.proxy()
219
220         self.assertEqual(2, self.alive_monitor_count())
221         for mon_ref in self.monitor_list():
222             self.daemon.node_can_shutdown(mon_ref.proxy()).get(self.TIMEOUT)
223         self.assertEqual(1, self.node_shutdown.start.call_count)
224
225     def test_booting_nodes_counted(self):
226         cloud_node = testutil.cloud_node_mock(1)
227         arv_node = testutil.arvados_node_mock(1)
228         server_wishlist = [testutil.MockSize(1)] * 2
229         self.make_daemon([cloud_node], [arv_node], server_wishlist)
230         self.daemon.max_nodes.get(self.TIMEOUT)
231         self.assertTrue(self.node_setup.start.called)
232         self.daemon.update_server_wishlist(server_wishlist).get(self.TIMEOUT)
233         self.stop_proxy(self.daemon)
234         self.assertEqual(1, self.node_setup.start.call_count)
235
236     def test_boot_new_node_when_all_nodes_busy(self):
237         size = testutil.MockSize(2)
238         arv_node = testutil.arvados_node_mock(2, job_uuid=True)
239         self.make_daemon([testutil.cloud_node_mock(2, size=size)], [arv_node],
240                          [size], avail_sizes=[(size, {"cores":1})])
241         self.busywait(lambda: self.node_setup.start.called)
242         self.stop_proxy(self.daemon)
243         self.assertTrue(self.node_setup.start.called)
244
245     def test_boot_new_node_below_min_nodes(self):
246         min_size = testutil.MockSize(1)
247         wish_size = testutil.MockSize(3)
248         avail_sizes = [(min_size, {"cores": 1}),
249                        (wish_size, {"cores": 3})]
250         self.make_daemon([], [], None, avail_sizes=avail_sizes, min_nodes=2)
251         self.daemon.update_server_wishlist([wish_size]).get(self.TIMEOUT)
252         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
253         self.daemon.update_server_wishlist([wish_size]).get(self.TIMEOUT)
254         self.stop_proxy(self.daemon)
255         self.assertEqual([wish_size, min_size],
256                          [call[1].get('cloud_size')
257                           for call in self.node_setup.start.call_args_list])
258
259     def test_no_new_node_when_ge_min_nodes_busy(self):
260         cloud_nodes = [testutil.cloud_node_mock(n) for n in range(1, 4)]
261         arv_nodes = [testutil.arvados_node_mock(n, job_uuid=True)
262                      for n in range(1, 4)]
263         self.make_daemon(cloud_nodes, arv_nodes, [], min_nodes=2)
264         self.stop_proxy(self.daemon)
265         self.assertEqual(0, self.node_setup.start.call_count)
266
267     def test_no_new_node_when_max_nodes_busy(self):
268         self.make_daemon([testutil.cloud_node_mock(3)],
269                          [testutil.arvados_node_mock(3, job_uuid=True)],
270                          [testutil.MockSize(3)],
271                          max_nodes=1)
272         self.stop_proxy(self.daemon)
273         self.assertFalse(self.node_setup.start.called)
274
275     def start_node_boot(self, cloud_node=None, arv_node=None, id_num=1):
276         if cloud_node is None:
277             cloud_node = testutil.cloud_node_mock(id_num)
278         if arv_node is None:
279             arv_node = testutil.arvados_node_mock(id_num)
280         self.make_daemon(want_sizes=[testutil.MockSize(id_num)],
281                          avail_sizes=[(testutil.MockSize(id_num), {"cores":1})])
282         self.daemon.max_nodes.get(self.TIMEOUT)
283         self.assertEqual(1, self.node_setup.start.call_count)
284         self.last_setup.cloud_node.get.return_value = cloud_node
285         self.last_setup.arvados_node.get.return_value = arv_node
286         return self.last_setup
287
288     def test_new_node_when_booted_node_not_usable(self):
289         cloud_node = testutil.cloud_node_mock(4)
290         arv_node = testutil.arvados_node_mock(4, crunch_worker_state='down')
291         setup = self.start_node_boot(cloud_node, arv_node)
292         self.daemon.node_up(setup).get(self.TIMEOUT)
293         self.assertEqual(1, self.alive_monitor_count())
294         self.daemon.update_arvados_nodes([arv_node])
295         self.daemon.update_cloud_nodes([cloud_node])
296         self.monitor_list()[0].proxy().cloud_node_start_time = time.time()-1801
297         self.daemon.update_server_wishlist(
298             [testutil.MockSize(1)]).get(self.TIMEOUT)
299         self.stop_proxy(self.daemon)
300         self.assertEqual(2, self.node_setup.start.call_count)
301
302     def test_no_duplication_when_booting_node_listed_fast(self):
303         # Test that we don't start two ComputeNodeMonitorActors when
304         # we learn about a booting node through a listing before we
305         # get the "node up" message from CloudNodeSetupActor.
306         cloud_node = testutil.cloud_node_mock(1)
307         setup = self.start_node_boot(cloud_node)
308         self.daemon.update_cloud_nodes([cloud_node])
309         self.daemon.node_up(setup).get(self.TIMEOUT)
310         self.assertEqual(1, self.alive_monitor_count())
311
312     def test_no_duplication_when_booted_node_listed(self):
313         cloud_node = testutil.cloud_node_mock(2)
314         setup = self.start_node_boot(cloud_node, id_num=2)
315         self.daemon.node_up(setup)
316         self.daemon.update_cloud_nodes([cloud_node]).get(self.TIMEOUT)
317         self.assertEqual(1, self.alive_monitor_count())
318
319     def test_node_counted_after_boot_with_slow_listing(self):
320         # Test that, after we boot a compute node, we assume it exists
321         # even it doesn't appear in the listing (e.g., because of delays
322         # propagating tags).
323         setup = self.start_node_boot()
324         self.daemon.node_up(setup).get(self.TIMEOUT)
325         self.assertEqual(1, self.alive_monitor_count())
326         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
327         self.assertEqual(1, self.alive_monitor_count())
328
329     def test_booted_unlisted_node_counted(self):
330         setup = self.start_node_boot(id_num=1)
331         self.daemon.node_up(setup)
332         self.daemon.update_server_wishlist(
333             [testutil.MockSize(1)]).get(self.TIMEOUT)
334         self.stop_proxy(self.daemon)
335         self.assertEqual(1, self.node_setup.start.call_count)
336
337     def test_booted_node_can_shutdown(self):
338         setup = self.start_node_boot()
339         self.daemon.node_up(setup).get(self.TIMEOUT)
340         self.assertEqual(1, self.alive_monitor_count())
341         monitor = self.monitor_list()[0].proxy()
342         self.daemon.update_server_wishlist([])
343         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
344         self.stop_proxy(self.daemon)
345         self.assertTrue(self.node_shutdown.start.called,
346                         "daemon did not shut down booted node on offer")
347
348     def test_booted_node_lifecycle(self):
349         cloud_node = testutil.cloud_node_mock(6)
350         setup = self.start_node_boot(cloud_node, id_num=6)
351         self.daemon.node_up(setup).get(self.TIMEOUT)
352         self.assertEqual(1, self.alive_monitor_count())
353         monitor = self.monitor_list()[0].proxy()
354         self.daemon.update_server_wishlist([])
355         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
356         self.assertShutdownCancellable(True)
357         shutdown = self.node_shutdown.start().proxy()
358         shutdown.cloud_node.get.return_value = cloud_node
359         self.daemon.node_finished_shutdown(shutdown).get(self.TIMEOUT)
360         self.daemon.update_cloud_nodes([])
361         self.assertTrue(shutdown.stop.called,
362                         "shutdown actor not stopped after finishing")
363         self.assertTrue(monitor.actor_ref.actor_stopped.wait(self.TIMEOUT),
364                         "monitor for booted node not stopped after shutdown")
365         self.daemon.update_server_wishlist(
366             [testutil.MockSize(2)]).get(self.TIMEOUT)
367         self.stop_proxy(self.daemon)
368         self.assertTrue(self.node_setup.start.called,
369                         "second node not started after booted node stopped")
370
371     def test_booted_node_shut_down_when_never_listed(self):
372         setup = self.start_node_boot()
373         self.cloud_factory().node_start_time.return_value = time.time() - 3601
374         self.daemon.node_up(setup).get(self.TIMEOUT)
375         self.assertEqual(1, self.alive_monitor_count())
376         self.assertFalse(self.node_shutdown.start.called)
377         now = time.time()
378         self.monitor_list()[0].tell_proxy().consider_shutdown()
379         self.busywait(lambda: self.node_shutdown.start.called)
380         self.stop_proxy(self.daemon)
381         self.assertShutdownCancellable(False)
382
383     def test_booted_node_shut_down_when_never_paired(self):
384         cloud_node = testutil.cloud_node_mock(2)
385         setup = self.start_node_boot(cloud_node)
386         self.cloud_factory().node_start_time.return_value = time.time() - 3601
387         self.daemon.node_up(setup).get(self.TIMEOUT)
388         self.assertEqual(1, self.alive_monitor_count())
389         self.daemon.update_cloud_nodes([cloud_node])
390         self.monitor_list()[0].tell_proxy().consider_shutdown()
391         self.busywait(lambda: self.node_shutdown.start.called)
392         self.stop_proxy(self.daemon)
393         self.assertShutdownCancellable(False)
394
395     def test_booted_node_shut_down_when_never_working(self):
396         cloud_node = testutil.cloud_node_mock(4)
397         arv_node = testutil.arvados_node_mock(4, crunch_worker_state='down')
398         setup = self.start_node_boot(cloud_node, arv_node)
399         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
400         self.daemon.node_up(setup).get(self.TIMEOUT)
401         self.assertEqual(1, self.alive_monitor_count())
402         self.monitor_list()[0].proxy().cloud_node_start_time = time.time()-3601
403         self.daemon.update_cloud_nodes([cloud_node])
404         self.busywait(lambda: self.node_shutdown.start.called)
405         self.stop_proxy(self.daemon)
406         self.assertShutdownCancellable(False)
407
408     def test_node_that_pairs_not_considered_failed_boot(self):
409         cloud_node = testutil.cloud_node_mock(3)
410         arv_node = testutil.arvados_node_mock(3)
411         setup = self.start_node_boot(cloud_node, arv_node)
412         self.daemon.node_up(setup).get(self.TIMEOUT)
413         self.assertEqual(1, self.alive_monitor_count())
414         self.daemon.update_cloud_nodes([cloud_node])
415         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
416         self.timer.deliver()
417         self.stop_proxy(self.daemon)
418         self.assertFalse(self.node_shutdown.start.called)
419
420     def test_node_that_pairs_busy_not_considered_failed_boot(self):
421         cloud_node = testutil.cloud_node_mock(5)
422         arv_node = testutil.arvados_node_mock(5, job_uuid=True)
423         setup = self.start_node_boot(cloud_node, arv_node)
424         self.daemon.node_up(setup).get(self.TIMEOUT)
425         self.assertEqual(1, self.alive_monitor_count())
426         self.daemon.update_cloud_nodes([cloud_node])
427         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
428         self.timer.deliver()
429         self.stop_proxy(self.daemon)
430         self.assertFalse(self.node_shutdown.start.called)
431
432     def test_booting_nodes_shut_down(self):
433         self.make_daemon(want_sizes=[testutil.MockSize(1)])
434         self.daemon.update_server_wishlist([]).get(self.TIMEOUT)
435         self.stop_proxy(self.daemon)
436         self.assertTrue(self.last_setup.stop_if_no_cloud_node.called)
437
438     def test_all_booting_nodes_tried_to_shut_down(self):
439         size = testutil.MockSize(2)
440         self.make_daemon(want_sizes=[size], avail_sizes=[(size, {"cores":1})])
441         self.daemon.max_nodes.get(self.TIMEOUT)
442         setup1 = self.last_setup
443         setup1.stop_if_no_cloud_node().get.return_value = False
444         setup1.stop_if_no_cloud_node.reset_mock()
445         self.daemon.update_server_wishlist([size, size]).get(self.TIMEOUT)
446         self.daemon.max_nodes.get(self.TIMEOUT)
447         self.assertIsNot(setup1, self.last_setup)
448         self.last_setup.stop_if_no_cloud_node().get.return_value = True
449         self.last_setup.stop_if_no_cloud_node.reset_mock()
450         self.daemon.update_server_wishlist([]).get(self.TIMEOUT)
451         self.daemon.max_nodes.get(self.TIMEOUT)
452         self.stop_proxy(self.daemon)
453         self.assertEqual(1, self.last_setup.stop_if_no_cloud_node.call_count)
454         self.assertTrue(setup1.stop_if_no_cloud_node.called)
455
456     def test_shutdown_declined_at_wishlist_capacity(self):
457         cloud_node = testutil.cloud_node_mock(1)
458         arv_node = testutil.arvados_node_mock(1)
459         size = testutil.MockSize(1)
460         self.make_daemon(cloud_nodes=[cloud_node], arvados_nodes=[arv_node], want_sizes=[size])
461         self.assertEqual(1, self.alive_monitor_count())
462         monitor = self.monitor_list()[0].proxy()
463         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
464         self.stop_proxy(self.daemon)
465         self.assertFalse(self.node_shutdown.start.called)
466
467     def test_shutdown_declined_below_min_nodes(self):
468         cloud_node = testutil.cloud_node_mock(1)
469         arv_node = testutil.arvados_node_mock(1)
470         self.make_daemon(cloud_nodes=[cloud_node], arvados_nodes=[arv_node], min_nodes=1)
471         self.assertEqual(1, self.alive_monitor_count())
472         monitor = self.monitor_list()[0].proxy()
473         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
474         self.stop_proxy(self.daemon)
475         self.assertFalse(self.node_shutdown.start.called)
476
477     def test_shutdown_accepted_below_capacity(self):
478         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock()])
479         self.assertEqual(1, self.alive_monitor_count())
480         monitor = self.monitor_list()[0].proxy()
481         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
482         self.stop_proxy(self.daemon)
483         self.assertTrue(self.node_shutdown.start.called)
484
485     def test_shutdown_declined_when_idle_and_job_queued(self):
486         cloud_nodes = [testutil.cloud_node_mock(n) for n in [3, 4]]
487         arv_nodes = [testutil.arvados_node_mock(3, job_uuid=True),
488                      testutil.arvados_node_mock(4, job_uuid=None)]
489         self.make_daemon(cloud_nodes, arv_nodes, [testutil.MockSize(1)])
490         self.assertEqual(2, self.alive_monitor_count())
491         for mon_ref in self.monitor_list():
492             monitor = mon_ref.proxy()
493             if monitor.cloud_node.get(self.TIMEOUT) is cloud_nodes[-1]:
494                 break
495         else:
496             self.fail("monitor for idle node not found")
497         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
498         self.stop_proxy(self.daemon)
499         self.assertFalse(self.node_shutdown.start.called)
500
501     def test_node_shutdown_after_cancelled_shutdown(self):
502         cloud_node = testutil.cloud_node_mock(5)
503         self.make_daemon([cloud_node], [testutil.arvados_node_mock(5)])
504         self.assertEqual(1, self.alive_monitor_count())
505         monitor = self.monitor_list()[0].proxy()
506         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
507         self.last_shutdown.success.get.return_value = False
508         self.daemon.node_finished_shutdown(self.last_shutdown).get(self.TIMEOUT)
509         self.assertEqual(1, self.alive_monitor_count())
510
511         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
512         self.last_shutdown.success.get.return_value = True
513         self.last_shutdown.stop.side_effect = lambda: monitor.stop()
514         self.daemon.node_finished_shutdown(self.last_shutdown).get(self.TIMEOUT)
515         self.assertEqual(0, self.alive_monitor_count())
516
517     def test_broken_node_blackholed_after_cancelled_shutdown(self):
518         size = testutil.MockSize(8)
519         cloud_node = testutil.cloud_node_mock(8, size=size)
520         wishlist = [size]
521         self.make_daemon([cloud_node], [testutil.arvados_node_mock(8)],
522                          wishlist, avail_sizes=[(size, {"cores":1})])
523         self.assertEqual(1, self.alive_monitor_count())
524         self.assertFalse(self.node_setup.start.called)
525         monitor = self.monitor_list()[0].proxy()
526         shutdown_proxy = self.node_shutdown.start().proxy
527         shutdown_proxy().cloud_node.get.return_value = cloud_node
528         shutdown_proxy().success.get.return_value = False
529         shutdown_proxy().cancel_reason.get.return_value = self.node_shutdown.NODE_BROKEN
530         self.daemon.update_server_wishlist([]).get(self.TIMEOUT)
531         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
532         self.daemon.node_finished_shutdown(shutdown_proxy()).get(self.TIMEOUT)
533         self.daemon.update_cloud_nodes([cloud_node]).get(self.TIMEOUT)
534         self.daemon.update_server_wishlist(wishlist).get(self.TIMEOUT)
535         self.stop_proxy(self.daemon)
536         self.assertEqual(1, self.node_setup.start.call_count)
537
538     def test_nodes_shutting_down_replaced_below_max_nodes(self):
539         size = testutil.MockSize(6)
540         cloud_node = testutil.cloud_node_mock(6, size=size)
541         self.make_daemon([cloud_node], [testutil.arvados_node_mock(6, crunch_worker_state='down')],
542                          avail_sizes=[(size, {"cores":1})])
543         self.assertEqual(1, self.alive_monitor_count())
544         monitor = self.monitor_list()[0].proxy()
545         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
546         self.assertTrue(self.node_shutdown.start.called)
547         self.daemon.update_server_wishlist(
548             [testutil.MockSize(6)]).get(self.TIMEOUT)
549         self.stop_proxy(self.daemon)
550         self.assertTrue(self.node_setup.start.called)
551
552     def test_nodes_shutting_down_not_replaced_at_max_nodes(self):
553         cloud_node = testutil.cloud_node_mock(7)
554         self.make_daemon([cloud_node], [testutil.arvados_node_mock(7)],
555                          max_nodes=1)
556         self.assertEqual(1, self.alive_monitor_count())
557         monitor = self.monitor_list()[0].proxy()
558         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
559         self.assertTrue(self.node_shutdown.start.called)
560         self.daemon.update_server_wishlist(
561             [testutil.MockSize(7)]).get(self.TIMEOUT)
562         self.stop_proxy(self.daemon)
563         self.assertFalse(self.node_setup.start.called)
564
565     def test_nodes_shutting_down_count_against_excess(self):
566         size = testutil.MockSize(8)
567         cloud_nodes = [testutil.cloud_node_mock(n, size=size) for n in [8, 9]]
568         arv_nodes = [testutil.arvados_node_mock(n, size=size) for n in [8, 9]]
569         self.make_daemon(cloud_nodes, arv_nodes, [size],
570                          avail_sizes=[(size, {"cores":1})])
571         self.assertEqual(2, self.alive_monitor_count())
572         for mon_ref in self.monitor_list():
573             self.daemon.node_can_shutdown(mon_ref.proxy()).get(self.TIMEOUT)
574         self.assertEqual(1, self.node_shutdown.start.call_count)
575
576     def test_clean_shutdown_waits_for_node_setup_finish(self):
577         new_node = self.start_node_boot()
578         new_node.stop_if_no_cloud_node().get.return_value = False
579         new_node.stop_if_no_cloud_node.reset_mock()
580         self.daemon.shutdown().get(self.TIMEOUT)
581         self.assertTrue(new_node.stop_if_no_cloud_node.called)
582         self.daemon.node_up(new_node).get(self.TIMEOUT)
583         self.assertTrue(new_node.stop.called)
584         self.timer.deliver()
585         self.assertTrue(
586             self.daemon.actor_ref.actor_stopped.wait(self.TIMEOUT))
587
588     def test_wishlist_ignored_after_shutdown(self):
589         new_node = self.start_node_boot()
590         new_node.stop_if_no_cloud_node().get.return_value = False
591         new_node.stop_if_no_cloud_node.reset_mock()
592         self.daemon.shutdown().get(self.TIMEOUT)
593         size = testutil.MockSize(2)
594         self.daemon.update_server_wishlist([size] * 2).get(self.TIMEOUT)
595         self.timer.deliver()
596         self.stop_proxy(self.daemon)
597         self.assertEqual(1, self.node_setup.start.call_count)
598
599     def test_shutdown_actor_stopped_when_cloud_node_delisted(self):
600         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock()])
601         self.assertEqual(1, self.alive_monitor_count())
602         monitor = self.monitor_list()[0].proxy()
603         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
604         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
605         self.stop_proxy(self.daemon)
606         self.assertEqual(
607             1, self.last_shutdown.stop.call_count)
608
609     def test_shutdown_actor_cleanup_copes_with_dead_actors(self):
610         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock()])
611         self.assertEqual(1, self.alive_monitor_count())
612         monitor = self.monitor_list()[0].proxy()
613         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
614         # We're mainly testing that update_cloud_nodes catches and handles
615         # the ActorDeadError.
616         self.last_shutdown.stop.side_effect = pykka.ActorDeadError
617         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
618         self.stop_proxy(self.daemon)
619         self.assertEqual(1, self.last_shutdown.stop.call_count)
620
621     def test_node_create_two_sizes(self):
622         small = testutil.MockSize(1)
623         big = testutil.MockSize(2)
624         avail_sizes = [(testutil.MockSize(1), {"cores":1}),
625                         (testutil.MockSize(2), {"cores":2})]
626         self.make_daemon(want_sizes=[small, small, small, big],
627                          avail_sizes=avail_sizes, max_nodes=4)
628
629         # the daemon runs in another thread, so we need to wait and see
630         # if it does all the work we're expecting it to do before stopping it.
631         self.busywait(lambda: self.node_setup.start.call_count == 4)
632         booting = self.daemon.booting.get(self.TIMEOUT)
633         self.stop_proxy(self.daemon)
634         sizecounts = {a[0].id: 0 for a in avail_sizes}
635         for b in booting.itervalues():
636             sizecounts[b.cloud_size.get().id] += 1
637         logging.info(sizecounts)
638         self.assertEqual(3, sizecounts[small.id])
639         self.assertEqual(1, sizecounts[big.id])
640
641     def test_node_max_nodes_two_sizes(self):
642         small = testutil.MockSize(1)
643         big = testutil.MockSize(2)
644         avail_sizes = [(testutil.MockSize(1), {"cores":1}),
645                         (testutil.MockSize(2), {"cores":2})]
646         self.make_daemon(want_sizes=[small, small, small, big],
647                          avail_sizes=avail_sizes, max_nodes=3)
648
649         # the daemon runs in another thread, so we need to wait and see
650         # if it does all the work we're expecting it to do before stopping it.
651         self.busywait(lambda: self.node_setup.start.call_count == 3)
652         booting = self.daemon.booting.get(self.TIMEOUT)
653         self.stop_proxy(self.daemon)
654         sizecounts = {a[0].id: 0 for a in avail_sizes}
655         for b in booting.itervalues():
656             sizecounts[b.cloud_size.get().id] += 1
657         self.assertEqual(2, sizecounts[small.id])
658         self.assertEqual(1, sizecounts[big.id])
659
660     def test_wishlist_reconfigure(self):
661         small = testutil.MockSize(1)
662         big = testutil.MockSize(2)
663         avail_sizes = [(small, {"cores":1}), (big, {"cores":2})]
664
665         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock(1, small),
666                                       testutil.cloud_node_mock(2, small),
667                                       testutil.cloud_node_mock(3, big)],
668                          arvados_nodes=[testutil.arvados_node_mock(1),
669                                         testutil.arvados_node_mock(2),
670                                         testutil.arvados_node_mock(3)],
671                          want_sizes=[small, small, big],
672                          avail_sizes=avail_sizes)
673
674         self.daemon.update_server_wishlist([small, big, big]).get(self.TIMEOUT)
675
676         self.assertEqual(0, self.node_shutdown.start.call_count)
677
678         for c in self.daemon.cloud_nodes.get().nodes.itervalues():
679             self.daemon.node_can_shutdown(c.actor)
680
681         booting = self.daemon.booting.get()
682         cloud_nodes = self.daemon.cloud_nodes.get()
683
684         self.stop_proxy(self.daemon)
685
686         self.assertEqual(1, self.node_setup.start.call_count)
687         self.assertEqual(1, self.node_shutdown.start.call_count)
688
689         # booting a new big node
690         sizecounts = {a[0].id: 0 for a in avail_sizes}
691         for b in booting.itervalues():
692             sizecounts[b.cloud_size.get().id] += 1
693         self.assertEqual(0, sizecounts[small.id])
694         self.assertEqual(1, sizecounts[big.id])
695
696         # shutting down a small node
697         sizecounts = {a[0].id: 0 for a in avail_sizes}
698         for b in cloud_nodes.nodes.itervalues():
699             if b.shutdown_actor is not None:
700                 sizecounts[b.cloud_node.size.id] += 1
701         self.assertEqual(1, sizecounts[small.id])
702         self.assertEqual(0, sizecounts[big.id])
703
704     def test_node_max_price(self):
705         small = testutil.MockSize(1)
706         big = testutil.MockSize(2)
707         avail_sizes = [(testutil.MockSize(1), {"cores":1, "price":1}),
708                         (testutil.MockSize(2), {"cores":2, "price":2})]
709         self.make_daemon(want_sizes=[small, small, small, big],
710                          avail_sizes=avail_sizes,
711                          max_nodes=4,
712                          max_total_price=4)
713         # the daemon runs in another thread, so we need to wait and see
714         # if it does all the work we're expecting it to do before stopping it.
715         self.busywait(lambda: self.node_setup.start.call_count == 3)
716         booting = self.daemon.booting.get()
717         self.stop_proxy(self.daemon)
718
719         sizecounts = {a[0].id: 0 for a in avail_sizes}
720         for b in booting.itervalues():
721             sizecounts[b.cloud_size.get().id] += 1
722         logging.info(sizecounts)
723
724         # Booting 3 small nodes and not booting a big node would also partially
725         # satisfy the wishlist and come in under the price cap, however the way
726         # the update_server_wishlist() currently works effectively results in a
727         # round-robin creation of one node of each size in the wishlist, so
728         # test for that.
729         self.assertEqual(2, sizecounts[small.id])
730         self.assertEqual(1, sizecounts[big.id])