|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""A DAG to test "Jobset Suspended Healthiness" metric.""" |
| 16 | + |
| 17 | +import datetime |
| 18 | + |
| 19 | +from airflow import models |
| 20 | +from airflow.utils.trigger_rule import TriggerRule |
| 21 | +from airflow.utils.task_group import TaskGroup |
| 22 | +from airflow.models.baseoperator import chain |
| 23 | + |
| 24 | +from dags import composer_env |
| 25 | +from dags.tpu_observability.utils import jobset_util as jobset |
| 26 | +from dags.tpu_observability.utils import node_pool_util as node_pool |
| 27 | +from dags.tpu_observability.utils.jobset_util import Workload, ReplicatedJobStatus |
| 28 | +from dags.tpu_observability.configs.common import ( |
| 29 | + MachineConfigMap, |
| 30 | + GCS_CONFIG_PATH, |
| 31 | + GCS_JOBSET_CONFIG_PATH, |
| 32 | +) |
| 33 | +from dags.common.scheduling_helper.scheduling_helper import SchedulingHelper, get_dag_timeout |
| 34 | + |
| 35 | + |
| 36 | +DAG_ID = "jobset_healthiness_validation" |
| 37 | +DAGRUN_TIMEOUT = get_dag_timeout(DAG_ID) |
| 38 | +SCHEDULE = SchedulingHelper.arrange_schedule_time(DAG_ID) |
| 39 | + |
| 40 | +FAIL_WORKLOAD = "python3 -c 'import logging; import sys; logging.error(\"Simulating Failure\"); sys.exit(1)'" |
| 41 | + |
| 42 | +# Keyword arguments are generated dynamically at runtime (pylint does not |
| 43 | +# know this signature). |
| 44 | +with models.DAG( # pylint: disable=unexpected-keyword-arg |
| 45 | + dag_id=DAG_ID, |
| 46 | + start_date=datetime.datetime(2025, 8, 10), |
| 47 | + schedule=SCHEDULE if composer_env.is_prod_env() else None, |
| 48 | + dagrun_timeout=DAGRUN_TIMEOUT, |
| 49 | + catchup=False, |
| 50 | + tags=[ |
| 51 | + "cloud-ml-auto-solutions", |
| 52 | + "jobset", |
| 53 | + "healthiness", |
| 54 | + "tpu-obervability", |
| 55 | + "TPU", |
| 56 | + "v6e-16", |
| 57 | + ], |
| 58 | + description=( |
| 59 | + "This DAG tests the 'Suspended' status of jobset healthiness by " |
| 60 | + "comparing the number of 'suspended' replicas before and after " |
| 61 | + "a jobset is running." |
| 62 | + ), |
| 63 | + doc_md=""" |
| 64 | + # JobSet Healthiness Test For the "Suspended" Status |
| 65 | + ### Description |
| 66 | + This DAG automates the process of creating node-pools, ensuring the |
| 67 | + correct number of "Suspended" replicas appear, then launching a jobset on |
| 68 | + multiple replicas to ensure the correct number begin running. |
| 69 | + ### Prerequisites |
| 70 | + This test requires an existing cluster to run. |
| 71 | + ### Procedures |
| 72 | + First a node-pool is created. The validation test is then run to |
| 73 | + check if the number of "Suspended" replicas is 0. Once the jobset is |
| 74 | + running the jobs should quickly enter the "Ready" state. Then using |
| 75 | + command to suspend entire jobset. The number of found replicas is |
| 76 | + tested against the number of replicas which should be "Suspended". |
| 77 | + If they match the DAG is a success. |
| 78 | + """, |
| 79 | +) as dag: |
| 80 | + for machine in MachineConfigMap: |
| 81 | + config = machine.value |
| 82 | + |
| 83 | + # Keyword arguments are generated dynamically at runtime (pylint does not |
| 84 | + # know this signature). |
| 85 | + with TaskGroup( # pylint: disable=unexpected-keyword-arg |
| 86 | + group_id=f"v{config.tpu_version.value}" |
| 87 | + ): |
| 88 | + selector = jobset.generate_node_pool_selector( |
| 89 | + "jobset-healthiness-validation" |
| 90 | + ) |
| 91 | + |
| 92 | + jobset_config = jobset.build_jobset_from_gcs_yaml( |
| 93 | + gcs_path=GCS_JOBSET_CONFIG_PATH, |
| 94 | + dag_name=DAG_ID, |
| 95 | + node_pool_selector=selector, |
| 96 | + ) |
| 97 | + |
| 98 | + cluster_info = node_pool.build_node_pool_info_from_gcs_yaml.override( |
| 99 | + task_id="build_node_pool_info_from_gcs_yaml" |
| 100 | + )( |
| 101 | + gcs_path=GCS_CONFIG_PATH, |
| 102 | + dag_name=DAG_ID, |
| 103 | + is_prod=composer_env.is_prod_env(), |
| 104 | + machine_type=config.machine_version.value, |
| 105 | + tpu_topology=config.tpu_topology, |
| 106 | + node_pool_selector=selector, |
| 107 | + ) |
| 108 | + |
| 109 | + create_node_pool = node_pool.create.override(task_id="create_node_pool")( |
| 110 | + node_pool=cluster_info, |
| 111 | + ) |
| 112 | + |
| 113 | + startup = jobset.create_jobset_startup_group( |
| 114 | + node_pool=cluster_info, |
| 115 | + jobset_config=jobset_config, |
| 116 | + workload_type=Workload.JAX_TPU_BENCHMARK, |
| 117 | + ) |
| 118 | + |
| 119 | + with TaskGroup(group_id="validate_running_metrics") as validate_running: |
| 120 | + running_metrics = [ |
| 121 | + (ReplicatedJobStatus.SPECIFIED, "USE_CONFIG_REPLICAS"), |
| 122 | + (ReplicatedJobStatus.ACTIVE, "USE_CONFIG_REPLICAS"), |
| 123 | + (ReplicatedJobStatus.READY, "USE_CONFIG_REPLICAS"), |
| 124 | + (ReplicatedJobStatus.FAILED, 0), |
| 125 | + (ReplicatedJobStatus.SUCCEEDED, 0), |
| 126 | + (ReplicatedJobStatus.SUSPENDED, 0), |
| 127 | + ] |
| 128 | + for status, expected in running_metrics: |
| 129 | + jobset.wait_for_jobset_metrics.override( |
| 130 | + task_id=f"wait_{status.value}" |
| 131 | + )( |
| 132 | + metric_name=status, |
| 133 | + expected_value=expected, |
| 134 | + node_pool=cluster_info, |
| 135 | + jobset_config=jobset_config, |
| 136 | + ) |
| 137 | + |
| 138 | + suspend_action = jobset.suspended_jobset.override( |
| 139 | + task_id="suspend_jobset" |
| 140 | + )( |
| 141 | + node_pool=cluster_info, |
| 142 | + jobset_config=jobset_config, |
| 143 | + ) |
| 144 | + |
| 145 | + with TaskGroup( |
| 146 | + group_id="validate_suspended_metrics" |
| 147 | + ) as validate_suspended: |
| 148 | + suspended_metrics = [ |
| 149 | + (ReplicatedJobStatus.ACTIVE, 0), |
| 150 | + (ReplicatedJobStatus.SUSPENDED, "USE_CONFIG_REPLICAS"), |
| 151 | + ] |
| 152 | + for status, expected in suspended_metrics: |
| 153 | + jobset.wait_for_jobset_metrics.override( |
| 154 | + task_id=f"wait_after_suspend_{status.value}" |
| 155 | + )( |
| 156 | + metric_name=status, |
| 157 | + expected_value=expected, |
| 158 | + node_pool=cluster_info, |
| 159 | + jobset_config=jobset_config, |
| 160 | + ) |
| 161 | + |
| 162 | + resume_action = jobset.resume_jobset.override(task_id="resume_jobset")( |
| 163 | + node_pool=cluster_info, |
| 164 | + jobset_config=jobset_config, |
| 165 | + ) |
| 166 | + |
| 167 | + with TaskGroup(group_id="inject_and_validate_failure") as failure_test: |
| 168 | + cleanup_for_failure = jobset.end_workload.override( |
| 169 | + task_id="cleanup_before_failure_injection" |
| 170 | + )( |
| 171 | + node_pool=cluster_info, |
| 172 | + jobset_config=jobset_config, |
| 173 | + ) |
| 174 | + |
| 175 | + start_fail_job = jobset.run_workload.override(task_id="start_fail_job")( |
| 176 | + node_pool=cluster_info, |
| 177 | + jobset_config=jobset_config, |
| 178 | + workload_type=FAIL_WORKLOAD, |
| 179 | + ) |
| 180 | + |
| 181 | + validate_failed_metric = jobset.wait_for_jobset_metrics.override( |
| 182 | + task_id="wait_for_failed_count" |
| 183 | + )( |
| 184 | + metric_name=ReplicatedJobStatus.FAILED, |
| 185 | + expected_value="USE_CONFIG_REPLICAS", |
| 186 | + node_pool=cluster_info, |
| 187 | + jobset_config=jobset_config, |
| 188 | + ) |
| 189 | + |
| 190 | + chain(cleanup_for_failure, start_fail_job, validate_failed_metric) |
| 191 | + |
| 192 | + cleanup_workload = jobset.end_workload.override( |
| 193 | + task_id="cleanup_workload", trigger_rule=TriggerRule.ALL_DONE |
| 194 | + )( |
| 195 | + node_pool=cluster_info, |
| 196 | + jobset_config=jobset_config, |
| 197 | + ).as_teardown( |
| 198 | + setups=startup.jobset_start_time |
| 199 | + ) |
| 200 | + |
| 201 | + cleanup_node_pool = node_pool.delete.override( |
| 202 | + task_id="cleanup_node_pool", trigger_rule=TriggerRule.ALL_DONE |
| 203 | + )(node_pool=cluster_info).as_teardown( |
| 204 | + setups=create_node_pool, |
| 205 | + ) |
| 206 | + |
| 207 | + chain( |
| 208 | + selector, |
| 209 | + jobset_config, |
| 210 | + cluster_info, |
| 211 | + create_node_pool, |
| 212 | + startup.task_group, |
| 213 | + validate_running, |
| 214 | + suspend_action, |
| 215 | + validate_suspended, |
| 216 | + resume_action, |
| 217 | + failure_test, |
| 218 | + cleanup_workload, |
| 219 | + cleanup_node_pool, |
| 220 | + ) |
0 commit comments