syntax = "proto2";

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

package alluxio.grpc.block;

import "grpc/common.proto";

enum BlockMasterInfoField {
  CAPACITY_BYTES = 1;
  CAPACITY_BYTES_ON_TIERS = 2;
  FREE_BYTES = 3;
  LIVE_WORKER_NUM = 4;
  LOST_WORKER_NUM = 5;
  USED_BYTES = 6;
  USED_BYTES_ON_TIERS = 7;
  DECOMMISSIONED_WORKER_NUM = 8;
}

message BlockMasterInfo {
  optional int64 capacityBytes = 1;
  map<string, int64> capacityBytesOnTiers = 2;
  optional int64 freeBytes = 3;
  optional int32 liveWorkerNum = 4;
  optional int32 lostWorkerNum = 5;
  optional int64 usedBytes = 6;
  map<string, int64> usedBytesOnTiers = 7;
  optional int32 decommissionedWorkerNum = 8;
}

message GetBlockInfoPOptions {}
message GetBlockInfoPRequest {
  /** the id of the block */
  optional int64 blockId = 1;

  optional GetBlockInfoPOptions options = 2;
}
message GetBlockInfoPResponse {
  optional grpc.BlockInfo blockInfo = 1;
}

message GetCapacityBytesPOptions {}
message GetCapacityBytesPResponse {
  optional int64 bytes = 1;
}

message GetBlockMasterInfoPOptions {
 repeated BlockMasterInfoField filters = 1;
}
message GetBlockMasterInfoPResponse {
 optional BlockMasterInfo blockMasterInfo = 1;
}

message GetUsedBytesPOptions {}
message GetUsedBytesPResponse {
  optional int64 bytes = 1;
}

message WorkerInfo {
  optional int64 id = 1 [deprecated = true]; // deprecated in favor of WorkerIdentity
  optional grpc.WorkerNetAddress address = 2;
  optional int32 lastContactSec = 3;
  optional string state = 4;
  optional int64 capacityBytes = 5;
  optional int64 usedBytes = 6;
  optional int64 startTimeMs = 7;
  map<string, int64> capacityBytesOnTiers = 8;
  map<string, int64> usedBytesOnTiers = 9;
  optional BuildVersion buildVersion = 10;
  optional WorkerIdentity identity = 11;
}

enum WorkerRange {
  ALL = 1;
  LIVE = 2;
  LOST = 3;
  SPECIFIED = 4;
  DECOMMISSIONED = 5;
}

enum WorkerInfoField {
  ADDRESS = 1;
  WORKER_CAPACITY_BYTES = 2;
  WORKER_CAPACITY_BYTES_ON_TIERS = 3;
  ID = 4;
  LAST_CONTACT_SEC = 5;
  START_TIME_MS = 6;
  STATE = 7;
  WORKER_USED_BYTES = 8;
  WORKER_USED_BYTES_ON_TIERS = 9;
  BLOCK_COUNT = 10;
  BUILD_VERSION = 11;
}

message GetWorkerReportPOptions {
  /** addresses are only valid when workerRange is SPECIFIED */
  repeated string addresses = 1;
  repeated WorkerInfoField fieldRanges = 2;
  optional WorkerRange workerRange = 3;
}
message GetWorkerInfoListPOptions {}
message GetWorkerInfoListPResponse {
  repeated WorkerInfo workerInfos = 1;
}

message WorkerLostStorageInfo {
  optional grpc.WorkerNetAddress address = 1;
  /** a map from tier alias to the lost storage paths */
  map<string, StorageList> lostStorage = 2;
}
message RemoveDisabledWorkerPOptions {
  required string workerHostname = 1;
  optional int64 workerWebPort = 2;
}
message RemoveDisabledWorkerPResponse {}

message GetWorkerLostStoragePOptions {}
message GetWorkerLostStoragePResponse {
  repeated WorkerLostStorageInfo workerLostStorageInfo = 1;
}

message DecommissionWorkerPResponse {}
message DecommissionWorkerPOptions {
  required string workerHostname = 1;
  optional int64 workerWebPort = 2;
  optional bool canRegisterAgain = 3;
}

/**
 * This interface contains block master service endpoints for Alluxio clients.
 */
service BlockMasterClientService {

  /**
   * Returns the block information for the given block id.
   */
  rpc GetBlockInfo(GetBlockInfoPRequest) returns (GetBlockInfoPResponse);

  /**
    * Returns block master information.
    */
  rpc GetBlockMasterInfo(GetBlockMasterInfoPOptions) returns (GetBlockMasterInfoPResponse);

  /**
   * Returns the capacity (in bytes).
   */
  rpc GetCapacityBytes(GetCapacityBytesPOptions) returns(GetCapacityBytesPResponse);

  /**
   * Returns the used storage (in bytes).
   */
  rpc GetUsedBytes(GetUsedBytesPOptions) returns (GetUsedBytesPResponse);

  /**
   * Returns a list of workers information.
   */
  rpc GetWorkerInfoList(GetWorkerInfoListPOptions) returns (GetWorkerInfoListPResponse);

  /**
 * Returns a list of workers information.
 */
  rpc GetLostWorkerList(GetWorkerInfoListPOptions) returns (GetWorkerInfoListPResponse);

  /**
   * If target worker is in the decommissioned worker set,
   * return true, remove target worker from decommissioned worker set; else, return false.
   */
  rpc RemoveDisabledWorker(RemoveDisabledWorkerPOptions)
      returns (RemoveDisabledWorkerPResponse);

  /**
   * Returns a list of workers information for report CLI.
   */
  rpc GetWorkerReport(GetWorkerReportPOptions) returns (GetWorkerInfoListPResponse);

  /**
   * Returns a list of worker lost storage information
   */
  rpc GetWorkerLostStorage(GetWorkerLostStoragePOptions) returns (GetWorkerLostStoragePResponse);

  /**
  *  Decommission the specific worker from Alluxio.
  */
  rpc DecommissionWorker(DecommissionWorkerPOptions) returns (DecommissionWorkerPResponse);
}

message TierList {
  repeated int64 tiers = 1;
}

message BlockIdList {
  repeated int64 blockId = 1;
}
/**
 * A list of lost storage paths inside a worker
 */
message StorageList {
  /** a lost storage path */
  repeated string storage = 1;
}

message BlockHeartbeatPOptions {
  repeated grpc.Metric metrics = 1;
  map<string, int64> capacityBytesOnTiers = 2;
}

message LocationBlockIdListEntry {
  optional BlockStoreLocationProto key= 1;
  optional BlockIdList value = 2;
}

message BlockHeartbeatPRequest {
  /** the id of the worker */
  optional int64 workerId = 1;
  /** the map of space used in bytes on all tiers */
  map<string, int64> usedBytesOnTiers = 2;
  /** the list of removed block ids */
  repeated int64 removedBlockIds = 3;
  /** the map of added blocks on all tiers (deprecated since 2.0, replaced by addedBlocks) */
  map<string, TierList> addedBlocksOnTiers = 4;
  optional BlockHeartbeatPOptions options = 5;
  /** the map of lost storage paths on all tiers */
  map<string, StorageList> lostStorage = 6;
  /** use repeated fields to represent mapping from BlockStoreLocationProto to TierList */
  repeated LocationBlockIdListEntry addedBlocks = 7;
}

message BlockHeartbeatPResponse {
  optional grpc.Command command = 1;
}

message CommitBlockPResponse {}
message CommitBlockPRequest {
  /** the id of the worker */
  optional int64 workerId = 1;
  /** the space used in bytes on the target tier */
  optional int64 usedBytesOnTier = 2;
  /** the alias of the target tier */
  optional string tierAlias = 3;
  /** the id of the block being committed */
  optional int64 blockId = 4;
  /** the length of the block being committed */
  optional int64 length = 5;
  optional CommitBlockPOptions options = 6;
  optional string mediumType = 7;
}

message CommitBlockPOptions {}

message NotifyWorkerIdPOptions {}
message NotifyWorkerIdPRequest {
  optional int64 workerId = 1;
  /** the worker network address */
  optional grpc.WorkerNetAddress workerNetAddress = 2;
  optional NotifyWorkerIdPOptions options = 3;
}
message NotifyWorkerIdPResponse {
}

message GetWorkerIdPOptions {}
message GetWorkerIdPRequest {
  /** the worker network address */
  optional grpc.WorkerNetAddress workerNetAddress = 1;
  optional GetWorkerIdPOptions options = 2;
}
message GetWorkerIdPResponse {
  optional int64 workerId = 1;
}

message GetRegisterLeasePRequest {
  optional int64 workerId = 1;
  /** This may not accurate because the worker can add/delete blocks before sending the RegisterWorkerPRequest */
  optional int64 blockCount = 2;
}
message GetRegisterLeasePOptions {}
message GetRegisterLeasePResponse {
  optional int64 workerId = 1;
  optional bool allowed = 2;
  optional int64 expiryMs = 3;
  optional GetRegisterLeasePOptions options = 4;
}

message RegisterWorkerPOptions {
  repeated grpc.ConfigProperty configs = 1;
  /** the worker version to display in info pages (useful for rolling upgrades) */
  optional BuildVersion buildVersion = 2;
}
message RegisterWorkerPRequest {
  /** the id of the worker */
  optional int64 workerId = 1;
  /** the list of storage tiers */
  repeated string storageTiers = 2;
  /** the map of total bytes on each tier */
  map<string, int64> totalBytesOnTiers = 3;
  /** the map of used bytes on each tier */
  map<string, int64> usedBytesOnTiers = 4;
  /** the map of list of blocks on each tier (deprecated since 2.0, replaced by currentBlocks*/
  map<string, TierList> currentBlocksOnTiers = 5;
  optional RegisterWorkerPOptions options = 6;
  /** the map of tier alias to a list of lost storage paths. */
  map<string, StorageList> lostStorage = 7;
  /** use repeated fields to represent mapping from BlockStoreLocationProto to TierList */
  repeated LocationBlockIdListEntry currentBlocks = 8;
}
message RegisterWorkerPResponse {}

/**
 * This interface contains block master service endpoints for Alluxio workers.
 */
service BlockMasterWorkerService {

  /**
   * Periodic block worker heartbeat returns an optional command for the block worker to execute.
   */
  rpc BlockHeartbeat(BlockHeartbeatPRequest) returns (BlockHeartbeatPResponse);

  /**
   * Marks the given block as committed.
   */
  rpc CommitBlock(CommitBlockPRequest) returns(CommitBlockPResponse);

  /**
   * Returns a worker id for the given network address.
   */
  rpc GetWorkerId(GetWorkerIdPRequest) returns (GetWorkerIdPResponse);

  /**
  * Notify all masters about the worker ID.
  */
  rpc NotifyWorkerId(NotifyWorkerIdPRequest) returns (NotifyWorkerIdPResponse);

  /**
   * Registers a worker.
   */
  rpc RegisterWorker(RegisterWorkerPRequest) returns (RegisterWorkerPResponse);

  /**
   * Registers a worker in a streaming way
   */
  rpc RegisterWorkerStream(stream RegisterWorkerPRequest) returns (stream RegisterWorkerPResponse);

  /**
   * Requests a lease for registration
   */
   rpc RequestRegisterLease(GetRegisterLeasePRequest) returns (GetRegisterLeasePResponse);
}
