go.temporal.io/server@v1.23.0/common/persistence/cassandra/util.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  	commonpb "go.temporal.io/api/common/v1"
    29  	enumspb "go.temporal.io/api/enums/v1"
    30  
    31  	persistencespb "go.temporal.io/server/api/persistence/v1"
    32  	"go.temporal.io/server/common/convert"
    33  	p "go.temporal.io/server/common/persistence"
    34  	"go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
    35  	"go.temporal.io/server/service/history/tasks"
    36  )
    37  
    38  func applyWorkflowMutationBatch(
    39  	batch gocql.Batch,
    40  	shardID int32,
    41  	workflowMutation *p.InternalWorkflowMutation,
    42  ) error {
    43  
    44  	// TODO update all call sites to update LastUpdatetime
    45  	// cqlNowTimestampMillis := p.UnixMilliseconds(time.Now().UTC())
    46  
    47  	namespaceID := workflowMutation.NamespaceID
    48  	workflowID := workflowMutation.WorkflowID
    49  	runID := workflowMutation.RunID
    50  
    51  	if err := updateExecution(
    52  		batch,
    53  		shardID,
    54  		namespaceID,
    55  		workflowID,
    56  		runID,
    57  		workflowMutation.ExecutionInfoBlob,
    58  		workflowMutation.ExecutionState,
    59  		workflowMutation.ExecutionStateBlob,
    60  		workflowMutation.NextEventID,
    61  		workflowMutation.Condition,
    62  		workflowMutation.DBRecordVersion,
    63  		workflowMutation.Checksum,
    64  	); err != nil {
    65  		return err
    66  	}
    67  
    68  	if err := updateActivityInfos(
    69  		batch,
    70  		workflowMutation.UpsertActivityInfos,
    71  		workflowMutation.DeleteActivityInfos,
    72  		shardID,
    73  		namespaceID,
    74  		workflowID,
    75  		runID,
    76  	); err != nil {
    77  		return err
    78  	}
    79  
    80  	if err := updateTimerInfos(
    81  		batch,
    82  		workflowMutation.UpsertTimerInfos,
    83  		workflowMutation.DeleteTimerInfos,
    84  		shardID,
    85  		namespaceID,
    86  		workflowID,
    87  		runID,
    88  	); err != nil {
    89  		return err
    90  	}
    91  
    92  	if err := updateChildExecutionInfos(
    93  		batch,
    94  		workflowMutation.UpsertChildExecutionInfos,
    95  		workflowMutation.DeleteChildExecutionInfos,
    96  		shardID,
    97  		namespaceID,
    98  		workflowID,
    99  		runID,
   100  	); err != nil {
   101  		return err
   102  	}
   103  
   104  	if err := updateRequestCancelInfos(
   105  		batch,
   106  		workflowMutation.UpsertRequestCancelInfos,
   107  		workflowMutation.DeleteRequestCancelInfos,
   108  		shardID,
   109  		namespaceID,
   110  		workflowID,
   111  		runID,
   112  	); err != nil {
   113  		return err
   114  	}
   115  
   116  	if err := updateSignalInfos(
   117  		batch,
   118  		workflowMutation.UpsertSignalInfos,
   119  		workflowMutation.DeleteSignalInfos,
   120  		shardID,
   121  		namespaceID,
   122  		workflowID,
   123  		runID,
   124  	); err != nil {
   125  		return err
   126  	}
   127  
   128  	updateSignalsRequested(
   129  		batch,
   130  		workflowMutation.UpsertSignalRequestedIDs,
   131  		workflowMutation.DeleteSignalRequestedIDs,
   132  		shardID,
   133  		namespaceID,
   134  		workflowID,
   135  		runID,
   136  	)
   137  
   138  	updateBufferedEvents(
   139  		batch,
   140  		workflowMutation.NewBufferedEvents,
   141  		workflowMutation.ClearBufferedEvents,
   142  		shardID,
   143  		namespaceID,
   144  		workflowID,
   145  		runID,
   146  	)
   147  
   148  	// transfer / replication / timer tasks
   149  	return applyTasks(
   150  		batch,
   151  		shardID,
   152  		workflowMutation.Tasks,
   153  	)
   154  }
   155  
   156  func applyWorkflowSnapshotBatchAsReset(
   157  	batch gocql.Batch,
   158  	shardID int32,
   159  	workflowSnapshot *p.InternalWorkflowSnapshot,
   160  ) error {
   161  
   162  	// TODO: update call site
   163  	// cqlNowTimestampMillis := p.UnixMilliseconds(time.Now().UTC())
   164  
   165  	namespaceID := workflowSnapshot.NamespaceID
   166  	workflowID := workflowSnapshot.WorkflowID
   167  	runID := workflowSnapshot.RunID
   168  
   169  	if err := updateExecution(
   170  		batch,
   171  		shardID,
   172  		namespaceID,
   173  		workflowID,
   174  		runID,
   175  		workflowSnapshot.ExecutionInfoBlob,
   176  		workflowSnapshot.ExecutionState,
   177  		workflowSnapshot.ExecutionStateBlob,
   178  		workflowSnapshot.NextEventID,
   179  		workflowSnapshot.Condition,
   180  		workflowSnapshot.DBRecordVersion,
   181  		workflowSnapshot.Checksum,
   182  	); err != nil {
   183  		return err
   184  	}
   185  
   186  	if err := resetActivityInfos(
   187  		batch,
   188  		workflowSnapshot.ActivityInfos,
   189  		shardID,
   190  		namespaceID,
   191  		workflowID,
   192  		runID,
   193  	); err != nil {
   194  		return err
   195  	}
   196  
   197  	if err := resetTimerInfos(
   198  		batch,
   199  		workflowSnapshot.TimerInfos,
   200  		shardID,
   201  		namespaceID,
   202  		workflowID,
   203  		runID,
   204  	); err != nil {
   205  		return err
   206  	}
   207  
   208  	if err := resetChildExecutionInfos(
   209  		batch,
   210  		workflowSnapshot.ChildExecutionInfos,
   211  		shardID,
   212  		namespaceID,
   213  		workflowID,
   214  		runID,
   215  	); err != nil {
   216  		return err
   217  	}
   218  
   219  	if err := resetRequestCancelInfos(
   220  		batch,
   221  		workflowSnapshot.RequestCancelInfos,
   222  		shardID,
   223  		namespaceID,
   224  		workflowID,
   225  		runID,
   226  	); err != nil {
   227  		return err
   228  	}
   229  
   230  	if err := resetSignalInfos(
   231  		batch,
   232  		workflowSnapshot.SignalInfos,
   233  		shardID,
   234  		namespaceID,
   235  		workflowID,
   236  		runID,
   237  	); err != nil {
   238  		return err
   239  	}
   240  
   241  	resetSignalRequested(
   242  		batch,
   243  		workflowSnapshot.SignalRequestedIDs,
   244  		shardID,
   245  		namespaceID,
   246  		workflowID,
   247  		runID,
   248  	)
   249  
   250  	deleteBufferedEvents(
   251  		batch,
   252  		shardID,
   253  		namespaceID,
   254  		workflowID,
   255  		runID,
   256  	)
   257  
   258  	// transfer / replication / timer tasks
   259  	return applyTasks(
   260  		batch,
   261  		shardID,
   262  		workflowSnapshot.Tasks,
   263  	)
   264  }
   265  
   266  func applyWorkflowSnapshotBatchAsNew(
   267  	batch gocql.Batch,
   268  	shardID int32,
   269  	workflowSnapshot *p.InternalWorkflowSnapshot,
   270  ) error {
   271  	namespaceID := workflowSnapshot.NamespaceID
   272  	workflowID := workflowSnapshot.WorkflowID
   273  	runID := workflowSnapshot.RunID
   274  
   275  	if err := createExecution(
   276  		batch,
   277  		shardID,
   278  		workflowSnapshot,
   279  	); err != nil {
   280  		return err
   281  	}
   282  
   283  	if err := updateActivityInfos(
   284  		batch,
   285  		workflowSnapshot.ActivityInfos,
   286  		nil,
   287  		shardID,
   288  		namespaceID,
   289  		workflowID,
   290  		runID,
   291  	); err != nil {
   292  		return err
   293  	}
   294  
   295  	if err := updateTimerInfos(
   296  		batch,
   297  		workflowSnapshot.TimerInfos,
   298  		nil,
   299  		shardID,
   300  		namespaceID,
   301  		workflowID,
   302  		runID,
   303  	); err != nil {
   304  		return err
   305  	}
   306  
   307  	if err := updateChildExecutionInfos(
   308  		batch,
   309  		workflowSnapshot.ChildExecutionInfos,
   310  		nil,
   311  		shardID,
   312  		namespaceID,
   313  		workflowID,
   314  		runID,
   315  	); err != nil {
   316  		return err
   317  	}
   318  
   319  	if err := updateRequestCancelInfos(
   320  		batch,
   321  		workflowSnapshot.RequestCancelInfos,
   322  		nil,
   323  		shardID,
   324  		namespaceID,
   325  		workflowID,
   326  		runID,
   327  	); err != nil {
   328  		return err
   329  	}
   330  
   331  	if err := updateSignalInfos(
   332  		batch,
   333  		workflowSnapshot.SignalInfos,
   334  		nil,
   335  		shardID,
   336  		namespaceID,
   337  		workflowID,
   338  		runID,
   339  	); err != nil {
   340  		return err
   341  	}
   342  
   343  	updateSignalsRequested(
   344  		batch,
   345  		workflowSnapshot.SignalRequestedIDs,
   346  		nil,
   347  		shardID,
   348  		namespaceID,
   349  		workflowID,
   350  		runID,
   351  	)
   352  
   353  	// transfer / replication / timer tasks
   354  	return applyTasks(
   355  		batch,
   356  		shardID,
   357  		workflowSnapshot.Tasks,
   358  	)
   359  }
   360  
   361  func createExecution(
   362  	batch gocql.Batch,
   363  	shardID int32,
   364  	snapshot *p.InternalWorkflowSnapshot,
   365  ) error {
   366  	// validate workflow state & close status
   367  	if err := p.ValidateCreateWorkflowStateStatus(
   368  		snapshot.ExecutionState.State,
   369  		snapshot.ExecutionState.Status); err != nil {
   370  		return err
   371  	}
   372  
   373  	// TODO also need to set the start / current / last write version
   374  	batch.Query(templateCreateWorkflowExecutionQuery,
   375  		shardID,
   376  		snapshot.NamespaceID,
   377  		snapshot.WorkflowID,
   378  		snapshot.RunID,
   379  		rowTypeExecution,
   380  		snapshot.ExecutionInfoBlob.Data,
   381  		snapshot.ExecutionInfoBlob.EncodingType.String(),
   382  		snapshot.ExecutionStateBlob.Data,
   383  		snapshot.ExecutionStateBlob.EncodingType.String(),
   384  		snapshot.NextEventID,
   385  		snapshot.DBRecordVersion,
   386  		defaultVisibilityTimestamp,
   387  		rowTypeExecutionTaskID,
   388  		snapshot.Checksum.Data,
   389  		snapshot.Checksum.EncodingType.String(),
   390  	)
   391  
   392  	return nil
   393  }
   394  
   395  func updateExecution(
   396  	batch gocql.Batch,
   397  	shardID int32,
   398  	namespaceID string,
   399  	workflowID string,
   400  	runID string,
   401  	executionInfoBlob *commonpb.DataBlob,
   402  	executionState *persistencespb.WorkflowExecutionState,
   403  	executionStateBlob *commonpb.DataBlob,
   404  	nextEventID int64,
   405  	condition int64,
   406  	dbRecordVersion int64,
   407  	checksumBlob *commonpb.DataBlob,
   408  ) error {
   409  
   410  	// validate workflow state & close status
   411  	if err := p.ValidateUpdateWorkflowStateStatus(
   412  		executionState.State,
   413  		executionState.Status); err != nil {
   414  		return err
   415  	}
   416  
   417  	if dbRecordVersion == 0 {
   418  		batch.Query(templateUpdateWorkflowExecutionQueryDeprecated,
   419  			executionInfoBlob.Data,
   420  			executionInfoBlob.EncodingType.String(),
   421  			executionStateBlob.Data,
   422  			executionStateBlob.EncodingType.String(),
   423  			nextEventID,
   424  			dbRecordVersion,
   425  			checksumBlob.Data,
   426  			checksumBlob.EncodingType.String(),
   427  			shardID,
   428  			rowTypeExecution,
   429  			namespaceID,
   430  			workflowID,
   431  			runID,
   432  			defaultVisibilityTimestamp,
   433  			rowTypeExecutionTaskID,
   434  			condition,
   435  		)
   436  	} else {
   437  		batch.Query(templateUpdateWorkflowExecutionQuery,
   438  			executionInfoBlob.Data,
   439  			executionInfoBlob.EncodingType.String(),
   440  			executionStateBlob.Data,
   441  			executionStateBlob.EncodingType.String(),
   442  			nextEventID,
   443  			dbRecordVersion,
   444  			checksumBlob.Data,
   445  			checksumBlob.EncodingType.String(),
   446  			shardID,
   447  			rowTypeExecution,
   448  			namespaceID,
   449  			workflowID,
   450  			runID,
   451  			defaultVisibilityTimestamp,
   452  			rowTypeExecutionTaskID,
   453  			dbRecordVersion-1,
   454  		)
   455  	}
   456  
   457  	return nil
   458  }
   459  
   460  func applyTasks(
   461  	batch gocql.Batch,
   462  	shardID int32,
   463  	insertTasks map[tasks.Category][]p.InternalHistoryTask,
   464  ) error {
   465  
   466  	var err error
   467  	for category, tasksByCategory := range insertTasks {
   468  		switch category.ID() {
   469  		case tasks.CategoryIDTransfer:
   470  			err = createTransferTasks(batch, tasksByCategory, shardID)
   471  		case tasks.CategoryIDTimer:
   472  			err = createTimerTasks(batch, tasksByCategory, shardID)
   473  		case tasks.CategoryIDVisibility:
   474  			err = createVisibilityTasks(batch, tasksByCategory, shardID)
   475  		case tasks.CategoryIDReplication:
   476  			err = createReplicationTasks(batch, tasksByCategory, shardID)
   477  		default:
   478  			err = createHistoryTasks(batch, category, tasksByCategory, shardID)
   479  		}
   480  
   481  		if err != nil {
   482  			return err
   483  		}
   484  	}
   485  
   486  	return nil
   487  }
   488  
   489  func createTransferTasks(
   490  	batch gocql.Batch,
   491  	transferTasks []p.InternalHistoryTask,
   492  	shardID int32,
   493  ) error {
   494  	for _, task := range transferTasks {
   495  		batch.Query(templateCreateTransferTaskQuery,
   496  			shardID,
   497  			rowTypeTransferTask,
   498  			rowTypeTransferNamespaceID,
   499  			rowTypeTransferWorkflowID,
   500  			rowTypeTransferRunID,
   501  			task.Blob.Data,
   502  			task.Blob.EncodingType.String(),
   503  			defaultVisibilityTimestamp,
   504  			task.Key.TaskID,
   505  		)
   506  	}
   507  	return nil
   508  }
   509  
   510  func createTimerTasks(
   511  	batch gocql.Batch,
   512  	timerTasks []p.InternalHistoryTask,
   513  	shardID int32,
   514  ) error {
   515  	for _, task := range timerTasks {
   516  		batch.Query(templateCreateTimerTaskQuery,
   517  			shardID,
   518  			rowTypeTimerTask,
   519  			rowTypeTimerNamespaceID,
   520  			rowTypeTimerWorkflowID,
   521  			rowTypeTimerRunID,
   522  			task.Blob.Data,
   523  			task.Blob.EncodingType.String(),
   524  			p.UnixMilliseconds(task.Key.FireTime),
   525  			task.Key.TaskID,
   526  		)
   527  	}
   528  	return nil
   529  }
   530  
   531  func createReplicationTasks(
   532  	batch gocql.Batch,
   533  	replicationTasks []p.InternalHistoryTask,
   534  	shardID int32,
   535  ) error {
   536  	for _, task := range replicationTasks {
   537  		batch.Query(templateCreateReplicationTaskQuery,
   538  			shardID,
   539  			rowTypeReplicationTask,
   540  			rowTypeReplicationNamespaceID,
   541  			rowTypeReplicationWorkflowID,
   542  			rowTypeReplicationRunID,
   543  			task.Blob.Data,
   544  			task.Blob.EncodingType.String(),
   545  			defaultVisibilityTimestamp,
   546  			task.Key.TaskID,
   547  		)
   548  	}
   549  	return nil
   550  }
   551  
   552  func createVisibilityTasks(
   553  	batch gocql.Batch,
   554  	visibilityTasks []p.InternalHistoryTask,
   555  	shardID int32,
   556  ) error {
   557  	for _, task := range visibilityTasks {
   558  		batch.Query(templateCreateVisibilityTaskQuery,
   559  			shardID,
   560  			rowTypeVisibilityTask,
   561  			rowTypeVisibilityTaskNamespaceID,
   562  			rowTypeVisibilityTaskWorkflowID,
   563  			rowTypeVisibilityTaskRunID,
   564  			task.Blob.Data,
   565  			task.Blob.EncodingType.String(),
   566  			defaultVisibilityTimestamp,
   567  			task.Key.TaskID,
   568  		)
   569  	}
   570  	return nil
   571  }
   572  
   573  func createHistoryTasks(
   574  	batch gocql.Batch,
   575  	category tasks.Category,
   576  	historyTasks []p.InternalHistoryTask,
   577  	shardID int32,
   578  ) error {
   579  	isScheduledTask := category.Type() == tasks.CategoryTypeScheduled
   580  	for _, task := range historyTasks {
   581  		visibilityTimestamp := defaultVisibilityTimestamp
   582  		if isScheduledTask {
   583  			visibilityTimestamp = p.UnixMilliseconds(task.Key.FireTime)
   584  		}
   585  		batch.Query(templateCreateHistoryTaskQuery,
   586  			shardID,
   587  			category.ID(),
   588  			rowTypeHistoryTaskNamespaceID,
   589  			rowTypeHistoryTaskWorkflowID,
   590  			rowTypeHistoryTaskRunID,
   591  			task.Blob.Data,
   592  			task.Blob.EncodingType.String(),
   593  			visibilityTimestamp,
   594  			task.Key.TaskID,
   595  		)
   596  	}
   597  	return nil
   598  }
   599  
   600  func updateActivityInfos(
   601  	batch gocql.Batch,
   602  	activityInfos map[int64]*commonpb.DataBlob,
   603  	deleteIDs map[int64]struct{},
   604  	shardID int32,
   605  	namespaceID string,
   606  	workflowID string,
   607  	runID string,
   608  ) error {
   609  
   610  	for scheduledEventID, blob := range activityInfos {
   611  		batch.Query(templateUpdateActivityInfoQuery,
   612  			scheduledEventID,
   613  			blob.Data,
   614  			blob.EncodingType.String(),
   615  			shardID,
   616  			rowTypeExecution,
   617  			namespaceID,
   618  			workflowID,
   619  			runID,
   620  			defaultVisibilityTimestamp,
   621  			rowTypeExecutionTaskID)
   622  	}
   623  
   624  	for deleteID := range deleteIDs {
   625  		batch.Query(templateDeleteActivityInfoQuery,
   626  			deleteID,
   627  			shardID,
   628  			rowTypeExecution,
   629  			namespaceID,
   630  			workflowID,
   631  			runID,
   632  			defaultVisibilityTimestamp,
   633  			rowTypeExecutionTaskID)
   634  	}
   635  	return nil
   636  }
   637  
   638  func deleteBufferedEvents(
   639  	batch gocql.Batch,
   640  	shardID int32,
   641  	namespaceID string,
   642  	workflowID string,
   643  	runID string,
   644  ) {
   645  	batch.Query(templateDeleteBufferedEventsQuery,
   646  		shardID,
   647  		rowTypeExecution,
   648  		namespaceID,
   649  		workflowID,
   650  		runID,
   651  		defaultVisibilityTimestamp,
   652  		rowTypeExecutionTaskID,
   653  	)
   654  }
   655  
   656  func resetActivityInfos(
   657  	batch gocql.Batch,
   658  	activityInfos map[int64]*commonpb.DataBlob,
   659  	shardID int32,
   660  	namespaceID string,
   661  	workflowID string,
   662  	runID string,
   663  ) error {
   664  
   665  	infoMap, encoding, err := resetActivityInfoMap(activityInfos)
   666  	if err != nil {
   667  		return err
   668  	}
   669  
   670  	batch.Query(templateResetActivityInfoQuery,
   671  		infoMap,
   672  		encoding.String(),
   673  		shardID,
   674  		rowTypeExecution,
   675  		namespaceID,
   676  		workflowID,
   677  		runID,
   678  		defaultVisibilityTimestamp,
   679  		rowTypeExecutionTaskID)
   680  	return nil
   681  }
   682  
   683  func updateTimerInfos(
   684  	batch gocql.Batch,
   685  	timerInfos map[string]*commonpb.DataBlob,
   686  	deleteInfos map[string]struct{},
   687  	shardID int32,
   688  	namespaceID string,
   689  	workflowID string,
   690  	runID string,
   691  ) error {
   692  	for timerID, blob := range timerInfos {
   693  		batch.Query(templateUpdateTimerInfoQuery,
   694  			timerID,
   695  			blob.Data,
   696  			blob.EncodingType.String(),
   697  			shardID,
   698  			rowTypeExecution,
   699  			namespaceID,
   700  			workflowID,
   701  			runID,
   702  			defaultVisibilityTimestamp,
   703  			rowTypeExecutionTaskID)
   704  	}
   705  
   706  	for deleteInfoID := range deleteInfos {
   707  		batch.Query(templateDeleteTimerInfoQuery,
   708  			deleteInfoID,
   709  			shardID,
   710  			rowTypeExecution,
   711  			namespaceID,
   712  			workflowID,
   713  			runID,
   714  			defaultVisibilityTimestamp,
   715  			rowTypeExecutionTaskID)
   716  	}
   717  
   718  	return nil
   719  }
   720  
   721  func resetTimerInfos(
   722  	batch gocql.Batch,
   723  	timerInfos map[string]*commonpb.DataBlob,
   724  	shardID int32,
   725  	namespaceID string,
   726  	workflowID string,
   727  	runID string,
   728  ) error {
   729  
   730  	timerMap, timerMapEncoding, err := resetTimerInfoMap(timerInfos)
   731  	if err != nil {
   732  		return err
   733  	}
   734  
   735  	batch.Query(templateResetTimerInfoQuery,
   736  		timerMap,
   737  		timerMapEncoding.String(),
   738  		shardID,
   739  		rowTypeExecution,
   740  		namespaceID,
   741  		workflowID,
   742  		runID,
   743  		defaultVisibilityTimestamp,
   744  		rowTypeExecutionTaskID)
   745  
   746  	return nil
   747  }
   748  
   749  func updateChildExecutionInfos(
   750  	batch gocql.Batch,
   751  	childExecutionInfos map[int64]*commonpb.DataBlob,
   752  	deleteIDs map[int64]struct{},
   753  	shardID int32,
   754  	namespaceID string,
   755  	workflowID string,
   756  	runID string,
   757  ) error {
   758  
   759  	for initiatedId, blob := range childExecutionInfos {
   760  		batch.Query(templateUpdateChildExecutionInfoQuery,
   761  			initiatedId,
   762  			blob.Data,
   763  			blob.EncodingType.String(),
   764  			shardID,
   765  			rowTypeExecution,
   766  			namespaceID,
   767  			workflowID,
   768  			runID,
   769  			defaultVisibilityTimestamp,
   770  			rowTypeExecutionTaskID)
   771  	}
   772  
   773  	for deleteID := range deleteIDs {
   774  		batch.Query(templateDeleteChildExecutionInfoQuery,
   775  			deleteID,
   776  			shardID,
   777  			rowTypeExecution,
   778  			namespaceID,
   779  			workflowID,
   780  			runID,
   781  			defaultVisibilityTimestamp,
   782  			rowTypeExecutionTaskID)
   783  	}
   784  	return nil
   785  }
   786  
   787  func resetChildExecutionInfos(
   788  	batch gocql.Batch,
   789  	childExecutionInfos map[int64]*commonpb.DataBlob,
   790  	shardID int32,
   791  	namespaceID string,
   792  	workflowID string,
   793  	runID string,
   794  ) error {
   795  
   796  	infoMap, encoding, err := resetChildExecutionInfoMap(childExecutionInfos)
   797  	if err != nil {
   798  		return err
   799  	}
   800  	batch.Query(templateResetChildExecutionInfoQuery,
   801  		infoMap,
   802  		encoding.String(),
   803  		shardID,
   804  		rowTypeExecution,
   805  		namespaceID,
   806  		workflowID,
   807  		runID,
   808  		defaultVisibilityTimestamp,
   809  		rowTypeExecutionTaskID)
   810  	return nil
   811  }
   812  
   813  func updateRequestCancelInfos(
   814  	batch gocql.Batch,
   815  	requestCancelInfos map[int64]*commonpb.DataBlob,
   816  	deleteIDs map[int64]struct{},
   817  	shardID int32,
   818  	namespaceID string,
   819  	workflowID string,
   820  	runID string,
   821  ) error {
   822  
   823  	for initiatedId, blob := range requestCancelInfos {
   824  		batch.Query(templateUpdateRequestCancelInfoQuery,
   825  			initiatedId,
   826  			blob.Data,
   827  			blob.EncodingType.String(),
   828  			shardID,
   829  			rowTypeExecution,
   830  			namespaceID,
   831  			workflowID,
   832  			runID,
   833  			defaultVisibilityTimestamp,
   834  			rowTypeExecutionTaskID)
   835  	}
   836  
   837  	for deleteID := range deleteIDs {
   838  		batch.Query(templateDeleteRequestCancelInfoQuery,
   839  			deleteID,
   840  			shardID,
   841  			rowTypeExecution,
   842  			namespaceID,
   843  			workflowID,
   844  			runID,
   845  			defaultVisibilityTimestamp,
   846  			rowTypeExecutionTaskID)
   847  	}
   848  	return nil
   849  }
   850  
   851  func resetRequestCancelInfos(
   852  	batch gocql.Batch,
   853  	requestCancelInfos map[int64]*commonpb.DataBlob,
   854  	shardID int32,
   855  	namespaceID string,
   856  	workflowID string,
   857  	runID string,
   858  ) error {
   859  
   860  	rciMap, rciMapEncoding, err := resetRequestCancelInfoMap(requestCancelInfos)
   861  
   862  	if err != nil {
   863  		return err
   864  	}
   865  
   866  	batch.Query(templateResetRequestCancelInfoQuery,
   867  		rciMap,
   868  		rciMapEncoding.String(),
   869  		shardID,
   870  		rowTypeExecution,
   871  		namespaceID,
   872  		workflowID,
   873  		runID,
   874  		defaultVisibilityTimestamp,
   875  		rowTypeExecutionTaskID)
   876  
   877  	return nil
   878  }
   879  
   880  func updateSignalInfos(
   881  	batch gocql.Batch,
   882  	signalInfos map[int64]*commonpb.DataBlob,
   883  	deleteIDs map[int64]struct{},
   884  	shardID int32,
   885  	namespaceID string,
   886  	workflowID string,
   887  	runID string,
   888  ) error {
   889  
   890  	for initiatedId, blob := range signalInfos {
   891  		batch.Query(templateUpdateSignalInfoQuery,
   892  			initiatedId,
   893  			blob.Data,
   894  			blob.EncodingType.String(),
   895  			shardID,
   896  			rowTypeExecution,
   897  			namespaceID,
   898  			workflowID,
   899  			runID,
   900  			defaultVisibilityTimestamp,
   901  			rowTypeExecutionTaskID)
   902  	}
   903  
   904  	for deleteID := range deleteIDs {
   905  		batch.Query(templateDeleteSignalInfoQuery,
   906  			deleteID,
   907  			shardID,
   908  			rowTypeExecution,
   909  			namespaceID,
   910  			workflowID,
   911  			runID,
   912  			defaultVisibilityTimestamp,
   913  			rowTypeExecutionTaskID)
   914  	}
   915  	return nil
   916  }
   917  
   918  func resetSignalInfos(
   919  	batch gocql.Batch,
   920  	signalInfos map[int64]*commonpb.DataBlob,
   921  	shardID int32,
   922  	namespaceID string,
   923  	workflowID string,
   924  	runID string,
   925  ) error {
   926  	sMap, sMapEncoding, err := resetSignalInfoMap(signalInfos)
   927  
   928  	if err != nil {
   929  		return err
   930  	}
   931  
   932  	batch.Query(templateResetSignalInfoQuery,
   933  		sMap,
   934  		sMapEncoding.String(),
   935  		shardID,
   936  		rowTypeExecution,
   937  		namespaceID,
   938  		workflowID,
   939  		runID,
   940  		defaultVisibilityTimestamp,
   941  		rowTypeExecutionTaskID)
   942  
   943  	return nil
   944  }
   945  
   946  func updateSignalsRequested(
   947  	batch gocql.Batch,
   948  	signalReqIDs map[string]struct{},
   949  	deleteSignalReqIDs map[string]struct{},
   950  	shardID int32,
   951  	namespaceID string,
   952  	workflowID string,
   953  	runID string,
   954  ) {
   955  
   956  	if len(signalReqIDs) > 0 {
   957  		batch.Query(templateUpdateSignalRequestedQuery,
   958  			convert.StringSetToSlice(signalReqIDs),
   959  			shardID,
   960  			rowTypeExecution,
   961  			namespaceID,
   962  			workflowID,
   963  			runID,
   964  			defaultVisibilityTimestamp,
   965  			rowTypeExecutionTaskID)
   966  	}
   967  
   968  	if len(deleteSignalReqIDs) > 0 {
   969  		batch.Query(templateDeleteWorkflowExecutionSignalRequestedQuery,
   970  			convert.StringSetToSlice(deleteSignalReqIDs),
   971  			shardID,
   972  			rowTypeExecution,
   973  			namespaceID,
   974  			workflowID,
   975  			runID,
   976  			defaultVisibilityTimestamp,
   977  			rowTypeExecutionTaskID)
   978  	}
   979  }
   980  
   981  func resetSignalRequested(
   982  	batch gocql.Batch,
   983  	signalRequested map[string]struct{},
   984  	shardID int32,
   985  	namespaceID string,
   986  	workflowID string,
   987  	runID string,
   988  ) {
   989  
   990  	batch.Query(templateResetSignalRequestedQuery,
   991  		convert.StringSetToSlice(signalRequested),
   992  		shardID,
   993  		rowTypeExecution,
   994  		namespaceID,
   995  		workflowID,
   996  		runID,
   997  		defaultVisibilityTimestamp,
   998  		rowTypeExecutionTaskID)
   999  }
  1000  
  1001  func updateBufferedEvents(
  1002  	batch gocql.Batch,
  1003  	newBufferedEvents *commonpb.DataBlob,
  1004  	clearBufferedEvents bool,
  1005  	shardID int32,
  1006  	namespaceID string,
  1007  	workflowID string,
  1008  	runID string,
  1009  ) {
  1010  
  1011  	if clearBufferedEvents {
  1012  		batch.Query(templateDeleteBufferedEventsQuery,
  1013  			shardID,
  1014  			rowTypeExecution,
  1015  			namespaceID,
  1016  			workflowID,
  1017  			runID,
  1018  			defaultVisibilityTimestamp,
  1019  			rowTypeExecutionTaskID)
  1020  	} else if newBufferedEvents != nil {
  1021  		values := make(map[string]interface{})
  1022  		values["encoding_type"] = newBufferedEvents.EncodingType.String()
  1023  		values["version"] = int64(0)
  1024  		values["data"] = newBufferedEvents.Data
  1025  		newEventValues := []map[string]interface{}{values}
  1026  		batch.Query(templateAppendBufferedEventsQuery,
  1027  			newEventValues,
  1028  			shardID,
  1029  			rowTypeExecution,
  1030  			namespaceID,
  1031  			workflowID,
  1032  			runID,
  1033  			defaultVisibilityTimestamp,
  1034  			rowTypeExecutionTaskID)
  1035  	}
  1036  }
  1037  
  1038  func resetActivityInfoMap(
  1039  	activityInfos map[int64]*commonpb.DataBlob,
  1040  ) (map[int64][]byte, enumspb.EncodingType, error) {
  1041  
  1042  	encoding := enumspb.ENCODING_TYPE_UNSPECIFIED
  1043  	aMap := make(map[int64][]byte)
  1044  	for scheduledEventID, blob := range activityInfos {
  1045  		aMap[scheduledEventID] = blob.Data
  1046  		encoding = blob.EncodingType
  1047  	}
  1048  
  1049  	return aMap, encoding, nil
  1050  }
  1051  
  1052  func resetTimerInfoMap(
  1053  	timerInfos map[string]*commonpb.DataBlob,
  1054  ) (map[string][]byte, enumspb.EncodingType, error) {
  1055  
  1056  	tMap := make(map[string][]byte)
  1057  	var encoding enumspb.EncodingType
  1058  	for timerID, blob := range timerInfos {
  1059  		encoding = blob.EncodingType
  1060  		tMap[timerID] = blob.Data
  1061  	}
  1062  
  1063  	return tMap, encoding, nil
  1064  }
  1065  
  1066  func resetChildExecutionInfoMap(
  1067  	childExecutionInfos map[int64]*commonpb.DataBlob,
  1068  ) (map[int64][]byte, enumspb.EncodingType, error) {
  1069  
  1070  	cMap := make(map[int64][]byte)
  1071  	encoding := enumspb.ENCODING_TYPE_UNSPECIFIED
  1072  	for initiatedID, blob := range childExecutionInfos {
  1073  		cMap[initiatedID] = blob.Data
  1074  		encoding = blob.EncodingType
  1075  	}
  1076  
  1077  	return cMap, encoding, nil
  1078  }
  1079  
  1080  func resetRequestCancelInfoMap(
  1081  	requestCancelInfos map[int64]*commonpb.DataBlob,
  1082  ) (map[int64][]byte, enumspb.EncodingType, error) {
  1083  
  1084  	rcMap := make(map[int64][]byte)
  1085  	var encoding enumspb.EncodingType
  1086  	for initiatedID, blob := range requestCancelInfos {
  1087  		encoding = blob.EncodingType
  1088  		rcMap[initiatedID] = blob.Data
  1089  	}
  1090  
  1091  	return rcMap, encoding, nil
  1092  }
  1093  
  1094  func resetSignalInfoMap(
  1095  	signalInfos map[int64]*commonpb.DataBlob,
  1096  ) (map[int64][]byte, enumspb.EncodingType, error) {
  1097  
  1098  	sMap := make(map[int64][]byte)
  1099  	var encoding enumspb.EncodingType
  1100  	for initiatedID, blob := range signalInfos {
  1101  		encoding = blob.EncodingType
  1102  		sMap[initiatedID] = blob.Data
  1103  	}
  1104  
  1105  	return sMap, encoding, nil
  1106  }
  1107  
  1108  func createHistoryEventBatchBlob(
  1109  	result map[string]interface{},
  1110  ) *commonpb.DataBlob {
  1111  	eventBatch := &commonpb.DataBlob{EncodingType: enumspb.ENCODING_TYPE_UNSPECIFIED}
  1112  	for k, v := range result {
  1113  		switch k {
  1114  		case "encoding_type":
  1115  			encodingStr := v.(string)
  1116  			if encoding, err := enumspb.EncodingTypeFromString(encodingStr); err == nil {
  1117  				eventBatch.EncodingType = enumspb.EncodingType(encoding)
  1118  			}
  1119  		case "data":
  1120  			eventBatch.Data = v.([]byte)
  1121  		}
  1122  	}
  1123  
  1124  	return eventBatch
  1125  }