syntax = "proto2";

option java_multiple_files = true;
option java_package = "alluxio.grpc";
option java_outer_classname = "JobMasterProto";

package alluxio.grpc.job;

import "grpc/common.proto";

enum Status {
  UNKNOWN = 0;
  CREATED = 1;
  CANCELED = 2;
  FAILED = 3;
  RUNNING = 4;
  COMPLETED = 5;
}

enum JobType {
  PLAN = 1;
  TASK = 2;
  WORKFLOW = 3;
}

message JobUnused {}

// next available id: 14
message JobInfo {
  optional int64 id = 1;
  optional string errorMessage = 2;
  optional Status status = 4;
  optional int64 lastUpdated = 6;
  optional JobType type = 8;
  optional bytes result = 9;
  optional string errorType = 15;

  // Some jobs don't have these
  optional string name = 7;
  optional int64 parentId = 10;
  repeated JobInfo children = 11;
  optional string workerHost = 12;
  optional string description = 13;
  repeated string affectedPaths = 14;

  // Around for backwards compatibility
  repeated JobUnused unused0 = 3; // formerly taskInfos
  optional string unused1 = 5; // formerly result
}

enum JobSource {
  UNSUPPORTED = 0;
  CLI = 1;
  POLICY = 2;
  SYSTEM = 3;
}

enum OperationType {
  DIST_LOAD = 1;
  DIST_CP = 2;
  PERSIST = 3;
}

message JobProgress {
  optional int64 jobId = 1;
  optional int64 fileCount = 2;
  optional int64 fileSize = 3;
  optional int64 lastUpdateMs = 4;
  optional string errorMsg = 5;
  optional string errorType = 6;
  optional Status status = 7;
}

message CmdProgress {
  repeated JobProgress jobProgress = 1;
}

message CmdSummary {
  optional int64 jobControlId = 1;
  optional OperationType operationType = 2;
  optional JobSource jobSource = 3;
  optional Status status = 4;
  optional int64 submissionTime = 5;
  optional int64 duration = 6;
  optional string errorMessage = 7;
  optional CmdProgress cmdProgress = 8;
  optional float progressPercentage = 9;
}

message StatusSummary {
  optional Status status = 1;
  optional int64 count = 2;
}

message JobServiceSummary {
  repeated StatusSummary summaryPerStatus = 1;
  repeated JobInfo recentActivities = 2;
  repeated JobInfo recentFailures = 3;
  repeated JobInfo longestRunning = 4;
}

message JobWorkerHealth {
  optional int64 workerId = 1;
  repeated double loadAverage = 2;
  optional int64 lastUpdated = 3;
  optional string hostname = 4;
  optional int32 taskPoolSize = 5;
  optional int32 numActiveTasks = 6;
  optional int32 unfinishedTasks = 7;
  optional grpc.BuildVersion version = 8;
}

message JobCommand {
  optional RunTaskCommand runTaskCommand = 1;
  optional CancelTaskCommand cancelTaskCommand = 2;
  optional RegisterCommand registerCommand = 3;
  optional SetTaskPoolSizeCommand setTaskPoolSizeCommand = 4;
}

message RunTaskCommand {
  optional int64 jobId = 1;
  optional int64 taskId = 2;
  optional bytes jobConfig = 3;
  optional bytes taskArgs = 4;
}

message RegisterCommand {}

message SetTaskPoolSizeCommand {
  optional int32 taskPoolSize = 1;
}

message CancelTaskCommand {
  optional int64 jobId = 1;
  optional int64 taskId = 2;
}

message CancelPOptions {}
message CancelPRequest {
  optional int64 jobId = 1;
  optional CancelPOptions options = 2;
}
message CancelPResponse {}

message GetJobStatusPOptions {}
message GetJobStatusPRequest {
  optional int64 jobId = 1;
  optional GetJobStatusPOptions options = 2;
}
message GetJobStatusPResponse {
  optional JobInfo jobInfo = 1;
}

message GetJobStatusDetailedPOptions {}
message GetJobStatusDetailedPRequest {
  optional int64 jobId = 1;
  optional GetJobStatusDetailedPOptions options = 2;
}
message GetJobStatusDetailedPResponse {
  optional JobInfo jobInfo = 1;
}

message ListAllPOptions {
  repeated Status status = 1;
  optional string name = 2;
  optional bool jobIdOnly = 3;
}
message ListAllPRequest {
  optional ListAllPOptions options = 1;
}
message ListAllPResponse {
  repeated int64 jobIds = 1;
  repeated JobInfo jobInfos = 2;
}

message RunPOptions {}
message RunPRequest {
  optional bytes jobConfig = 1;
  optional RunPOptions options = 2;
}
message RunPResponse {
  optional int64 jobId = 1;
}

message GetJobServiceSummaryPOptions {}
message GetJobServiceSummaryPRequest {
  optional GetJobServiceSummaryPOptions options = 1;
}
message GetJobServiceSummaryPResponse {
  optional JobServiceSummary summary = 1;
}

message GetAllWorkerHealthPOptions {}
message GetAllWorkerHealthPRequest {
  optional GetAllWorkerHealthPOptions options = 1;
}
message GetAllWorkerHealthPResponse {
  repeated JobWorkerHealth workerHealths = 1;
}

message JobMasterStatus {
  optional string state = 1;
  optional grpc.NetAddress masterAddress = 2;
  optional int64 startTime = 3;
  optional grpc.BuildVersion version = 4;
}

message GetAllMasterStatusPOptions {}
message GetAllMasterStatusPRequest {
  optional GetAllMasterStatusPOptions options = 1;
}
message GetAllMasterStatusPResponse {
  repeated JobMasterStatus jobMasterStatus = 1;
}

message SubmitOptions {}
message SubmitRequest {
  optional bytes cmdConfig = 1;
  optional SubmitOptions options = 2;
}
message SubmitResponse {
  optional int64 jobControlId = 1;
}

message GetCmdStatusOptions {}
message GetCmdStatusRequest {
  optional int64 jobControlId = 1;
  optional GetCmdStatusOptions options = 2;
}
message GetCmdStatusResponse {
  optional Status cmdStatus = 1;
}

message GetCmdStatusDetailedOptions {}
message GetCmdStatusDetailedRequest {
  optional int64 jobControlId = 1;
  optional GetCmdStatusOptions options = 2;
}
message JobStatusBlock {
  optional int64 jobId = 1;
  optional Status jobStatus = 2;
  optional string filePath = 3;
  optional string filePathFailed = 4;
}
message CmdStatusBlock {
  optional int64 jobControlId = 1;
  repeated JobStatusBlock jobStatusBlock = 2;
  optional OperationType operationType = 3;
}
message GetCmdStatusDetailedResponse {
  optional CmdStatusBlock cmdStatusBlock = 1;
}

/**
 * This interface contains job master service endpoints for job service clients.
 */
service JobMasterClientService {

  /**
   * Cancels the given job.
   */
  rpc Cancel(CancelPRequest) returns (CancelPResponse);

  /**
   * Gets the status of the given job.
   */
  rpc GetJobStatus(GetJobStatusPRequest) returns (GetJobStatusPResponse);

  /**
   * Gets the status of the given job.
   */
  rpc GetJobStatusDetailed(GetJobStatusDetailedPRequest) returns (GetJobStatusDetailedPResponse);

  /**
   * Gets the summary of the job service.
   */
  rpc GetJobServiceSummary(GetJobServiceSummaryPRequest) returns (GetJobServiceSummaryPResponse);

  /**
   * Lists ids of all known jobs.
   */
  rpc ListAll(ListAllPRequest) returns (ListAllPResponse);

  /**
   * Starts the given job, returning a job id.
   */
  rpc Run(RunPRequest) returns (RunPResponse);

  /**
   * Lists all worker health.
   */
  rpc GetAllWorkerHealth(GetAllWorkerHealthPRequest) returns (GetAllWorkerHealthPResponse);

  /**
   * Lists all job master status.
   */
  rpc GetAllMasterStatus(GetAllMasterStatusPRequest) returns (GetAllMasterStatusPResponse);

  /**
   * Submit a CMD job, return a jobControlId.
   */
  rpc Submit(SubmitRequest) returns (SubmitResponse);

  /**
 * Get status for a CMD job, return a status.
 */
  rpc GetCmdStatus(GetCmdStatusRequest) returns (GetCmdStatusResponse);

  /**
* Get detailed status for a CMD job, return detailed status information.
*/
  rpc GetCmdStatusDetailed(GetCmdStatusDetailedRequest) returns (GetCmdStatusDetailedResponse);
}

message JobHeartbeatPOptions {}
message JobHeartbeatPRequest {
  optional JobWorkerHealth jobWorkerHealth = 1;
  repeated JobInfo taskInfos = 2;
  optional JobHeartbeatPOptions options = 3;
}
message JobHeartbeatPResponse {
  repeated JobCommand commands = 1;
}

message RegisterJobWorkerPOptions {}
message RegisterJobWorkerPRequest {
  optional grpc.WorkerNetAddress workerNetAddress = 1;
  optional RegisterJobWorkerPOptions options = 2;
  optional grpc.BuildVersion version = 3;
}
message RegisterJobWorkerPResponse {
 optional int64 id = 1;
}

/**
 * This interface contains job master service endpoints for job service workers.
 */
service JobMasterWorkerService {

  /**
   * Periodic worker heartbeat returns a list of commands for the worker to execute.
   */
  rpc Heartbeat(JobHeartbeatPRequest) returns (JobHeartbeatPResponse);

  /**
   * Returns a worker id for the given network address.
   */
  rpc RegisterJobWorker(RegisterJobWorkerPRequest) returns (RegisterJobWorkerPResponse);
}

message GetJobMasterIdPOptions {}
message GetJobMasterIdPRequest {
  optional grpc.NetAddress masterAddress = 1;
  optional GetJobMasterIdPOptions options = 2;
}
message GetJobMasterIdPResponse {
  optional int64 masterId = 1;
}

enum JobMasterMetaCommand {
  MetaCommand_Unknown = 0;
  MetaCommand_Nothing = 1;
  MetaCommand_Register = 2; // Ask the standby master to re-register.
}

message RegisterJobMasterPOptions {
  optional int64 startTimeMs = 2;
  optional int64 losePrimacyTimeMs = 3;
  optional grpc.BuildVersion version = 4;
}

message RegisterJobMasterPRequest {
  optional int64 jobMasterId = 1;
  optional RegisterJobMasterPOptions options = 2;
}
message RegisterJobMasterPResponse {}

message JobMasterHeartbeatPOptions {
}
message JobMasterHeartbeatPRequest {
  optional int64 masterId = 1;
  optional JobMasterHeartbeatPOptions options = 2;
}
message JobMasterHeartbeatPResponse {
  optional JobMasterMetaCommand command = 1;
}

/**
  * This interface contains meta master service endpoints for Alluxio standby masters.
  */
service JobMasterMasterService {

  /**
   * Returns a master id for the given master address.
   */
  rpc GetMasterId(GetJobMasterIdPRequest) returns (GetJobMasterIdPResponse);

  /**
   * Registers a master.
   */
  rpc RegisterMaster(RegisterJobMasterPRequest) returns (RegisterJobMasterPResponse);

  /**
   * Heartbeats to indicate the master is lost or not.
   */
  rpc MasterHeartbeat(JobMasterHeartbeatPRequest) returns (JobMasterHeartbeatPResponse);
}

