groups:
- name: compact-backup
  rules:
  # Compact backup duration alerts
  - alert: CompactBackupDurationTooLong
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} compact backup running duration more than 5h
    expr: |
      dataflow_compact_backup_execution_status_duration_seconds{status="running"} > 60 * 60 * 5
    labels:
      severity: major
      response_class: ticket
      component: compact-backup
      oncall_team: tidb_x_tools
  
  - alert: CompactBackupDurationCritical
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} compact backup running duration more than 7h. This may indicate a serious issue with the compact operation.
    expr: |
      dataflow_compact_backup_execution_status_duration_seconds{status="running"} > 60 * 60 * 7
    labels:
      severity: critical
      response_class: page
      component: compact-backup
      oncall_team: tidb_x_tools

  # Compact backup status alerts
  - alert: CompactBackupStatusFailed
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} compact backup {{ $labels.compact_backup_id }} status is failed.
    expr: |
      dataflow_compact_backup_execution_status_duration_seconds{status="failed"} > 0
    labels:
      severity: critical
      response_class: page
      alerttype: event
      component: compact-backup
      oncall_team: tidb_x_tools

  - alert: CompactBackupStatusPending
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} compact backup in pending status more than 1h. Please check the compact backup scheduler and resource availability.
    expr: |
      dataflow_compact_backup_execution_status_duration_seconds{status="pending"} > 60 * 60 * 1
    labels:
      severity: major
      response_class: ticket
      component: compact-backup
      oncall_team: tidb_x_tools

  - alert: CompactBackupDeletingTooLong
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} compact backup {{ $labels.compact_backup_id }} stays in deleting status for more than 60 minutes.
    expr: |
      dataflow_compact_backup_execution_status_duration_seconds{status="deleting"} > 60 * 60
    labels:
      severity: major
      response_class: ticket
      component: compact-backup
      oncall_team: tidb_x_tools
  - alert: CompactBackupDeletingTooLongCritical
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} compact backup {{ $labels.compact_backup_id }} stays in deleting status for more than 3 hours.
    expr: |
      dataflow_compact_backup_execution_status_duration_seconds{status="deleting"} > 60 * 60 * 3
    labels:
      severity: critical
      response_class: page
      component: compact-backup
      oncall_team: tidb_x_tools

  # Compact backup health alerts
  - alert: CompactBackupFailedRateHigh
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} has multiple compact backup failures in the last hour.
    expr: |
      increase(dataflow_compact_backup_failed_total[1h]) > 2
    labels:
      severity: critical
      response_class: page
      component: compact-backup
      oncall_team: tidb_x_tools

  - alert: CompactBackupStuck
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} has {{ $value }} compact backups stuck in running state.
    expr: |
      dataflow_compact_backup_in_progress > 2
    for: 30m
    labels:
      severity: major
      response_class: ticket
      component: compact-backup
      oncall_team: tidb_x_tools

  # Compact backup scheduler alert
  - alert: CompactBackupSchedulerAbnormal
    annotations:
      message: The compact backup scheduler encountered abnormal conditions. Please check the scheduler logs.
    expr: |
      increase(dataflow_worker_backup_scheduler_abnormal{type="compact_backup"}[1h]) > 2
    labels:
      severity: critical
      response_class: page
      component: compact-backup
      oncall_team: tidb_x_tools

  # Long-running compact backup for specific clusters (if needed)
  - alert: CompactBackupExtendedDuration
    annotations:
      message: The TiDB Cluster {{ $labels.tenant_id }}/{{ $labels.cluster_id }} compact backup {{ $labels.compact_backup_id }} has been running for more than 12 hours. Consider manual intervention.
    expr: |
      dataflow_compact_backup_execution_status_duration_seconds{status="running"} > 60 * 60 * 12
    labels:
      severity: critical
      response_class: page
      component: compact-backup
      oncall_team: tidb_x_tools
