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;

  // 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;

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

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;
}

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 {}
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;
}

/**
 * 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);
}

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;
}
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);
}
