go.temporal.io/server@v1.23.0/common/persistence/sql/execution.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 sql
    26  
    27  import (
    28  	"bytes"
    29  	"context"
    30  	"database/sql"
    31  	"fmt"
    32  
    33  	"go.temporal.io/api/serviceerror"
    34  
    35  	enumsspb "go.temporal.io/server/api/enums/v1"
    36  	"go.temporal.io/server/api/persistence/v1"
    37  	"go.temporal.io/server/common/log"
    38  	p "go.temporal.io/server/common/persistence"
    39  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    40  	"go.temporal.io/server/common/primitives"
    41  )
    42  
    43  type sqlExecutionStore struct {
    44  	SqlStore
    45  	p.HistoryBranchUtilImpl
    46  }
    47  
    48  var _ p.ExecutionStore = (*sqlExecutionStore)(nil)
    49  
    50  // NewSQLExecutionStore creates an instance of ExecutionStore
    51  func NewSQLExecutionStore(
    52  	db sqlplugin.DB,
    53  	logger log.Logger,
    54  ) (p.ExecutionStore, error) {
    55  
    56  	return &sqlExecutionStore{
    57  		SqlStore: NewSqlStore(db, logger),
    58  	}, nil
    59  }
    60  
    61  // txExecuteShardLocked executes f under transaction and with read lock on shard row
    62  func (m *sqlExecutionStore) txExecuteShardLocked(
    63  	ctx context.Context,
    64  	operation string,
    65  	shardID int32,
    66  	rangeID int64,
    67  	fn func(tx sqlplugin.Tx) error,
    68  ) error {
    69  
    70  	return m.txExecute(ctx, operation, func(tx sqlplugin.Tx) error {
    71  		if err := readLockShard(ctx, tx, shardID, rangeID); err != nil {
    72  			return err
    73  		}
    74  		err := fn(tx)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		return nil
    79  	})
    80  }
    81  
    82  func (m *sqlExecutionStore) CreateWorkflowExecution(
    83  	ctx context.Context,
    84  	request *p.InternalCreateWorkflowExecutionRequest,
    85  ) (response *p.InternalCreateWorkflowExecutionResponse, err error) {
    86  	for _, req := range request.NewWorkflowNewEvents {
    87  		if err := m.AppendHistoryNodes(ctx, req); err != nil {
    88  			return nil, err
    89  		}
    90  	}
    91  
    92  	err = m.txExecuteShardLocked(ctx,
    93  		"CreateWorkflowExecution",
    94  		request.ShardID,
    95  		request.RangeID,
    96  		func(tx sqlplugin.Tx) error {
    97  			response, err = m.createWorkflowExecutionTx(ctx, tx, request)
    98  			return err
    99  		})
   100  	return
   101  }
   102  
   103  func (m *sqlExecutionStore) createWorkflowExecutionTx(
   104  	ctx context.Context,
   105  	tx sqlplugin.Tx,
   106  	request *p.InternalCreateWorkflowExecutionRequest,
   107  ) (*p.InternalCreateWorkflowExecutionResponse, error) {
   108  
   109  	newWorkflow := request.NewWorkflowSnapshot
   110  	lastWriteVersion := newWorkflow.LastWriteVersion
   111  	shardID := request.ShardID
   112  	namespaceID := primitives.MustParseUUID(newWorkflow.NamespaceID)
   113  	workflowID := newWorkflow.WorkflowID
   114  	runID := primitives.MustParseUUID(newWorkflow.RunID)
   115  
   116  	var err error
   117  	var currentRow *sqlplugin.CurrentExecutionsRow
   118  	if currentRow, err = lockCurrentExecutionIfExists(ctx,
   119  		tx,
   120  		shardID,
   121  		namespaceID,
   122  		workflowID,
   123  	); err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	// current run ID, last write version, current workflow state check
   128  	switch request.Mode {
   129  	case p.CreateWorkflowModeBrandNew:
   130  		if currentRow == nil {
   131  			// current row does not exists, suits the create mode
   132  		} else {
   133  			if currentRow.RunID.String() != request.PreviousRunID {
   134  				return nil, extractCurrentWorkflowConflictError(
   135  					currentRow,
   136  					fmt.Sprintf(
   137  						"Workflow execution creation condition failed. workflow ID: %v, current run ID: %v, request run ID: %v",
   138  						workflowID,
   139  						currentRow.RunID.String(),
   140  						request.PreviousRunID,
   141  					),
   142  				)
   143  			}
   144  			// current run ID is already request ID
   145  		}
   146  
   147  	case p.CreateWorkflowModeUpdateCurrent:
   148  		if currentRow == nil {
   149  			return nil, extractCurrentWorkflowConflictError(currentRow, "")
   150  		}
   151  
   152  		// currentRow != nil
   153  
   154  		if currentRow.RunID.String() != request.PreviousRunID {
   155  			return nil, extractCurrentWorkflowConflictError(
   156  				currentRow,
   157  				fmt.Sprintf(
   158  					"Workflow execution creation condition failed. workflow ID: %v, current run ID: %v, request run ID: %v",
   159  					workflowID,
   160  					currentRow.RunID.String(),
   161  					request.PreviousRunID,
   162  				),
   163  			)
   164  		}
   165  		if request.PreviousLastWriteVersion != currentRow.LastWriteVersion {
   166  			return nil, extractCurrentWorkflowConflictError(
   167  				currentRow,
   168  				fmt.Sprintf(
   169  					"Workflow execution creation condition failed. workflow ID: %v, current last write version: %v, request last write version: %v",
   170  					workflowID,
   171  					currentRow.LastWriteVersion,
   172  					request.PreviousLastWriteVersion,
   173  				),
   174  			)
   175  		}
   176  		if currentRow.State != enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED {
   177  			return nil, extractCurrentWorkflowConflictError(
   178  				currentRow,
   179  				fmt.Sprintf(
   180  					"Workflow execution creation condition failed. workflow ID: %v, current state: %v, request state: %v",
   181  					workflowID,
   182  					currentRow.State,
   183  					enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED,
   184  				),
   185  			)
   186  		}
   187  
   188  	case p.CreateWorkflowModeBypassCurrent:
   189  		if err := assertRunIDMismatch(
   190  			primitives.MustParseUUID(newWorkflow.ExecutionState.RunId),
   191  			currentRow,
   192  		); err != nil {
   193  			return nil, err
   194  		}
   195  
   196  	default:
   197  		return nil, serviceerror.NewInternal(fmt.Sprintf("CreteWorkflowExecution: unknown mode: %v", request.Mode))
   198  	}
   199  
   200  	if err := createOrUpdateCurrentExecution(ctx,
   201  		tx,
   202  		request.Mode,
   203  		request.ShardID,
   204  		namespaceID,
   205  		workflowID,
   206  		runID,
   207  		newWorkflow.ExecutionState.State,
   208  		newWorkflow.ExecutionState.Status,
   209  		newWorkflow.ExecutionState.CreateRequestId,
   210  		lastWriteVersion,
   211  	); err != nil {
   212  		return nil, err
   213  	}
   214  
   215  	if err := m.applyWorkflowSnapshotTxAsNew(ctx,
   216  		tx,
   217  		shardID,
   218  		&request.NewWorkflowSnapshot,
   219  	); err != nil {
   220  		return nil, err
   221  	}
   222  
   223  	return &p.InternalCreateWorkflowExecutionResponse{}, nil
   224  }
   225  
   226  func (m *sqlExecutionStore) GetWorkflowExecution(
   227  	ctx context.Context,
   228  	request *p.GetWorkflowExecutionRequest,
   229  ) (*p.InternalGetWorkflowExecutionResponse, error) {
   230  	namespaceID := primitives.MustParseUUID(request.NamespaceID)
   231  	workflowID := request.WorkflowID
   232  	runID := primitives.MustParseUUID(request.RunID)
   233  	executionsRow, err := m.Db.SelectFromExecutions(ctx, sqlplugin.ExecutionsFilter{
   234  		ShardID:     request.ShardID,
   235  		NamespaceID: namespaceID,
   236  		WorkflowID:  workflowID,
   237  		RunID:       runID,
   238  	})
   239  	switch err {
   240  	case nil:
   241  		// noop
   242  	case sql.ErrNoRows:
   243  		return nil, serviceerror.NewNotFound(fmt.Sprintf("Workflow executionsRow not found.  WorkflowId: %v, RunId: %v", workflowID, runID))
   244  	default:
   245  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed. Error: %v", err))
   246  	}
   247  
   248  	state := &p.InternalWorkflowMutableState{
   249  		ExecutionInfo:  p.NewDataBlob(executionsRow.Data, executionsRow.DataEncoding),
   250  		ExecutionState: p.NewDataBlob(executionsRow.State, executionsRow.StateEncoding),
   251  		NextEventID:    executionsRow.NextEventID,
   252  
   253  		DBRecordVersion: executionsRow.DBRecordVersion,
   254  	}
   255  
   256  	state.ActivityInfos, err = getActivityInfoMap(ctx,
   257  		m.Db,
   258  		request.ShardID,
   259  		namespaceID,
   260  		workflowID,
   261  		runID,
   262  	)
   263  	if err != nil {
   264  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed to get activity info. Error: %v", err))
   265  	}
   266  
   267  	state.TimerInfos, err = getTimerInfoMap(ctx,
   268  		m.Db,
   269  		request.ShardID,
   270  		namespaceID,
   271  		workflowID,
   272  		runID,
   273  	)
   274  	if err != nil {
   275  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed to get timer info. Error: %v", err))
   276  	}
   277  
   278  	state.ChildExecutionInfos, err = getChildExecutionInfoMap(ctx,
   279  		m.Db,
   280  		request.ShardID,
   281  		namespaceID,
   282  		workflowID,
   283  		runID,
   284  	)
   285  	if err != nil {
   286  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed to get child executionsRow info. Error: %v", err))
   287  	}
   288  
   289  	state.RequestCancelInfos, err = getRequestCancelInfoMap(ctx,
   290  		m.Db,
   291  		request.ShardID,
   292  		namespaceID,
   293  		workflowID,
   294  		runID,
   295  	)
   296  	if err != nil {
   297  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed to get request cancel info. Error: %v", err))
   298  	}
   299  
   300  	state.SignalInfos, err = getSignalInfoMap(ctx,
   301  		m.Db,
   302  		request.ShardID,
   303  		namespaceID,
   304  		workflowID,
   305  		runID,
   306  	)
   307  	if err != nil {
   308  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed to get signal info. Error: %v", err))
   309  	}
   310  
   311  	state.BufferedEvents, err = getBufferedEvents(ctx,
   312  		m.Db,
   313  		request.ShardID,
   314  		namespaceID,
   315  		workflowID,
   316  		runID,
   317  	)
   318  	if err != nil {
   319  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed to get buffered events. Error: %v", err))
   320  	}
   321  
   322  	state.SignalRequestedIDs, err = getSignalsRequested(ctx,
   323  		m.Db,
   324  		request.ShardID,
   325  		namespaceID,
   326  		workflowID,
   327  		runID,
   328  	)
   329  	if err != nil {
   330  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetWorkflowExecution: failed to get signals requested. Error: %v", err))
   331  	}
   332  
   333  	return &p.InternalGetWorkflowExecutionResponse{
   334  		State:           state,
   335  		DBRecordVersion: executionsRow.DBRecordVersion,
   336  	}, nil
   337  }
   338  
   339  func (m *sqlExecutionStore) UpdateWorkflowExecution(
   340  	ctx context.Context,
   341  	request *p.InternalUpdateWorkflowExecutionRequest,
   342  ) error {
   343  	// first append history
   344  	for _, req := range request.UpdateWorkflowNewEvents {
   345  		if err := m.AppendHistoryNodes(ctx, req); err != nil {
   346  			return err
   347  		}
   348  	}
   349  	for _, req := range request.NewWorkflowNewEvents {
   350  		if err := m.AppendHistoryNodes(ctx, req); err != nil {
   351  			return err
   352  		}
   353  	}
   354  
   355  	// then update mutable state
   356  	return m.txExecuteShardLocked(ctx,
   357  		"UpdateWorkflowExecution",
   358  		request.ShardID,
   359  		request.RangeID,
   360  		func(tx sqlplugin.Tx) error {
   361  			return m.updateWorkflowExecutionTx(ctx, tx, request)
   362  		})
   363  }
   364  
   365  func (m *sqlExecutionStore) updateWorkflowExecutionTx(
   366  	ctx context.Context,
   367  	tx sqlplugin.Tx,
   368  	request *p.InternalUpdateWorkflowExecutionRequest,
   369  ) error {
   370  
   371  	updateWorkflow := request.UpdateWorkflowMutation
   372  	newWorkflow := request.NewWorkflowSnapshot
   373  
   374  	namespaceID := primitives.MustParseUUID(updateWorkflow.NamespaceID)
   375  	workflowID := updateWorkflow.WorkflowID
   376  	runID := primitives.MustParseUUID(updateWorkflow.ExecutionState.RunId)
   377  	shardID := request.ShardID
   378  
   379  	switch request.Mode {
   380  	case p.UpdateWorkflowModeBypassCurrent:
   381  		if err := assertNotCurrentExecution(ctx,
   382  			tx,
   383  			shardID,
   384  			namespaceID,
   385  			workflowID,
   386  			runID,
   387  		); err != nil {
   388  			return err
   389  		}
   390  
   391  	case p.UpdateWorkflowModeUpdateCurrent:
   392  		if newWorkflow != nil {
   393  			lastWriteVersion := newWorkflow.LastWriteVersion
   394  			newNamespaceID := primitives.MustParseUUID(newWorkflow.NamespaceID)
   395  			newRunID := primitives.MustParseUUID(newWorkflow.ExecutionState.RunId)
   396  
   397  			if !bytes.Equal(namespaceID, newNamespaceID) {
   398  				return serviceerror.NewUnavailable("UpdateWorkflowExecution: cannot continue as new to another namespace")
   399  			}
   400  
   401  			if err := assertRunIDAndUpdateCurrentExecution(ctx,
   402  				tx,
   403  				shardID,
   404  				namespaceID,
   405  				workflowID,
   406  				newRunID,
   407  				runID,
   408  				newWorkflow.ExecutionState.CreateRequestId,
   409  				newWorkflow.ExecutionState.State,
   410  				newWorkflow.ExecutionState.Status,
   411  				lastWriteVersion,
   412  			); err != nil {
   413  				return err
   414  			}
   415  		} else {
   416  			lastWriteVersion := updateWorkflow.LastWriteVersion
   417  			// this is only to update the current record
   418  			if err := assertRunIDAndUpdateCurrentExecution(ctx,
   419  				tx,
   420  				shardID,
   421  				namespaceID,
   422  				workflowID,
   423  				runID,
   424  				runID,
   425  				updateWorkflow.ExecutionState.CreateRequestId,
   426  				updateWorkflow.ExecutionState.State,
   427  				updateWorkflow.ExecutionState.Status,
   428  				lastWriteVersion,
   429  			); err != nil {
   430  				return err
   431  			}
   432  		}
   433  
   434  	default:
   435  		return serviceerror.NewUnavailable(fmt.Sprintf("UpdateWorkflowExecution: unknown mode: %v", request.Mode))
   436  	}
   437  
   438  	if err := applyWorkflowMutationTx(ctx,
   439  		tx,
   440  		shardID,
   441  		&updateWorkflow,
   442  	); err != nil {
   443  		return err
   444  	}
   445  
   446  	if newWorkflow != nil {
   447  		if err := m.applyWorkflowSnapshotTxAsNew(ctx,
   448  			tx,
   449  			shardID,
   450  			newWorkflow,
   451  		); err != nil {
   452  			return err
   453  		}
   454  	}
   455  	return nil
   456  }
   457  
   458  func (m *sqlExecutionStore) ConflictResolveWorkflowExecution(
   459  	ctx context.Context,
   460  	request *p.InternalConflictResolveWorkflowExecutionRequest,
   461  ) error {
   462  	// first append history
   463  	for _, req := range request.CurrentWorkflowEventsNewEvents {
   464  		if err := m.AppendHistoryNodes(ctx, req); err != nil {
   465  			return err
   466  		}
   467  	}
   468  	for _, req := range request.ResetWorkflowEventsNewEvents {
   469  		if err := m.AppendHistoryNodes(ctx, req); err != nil {
   470  			return err
   471  		}
   472  	}
   473  	for _, req := range request.NewWorkflowEventsNewEvents {
   474  		if err := m.AppendHistoryNodes(ctx, req); err != nil {
   475  			return err
   476  		}
   477  	}
   478  
   479  	return m.txExecuteShardLocked(ctx,
   480  		"ConflictResolveWorkflowExecution",
   481  		request.ShardID,
   482  		request.RangeID,
   483  		func(tx sqlplugin.Tx) error {
   484  			return m.conflictResolveWorkflowExecutionTx(ctx, tx, request)
   485  		})
   486  }
   487  
   488  func (m *sqlExecutionStore) conflictResolveWorkflowExecutionTx(
   489  	ctx context.Context,
   490  	tx sqlplugin.Tx,
   491  	request *p.InternalConflictResolveWorkflowExecutionRequest,
   492  ) error {
   493  
   494  	currentWorkflow := request.CurrentWorkflowMutation
   495  	resetWorkflow := request.ResetWorkflowSnapshot
   496  	newWorkflow := request.NewWorkflowSnapshot
   497  
   498  	shardID := request.ShardID
   499  
   500  	namespaceID := primitives.MustParseUUID(resetWorkflow.NamespaceID)
   501  	workflowID := resetWorkflow.WorkflowID
   502  
   503  	switch request.Mode {
   504  	case p.ConflictResolveWorkflowModeBypassCurrent:
   505  		if err := assertNotCurrentExecution(ctx,
   506  			tx,
   507  			shardID,
   508  			namespaceID,
   509  			workflowID,
   510  			primitives.MustParseUUID(resetWorkflow.ExecutionState.RunId),
   511  		); err != nil {
   512  			return err
   513  		}
   514  
   515  	case p.ConflictResolveWorkflowModeUpdateCurrent:
   516  		executionState := resetWorkflow.ExecutionState
   517  		lastWriteVersion := resetWorkflow.LastWriteVersion
   518  		if newWorkflow != nil {
   519  			executionState = newWorkflow.ExecutionState
   520  			lastWriteVersion = newWorkflow.LastWriteVersion
   521  		}
   522  		runID := primitives.MustParseUUID(executionState.RunId)
   523  		createRequestID := executionState.CreateRequestId
   524  		state := executionState.State
   525  		status := executionState.Status
   526  
   527  		if currentWorkflow != nil {
   528  			prevRunID := primitives.MustParseUUID(currentWorkflow.ExecutionState.RunId)
   529  
   530  			if err := assertRunIDAndUpdateCurrentExecution(ctx,
   531  				tx,
   532  				shardID,
   533  				namespaceID,
   534  				workflowID,
   535  				runID,
   536  				prevRunID,
   537  				createRequestID,
   538  				state,
   539  				status,
   540  				lastWriteVersion,
   541  			); err != nil {
   542  				return err
   543  			}
   544  		} else {
   545  			// reset workflow is current
   546  			prevRunID := primitives.MustParseUUID(resetWorkflow.ExecutionState.RunId)
   547  
   548  			if err := assertRunIDAndUpdateCurrentExecution(ctx,
   549  				tx,
   550  				shardID,
   551  				namespaceID,
   552  				workflowID,
   553  				runID,
   554  				prevRunID,
   555  				createRequestID,
   556  				state,
   557  				status,
   558  				lastWriteVersion,
   559  			); err != nil {
   560  				return err
   561  			}
   562  		}
   563  
   564  	default:
   565  		return serviceerror.NewUnavailable(fmt.Sprintf("ConflictResolveWorkflowExecution: unknown mode: %v", request.Mode))
   566  	}
   567  
   568  	if err := applyWorkflowSnapshotTxAsReset(ctx,
   569  		tx,
   570  		shardID,
   571  		&resetWorkflow,
   572  	); err != nil {
   573  		return err
   574  	}
   575  
   576  	if currentWorkflow != nil {
   577  		if err := applyWorkflowMutationTx(ctx,
   578  			tx,
   579  			shardID,
   580  			currentWorkflow,
   581  		); err != nil {
   582  			return err
   583  		}
   584  	}
   585  
   586  	if newWorkflow != nil {
   587  		if err := m.applyWorkflowSnapshotTxAsNew(ctx,
   588  			tx,
   589  			shardID,
   590  			newWorkflow,
   591  		); err != nil {
   592  			return err
   593  		}
   594  	}
   595  	return nil
   596  }
   597  
   598  func (m *sqlExecutionStore) DeleteWorkflowExecution(
   599  	ctx context.Context,
   600  	request *p.DeleteWorkflowExecutionRequest,
   601  ) error {
   602  	namespaceID := primitives.MustParseUUID(request.NamespaceID)
   603  	runID := primitives.MustParseUUID(request.RunID)
   604  	return m.txExecute(ctx, "DeleteWorkflowExecution", func(tx sqlplugin.Tx) error {
   605  		_, err := tx.DeleteAllFromChildExecutionInfoMaps(ctx, sqlplugin.ChildExecutionInfoMapsAllFilter{
   606  			ShardID:     request.ShardID,
   607  			NamespaceID: namespaceID,
   608  			WorkflowID:  request.WorkflowID,
   609  			RunID:       runID,
   610  		})
   611  		if err != nil {
   612  			return fmt.Errorf("failed to execute DeleteAllFromChildExecutionInfoMaps: %w", err)
   613  		}
   614  		_, err = tx.DeleteAllFromActivityInfoMaps(ctx, sqlplugin.ActivityInfoMapsAllFilter{
   615  			ShardID:     request.ShardID,
   616  			NamespaceID: namespaceID,
   617  			WorkflowID:  request.WorkflowID,
   618  			RunID:       runID,
   619  		})
   620  		if err != nil {
   621  			return fmt.Errorf("failed to execute DeleteAllFromActivityInfoMaps: %w", err)
   622  		}
   623  		_, err = tx.DeleteAllFromRequestCancelInfoMaps(ctx, sqlplugin.RequestCancelInfoMapsAllFilter{
   624  			ShardID:     request.ShardID,
   625  			NamespaceID: namespaceID,
   626  			WorkflowID:  request.WorkflowID,
   627  			RunID:       runID,
   628  		})
   629  		if err != nil {
   630  			return fmt.Errorf("failed to execute DeleteAllFromRequestCancelInfoMaps: %w", err)
   631  		}
   632  		_, err = tx.DeleteAllFromSignalInfoMaps(ctx, sqlplugin.SignalInfoMapsAllFilter{
   633  			ShardID:     request.ShardID,
   634  			NamespaceID: namespaceID,
   635  			WorkflowID:  request.WorkflowID,
   636  			RunID:       runID,
   637  		})
   638  		if err != nil {
   639  			return fmt.Errorf("failed to execute DeleteAllFromSignalInfoMaps: %w", err)
   640  		}
   641  		_, err = tx.DeleteAllFromTimerInfoMaps(ctx, sqlplugin.TimerInfoMapsAllFilter{
   642  			ShardID:     request.ShardID,
   643  			NamespaceID: namespaceID,
   644  			WorkflowID:  request.WorkflowID,
   645  			RunID:       runID,
   646  		})
   647  		if err != nil {
   648  			return fmt.Errorf("failed to execute DeleteAllFromTimerInfoMaps: %w", err)
   649  		}
   650  		_, err = tx.DeleteAllFromSignalsRequestedSets(ctx, sqlplugin.SignalsRequestedSetsAllFilter{
   651  			ShardID:     request.ShardID,
   652  			NamespaceID: namespaceID,
   653  			WorkflowID:  request.WorkflowID,
   654  			RunID:       runID,
   655  		})
   656  		if err != nil {
   657  			return fmt.Errorf("failed to execute DeleteAllFromSignalsRequestedSets: %w", err)
   658  		}
   659  		_, err = tx.DeleteFromBufferedEvents(ctx, sqlplugin.BufferedEventsFilter{
   660  			ShardID:     request.ShardID,
   661  			NamespaceID: namespaceID,
   662  			WorkflowID:  request.WorkflowID,
   663  			RunID:       runID,
   664  		})
   665  		if err != nil {
   666  			return fmt.Errorf("failed to execute DeleteFromBufferedEvents: %w", err)
   667  		}
   668  		_, err = tx.DeleteFromExecutions(ctx, sqlplugin.ExecutionsFilter{
   669  			ShardID:     request.ShardID,
   670  			NamespaceID: namespaceID,
   671  			WorkflowID:  request.WorkflowID,
   672  			RunID:       runID,
   673  		})
   674  		if err != nil {
   675  			return fmt.Errorf("failed to execute DeleteFromExecutions: %w", err)
   676  		}
   677  		return nil
   678  	})
   679  }
   680  
   681  // its possible for a new run of the same workflow to have started after the run we are deleting
   682  // here was finished. In that case, current_executions table will have the same workflowID but different
   683  // runID. The following code will delete the row from current_executions if and only if the runID is
   684  // same as the one we are trying to delete here
   685  func (m *sqlExecutionStore) DeleteCurrentWorkflowExecution(
   686  	ctx context.Context,
   687  	request *p.DeleteCurrentWorkflowExecutionRequest,
   688  ) error {
   689  	namespaceID := primitives.MustParseUUID(request.NamespaceID)
   690  	runID := primitives.MustParseUUID(request.RunID)
   691  	_, err := m.Db.DeleteFromCurrentExecutions(ctx, sqlplugin.CurrentExecutionsFilter{
   692  		ShardID:     request.ShardID,
   693  		NamespaceID: namespaceID,
   694  		WorkflowID:  request.WorkflowID,
   695  		RunID:       runID,
   696  	})
   697  	return err
   698  }
   699  
   700  func (m *sqlExecutionStore) GetCurrentExecution(
   701  	ctx context.Context,
   702  	request *p.GetCurrentExecutionRequest,
   703  ) (*p.InternalGetCurrentExecutionResponse, error) {
   704  	row, err := m.Db.SelectFromCurrentExecutions(ctx, sqlplugin.CurrentExecutionsFilter{
   705  		ShardID:     request.ShardID,
   706  		NamespaceID: primitives.MustParseUUID(request.NamespaceID),
   707  		WorkflowID:  request.WorkflowID,
   708  	})
   709  	if err != nil {
   710  		if err == sql.ErrNoRows {
   711  			return nil, serviceerror.NewNotFound(err.Error())
   712  		}
   713  		return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetCurrentExecution operation failed. Error: %v", err))
   714  	}
   715  
   716  	return &p.InternalGetCurrentExecutionResponse{
   717  		RunID: row.RunID.String(),
   718  		ExecutionState: &persistence.WorkflowExecutionState{
   719  			CreateRequestId: row.CreateRequestID,
   720  			State:           row.State,
   721  			Status:          row.Status,
   722  		},
   723  	}, nil
   724  }
   725  
   726  func (m *sqlExecutionStore) SetWorkflowExecution(
   727  	ctx context.Context,
   728  	request *p.InternalSetWorkflowExecutionRequest,
   729  ) error {
   730  	return m.txExecuteShardLocked(ctx,
   731  		"SetWorkflowExecution",
   732  		request.ShardID,
   733  		request.RangeID,
   734  		func(tx sqlplugin.Tx) error {
   735  			return m.setWorkflowExecutionTx(ctx, tx, request)
   736  		})
   737  }
   738  
   739  func (m *sqlExecutionStore) setWorkflowExecutionTx(
   740  	ctx context.Context,
   741  	tx sqlplugin.Tx,
   742  	request *p.InternalSetWorkflowExecutionRequest,
   743  ) error {
   744  	shardID := request.ShardID
   745  	setSnapshot := request.SetWorkflowSnapshot
   746  
   747  	return applyWorkflowSnapshotTxAsReset(ctx,
   748  		tx,
   749  		shardID,
   750  		&setSnapshot,
   751  	)
   752  }
   753  
   754  func (m *sqlExecutionStore) ListConcreteExecutions(
   755  	_ context.Context,
   756  	_ *p.ListConcreteExecutionsRequest,
   757  ) (*p.InternalListConcreteExecutionsResponse, error) {
   758  	return nil, serviceerror.NewUnimplemented("ListConcreteExecutions is not implemented")
   759  }