syntax = "proto3";

option go_package = "pkg/tidbworker/proto/scaler";

service Scaler {
  // Ping checks if the manager is operating normally.
  rpc Ping(PingRequest) returns (Response) {}

  // RegisterGC notifies the manager that a GC task has been registered at deletionTs.
  rpc RegisterGC(RegisterGCRequest) returns (Response) {}
  // RecycleGC notifies the manager that all GC tasks at or before safePoint have been completed.
  rpc RecycleGC(RecycleGCRequest) returns (Response) {}

  // RegisterGCV2 notifies the manager that a round of GC at safePoint has just been completed.
  // If no more heartbeats are received, a new round of GC should be scheduled after gcLifeTime.
  rpc RegisterGCV2(RegisterGCV2Request) returns (Response) {}
  // RecycleGCV2 notifies the manager that all GC tasks at or before safePoint have been completed.
  rpc RecycleGCV2(RecycleGCV2Request) returns (Response) {}
  // UpdateGCLifeTime notifies the manager that the user's GCLifeTime setting has been updated.
  rpc UpdateGCLifeTime(UpdateGCLifeTimeRequest) returns (Response) {}

  // GetBgTaskConfig retrieves the configuration of the background task.
  rpc GetBgTaskConfig(GetBgTaskConfigRequest) returns (GetBgTaskConfigResponse) {}
  // RegisterBgTask notifies the manager that a background sub-task has been created.
  rpc RegisterBgTask(RegisterBgTaskRequest) returns (Response) {}
  // RecycleBgTask notifies the manager that a background global task has been completed.
  rpc RecycleBgTask(RecycleBgTaskRequest) returns (Response) {}
  // UpdateBgTaskExecID notifies the manager that the execID of a background task has been updated.
  rpc UpdateBgTaskExecID(UpdateBgTaskExecIDRequest) returns (Response) {}

  // RegisterRemoteQuery notifies the manager that a query has been set to execute remotely.
  rpc RegisterRemoteQuery(RegisterRemoteQueryRequest) returns (Response) {}

  // RegisterTTLTask notifies the manager that a table with TTL information has been created.
  rpc RegisterTTLTask(RegisterTTLTaskRequest) returns (Response) {}
  // DeleteTTLTableInfo notifies the manager that a table no longer has TTL settings or has been dropped.
  rpc DeleteTTLTableInfo(DeleteTTLTableInfoRequest) returns (Response) {}
  // RecycleTTLTask notifies the manager that a TTL task has been completed.
  rpc RecycleTTLTask(RecycleTTLTaskRequest) returns (Response) {}
  // UpdateTTLJobEnable notifies the manager that the user's 'tidb_ttl_job_enable' variable has been updated.
  rpc UpdateTTLJobEnable(UpdateTTLJobEnableRequest) returns (Response) {}

  // RegisterAutoAnalyze notifies the manager that an auto-analyze task has been registered.
  rpc RegisterAutoAnalyze(RegisterAutoAnalyzeRequest) returns (Response) {}
  // RecycleAutoAnalyze notifies the manager that an auto-analyze task has been completed.
  rpc RecycleAutoAnalyze(RecycleAutoAnalyzeRequest) returns (Response) {}
}

message PingRequest {}

message Response {
  Error error = 1;
}

message Error {
  ErrorType type = 1;
  string message = 2;
}

enum ErrorType {
  OK = 0;
  UNKNOWN = 1;
  PAUSED = 2;
}

message RequestHeader {
  // keyspace_id is the ID of the requesting TiDB's keyspace.
  uint32 keyspace_id = 1;
  // keyspace_name is the name of the requesting TiDB's keyspace.
  string keyspace_name = 2;
  // tidb_pool is used to record the pool of the requesting TiDB.
  // This is used to pull the corresponding TiDB image and config.
  string tidb_pool = 3;
}

message RegisterGCRequest {
  RequestHeader header = 1;
  // deletion_ts is the timestamp when a new record was inserted into the gc delete range table.
  uint64 deletion_ts = 2;
}

message RecycleGCRequest {
  RequestHeader header = 1;
  // safe_point is the timestamp before which all entries in the gc delete range table have been handled.
  uint64 safe_point = 2;
}

message RegisterGCV2Request {
  RequestHeader header = 1;
  // safe_point is the timestamp of the most recent GC.
  uint64 safe_point = 2;
  // gc_life_time is the GC life time of the requesting TiDB.
  int64 gc_life_time = 3;
}

message RecycleGCV2Request {
  RequestHeader header = 1;
  // safe_point is the timestamp of the most recent GC.
  uint64 safe_point = 2;
}

message UpdateGCLifeTimeRequest {
  RequestHeader header = 1;
  // gc_life_time is the newly set gc_life_time of the user.
  int64 gc_life_time = 2;
}

message GetBgTaskConfigRequest {
  RequestHeader header = 1;
  // worker_type is the type of worker being checked.
  string worker_type = 2;
}

message GetBgTaskConfigResponse {
  Error error = 1;
  // worker_count is the number of workers that the scaler supports for the given task type.
  uint32 worker_count = 2;
  // auto_scale_enabled indicates whether the scaler has auto-scale enabled.
  bool auto_scale_enabled = 3;
}

message RegisterBgTaskRequest {
  RequestHeader header = 1;
  // task_type indicates the type of background worker.
  string task_type = 2;
  // task_key is the key of the distributed task job.
  string task_key = 3;
  // global_task_id is the ID of the task in mysql.tidb_global_task.
  int64 global_task_id = 4;
  // sub_task_id is the ID of the task in mysql.tidb_background_subtask.
  int64 sub_task_id = 5;
  // exec_id is the logical ID for the TiDB that was assigned the task.
  string exec_id = 6;
}

message RecycleBgTaskRequest {
  RequestHeader header = 1;
  // global_task_id is the ID of the task in mysql.tidb_global_task.
  int64 global_task_id = 4;
  // sub_task_id is the ID of the task in mysql.tidb_background_subtask.
  int64 sub_task_id = 5;
}

message UpdateBgTaskExecIDRequest {
  RequestHeader header = 1;
  // global_task_id is the ID of the task in mysql.tidb_global_task.
  int64 global_task_id = 2;
  // sub_task_id is the IDs of the task in mysql.tidb_background_subtask.
  repeated int64 sub_task_ids = 3;
  // exec_id is the logical IDs for the TiDB that was assigned the task.
  repeated string exec_ids = 4;
}

message RegisterRemoteQueryRequest {
  RequestHeader header = 1;
  // query_id is the ID of the query.
  string query_id = 2;
  // query_address is the address of the TiDB executing the query.
  string query_address = 3;
}

message RegisterTTLTaskRequest {
  RequestHeader header = 1;
  // table_id is the ID of the table that has TTL enabled.
  int64 table_id = 2;
  // ttl_job_enable is the current value of the user's 'tidb_ttl_job_enable' variable.
  bool ttl_job_enable = 3;
}

message DeleteTTLTableInfoRequest {
  RequestHeader header = 1;
  // table_id is the ID of the table that has had its TTL attribute removed.
  int64 table_id = 2;
}

message RecycleTTLTaskRequest {
  RequestHeader header = 1;
  // finish_time is the creation time of the last successful ttl job execution,
  // before which all data that expired due to TTL have been handled.
  uint64 finish_time = 2;
}

message UpdateTTLJobEnableRequest {
  RequestHeader header = 1;
  // ttl_job_enable is the new value of the user's 'tidb_ttl_job_enable' variable.
  bool ttl_job_enable = 2;
}

message RegisterAutoAnalyzeRequest {
  RequestHeader header = 1;
  // task_id is the ID of the auto-analyze task that has been registered.
  uint64 task_id = 2;
}

message RecycleAutoAnalyzeRequest {
  RequestHeader header = 1;
  // task_id is the ID of the auto-analyze task that has been completed.
  uint64 task_id = 2;
}
