// Copyright (C) 2024 Quickwit, Inc.
//
// Quickwit is offered under the AGPL v3.0 and as commercial software.
// For commercial licensing, contact us at hello@quickwit.io.
//
// AGPL:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

syntax = "proto3";

package quickwit.ingest.router;

import "quickwit/common.proto";
import "quickwit/ingest.proto";

service IngestRouterService {
  // Ingests batches of documents for one or multiple indexes.
  // TODO: Describe error cases and how to handle them.
  rpc Ingest(IngestRequestV2) returns (IngestResponseV2);
}

message IngestRequestV2 {
  repeated IngestSubrequest subrequests = 1;
  quickwit.ingest.CommitTypeV2 commit_type = 2;
}

message IngestSubrequest {
  // The subrequest ID is used to identify the various subrequests and responses
  // (ingest, persist, replicate) at play during the ingest and replication
  // process.
  uint32 subrequest_id = 1;
  string index_id = 2;
  string source_id = 3;
  quickwit.ingest.DocBatchV2 doc_batch = 4;
}

message IngestResponseV2 {
  repeated IngestSuccess successes  = 1;
  repeated IngestFailure failures  = 2;
}

message IngestSuccess {
  uint32 subrequest_id = 1;
  quickwit.common.IndexUid index_uid = 2;
  string source_id = 3;
  quickwit.ingest.ShardId shard_id = 4;
  // Replication position inclusive.
  quickwit.ingest.Position replication_position_inclusive = 5;
  uint32 num_ingested_docs = 6;
  repeated quickwit.ingest.ParseFailure parse_failures = 7;
}

enum IngestFailureReason {
  INGEST_FAILURE_REASON_UNSPECIFIED = 0;
  INGEST_FAILURE_REASON_INDEX_NOT_FOUND = 1;
  INGEST_FAILURE_REASON_SOURCE_NOT_FOUND = 2;
  INGEST_FAILURE_REASON_INTERNAL = 3;
  INGEST_FAILURE_REASON_NO_SHARDS_AVAILABLE = 4;
  INGEST_FAILURE_REASON_SHARD_RATE_LIMITED = 5;
  INGEST_FAILURE_REASON_WAL_FULL = 6;
  INGEST_FAILURE_REASON_TIMEOUT = 7;
  INGEST_FAILURE_REASON_ROUTER_LOAD_SHEDDING = 8;
  INGEST_FAILURE_REASON_LOAD_SHEDDING = 9;
  INGEST_FAILURE_REASON_CIRCUIT_BREAKER = 10;
}

message IngestFailure {
  uint32 subrequest_id = 1;
  string index_id = 2;
  string source_id = 3;
  IngestFailureReason reason = 5;
}
