syntax = "proto3";

option java_package = "com.anaconda.skein";
option java_outer_classname = "Msg";


package skein;


// Common definitions


message Empty {
}


message FinalStatus {
  enum Type {
    UNDEFINED = 0;
    SUCCEEDED = 1;
    FAILED = 2;
    KILLED = 3;
  }
}

message ApplicationState {
  enum Type {
    NEW = 0;
    NEW_SAVING = 1;
    SUBMITTED = 2;
    ACCEPTED = 3;
    RUNNING = 4;
    FINISHED = 5;
    FAILED = 6;
    KILLED = 7;
  }
}


message Resources {
  int32 memory = 1;
  int32 vcores = 2;
}


message Url {
  string scheme = 1;
  string host = 2;
  int32 port = 3;
  string file = 4;
}


message File {
  enum Type {
    FILE = 0;
    ARCHIVE = 1;
  }

  enum Visibility {
    PUBLIC = 0;
    PRIVATE = 1;
    APPLICATION = 2;
  }

  Url source = 1;
  Type type = 2;
  Visibility visibility = 3;
  int64 size = 4;
  int64 timestamp = 5;
}


message Service {
  int32 instances = 1;
  int32 max_restarts = 2;
  Resources resources = 3;
  map<string, File> files = 4;
  map<string, string> env = 5;
  repeated string commands = 6;
  repeated string depends = 7;
}


message ApplicationSpec {
  string name = 1;
  string queue = 2;
  int32 max_attempts = 3;
  repeated string tags = 4;
  map<string, Service> services = 5;
}


message Container {
  enum State {
    WAITING = 0;
    REQUESTED = 1;
    RUNNING = 2;
    SUCCEEDED = 3;
    FAILED = 4;
    KILLED = 5;
  }
  string service_name = 1;
  int32 instance = 2;
  State state = 3;
  string yarn_container_id = 4;
  int64 start_time = 5;
  int64 finish_time = 6;
}


message ResourceUsageReport {
  int64 memory_seconds = 1;
  int64 vcore_seconds = 2;
  int64 num_used_containers = 3;
  Resources needed_resources = 4;
  Resources reserved_resources = 5;
  Resources used_resources = 6;
}


message ApplicationReport {
  string id = 1;
  string name = 2;
  string user = 3;
  string queue = 4;
  repeated string tags = 5;
  string host = 6;
  int32 port = 7;
  string tracking_url = 8;
  ApplicationState.Type state = 9;
  FinalStatus.Type final_status = 10;
  float progress = 11;
  ResourceUsageReport usage = 12;
  string diagnostics = 13;
  int64 start_time = 14;
  int64 finish_time = 15;
}


// Daemon only definitions


service Daemon {
  rpc ping (Empty) returns (Empty);

  rpc getStatus (Application) returns (ApplicationReport);

  rpc getApplications (ApplicationsRequest) returns (ApplicationsResponse);

  rpc submit (ApplicationSpec) returns (Application);

  rpc waitForStart (Application) returns (ApplicationReport);

  rpc kill (Application) returns (Empty);
}


message Application {
  string id = 1;
}


message ApplicationsRequest {
  repeated ApplicationState.Type states = 1;
}


message ApplicationsResponse {
  repeated ApplicationReport reports = 1;
}


// Master only definitions


service Master {
  rpc shutdown (ShutdownRequest) returns (Empty);

  rpc keyvalueGetKey (GetKeyRequest) returns (GetKeyResponse);

  rpc keyvalueSetKey (SetKeyRequest) returns (Empty);

  rpc keyvalueDelKey (DelKeyRequest) returns (Empty);

  rpc keyvalueGetAll (Empty) returns (KeyValueResponse);

  rpc getService (ServiceRequest) returns (Service);

  rpc getApplicationSpec (Empty) returns (ApplicationSpec);

  rpc getContainers (ContainersRequest) returns (ContainersResponse);

  rpc killContainer (ContainerInstance) returns (Empty);

  rpc scale (ScaleRequest) returns (ContainersResponse);
}


message ShutdownRequest {
  FinalStatus.Type final_status = 1;
}


message GetKeyRequest {
  string key = 1;
  bool wait = 2;
}


message GetKeyResponse {
  string val = 1;
}


message SetKeyRequest {
  string key = 1;
  string val = 2;
}

message DelKeyRequest {
  string key = 1;
}

message KeyValueResponse {
  map<string, string> items = 1;
}


message ServiceRequest {
  string name = 1;
}


message ContainerInstance {
  string service_name = 1;
  int32 instance = 2;
}


message ContainersRequest {
  repeated Container.State states = 1;
  repeated string services = 2;
}


message ContainersResponse {
  repeated Container containers = 1;
}


message ScaleRequest {
  string service_name = 1;
  int32 instances = 2;
}
