go.temporal.io/server@v1.23.0/common/persistence/size.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 persistence
    26  
    27  import (
    28  	persistencespb "go.temporal.io/server/api/persistence/v1"
    29  	"go.temporal.io/server/service/history/tasks"
    30  )
    31  
    32  func statusOfInternalWorkflow(
    33  	internalState *InternalWorkflowMutableState,
    34  	state *persistencespb.WorkflowMutableState,
    35  	historyStatistics *HistoryStatistics,
    36  ) *MutableStateStatistics {
    37  	if internalState == nil {
    38  		return nil
    39  	}
    40  
    41  	executionInfoSize := sizeOfBlob(internalState.ExecutionInfo)
    42  	executionStateSize := sizeOfBlob(internalState.ExecutionState)
    43  
    44  	totalActivityCount := state.ExecutionInfo.ActivityCount
    45  	activityInfoCount := len(internalState.ActivityInfos)
    46  	activityInfoSize := sizeOfInt64BlobMap(internalState.ActivityInfos)
    47  
    48  	totalUserTimerCount := state.ExecutionInfo.UserTimerCount
    49  	timerInfoCount := len(internalState.TimerInfos)
    50  	timerInfoSize := sizeOfStringBlobMap(internalState.TimerInfos)
    51  
    52  	totalChildExecutionCount := state.ExecutionInfo.ChildExecutionCount
    53  	childExecutionInfoCount := len(internalState.ChildExecutionInfos)
    54  	childExecutionInfoSize := sizeOfInt64BlobMap(internalState.ChildExecutionInfos)
    55  
    56  	totalRequestCancelExternalCount := state.ExecutionInfo.RequestCancelExternalCount
    57  	requestCancelInfoCount := len(internalState.RequestCancelInfos)
    58  	requestCancelInfoSize := sizeOfInt64BlobMap(internalState.RequestCancelInfos)
    59  
    60  	totalSignalExternalCount := state.ExecutionInfo.SignalExternalCount
    61  	signalInfoCount := len(internalState.SignalInfos)
    62  	signalInfoSize := sizeOfInt64BlobMap(internalState.SignalInfos)
    63  
    64  	totalSignalCount := state.ExecutionInfo.SignalCount
    65  	signalRequestIDCount := len(internalState.SignalRequestedIDs)
    66  	signalRequestIDSize := sizeOfStringSlice(internalState.SignalRequestedIDs)
    67  
    68  	bufferedEventsCount := len(internalState.BufferedEvents)
    69  	bufferedEventsSize := sizeOfBlobSlice(internalState.BufferedEvents)
    70  
    71  	totalUpdateCount := state.ExecutionInfo.UpdateCount
    72  	updateInfoCount := len(state.ExecutionInfo.UpdateInfos)
    73  
    74  	totalSize := executionInfoSize
    75  	totalSize += executionStateSize
    76  	totalSize += activityInfoSize
    77  	totalSize += timerInfoSize
    78  	totalSize += childExecutionInfoSize
    79  	totalSize += requestCancelInfoSize
    80  	totalSize += signalInfoSize
    81  	totalSize += signalRequestIDSize
    82  	totalSize += bufferedEventsSize
    83  
    84  	return &MutableStateStatistics{
    85  		TotalSize:         totalSize,
    86  		HistoryStatistics: historyStatistics,
    87  
    88  		ExecutionInfoSize:  executionInfoSize,
    89  		ExecutionStateSize: executionStateSize,
    90  
    91  		ActivityInfoSize:   activityInfoSize,
    92  		ActivityInfoCount:  activityInfoCount,
    93  		TotalActivityCount: totalActivityCount,
    94  
    95  		TimerInfoSize:       timerInfoSize,
    96  		TimerInfoCount:      timerInfoCount,
    97  		TotalUserTimerCount: totalUserTimerCount,
    98  
    99  		ChildInfoSize:            childExecutionInfoSize,
   100  		ChildInfoCount:           childExecutionInfoCount,
   101  		TotalChildExecutionCount: totalChildExecutionCount,
   102  
   103  		RequestCancelInfoSize:           requestCancelInfoSize,
   104  		RequestCancelInfoCount:          requestCancelInfoCount,
   105  		TotalRequestCancelExternalCount: totalRequestCancelExternalCount,
   106  
   107  		SignalInfoSize:           signalInfoSize,
   108  		SignalInfoCount:          signalInfoCount,
   109  		TotalSignalExternalCount: totalSignalExternalCount,
   110  
   111  		SignalRequestIDSize:  signalRequestIDSize,
   112  		SignalRequestIDCount: signalRequestIDCount,
   113  		TotalSignalCount:     totalSignalCount,
   114  
   115  		BufferedEventsSize:  bufferedEventsSize,
   116  		BufferedEventsCount: bufferedEventsCount,
   117  
   118  		UpdateInfoCount:  updateInfoCount,
   119  		TotalUpdateCount: totalUpdateCount,
   120  	}
   121  }
   122  
   123  func statusOfInternalWorkflowMutation(
   124  	mutation *InternalWorkflowMutation,
   125  	historyStatistics *HistoryStatistics,
   126  ) *MutableStateStatistics {
   127  	if mutation == nil {
   128  		return nil
   129  	}
   130  
   131  	executionInfoSize := sizeOfBlob(mutation.ExecutionInfoBlob)
   132  	executionStateSize := sizeOfBlob(mutation.ExecutionStateBlob)
   133  
   134  	totalActivityCount := mutation.ExecutionInfo.ActivityCount
   135  	activityInfoCount := len(mutation.UpsertActivityInfos)
   136  	activityInfoCount += len(mutation.DeleteActivityInfos)
   137  	activityInfoSize := sizeOfInt64BlobMap(mutation.UpsertActivityInfos)
   138  	activityInfoSize += sizeOfInt64Set(mutation.DeleteActivityInfos)
   139  
   140  	totalUserTimerCount := mutation.ExecutionInfo.UserTimerCount
   141  	timerInfoCount := len(mutation.UpsertTimerInfos)
   142  	timerInfoCount += len(mutation.DeleteTimerInfos)
   143  	timerInfoSize := sizeOfStringBlobMap(mutation.UpsertTimerInfos)
   144  	timerInfoSize += sizeOfStringSet(mutation.DeleteTimerInfos)
   145  
   146  	totalChildExecutionCount := mutation.ExecutionInfo.ChildExecutionCount
   147  	childExecutionInfoCount := len(mutation.UpsertChildExecutionInfos)
   148  	childExecutionInfoCount += len(mutation.DeleteChildExecutionInfos)
   149  	childExecutionInfoSize := sizeOfInt64BlobMap(mutation.UpsertChildExecutionInfos)
   150  	childExecutionInfoSize += sizeOfInt64Set(mutation.DeleteChildExecutionInfos)
   151  
   152  	totalRequestCancelExternalCount := mutation.ExecutionInfo.RequestCancelExternalCount
   153  	requestCancelInfoCount := len(mutation.UpsertRequestCancelInfos)
   154  	requestCancelInfoCount += len(mutation.DeleteRequestCancelInfos)
   155  	requestCancelInfoSize := sizeOfInt64BlobMap(mutation.UpsertRequestCancelInfos)
   156  	requestCancelInfoSize += sizeOfInt64Set(mutation.DeleteRequestCancelInfos)
   157  
   158  	totalSignalExternalCount := mutation.ExecutionInfo.SignalExternalCount
   159  	signalInfoCount := len(mutation.UpsertSignalInfos)
   160  	signalInfoCount += len(mutation.DeleteSignalInfos)
   161  	signalInfoSize := sizeOfInt64BlobMap(mutation.UpsertSignalInfos)
   162  	signalInfoSize += sizeOfInt64Set(mutation.DeleteSignalInfos)
   163  
   164  	totalSignalCount := mutation.ExecutionInfo.SignalCount
   165  	signalRequestIDCount := len(mutation.UpsertSignalRequestedIDs)
   166  	signalRequestIDCount += len(mutation.DeleteSignalRequestedIDs)
   167  	signalRequestIDSize := sizeOfStringSet(mutation.UpsertSignalRequestedIDs)
   168  	signalRequestIDSize += sizeOfStringSet(mutation.DeleteSignalRequestedIDs)
   169  
   170  	totalUpdateCount := mutation.ExecutionInfo.UpdateCount
   171  	updateInfoCount := len(mutation.ExecutionInfo.UpdateInfos)
   172  
   173  	bufferedEventsCount := 0
   174  	bufferedEventsSize := 0
   175  	if mutation.NewBufferedEvents != nil {
   176  		bufferedEventsCount = 1
   177  		bufferedEventsSize = mutation.NewBufferedEvents.Size()
   178  	}
   179  
   180  	taskCountByCategory := taskCountsByCategory(&mutation.Tasks)
   181  
   182  	// TODO what about checksum?
   183  
   184  	totalSize := executionInfoSize
   185  	totalSize += executionStateSize
   186  	totalSize += activityInfoSize
   187  	totalSize += timerInfoSize
   188  	totalSize += childExecutionInfoSize
   189  	totalSize += requestCancelInfoSize
   190  	totalSize += signalInfoSize
   191  	totalSize += signalRequestIDSize
   192  	totalSize += bufferedEventsSize
   193  
   194  	return &MutableStateStatistics{
   195  		TotalSize:         totalSize,
   196  		HistoryStatistics: historyStatistics,
   197  
   198  		ExecutionInfoSize:  executionInfoSize,
   199  		ExecutionStateSize: executionStateSize,
   200  
   201  		ActivityInfoSize:   activityInfoSize,
   202  		ActivityInfoCount:  activityInfoCount,
   203  		TotalActivityCount: totalActivityCount,
   204  
   205  		TimerInfoSize:       timerInfoSize,
   206  		TimerInfoCount:      timerInfoCount,
   207  		TotalUserTimerCount: totalUserTimerCount,
   208  
   209  		ChildInfoSize:            childExecutionInfoSize,
   210  		ChildInfoCount:           childExecutionInfoCount,
   211  		TotalChildExecutionCount: totalChildExecutionCount,
   212  
   213  		RequestCancelInfoSize:           requestCancelInfoSize,
   214  		RequestCancelInfoCount:          requestCancelInfoCount,
   215  		TotalRequestCancelExternalCount: totalRequestCancelExternalCount,
   216  
   217  		SignalInfoSize:           signalInfoSize,
   218  		SignalInfoCount:          signalInfoCount,
   219  		TotalSignalExternalCount: totalSignalExternalCount,
   220  
   221  		SignalRequestIDSize:  signalRequestIDSize,
   222  		SignalRequestIDCount: signalRequestIDCount,
   223  		TotalSignalCount:     totalSignalCount,
   224  
   225  		BufferedEventsSize:  bufferedEventsSize,
   226  		BufferedEventsCount: bufferedEventsCount,
   227  
   228  		TaskCountByCategory: taskCountByCategory,
   229  
   230  		TotalUpdateCount: totalUpdateCount,
   231  		UpdateInfoCount:  updateInfoCount,
   232  	}
   233  }
   234  
   235  func taskCountsByCategory(t *map[tasks.Category][]InternalHistoryTask) map[string]int {
   236  	counts := make(map[string]int)
   237  	for category, tasks := range *t {
   238  		counts[category.Name()] = len(tasks)
   239  	}
   240  	return counts
   241  }
   242  
   243  func statusOfInternalWorkflowSnapshot(
   244  	snapshot *InternalWorkflowSnapshot,
   245  	historyStatistics *HistoryStatistics,
   246  ) *MutableStateStatistics {
   247  	if snapshot == nil {
   248  		return nil
   249  	}
   250  
   251  	executionInfoSize := sizeOfBlob(snapshot.ExecutionInfoBlob)
   252  	executionStateSize := sizeOfBlob(snapshot.ExecutionStateBlob)
   253  
   254  	totalActivityCount := snapshot.ExecutionInfo.ActivityCount
   255  	activityInfoCount := len(snapshot.ActivityInfos)
   256  	activityInfoSize := sizeOfInt64BlobMap(snapshot.ActivityInfos)
   257  
   258  	totalUserTimerCount := snapshot.ExecutionInfo.UserTimerCount
   259  	timerInfoCount := len(snapshot.TimerInfos)
   260  	timerInfoSize := sizeOfStringBlobMap(snapshot.TimerInfos)
   261  
   262  	totalChildExecutionCount := snapshot.ExecutionInfo.ChildExecutionCount
   263  	childExecutionInfoCount := len(snapshot.ChildExecutionInfos)
   264  	childExecutionInfoSize := sizeOfInt64BlobMap(snapshot.ChildExecutionInfos)
   265  
   266  	totalRequestCancelExternalCount := snapshot.ExecutionInfo.RequestCancelExternalCount
   267  	requestCancelInfoCount := len(snapshot.RequestCancelInfos)
   268  	requestCancelInfoSize := sizeOfInt64BlobMap(snapshot.RequestCancelInfos)
   269  
   270  	totalSignalExternalCount := snapshot.ExecutionInfo.SignalExternalCount
   271  	signalInfoCount := len(snapshot.SignalInfos)
   272  	signalInfoSize := sizeOfInt64BlobMap(snapshot.SignalInfos)
   273  
   274  	totalSignalCount := snapshot.ExecutionInfo.SignalCount
   275  	signalRequestIDCount := len(snapshot.SignalRequestedIDs)
   276  	signalRequestIDSize := sizeOfStringSet(snapshot.SignalRequestedIDs)
   277  
   278  	totalUpdateCount := snapshot.ExecutionInfo.UpdateCount
   279  	updateInfoCount := len(snapshot.ExecutionInfo.UpdateInfos)
   280  
   281  	bufferedEventsCount := 0
   282  	bufferedEventsSize := 0
   283  
   284  	totalSize := executionInfoSize
   285  	totalSize += executionStateSize
   286  	totalSize += activityInfoSize
   287  	totalSize += timerInfoSize
   288  	totalSize += childExecutionInfoSize
   289  	totalSize += requestCancelInfoSize
   290  	totalSize += signalInfoSize
   291  	totalSize += signalRequestIDSize
   292  	totalSize += bufferedEventsSize
   293  
   294  	taskCountByCategory := taskCountsByCategory(&snapshot.Tasks)
   295  
   296  	return &MutableStateStatistics{
   297  		TotalSize:         totalSize,
   298  		HistoryStatistics: historyStatistics,
   299  
   300  		ExecutionInfoSize:  executionInfoSize,
   301  		ExecutionStateSize: executionStateSize,
   302  
   303  		ActivityInfoSize:   activityInfoSize,
   304  		ActivityInfoCount:  activityInfoCount,
   305  		TotalActivityCount: totalActivityCount,
   306  
   307  		TimerInfoSize:       timerInfoSize,
   308  		TimerInfoCount:      timerInfoCount,
   309  		TotalUserTimerCount: totalUserTimerCount,
   310  
   311  		ChildInfoSize:            childExecutionInfoSize,
   312  		ChildInfoCount:           childExecutionInfoCount,
   313  		TotalChildExecutionCount: totalChildExecutionCount,
   314  
   315  		RequestCancelInfoSize:           requestCancelInfoSize,
   316  		RequestCancelInfoCount:          requestCancelInfoCount,
   317  		TotalRequestCancelExternalCount: totalRequestCancelExternalCount,
   318  
   319  		SignalInfoSize:           signalInfoSize,
   320  		SignalInfoCount:          signalInfoCount,
   321  		TotalSignalExternalCount: totalSignalExternalCount,
   322  
   323  		SignalRequestIDSize:  signalRequestIDSize,
   324  		SignalRequestIDCount: signalRequestIDCount,
   325  		TotalSignalCount:     totalSignalCount,
   326  
   327  		BufferedEventsSize:  bufferedEventsSize,
   328  		BufferedEventsCount: bufferedEventsCount,
   329  
   330  		TaskCountByCategory: taskCountByCategory,
   331  
   332  		TotalUpdateCount: totalUpdateCount,
   333  		UpdateInfoCount:  updateInfoCount,
   334  	}
   335  }