// Copyright (c) 2020 Temporal Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

syntax = "proto3";

package temporal.server.api.persistence.v1;
option go_package = "go.temporal.io/server/api/persistence/v1;persistence";

import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";

import "dependencies/gogoproto/gogo.proto";

import "temporal/api/common/v1/message.proto";
import "temporal/api/enums/v1/workflow.proto";
import "temporal/api/failure/v1/message.proto";
import "temporal/api/history/v1/message.proto";
import "temporal/api/workflow/v1/message.proto";

import "temporal/server/api/enums/v1/common.proto";
import "temporal/server/api/enums/v1/workflow.proto";
import "temporal/server/api/enums/v1/task.proto";
import "temporal/server/api/history/v1/message.proto";

// shard column
message ShardInfo {
    int32 shard_id = 1;
    int64 range_id = 2;
    string owner = 3;
    int64 replication_ack_level = 4;
    int64 transfer_ack_level = 5;
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "since" is needed here. --)
    int32 stolen_since_renew = 6;
    google.protobuf.Timestamp update_time = 7 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp timer_ack_level_time = 8 [(gogoproto.stdtime) = true];
    int64 namespace_notification_version = 9;
    map<string, int64> cluster_transfer_ack_level = 10;
    map<string, google.protobuf.Timestamp> cluster_timer_ack_level = 11 [(gogoproto.stdtime) = true];
    map<string, int64> cluster_replication_level = 12;
    map<string, int64> replication_dlq_ack_level = 13;
    int64 visibility_ack_level = 14;
}

// execution column
message WorkflowExecutionInfo {
    string namespace_id = 1;
    string workflow_id = 2;
    string parent_namespace_id = 3;
    string parent_workflow_id = 4;
    string parent_run_id = 5;
    int64 initiated_id = 6;
    int64 completion_event_batch_id = 7;
    temporal.api.history.v1.HistoryEvent completion_event = 8;
    string task_queue = 9;
    string workflow_type_name = 10;
    google.protobuf.Duration workflow_execution_timeout = 11 [(gogoproto.stdduration) = true];
    google.protobuf.Duration workflow_run_timeout = 12 [(gogoproto.stdduration) = true];
    google.protobuf.Duration default_workflow_task_timeout = 13 [(gogoproto.stdduration) = true];
    int64 start_version = 14;
    reserved 15;
    reserved 16;
    int64 last_event_task_id = 17;
    int64 last_first_event_id = 18;
    int64 last_workflow_task_start_id = 19;
    google.protobuf.Timestamp start_time = 20 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp last_update_time = 21 [(gogoproto.stdtime) = true];
    int64 workflow_task_version = 22;
    int64 workflow_task_schedule_id = 23;
    int64 workflow_task_started_id = 24;
    google.protobuf.Duration workflow_task_timeout = 25 [(gogoproto.stdduration) = true];
    int32 workflow_task_attempt = 26;
    google.protobuf.Timestamp workflow_task_started_time = 27 [(gogoproto.stdtime) = true];
    google.protobuf.Timestamp workflow_task_scheduled_time = 28 [(gogoproto.stdtime) = true];
    bool cancel_requested = 29;
    google.protobuf.Timestamp workflow_task_original_scheduled_time = 30 [(gogoproto.stdtime) = true];
    string workflow_task_request_id = 31;
    string cancel_request_id = 32;
    string sticky_task_queue = 33;
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "to" is used to indicate interval. --)
    google.protobuf.Duration sticky_schedule_to_start_timeout = 34 [(gogoproto.stdduration) = true];
    int32 attempt = 35;
    google.protobuf.Duration retry_initial_interval = 36 [(gogoproto.stdduration) = true];
    google.protobuf.Duration retry_maximum_interval = 37 [(gogoproto.stdduration) = true];
    int32 retry_maximum_attempts = 38;
    double retry_backoff_coefficient = 39;
    google.protobuf.Timestamp workflow_execution_expiration_time = 40 [(gogoproto.stdtime) = true];
    repeated string retry_non_retryable_error_types = 41;
    bool has_retry_policy = 42;
    string cron_schedule = 43;
    reserved 44;
    reserved 45;
    int64 signal_count = 46;
    int64 history_size = 47 [deprecated = true];
    reserved 48;
    reserved 49;
    reserved 50;
    temporal.api.workflow.v1.ResetPoints auto_reset_points = 51;
    map<string, temporal.api.common.v1.Payload> search_attributes = 52;
    map<string, temporal.api.common.v1.Payload> memo = 53;
    temporal.server.api.history.v1.VersionHistories version_histories = 54;
    string first_execution_run_id = 55;
    ExecutionStats execution_stats = 56;
    google.protobuf.Timestamp workflow_run_expiration_time = 57 [(gogoproto.stdtime) = true];
    // Transaction Id of the first event in the last batch of events.
    int64 last_first_event_txn_id = 58;
    int64 state_transition_count = 59;
    google.protobuf.Timestamp execution_time = 60 [(gogoproto.stdtime) = true];
}

message ExecutionStats {
    int64 history_size = 1;
}

// execution_state column
message WorkflowExecutionState {
    string create_request_id = 1;
    string run_id = 2;
    temporal.server.api.enums.v1.WorkflowExecutionState state = 3;
    temporal.api.enums.v1.WorkflowExecutionStatus status = 4;
}

// transfer column
message TransferTaskInfo {
    string namespace_id = 1;
    string workflow_id = 2;
    string run_id = 3;
    temporal.server.api.enums.v1.TaskType task_type = 4;
    string target_namespace_id = 5;
    string target_workflow_id = 6;
    string target_run_id = 7;
    string task_queue = 8;
    bool target_child_workflow_only = 9;
    int64 schedule_id = 10;
    int64 version = 11;
    int64 task_id = 12;
    google.protobuf.Timestamp visibility_time = 13 [(gogoproto.stdtime) = true];
    reserved 14;
}

// replication column
message ReplicationTaskInfo {
    string namespace_id = 1;
    string workflow_id = 2;
    string run_id = 3;
    temporal.server.api.enums.v1.TaskType task_type = 4;
    int64 version = 5;
    int64 first_event_id = 6;
    int64 next_event_id = 7;
    int64 scheduled_id = 8;
    reserved 9;
    reserved 10;
    bytes branch_token = 11;
    reserved 12;
    bytes new_run_branch_token = 13;
    reserved 14;
    int64 task_id = 15;
}

// visibility_task_data column
message VisibilityTaskInfo {
    string namespace_id = 1;
    string workflow_id = 2;
    string run_id = 3;
    temporal.server.api.enums.v1.TaskType task_type = 4;
    int64 version = 5;
    int64 task_id = 6;
    google.protobuf.Timestamp visibility_time = 7 [(gogoproto.stdtime) = true];
}

// timer column
message TimerTaskInfo {
    string namespace_id = 1;
    string workflow_id = 2;
    string run_id = 3;
    temporal.server.api.enums.v1.TaskType task_type = 4;
    temporal.api.enums.v1.TimeoutType timeout_type = 5;
    temporal.server.api.enums.v1.WorkflowBackoffType workflow_backoff_type = 6;
    int64 version = 7;
    int32 schedule_attempt = 8;
    int64 event_id = 9;
    int64 task_id = 10;
    google.protobuf.Timestamp visibility_time = 11 [(gogoproto.stdtime) = true];
}

// activity_map column
message ActivityInfo {
    int64 version = 1;
    int64 scheduled_event_batch_id = 2;
    temporal.api.history.v1.HistoryEvent scheduled_event = 3;
    google.protobuf.Timestamp scheduled_time = 4 [(gogoproto.stdtime) = true];
    int64 started_id = 5;
    temporal.api.history.v1.HistoryEvent started_event = 6;
    google.protobuf.Timestamp started_time = 7 [(gogoproto.stdtime) = true];
    string activity_id = 8;
    string request_id = 9;
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "to" is used to indicate interval. --)
    google.protobuf.Duration schedule_to_start_timeout = 10 [(gogoproto.stdduration) = true];
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "to" is used to indicate interval. --)
    google.protobuf.Duration schedule_to_close_timeout = 11 [(gogoproto.stdduration) = true];
    // (-- api-linter: core::0140::prepositions=disabled
    //     aip.dev/not-precedent: "to" is used to indicate interval. --)
    google.protobuf.Duration start_to_close_timeout = 12 [(gogoproto.stdduration) = true];
    google.protobuf.Duration heartbeat_timeout = 13 [(gogoproto.stdduration) = true];
    bool cancel_requested = 14;
    int64 cancel_request_id = 15;
    int32 timer_task_status = 16;
    int32 attempt = 17;
    string task_queue = 18;
    string started_identity = 19;
    bool has_retry_policy = 20;
    google.protobuf.Duration retry_initial_interval = 21 [(gogoproto.stdduration) = true];
    google.protobuf.Duration retry_maximum_interval = 22 [(gogoproto.stdduration) = true];
    int32 retry_maximum_attempts = 23;
    google.protobuf.Timestamp retry_expiration_time = 24 [(gogoproto.stdtime) = true];
    double retry_backoff_coefficient = 25;
    repeated string retry_non_retryable_error_types = 26;
    temporal.api.failure.v1.Failure retry_last_failure = 27;
    string retry_last_worker_identity = 28;
    string namespace_id = 29;
    int64 schedule_id = 30;
    temporal.api.common.v1.Payloads last_heartbeat_details = 31;
    google.protobuf.Timestamp last_heartbeat_update_time = 32 [(gogoproto.stdtime) = true];
}

// timer_map column
message TimerInfo {
    int64 version = 1;
    int64 started_id = 2;
    google.protobuf.Timestamp expiry_time = 3 [(gogoproto.stdtime) = true];
    int64 task_status = 4;
    // timerId serves the purpose of indicating whether a timer task is generated for this timer info.
    string timer_id = 5;
}

// child_executions_map column
message ChildExecutionInfo {
    int64 version = 1;
    int64 initiated_event_batch_id = 2;
    int64 started_id = 3;
    temporal.api.history.v1.HistoryEvent initiated_event = 4;
    string started_workflow_id = 5;
    string started_run_id = 6;
    temporal.api.history.v1.HistoryEvent started_event = 7;
    string create_request_id = 8;
    string namespace = 9;
    string workflow_type_name = 10;
    temporal.api.enums.v1.ParentClosePolicy parent_close_policy = 11;
    int64 initiated_id = 12;
}

// request_cancel_map column
message RequestCancelInfo {
    int64 version = 1;
    int64 initiated_event_batch_id = 2;
    string cancel_request_id = 3;
    int64 initiated_id = 4;
}

// signal_map column
message SignalInfo {
    int64 version = 1;
    int64 initiated_event_batch_id = 2;
    string request_id = 3;
    string name = 4;
    temporal.api.common.v1.Payloads input = 5;
    string control = 6;
    int64 initiated_id = 7;
}

// checksum column
message Checksum {
    int32 version = 1;
    temporal.server.api.enums.v1.ChecksumFlavor flavor = 2;
    bytes value = 3;
}
