go.temporal.io/server@v1.23.0/common/persistence/visibility/manager/visibility_manager.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  package manager
    26  
    27  // -aux_files is required here due to Closeable interface being in another file.
    28  //go:generate mockgen -copyright_file ../../../../LICENSE -package $GOPACKAGE -source $GOFILE -destination visibility_manager_mock.go -aux_files go.temporal.io/server/common/persistence=../../data_interfaces.go
    29  
    30  import (
    31  	"context"
    32  	"time"
    33  
    34  	commonpb "go.temporal.io/api/common/v1"
    35  	enumspb "go.temporal.io/api/enums/v1"
    36  	workflowpb "go.temporal.io/api/workflow/v1"
    37  	"go.temporal.io/api/workflowservice/v1"
    38  
    39  	"go.temporal.io/server/common/namespace"
    40  	"go.temporal.io/server/common/persistence"
    41  )
    42  
    43  type (
    44  	// VisibilityManager is used to manage the visibility store
    45  	VisibilityManager interface {
    46  		persistence.Closeable
    47  		GetReadStoreName(nsName namespace.Name) string
    48  		GetStoreNames() []string
    49  		HasStoreName(stName string) bool
    50  		GetIndexName() string
    51  		ValidateCustomSearchAttributes(searchAttributes map[string]any) (map[string]any, error)
    52  
    53  		// Write APIs.
    54  		RecordWorkflowExecutionStarted(ctx context.Context, request *RecordWorkflowExecutionStartedRequest) error
    55  		RecordWorkflowExecutionClosed(ctx context.Context, request *RecordWorkflowExecutionClosedRequest) error
    56  		UpsertWorkflowExecution(ctx context.Context, request *UpsertWorkflowExecutionRequest) error
    57  		DeleteWorkflowExecution(ctx context.Context, request *VisibilityDeleteWorkflowExecutionRequest) error
    58  
    59  		// Read APIs.
    60  		ListOpenWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequest) (*ListWorkflowExecutionsResponse, error)
    61  		ListClosedWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequest) (*ListWorkflowExecutionsResponse, error)
    62  		ListOpenWorkflowExecutionsByType(ctx context.Context, request *ListWorkflowExecutionsByTypeRequest) (*ListWorkflowExecutionsResponse, error)
    63  		ListClosedWorkflowExecutionsByType(ctx context.Context, request *ListWorkflowExecutionsByTypeRequest) (*ListWorkflowExecutionsResponse, error)
    64  		ListOpenWorkflowExecutionsByWorkflowID(ctx context.Context, request *ListWorkflowExecutionsByWorkflowIDRequest) (*ListWorkflowExecutionsResponse, error)
    65  		ListClosedWorkflowExecutionsByWorkflowID(ctx context.Context, request *ListWorkflowExecutionsByWorkflowIDRequest) (*ListWorkflowExecutionsResponse, error)
    66  		ListClosedWorkflowExecutionsByStatus(ctx context.Context, request *ListClosedWorkflowExecutionsByStatusRequest) (*ListWorkflowExecutionsResponse, error)
    67  		ListWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequestV2) (*ListWorkflowExecutionsResponse, error)
    68  		ScanWorkflowExecutions(ctx context.Context, request *ListWorkflowExecutionsRequestV2) (*ListWorkflowExecutionsResponse, error)
    69  		CountWorkflowExecutions(ctx context.Context, request *CountWorkflowExecutionsRequest) (*CountWorkflowExecutionsResponse, error)
    70  		GetWorkflowExecution(ctx context.Context, request *GetWorkflowExecutionRequest) (*GetWorkflowExecutionResponse, error)
    71  	}
    72  
    73  	VisibilityRequestBase struct {
    74  		NamespaceID      namespace.ID
    75  		Namespace        namespace.Name // namespace.Name is not persisted.
    76  		Execution        *commonpb.WorkflowExecution
    77  		WorkflowTypeName string
    78  		StartTime        time.Time
    79  		Status           enumspb.WorkflowExecutionStatus
    80  		ExecutionTime    time.Time
    81  		TaskID           int64 // not persisted, used as condition update version for ES
    82  		ShardID          int32 // not persisted
    83  		Memo             *commonpb.Memo
    84  		TaskQueue        string
    85  		SearchAttributes *commonpb.SearchAttributes
    86  		ParentExecution  *commonpb.WorkflowExecution
    87  	}
    88  
    89  	// RecordWorkflowExecutionStartedRequest is used to add a record of a newly started execution
    90  	RecordWorkflowExecutionStartedRequest struct {
    91  		*VisibilityRequestBase
    92  	}
    93  
    94  	// RecordWorkflowExecutionClosedRequest is used to add a record of a closed execution
    95  	RecordWorkflowExecutionClosedRequest struct {
    96  		*VisibilityRequestBase
    97  		CloseTime            time.Time
    98  		HistoryLength        int64
    99  		HistorySizeBytes     int64
   100  		StateTransitionCount int64
   101  	}
   102  
   103  	// UpsertWorkflowExecutionRequest is used to upsert workflow execution
   104  	UpsertWorkflowExecutionRequest struct {
   105  		*VisibilityRequestBase
   106  	}
   107  
   108  	// ListWorkflowExecutionsRequest is used to list executions in a namespace
   109  	ListWorkflowExecutionsRequest struct {
   110  		NamespaceID       namespace.ID
   111  		Namespace         namespace.Name // namespace.Name is not persisted.
   112  		NamespaceDivision string
   113  		EarliestStartTime time.Time
   114  		LatestStartTime   time.Time
   115  		// Maximum number of workflow executions per page
   116  		PageSize int
   117  		// Token to continue reading next page of workflow executions.
   118  		// Pass in empty slice for first page.
   119  		NextPageToken []byte
   120  	}
   121  
   122  	// ListWorkflowExecutionsRequestV2 is used to list executions in a namespace
   123  	ListWorkflowExecutionsRequestV2 struct {
   124  		NamespaceID namespace.ID
   125  		Namespace   namespace.Name // namespace.Name is not persisted.
   126  		PageSize    int            // Maximum number of workflow executions per page
   127  		// Token to continue reading next page of workflow executions.
   128  		// Pass in empty slice for first page.
   129  		NextPageToken []byte
   130  		Query         string
   131  	}
   132  
   133  	// ListWorkflowExecutionsResponse is the response to ListWorkflowExecutionsRequest
   134  	ListWorkflowExecutionsResponse struct {
   135  		Executions []*workflowpb.WorkflowExecutionInfo
   136  		// Token to read next page if there are more workflow executions beyond page size.
   137  		// Use this to set NextPageToken on ListWorkflowExecutionsRequest to read the next page.
   138  		NextPageToken []byte
   139  	}
   140  
   141  	// CountWorkflowExecutionsRequest is request from CountWorkflowExecutions
   142  	CountWorkflowExecutionsRequest struct {
   143  		NamespaceID namespace.ID
   144  		Namespace   namespace.Name // namespace.Name is not persisted.
   145  		Query       string
   146  	}
   147  
   148  	// CountWorkflowExecutionsResponse is response to CountWorkflowExecutions
   149  	CountWorkflowExecutionsResponse struct {
   150  		Count  int64 // sum of counts in Groups
   151  		Groups []*workflowservice.CountWorkflowExecutionsResponse_AggregationGroup
   152  	}
   153  
   154  	// ListWorkflowExecutionsByTypeRequest is used to list executions of
   155  	// a specific type in a namespace
   156  	ListWorkflowExecutionsByTypeRequest struct {
   157  		*ListWorkflowExecutionsRequest
   158  		WorkflowTypeName string
   159  	}
   160  
   161  	// ListWorkflowExecutionsByWorkflowIDRequest is used to list executions that
   162  	// have specific WorkflowID in a namespace
   163  	ListWorkflowExecutionsByWorkflowIDRequest struct {
   164  		*ListWorkflowExecutionsRequest
   165  		WorkflowID string
   166  	}
   167  
   168  	// ListClosedWorkflowExecutionsByStatusRequest is used to list executions that
   169  	// have specific close status
   170  	ListClosedWorkflowExecutionsByStatusRequest struct {
   171  		*ListWorkflowExecutionsRequest
   172  		Status enumspb.WorkflowExecutionStatus
   173  	}
   174  
   175  	// VisibilityDeleteWorkflowExecutionRequest contains the request params for DeleteWorkflowExecution call
   176  	VisibilityDeleteWorkflowExecutionRequest struct {
   177  		NamespaceID namespace.ID
   178  		RunID       string
   179  		WorkflowID  string
   180  		TaskID      int64
   181  		StartTime   time.Time // if start time is not empty, delete record from open_execution for cassandra db
   182  		CloseTime   time.Time // if end time is not empty, delete record from closed_execution for cassandra db
   183  	}
   184  
   185  	// GetWorkflowExecutionRequest is request from GetWorkflowExecution
   186  	GetWorkflowExecutionRequest struct {
   187  		NamespaceID namespace.ID
   188  		Namespace   namespace.Name // namespace.Name is not persisted
   189  		RunID       string
   190  		WorkflowID  string
   191  		StartTime   time.Time // if start time is not empty, search record from open_execution for cassandra db
   192  		CloseTime   time.Time // if end time is not empty, search record from closed_execution for cassandra db
   193  	}
   194  
   195  	// GetWorkflowExecutionResponse is response to GetWorkflowExecution
   196  	GetWorkflowExecutionResponse struct {
   197  		Execution *workflowpb.WorkflowExecutionInfo
   198  	}
   199  )
   200  
   201  func (r *ListWorkflowExecutionsRequest) OverrideToken(token []byte) {
   202  	r.NextPageToken = token
   203  }
   204  
   205  func (r *ListWorkflowExecutionsRequest) GetToken() []byte {
   206  	return r.NextPageToken
   207  }
   208  func (r *ListWorkflowExecutionsRequest) OverridePageSize(pageSize int) {
   209  	r.PageSize = pageSize
   210  }
   211  func (r *ListWorkflowExecutionsRequest) GetPageSize() int {
   212  	return r.PageSize
   213  }