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