//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package exec.bit.control;

option java_package = "org.apache.drill.exec.proto";
option java_outer_classname = "BitControl";
option optimize_for = SPEED;

import "ExecutionProtos.proto";
import "Coordination.proto";
import "UserBitShared.proto";


////// BitControl RPC ///////
enum RpcType {
  HANDSHAKE = 0;
  ACK = 1;
  GOODBYE = 2;

  // bit requests
  REQ_INITIALIZE_FRAGMENTS = 3; // Returns Handle

  REQ_CANCEL_FRAGMENT = 6; // send a cancellation message for a fragment, returns Ack
  REQ_RECEIVER_FINISHED = 7;
  REQ_FRAGMENT_STATUS = 8; // send a fragment status, return Ack
  REQ_BIT_STATUS = 9; // get bit status.
  REQ_QUERY_STATUS = 10;
  REQ_QUERY_CANCEL = 15;
  REQ_UNPAUSE_FRAGMENT = 16; // send a resume message for a fragment, returns Ack
  REQ_CUSTOM = 17;

  // bit responses
  RESP_FRAGMENT_HANDLE = 11;
  RESP_FRAGMENT_STATUS = 12;
  RESP_BIT_STATUS = 13;
  RESP_QUERY_STATUS = 14;
  RESP_CUSTOM = 18;

  // both bit request and response
  SASL_MESSAGE = 19;
}

message BitControlHandshake{
  optional int32 rpc_version = 1;
  optional exec.shared.RpcChannel channel = 2 [default = BIT_CONTROL];
  optional DrillbitEndpoint endpoint = 3;
  repeated string authenticationMechanisms = 4;
}

message BitStatus {
  repeated FragmentStatus fragment_status = 1;
}

message FragmentStatus {
  optional exec.shared.MinorFragmentProfile profile = 1;
  optional FragmentHandle handle = 2;
}

message InitializeFragments {
  repeated PlanFragment fragment = 1;
}

message CustomMessage {
  optional int32 type = 1;
  optional bytes message = 2;
}

message PlanFragment {
  optional FragmentHandle handle = 1;
  optional float network_cost = 4;
  optional float cpu_cost = 5;
  optional float disk_cost = 6;
  optional float memory_cost = 7;
  optional string fragment_json = 8;
  optional bool leaf_fragment = 9;
  optional DrillbitEndpoint assignment = 10;
  optional DrillbitEndpoint foreman = 11;
  optional int64 mem_initial = 12 [default = 20000000]; // 20 megs
  optional int64 mem_max = 13 [default = 2000000000]; // 20 gigs
  optional exec.shared.UserCredentials credentials = 14;
  optional string options_json = 15;
  optional QueryContextInformation context = 16;
  repeated Collector collector = 17;
}

message Collector {
  optional int32 opposite_major_fragment_id = 1;
  repeated int32 incoming_minor_fragment = 2 [packed=true];
  optional bool supports_out_of_order = 3;
  optional bool is_spooling = 4;
  optional bool enable_dynamic_fc = 5;
}

message QueryContextInformation {
  optional int64 query_start_time = 1;      // start time of query in milliseconds
  optional int32 time_zone = 2;             // timezone of the Drillbit where user is connected
  optional string default_schema_name = 3;  // default schema in current session when the query is submitted
  optional string session_id = 4;           // current session id
}

message WorkQueueStatus{
  optional DrillbitEndpoint endpoint = 1;
  optional int32 queue_length = 2;
  optional int64 report_time = 3;
}

message FinishedReceiver {
  optional FragmentHandle receiver = 1;
  optional FragmentHandle sender = 2;
}
