syntax = "proto3";

package milvus.proto.query;

option go_package = "github.com/milvus-io/milvus/internal/proto/querypb";

import "common.proto";
import "milvus.proto";
import "internal.proto";
import "schema.proto";
import "data_coord.proto";

service QueryCoord {
  rpc GetComponentStates(internal.GetComponentStatesRequest) returns (internal.ComponentStates) {}
  rpc GetTimeTickChannel(internal.GetTimeTickChannelRequest) returns(milvus.StringResponse) {}
  rpc GetStatisticsChannel(internal.GetStatisticsChannelRequest) returns(milvus.StringResponse){}

  rpc ShowCollections(ShowCollectionsRequest) returns (ShowCollectionsResponse) {}
  rpc ShowPartitions(ShowPartitionsRequest) returns (ShowPartitionsResponse) {}

  rpc LoadPartitions(LoadPartitionsRequest) returns (common.Status) {}
  rpc ReleasePartitions(ReleasePartitionsRequest) returns (common.Status) {}
  rpc LoadCollection(LoadCollectionRequest) returns (common.Status) {}
  rpc ReleaseCollection(ReleaseCollectionRequest) returns (common.Status) {}

  rpc CreateQueryChannel(CreateQueryChannelRequest) returns (CreateQueryChannelResponse) {}
  rpc GetPartitionStates(GetPartitionStatesRequest) returns (GetPartitionStatesResponse) {}
  rpc GetSegmentInfo(GetSegmentInfoRequest) returns (GetSegmentInfoResponse) {}

  // https://wiki.lfaidata.foundation/display/MIL/MEP+8+--+Add+metrics+for+proxy
  rpc GetMetrics(milvus.GetMetricsRequest) returns (milvus.GetMetricsResponse) {}
}

service QueryNode {
  rpc GetComponentStates(internal.GetComponentStatesRequest) returns (internal.ComponentStates) {}
  rpc GetTimeTickChannel(internal.GetTimeTickChannelRequest) returns(milvus.StringResponse) {}
  rpc GetStatisticsChannel(internal.GetStatisticsChannelRequest) returns(milvus.StringResponse){}

  rpc AddQueryChannel(AddQueryChannelRequest) returns (common.Status) {}
  rpc RemoveQueryChannel(RemoveQueryChannelRequest) returns (common.Status) {}
  rpc WatchDmChannels(WatchDmChannelsRequest) returns (common.Status) {}
  rpc LoadSegments(LoadSegmentsRequest) returns (common.Status) {}
  rpc ReleaseCollection(ReleaseCollectionRequest) returns (common.Status) {}
  rpc ReleasePartitions(ReleasePartitionsRequest) returns (common.Status) {}
  rpc ReleaseSegments(ReleaseSegmentsRequest) returns (common.Status) {}
  rpc GetSegmentInfo(GetSegmentInfoRequest) returns (GetSegmentInfoResponse) {}

  // https://wiki.lfaidata.foundation/display/MIL/MEP+8+--+Add+metrics+for+proxy
  rpc GetMetrics(milvus.GetMetricsRequest) returns (milvus.GetMetricsResponse) {}
}

//--------------------query coordinator proto------------------
message ShowCollectionsRequest {
  common.MsgBase base = 1;
  // Not useful for now
  int64 dbID = 2;
  repeated int64 collectionIDs = 3;
}

message ShowCollectionsResponse {
  common.Status status = 1;
  repeated int64 collectionIDs = 2;
  repeated int64 inMemory_percentages = 3;
}

message ShowPartitionsRequest {
  common.MsgBase base = 1;
  int64 dbID = 2;
  int64 collectionID = 3;
  repeated int64 partitionIDs = 4;
}

message ShowPartitionsResponse {
  common.Status status = 1;
  repeated int64 partitionIDs = 2;
  repeated int64 inMemory_percentages = 3;
}

message LoadCollectionRequest {
  common.MsgBase base = 1;
  int64 dbID = 2;
  int64 collectionID = 3;
  schema.CollectionSchema schema = 4;
}

message ReleaseCollectionRequest {
  common.MsgBase base = 1;
  int64 dbID = 2;
  int64 collectionID = 3;
  int64 nodeID = 4;
}

message LoadPartitionsRequest {
  common.MsgBase base = 1;
  int64 dbID = 2;
  int64 collectionID = 3;
  repeated int64 partitionIDs = 4;
  schema.CollectionSchema schema = 5;
}

message ReleasePartitionsRequest {
  common.MsgBase base = 1;
  int64 dbID = 2;
  int64 collectionID = 3;
  repeated int64 partitionIDs = 4;
  int64 nodeID = 5;
}

message CreateQueryChannelRequest {
  int64 collectionID = 1;
  int64 proxyID = 2;
}

message CreateQueryChannelResponse {
  common.Status status = 1;
  string request_channel = 2;
  string result_channel = 3;
}

message GetPartitionStatesRequest {
  common.MsgBase base = 1;
  int64 dbID = 2;
  int64 collectionID = 3;
  repeated int64 partitionIDs = 4;
}

enum PartitionState {
  NotExist = 0;
  NotPresent = 1;
  OnDisk = 2;
  PartialInMemory = 3;
  InMemory = 4;
  PartialInGPU = 5;
  InGPU = 6;
}

message PartitionStates {
  int64 partitionID = 1;
  PartitionState state = 2;
  int64 inMemory_percentage = 3;
}

message GetPartitionStatesResponse {
  common.Status status = 1;
  repeated PartitionStates partition_descriptions = 2;
}

message GetSegmentInfoRequest {
  common.MsgBase base = 1;
  repeated int64 segmentIDs = 2; // deprecated
  int64 collectionID = 3;
}

message SegmentInfo {
  int64 segmentID = 1;
  int64 collectionID = 2;
  int64 partitionID = 3;
  int64 nodeID = 4;
  int64 mem_size = 5;
  int64 num_rows = 6;
  string index_name = 7;
  int64 indexID = 8;
  string channelID = 9;
  SegmentState segment_state = 10;
}

message GetSegmentInfoResponse {
  common.Status status = 1;
  repeated SegmentInfo infos = 2;
}

//-----------------query node proto----------------
message AddQueryChannelRequest {
  common.MsgBase base = 1;
  int64 nodeID = 2;
  int64 collectionID = 3;
  string request_channelID = 4;
  string result_channelID = 5;
  repeated int64 global_sealed_segmentID = 6;
  internal.MsgPosition seek_position = 7;
  repeated SegmentInfo global_sealed_segments = 8;
}

message RemoveQueryChannelRequest {
  common.MsgBase base = 1;
  int64 nodeID = 2;
  int64 collectionID = 3;
  string request_channelID = 4;
  string result_channelID = 5;
}

//message excludedSegmentInfo {
//  int64 segmentID = 1;
//  internal.MsgPosition pos = 2;
//}

//message WatchDmChannelInfo {
//  string channelID = 1;
//  internal.MsgPosition pos = 2;
//}

message WatchDmChannelsRequest {
  common.MsgBase base = 1;
  int64 nodeID = 2;
  int64 collectionID = 3;
  int64 partitionID = 4;
  repeated data.VchannelInfo infos = 5;
  schema.CollectionSchema schema = 6;
  repeated data.SegmentInfo exclude_infos = 7;
}

enum TriggerCondition {
  handoff = 0;
  loadBalance = 1;
  grpcRequest = 2;
  nodeDown = 3;
}

//message FieldBinlogPath {
//  int64 filedID = 1;
//  repeated string binlog_path = 2;
//}

//used for handoff task
message SegmentLoadInfo {
  int64 segmentID = 1;
  int64 partitionID = 2;
  int64 collectionID = 3;
  int64 dbID = 4;
  int64 flush_time = 5;
  repeated data.FieldBinlog binlog_paths = 6;
  int64 num_of_rows = 7;
}

message LoadSegmentsRequest {
  common.MsgBase base = 1;
  int64 nodeID = 2;
  repeated SegmentLoadInfo infos = 3;
  schema.CollectionSchema schema = 4;
  TriggerCondition load_condition = 5;
}

message ReleaseSegmentsRequest {
  common.MsgBase base = 1;
  int64 nodeID = 2;
  // Not useful for now
  int64 dbID = 3;
  int64 collectionID = 4;
  repeated int64 partitionIDs = 5;
  repeated int64 segmentIDs = 6;
}

//----------------etcd-----------------
enum SegmentState {
  None = 0;
  Growing = 1;
  Frozen = 2;
  sealing = 3;
  sealed = 4;
}

enum LoadType {
  LoadPartition = 0;
  loadCollection = 1;
}

message DmChannelInfo {
  int64 nodeID_loaded = 1;
  repeated string channelIDs = 2;
}

message QueryChannelInfo {
  int64 collectionID = 1;
  string query_channelID = 2;
  string query_result_channelID = 3;
}

message CollectionInfo {
  int64 collectionID = 1;
  repeated int64 partitionIDs = 2;
  repeated PartitionStates partition_states = 3;
  repeated DmChannelInfo channel_infos = 4;
  LoadType load_type = 5;
  schema.CollectionSchema schema = 6;
  repeated int64 released_partitionIDs = 7;
  int64 inMemory_percentage = 8;
}

message HandoffSegments {
  common.MsgBase base = 1;
  repeated SegmentLoadInfo infos = 2;
}

message LoadBalanceSegmentInfo {
  int64 segmentID = 1;
  int64 partitionID = 2;
  int64 collectionID = 3;
  int64 source_nodeID = 4;
  int64 dst_nodeID = 5;
  bool source_done = 6;
  bool dst_done = 7;
  bool valid_info = 8;
}

message LoadBalanceRequest {
  common.MsgBase base = 1;
  repeated int64 source_nodeIDs = 2;
  TriggerCondition balance_reason = 3;
}

//---------------- common query proto -----------------
message SealedSegmentsChangeInfo {
  common.MsgBase base = 1;
  int64 online_nodeID = 2;
  repeated SegmentInfo online_segments = 3;
  int64 offline_nodeID = 4;
  repeated SegmentInfo offline_segments = 5;
}
