|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from ray import workflow |
| 4 | +from ray.tests.conftest import * # noqa |
| 5 | +from ray.workflow import workflow_storage |
| 6 | +from ray.workflow.storage import get_global_storage |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +def get_metadata(paths, is_json=True): |
| 12 | + store = get_global_storage() |
| 13 | + key = store.make_key(*paths) |
| 14 | + return asyncio.get_event_loop().run_until_complete(store.get(key, is_json)) |
| 15 | + |
| 16 | + |
| 17 | +def test_step_user_metadata(workflow_start_regular): |
| 18 | + |
| 19 | + metadata = {"k1": "v1"} |
| 20 | + step_name = "simple_step" |
| 21 | + workflow_id = "simple" |
| 22 | + |
| 23 | + @workflow.step(name=step_name, metadata=metadata) |
| 24 | + def simple(): |
| 25 | + return 0 |
| 26 | + |
| 27 | + simple.step().run(workflow_id) |
| 28 | + |
| 29 | + checkpointed_metadata = get_metadata( |
| 30 | + [workflow_id, "steps", step_name, workflow_storage.STEP_USER_METADATA]) |
| 31 | + assert metadata == checkpointed_metadata |
| 32 | + |
| 33 | + |
| 34 | +def test_step_runtime_metadata(workflow_start_regular): |
| 35 | + |
| 36 | + step_name = "simple_step" |
| 37 | + workflow_id = "simple" |
| 38 | + |
| 39 | + @workflow.step(name=step_name) |
| 40 | + def simple(): |
| 41 | + return 0 |
| 42 | + |
| 43 | + simple.step().run(workflow_id) |
| 44 | + |
| 45 | + prerun_meta = get_metadata([ |
| 46 | + workflow_id, "steps", step_name, workflow_storage.STEP_PRERUN_METADATA |
| 47 | + ]) |
| 48 | + postrun_meta = get_metadata([ |
| 49 | + workflow_id, "steps", step_name, workflow_storage.STEP_POSTRUN_METADATA |
| 50 | + ]) |
| 51 | + assert "start_time" in prerun_meta |
| 52 | + assert "end_time" in postrun_meta |
| 53 | + |
| 54 | + |
| 55 | +def test_workflow_user_metadata(workflow_start_regular): |
| 56 | + |
| 57 | + metadata = {"k1": "v1"} |
| 58 | + workflow_id = "simple" |
| 59 | + |
| 60 | + @workflow.step |
| 61 | + def simple(): |
| 62 | + return 0 |
| 63 | + |
| 64 | + simple.step().run(workflow_id, metadata=metadata) |
| 65 | + |
| 66 | + checkpointed_metadata = get_metadata( |
| 67 | + [workflow_id, workflow_storage.WORKFLOW_USER_METADATA]) |
| 68 | + assert metadata == checkpointed_metadata |
| 69 | + |
| 70 | + |
| 71 | +def test_workflow_runtime_metadata(workflow_start_regular): |
| 72 | + |
| 73 | + workflow_id = "simple" |
| 74 | + |
| 75 | + @workflow.step |
| 76 | + def simple(): |
| 77 | + return 0 |
| 78 | + |
| 79 | + simple.step().run(workflow_id) |
| 80 | + |
| 81 | + prerun_meta = get_metadata( |
| 82 | + [workflow_id, workflow_storage.WORKFLOW_PRERUN_METADATA]) |
| 83 | + postrun_meta = get_metadata( |
| 84 | + [workflow_id, workflow_storage.WORKFLOW_POSTRUN_METADATA]) |
| 85 | + assert "start_time" in prerun_meta |
| 86 | + assert "end_time" in postrun_meta |
| 87 | + |
| 88 | + |
| 89 | +def test_all_metadata(workflow_start_regular): |
| 90 | + |
| 91 | + user_step_metadata = {"k1": "v1"} |
| 92 | + user_run_metadata = {"k2": "v2"} |
| 93 | + step_name = "simple_step" |
| 94 | + workflow_id = "simple" |
| 95 | + |
| 96 | + @workflow.step |
| 97 | + def simple(): |
| 98 | + return 0 |
| 99 | + |
| 100 | + simple.options( |
| 101 | + name=step_name, metadata=user_step_metadata).step().run( |
| 102 | + workflow_id, metadata=user_run_metadata) |
| 103 | + |
| 104 | + checkpointed_user_step_metadata = get_metadata( |
| 105 | + [workflow_id, "steps", step_name, workflow_storage.STEP_USER_METADATA]) |
| 106 | + checkpointed_user_run_metadata = get_metadata( |
| 107 | + [workflow_id, workflow_storage.WORKFLOW_USER_METADATA]) |
| 108 | + checkpointed_pre_step_meta = get_metadata([ |
| 109 | + workflow_id, "steps", step_name, workflow_storage.STEP_PRERUN_METADATA |
| 110 | + ]) |
| 111 | + checkpointed_post_step_meta = get_metadata([ |
| 112 | + workflow_id, "steps", step_name, workflow_storage.STEP_POSTRUN_METADATA |
| 113 | + ]) |
| 114 | + checkpointed_pre_run_meta = get_metadata( |
| 115 | + [workflow_id, workflow_storage.WORKFLOW_PRERUN_METADATA]) |
| 116 | + checkpointed_post_run_meta = get_metadata( |
| 117 | + [workflow_id, workflow_storage.WORKFLOW_POSTRUN_METADATA]) |
| 118 | + assert user_step_metadata == checkpointed_user_step_metadata |
| 119 | + assert user_run_metadata == checkpointed_user_run_metadata |
| 120 | + assert "start_time" in checkpointed_pre_step_meta |
| 121 | + assert "start_time" in checkpointed_pre_run_meta |
| 122 | + assert "end_time" in checkpointed_post_step_meta |
| 123 | + assert "end_time" in checkpointed_post_run_meta |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + import sys |
| 128 | + sys.exit(pytest.main(["-v", __file__])) |
0 commit comments