|
| 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 | +"""Helper module for scheduling DAGs across clusters.""" |
| 16 | +import datetime as dt |
| 17 | +import enum |
| 18 | +from typing import TypeAlias |
| 19 | + |
| 20 | +from xlml.apis.xpk_cluster_config import XpkClusterConfig |
| 21 | +from dags.common.vm_resource import TpuVersion, Zone |
| 22 | + |
| 23 | + |
| 24 | +class DayOfWeek(enum.Enum): |
| 25 | + ALL = "*" |
| 26 | + WEEK_DAY = "1-5" |
| 27 | + WEEKEND = "0,6" |
| 28 | + |
| 29 | + |
| 30 | +# Mock cluster to group TPU Observability DAGs |
| 31 | +TPU_OBS_MOCK_CLUSTER = XpkClusterConfig( |
| 32 | + name="tpu-observability-automation-prod", |
| 33 | + device_version=TpuVersion.TRILLIUM, |
| 34 | + core_count=16, |
| 35 | + project="cienet-cmcs", |
| 36 | + zone=Zone.US_CENTRAL1_B.value, |
| 37 | +) |
| 38 | + |
| 39 | +DagIdToTimeout: TypeAlias = dict[str, dt.timedelta] |
| 40 | + |
| 41 | +REGISTERED_DAGS: dict[str, DagIdToTimeout] = { |
| 42 | + TPU_OBS_MOCK_CLUSTER.name: { |
| 43 | + "gke_node_pool_label_update": dt.timedelta(minutes=30), |
| 44 | + "gke_node_pool_status": dt.timedelta(minutes=30), |
| 45 | + "jobset_rollback_ttr": dt.timedelta(minutes=90), |
| 46 | + "jobset_ttr_node_pool_resize": dt.timedelta(minutes=90), |
| 47 | + "jobset_ttr_pod_delete": dt.timedelta(minutes=90), |
| 48 | + "multi-host-availability-rollback": dt.timedelta(minutes=30), |
| 49 | + "node_pool_ttr_disk_size": dt.timedelta(minutes=90), |
| 50 | + "node_pool_ttr_update_label": dt.timedelta(minutes=90), |
| 51 | + "tpu_info_format_validation_dag": dt.timedelta(minutes=30), |
| 52 | + "tpu_sdk_monitoring_validation": dt.timedelta(minutes=30), |
| 53 | + "jobset_ttr_kill_process": dt.timedelta(minutes=90), |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +def get_dag_timeout(dag_id: str) -> dt.timedelta: |
| 59 | + """Searches the registry and returns the specific timeout for a DAG.""" |
| 60 | + for cluster_dags in REGISTERED_DAGS.values(): |
| 61 | + if dag_id in cluster_dags: |
| 62 | + return cluster_dags[dag_id] |
| 63 | + raise ValueError( |
| 64 | + f"DAG '{dag_id}' is not registered. Please add it to REGISTERED_DAGS." |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class SchedulingHelper: |
| 69 | + """Manages DAG scheduling across different clusters.""" |
| 70 | + |
| 71 | + DEFAULT_MARGIN = dt.timedelta(minutes=15) |
| 72 | + DEFAULT_ANCHOR = dt.datetime(2000, 1, 1, 8, 0, 0, tzinfo=dt.timezone.utc) |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def arrange_schedule_time( |
| 76 | + cls, |
| 77 | + dag_id: str, |
| 78 | + day_of_week: DayOfWeek = DayOfWeek.ALL, |
| 79 | + ) -> str: |
| 80 | + """Calculates a cron schedule by stacking timeouts and margins.""" |
| 81 | + anchor = cls.DEFAULT_ANCHOR |
| 82 | + |
| 83 | + for cluster_name, dags in REGISTERED_DAGS.items(): |
| 84 | + if dag_id not in dags: |
| 85 | + continue |
| 86 | + |
| 87 | + offset = dt.timedelta(0) |
| 88 | + for current_dag_id, timeout in dags.items(): |
| 89 | + if current_dag_id == dag_id: |
| 90 | + schedule = anchor + offset |
| 91 | + return f"{schedule.minute} {schedule.hour} * * {day_of_week.value}" |
| 92 | + offset += timeout + cls.DEFAULT_MARGIN |
| 93 | + |
| 94 | + if offset >= dt.timedelta(hours=24): |
| 95 | + raise ValueError( |
| 96 | + f"Schedule exceeds 24h window at '{dag_id}' in cluster '{cluster_name}'." |
| 97 | + ) |
| 98 | + |
| 99 | + raise ValueError( |
| 100 | + f"DAG '{dag_id}' is not registered. Please add it to REGISTERED_DAGS." |
| 101 | + ) |
0 commit comments