forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_application_state.py
1013 lines (865 loc) · 39.8 KB
/
test_application_state.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pytest
import sys
from typing import Dict, List, Tuple
from unittest.mock import patch, PropertyMock, Mock
from ray.exceptions import RayTaskError
from ray.serve._private.application_state import (
ApplicationState,
ApplicationStateManager,
override_deployment_info,
)
from ray.serve._private.common import (
ApplicationStatus,
DeploymentConfig,
DeploymentStatus,
DeploymentStatusInfo,
ReplicaConfig,
DeploymentInfo,
)
from ray.serve.tests.utils import MockKVStore
from ray.serve._private.utils import get_random_letters
from ray.serve.exceptions import RayServeException
from ray.serve.schema import ServeApplicationSchema, DeploymentSchema
class MockEndpointState:
def __init__(self):
self.endpoints = dict()
def update_endpoint(self, endpoint, endpoint_info):
self.endpoints[endpoint] = endpoint_info
def delete_endpoint(self, endpoint):
if endpoint in self.endpoints:
del self.endpoints[endpoint]
class MockDeploymentStateManager:
def __init__(self, kv_store):
self.kv_store = kv_store
self.deployment_infos: Dict[str, DeploymentInfo] = dict()
self.deployment_statuses: Dict[str, DeploymentStatusInfo] = dict()
self.deleting: Dict[str, bool] = dict()
# Recover
recovered_deployments = self.kv_store.get("fake_deployment_state_checkpoint")
if recovered_deployments is not None:
for name, checkpointed_data in recovered_deployments.items():
(info, deleting) = checkpointed_data
self.deployment_infos[name] = info
self.deployment_statuses[name] = DeploymentStatus.UPDATING
self.deleting[name] = deleting
def deploy(self, deployment_name: str, deployment_info: DeploymentInfo):
existing_info = self.deployment_infos.get(deployment_name)
self.deleting[deployment_name] = False
self.deployment_infos[deployment_name] = deployment_info
if not existing_info or existing_info.version != deployment_info.version:
self.deployment_statuses[deployment_name] = DeploymentStatusInfo(
name=deployment_name,
status=DeploymentStatus.UPDATING,
message="",
)
self.kv_store.put(
"fake_deployment_state_checkpoint",
dict(
zip(
self.deployment_infos.keys(),
zip(self.deployment_infos.values(), self.deleting.values()),
)
),
)
@property
def deployments(self) -> List[str]:
return list(self.deployment_infos.keys())
def get_deployment_statuses(self, deployment_names: List[str]):
return [self.deployment_statuses[name] for name in deployment_names]
def get_deployment(self, deployment_name: str) -> DeploymentInfo:
if deployment_name in self.deployment_statuses:
# Return dummy deployment info object
return DeploymentInfo(
deployment_config=DeploymentConfig(num_replicas=1, user_config={}),
replica_config=ReplicaConfig.create(lambda x: x),
start_time_ms=0,
deployer_job_id="",
)
def get_deployments_in_application(self, app_name: str):
return [
name
for name, info in self.deployment_infos.items()
if info.app_name == app_name
]
def set_deployment_unhealthy(self, name: str):
self.deployment_statuses[name].status = DeploymentStatus.UNHEALTHY
def set_deployment_healthy(self, name: str):
self.deployment_statuses[name].status = DeploymentStatus.HEALTHY
def set_deployment_updating(self, name: str):
self.deployment_statuses[name].status = DeploymentStatus.UPDATING
def set_deployment_deleted(self, name: str):
if not self.deployment_infos[name]:
raise ValueError(
f"Tried to mark deployment {name} as deleted, but {name} not found"
)
if not self.deleting[name]:
raise ValueError(
f"Tried to mark deployment {name} as deleted, but delete_deployment()"
f"hasn't been called for {name} yet"
)
del self.deployment_infos[name]
del self.deployment_statuses[name]
del self.deleting[name]
def set_deployment(
self,
deployment_name: str,
deployment_info: DeploymentInfo,
status: DeploymentStatus,
):
self.deleting[deployment_name] = False
self.deployment_infos[deployment_name] = deployment_info
self.deployment_statuses[deployment_name] = DeploymentStatusInfo(
name=deployment_name,
status=status,
message="",
)
def delete_deployment(self, deployment_name: str):
self.deleting[deployment_name] = True
@pytest.fixture
def mocked_application_state_manager() -> Tuple[
ApplicationStateManager, MockDeploymentStateManager
]:
kv_store = MockKVStore()
deployment_state_manager = MockDeploymentStateManager(kv_store)
application_state_manager = ApplicationStateManager(
deployment_state_manager, MockEndpointState(), kv_store
)
yield application_state_manager, deployment_state_manager, kv_store
def deployment_params(name: str, route_prefix: str = None, docs_path: str = None):
return {
"name": name,
"deployment_config_proto_bytes": DeploymentConfig(
num_replicas=1, user_config={}, version=get_random_letters()
).to_proto_bytes(),
"replica_config_proto_bytes": ReplicaConfig.create(
lambda x: x
).to_proto_bytes(),
"deployer_job_id": "random",
"route_prefix": route_prefix,
"docs_path": docs_path,
"ingress": False,
"is_driver_deployment": False,
}
@pytest.fixture
def mocked_application_state() -> Tuple[ApplicationState, MockDeploymentStateManager]:
kv_store = MockKVStore()
deployment_state_manager = MockDeploymentStateManager(kv_store)
application_state = ApplicationState(
"test_app",
deployment_state_manager,
MockEndpointState(),
lambda *args, **kwargs: None,
)
yield application_state, deployment_state_manager
@patch.object(
ApplicationState, "target_deployments", PropertyMock(return_value=["a", "b", "c"])
)
class TestDetermineAppStatus:
@patch.object(ApplicationState, "get_deployments_statuses")
def test_running(self, get_deployments_statuses, mocked_application_state):
app_state, _ = mocked_application_state
get_deployments_statuses.return_value = [
DeploymentStatusInfo("a", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("b", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("c", DeploymentStatus.HEALTHY),
]
assert app_state._determine_app_status() == (ApplicationStatus.RUNNING, "")
@patch.object(ApplicationState, "get_deployments_statuses")
def test_stay_running(self, get_deployments_statuses, mocked_application_state):
app_state, _ = mocked_application_state
app_state._status = ApplicationStatus.RUNNING
get_deployments_statuses.return_value = [
DeploymentStatusInfo("a", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("b", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("c", DeploymentStatus.HEALTHY),
]
assert app_state._determine_app_status() == (ApplicationStatus.RUNNING, "")
@patch.object(ApplicationState, "get_deployments_statuses")
def test_deploying(self, get_deployments_statuses, mocked_application_state):
app_state, _ = mocked_application_state
get_deployments_statuses.return_value = [
DeploymentStatusInfo("a", DeploymentStatus.UPDATING),
DeploymentStatusInfo("b", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("c", DeploymentStatus.HEALTHY),
]
assert app_state._determine_app_status() == (ApplicationStatus.DEPLOYING, "")
@patch.object(ApplicationState, "get_deployments_statuses")
def test_deploy_failed(self, get_deployments_statuses, mocked_application_state):
app_state, _ = mocked_application_state
get_deployments_statuses.return_value = [
DeploymentStatusInfo("a", DeploymentStatus.UPDATING),
DeploymentStatusInfo("b", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("c", DeploymentStatus.UNHEALTHY),
]
status, error_msg = app_state._determine_app_status()
assert status == ApplicationStatus.DEPLOY_FAILED
assert error_msg
@patch.object(ApplicationState, "get_deployments_statuses")
def test_unhealthy(self, get_deployments_statuses, mocked_application_state):
app_state, _ = mocked_application_state
app_state._status = ApplicationStatus.RUNNING
get_deployments_statuses.return_value = [
DeploymentStatusInfo("a", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("b", DeploymentStatus.HEALTHY),
DeploymentStatusInfo("c", DeploymentStatus.UNHEALTHY),
]
status, error_msg = app_state._determine_app_status()
assert status == ApplicationStatus.UNHEALTHY
assert error_msg
def test_deploy_and_delete_app(mocked_application_state):
"""Deploy app with 2 deployments, transition DEPLOYING -> RUNNING -> DELETING.
This tests the basic typical workflow.
"""
app_state, deployment_state_manager = mocked_application_state
# DEPLOY application with deployments {d1, d2}
app_state.apply_deployment_args(
[deployment_params("d1", "/hi", "/documentation"), deployment_params("d2")]
)
assert app_state.route_prefix == "/hi"
assert app_state.docs_path == "/documentation"
app_status = app_state.get_application_status_info()
assert app_status.status == ApplicationStatus.DEPLOYING
assert app_status.deployment_timestamp > 0
app_state.update()
# After one update, deployments {d1, d2} should be created
assert deployment_state_manager.get_deployment("d1")
assert deployment_state_manager.get_deployment("d2")
assert app_state.status == ApplicationStatus.DEPLOYING
# Until both deployments are healthy, app should be deploying
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Mark deployment d1 healthy (app should be deploying)
deployment_state_manager.set_deployment_healthy("d1")
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Mark deployment d2 healthy (app should be running)
deployment_state_manager.set_deployment_healthy("d2")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
# Rerun update, status shouldn't change (still running)
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
# Delete application (app should be deleting)
app_state.delete()
assert app_state.status == ApplicationStatus.DELETING
app_state.update()
deployment_state_manager.set_deployment_deleted("d1")
ready_to_be_deleted = app_state.update()
assert not ready_to_be_deleted
assert app_state.status == ApplicationStatus.DELETING
# Once both deployments are deleted, the app should be ready to delete
deployment_state_manager.set_deployment_deleted("d2")
ready_to_be_deleted = app_state.update()
assert ready_to_be_deleted
def test_app_deploy_failed_and_redeploy(mocked_application_state):
"""Test DEPLOYING -> DEPLOY_FAILED -> (redeploy) -> DEPLOYING -> RUNNING"""
app_state, deployment_state_manager = mocked_application_state
app_state.apply_deployment_args([deployment_params("d1")])
assert app_state.status == ApplicationStatus.DEPLOYING
# Before status of deployment changes, app should still be DEPLOYING
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Mark deployment unhealthy -> app should be DEPLOY_FAILED
deployment_state_manager.set_deployment_unhealthy("d1")
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOY_FAILED
# Message and status should not change
deploy_failed_msg = app_state._status_msg
assert len(deploy_failed_msg) != 0
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOY_FAILED
assert app_state._status_msg == deploy_failed_msg
app_state.apply_deployment_args([deployment_params("d1"), deployment_params("d2")])
assert app_state.status == ApplicationStatus.DEPLOYING
assert app_state._status_msg != deploy_failed_msg
# After one update, deployments {d1, d2} should be created
app_state.update()
assert deployment_state_manager.get_deployment("d1")
assert deployment_state_manager.get_deployment("d2")
assert app_state.status == ApplicationStatus.DEPLOYING
deployment_state_manager.set_deployment_healthy("d1")
deployment_state_manager.set_deployment_healthy("d2")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
# Message and status should not change
running_msg = app_state._status_msg
assert running_msg != deploy_failed_msg
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
assert app_state._status_msg == running_msg
def test_app_deploy_failed_and_recover(mocked_application_state):
"""Test DEPLOYING -> DEPLOY_FAILED -> (self recovered) -> RUNNING
If while the application is deploying a deployment becomes unhealthy,
the app is marked as deploy failed. But if the deployment recovers,
the application status should update to running.
"""
app_state, deployment_state_manager = mocked_application_state
app_state.apply_deployment_args([deployment_params("d1")])
assert app_state.status == ApplicationStatus.DEPLOYING
# Before status of deployment changes, app should still be DEPLOYING
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Mark deployment unhealthy -> app should be DEPLOY_FAILED
deployment_state_manager.set_deployment_unhealthy("d1")
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOY_FAILED
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOY_FAILED
# Deployment recovers to healthy -> app should be RUNNING
deployment_state_manager.set_deployment_healthy("d1")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
def test_app_unhealthy(mocked_application_state):
"""Test DEPLOYING -> RUNNING -> UNHEALTHY -> RUNNING.
Even after an application becomes running, if a deployment becomes
unhealthy at some point, the application status should also be
updated to unhealthy.
"""
app_state, deployment_state_manager = mocked_application_state
app_state.apply_deployment_args([deployment_params("a"), deployment_params("b")])
assert app_state.status == ApplicationStatus.DEPLOYING
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Once both deployments become healthy, app should be running
deployment_state_manager.set_deployment_healthy("a")
deployment_state_manager.set_deployment_healthy("b")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
# If a deployment becomes unhealthy, application should become unhealthy
deployment_state_manager.set_deployment_unhealthy("a")
app_state.update()
assert app_state.status == ApplicationStatus.UNHEALTHY
# Rerunning update shouldn't make a difference
app_state.update()
assert app_state.status == ApplicationStatus.UNHEALTHY
# If the deployment recovers, the application should also recover
deployment_state_manager.set_deployment_healthy("a")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
@patch("ray.serve._private.application_state.build_serve_application", Mock())
@patch("ray.get", Mock(return_value=([deployment_params("a", "/old", "/docs")], None)))
@patch("ray.serve._private.application_state.check_obj_ref_ready_nowait")
def test_deploy_through_config_succeed(check_obj_ref_ready_nowait):
"""Test deploying through config successfully.
Deploy obj ref finishes successfully, so status should transition to running.
"""
kv_store = MockKVStore()
deployment_state_manager = MockDeploymentStateManager(kv_store)
app_state_manager = ApplicationStateManager(
deployment_state_manager, MockEndpointState(), kv_store
)
# Deploy config
app_config = ServeApplicationSchema(import_path="fa.ke", route_prefix="/new")
app_state_manager.deploy_config(name="test_app", app_config=app_config)
app_state = app_state_manager._application_states["test_app"]
assert app_state.status == ApplicationStatus.DEPLOYING
# Before object ref is ready
check_obj_ref_ready_nowait.return_value = False
app_state.update()
assert app_state._build_app_task_info
assert app_state.status == ApplicationStatus.DEPLOYING
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Object ref is ready
check_obj_ref_ready_nowait.return_value = True
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
assert app_state.target_deployments == ["a"]
assert app_state.route_prefix == "/new"
assert app_state.docs_path == "/docs"
# Set healthy
deployment_state_manager.set_deployment_healthy("a")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
@patch(
"ray.serve._private.application_state.get_app_code_version",
Mock(return_value="123"),
)
@patch("ray.serve._private.application_state.build_serve_application", Mock())
@patch("ray.get", Mock(side_effect=RayTaskError(None, "intentionally failed", None)))
@patch("ray.serve._private.application_state.check_obj_ref_ready_nowait")
def test_deploy_through_config_fail(check_obj_ref_ready_nowait):
"""Test fail to deploy through config.
Deploy obj ref errors out, so status should transition to deploy failed.
"""
kv_store = MockKVStore()
deployment_state_manager = MockDeploymentStateManager(kv_store)
app_state_manager = ApplicationStateManager(
deployment_state_manager, MockEndpointState(), kv_store
)
# Deploy config
app_state_manager.deploy_config(name="test_app", app_config=Mock())
app_state = app_state_manager._application_states["test_app"]
assert app_state.status == ApplicationStatus.DEPLOYING
# Before object ref is ready
check_obj_ref_ready_nowait.return_value = False
app_state.update()
assert app_state._build_app_task_info
assert app_state.status == ApplicationStatus.DEPLOYING
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Object ref is ready, and the task has called apply_deployment_args
check_obj_ref_ready_nowait.return_value = True
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOY_FAILED
assert "failed" in app_state._status_msg or "error" in app_state._status_msg
def test_redeploy_same_app(mocked_application_state):
"""Test redeploying same application with updated deployments."""
app_state, deployment_state_manager = mocked_application_state
app_state.apply_deployment_args([deployment_params("a"), deployment_params("b")])
assert app_state.status == ApplicationStatus.DEPLOYING
# Update
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
assert set(app_state.target_deployments) == {"a", "b"}
# Transition to running
deployment_state_manager.set_deployment_healthy("a")
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
deployment_state_manager.set_deployment_healthy("b")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
# Deploy the same app with different deployments
app_state.apply_deployment_args([deployment_params("b"), deployment_params("c")])
assert app_state.status == ApplicationStatus.DEPLOYING
# Target state should be updated immediately
assert "a" not in app_state.target_deployments
# Remove deployment `a`
app_state.update()
deployment_state_manager.set_deployment_deleted("a")
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
# Move to running
deployment_state_manager.set_deployment_healthy("c")
app_state.update()
assert app_state.status == ApplicationStatus.DEPLOYING
deployment_state_manager.set_deployment_healthy("b")
app_state.update()
assert app_state.status == ApplicationStatus.RUNNING
def test_deploy_with_route_prefix_conflict(mocked_application_state_manager):
"""Test that an application with a route prefix conflict fails to deploy"""
app_state_manager, _, _ = mocked_application_state_manager
app_state_manager.apply_deployment_args("app1", [deployment_params("a", "/hi")])
with pytest.raises(RayServeException):
app_state_manager.apply_deployment_args("app2", [deployment_params("b", "/hi")])
def test_deploy_with_renamed_app(mocked_application_state_manager):
"""
Test that an application deploys successfully when there is a route prefix
conflict with an old app running on the cluster.
"""
app_state_manager, deployment_state_manager, _ = mocked_application_state_manager
# deploy app1
app_state_manager.apply_deployment_args("app1", [deployment_params("a", "/url1")])
app_state = app_state_manager._application_states["app1"]
assert app_state_manager.get_app_status("app1") == ApplicationStatus.DEPLOYING
# Update
app_state_manager.update()
assert app_state_manager.get_app_status("app1") == ApplicationStatus.DEPLOYING
assert set(app_state.target_deployments) == {"a"}
# Once its single deployment is healthy, app1 should be running
deployment_state_manager.set_deployment_healthy("a")
app_state_manager.update()
assert app_state_manager.get_app_status("app1") == ApplicationStatus.RUNNING
# delete app1
app_state_manager.delete_application("app1")
assert app_state_manager.get_app_status("app1") == ApplicationStatus.DELETING
app_state_manager.update()
# deploy app2
app_state_manager.apply_deployment_args("app2", [deployment_params("b", "/url1")])
assert app_state_manager.get_app_status("app2") == ApplicationStatus.DEPLOYING
app_state_manager.update()
# app2 deploys before app1 finishes deleting
deployment_state_manager.set_deployment_healthy("b")
app_state_manager.update()
assert app_state_manager.get_app_status("app2") == ApplicationStatus.RUNNING
assert app_state_manager.get_app_status("app1") == ApplicationStatus.DELETING
# app1 finally finishes deleting
deployment_state_manager.set_deployment_deleted("a")
app_state_manager.update()
assert app_state_manager.get_app_status("app1") == ApplicationStatus.NOT_STARTED
assert app_state_manager.get_app_status("app2") == ApplicationStatus.RUNNING
def test_application_state_recovery(mocked_application_state_manager):
"""Test DEPLOYING -> RUNNING -> (controller crash) -> DEPLOYING -> RUNNING"""
(
app_state_manager,
deployment_state_manager,
kv_store,
) = mocked_application_state_manager
app_name = "test_app"
# DEPLOY application with deployments {d1, d2}
params = deployment_params("d1")
app_state_manager.apply_deployment_args(app_name, [params])
app_state = app_state_manager._application_states[app_name]
assert app_state.status == ApplicationStatus.DEPLOYING
# Once deployment is healthy, app should be running
app_state_manager.update()
assert deployment_state_manager.get_deployment("d1")
deployment_state_manager.set_deployment_healthy("d1")
app_state_manager.update()
assert app_state.status == ApplicationStatus.RUNNING
# Simulate controller crashed!! Create new deployment state manager,
# which should recover target state for deployment "d1" from kv store
new_deployment_state_manager = MockDeploymentStateManager(kv_store)
version1 = new_deployment_state_manager.deployment_infos["d1"].version
# Create new application state manager, and it should call _recover_from_checkpoint
new_app_state_manager = ApplicationStateManager(
new_deployment_state_manager, MockEndpointState(), kv_store
)
app_state = new_app_state_manager._application_states[app_name]
assert app_state.status == ApplicationStatus.DEPLOYING
assert app_state._target_state.deployment_infos["d1"].version == version1
new_deployment_state_manager.set_deployment_healthy("d1")
new_app_state_manager.update()
assert app_state.status == ApplicationStatus.RUNNING
def test_recover_during_update(mocked_application_state_manager):
"""Test that application and deployment states are recovered if
controller crashed in the middle of a redeploy.
Target state is checkpointed in the application state manager,
but not yet the deployment state manager when the controller crashes
Then the deployment state manager should recover an old version of
the deployment during initial recovery, but the application state
manager should eventually reconcile this.
"""
(
app_state_manager,
deployment_state_manager,
kv_store,
) = mocked_application_state_manager
app_name = "test_app"
# DEPLOY application with deployment "d1"
params = deployment_params("d1")
app_state_manager.apply_deployment_args(app_name, [params])
app_state = app_state_manager._application_states[app_name]
assert app_state.status == ApplicationStatus.DEPLOYING
# Once deployment is healthy, app should be running
app_state_manager.update()
assert deployment_state_manager.get_deployment("d1")
deployment_state_manager.set_deployment_healthy("d1")
app_state_manager.update()
assert app_state.status == ApplicationStatus.RUNNING
# Deploy new version of "d1" (this auto generates new random version)
params2 = deployment_params("d1")
app_state_manager.apply_deployment_args(app_name, [params2])
assert app_state.status == ApplicationStatus.DEPLOYING
# Before application state manager could propagate new version to
# deployment state manager, controller crashes.
# Create new deployment state manager. It should recover the old
# version of the deployment from the kv store
new_deployment_state_manager = MockDeploymentStateManager(kv_store)
dr_version = new_deployment_state_manager.deployment_infos["d1"].version
# Create new application state manager, and it should call _recover_from_checkpoint
new_app_state_manager = ApplicationStateManager(
new_deployment_state_manager, MockEndpointState(), kv_store
)
app_state = new_app_state_manager._application_states[app_name]
ar_version = app_state._target_state.deployment_infos["d1"].version
assert app_state.status == ApplicationStatus.DEPLOYING
assert ar_version != dr_version
new_app_state_manager.update()
assert new_deployment_state_manager.deployment_infos["d1"].version == ar_version
assert app_state.status == ApplicationStatus.DEPLOYING
new_deployment_state_manager.set_deployment_healthy("d1")
new_app_state_manager.update()
assert app_state.status == ApplicationStatus.RUNNING
def test_is_ready_for_shutdown(mocked_application_state_manager):
"""Test `is_ready_for_shutdown()` returns the correct state.
When shutting down applications before deployments are deleted, application state
`is_deleted()` should return False and `is_ready_for_shutdown()` should return
False. When shutting down applications after deployments are deleted, application
state `is_deleted()` should return True and `is_ready_for_shutdown()` should return
True.
"""
(
app_state_manager,
deployment_state_manager,
kv_store,
) = mocked_application_state_manager
app_name = "test_app"
deployment_name = "d1"
# DEPLOY application with deployment "d1"
params = deployment_params(deployment_name)
app_state_manager.apply_deployment_args(app_name, [params])
app_state = app_state_manager._application_states[app_name]
assert app_state.status == ApplicationStatus.DEPLOYING
# Once deployment is healthy, app should be running
app_state_manager.update()
assert deployment_state_manager.get_deployment(deployment_name)
deployment_state_manager.set_deployment_healthy(deployment_name)
app_state_manager.update()
assert app_state.status == ApplicationStatus.RUNNING
# When shutting down applications before deployments are deleted, application state
# `is_deleted()` should return False and `is_ready_for_shutdown()` should return
# False
app_state_manager.shutdown()
assert not app_state.is_deleted()
assert not app_state_manager.is_ready_for_shutdown()
# When shutting down applications after deployments are deleted, application state
# `is_deleted()` should return True and `is_ready_for_shutdown()` should return True
deployment_state_manager.delete_deployment(deployment_name)
deployment_state_manager.set_deployment_deleted(deployment_name)
app_state_manager.update()
assert app_state.is_deleted()
assert app_state_manager.is_ready_for_shutdown()
class TestOverrideDeploymentInfo:
@pytest.fixture
def info(self):
return DeploymentInfo(
app_name="default",
route_prefix="/",
version="123",
deployment_config=DeploymentConfig(num_replicas=1),
replica_config=ReplicaConfig.create(lambda x: x),
start_time_ms=0,
deployer_job_id="",
)
def test_override_deployment_config(self, info):
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
deployments=[
DeploymentSchema(
name="A",
num_replicas=3,
max_concurrent_queries=200,
user_config={"price": "4"},
graceful_shutdown_wait_loop_s=4,
graceful_shutdown_timeout_s=40,
health_check_period_s=20,
health_check_timeout_s=60,
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/"
assert updated_info.version == "123"
assert updated_info.deployment_config.max_concurrent_queries == 200
assert updated_info.deployment_config.user_config == {"price": "4"}
assert updated_info.deployment_config.graceful_shutdown_wait_loop_s == 4
assert updated_info.deployment_config.graceful_shutdown_timeout_s == 40
assert updated_info.deployment_config.health_check_period_s == 20
assert updated_info.deployment_config.health_check_timeout_s == 60
def test_override_autoscaling_config(self, info):
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
deployments=[
DeploymentSchema(
name="A",
autoscaling_config={
"min_replicas": 1,
"initial_replicas": 12,
"max_replicas": 79,
},
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/"
assert updated_info.version == "123"
assert updated_info.autoscaling_policy.config.min_replicas == 1
assert updated_info.autoscaling_policy.config.initial_replicas == 12
assert updated_info.autoscaling_policy.config.max_replicas == 79
def test_override_route_prefix_1(self, info):
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
deployments=[DeploymentSchema(name="A", route_prefix="/alice")],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/alice"
assert updated_info.version == "123"
def test_override_route_prefix_2(self, info):
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
route_prefix="/bob",
deployments=[
DeploymentSchema(
name="A",
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/bob"
assert updated_info.version == "123"
def test_override_route_prefix_3(self, info):
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
route_prefix="/bob",
deployments=[DeploymentSchema(name="A", route_prefix="/alice")],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/bob"
assert updated_info.version == "123"
def test_override_is_driver_deployment(self, info):
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
deployments=[
DeploymentSchema(
name="A",
is_driver_deployment=True,
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/"
assert updated_info.version == "123"
assert updated_info.is_driver_deployment
def test_override_ray_actor_options_1(self, info):
"""Test runtime env specified in config at deployment level."""
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
deployments=[
DeploymentSchema(
name="A",
ray_actor_options={"runtime_env": {"working_dir": "s3://B"}},
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/"
assert updated_info.version == "123"
assert (
updated_info.replica_config.ray_actor_options["runtime_env"]["working_dir"]
== "s3://B"
)
def test_override_ray_actor_options_2(self, info):
"""Test application runtime env is propagated to deployments."""
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
runtime_env={"working_dir": "s3://C"},
deployments=[
DeploymentSchema(
name="A",
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/"
assert updated_info.version == "123"
assert (
updated_info.replica_config.ray_actor_options["runtime_env"]["working_dir"]
== "s3://C"
)
def test_override_ray_actor_options_3(self, info):
"""If runtime env is specified in the config at the deployment level, it should
override the application-level runtime env.
"""
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
runtime_env={"working_dir": "s3://C"},
deployments=[
DeploymentSchema(
name="A",
ray_actor_options={"runtime_env": {"working_dir": "s3://B"}},
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/"
assert updated_info.version == "123"
assert (
updated_info.replica_config.ray_actor_options["runtime_env"]["working_dir"]
== "s3://B"
)
def test_override_ray_actor_options_4(self):
"""If runtime env is specified for the deployment in code, it should override
the application-level runtime env.
"""
info = DeploymentInfo(
app_name="default",
route_prefix="/",
version="123",
deployment_config=DeploymentConfig(num_replicas=1),
replica_config=ReplicaConfig.create(
lambda x: x,
ray_actor_options={"runtime_env": {"working_dir": "s3://A"}},
),
start_time_ms=0,
deployer_job_id="",
)
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
runtime_env={"working_dir": "s3://C"},
deployments=[
DeploymentSchema(
name="A",
)
],
)
updated_infos = override_deployment_info("default", {"default_A": info}, config)
updated_info = updated_infos["default_A"]
assert updated_info.app_name == "default"
assert updated_info.route_prefix == "/"
assert updated_info.version == "123"
assert (
updated_info.replica_config.ray_actor_options["runtime_env"]["working_dir"]
== "s3://A"
)
def test_override_ray_actor_options_5(self):
"""If runtime env is specified in all three places:
- In code
- In the config at the deployment level
- In the config at the application level
The one specified in the config at the deployment level should take precedence.
"""
info = DeploymentInfo(
app_name="default",
route_prefix="/",
version="123",
deployment_config=DeploymentConfig(num_replicas=1),
replica_config=ReplicaConfig.create(
lambda x: x,
ray_actor_options={"runtime_env": {"working_dir": "s3://A"}},
),
start_time_ms=0,
deployer_job_id="",
)
config = ServeApplicationSchema(
name="default",
import_path="test.import.path",
runtime_env={"working_dir": "s3://C"},
deployments=[
DeploymentSchema(
name="A",
ray_actor_options={"runtime_env": {"working_dir": "s3://B"}},
)
],
)