syntax = "proto3";

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


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;
  int64 gpus = 3;
  int64 fpgas = 4;
}


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;
  string node_label = 2;
  repeated string nodes = 3;
  repeated string racks = 4;
  bool relax_locality = 5;
  int32 max_restarts = 6;
  bool allow_failures = 7;
  Resources resources = 8;
  map<string, File> files = 9;
  map<string, string> env = 10;
  string script = 11;
  repeated string depends = 12;
}


message Acls {
  bool enable = 1;
  repeated string view_users = 2;
  repeated string view_groups = 3;
  repeated string modify_users = 4;
  repeated string modify_groups = 5;
  repeated string ui_users = 6;
}


message Log {
  enum Level {
    INFO = 0;
    ALL = 1;
    TRACE = 2;
    DEBUG = 3;
    WARN = 4;
    ERROR = 5;
    FATAL = 6;
    OFF = 7;
  }
}


message Security {
  oneof cert {
    File cert_file = 1;
    bytes cert_bytes = 2;
  }
  oneof key {
    File key_file = 3;
    bytes key_bytes = 4;
  }
}


message Master {
  // Application-master configuration
  File log_config = 1;
  Log.Level log_level = 2;
  Security security = 3;

  // Master service configuration parameters
  Resources resources = 4;
  map<string, File> files = 5;
  map<string, string> env = 6;
  string script = 7;
}


message ApplicationSpec {
  string name = 1;
  string queue = 2;
  string user = 3;
  string node_label = 4;
  int32 max_attempts = 5;
  repeated string tags = 6;
  repeated string file_systems = 7;
  Acls acls = 8;
  Master master = 9;
  map<string, Service> services = 10;
}


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;
  string yarn_node_http_address = 5;
  int64 start_time = 6;
  int64 finish_time = 7;
  string exit_message = 8;
}


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


message NodeState {
  enum Type {
    DECOMMISSIONED = 0;
    DECOMMISSIONING = 1;
    LOST = 2;
    NEW = 3;
    REBOOTED = 4;
    RUNNING = 5;
    SHUTDOWN = 6;
    UNHEALTHY = 7;
  }
}


message NodeReport {
  string id = 1;
  string http_address = 2;
  string rack_name = 3;
  repeated string labels = 4;
  NodeState.Type state = 5;
  string health_report = 6;
  Resources total_resources = 7;
  Resources used_resources = 8;
}


message Queue {
  enum State {
    RUNNING = 0;
    STOPPED = 1;
  }
  string name = 1;
  State state = 2;
  float capacity = 3;
  float max_capacity = 4;
  float percent_used = 5;
  repeated string node_labels = 6;
  string default_node_label = 7;
}

// Driver only definitions


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

  rpc getStatus (Application) returns (ApplicationReport);

  rpc getApplications (ApplicationsRequest) returns (ApplicationsResponse);

  rpc getNodes (NodesRequest) returns (NodesResponse);

  rpc getQueue (QueueRequest) returns (Queue);

  rpc getChildQueues (QueueRequest) returns (QueuesResponse);

  rpc getAllQueues (Empty) returns (QueuesResponse);

  rpc moveApplication (MoveRequest) returns (Empty);

  rpc submit (ApplicationSpec) returns (Application);

  rpc waitForStart (Application) returns (ApplicationReport);

  rpc kill (KillRequest) returns (Empty);

  rpc getLogs (LogsRequest) returns (LogsResponse);
}


message Application {
  string id = 1;
}


message ApplicationsRequest {
  repeated ApplicationState.Type states = 1;
  string name = 2;
  string user = 3;
  string queue = 4;
  int64 started_begin = 5;
  int64 started_end = 6;
  int64 finished_begin = 7;
  int64 finished_end = 8;
}


message ApplicationsResponse {
  repeated ApplicationReport reports = 1;
}


message NodesRequest {
  repeated NodeState.Type states = 1;
}


message NodesResponse {
  repeated NodeReport reports = 1;
}


message QueueRequest {
  string name = 1;
}


message QueuesResponse {
  repeated Queue queues = 1;
}


message LogsRequest {
  string id = 1;
  string user = 2;
}


message LogsResponse {
  map<string, string> logs = 2;
}


message MoveRequest {
  string id = 1;
  string queue = 2;
}


message KillRequest {
  string id = 1;
  string user = 2;
}


// Master only definitions


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

  rpc GetRange (GetRangeRequest) returns (GetRangeResponse);

  rpc PutKey (PutKeyRequest) returns (PutKeyResponse);

  rpc DeleteRange (DeleteRangeRequest) returns (DeleteRangeResponse);

  rpc Transaction (TransactionRequest) returns (TransactionResponse);

  rpc Watch (stream WatchRequest) returns (stream WatchResponse);

  rpc getApplicationSpec (Empty) returns (ApplicationSpec);

  rpc getContainers (ContainersRequest) returns (ContainersResponse);

  rpc killContainer (ContainerInstance) returns (Empty);

  rpc scale (ScaleRequest) returns (ContainersResponse);

  rpc addContainer (AddContainerRequest) returns (Container);

  rpc SetProgress (SetProgressRequest) returns (Empty);

  rpc AddProxy (Proxy) returns (Empty);

  rpc RemoveProxy (RemoveProxyRequest) returns (Empty);

  rpc UiInfo (UIInfoRequest) returns (UIInfoResponse);

  rpc GetProxies (GetProxiesRequest) returns (GetProxiesResponse);
}


message ShutdownRequest {
  FinalStatus.Type final_status = 1;
  string diagnostics = 2;
}


message KeyValue {
  string key = 1;
  bytes value = 2;
  ContainerInstance owner = 3;
}


message GetRangeRequest {
  enum ResultType {
    ITEMS = 0;
    KEYS = 1;
    NONE = 2;
  }
  string start = 1;
  string end = 2;
  ResultType result_type = 3;
}


message GetRangeResponse {
  int32 count = 1;
  GetRangeRequest.ResultType result_type = 2;
  repeated KeyValue result = 3;
}


message PutKeyRequest {
  string key = 1;
  bytes value = 2;
  ContainerInstance owner = 3;
  bool ignore_value = 4;
  bool ignore_owner = 5;
  bool return_previous = 6;
}


message PutKeyResponse {
  bool return_previous = 1;
  KeyValue previous = 2;
}


message DeleteRangeRequest {
  enum ResultType {
    NONE = 0;
    ITEMS = 1;
    KEYS = 2;
  }
  string start = 1;
  string end = 2;
  ResultType result_type = 3;
}


message DeleteRangeResponse {
  int32 count = 1;
  DeleteRangeRequest.ResultType result_type = 2;
  repeated KeyValue result = 3;
}


message Condition {
  enum Operator {
    EQUAL = 0;
    NOT_EQUAL = 1;
    LESS = 2;
    LESS_EQUAL = 3;
    GREATER = 4;
    GREATER_EQUAL = 5;
  }
  enum Field {
    VALUE = 0;
    OWNER = 1;
  }
  string key = 1;
  Operator operator = 2;
  Field field = 3;
  oneof rhs {
    bytes value = 4;
    ContainerInstance owner = 5;
  }
}


message OpRequest {
  oneof request {
    GetRangeRequest get_range = 1;
    PutKeyRequest put_key = 2;
    DeleteRangeRequest delete_range = 3;
  }
}


message OpResponse {
  oneof response {
    GetRangeResponse get_range = 1;
    PutKeyResponse put_key = 2;
    DeleteRangeResponse delete_range = 3;
  }
}


message TransactionRequest {
  repeated Condition condition = 1;
  repeated OpRequest on_success = 2;
  repeated OpRequest on_failure = 3;
}


message TransactionResponse {
  bool succeeded = 1;
  repeated OpResponse result = 2;
}


message WatchCreateRequest {
  enum Type {
    ALL = 0;
    PUT = 1;
    DELETE = 2;
  }
  string start = 1;
  string end = 2;
  Type event_type = 3;
}


message WatchCancelRequest {
  int32 watch_id = 1;
}


message WatchRequest {
  oneof request {
    WatchCreateRequest create = 1;
    WatchCancelRequest cancel = 2;
  }
}


message WatchResponse {
  enum Type {
    CREATE = 0;
    CANCEL = 1;
    PUT = 2;
    DELETE = 3;
  }
  Type type = 1;
  int32 watch_id = 2;
  repeated KeyValue event = 3;
}


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 count = 2;
  int32 delta = 3;
}


message AddContainerRequest {
  string service_name = 1;
  map<string, string> env = 2;
}


message SetProgressRequest {
  float progress = 1;
}


message Proxy {
  string route = 1;
  string link_name = 2;
  string target = 3;
}


message RemoveProxyRequest {
  string route = 1;
}


message UIInfoRequest {
}


message UIInfoResponse {
  repeated string ui_address = 1;
  string proxy_prefix = 2;
}


message GetProxiesRequest {
}


message GetProxiesResponse {
  repeated Proxy proxy = 1;
}
