Datasets:
File size: 5,790 Bytes
e8c001c | 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 | ---
id: offline-compute_MySQL_mysql_002
name: Inference Service Replica Forecast and Bias Coefficient Computation
category: offline-compute/MySQL
timeout_seconds: 1800
modality: pure-text
engine: mysql
---
## Prompt
I need you to generate a MySQL script that, for inference services with autoscaling enabled, expands hourly expected pod counts to 10-minute granularity, clips them using configured upper and lower bounds, computes a bias correction coefficient based on the actual pod count over the past 24 hours, and labels the AHPA recommended status.
**Business Objective**: For all inference services with autoscaling enabled (HPA or AHPA), expand the existing hourly expected pod counts to 10-minute intervals, and clip them using the service's configured replica upper and lower bounds (which must be multiplied by the machine count to convert to pod counts). Combined with the actual running pod count over the past 24 hours, compute a bias correction coefficient (actual pod count / clipped P90 expected value). Additionally, determine the AHPA service recommendation status (normal, degraded, no request). Write the final results to the target table for the partition `dt='2026050700'`.
**Input Tables (full name + brief description)**:
- `internal_platform_db.dwd_tj_model_service_hpa_mysql_002` (HPA configuration dimension table)
- `internal_platform_db.nextgen_platform_dsl_autotune_rec_gpu_config_fht0_mysql_002` (AHPA recommendation fact table)
- `internal_platform_db.dwd_aide_inferencev2_done_service_info_h_mysql_002` (service information dimension table)
- `internal_platform_db.dwm_gputj_platform_hourly_expected_pod_mysql_002` (hourly expected pod count intermediate table)
- `internal_platform_db.dwm_gputj_platform_gpu_base_feature_agg_v2_mysql_002` (actual pod count fact table)
(Please connect to the database and query to confirm the table structures and field semantics.)
**Key Filters and Joins**:
- **Filters**: The service information table must select services where `status='done'`, not deleted (`deleted <> '1'`), and with HPA or AHPA enabled. The HPA configuration table takes the latest visible configuration (first row by `visible desc, id desc`). The AHPA recommendation table takes data from the last 10 minutes (current max timestamp - 600000ms). The hourly expected table takes the latest partition within the past 5 days. The actual pod count takes data from the past 24 hours (`dt >= '2026050600' and dt <= '2026050700'`) with `agg_type=2` and `avg_pod_count > 0`.
- **Joins**: Join all tables primarily by `service_name`. The service information table is inner-joined with the hourly expected table, then left-joined with the HPA configuration table (via `service_id = serving_id`) and the AHPA status table. The hourly expected table must be cross-joined with a set of 6 time offsets (0, 10, 20, 30, 40, 50 minutes) to expand to 10-minute granularity.
**Derived Logic and Business Semantics**:
1. **Scale type**: If `ahpa_enable='true'`, then `'ahpa'`; if `hpa_enable='true'`, then `'hpa'`.
2. **AHPA recommendation status**: For AHPA services, if there are no records in the last 10 minutes, the status is `'no_request'`; if there are records but all have `response_code` other than `'200'`, the status is `'degraded'`; otherwise, `'normal'`.
3. **Pod count upper and lower bounds**: The `min_replicas` and `max_replicas` from the HPA/AHPA configuration must be multiplied by `host_num` (machine count) from the service information to obtain `hpa_min_pods`, `hpa_max_pods`, `ahpa_min_pods`, and `ahpa_max_pods`.
4. **Expected value clipping**: All 8 expected pod count fields (e.g., `expected_pod_avg_qpm_p90`) must be clipped using the computed upper and lower bounds (`GREATEST(LEAST(original_value, upper_bound), lower_bound)`).
5. **Bias coefficient**: Bias coefficient = `actual_pod_count` / clipped `expected_pod_avg_qpm_p90` value (use 1.0 when the denominator is 0).
6. **Time expansion**: Expand the hourly prediction time to 6 time points at 0, 10, 20, 30, 40, and 50 minutes of each hour (constructed using `CONCAT(SUBSTRING(agg_time,1,14), LPAD(offset,2,'0'), ':00')`).
**Output Requirements**:
- Target table: `internal_platform_db.dwm_gputj_platform_replica_forecast_with_bias_cand_mysql_002`
- Output field order: `service_name`, `instance_uuid`, `workload_name`, `namespace`, `agg_time`, `hour_of_day`, `scale_type`, `host_num`, `gpu_name`, `queue_name`, `hpa_min_pods`, `hpa_max_pods`, `ahpa_min_pods`, `ahpa_max_pods`, `ahpa_status`, `ahpa_response_code`, `expected_pod_avg_qpm_avg`, `expected_pod_avg_qpm_p50`, `expected_pod_avg_qpm_p90`, `expected_pod_max_qpm_avg`, `expected_pod_max_qpm_p50`, `expected_pod_max_qpm_p90`, `expected_pod_latest_avg_qpm`, `expected_pod_latest_max_qpm`, `actual_pod_count`, `bias_coefficient`, `dt`, `host_gpu_num`, `model_req_count`
- `agg_time` format: `yyyy-MM-dd HH:mm:00`, at 10-minute granularity
- `hour_of_day` format: `HH:00:00`
- Write partition: `dt='2026050700'`
- If the target table does not exist, first create it using standard MySQL InnoDB format, then write the data
- Use standard MySQL syntax; do not use Hive/Spark SQL dialects (do not use `INSERT OVERWRITE`; use `INSERT INTO ... SELECT`)
**Environment and Execution Notes**:
- Your final output must be written to the file `/tmp_workspace/result.py`, not `result.sql`
- The local MySQL is running at localhost:3306, username `root`, password `root123`
- Use Python `pymysql` in `result.py` to execute the SQL (do not use the `mysql` command-line tool)
- The script must include complete table creation (if the target table does not exist) and data writing logic
- After writing `result.py`, you must execute `python3 /tmp_workspace/result.py` yourself to verify that it runs successfully and produces correct data
|