{
  "repo": "tidbcloud/aws-shared-provider",
  "binary": "tidb-worker",
  "binary_path": "cmd/tidb-worker/main.go",
  "dependencies": {
    "TidbWorkerLifecycle.LoadFlags": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerLifecycle.BuildKubeConfig": {
      "infrastructure": {
        "filesystem": {
          "conditions": [
            "--kubeconfig is set"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Load the kubeconfig used by tidb-worker",
              "conditions": [
                "Only when the kubeconfig flag points to a file"
              ],
              "implementation": "clientcmd.BuildConfigFromFlags(\"\", kubeConfigPath)"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerLifecycle.CreateManager": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "Always after BuildKubeConfig"
          ],
          "operations": [
            {
              "operation": "BUILD_MANAGER",
              "trigger": "Create the controller-runtime manager that owns caches, leader election, and the metrics listener",
              "conditions": [
                "Always during server.New(cfg)"
              ],
              "implementation": "ctrl.NewManager(cfg.KubeConfig, manager.Options{LeaderElection: cfg.EnableLeaderElection, Metrics: metricsserver.Options{BindAddress: cfg.MetricBindAddr, ...}})"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerLifecycle.InitStorage": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "--dsn is configured and server startup reached initDB"
          ],
          "operations": [
            {
              "operation": "CONNECT",
              "trigger": "Open the base MySQL connection before selecting the worker database",
              "conditions": [
                "Always during initDB(cfg.DSN)"
              ],
              "implementation": "gorm.Open(mysql.Open(baseDsn))"
            },
            {
              "operation": "EXECUTE",
              "trigger": "Ensure the worker task database exists",
              "conditions": [
                "Always after the base MySQL connection succeeds"
              ],
              "implementation": "db.Exec(fmt.Sprintf(\"CREATE DATABASE IF NOT EXISTS %s\", dbName))"
            },
            {
              "operation": "CONNECT",
              "trigger": "Open the actual worker task database connection",
              "conditions": [
                "Always after the database exists"
              ],
              "implementation": "gorm.Open(mysql.Open(cfg.FormatDSN()))"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerLifecycle.CreateServer": {
      "infrastructure": {
        "filesystem": {
          "conditions": [
            "TLS file flags are configured"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Load the CA, certificate, and key used by the primary listener and metrics listener",
              "conditions": [
                "Only when any TLS path is configured"
              ],
              "implementation": "util.NewTLSConfig(util.WithCAPath(cfg.Security.CA), util.WithCertAndKeyPath(cfg.Security.Cert, cfg.Security.Key))"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerLifecycle.CreatePDClient": {
      "infrastructure": {},
      "services": [
        {
          "name": "pd-server",
          "target_interface": "PD keyspace metadata and GC safe point APIs",
          "operation_type": "client_init",
          "conditions": [
            "Always after the server and storage are initialized"
          ],
          "operations": [
            {
              "operation": "CONNECT",
              "trigger": "Create the PD client used by GC, GCV2, TTL, background-task, and auto-analyze handlers",
              "conditions": [
                "Always during server.New(cfg)"
              ],
              "implementation": "pd.NewClient(cfg.PDAddr, pd.SecurityOption{KeyPath: cfg.Security.Key, CertPath: cfg.Security.Cert, CAPath: cfg.Security.CA})"
            }
          ]
        }
      ]
    },
    "TidbWorkerLifecycle.InitAdminController": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "Always after the manager and storage exist"
          ],
          "operations": [
            {
              "operation": "LIST_WATCH",
              "trigger": "Watch the tidb-worker admin ConfigMap to keep pause, cleanup, and worker-image settings fresh",
              "conditions": [
                "Always after admin.NewReconciler(mgr, db)"
              ],
              "implementation": "controller.New(...); c.Watch(source.Kind(mgr.GetCache(), &corev1.ConfigMap{}, ... predicate namespace=tidb-worker,name=admin-cm))"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerLifecycle.InitOptionalResourceManager": {
      "infrastructure": {
        "background_runtime": {
          "conditions": [
            "--enable-resource-manager=true"
          ],
          "operations": [
            {
              "operation": "CREATE_SCHEDULER",
              "trigger": "Construct the optional scheduler, pool manager, node manager, resource limiter, and debug server stack",
              "conditions": [
                "Only when resource-manager mode is enabled"
              ],
              "implementation": "poolmanager.NewManager(...); scheduler.NewScheduler(db, svr.poolManager); svr.Add(svr.workerResourceManager)"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerLifecycle.RegisterGCWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerLifecycle.RegisterGCV2Worker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerLifecycle.RegisterBgTaskWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerLifecycle.RegisterTTLWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerLifecycle.RegisterRemoteQueryWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerLifecycle.RegisterAutoAnalyzeWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerLifecycle.StartPrimaryListeners": {
      "infrastructure": {
        "http_server": {
          "conditions": [
            "--api-server-addr is not empty"
          ],
          "operations": [
            {
              "operation": "LISTEN_AND_SERVE",
              "trigger": "Serve the primary HTTP API on the cmux HTTP branch",
              "conditions": [
                "Always after all handlers are registered"
              ],
              "implementation": "net.Listen(\"tcp\", apiServerAddr); cmux.New(ln); http.Serve(httpListener, s.apiServer)"
            }
          ]
        },
        "grpc_server": {
          "conditions": [
            "--api-server-addr is not empty"
          ],
          "operations": [
            {
              "operation": "REGISTER_AND_SERVE",
              "trigger": "Serve the Scaler gRPC API on the cmux gRPC branch",
              "conditions": [
                "Always after the primary listener is created"
              ],
              "implementation": "scalerpb.RegisterScalerServer(s.grpcServer, &service.ScalerService{Server: s}); s.grpcServer.Serve(grpcListener)"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerLifecycle.StartManager": {
      "infrastructure": {
        "background_runtime": {
          "conditions": [
            "Always after StartPrimaryListeners"
          ],
          "operations": [
            {
              "operation": "START_MANAGER",
              "trigger": "Run all controller-runtime runnables, including the admin reconciler and task handlers",
              "conditions": [
                "Always unless startup returns an error"
              ],
              "implementation": "s.Manager.Start(ctx)"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerMetrics.Metrics": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerAPI.PauseTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "The request reached /scaler/api/v1/admin/pause"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Mark GC, GCV2, background-task, auto-analyze, TTL task rows, and TTL table info as paused",
              "conditions": [
                "Always after the optional keyspace and pool filters are derived"
              ],
              "implementation": "cw.db.Transaction(func(tx *gorm.DB) error { tx.Model(&gc.Task{}).Scopes(scope).Update(...); ... tx.Model(&ttl_task.TTLTableInfo{}).Update(\"paused\", true) })"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "The SQL transaction above succeeded"
          ],
          "operations": [
            {
              "operation": "DELETE_ALL_OF",
              "trigger": "Delete matching worker jobs so paused tasks stop running immediately",
              "conditions": [
                "Always after the task rows are marked paused"
              ],
              "implementation": "cw.manager.GetClient().DeleteAllOf(..., &batchv1.Job{}, client.InNamespace(os.Getenv(\"NAMESPACE\")), client.MatchingLabels(targetLabels(keyspace, pool)))"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerAPI.ResumeTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "The request reached /scaler/api/v1/admin/resume"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Move paused GC, GCV2, background-task, auto-analyze, and TTL rows back to pending and unpause TTL table info",
              "conditions": [
                "Always after the optional keyspace and pool filters are derived"
              ],
              "implementation": "cw.db.Transaction(func(tx *gorm.DB) error { tx.Model(&gc.Task{}).Where(\"state = ?\", gc.StatePaused).Update(...); ... tx.Model(&ttl_task.TTLTableInfo{}).Update(\"paused\", false) })"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerAPI.CleanTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "The request reached /scaler/api/v1/admin/clean",
            "keyspace query parameter is not empty"
          ],
          "operations": [
            {
              "operation": "DELETE",
              "trigger": "Delete task rows and TTL table metadata for the selected keyspace and optional pool",
              "conditions": [
                "Always after keyspace validation succeeds"
              ],
              "implementation": "cw.db.Transaction(func(tx *gorm.DB) error { tx.Model(&gc.Task{}).Scopes(scope).Delete(...); ... tx.Model(&ttl_task.TTLTableInfo{}).Delete(...) })"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "The SQL transaction above succeeded"
          ],
          "operations": [
            {
              "operation": "DELETE_ALL_OF",
              "trigger": "Delete matching worker jobs for the selected keyspace or pool",
              "conditions": [
                "Always after the task rows are deleted"
              ],
              "implementation": "cw.manager.GetClient().DeleteAllOf(..., &batchv1.Job{}, client.InNamespace(os.Getenv(\"NAMESPACE\")), client.MatchingLabels(targetLabels(keyspace, pool)))"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerAPI.SetLogLevel": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerAPI.SetTestKeyspace": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerAPI.GetKeyspacePodCount": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "The request reached /scaler/api/v1/admin/get_keyspace_pod_ct",
            "keyspace query parameter is not empty"
          ],
          "operations": [
            {
              "operation": "LIST",
              "trigger": "Count worker pods for the selected keyspace",
              "conditions": [
                "Always after keyspace validation succeeds"
              ],
              "implementation": "cw.manager.GetClient().List(..., &corev1.PodList{}, client.InNamespace(\"tidb-worker\"), client.MatchingLabels{types.KeyspaceNameLabelKey: keyspace, labels.ComponentLabelKey: \"worker\"})"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerAPI.GetBatchWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerAPI.GetDDLWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerAPI.GetImportIntoWorker": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerAPI.GetBatchWorkerByKeyspace": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "--enable-worker-autoscale=true",
            "batch worker auto scale is not disabled"
          ],
          "operations": [
            {
              "operation": "LIST_FROM_CACHE",
              "trigger": "Read shared-worker jobs from the informer cache to calculate the keyspace worker count",
              "conditions": [
                "Only when shared-worker autoscaling is enabled for batch tasks"
              ],
              "implementation": "h.getExecJobsCount(mux.Vars(r)[\"keyspace\"]) -> h.jobLister.List(selector)"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerAPI.GetDDLWorkerByKeyspace": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "--enable-worker-autoscale=true",
            "DDL worker auto scale is not disabled"
          ],
          "operations": [
            {
              "operation": "LIST_FROM_CACHE",
              "trigger": "Read shared-worker jobs from the informer cache to calculate the keyspace worker count",
              "conditions": [
                "Only when shared-worker autoscaling is enabled for DDL tasks"
              ],
              "implementation": "h.getExecJobsCount(mux.Vars(r)[\"keyspace\"]) -> h.jobLister.List(selector)"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerAPI.GetImportIntoWorkerByKeyspace": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "--enable-worker-autoscale=true",
            "import-into worker auto scale is not disabled"
          ],
          "operations": [
            {
              "operation": "LIST_FROM_CACHE",
              "trigger": "Read shared-worker jobs from the informer cache to calculate the keyspace worker count",
              "conditions": [
                "Only when shared-worker autoscaling is enabled for import-into tasks"
              ],
              "implementation": "h.getExecJobsCount(mux.Vars(r)[\"keyspace\"]) -> h.jobLister.List(selector)"
            }
          ]
        }
      },
      "services": []
    },
    "TidbWorkerAPI.GetAutoAnalyzeWorkerByKeyspace": {
      "infrastructure": {},
      "services": []
    },
    "TidbWorkerAPI.SubmitRemoteQuery": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "The request body decoded into a remote-query payload successfully",
            "adminConfig.IsPaused(\"remote-query\") is false"
          ],
          "operations": [
            {
              "operation": "GET",
              "trigger": "Resolve worker pod template inputs from pool and scaler ConfigMaps while building the remote-query job",
              "conditions": [
                "Always after request decoding succeeds"
              ],
              "implementation": "h.jobBuilder.Build(...) -> acquireWorkerConfig(...) -> client.Get(poolConfig); client.Get(workerConfig)"
            },
            {
              "operation": "CREATE",
              "trigger": "Create the remote-query worker job",
              "conditions": [
                "Only when jobBuilder.Build succeeds"
              ],
              "implementation": "h.clusterCli.Create(r.Context(), job)"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.Ping": {
      "infrastructure": {},
      "services": []
    },
    "ScalerGRPC.RegisterGC": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "INSERT",
              "trigger": "Insert a pending GC task row",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Clauses(clause.OnConflict{DoNothing: true}).Create(&gc.Task{...})"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RecycleGC": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Mark GC rows at or below the reported safe point as done",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Model(&gc.Task{}).Where(\"deletion_ts < ?\", request.GetSafePoint()).Update(\"state\", gc.StateDone)"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RegisterGCV2": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "TRANSACTION",
              "trigger": "Retire older GCV2 rows and insert the new pending GCV2 task",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Transaction(func(tx *gorm.DB) error { tx.Model(&gcv2.Task{}).Where(...).Update(\"state\", gcv2.StateDone); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&gcv2.Task{...}) })"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RecycleGCV2": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Mark GCV2 rows at or below the reported safe point as done",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Model(&gcv2.Task{}).Where(\"last_gc_run_ts < ?\", request.GetSafePoint()).Update(\"state\", gcv2.StateDone)"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.UpdateGCLifeTime": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Persist the latest GC life time for the keyspace",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Model(&gcv2.Task{}).Where(\"keyspace_name = ?\", request.GetHeader().GetKeyspaceName()).Update(\"gc_life_time\", request.GetGcLifeTime())"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.GetBgTaskConfig": {
      "infrastructure": {},
      "services": []
    },
    "ScalerGRPC.RegisterBgTask": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "INSERT",
              "trigger": "Insert a pending background-task row",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Clauses(clause.OnConflict{DoNothing: true}).Create(&bgtask.Task{...})"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RecycleBgTask": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Mark a background-task row as done",
              "conditions": [
                "Always after request validation succeeds",
                "sub_task_id filter is only applied when request.GetSubTaskId() != 0"
              ],
              "implementation": "txn := db.Model(&bgtask.Task{}).Where(\"keyspace_name = ?\", ...).Where(\"global_task_id = ?\", ...); if request.GetSubTaskId() != 0 { txn = txn.Where(\"sub_task_id = ?\", ...) }; txn.Update(\"state\", bgtask.StateDone)"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.UpdateBgTaskExecID": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "TRANSACTION",
              "trigger": "Rewrite exec IDs for the selected background sub-task IDs",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Transaction(func(txn *gorm.DB) error { for execID, subTaskIDs := range groupedUpdates { txn.Model(&bgtask.Task{}).Where(...).Where(\"sub_task_id IN ?\", subTaskIDs).Update(\"exec_id\", execID) } })"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RegisterRemoteQuery": {
      "infrastructure": {},
      "services": []
    },
    "ScalerGRPC.RegisterTTLTask": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "TRANSACTION",
              "trigger": "Upsert TTL table metadata, retire older TTL rows, and insert the new pending TTL task",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Transaction(func(tx *gorm.DB) error { tx.Clauses(clause.OnConflict{...}).Create(&ttl_task.TTLTableInfo{...}); tx.Model(&ttl_task.Task{}).Where(...).Update(\"state\", ttl_task.StateDeleted); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&ttl_task.Task{...}) })"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.DeleteTTLTableInfo": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "TRANSACTION",
              "trigger": "Delete TTL table metadata and mark corresponding TTL task rows deleted",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Transaction(func(tx *gorm.DB) error { tx.Model(&ttl_task.TTLTableInfo{}).Where(...).Delete(nil); tx.Model(&ttl_task.Task{}).Where(...).Update(\"state\", ttl_task.StateDeleted) })"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RecycleTTLTask": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Mark TTL task rows completed before the reported finish time as done",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Model(&ttl_task.Task{}).Where(\"last_ttl_run_time < ?\", request.GetFinishTime()).Update(\"state\", ttl_task.StateDone)"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.UpdateTTLJobEnable": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Persist the new TTL enable switch for the keyspace",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Model(&ttl_task.TTLTableInfo{}).Where(\"keyspace_name = ?\", request.GetHeader().GetKeyspaceName()).Update(\"is_enable\", request.GetTtlJobEnable())"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RegisterAutoAnalyze": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "INSERT",
              "trigger": "Insert a pending shared-worker background task for auto-analyze",
              "conditions": [
                "cfg.EnableAutoAnalyze && cfg.EnableSharedWorkerForAutoAnalyze"
              ],
              "implementation": "db.Clauses(clause.OnConflict{DoNothing: true}).Create(&bgtask.Task{TaskType: \"auto-analyze\", ...})"
            },
            {
              "operation": "INSERT",
              "trigger": "Insert a dedicated pending auto-analyze task",
              "conditions": [
                "Only when shared-worker mode for auto-analyze is disabled"
              ],
              "implementation": "db.Clauses(clause.OnConflict{DoNothing: true}).Create(&autoanalyze.Task{...})"
            }
          ]
        }
      },
      "services": []
    },
    "ScalerGRPC.RecycleAutoAnalyze": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "request != nil and request.Header != nil"
          ],
          "operations": [
            {
              "operation": "UPDATE",
              "trigger": "Mark the dedicated auto-analyze task done",
              "conditions": [
                "Always after request validation succeeds"
              ],
              "implementation": "db.Model(&autoanalyze.Task{}).Where(...).Update(\"state\", autoanalyze.StateDone)"
            },
            {
              "operation": "UPDATE",
              "trigger": "Fallback to the shared-worker background-task table when no dedicated row exists",
              "conditions": [
                "Only when the dedicated auto-analyze update affected zero rows"
              ],
              "implementation": "db.Model(&bgtask.Task{}).Where(\"global_task_id = ?\", request.GetTaskId()).Where(\"task_type = ?\", \"auto-analyze\").Update(\"state\", bgtask.StateDone)"
            }
          ]
        }
      },
      "services": []
    },
    "AdminConfigController.ReconcileAdminConfig": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "The manager cache observed an update for tidb-worker/admin-cm"
          ],
          "operations": [
            {
              "operation": "GET",
              "trigger": "Read the current admin ConfigMap contents",
              "conditions": [
                "Always for each reconcile event"
              ],
              "implementation": "cw.manager.GetClient().Get(ctx, req.NamespacedName, &configMap)"
            }
          ]
        }
      },
      "services": []
    },
    "AdminConfigController.CleanupMarkedJobs": {
      "infrastructure": {
        "kubernetes_api": {
          "conditions": [
            "Every cleanupJobsInterval",
            "At least one task type has cleanup=true in the admin ConfigMap"
          ],
          "operations": [
            {
              "operation": "DELETE_ALL_OF",
              "trigger": "Delete jobs for task types marked for cleanup",
              "conditions": [
                "Only for task types whose WorkerConfig.Cleanup is true"
              ],
              "implementation": "cw.manager.GetClient().DeleteAllOf(ctx, &batchv1.Job{}, client.InNamespace(os.Getenv(\"NAMESPACE\")), client.MatchingLabels(map[string]string{types.JobTypeLabelKey: t}))"
            }
          ]
        }
      },
      "services": []
    },
    "GCWorker.PollTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "GC worker was registered and leader election granted"
          ],
          "operations": [
            {
              "operation": "SELECT/UPDATE",
              "trigger": "Load pending GC tasks and mark them running once a worker or resource-manager task is allocated",
              "conditions": [
                "Skips entirely when adminConfig.IsPaused(\"gc\") is true"
              ],
              "implementation": "db.Model(&gc.Task{}).Select(...).Where(\"state = ?\", gc.StatePending)...Find(&pendingTasks); syncJobIntoTable(...)"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "Only when resource-manager mode is disabled"
          ],
          "operations": [
            {
              "operation": "LIST",
              "trigger": "Check the current GC job quota before launching new workers",
              "conditions": [
                "Always before activating jobs"
              ],
              "implementation": "job.CheckJobQuota(h.clusterCli, h.namespace, types2.JobTypeLabelKey, string(job.TypeGC), h.jobLimit)"
            },
            {
              "operation": "GET/CREATE/DELETE",
              "trigger": "Read worker ConfigMaps, create a GC job, and delete orphan jobs that fail to sync into MySQL",
              "conditions": [
                "Only when a pending GC task survives keyspace validation and resource-manager mode is disabled"
              ],
              "implementation": "h.jobBuilder.Build(...); h.clusterCli.Create(ctx, jobSpec); h.clusterCli.Delete(ctx, jobSpec, ...)"
            }
          ]
        },
        "internal_scheduler": {
          "conditions": [
            "h.taskRM != nil"
          ],
          "operations": [
            {
              "operation": "ALLOCATE_RESOURCE",
              "trigger": "Launch the GC task through the internal resource manager instead of a Kubernetes Job",
              "conditions": [
                "Only when resource-manager support is enabled for GC"
              ],
              "implementation": "h.taskRM.AllocateResource(ctx, schedulerutils.MakeGCRequest(keyspaceName, taskID))"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "UpdateGCSafePoint and LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always while GC polling is active"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Read the global GC safe point and keyspace state before activating workers",
              "conditions": [
                "Always for each poll round"
              ],
              "implementation": "h.pdCli.UpdateGCSafePoint(ctx, 0); h.pdCli.LoadKeyspace(ctx, task.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "GCWorker.RecycleDoneJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleDoneJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/TRANSACTION",
              "trigger": "Find finished GC rows, delete them, and archive them into gc_task_done",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&gc.Task{}).Where(\"state = ?\", gc.StateDone).Find(&doneTasks); db.Transaction(func(tx *gorm.DB) error { tx.Delete(doneTask); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&gc.TaskDone{...}) })"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "doneTask.Runner is not empty",
            "Either resource-manager mode is disabled or the runner is not an RM task ID"
          ],
          "operations": [
            {
              "operation": "DELETE",
              "trigger": "Delete finished GC worker jobs",
              "conditions": [
                "Only for regular Kubernetes Job-backed GC runners"
              ],
              "implementation": "h.clusterCli.Delete(ctx, &batchv1.Job{ObjectMeta: metav1.ObjectMeta{Name: jobName, Namespace: h.namespace}}, ...)"
            }
          ]
        },
        "internal_scheduler": {
          "conditions": [
            "h.taskRM != nil",
            "runner name starts with rm-tidb-worker-gc"
          ],
          "operations": [
            {
              "operation": "RECYCLE_RESOURCE",
              "trigger": "Recycle GC resource-manager workers instead of deleting Kubernetes Jobs",
              "conditions": [
                "Only for RM-backed GC tasks"
              ],
              "implementation": "h.taskRM.RecycleResource(ctx, keyspaceName, jobName)"
            }
          ]
        }
      },
      "services": []
    },
    "GCWorker.RecycleOrphanJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleOrphanJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/UPDATE",
              "trigger": "Load running GC rows and mark them done when their keyspace is no longer enabled",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&gc.Task{}).Distinct(\"keyspace_name, runner\").Where(\"state = ?\", gc.StateRunning).Find(&results); db.Model(&gc.Task{}).Where(...).Update(\"state\", gc.StateDone)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always for each running GC runner"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before keeping the runner alive",
              "conditions": [
                "Always during orphan GC scan"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, r.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "GCWorker.UpdateMetrics": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every updateMetricsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Aggregate GC task counts by state for Prometheus gauges",
              "conditions": [
                "Always during metric refresh"
              ],
              "implementation": "db.Model(&gc.Task{}).Select(\"state, count(DISTINCT keyspace_name) as count\").Group(\"state\").Find(&taskCounts)"
            }
          ]
        }
      },
      "services": []
    },
    "GCV2Worker.PollTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "GCV2 worker was registered and leader election granted"
          ],
          "operations": [
            {
              "operation": "RAW_SELECT/UPDATE",
              "trigger": "Load eligible GCV2 rows and mark them running once a worker or resource-manager task is allocated",
              "conditions": [
                "Skips entirely when adminConfig.IsPaused(\"gcv2\") is true"
              ],
              "implementation": "db.Raw(`SELECT ... FROM gc_v2_tasks ...`).Scan(&pendingTasks); syncJobIntoTable(...)"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "Only when resource-manager mode is disabled"
          ],
          "operations": [
            {
              "operation": "LIST",
              "trigger": "Check the current GCV2 job quota before launching new workers",
              "conditions": [
                "Always before activating jobs"
              ],
              "implementation": "job.CheckJobQuota(h.clusterCli, h.namespace, types2.JobTypeLabelKey, string(job.TypeGCV2), h.jobLimit)"
            },
            {
              "operation": "GET/CREATE/DELETE",
              "trigger": "Read worker ConfigMaps, create a GCV2 job, and delete orphan jobs that fail to sync into MySQL",
              "conditions": [
                "Only when a pending GCV2 task survives keyspace validation and resource-manager mode is disabled"
              ],
              "implementation": "h.jobBuilder.Build(...); h.clusterCli.Create(ctx, jobSpec); h.clusterCli.Delete(ctx, jobSpec, ...)"
            }
          ]
        },
        "internal_scheduler": {
          "conditions": [
            "h.taskRM != nil"
          ],
          "operations": [
            {
              "operation": "ALLOCATE_RESOURCE",
              "trigger": "Launch the GCV2 task through the internal resource manager instead of a Kubernetes Job",
              "conditions": [
                "Only when resource-manager support is enabled for GCV2"
              ],
              "implementation": "h.taskRM.AllocateResource(ctx, schedulerutils.MakeGCV2Request(keyspaceName, taskID))"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always while GCV2 polling is active"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before activating a GCV2 worker",
              "conditions": [
                "Always for each pending task candidate"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, task.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "GCV2Worker.RecycleDoneJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleDoneJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/TRANSACTION",
              "trigger": "Find finished GCV2 rows, delete them, and archive them into gc_v2_task_done",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&gcv2.Task{}).Where(\"state = ?\", gcv2.StateDone).Find(&doneTasks); db.Transaction(func(tx *gorm.DB) error { tx.Delete(doneTask); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&gcv2.TaskDone{...}) })"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "doneTask.Runner is not empty",
            "Either resource-manager mode is disabled or the runner is not an RM task ID"
          ],
          "operations": [
            {
              "operation": "DELETE",
              "trigger": "Delete finished GCV2 worker jobs",
              "conditions": [
                "Only for regular Kubernetes Job-backed GCV2 runners"
              ],
              "implementation": "h.clusterCli.Delete(ctx, &batchv1.Job{ObjectMeta: metav1.ObjectMeta{Name: jobName, Namespace: h.namespace}}, ...)"
            }
          ]
        },
        "internal_scheduler": {
          "conditions": [
            "h.taskRM != nil",
            "runner name starts with rm-tidb-worker-gcv2"
          ],
          "operations": [
            {
              "operation": "RECYCLE_RESOURCE",
              "trigger": "Recycle GCV2 resource-manager workers instead of deleting Kubernetes Jobs",
              "conditions": [
                "Only for RM-backed GCV2 tasks"
              ],
              "implementation": "h.taskRM.RecycleResource(ctx, keyspaceName, jobName)"
            }
          ]
        }
      },
      "services": []
    },
    "GCV2Worker.RecycleOrphanJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleOrphanJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/UPDATE",
              "trigger": "Load running GCV2 rows and mark them done when their keyspace is no longer enabled",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&gcv2.Task{}).Distinct(\"keyspace_name, runner\").Where(\"state = ?\", gcv2.StateRunning).Scan(&results); db.Model(&gcv2.Task{}).Where(...).Update(\"state\", gcv2.StateDone)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always for each running GCV2 runner"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before keeping the runner alive",
              "conditions": [
                "Always during orphan GCV2 scan"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, r.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "GCV2Worker.RecycleJammedWorkers": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleJammedWorkerInterval"
          ],
          "operations": [
            {
              "operation": "TRANSACTION",
              "trigger": "Recycle stale GCV2 workers by marking them done and enqueueing a fresh pending task",
              "conditions": [
                "Only for rows whose runner_created is older than maxWorkerLifetime"
              ],
              "implementation": "db.Transaction(func(tx *gorm.DB) error { tx.Model(&gcv2.Task{}).Where(\"runner_created < ?\", ...).Update(\"state\", gcv2.StateDone); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&gcv2.Task{LastGCRunTime: requeueTs.Unix(), ...}) })"
            }
          ]
        }
      },
      "services": []
    },
    "GCV2Worker.UpdateMetrics": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every updateMetricsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Aggregate GCV2 task counts by state for Prometheus gauges",
              "conditions": [
                "Always during metric refresh"
              ],
              "implementation": "db.Model(&gcv2.Task{}).Select(\"state, count(*) as count\").Group(\"state\").Find(&taskCounts)"
            }
          ]
        }
      },
      "services": []
    },
    "BgTaskWorker.PollTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Background-task worker was registered and leader election granted"
          ],
          "operations": [
            {
              "operation": "SELECT/UPDATE",
              "trigger": "Load pending background tasks and either mark them running for autoscale mode or move them to running after job creation",
              "conditions": [
                "Always during pollLoop"
              ],
              "implementation": "db.Model(&bgtask.Task{}).Select(...).Where(\"state = ?\", bgtask.StatePending).Find(&pendingTasks); markTaskRunning(...); db.Model(&bgtask.Task{}).Where(...).Updates(...)"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "Only when the specific task type is not handled solely by shared-worker autoscale mode"
          ],
          "operations": [
            {
              "operation": "LIST",
              "trigger": "Check per-task-type quotas before launching background workers",
              "conditions": [
                "Always before activating jobs"
              ],
              "implementation": "job.CheckJobQuota(... job.TypeBatch ...); job.CheckJobQuota(... job.TypeDDL ...); job.CheckJobQuota(... job.TypeImportInto ...)"
            },
            {
              "operation": "GET/CREATE/DELETE",
              "trigger": "Read worker ConfigMaps, create background worker jobs, and delete orphan jobs that fail to sync into MySQL",
              "conditions": [
                "Only when shared-worker autoscale mode is disabled for the task type"
              ],
              "implementation": "h.jobBuilder.Build(...); h.clusterCli.Create(ctx, jobSpec); h.clusterCli.Delete(ctx, jobSpec, ...)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always while background-task polling is active"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before activating a background worker",
              "conditions": [
                "Always for each pending task candidate"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, task.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "BgTaskWorker.RecycleDoneJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleDoneJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/TRANSACTION",
              "trigger": "Find finished background-task rows, delete them, and archive them into bgtask_task_done",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&bgtask.Task{}).Where(\"state = ?\", bgtask.StateDone).Find(&doneTasks); db.Transaction(func(tx *gorm.DB) error { tx.Delete(doneTask); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&bgtask.TaskDone{...}) })"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "doneTask.Runner is not empty"
          ],
          "operations": [
            {
              "operation": "DELETE",
              "trigger": "Delete finished background worker jobs",
              "conditions": [
                "Always for each unique runner"
              ],
              "implementation": "h.clusterCli.Delete(ctx, &batchv1.Job{ObjectMeta: metav1.ObjectMeta{Name: doneTask.Runner, Namespace: h.namespace}}, ...)"
            }
          ]
        }
      },
      "services": []
    },
    "BgTaskWorker.RecycleOrphanJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleOrphanJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/UPDATE",
              "trigger": "Load running background-task rows and mark them done when their keyspace is no longer enabled",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&bgtask.Task{}).Distinct(...).Where(\"state = ?\", bgtask.StateRunning).Find(&results); db.Model(&bgtask.Task{}).Where(...).Update(\"state\", bgtask.StateDone)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always for each running background task"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before keeping the runner alive",
              "conditions": [
                "Always during orphan background-task scan"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, r.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "BgTaskWorker.AutoScaleSharedWorkers": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "--enable-worker-autoscale=true"
          ],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Measure shared-worker backlog and task-consume speed per keyspace",
              "conditions": [
                "Always during autoScaleLoop"
              ],
              "implementation": "db.Model(&bgtask.Task{}).Where(...).Count(...); recordTaskCount(...); getTaskConsumeSpeed(...)"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "--enable-worker-autoscale=true"
          ],
          "operations": [
            {
              "operation": "LIST_FROM_CACHE",
              "trigger": "Inspect current shared-worker jobs from the informer cache",
              "conditions": [
                "Always during autoScaleLoop"
              ],
              "implementation": "h.getAllJobs(); h.getJobs(keyspace); h.jobLister.List(selector)"
            },
            {
              "operation": "GET/CREATE/DELETE",
              "trigger": "Create or delete shared-worker jobs to match demand",
              "conditions": [
                "Scale out when backlog is high and worker count is below the calculated target",
                "Scale in when shared workers are idle"
              ],
              "implementation": "h.activateJobV2(...); h.clusterCli.Delete(ctx, job, ...)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always for each keyspace considered during auto scale"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Skip shared-worker actions for keyspaces that are no longer enabled",
              "conditions": [
                "Always during autoScaleLoop"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, keyspaceName)"
            }
          ]
        }
      ]
    },
    "BgTaskWorker.UpdateMetrics": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every updateMetricsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Aggregate background-task counts by state and task type for Prometheus gauges",
              "conditions": [
                "Always during metric refresh"
              ],
              "implementation": "db.Model(&bgtask.Task{}).Select(\"state, task_type, count(*) as count\").Group(\"state, task_type\").Find(&taskCounts)"
            }
          ]
        }
      },
      "services": []
    },
    "TTLWorker.PollTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "TTL worker was registered and leader election granted"
          ],
          "operations": [
            {
              "operation": "RAW_SELECT/UPSERT",
              "trigger": "Load enabled TTL keyspaces, select one pending TTL task per keyspace, and mark running TTL tasks after allocation",
              "conditions": [
                "Skips entirely when adminConfig.IsPaused(\"ttl\") is true"
              ],
              "implementation": "getEnableTTLKeyspaceFromTTLInfoTab(...); selectTaskByKeyspace(...); updateRunningTTLJob(...); addPendingTask(...)"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "Either resource-manager mode is disabled or the keyspace is not marked as a test keyspace"
          ],
          "operations": [
            {
              "operation": "GET/CREATE/DELETE",
              "trigger": "Read worker ConfigMaps, create TTL jobs, and delete orphan jobs when DB sync fails",
              "conditions": [
                "Only when the pending TTL task survives keyspace validation and is not routed to resource manager"
              ],
              "implementation": "h.jobBuilder.Build(...); h.clusterCli.Create(ctx, jobSpec); h.clusterCli.Delete(ctx, jobSpec, ...)"
            },
            {
              "operation": "LIST",
              "trigger": "Skip launching duplicate TTL jobs when a worker already exists",
              "conditions": [
                "Always before activating a TTL job"
              ],
              "implementation": "job.CheckJobExists(h.clusterCli, h.namespace, task.KeyspaceName, string(job.TypeTTL))"
            }
          ]
        },
        "internal_scheduler": {
          "conditions": [
            "h.taskRM != nil",
            "h.isTestKeyspace(keyspaceName) is true"
          ],
          "operations": [
            {
              "operation": "ALLOCATE_RESOURCE",
              "trigger": "Run TTL work through the internal resource manager for designated test keyspaces",
              "conditions": [
                "Only for test keyspaces when resource-manager support is enabled for TTL"
              ],
              "implementation": "h.taskRM.AllocateResource(ctx, schedulerutils.MakeTTLRequest(keyspaceName, taskID))"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always while TTL polling is active"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before activating a TTL worker",
              "conditions": [
                "Always for each pending TTL candidate"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, task.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "TTLWorker.RecycleDoneJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleDoneJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/TRANSACTION",
              "trigger": "Delete finished or deleted TTL rows and archive them into ttl_task_done",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&ttl_task.Task{}).Where(\"state IN ?\", ...).Find(&doneOrDeletedTasks); db.Transaction(func(tx *gorm.DB) error { tx.Delete(doneOrDeletedTask); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&ttl_task.TaskDone{...}) })"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "doneOrDeletedTask.Runner is not empty",
            "Either resource-manager mode is disabled or the keyspace is not a resource-manager test keyspace"
          ],
          "operations": [
            {
              "operation": "DELETE",
              "trigger": "Delete finished TTL worker jobs",
              "conditions": [
                "Only for regular Kubernetes Job-backed TTL runners"
              ],
              "implementation": "h.clusterCli.Delete(ctx, &batchv1.Job{ObjectMeta: meta.ObjectMeta{Name: doneOrDeletedTask.Runner, Namespace: h.namespace}}, ...)"
            }
          ]
        },
        "internal_scheduler": {
          "conditions": [
            "h.taskRM != nil",
            "h.isTestKeyspace(doneOrDeletedTask.KeyspaceName) is true",
            "doneOrDeletedTask.Runner != \"\""
          ],
          "operations": [
            {
              "operation": "RECYCLE_RESOURCE",
              "trigger": "Recycle TTL resource-manager workers instead of deleting Kubernetes Jobs",
              "conditions": [
                "Only for RM-backed TTL tasks"
              ],
              "implementation": "h.taskRM.RecycleResource(ctx, doneOrDeletedTask.KeyspaceName, doneOrDeletedTask.Runner)"
            }
          ]
        }
      },
      "services": []
    },
    "TTLWorker.RecycleOrphanJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleOrphanJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/TRANSACTION",
              "trigger": "Delete TTL metadata and mark TTL tasks deleted when their keyspace is no longer valid",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&ttl_task.TTLTableInfo{}).Select(\"keyspace_name\", \"table_id\").Scan(&results); db.Transaction(func(tx *gorm.DB) error { tx.Model(&ttl_task.Task{}).Where(...).Update(\"state\", ttl_task.StateDeleted); tx.Model(&ttl_task.TTLTableInfo{}).Delete(&toDeleteTTLTableInfo) })"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always for each TTL table info row"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still valid before keeping TTL metadata",
              "conditions": [
                "Always during orphan TTL scan"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, r.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "TTLWorker.RecycleJammedWorkers": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleJammedWorkerInterval"
          ],
          "operations": [
            {
              "operation": "TRANSACTION",
              "trigger": "Recycle stale TTL workers by marking them deleted and enqueueing fresh pending TTL work",
              "conditions": [
                "Only for rows whose runner_created is older than maxWorkerLifetime"
              ],
              "implementation": "db.Transaction(func(tx *gorm.DB) error { tx.Model(&ttl_task.Task{}).Where(\"runner_created < ?\", ...).Update(\"state\", ttl_task.StateDeleted); h.addPendingTask(tx, pendingTask) })"
            }
          ]
        }
      },
      "services": []
    },
    "TTLWorker.UpdateMetrics": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every updateMetricsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Aggregate TTL task and TTL table counts for Prometheus gauges",
              "conditions": [
                "Always during metric refresh"
              ],
              "implementation": "db.Model(&ttl_task.Task{}).Select(...).Group(...).Find(...); db.Model(&ttl_task.TTLTableInfo{}).Select(...).Group(...).Find(...)"
            }
          ]
        }
      },
      "services": []
    },
    "AutoAnalyzeWorker.PollTasks": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Auto-analyze worker was registered and leader election granted"
          ],
          "operations": [
            {
              "operation": "SELECT/UPDATE",
              "trigger": "Load pending auto-analyze rows and mark them running after worker creation",
              "conditions": [
                "Skips entirely when adminConfig.IsPaused(\"auto-analyze\") is true"
              ],
              "implementation": "db.Model(&autoanalyze.Task{}).Select(...).Where(\"state = ?\", autoanalyze.StatePending).Find(&pendingTasks); db.Model(&autoanalyze.Task{}).Where(...).Updates(...)"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "Always before and during worker activation"
          ],
          "operations": [
            {
              "operation": "LIST",
              "trigger": "Check the current auto-analyze job quota and skip duplicate workers",
              "conditions": [
                "Always before activating jobs"
              ],
              "implementation": "job.CheckJobQuota(... string(job.TypeAutoAnalyze) ...); job.CheckJobExists(h.clusterCli, h.namespace, task.KeyspaceName, string(job.TypeAutoAnalyze))"
            },
            {
              "operation": "GET/CREATE/DELETE",
              "trigger": "Read worker ConfigMaps, create auto-analyze jobs, and delete orphan jobs when DB sync fails",
              "conditions": [
                "Only when a pending task survives keyspace validation"
              ],
              "implementation": "h.jobBuilder.Build(...); h.clusterCli.Create(ctx, jobSpec); h.clusterCli.Delete(ctx, jobSpec, ...)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always while auto-analyze polling is active"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before activating an auto-analyze worker",
              "conditions": [
                "Always for each pending auto-analyze candidate"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, task.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "AutoAnalyzeWorker.RecycleDoneJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleDoneJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/TRANSACTION",
              "trigger": "Find finished auto-analyze rows, delete them, and archive them into autoanalyze_task_done",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&autoanalyze.Task{}).Where(\"state = ?\", autoanalyze.StateDone).Find(&doneTasks); db.Transaction(func(tx *gorm.DB) error { tx.Delete(doneTask); tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&autoanalyze.TaskDone{...}) })"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "doneTask.Runner is not empty"
          ],
          "operations": [
            {
              "operation": "DELETE",
              "trigger": "Delete finished auto-analyze worker jobs",
              "conditions": [
                "Always for each unique runner"
              ],
              "implementation": "h.clusterCli.Delete(ctx, &batchv1.Job{ObjectMeta: metav1.ObjectMeta{Name: doneTask.Runner, Namespace: h.namespace}}, ...)"
            }
          ]
        }
      },
      "services": []
    },
    "AutoAnalyzeWorker.RecycleOrphanJobs": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleOrphanJobsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT/UPDATE",
              "trigger": "Load running auto-analyze rows and mark them done when their keyspace is no longer enabled",
              "conditions": [
                "Always for each recycle round"
              ],
              "implementation": "db.Model(&autoanalyze.Task{}).Distinct(\"keyspace_name, runner\").Where(\"state = ?\", autoanalyze.StateRunning).Find(&results); db.Model(&autoanalyze.Task{}).Where(...).Update(\"state\", autoanalyze.StateDone)"
            }
          ]
        }
      },
      "services": [
        {
          "name": "pd-server",
          "target_interface": "LoadKeyspace",
          "operation_type": "rpc",
          "conditions": [
            "Always for each running auto-analyze runner"
          ],
          "operations": [
            {
              "operation": "READ",
              "trigger": "Check whether the keyspace is still enabled before keeping the runner alive",
              "conditions": [
                "Always during orphan auto-analyze scan"
              ],
              "implementation": "h.pdCli.LoadKeyspace(ctx, r.KeyspaceName)"
            }
          ]
        }
      ]
    },
    "AutoAnalyzeWorker.RecycleJammedWorkers": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every recycleJammedWorkerInterval"
          ],
          "operations": [
            {
              "operation": "TRANSACTION",
              "trigger": "Recycle stale auto-analyze workers by marking their tasks done",
              "conditions": [
                "Only for rows whose runner_created is older than maxWorkerLifetime"
              ],
              "implementation": "db.Transaction(func(tx *gorm.DB) error { tx.Model(&autoanalyze.Task{}).Where(\"runner_created < ?\", ...).Update(\"state\", autoanalyze.StateDone) })"
            }
          ]
        }
      },
      "services": []
    },
    "AutoAnalyzeWorker.UpdateMetrics": {
      "infrastructure": {
        "mysql": {
          "conditions": [
            "Every updateMetricsInterval"
          ],
          "operations": [
            {
              "operation": "SELECT",
              "trigger": "Aggregate auto-analyze task counts for Prometheus gauges",
              "conditions": [
                "Always during metric refresh"
              ],
              "implementation": "db.Model(&autoanalyze.Task{}).Select(\"state, count(*) as count\").Group(\"state\").Find(&taskCounts)"
            }
          ]
        }
      },
      "services": []
    },
    "ResourceManagerLifecycle.StartResourceScheduler": {
      "infrastructure": {
        "http_server": {
          "conditions": [
            "--enable-resource-manager=true"
          ],
          "operations": [
            {
              "operation": "LISTEN_AND_SERVE",
              "trigger": "Start the resource-manager debug HTTP server on :9292",
              "conditions": [
                "Only when poolManager.Start(ctx) is entered"
              ],
              "implementation": "server.NewServer(m, \":9292\"); s.server.ListenAndServe()"
            }
          ]
        },
        "kubernetes_api": {
          "conditions": [
            "--enable-resource-manager=true"
          ],
          "operations": [
            {
              "operation": "GET",
              "trigger": "Read worker and pool ConfigMaps that define shared-worker pools and scheduler limits",
              "conditions": [
                "Always while the config watcher and scheduler config loop are running"
              ],
              "implementation": "configwatcher.getWorkerImage(...); configwatcher.parsePoolConfig(...); scheduler.updateConfig(...)"
            },
            {
              "operation": "LIST/CREATE/DELETE",
              "trigger": "Reconcile worker Pods and Services so the standby pool matches the current pool configuration",
              "conditions": [
                "Always while the pool reconciler is running"
              ],
              "implementation": "reconciler.reconcilePool(...); clusterCli.List(...); clusterCli.Create(...); clusterCli.DeleteAllOf(...)"
            }
          ]
        },
        "scheduler": {
          "conditions": [
            "--enable-resource-manager=true"
          ],
          "operations": [
            {
              "operation": "START_BACKGROUND_WORKERS",
              "trigger": "Start the resource scheduler, pool manager, node manager, config watcher, standby watcher, reconciler, and scheduler config loop",
              "conditions": [
                "Only when the optional resource-manager path is initialized"
              ],
              "implementation": "scheduler.Start(ctx) -> poolManager.Start(ctx), nodeManager.Start(ctx), updateConfigLoop(ctx)"
            }
          ]
        }
      },
      "services": []
    },
    "ResourceManagerDebug.GetPools": {
      "infrastructure": {},
      "services": []
    },
    "ResourceManagerDebug.GetStandbyWorkers": {
      "infrastructure": {},
      "services": []
    },
    "ResourceManagerDebug.ActivateWorker": {
      "infrastructure": {
        "internal_scheduler": {
          "conditions": [
            "--enable-resource-manager=true",
            "pool and worker query parameters are both present",
            "request body decoded into tidb.ActivateRequest successfully"
          ],
          "operations": [
            {
              "operation": "ACTIVATE_WORKER",
              "trigger": "Manually activate a standby worker through the pool manager",
              "conditions": [
                "Only when the debug request passes validation"
              ],
              "implementation": "s.manager.ActivateWorker(s.ctx, rm.WorkerPool(pool), rm.Worker(worker), activateRequest)"
            }
          ]
        }
      },
      "services": []
    },
    "ResourceManagerDebug.RecycleWorker": {
      "infrastructure": {
        "internal_scheduler": {
          "conditions": [
            "--enable-resource-manager=true",
            "pool and worker query parameters are both present"
          ],
          "operations": [
            {
              "operation": "RECYCLE_WORKER",
              "trigger": "Manually recycle a worker back into standby state",
              "conditions": [
                "Only when the debug request passes validation"
              ],
              "implementation": "s.manager.RecycleWorker(s.ctx, rm.WorkerPool(pool), rm.Worker(worker), keyspace)"
            }
          ]
        }
      },
      "services": []
    }
  }
}
