go.temporal.io/server@v1.23.0/common/persistence/data_interfaces.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  //go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination data_interfaces_mock.go
    26  
    27  package persistence
    28  
    29  import (
    30  	"context"
    31  	"fmt"
    32  	"net"
    33  	"strings"
    34  	"time"
    35  
    36  	"github.com/pborman/uuid"
    37  	"google.golang.org/protobuf/types/known/timestamppb"
    38  
    39  	commonpb "go.temporal.io/api/common/v1"
    40  	enumspb "go.temporal.io/api/enums/v1"
    41  	historypb "go.temporal.io/api/history/v1"
    42  	enumsspb "go.temporal.io/server/api/enums/v1"
    43  	persistencespb "go.temporal.io/server/api/persistence/v1"
    44  	"go.temporal.io/server/common/persistence/serialization"
    45  	"go.temporal.io/server/service/history/tasks"
    46  )
    47  
    48  // CreateWorkflowMode workflow creation mode
    49  type CreateWorkflowMode int
    50  
    51  // QueueType is an enum that represents various queue types in persistence
    52  type QueueType int32
    53  
    54  // Queue types used in queue table
    55  // Use positive numbers for queue type
    56  // Negative numbers are reserved for DLQ
    57  
    58  const (
    59  	NamespaceReplicationQueueType QueueType = iota + 1
    60  )
    61  
    62  // Create Workflow Execution Mode
    63  const (
    64  	// CreateWorkflowModeBrandNew fail if current record exists
    65  	// Only applicable for CreateWorkflowExecution
    66  	CreateWorkflowModeBrandNew CreateWorkflowMode = iota
    67  	// CreateWorkflowModeUpdateCurrent update current record only if workflow is closed
    68  	// Only applicable for CreateWorkflowExecution
    69  	CreateWorkflowModeUpdateCurrent
    70  	// CreateWorkflowModeBypassCurrent do not update current record since workflow is in zombie state
    71  	// applicable for CreateWorkflowExecution, UpdateWorkflowExecution
    72  	CreateWorkflowModeBypassCurrent
    73  )
    74  
    75  // UpdateWorkflowMode update mode
    76  type UpdateWorkflowMode int
    77  
    78  // Update Workflow Execution Mode
    79  const (
    80  	// UpdateWorkflowModeUpdateCurrent update workflow, including current record
    81  	// NOTE: update on current record is a condition update
    82  	UpdateWorkflowModeUpdateCurrent UpdateWorkflowMode = iota
    83  	// UpdateWorkflowModeBypassCurrent update workflow, without current record
    84  	// NOTE: current record CANNOT point to the workflow to be updated
    85  	UpdateWorkflowModeBypassCurrent
    86  )
    87  
    88  // ConflictResolveWorkflowMode conflict resolve mode
    89  type ConflictResolveWorkflowMode int
    90  
    91  // Conflict Resolve Workflow Mode
    92  const (
    93  	// ConflictResolveWorkflowModeUpdateCurrent conflict resolve workflow, including current record
    94  	// NOTE: update on current record is a condition update
    95  	ConflictResolveWorkflowModeUpdateCurrent ConflictResolveWorkflowMode = iota
    96  	// ConflictResolveWorkflowModeBypassCurrent conflict resolve workflow, without current record
    97  	// NOTE: current record CANNOT point to the workflow to be updated
    98  	ConflictResolveWorkflowModeBypassCurrent
    99  )
   100  
   101  // UnknownNumRowsAffected is returned when the number of rows that an API affected cannot be determined
   102  const UnknownNumRowsAffected = -1
   103  
   104  const (
   105  	// InitialFailoverNotificationVersion is the initial failover version for a namespace
   106  	InitialFailoverNotificationVersion int64 = 0
   107  )
   108  
   109  const numItemsInGarbageInfo = 3
   110  
   111  const ScheduledTaskMinPrecision = time.Millisecond
   112  
   113  type (
   114  	// InvalidPersistenceRequestError represents invalid request to persistence
   115  	InvalidPersistenceRequestError struct {
   116  		Msg string
   117  	}
   118  
   119  	// AppendHistoryTimeoutError represents a failed insert to history tree / node request
   120  	AppendHistoryTimeoutError struct {
   121  		Msg string
   122  	}
   123  
   124  	// CurrentWorkflowConditionFailedError represents a failed conditional update for current workflow record
   125  	CurrentWorkflowConditionFailedError struct {
   126  		Msg              string
   127  		RequestID        string
   128  		RunID            string
   129  		State            enumsspb.WorkflowExecutionState
   130  		Status           enumspb.WorkflowExecutionStatus
   131  		LastWriteVersion int64
   132  	}
   133  
   134  	// WorkflowConditionFailedError represents a failed conditional update for workflow record
   135  	WorkflowConditionFailedError struct {
   136  		Msg             string
   137  		NextEventID     int64
   138  		DBRecordVersion int64
   139  	}
   140  
   141  	// ConditionFailedError represents a failed conditional update for execution record
   142  	ConditionFailedError struct {
   143  		Msg string
   144  	}
   145  
   146  	// ShardAlreadyExistError is returned when conditionally creating a shard fails
   147  	ShardAlreadyExistError struct {
   148  		Msg string
   149  	}
   150  
   151  	// ShardOwnershipLostError is returned when conditional update fails due to RangeID for the shard
   152  	ShardOwnershipLostError struct {
   153  		ShardID int32
   154  		Msg     string
   155  	}
   156  
   157  	// TimeoutError is returned when a write operation fails due to a timeout
   158  	TimeoutError struct {
   159  		Msg string
   160  	}
   161  
   162  	// TransactionSizeLimitError is returned when the transaction size is too large
   163  	TransactionSizeLimitError struct {
   164  		Msg string
   165  	}
   166  
   167  	// TaskQueueKey is the struct used to identity TaskQueues
   168  	TaskQueueKey struct {
   169  		NamespaceID   string
   170  		TaskQueueName string
   171  		TaskQueueType enumspb.TaskQueueType
   172  	}
   173  
   174  	// GetOrCreateShardRequest is used to get shard information, or supply
   175  	// initial information to create a shard in executions table
   176  	GetOrCreateShardRequest struct {
   177  		ShardID          int32
   178  		InitialShardInfo *persistencespb.ShardInfo // optional, zero value will be used if missing
   179  		LifecycleContext context.Context           // cancelled when shard is unloaded
   180  	}
   181  
   182  	// GetOrCreateShardResponse is the response to GetOrCreateShard
   183  	GetOrCreateShardResponse struct {
   184  		ShardInfo *persistencespb.ShardInfo
   185  	}
   186  
   187  	// UpdateShardRequest  is used to update shard information
   188  	UpdateShardRequest struct {
   189  		ShardInfo       *persistencespb.ShardInfo
   190  		PreviousRangeID int64
   191  	}
   192  
   193  	// AssertShardOwnershipRequest is used to assert shard ownership
   194  	AssertShardOwnershipRequest struct {
   195  		ShardID int32
   196  		RangeID int64
   197  	}
   198  
   199  	// AddHistoryTasksRequest is used to write new tasks
   200  	AddHistoryTasksRequest struct {
   201  		ShardID int32
   202  		RangeID int64
   203  
   204  		NamespaceID string
   205  		WorkflowID  string
   206  		RunID       string
   207  
   208  		Tasks map[tasks.Category][]tasks.Task
   209  	}
   210  
   211  	// CreateWorkflowExecutionRequest is used to write a new workflow execution
   212  	CreateWorkflowExecutionRequest struct {
   213  		ShardID int32
   214  		RangeID int64
   215  
   216  		Mode CreateWorkflowMode
   217  
   218  		PreviousRunID            string
   219  		PreviousLastWriteVersion int64
   220  
   221  		NewWorkflowSnapshot WorkflowSnapshot
   222  		NewWorkflowEvents   []*WorkflowEvents
   223  	}
   224  
   225  	// CreateWorkflowExecutionResponse is the response to CreateWorkflowExecutionRequest
   226  	CreateWorkflowExecutionResponse struct {
   227  		NewMutableStateStats MutableStateStatistics
   228  	}
   229  
   230  	// UpdateWorkflowExecutionRequest is used to update a workflow execution
   231  	UpdateWorkflowExecutionRequest struct {
   232  		ShardID int32
   233  		RangeID int64
   234  
   235  		Mode UpdateWorkflowMode
   236  
   237  		UpdateWorkflowMutation WorkflowMutation
   238  		UpdateWorkflowEvents   []*WorkflowEvents
   239  		NewWorkflowSnapshot    *WorkflowSnapshot
   240  		NewWorkflowEvents      []*WorkflowEvents
   241  	}
   242  
   243  	// UpdateWorkflowExecutionResponse is response for UpdateWorkflowExecutionRequest
   244  	UpdateWorkflowExecutionResponse struct {
   245  		UpdateMutableStateStats MutableStateStatistics
   246  		NewMutableStateStats    *MutableStateStatistics
   247  	}
   248  
   249  	// ConflictResolveWorkflowExecutionRequest is used to reset workflow execution state for a single run
   250  	ConflictResolveWorkflowExecutionRequest struct {
   251  		ShardID int32
   252  		RangeID int64
   253  
   254  		Mode ConflictResolveWorkflowMode
   255  
   256  		// workflow to be resetted
   257  		ResetWorkflowSnapshot WorkflowSnapshot
   258  		ResetWorkflowEvents   []*WorkflowEvents
   259  
   260  		// maybe new workflow
   261  		NewWorkflowSnapshot *WorkflowSnapshot
   262  		NewWorkflowEvents   []*WorkflowEvents
   263  
   264  		// current workflow
   265  		CurrentWorkflowMutation *WorkflowMutation
   266  		CurrentWorkflowEvents   []*WorkflowEvents
   267  	}
   268  
   269  	ConflictResolveWorkflowExecutionResponse struct {
   270  		ResetMutableStateStats   MutableStateStatistics
   271  		NewMutableStateStats     *MutableStateStatistics
   272  		CurrentMutableStateStats *MutableStateStatistics
   273  	}
   274  
   275  	// GetCurrentExecutionRequest is used to retrieve the current RunId for an execution
   276  	GetCurrentExecutionRequest struct {
   277  		ShardID     int32
   278  		NamespaceID string
   279  		WorkflowID  string
   280  	}
   281  
   282  	// GetCurrentExecutionResponse is the response to GetCurrentExecution
   283  	GetCurrentExecutionResponse struct {
   284  		StartRequestID string
   285  		RunID          string
   286  		State          enumsspb.WorkflowExecutionState
   287  		Status         enumspb.WorkflowExecutionStatus
   288  	}
   289  
   290  	// GetWorkflowExecutionRequest is used to retrieve the info of a workflow execution
   291  	GetWorkflowExecutionRequest struct {
   292  		ShardID     int32
   293  		NamespaceID string
   294  		WorkflowID  string
   295  		RunID       string
   296  	}
   297  
   298  	// GetWorkflowExecutionResponse is the response to GetWorkflowExecutionRequest
   299  	GetWorkflowExecutionResponse struct {
   300  		State             *persistencespb.WorkflowMutableState
   301  		DBRecordVersion   int64
   302  		MutableStateStats MutableStateStatistics
   303  	}
   304  
   305  	// SetWorkflowExecutionRequest is used to overwrite the info of a workflow execution
   306  	SetWorkflowExecutionRequest struct {
   307  		ShardID int32
   308  		RangeID int64
   309  
   310  		SetWorkflowSnapshot WorkflowSnapshot
   311  	}
   312  
   313  	// SetWorkflowExecutionResponse is the response to SetWorkflowExecutionRequest
   314  	SetWorkflowExecutionResponse struct {
   315  	}
   316  
   317  	// ListConcreteExecutionsRequest is request to ListConcreteExecutions
   318  	ListConcreteExecutionsRequest struct {
   319  		ShardID   int32
   320  		PageSize  int
   321  		PageToken []byte
   322  	}
   323  
   324  	// ListConcreteExecutionsResponse is response to ListConcreteExecutions
   325  	ListConcreteExecutionsResponse struct {
   326  		States    []*persistencespb.WorkflowMutableState
   327  		PageToken []byte
   328  	}
   329  
   330  	// WorkflowEvents is used as generic workflow history events transaction container
   331  	WorkflowEvents struct {
   332  		NamespaceID string
   333  		WorkflowID  string
   334  		RunID       string
   335  		BranchToken []byte
   336  		PrevTxnID   int64
   337  		TxnID       int64
   338  		Events      []*historypb.HistoryEvent
   339  	}
   340  
   341  	// WorkflowMutation is used as generic workflow execution state mutation
   342  	WorkflowMutation struct {
   343  		ExecutionInfo  *persistencespb.WorkflowExecutionInfo
   344  		ExecutionState *persistencespb.WorkflowExecutionState
   345  		// TODO deprecate NextEventID in favor of DBRecordVersion
   346  		NextEventID int64
   347  
   348  		UpsertActivityInfos       map[int64]*persistencespb.ActivityInfo
   349  		DeleteActivityInfos       map[int64]struct{}
   350  		UpsertTimerInfos          map[string]*persistencespb.TimerInfo
   351  		DeleteTimerInfos          map[string]struct{}
   352  		UpsertChildExecutionInfos map[int64]*persistencespb.ChildExecutionInfo
   353  		DeleteChildExecutionInfos map[int64]struct{}
   354  		UpsertRequestCancelInfos  map[int64]*persistencespb.RequestCancelInfo
   355  		DeleteRequestCancelInfos  map[int64]struct{}
   356  		UpsertSignalInfos         map[int64]*persistencespb.SignalInfo
   357  		DeleteSignalInfos         map[int64]struct{}
   358  		UpsertSignalRequestedIDs  map[string]struct{}
   359  		DeleteSignalRequestedIDs  map[string]struct{}
   360  		NewBufferedEvents         []*historypb.HistoryEvent
   361  		ClearBufferedEvents       bool
   362  
   363  		Tasks map[tasks.Category][]tasks.Task
   364  
   365  		// TODO deprecate Condition in favor of DBRecordVersion
   366  		Condition       int64
   367  		DBRecordVersion int64
   368  		Checksum        *persistencespb.Checksum
   369  	}
   370  
   371  	// WorkflowSnapshot is used as generic workflow execution state snapshot
   372  	WorkflowSnapshot struct {
   373  		ExecutionInfo  *persistencespb.WorkflowExecutionInfo
   374  		ExecutionState *persistencespb.WorkflowExecutionState
   375  		// TODO deprecate NextEventID in favor of DBRecordVersion
   376  		NextEventID int64
   377  
   378  		ActivityInfos       map[int64]*persistencespb.ActivityInfo
   379  		TimerInfos          map[string]*persistencespb.TimerInfo
   380  		ChildExecutionInfos map[int64]*persistencespb.ChildExecutionInfo
   381  		RequestCancelInfos  map[int64]*persistencespb.RequestCancelInfo
   382  		SignalInfos         map[int64]*persistencespb.SignalInfo
   383  		SignalRequestedIDs  map[string]struct{}
   384  
   385  		Tasks map[tasks.Category][]tasks.Task
   386  
   387  		// TODO deprecate Condition in favor of DBRecordVersion
   388  		Condition       int64
   389  		DBRecordVersion int64
   390  		Checksum        *persistencespb.Checksum
   391  	}
   392  
   393  	// DeleteWorkflowExecutionRequest is used to delete a workflow execution
   394  	DeleteWorkflowExecutionRequest struct {
   395  		ShardID     int32
   396  		NamespaceID string
   397  		WorkflowID  string
   398  		RunID       string
   399  	}
   400  
   401  	// DeleteCurrentWorkflowExecutionRequest is used to delete the current workflow execution
   402  	DeleteCurrentWorkflowExecutionRequest struct {
   403  		ShardID     int32
   404  		NamespaceID string
   405  		WorkflowID  string
   406  		RunID       string
   407  	}
   408  
   409  	// RegisterHistoryTaskReaderRequest is a hint for underlying persistence implementation
   410  	// that a new queue reader is created by queue processing logic
   411  	RegisterHistoryTaskReaderRequest struct {
   412  		ShardID      int32
   413  		ShardOwner   string
   414  		TaskCategory tasks.Category
   415  		ReaderID     int64
   416  	}
   417  
   418  	// UnregisterHistoryTaskReaderRequest is a hint for underlying persistence implementation
   419  	// that queue processing logic is done using an existing queue reader
   420  	UnregisterHistoryTaskReaderRequest RegisterHistoryTaskReaderRequest
   421  
   422  	// UpdateHistoryTaskReaderProgressRequest is a hint for underlying persistence implementation
   423  	// that a certain queue reader's process and the fact that it won't try to load tasks with
   424  	// key less than InclusiveMinPendingTaskKey
   425  	UpdateHistoryTaskReaderProgressRequest struct {
   426  		ShardID                    int32
   427  		ShardOwner                 string
   428  		TaskCategory               tasks.Category
   429  		ReaderID                   int64
   430  		InclusiveMinPendingTaskKey tasks.Key
   431  	}
   432  
   433  	// GetHistoryTasksRequest is used to get a range of history tasks
   434  	// Either max TaskID or FireTime is required depending on the
   435  	// task category type. Min TaskID or FireTime is optional.
   436  	GetHistoryTasksRequest struct {
   437  		ShardID             int32
   438  		TaskCategory        tasks.Category
   439  		ReaderID            int64
   440  		InclusiveMinTaskKey tasks.Key
   441  		ExclusiveMaxTaskKey tasks.Key
   442  		BatchSize           int
   443  		NextPageToken       []byte
   444  	}
   445  
   446  	// GetHistoryTasksResponse is the response for GetHistoryTasks
   447  	GetHistoryTasksResponse struct {
   448  		Tasks         []tasks.Task
   449  		NextPageToken []byte
   450  	}
   451  
   452  	// CompleteHistoryTaskRequest delete one history task
   453  	CompleteHistoryTaskRequest struct {
   454  		ShardID      int32
   455  		TaskCategory tasks.Category
   456  		TaskKey      tasks.Key
   457  	}
   458  
   459  	// RangeCompleteHistoryTasksRequest deletes a range of history tasks
   460  	// Either max TaskID or FireTime is required depending on the
   461  	// task category type. Min TaskID or FireTime is optional.
   462  	RangeCompleteHistoryTasksRequest struct {
   463  		ShardID             int32
   464  		TaskCategory        tasks.Category
   465  		InclusiveMinTaskKey tasks.Key
   466  		ExclusiveMaxTaskKey tasks.Key
   467  	}
   468  
   469  	// GetReplicationTasksRequest is used to read tasks from the replication task queue
   470  	GetReplicationTasksRequest struct {
   471  		ShardID       int32
   472  		MinTaskID     int64
   473  		MaxTaskID     int64
   474  		BatchSize     int
   475  		NextPageToken []byte
   476  	}
   477  
   478  	// PutReplicationTaskToDLQRequest is used to put a replication task to dlq
   479  	PutReplicationTaskToDLQRequest struct {
   480  		ShardID           int32
   481  		SourceClusterName string
   482  		TaskInfo          *persistencespb.ReplicationTaskInfo
   483  	}
   484  
   485  	// GetReplicationTasksFromDLQRequest is used to get replication tasks from dlq
   486  	GetReplicationTasksFromDLQRequest struct {
   487  		GetHistoryTasksRequest
   488  
   489  		SourceClusterName string
   490  	}
   491  
   492  	// DeleteReplicationTaskFromDLQRequest is used to delete replication task from DLQ
   493  	DeleteReplicationTaskFromDLQRequest struct {
   494  		CompleteHistoryTaskRequest
   495  
   496  		SourceClusterName string
   497  	}
   498  
   499  	// RangeDeleteReplicationTaskFromDLQRequest is used to delete replication tasks from DLQ
   500  	RangeDeleteReplicationTaskFromDLQRequest struct {
   501  		RangeCompleteHistoryTasksRequest
   502  
   503  		SourceClusterName string
   504  	}
   505  
   506  	// CreateTaskQueueRequest create a new task queue
   507  	CreateTaskQueueRequest struct {
   508  		RangeID       int64
   509  		TaskQueueInfo *persistencespb.TaskQueueInfo
   510  	}
   511  
   512  	// CreateTaskQueueResponse is the response to CreateTaskQueue
   513  	CreateTaskQueueResponse struct {
   514  	}
   515  
   516  	// UpdateTaskQueueRequest is used to update task queue implementation information
   517  	UpdateTaskQueueRequest struct {
   518  		RangeID       int64
   519  		TaskQueueInfo *persistencespb.TaskQueueInfo
   520  
   521  		PrevRangeID int64
   522  	}
   523  
   524  	// UpdateTaskQueueResponse is the response to UpdateTaskQueue
   525  	UpdateTaskQueueResponse struct {
   526  	}
   527  
   528  	// GetTaskQueueRequest get the target task queue
   529  	GetTaskQueueRequest struct {
   530  		NamespaceID string
   531  		TaskQueue   string
   532  		TaskType    enumspb.TaskQueueType
   533  	}
   534  
   535  	// GetTaskQueueResponse is the response to GetTaskQueue
   536  	GetTaskQueueResponse struct {
   537  		RangeID       int64
   538  		TaskQueueInfo *persistencespb.TaskQueueInfo
   539  	}
   540  
   541  	// GetTaskQueueUserDataRequest is the input type for the GetTaskQueueUserData API
   542  	GetTaskQueueUserDataRequest struct {
   543  		NamespaceID string
   544  		TaskQueue   string
   545  	}
   546  
   547  	// GetTaskQueueUserDataResponse is the output type for the GetTaskQueueUserData API
   548  	GetTaskQueueUserDataResponse struct {
   549  		UserData *persistencespb.VersionedTaskQueueUserData
   550  	}
   551  
   552  	// UpdateTaskQueueUserDataRequest is the input type for the UpdateTaskQueueUserData API
   553  	UpdateTaskQueueUserDataRequest struct {
   554  		NamespaceID     string
   555  		TaskQueue       string
   556  		UserData        *persistencespb.VersionedTaskQueueUserData
   557  		BuildIdsAdded   []string
   558  		BuildIdsRemoved []string
   559  	}
   560  
   561  	ListTaskQueueUserDataEntriesRequest struct {
   562  		NamespaceID   string
   563  		PageSize      int
   564  		NextPageToken []byte
   565  	}
   566  
   567  	TaskQueueUserDataEntry struct {
   568  		TaskQueue string
   569  		UserData  *persistencespb.VersionedTaskQueueUserData
   570  	}
   571  
   572  	ListTaskQueueUserDataEntriesResponse struct {
   573  		NextPageToken []byte
   574  		Entries       []*TaskQueueUserDataEntry
   575  	}
   576  
   577  	GetTaskQueuesByBuildIdRequest struct {
   578  		NamespaceID string
   579  		BuildID     string
   580  	}
   581  
   582  	CountTaskQueuesByBuildIdRequest struct {
   583  		NamespaceID string
   584  		BuildID     string
   585  	}
   586  
   587  	// ListTaskQueueRequest contains the request params needed to invoke ListTaskQueue API
   588  	ListTaskQueueRequest struct {
   589  		PageSize  int
   590  		PageToken []byte
   591  	}
   592  
   593  	// ListTaskQueueResponse is the response from ListTaskQueue API
   594  	ListTaskQueueResponse struct {
   595  		Items         []*PersistedTaskQueueInfo
   596  		NextPageToken []byte
   597  	}
   598  
   599  	// DeleteTaskQueueRequest contains the request params needed to invoke DeleteTaskQueue API
   600  	DeleteTaskQueueRequest struct {
   601  		TaskQueue *TaskQueueKey
   602  		RangeID   int64
   603  	}
   604  
   605  	// CreateTasksRequest is used to create a new task for a workflow execution
   606  	CreateTasksRequest struct {
   607  		TaskQueueInfo *PersistedTaskQueueInfo
   608  		Tasks         []*persistencespb.AllocatedTaskInfo
   609  	}
   610  
   611  	// CreateTasksResponse is the response to CreateTasksRequest
   612  	CreateTasksResponse struct {
   613  	}
   614  
   615  	PersistedTaskQueueInfo struct {
   616  		Data    *persistencespb.TaskQueueInfo
   617  		RangeID int64
   618  	}
   619  
   620  	// GetTasksRequest is used to retrieve tasks of a task queue
   621  	GetTasksRequest struct {
   622  		NamespaceID        string
   623  		TaskQueue          string
   624  		TaskType           enumspb.TaskQueueType
   625  		InclusiveMinTaskID int64
   626  		ExclusiveMaxTaskID int64
   627  		PageSize           int
   628  		NextPageToken      []byte
   629  	}
   630  
   631  	// GetTasksResponse is the response to GetTasksRequests
   632  	GetTasksResponse struct {
   633  		Tasks         []*persistencespb.AllocatedTaskInfo
   634  		NextPageToken []byte
   635  	}
   636  
   637  	// CompleteTaskRequest is used to complete a task
   638  	CompleteTaskRequest struct {
   639  		TaskQueue *TaskQueueKey
   640  		TaskID    int64
   641  	}
   642  
   643  	// CompleteTasksLessThanRequest contains the request params needed to invoke CompleteTasksLessThan API
   644  	CompleteTasksLessThanRequest struct {
   645  		NamespaceID        string
   646  		TaskQueueName      string
   647  		TaskType           enumspb.TaskQueueType
   648  		ExclusiveMaxTaskID int64 // Tasks less than this ID will be completed
   649  		Limit              int   // Limit on the max number of tasks that can be completed. Required param
   650  	}
   651  
   652  	// CreateNamespaceRequest is used to create the namespace
   653  	CreateNamespaceRequest struct {
   654  		Namespace         *persistencespb.NamespaceDetail
   655  		IsGlobalNamespace bool
   656  	}
   657  
   658  	// CreateNamespaceResponse is the response for CreateNamespace
   659  	CreateNamespaceResponse struct {
   660  		ID string
   661  	}
   662  
   663  	// GetNamespaceRequest is used to read namespace
   664  	GetNamespaceRequest struct {
   665  		ID   string
   666  		Name string
   667  	}
   668  
   669  	// GetNamespaceResponse is the response for GetNamespace
   670  	GetNamespaceResponse struct {
   671  		Namespace           *persistencespb.NamespaceDetail
   672  		IsGlobalNamespace   bool
   673  		NotificationVersion int64
   674  	}
   675  
   676  	// UpdateNamespaceRequest is used to update namespace
   677  	UpdateNamespaceRequest struct {
   678  		Namespace           *persistencespb.NamespaceDetail
   679  		IsGlobalNamespace   bool
   680  		NotificationVersion int64
   681  	}
   682  
   683  	// RenameNamespaceRequest is used to rename namespace.
   684  	RenameNamespaceRequest struct {
   685  		PreviousName string
   686  		NewName      string
   687  	}
   688  
   689  	// DeleteNamespaceRequest is used to delete namespace entry from namespaces table
   690  	DeleteNamespaceRequest struct {
   691  		ID string
   692  	}
   693  
   694  	// DeleteNamespaceByNameRequest is used to delete namespace entry from namespaces_by_name table
   695  	DeleteNamespaceByNameRequest struct {
   696  		Name string
   697  	}
   698  
   699  	// ListNamespacesRequest is used to list namespaces
   700  	ListNamespacesRequest struct {
   701  		PageSize       int
   702  		NextPageToken  []byte
   703  		IncludeDeleted bool
   704  	}
   705  
   706  	// ListNamespacesResponse is the response for GetNamespace
   707  	ListNamespacesResponse struct {
   708  		Namespaces    []*GetNamespaceResponse
   709  		NextPageToken []byte
   710  	}
   711  
   712  	// GetMetadataResponse is the response for GetMetadata
   713  	GetMetadataResponse struct {
   714  		NotificationVersion int64
   715  	}
   716  
   717  	// MutableStateStatistics is the size stats for MutableState
   718  	MutableStateStatistics struct {
   719  		TotalSize         int
   720  		HistoryStatistics *HistoryStatistics
   721  
   722  		// Breakdown of size into more granular stats
   723  		ExecutionInfoSize  int
   724  		ExecutionStateSize int
   725  
   726  		ActivityInfoSize      int
   727  		TimerInfoSize         int
   728  		ChildInfoSize         int
   729  		RequestCancelInfoSize int
   730  		SignalInfoSize        int
   731  		SignalRequestIDSize   int
   732  		BufferedEventsSize    int
   733  		// UpdateInfoSize is included in ExecutionInfoSize
   734  
   735  		// Item count for various information captured within mutable state
   736  		ActivityInfoCount      int
   737  		TimerInfoCount         int
   738  		ChildInfoCount         int
   739  		RequestCancelInfoCount int
   740  		SignalInfoCount        int
   741  		SignalRequestIDCount   int
   742  		BufferedEventsCount    int
   743  		TaskCountByCategory    map[string]int
   744  		UpdateInfoCount        int
   745  
   746  		// Total item count for various information captured within mutable state
   747  		TotalActivityCount              int64
   748  		TotalUserTimerCount             int64
   749  		TotalChildExecutionCount        int64
   750  		TotalRequestCancelExternalCount int64
   751  		TotalSignalExternalCount        int64
   752  		TotalSignalCount                int64
   753  		TotalUpdateCount                int64
   754  	}
   755  
   756  	HistoryStatistics struct {
   757  		SizeDiff  int
   758  		CountDiff int
   759  	}
   760  
   761  	// AppendHistoryNodesRequest is used to append a batch of history nodes
   762  	AppendHistoryNodesRequest struct {
   763  		// The shard to get history node data
   764  		ShardID int32
   765  		// true if this is the first append request to the branch
   766  		IsNewBranch bool
   767  		// the info for clean up data in background
   768  		Info string
   769  		// The branch to be appended
   770  		BranchToken []byte
   771  		// The batch of events to be appended. The first eventID will become the nodeID of this batch
   772  		Events []*historypb.HistoryEvent
   773  		// TransactionID for events before these events. For events chaining
   774  		PrevTransactionID int64
   775  		// requested TransactionID for this write operation. For the same eventID, the node with larger TransactionID always wins
   776  		TransactionID int64
   777  	}
   778  
   779  	// AppendHistoryNodesResponse is a response to AppendHistoryNodesRequest
   780  	AppendHistoryNodesResponse struct {
   781  		// the size of the event data that has been appended
   782  		Size int
   783  	}
   784  
   785  	// AppendRawHistoryNodesRequest is used to append a batch of raw history nodes
   786  	AppendRawHistoryNodesRequest struct {
   787  		// The shard to get history node data
   788  		ShardID int32
   789  		// true if this is the first append request to the branch
   790  		IsNewBranch bool
   791  		// the info for clean up data in background
   792  		Info string
   793  		// The branch to be appended
   794  		BranchToken []byte
   795  		// The batch of events to be appended. The first eventID will become the nodeID of this batch
   796  		History *commonpb.DataBlob
   797  		// TransactionID for events before these events. For events chaining
   798  		PrevTransactionID int64
   799  		// requested TransactionID for this write operation. For the same eventID, the node with larger TransactionID always wins
   800  		TransactionID int64
   801  		// NodeID is the first event id.
   802  		NodeID int64
   803  	}
   804  
   805  	// ReadHistoryBranchRequest is used to read a history branch
   806  	ReadHistoryBranchRequest struct {
   807  		// The shard to get history branch data
   808  		ShardID int32
   809  		// The branch to be read
   810  		BranchToken []byte
   811  		// Get the history nodes from MinEventID. Inclusive.
   812  		MinEventID int64
   813  		// Get the history nodes upto MaxEventID.  Exclusive.
   814  		MaxEventID int64
   815  		// Maximum number of batches of events per page. Not that number of events in a batch >=1, it is not number of events per page.
   816  		// However for a single page, it is also possible that the returned events is less than PageSize (event zero events) due to stale events.
   817  		PageSize int
   818  		// Token to continue reading next page of history append transactions.  Pass in empty slice for first page
   819  		NextPageToken []byte
   820  	}
   821  
   822  	// ReadHistoryBranchResponse is the response to ReadHistoryBranchRequest
   823  	ReadHistoryBranchResponse struct {
   824  		// History events
   825  		HistoryEvents []*historypb.HistoryEvent
   826  		// Token to read next page if there are more events beyond page size.
   827  		// Use this to set NextPageToken on ReadHistoryBranchRequest to read the next page.
   828  		// Empty means we have reached the last page, not need to continue
   829  		NextPageToken []byte
   830  		// Size of history read from store
   831  		Size int
   832  	}
   833  
   834  	// ReadHistoryBranchRequest is used to read a history branch
   835  	ReadHistoryBranchReverseRequest struct {
   836  		// The shard to get history branch data
   837  		ShardID int32
   838  		// The branch to be read
   839  		BranchToken []byte
   840  		// Get the history nodes upto MaxEventID.  Exclusive.
   841  		MaxEventID int64
   842  		// Maximum number of batches of events per page. Not that number of events in a batch >=1, it is not number of events per page.
   843  		// However for a single page, it is also possible that the returned events is less than PageSize (event zero events) due to stale events.
   844  		PageSize int
   845  		// LastFirstTransactionID specified in mutable state. Only used for reading in reverse order.
   846  		LastFirstTransactionID int64
   847  		// Token to continue reading next page of history append transactions.  Pass in empty slice for first page
   848  		NextPageToken []byte
   849  	}
   850  
   851  	// ReadHistoryBranchResponse is the response to ReadHistoryBranchRequest
   852  	ReadHistoryBranchReverseResponse struct {
   853  		// History events
   854  		HistoryEvents []*historypb.HistoryEvent
   855  		// Token to read next page if there are more events beyond page size.
   856  		// Use this to set NextPageToken on ReadHistoryBranchRequest to read the next page.
   857  		// Empty means we have reached the last page, not need to continue
   858  		NextPageToken []byte
   859  		// Size of history read from store
   860  		Size int
   861  	}
   862  
   863  	// ReadHistoryBranchByBatchResponse is the response to ReadHistoryBranchRequest
   864  	ReadHistoryBranchByBatchResponse struct {
   865  		// History events by batch
   866  		History []*historypb.History
   867  		// TransactionID for relevant History batch
   868  		TransactionIDs []int64
   869  		// Token to read next page if there are more events beyond page size.
   870  		// Use this to set NextPageToken on ReadHistoryBranchRequest to read the next page.
   871  		// Empty means we have reached the last page, not need to continue
   872  		NextPageToken []byte
   873  		// Size of history read from store
   874  		Size int
   875  	}
   876  
   877  	// ReadRawHistoryBranchResponse is the response to ReadHistoryBranchRequest
   878  	ReadRawHistoryBranchResponse struct {
   879  		// HistoryEventBlobs history event blobs
   880  		HistoryEventBlobs []*commonpb.DataBlob
   881  		// NodeIDs is the first event id of each history blob
   882  		NodeIDs []int64
   883  		// Token to read next page if there are more events beyond page size.
   884  		// Use this to set NextPageToken on ReadHistoryBranchRequest to read the next page.
   885  		// Empty means we have reached the last page, not need to continue
   886  		NextPageToken []byte
   887  		// Size of history read from store
   888  		Size int
   889  	}
   890  
   891  	// ForkHistoryBranchRequest is used to fork a history branch
   892  	ForkHistoryBranchRequest struct {
   893  		// The shard to get history branch data
   894  		ShardID int32
   895  		// The namespace performing the fork
   896  		NamespaceID string
   897  		// The base branch to fork from
   898  		ForkBranchToken []byte
   899  		// The nodeID to fork from, the new branch will start from ( inclusive ), the base branch will stop at(exclusive)
   900  		// Application must provide a void forking nodeID, it must be a valid nodeID in that branch. A valid nodeID is the firstEventID of a valid batch of events.
   901  		// And ForkNodeID > 1 because forking from 1 doesn't make any sense.
   902  		ForkNodeID int64
   903  		// the info for clean up data in background
   904  		Info string
   905  	}
   906  
   907  	// ForkHistoryBranchResponse is the response to ForkHistoryBranchRequest
   908  	ForkHistoryBranchResponse struct {
   909  		// branchToken to represent the new branch
   910  		NewBranchToken []byte
   911  	}
   912  
   913  	// CompleteForkBranchRequest is used to complete forking
   914  	CompleteForkBranchRequest struct {
   915  		// the new branch returned from ForkHistoryBranchRequest
   916  		BranchToken []byte
   917  		// true means the fork is success, will update the flag, otherwise will delete the new branch
   918  		Success bool
   919  		// The shard to update history branch data
   920  		ShardID *int
   921  	}
   922  
   923  	// DeleteHistoryBranchRequest is used to remove a history branch
   924  	DeleteHistoryBranchRequest struct {
   925  		// The shard to delete history branch data
   926  		ShardID int32
   927  		// branch to be deleted
   928  		BranchToken []byte
   929  	}
   930  
   931  	// TrimHistoryBranchRequest is used to validate & trim a history branch
   932  	TrimHistoryBranchRequest struct {
   933  		// The shard to delete history branch data
   934  		ShardID int32
   935  		// branch to be validated & trimmed
   936  		BranchToken []byte
   937  		// known valid node ID
   938  		NodeID int64
   939  		// known valid transaction ID
   940  		TransactionID int64
   941  	}
   942  
   943  	// TrimHistoryBranchResponse is the response to TrimHistoryBranchRequest
   944  	TrimHistoryBranchResponse struct {
   945  	}
   946  
   947  	// GetHistoryTreeRequest is used to retrieve branch info of a history tree
   948  	GetHistoryTreeRequest struct {
   949  		// A UUID of a tree
   950  		TreeID string
   951  		// Get data from this shard
   952  		ShardID int32
   953  	}
   954  
   955  	// HistoryBranchDetail contains detailed information of a branch
   956  	HistoryBranchDetail struct {
   957  		BranchInfo *persistencespb.HistoryBranch
   958  		ForkTime   *timestamppb.Timestamp
   959  		Info       string
   960  	}
   961  
   962  	// GetHistoryTreeResponse is a response to GetHistoryTreeRequest
   963  	GetHistoryTreeResponse struct {
   964  		BranchInfos []*persistencespb.HistoryBranch
   965  	}
   966  
   967  	// GetAllHistoryTreeBranchesRequest is a request of GetAllHistoryTreeBranches
   968  	GetAllHistoryTreeBranchesRequest struct {
   969  		// pagination token
   970  		NextPageToken []byte
   971  		// maximum number of branches returned per page
   972  		PageSize int
   973  	}
   974  
   975  	// GetAllHistoryTreeBranchesResponse is a response to GetAllHistoryTreeBranches
   976  	GetAllHistoryTreeBranchesResponse struct {
   977  		// pagination token
   978  		NextPageToken []byte
   979  		// all branches of all trees
   980  		Branches []HistoryBranchDetail
   981  	}
   982  
   983  	// ListClusterMetadataRequest is the request to ListClusterMetadata
   984  	ListClusterMetadataRequest struct {
   985  		PageSize      int
   986  		NextPageToken []byte
   987  	}
   988  
   989  	// ListClusterMetadataResponse is the response to ListClusterMetadata
   990  	ListClusterMetadataResponse struct {
   991  		ClusterMetadata []*GetClusterMetadataResponse
   992  		NextPageToken   []byte
   993  	}
   994  
   995  	// GetClusterMetadataRequest is the request to GetClusterMetadata
   996  	GetClusterMetadataRequest struct {
   997  		ClusterName string
   998  	}
   999  
  1000  	// GetClusterMetadataResponse is the response to GetClusterMetadata
  1001  	GetClusterMetadataResponse struct {
  1002  		*persistencespb.ClusterMetadata
  1003  		Version int64
  1004  	}
  1005  
  1006  	// SaveClusterMetadataRequest is the request to SaveClusterMetadata
  1007  	SaveClusterMetadataRequest struct {
  1008  		*persistencespb.ClusterMetadata
  1009  		Version int64
  1010  	}
  1011  
  1012  	// DeleteClusterMetadataRequest is the request to DeleteClusterMetadata
  1013  	DeleteClusterMetadataRequest struct {
  1014  		ClusterName string
  1015  	}
  1016  
  1017  	// GetClusterMembersRequest is the request to GetClusterMembers
  1018  	GetClusterMembersRequest struct {
  1019  		LastHeartbeatWithin time.Duration
  1020  		RPCAddressEquals    net.IP
  1021  		HostIDEquals        uuid.UUID
  1022  		RoleEquals          ServiceType
  1023  		SessionStartedAfter time.Time
  1024  		NextPageToken       []byte
  1025  		PageSize            int
  1026  	}
  1027  
  1028  	// GetClusterMembersResponse is the response to GetClusterMembers
  1029  	GetClusterMembersResponse struct {
  1030  		ActiveMembers []*ClusterMember
  1031  		NextPageToken []byte
  1032  	}
  1033  
  1034  	// ClusterMember is used as a response to GetClusterMembers
  1035  	ClusterMember struct {
  1036  		Role          ServiceType
  1037  		HostID        uuid.UUID
  1038  		RPCAddress    net.IP
  1039  		RPCPort       uint16
  1040  		SessionStart  time.Time
  1041  		LastHeartbeat time.Time
  1042  		RecordExpiry  time.Time
  1043  	}
  1044  
  1045  	// UpsertClusterMembershipRequest is the request to UpsertClusterMembership
  1046  	UpsertClusterMembershipRequest struct {
  1047  		Role         ServiceType
  1048  		HostID       uuid.UUID
  1049  		RPCAddress   net.IP
  1050  		RPCPort      uint16
  1051  		SessionStart time.Time
  1052  		RecordExpiry time.Duration
  1053  	}
  1054  
  1055  	// PruneClusterMembershipRequest is the request to PruneClusterMembership
  1056  	PruneClusterMembershipRequest struct {
  1057  		MaxRecordsPruned int
  1058  	}
  1059  
  1060  	// Closeable is an interface for any entity that supports a close operation to release resources
  1061  	// TODO: allow this method to return errors
  1062  	Closeable interface {
  1063  		Close()
  1064  	}
  1065  
  1066  	// ShardManager is used to manage all shards
  1067  	ShardManager interface {
  1068  		Closeable
  1069  		GetName() string
  1070  
  1071  		GetOrCreateShard(ctx context.Context, request *GetOrCreateShardRequest) (*GetOrCreateShardResponse, error)
  1072  		UpdateShard(ctx context.Context, request *UpdateShardRequest) error
  1073  		AssertShardOwnership(ctx context.Context, request *AssertShardOwnershipRequest) error
  1074  	}
  1075  
  1076  	// ExecutionManager is used to manage workflow executions
  1077  	ExecutionManager interface {
  1078  		Closeable
  1079  		GetName() string
  1080  		GetHistoryBranchUtil() HistoryBranchUtil
  1081  
  1082  		CreateWorkflowExecution(ctx context.Context, request *CreateWorkflowExecutionRequest) (*CreateWorkflowExecutionResponse, error)
  1083  		UpdateWorkflowExecution(ctx context.Context, request *UpdateWorkflowExecutionRequest) (*UpdateWorkflowExecutionResponse, error)
  1084  		ConflictResolveWorkflowExecution(ctx context.Context, request *ConflictResolveWorkflowExecutionRequest) (*ConflictResolveWorkflowExecutionResponse, error)
  1085  		DeleteWorkflowExecution(ctx context.Context, request *DeleteWorkflowExecutionRequest) error
  1086  		DeleteCurrentWorkflowExecution(ctx context.Context, request *DeleteCurrentWorkflowExecutionRequest) error
  1087  		GetCurrentExecution(ctx context.Context, request *GetCurrentExecutionRequest) (*GetCurrentExecutionResponse, error)
  1088  		GetWorkflowExecution(ctx context.Context, request *GetWorkflowExecutionRequest) (*GetWorkflowExecutionResponse, error)
  1089  		SetWorkflowExecution(ctx context.Context, request *SetWorkflowExecutionRequest) (*SetWorkflowExecutionResponse, error)
  1090  
  1091  		// Scan operations
  1092  
  1093  		ListConcreteExecutions(ctx context.Context, request *ListConcreteExecutionsRequest) (*ListConcreteExecutionsResponse, error)
  1094  
  1095  		// Tasks related APIs
  1096  
  1097  		// Hints for persistence implementation regarding history task readers
  1098  		RegisterHistoryTaskReader(ctx context.Context, request *RegisterHistoryTaskReaderRequest) error
  1099  		UnregisterHistoryTaskReader(ctx context.Context, request *UnregisterHistoryTaskReaderRequest)
  1100  		UpdateHistoryTaskReaderProgress(ctx context.Context, request *UpdateHistoryTaskReaderProgressRequest)
  1101  
  1102  		AddHistoryTasks(ctx context.Context, request *AddHistoryTasksRequest) error
  1103  		GetHistoryTasks(ctx context.Context, request *GetHistoryTasksRequest) (*GetHistoryTasksResponse, error)
  1104  		CompleteHistoryTask(ctx context.Context, request *CompleteHistoryTaskRequest) error
  1105  		RangeCompleteHistoryTasks(ctx context.Context, request *RangeCompleteHistoryTasksRequest) error
  1106  
  1107  		PutReplicationTaskToDLQ(ctx context.Context, request *PutReplicationTaskToDLQRequest) error
  1108  		GetReplicationTasksFromDLQ(ctx context.Context, request *GetReplicationTasksFromDLQRequest) (*GetHistoryTasksResponse, error)
  1109  		DeleteReplicationTaskFromDLQ(ctx context.Context, request *DeleteReplicationTaskFromDLQRequest) error
  1110  		RangeDeleteReplicationTaskFromDLQ(ctx context.Context, request *RangeDeleteReplicationTaskFromDLQRequest) error
  1111  		IsReplicationDLQEmpty(ctx context.Context, request *GetReplicationTasksFromDLQRequest) (bool, error)
  1112  
  1113  		// The below are history V2 APIs
  1114  		// V2 regards history events growing as a tree, decoupled from workflow concepts
  1115  		// For Temporal, treeID is new runID, except for fork(reset), treeID will be the runID that it forks from.
  1116  
  1117  		// AppendHistoryNodes add a node to history node table
  1118  		AppendHistoryNodes(ctx context.Context, request *AppendHistoryNodesRequest) (*AppendHistoryNodesResponse, error)
  1119  		// AppendRawHistoryNodes add a node of raw histories to history node table
  1120  		AppendRawHistoryNodes(ctx context.Context, request *AppendRawHistoryNodesRequest) (*AppendHistoryNodesResponse, error)
  1121  		// ReadHistoryBranch returns history node data for a branch
  1122  		ReadHistoryBranch(ctx context.Context, request *ReadHistoryBranchRequest) (*ReadHistoryBranchResponse, error)
  1123  		// ReadHistoryBranchByBatch returns history node data for a branch ByBatch
  1124  		ReadHistoryBranchByBatch(ctx context.Context, request *ReadHistoryBranchRequest) (*ReadHistoryBranchByBatchResponse, error)
  1125  		// ReadHistoryBranchReverse returns history node data for a branch
  1126  		ReadHistoryBranchReverse(ctx context.Context, request *ReadHistoryBranchReverseRequest) (*ReadHistoryBranchReverseResponse, error)
  1127  		// ReadRawHistoryBranch returns history node raw data for a branch ByBatch
  1128  		// NOTE: this API should only be used by 3+DC
  1129  		ReadRawHistoryBranch(ctx context.Context, request *ReadHistoryBranchRequest) (*ReadRawHistoryBranchResponse, error)
  1130  		// ForkHistoryBranch forks a new branch from a old branch
  1131  		ForkHistoryBranch(ctx context.Context, request *ForkHistoryBranchRequest) (*ForkHistoryBranchResponse, error)
  1132  		// DeleteHistoryBranch removes a branch
  1133  		// If this is the last branch to delete, it will also remove the root node
  1134  		DeleteHistoryBranch(ctx context.Context, request *DeleteHistoryBranchRequest) error
  1135  		// TrimHistoryBranch validate & trim a history branch
  1136  		TrimHistoryBranch(ctx context.Context, request *TrimHistoryBranchRequest) (*TrimHistoryBranchResponse, error)
  1137  		// GetHistoryTree returns all branch information of a tree
  1138  		GetHistoryTree(ctx context.Context, request *GetHistoryTreeRequest) (*GetHistoryTreeResponse, error)
  1139  		// GetAllHistoryTreeBranches returns all branches of all trees
  1140  		GetAllHistoryTreeBranches(ctx context.Context, request *GetAllHistoryTreeBranchesRequest) (*GetAllHistoryTreeBranchesResponse, error)
  1141  	}
  1142  
  1143  	// TaskManager is used to manage tasks and task queues
  1144  	TaskManager interface {
  1145  		Closeable
  1146  		GetName() string
  1147  		CreateTaskQueue(ctx context.Context, request *CreateTaskQueueRequest) (*CreateTaskQueueResponse, error)
  1148  		UpdateTaskQueue(ctx context.Context, request *UpdateTaskQueueRequest) (*UpdateTaskQueueResponse, error)
  1149  		GetTaskQueue(ctx context.Context, request *GetTaskQueueRequest) (*GetTaskQueueResponse, error)
  1150  		ListTaskQueue(ctx context.Context, request *ListTaskQueueRequest) (*ListTaskQueueResponse, error)
  1151  		DeleteTaskQueue(ctx context.Context, request *DeleteTaskQueueRequest) error
  1152  		CreateTasks(ctx context.Context, request *CreateTasksRequest) (*CreateTasksResponse, error)
  1153  		GetTasks(ctx context.Context, request *GetTasksRequest) (*GetTasksResponse, error)
  1154  		CompleteTask(ctx context.Context, request *CompleteTaskRequest) error
  1155  		// CompleteTasksLessThan completes tasks less than or equal to the given task id
  1156  		// This API takes a limit parameter which specifies the count of maxRows that
  1157  		// can be deleted. This parameter may be ignored by the underlying storage, but
  1158  		// its mandatory to specify it. On success this method returns the number of rows
  1159  		// actually deleted. If the underlying storage doesn't support "limit", all rows
  1160  		// less than or equal to taskID will be deleted.
  1161  		// On success, this method returns either:
  1162  		//  - UnknownNumRowsAffected (this means all rows below value are deleted)
  1163  		//  - number of rows deleted, which may be equal to limit
  1164  		CompleteTasksLessThan(ctx context.Context, request *CompleteTasksLessThanRequest) (int, error)
  1165  
  1166  		// GetTaskQueueUserData gets versioned user data.
  1167  		// This data would only exist if a user uses APIs that generate it, such as the worker versioning related APIs.
  1168  		// The caller should be prepared to gracefully handle the "NotFound" service error.
  1169  		GetTaskQueueUserData(ctx context.Context, request *GetTaskQueueUserDataRequest) (*GetTaskQueueUserDataResponse, error)
  1170  		// UpdateTaskQueueUserData updates the user data for a given task queue.
  1171  		// The request takes the _current_ known version along with the data to update.
  1172  		// The caller should +1 increment the cached version number if this call succeeds.
  1173  		// Fails with ConditionFailedError if the user data was updated concurrently.
  1174  		UpdateTaskQueueUserData(ctx context.Context, request *UpdateTaskQueueUserDataRequest) error
  1175  		ListTaskQueueUserDataEntries(ctx context.Context, request *ListTaskQueueUserDataEntriesRequest) (*ListTaskQueueUserDataEntriesResponse, error)
  1176  		GetTaskQueuesByBuildId(ctx context.Context, request *GetTaskQueuesByBuildIdRequest) ([]string, error)
  1177  		CountTaskQueuesByBuildId(ctx context.Context, request *CountTaskQueuesByBuildIdRequest) (int, error)
  1178  	}
  1179  
  1180  	// MetadataManager is used to manage metadata CRUD for namespace entities
  1181  	MetadataManager interface {
  1182  		Closeable
  1183  		GetName() string
  1184  		CreateNamespace(ctx context.Context, request *CreateNamespaceRequest) (*CreateNamespaceResponse, error)
  1185  		GetNamespace(ctx context.Context, request *GetNamespaceRequest) (*GetNamespaceResponse, error)
  1186  		UpdateNamespace(ctx context.Context, request *UpdateNamespaceRequest) error
  1187  		RenameNamespace(ctx context.Context, request *RenameNamespaceRequest) error
  1188  		DeleteNamespace(ctx context.Context, request *DeleteNamespaceRequest) error
  1189  		DeleteNamespaceByName(ctx context.Context, request *DeleteNamespaceByNameRequest) error
  1190  		ListNamespaces(ctx context.Context, request *ListNamespacesRequest) (*ListNamespacesResponse, error)
  1191  		GetMetadata(ctx context.Context) (*GetMetadataResponse, error)
  1192  		InitializeSystemNamespaces(ctx context.Context, currentClusterName string) error
  1193  	}
  1194  
  1195  	// ClusterMetadataManager is used to manage cluster-wide metadata and configuration
  1196  	ClusterMetadataManager interface {
  1197  		Closeable
  1198  		GetName() string
  1199  		GetClusterMembers(ctx context.Context, request *GetClusterMembersRequest) (*GetClusterMembersResponse, error)
  1200  		UpsertClusterMembership(ctx context.Context, request *UpsertClusterMembershipRequest) error
  1201  		PruneClusterMembership(ctx context.Context, request *PruneClusterMembershipRequest) error
  1202  		ListClusterMetadata(ctx context.Context, request *ListClusterMetadataRequest) (*ListClusterMetadataResponse, error)
  1203  		GetCurrentClusterMetadata(ctx context.Context) (*GetClusterMetadataResponse, error)
  1204  		GetClusterMetadata(ctx context.Context, request *GetClusterMetadataRequest) (*GetClusterMetadataResponse, error)
  1205  		SaveClusterMetadata(ctx context.Context, request *SaveClusterMetadataRequest) (bool, error)
  1206  		DeleteClusterMetadata(ctx context.Context, request *DeleteClusterMetadataRequest) error
  1207  	}
  1208  
  1209  	// HistoryTaskQueueManager is responsible for managing a queue of internal history tasks. This is called a history
  1210  	// task queue manager, but the actual history task queues are not managed by this object. Instead, this object is
  1211  	// responsible for managing a generic queue of history tasks (which is what the history task DLQ is).
  1212  	HistoryTaskQueueManager interface {
  1213  		EnqueueTask(ctx context.Context, request *EnqueueTaskRequest) (*EnqueueTaskResponse, error)
  1214  		ReadRawTasks(
  1215  			ctx context.Context,
  1216  			request *ReadRawTasksRequest,
  1217  		) (*ReadRawTasksResponse, error)
  1218  		ReadTasks(ctx context.Context, request *ReadTasksRequest) (*ReadTasksResponse, error)
  1219  		// CreateQueue must return an ErrQueueAlreadyExists if the queue already exists.
  1220  		CreateQueue(ctx context.Context, request *CreateQueueRequest) (*CreateQueueResponse, error)
  1221  		DeleteTasks(ctx context.Context, request *DeleteTasksRequest) (*DeleteTasksResponse, error)
  1222  		ListQueues(ctx context.Context, request *ListQueuesRequest) (*ListQueuesResponse, error)
  1223  	}
  1224  
  1225  	HistoryTaskQueueManagerImpl struct {
  1226  		queue      QueueV2
  1227  		serializer *serialization.TaskSerializer
  1228  	}
  1229  
  1230  	// QueueKey identifies a history task queue. It is converted to a queue name using the GetQueueName method.
  1231  	QueueKey struct {
  1232  		QueueType     QueueV2Type
  1233  		Category      tasks.Category
  1234  		SourceCluster string
  1235  		// TargetCluster is only used for cross-cluster replication tasks.
  1236  		TargetCluster string
  1237  	}
  1238  
  1239  	// EnqueueTaskRequest does not include a QueueKey because it does not need the QueueKey.Category field, as that can
  1240  	// already be inferred from the Task field.
  1241  	EnqueueTaskRequest struct {
  1242  		QueueType     QueueV2Type
  1243  		SourceCluster string
  1244  		TargetCluster string
  1245  		Task          tasks.Task
  1246  		// SourceShardID of the task in its original cluster. Note that tasks may move between clusters, so this shard
  1247  		// id may not be the same as the shard id of the task in the current cluster.
  1248  		SourceShardID int
  1249  	}
  1250  
  1251  	EnqueueTaskResponse struct {
  1252  		Metadata MessageMetadata
  1253  	}
  1254  
  1255  	ReadTasksRequest struct {
  1256  		QueueKey      QueueKey
  1257  		PageSize      int
  1258  		NextPageToken []byte
  1259  	}
  1260  
  1261  	HistoryTask struct {
  1262  		MessageMetadata MessageMetadata
  1263  		Task            tasks.Task
  1264  	}
  1265  
  1266  	ReadTasksResponse struct {
  1267  		Tasks         []HistoryTask
  1268  		NextPageToken []byte
  1269  	}
  1270  
  1271  	ReadRawTasksRequest = ReadTasksRequest
  1272  
  1273  	RawHistoryTask struct {
  1274  		MessageMetadata MessageMetadata
  1275  		Payload         *persistencespb.HistoryTask
  1276  	}
  1277  
  1278  	ReadRawTasksResponse struct {
  1279  		Tasks         []RawHistoryTask
  1280  		NextPageToken []byte
  1281  	}
  1282  
  1283  	CreateQueueRequest struct {
  1284  		QueueKey QueueKey
  1285  	}
  1286  
  1287  	CreateQueueResponse struct {
  1288  	}
  1289  
  1290  	DeleteTasksRequest struct {
  1291  		QueueKey                    QueueKey
  1292  		InclusiveMaxMessageMetadata MessageMetadata
  1293  	}
  1294  
  1295  	DeleteTasksResponse struct {
  1296  		MessagesDeleted int64
  1297  	}
  1298  
  1299  	ListQueuesRequest struct {
  1300  		QueueType     QueueV2Type
  1301  		PageSize      int
  1302  		NextPageToken []byte
  1303  	}
  1304  
  1305  	ListQueuesResponse struct {
  1306  		Queues        []QueueInfo
  1307  		NextPageToken []byte
  1308  	}
  1309  )
  1310  
  1311  func (e *InvalidPersistenceRequestError) Error() string {
  1312  	return e.Msg
  1313  }
  1314  
  1315  func (e *AppendHistoryTimeoutError) Error() string {
  1316  	return e.Msg
  1317  }
  1318  
  1319  func (e *CurrentWorkflowConditionFailedError) Error() string {
  1320  	return e.Msg
  1321  }
  1322  
  1323  func (e *WorkflowConditionFailedError) Error() string {
  1324  	return e.Msg
  1325  }
  1326  
  1327  func (e *ConditionFailedError) Error() string {
  1328  	return e.Msg
  1329  }
  1330  
  1331  func (e *ShardAlreadyExistError) Error() string {
  1332  	return e.Msg
  1333  }
  1334  
  1335  func (e *ShardOwnershipLostError) Error() string {
  1336  	return e.Msg
  1337  }
  1338  
  1339  func (e *TimeoutError) Error() string {
  1340  	return e.Msg
  1341  }
  1342  
  1343  func (e *TransactionSizeLimitError) Error() string {
  1344  	return e.Msg
  1345  }
  1346  
  1347  func IsConflictErr(err error) bool {
  1348  	switch err.(type) {
  1349  	case *CurrentWorkflowConditionFailedError,
  1350  		*WorkflowConditionFailedError,
  1351  		*ConditionFailedError:
  1352  		return true
  1353  	}
  1354  	return false
  1355  }
  1356  
  1357  // UnixMilliseconds returns t as a Unix time, the number of milliseconds elapsed since January 1, 1970 UTC.
  1358  // It should be used for all CQL timestamp.
  1359  func UnixMilliseconds(t time.Time) int64 {
  1360  	// Handling zero time separately because UnixNano is undefined for zero times.
  1361  	if t.IsZero() {
  1362  		return 0
  1363  	}
  1364  
  1365  	unixNano := t.UnixNano()
  1366  	if unixNano < 0 {
  1367  		// Time is before January 1, 1970 UTC
  1368  		return 0
  1369  	}
  1370  	return unixNano / int64(time.Millisecond)
  1371  }
  1372  
  1373  // BuildHistoryGarbageCleanupInfo combine the workflow identity information into a string
  1374  func BuildHistoryGarbageCleanupInfo(namespaceID, workflowID, runID string) string {
  1375  	return fmt.Sprintf("%v:%v:%v", namespaceID, workflowID, runID)
  1376  }
  1377  
  1378  // SplitHistoryGarbageCleanupInfo returns workflow identity information
  1379  func SplitHistoryGarbageCleanupInfo(info string) (namespaceID, workflowID, runID string, err error) {
  1380  	ss := strings.Split(info, ":")
  1381  	// workflowID can contain ":" so len(ss) can be greater than 3
  1382  	if len(ss) < numItemsInGarbageInfo {
  1383  		return "", "", "", fmt.Errorf("not able to split info for  %s", info)
  1384  	}
  1385  	namespaceID = ss[0]
  1386  	runID = ss[len(ss)-1]
  1387  	workflowEnd := len(info) - len(runID) - 1
  1388  	workflowID = info[len(namespaceID)+1 : workflowEnd]
  1389  	return
  1390  }
  1391  
  1392  type ServiceType int
  1393  
  1394  const (
  1395  	All ServiceType = iota
  1396  	Frontend
  1397  	History
  1398  	Matching
  1399  	Worker
  1400  	InternalFrontend
  1401  )