|
| 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 uptime metric.""" |
| 16 | + |
| 17 | +import datetime |
| 18 | + |
| 19 | +from airflow import models |
| 20 | +from airflow.decorators import task |
| 21 | +from airflow.models.baseoperator import chain |
| 22 | +from airflow.utils.task_group import TaskGroup |
| 23 | +from airflow.utils.trigger_rule import TriggerRule |
| 24 | + |
| 25 | +from dags import composer_env |
| 26 | +from dags.tpu_observability.configs.common import ( |
| 27 | + MachineConfigMap, |
| 28 | + GCS_CONFIG_PATH, |
| 29 | + GCS_JOBSET_CONFIG_PATH, |
| 30 | +) |
| 31 | +from dags.tpu_observability.utils import jobset_util as jobset |
| 32 | +from dags.tpu_observability.utils import node_pool_util as node_pool |
| 33 | +from dags.tpu_observability.utils.jobset_util import Workload |
| 34 | +from dags.tpu_observability.utils.time_util import TimeUtil |
| 35 | + |
| 36 | + |
| 37 | +@task |
| 38 | +def get_current_time() -> TimeUtil: |
| 39 | + """Get the current time in UTC.""" |
| 40 | + current_time_utc = datetime.datetime.now(datetime.timezone.utc) |
| 41 | + return TimeUtil.from_datetime(current_time_utc) |
| 42 | + |
| 43 | + |
| 44 | +# Keyword arguments are generated dynamically at runtime (pylint does not |
| 45 | +# know this signature). |
| 46 | +with models.DAG( # pylint: disable=unexpected-keyword-arg |
| 47 | + dag_id="jobset_uptime_validation", |
| 48 | + start_date=datetime.datetime(2025, 8, 15), |
| 49 | + default_args={"retries": 0}, |
| 50 | + schedule="30 16 * * *" if composer_env.is_prod_env() else None, |
| 51 | + catchup=False, |
| 52 | + tags=[ |
| 53 | + "cloud-ml-auto-solutions", |
| 54 | + "jobset", |
| 55 | + "uptime", |
| 56 | + "tpu-observability", |
| 57 | + "TPU", |
| 58 | + "v6e-16", |
| 59 | + ], |
| 60 | + description=( |
| 61 | + "This DAG tests the jobset uptime metric by deploying a workload on a " |
| 62 | + "TPU v6e-16 node pool and verifying that " |
| 63 | + "the metric behaves as expected." |
| 64 | + ), |
| 65 | + doc_md=""" |
| 66 | + # JobSet Uptime Metric Test Using TPU v6e-16 Node Pool |
| 67 | +
|
| 68 | + ### Description |
| 69 | + This DAG automates the process of creating a TPU v6e-16 node pool, launching |
| 70 | + a jobset, and monitoring the jobset uptime metric to ensure it behaves |
| 71 | + correctly. It also includes a negative test case to verify metric behavior |
| 72 | + over invalid time ranges. Finally, the DAG cleans up all created resources. |
| 73 | +
|
| 74 | + ### Prerequisites |
| 75 | + This test requires an existing GKE cluster with TPU v6e-16 quota. |
| 76 | +
|
| 77 | + ### Procedures |
| 78 | + 1. **Provisioning**: Creates a TPU v6e-16 node pool with a specified reservation. |
| 79 | + 2. **Deployment**: Applies a JobSet workload and waits for Pods to become active. |
| 80 | + 3. **Metric Validation**: Polls the jobset uptime metric to confirm |
| 81 | + it behaves as expected. |
| 82 | + 4. **Negative Testing**: Attempts to verify uptime against a current (future) |
| 83 | + timestamp to ensure the sensor correctly handles out-of-bounds queries. |
| 84 | + 5. **Cleanup**: Deletes both the JobSet workload and the node pool to prevent |
| 85 | + resource leakage. |
| 86 | + """, |
| 87 | +) as dag: |
| 88 | + for machine in MachineConfigMap: |
| 89 | + config = machine.value |
| 90 | + |
| 91 | + # Keyword arguments are generated dynamically at runtime (pylint does not |
| 92 | + # know this signature). |
| 93 | + with TaskGroup( # pylint: disable=unexpected-keyword-arg |
| 94 | + group_id=f"v{config.tpu_version.value}" |
| 95 | + ): |
| 96 | + jobset_config = jobset.build_jobset_from_gcs_yaml( |
| 97 | + gcs_path=GCS_JOBSET_CONFIG_PATH, |
| 98 | + dag_name="jobset_uptime_validation", |
| 99 | + ) |
| 100 | + |
| 101 | + cluster_info = node_pool.build_node_pool_info_from_gcs_yaml.override( |
| 102 | + task_id="build_node_pool_info_from_gcs_yaml" |
| 103 | + )( |
| 104 | + gcs_path=GCS_CONFIG_PATH, |
| 105 | + dag_name="jobset_uptime_validation", |
| 106 | + is_prod=composer_env.is_prod_env(), |
| 107 | + machine_type=config.machine_version.value, |
| 108 | + tpu_topology=config.tpu_topology, |
| 109 | + ) |
| 110 | + |
| 111 | + create_node_pool = node_pool.create.override(task_id="create_node_pool")( |
| 112 | + node_pool=cluster_info, |
| 113 | + ) |
| 114 | + |
| 115 | + apply_time = jobset.run_workload.override(task_id="run_workload")( |
| 116 | + node_pool=cluster_info, |
| 117 | + jobset_config=jobset_config, |
| 118 | + workload_type=Workload.JAX_TPU_BENCHMARK, |
| 119 | + ) |
| 120 | + |
| 121 | + pod_names = jobset.list_pod_names.override(task_id="list_pod_names")( |
| 122 | + node_pool=cluster_info, |
| 123 | + jobset_config=jobset_config, |
| 124 | + ) |
| 125 | + |
| 126 | + wait_for_job_start = jobset.wait_for_jobset_started.override( |
| 127 | + task_id="wait_for_job_start" |
| 128 | + )(cluster_info, pod_name_list=pod_names, job_apply_time=apply_time) |
| 129 | + |
| 130 | + wait_for_jobset_uptime_data = jobset.wait_for_jobset_uptime_data.override( |
| 131 | + task_id="wait_for_jobset_uptime_data" |
| 132 | + )( |
| 133 | + node_pool=cluster_info, |
| 134 | + jobset_config=jobset_config, |
| 135 | + jobset_apply_time=apply_time, |
| 136 | + ) |
| 137 | + |
| 138 | + clean_up_workload = jobset.end_workload.override( |
| 139 | + task_id="clean_up_workload", trigger_rule=TriggerRule.ALL_DONE |
| 140 | + )( |
| 141 | + node_pool=cluster_info, |
| 142 | + jobset_config=jobset_config, |
| 143 | + ).as_teardown( |
| 144 | + setups=apply_time |
| 145 | + ) |
| 146 | + |
| 147 | + jobset_clear_time = get_current_time.override( |
| 148 | + task_id="get_current_time" |
| 149 | + )() |
| 150 | + |
| 151 | + ensure_no_jobset_uptime_data = jobset.ensure_no_jobset_uptime_data.override( |
| 152 | + task_id="ensure_no_jobset_uptime_data" |
| 153 | + )( |
| 154 | + node_pool=cluster_info, |
| 155 | + jobset_config=jobset_config, |
| 156 | + jobset_clear_time=jobset_clear_time, |
| 157 | + # Wait 5 minutes to confirm no data has been detected. |
| 158 | + wait_time_seconds=300, |
| 159 | + ) |
| 160 | + |
| 161 | + cleanup_node_pool = node_pool.delete.override( |
| 162 | + task_id="cleanup_node_pool", trigger_rule=TriggerRule.ALL_DONE |
| 163 | + )(node_pool=cluster_info).as_teardown( |
| 164 | + setups=create_node_pool, |
| 165 | + ) |
| 166 | + |
| 167 | + chain( |
| 168 | + cluster_info, |
| 169 | + create_node_pool, |
| 170 | + apply_time, |
| 171 | + pod_names, |
| 172 | + wait_for_job_start, |
| 173 | + wait_for_jobset_uptime_data, |
| 174 | + clean_up_workload, |
| 175 | + jobset_clear_time, |
| 176 | + ensure_no_jobset_uptime_data, |
| 177 | + cleanup_node_pool, |
| 178 | + ) |
0 commit comments