syntax = "proto3";

package gitaly;

option go_package = "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb";

import "lint.proto";
import "shared.proto";

service PraefectInfoService {
  option (intercepted) = true;

  rpc RepositoryReplicas(RepositoryReplicasRequest) returns (RepositoryReplicasResponse);

  // DatalossCheck checks for unavailable repositories.
  rpc DatalossCheck(DatalossCheckRequest) returns (DatalossCheckResponse);

  // SetAuthoritativeStorage sets the authoritative storage for a repository on a given virtual storage.
  // This causes the current version of the repository on the authoritative storage to be considered the
  // latest and overwrite any other version on the virtual storage.
  rpc SetAuthoritativeStorage(SetAuthoritativeStorageRequest) returns (SetAuthoritativeStorageResponse);

  // SetReplicationFactor assigns or unassigns host nodes from the repository to meet the desired replication factor.
  // SetReplicationFactor returns an error when trying to set a replication factor that exceeds the storage node count
  // in the virtual storage. An error is also returned when trying to set a replication factor below one. The primary node
  // won't be unassigned as it needs a copy of the repository to accept writes. Likewise, the primary is the first storage
  // that gets assigned when setting a replication factor for a repository. Assignments of unconfigured storages are ignored.
  // This might cause the actual replication factor to be higher than desired if the replication factor is set during an upgrade
  // from a Praefect node that does not yet know about a new node. As assignments of unconfigured storages are ignored, replication
  // factor of repositories assigned to a storage node removed from the cluster is effectively decreased.
  rpc SetReplicationFactor(SetReplicationFactorRequest) returns (SetReplicationFactorResponse);
  // GetRepositoryMetadata returns the cluster metadata for a repository. Returns NotFound if the repository does not exist.
  rpc GetRepositoryMetadata(GetRepositoryMetadataRequest) returns (GetRepositoryMetadataResponse);
}

// GetRepositoryMetadataRequest specifies the repository to retrieve metadata for.
message GetRepositoryMetadataRequest {
  message Path {
    // virtual_storage is the virtual storage where the repository is.
    string virtual_storage = 1;
    // relative_path is the relative path of the repository in the virtual storage.
    string relative_path = 2;
  }

  oneof query {
    // repository_id is the ID of the repository to retrieve information for.
    int64 repository_id = 1;
    // path is the external path of the repository to retrieve information for.
    Path path = 2;
  };
}

// GeRepositoryMetadataResponse contains the repository's cluster metadata.
message GetRepositoryMetadataResponse {
  message Replica {
    // storage is the name of the replica's storage.
    string storage = 1;
    // assigned indicates whether the replica is on an assigned storage.
    bool assigned = 2;
    // generation is the replicas confirmed generation. Generation is -1 if the replica
    // does not yet exist.
    int64 generation = 4;
    // healthy indicates whether the replica is on a healthy storage.
    bool healthy = 5;
    // valid_primary indicates whether the replica is considered a valid primary.
    bool valid_primary = 6;
  }

  // repository_id is the internal ID of the repository.
  int64 repository_id = 1;
  // virtual_storage is the virtual storage in which the repository is located.
  string virtual_storage = 2;
  // relative_path is the relative path of the repository in the virtual storage.
  string relative_path = 3;
  // replica_path is the path on the disk where the replicas are stored.
  string replica_path = 4;
  // primary is the current primary of the repository.
  string primary = 5;
  // generation is the repository's current generation.
  int64 generation = 6;
  // replicas are the existing and supposed to exist replicas of this repository.
  repeated Replica replicas = 7;
}

// SetReplicationFactorRequest sets the desired replication factor for a repository.
message SetReplicationFactorRequest {
  // virtual_storage is the virtual storage the repository is located in
  string virtual_storage = 1;
  // relative_path is the relative path of the repository
  string relative_path = 2;
  // replication_factor is the desired replication factor. Replication must be equal or greater than 1.
  int32 replication_factor = 3;
}

// SetReplicationFactorResponse returns the assigned hosts after setting the desired replication factor.
message SetReplicationFactorResponse {
  // storages are the storages assigned to host the repository.
  repeated string storages = 1;
}

message SetAuthoritativeStorageRequest {
  string virtual_storage = 1;
  string relative_path = 2;
  string authoritative_storage = 3;
}

message SetAuthoritativeStorageResponse {}

message DatalossCheckRequest {
  string virtual_storage = 1;
  // include_partially_unavailable indicates whether to include repositories which are available but
  // are unavailable on some assigned storages.
  bool include_partially_replicated = 2;
}

message DatalossCheckResponse {
  message Repository {
    message Storage {
      // name of the storage
      string name = 1;
      // behind_by indicates how many generations this storage is behind.
      int64 behind_by = 2;
      // assigned indicates whether the storage is assigned to host the repository.
      bool assigned = 3;
      // healthy indicates whether the storage is considered healthy by the consensus of Praefect nodes.
      bool healthy = 4;
      // valid_primary indicates whether the storage is ready to act as the primary if necessary.
      bool valid_primary = 5;
    }

    // relative path of the repository with outdated replicas
    string relative_path = 1;
    // storages on which the repository is outdated
    repeated Storage storages = 2;
    // unavailable indicates whether the repository is in unavailable.
    bool unavailable = 3;

    // current primary storage of the repository
    string primary = 4;
  }

  // repositories with data loss
  repeated Repository repositories = 2;
}

message RepositoryReplicasRequest{
  Repository repository = 1;
}

message RepositoryReplicasResponse{
  message RepositoryDetails {
    Repository repository = 1;
    string checksum = 2;
  };

  RepositoryDetails primary = 1;
  repeated RepositoryDetails replicas = 2;
}
