go.temporal.io/server@v1.23.0/common/persistence/cassandra/execution_store.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 cassandra
    26  
    27  import (
    28  	"context"
    29  	"time"
    30  
    31  	"go.temporal.io/server/common/log"
    32  	p "go.temporal.io/server/common/persistence"
    33  	"go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
    34  )
    35  
    36  // Guidelines for creating new special UUID constants
    37  // Each UUID should be of the form: E0000000-R000-f000-f000-00000000000x
    38  // Where x is any hexadecimal value, E represents the entity type valid values are:
    39  // E = {NamespaceID = 1, WorkflowID = 2, RunID = 3}
    40  // R represents row type in executions table, valid values are:
    41  // R = {Shard = 1, Execution = 2, Transfer = 3, Timer = 4, Replication = 5}
    42  const (
    43  	// Special Run IDs
    44  	permanentRunID = "30000000-0000-f000-f000-000000000001"
    45  	// Row Constants for Shard Row
    46  	rowTypeShardNamespaceID = "10000000-1000-f000-f000-000000000000"
    47  	rowTypeShardWorkflowID  = "20000000-1000-f000-f000-000000000000"
    48  	rowTypeShardRunID       = "30000000-1000-f000-f000-000000000000"
    49  	// Row Constants for Transfer Task Row
    50  	rowTypeTransferNamespaceID = "10000000-3000-f000-f000-000000000000"
    51  	rowTypeTransferWorkflowID  = "20000000-3000-f000-f000-000000000000"
    52  	rowTypeTransferRunID       = "30000000-3000-f000-f000-000000000000"
    53  	// Row Constants for Timer Task Row
    54  	rowTypeTimerNamespaceID = "10000000-4000-f000-f000-000000000000"
    55  	rowTypeTimerWorkflowID  = "20000000-4000-f000-f000-000000000000"
    56  	rowTypeTimerRunID       = "30000000-4000-f000-f000-000000000000"
    57  	// Row Constants for Replication Task Row
    58  	rowTypeReplicationNamespaceID = "10000000-5000-f000-f000-000000000000"
    59  	rowTypeReplicationWorkflowID  = "20000000-5000-f000-f000-000000000000"
    60  	rowTypeReplicationRunID       = "30000000-5000-f000-f000-000000000000"
    61  	// Row constants for visibility task row.
    62  	rowTypeVisibilityTaskNamespaceID = "10000000-6000-f000-f000-000000000000"
    63  	rowTypeVisibilityTaskWorkflowID  = "20000000-6000-f000-f000-000000000000"
    64  	rowTypeVisibilityTaskRunID       = "30000000-6000-f000-f000-000000000000"
    65  	// Row Constants for Replication Task DLQ Row. Source cluster name will be used as WorkflowID.
    66  	rowTypeDLQNamespaceID = "10000000-6000-f000-f000-000000000000"
    67  	rowTypeDLQRunID       = "30000000-6000-f000-f000-000000000000"
    68  	// Row constants for History task row.
    69  	rowTypeHistoryTaskNamespaceID = "10000000-8000-f000-f000-000000000000"
    70  	rowTypeHistoryTaskWorkflowID  = "20000000-8000-f000-f000-000000000000"
    71  	rowTypeHistoryTaskRunID       = "30000000-8000-f000-f000-000000000000"
    72  	// Special TaskId constants
    73  	rowTypeExecutionTaskID = int64(-10)
    74  	rowTypeShardTaskID     = int64(-11)
    75  )
    76  
    77  const (
    78  	// Row types for table executions
    79  	rowTypeShard = iota
    80  	rowTypeExecution
    81  	rowTypeTransferTask
    82  	rowTypeTimerTask
    83  	rowTypeReplicationTask
    84  	rowTypeDLQ
    85  	rowTypeVisibilityTask
    86  	// NOTE: the row type for history task is the task category ID
    87  	// rowTypeHistoryTask
    88  )
    89  
    90  const (
    91  	// Row types for table tasks
    92  	rowTypeTask = iota
    93  	rowTypeTaskQueue
    94  )
    95  
    96  const (
    97  	taskQueueTaskID = -12345
    98  
    99  	// ref: https://docs.datastax.com/en/dse-trblshoot/doc/troubleshooting/recoveringTtlYear2038Problem.html
   100  	maxCassandraTTL = int64(315360000) // Cassandra max support time is 2038-01-19T03:14:06+00:00. Updated this to 10 years to support until year 2028
   101  )
   102  
   103  var (
   104  	defaultDateTime            = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
   105  	defaultVisibilityTimestamp = p.UnixMilliseconds(defaultDateTime)
   106  )
   107  
   108  type (
   109  	ExecutionStore struct {
   110  		*HistoryStore
   111  		*MutableStateStore
   112  		*MutableStateTaskStore
   113  	}
   114  )
   115  
   116  var _ p.ExecutionStore = (*ExecutionStore)(nil)
   117  
   118  func NewExecutionStore(
   119  	session gocql.Session,
   120  	logger log.Logger,
   121  ) *ExecutionStore {
   122  	return &ExecutionStore{
   123  		HistoryStore:          NewHistoryStore(session, logger),
   124  		MutableStateStore:     NewMutableStateStore(session, logger),
   125  		MutableStateTaskStore: NewMutableStateTaskStore(session, logger),
   126  	}
   127  }
   128  
   129  func (d *ExecutionStore) CreateWorkflowExecution(
   130  	ctx context.Context,
   131  	request *p.InternalCreateWorkflowExecutionRequest,
   132  ) (*p.InternalCreateWorkflowExecutionResponse, error) {
   133  	for _, req := range request.NewWorkflowNewEvents {
   134  		if err := d.AppendHistoryNodes(ctx, req); err != nil {
   135  			return nil, err
   136  		}
   137  	}
   138  
   139  	return d.MutableStateStore.CreateWorkflowExecution(ctx, request)
   140  }
   141  
   142  func (d *ExecutionStore) UpdateWorkflowExecution(
   143  	ctx context.Context,
   144  	request *p.InternalUpdateWorkflowExecutionRequest,
   145  ) error {
   146  	for _, req := range request.UpdateWorkflowNewEvents {
   147  		if err := d.AppendHistoryNodes(ctx, req); err != nil {
   148  			return err
   149  		}
   150  	}
   151  	for _, req := range request.NewWorkflowNewEvents {
   152  		if err := d.AppendHistoryNodes(ctx, req); err != nil {
   153  			return err
   154  		}
   155  	}
   156  
   157  	return d.MutableStateStore.UpdateWorkflowExecution(ctx, request)
   158  }
   159  
   160  func (d *ExecutionStore) ConflictResolveWorkflowExecution(
   161  	ctx context.Context,
   162  	request *p.InternalConflictResolveWorkflowExecutionRequest,
   163  ) error {
   164  	for _, req := range request.CurrentWorkflowEventsNewEvents {
   165  		if err := d.AppendHistoryNodes(ctx, req); err != nil {
   166  			return err
   167  		}
   168  	}
   169  	for _, req := range request.ResetWorkflowEventsNewEvents {
   170  		if err := d.AppendHistoryNodes(ctx, req); err != nil {
   171  			return err
   172  		}
   173  	}
   174  	for _, req := range request.NewWorkflowEventsNewEvents {
   175  		if err := d.AppendHistoryNodes(ctx, req); err != nil {
   176  			return err
   177  		}
   178  	}
   179  
   180  	return d.MutableStateStore.ConflictResolveWorkflowExecution(ctx, request)
   181  }
   182  
   183  func (d *ExecutionStore) GetName() string {
   184  	return cassandraPersistenceName
   185  }
   186  
   187  func (d *ExecutionStore) Close() {
   188  	if d.HistoryStore.Session != nil {
   189  		d.HistoryStore.Session.Close()
   190  	}
   191  	if d.MutableStateStore.Session != nil {
   192  		d.MutableStateStore.Session.Close()
   193  	}
   194  	if d.MutableStateTaskStore.Session != nil {
   195  		d.MutableStateTaskStore.Session.Close()
   196  	}
   197  }