go.temporal.io/server@v1.23.0/common/testing/history_event_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 testing 26 27 import ( 28 "time" 29 30 "github.com/pborman/uuid" 31 commonpb "go.temporal.io/api/common/v1" 32 enumspb "go.temporal.io/api/enums/v1" 33 failurepb "go.temporal.io/api/failure/v1" 34 historypb "go.temporal.io/api/history/v1" 35 taskqueuepb "go.temporal.io/api/taskqueue/v1" 36 "google.golang.org/protobuf/types/known/durationpb" 37 "google.golang.org/protobuf/types/known/timestamppb" 38 39 "go.temporal.io/server/common/failure" 40 "go.temporal.io/server/common/namespace" 41 ) 42 43 const ( 44 timeout = 10000 * time.Second 45 signal = "NDC signal" 46 checksum = "NDC checksum" 47 childWorkflowPrefix = "child-" 48 reason = "NDC reason" 49 workflowType = "test-workflow-type" 50 taskQueue = "taskQueue" 51 identity = "identity" 52 workflowTaskAttempts = 1 53 childWorkflowID = "child-workflowID" 54 externalWorkflowID = "external-workflowID" 55 ) 56 57 var ( 58 globalTaskID int64 = 1 59 ) 60 61 // InitializeHistoryEventGenerator initializes the history event generator 62 func InitializeHistoryEventGenerator( 63 nsName namespace.Name, 64 nsID namespace.ID, 65 defaultVersion int64, 66 ) Generator { 67 68 generator := NewEventGenerator(time.Now().UnixNano()) 69 generator.SetVersion(defaultVersion) 70 // Functions 71 notPendingWorkflowTask := func(input ...interface{}) bool { 72 count := 0 73 history := input[0].([]Vertex) 74 for _, e := range history { 75 switch e.GetName() { 76 case enumspb.EVENT_TYPE_WORKFLOW_TASK_SCHEDULED.String(): 77 count++ 78 case enumspb.EVENT_TYPE_WORKFLOW_TASK_COMPLETED.String(), 79 enumspb.EVENT_TYPE_WORKFLOW_TASK_FAILED.String(), 80 enumspb.EVENT_TYPE_WORKFLOW_TASK_TIMED_OUT.String(): 81 count-- 82 } 83 } 84 return count <= 0 85 } 86 containActivityComplete := func(input ...interface{}) bool { 87 history := input[0].([]Vertex) 88 for _, e := range history { 89 if e.GetName() == enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED.String() { 90 return true 91 } 92 } 93 return false 94 } 95 hasPendingActivity := func(input ...interface{}) bool { 96 count := 0 97 history := input[0].([]Vertex) 98 for _, e := range history { 99 switch e.GetName() { 100 case enumspb.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED.String(): 101 count++ 102 case enumspb.EVENT_TYPE_ACTIVITY_TASK_CANCELED.String(), 103 enumspb.EVENT_TYPE_ACTIVITY_TASK_FAILED.String(), 104 enumspb.EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT.String(), 105 enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED.String(): 106 count-- 107 } 108 } 109 return count > 0 110 } 111 canDoBatch := func(currentBatch []Vertex, history []Vertex) bool { 112 if len(currentBatch) == 0 { 113 return true 114 } 115 116 hasPendingWorkflowTask := false 117 for _, event := range history { 118 switch event.GetName() { 119 case enumspb.EVENT_TYPE_WORKFLOW_TASK_SCHEDULED.String(): 120 hasPendingWorkflowTask = true 121 case enumspb.EVENT_TYPE_WORKFLOW_TASK_COMPLETED.String(), 122 enumspb.EVENT_TYPE_WORKFLOW_TASK_FAILED.String(), 123 enumspb.EVENT_TYPE_WORKFLOW_TASK_TIMED_OUT.String(): 124 hasPendingWorkflowTask = false 125 } 126 } 127 if hasPendingWorkflowTask { 128 return false 129 } 130 if currentBatch[len(currentBatch)-1].GetName() == enumspb.EVENT_TYPE_WORKFLOW_TASK_SCHEDULED.String() { 131 return false 132 } 133 if currentBatch[0].GetName() == enumspb.EVENT_TYPE_WORKFLOW_TASK_COMPLETED.String() { 134 return len(currentBatch) == 1 135 } 136 return true 137 } 138 139 // Setup workflow task model 140 historyEventModel := NewHistoryEventModel() 141 workflowTaskSchedule := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_TASK_SCHEDULED.String()) 142 workflowTaskSchedule.SetDataFunc(func(input ...interface{}) interface{} { 143 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 144 eventID := lastGeneratedEvent.GetEventId() + 1 145 version := input[2].(int64) 146 historyEvent := getDefaultHistoryEvent(eventID, version) 147 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_TASK_SCHEDULED 148 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowTaskScheduledEventAttributes{WorkflowTaskScheduledEventAttributes: &historypb.WorkflowTaskScheduledEventAttributes{ 149 TaskQueue: &taskqueuepb.TaskQueue{ 150 Name: taskQueue, 151 Kind: enumspb.TASK_QUEUE_KIND_NORMAL, 152 }, 153 StartToCloseTimeout: durationpb.New(timeout), 154 Attempt: workflowTaskAttempts, 155 }} 156 return historyEvent 157 }) 158 workflowTaskStart := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_TASK_STARTED.String()) 159 workflowTaskStart.SetIsStrictOnNextVertex(true) 160 workflowTaskStart.SetDataFunc(func(input ...interface{}) interface{} { 161 lastEvent := input[0].(*historypb.HistoryEvent) 162 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 163 eventID := lastGeneratedEvent.GetEventId() + 1 164 version := input[2].(int64) 165 historyEvent := getDefaultHistoryEvent(eventID, version) 166 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_TASK_STARTED 167 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowTaskStartedEventAttributes{WorkflowTaskStartedEventAttributes: &historypb.WorkflowTaskStartedEventAttributes{ 168 ScheduledEventId: lastEvent.EventId, 169 Identity: identity, 170 RequestId: uuid.New(), 171 }} 172 return historyEvent 173 }) 174 workflowTaskFail := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_TASK_FAILED.String()) 175 workflowTaskFail.SetDataFunc(func(input ...interface{}) interface{} { 176 lastEvent := input[0].(*historypb.HistoryEvent) 177 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 178 eventID := lastGeneratedEvent.GetEventId() + 1 179 version := input[2].(int64) 180 historyEvent := getDefaultHistoryEvent(eventID, version) 181 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_TASK_FAILED 182 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowTaskFailedEventAttributes{WorkflowTaskFailedEventAttributes: &historypb.WorkflowTaskFailedEventAttributes{ 183 ScheduledEventId: lastEvent.GetWorkflowTaskStartedEventAttributes().ScheduledEventId, 184 StartedEventId: lastEvent.EventId, 185 Cause: enumspb.WORKFLOW_TASK_FAILED_CAUSE_UNHANDLED_COMMAND, 186 Identity: identity, 187 ForkEventVersion: version, 188 }} 189 return historyEvent 190 }) 191 workflowTaskTimedOut := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_TASK_TIMED_OUT.String()) 192 workflowTaskTimedOut.SetDataFunc(func(input ...interface{}) interface{} { 193 lastEvent := input[0].(*historypb.HistoryEvent) 194 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 195 eventID := lastGeneratedEvent.GetEventId() + 1 196 version := input[2].(int64) 197 historyEvent := getDefaultHistoryEvent(eventID, version) 198 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_TASK_TIMED_OUT 199 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowTaskTimedOutEventAttributes{WorkflowTaskTimedOutEventAttributes: &historypb.WorkflowTaskTimedOutEventAttributes{ 200 ScheduledEventId: lastEvent.GetWorkflowTaskStartedEventAttributes().ScheduledEventId, 201 StartedEventId: lastEvent.EventId, 202 TimeoutType: enumspb.TIMEOUT_TYPE_SCHEDULE_TO_START, 203 }} 204 return historyEvent 205 }) 206 workflowTaskComplete := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_TASK_COMPLETED.String()) 207 workflowTaskComplete.SetDataFunc(func(input ...interface{}) interface{} { 208 lastEvent := input[0].(*historypb.HistoryEvent) 209 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 210 eventID := lastGeneratedEvent.GetEventId() + 1 211 version := input[2].(int64) 212 historyEvent := getDefaultHistoryEvent(eventID, version) 213 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_TASK_COMPLETED 214 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowTaskCompletedEventAttributes{WorkflowTaskCompletedEventAttributes: &historypb.WorkflowTaskCompletedEventAttributes{ 215 ScheduledEventId: lastEvent.GetWorkflowTaskStartedEventAttributes().ScheduledEventId, 216 StartedEventId: lastEvent.EventId, 217 Identity: identity, 218 BinaryChecksum: checksum, 219 }} 220 return historyEvent 221 }) 222 workflowTaskComplete.SetIsStrictOnNextVertex(true) 223 workflowTaskComplete.SetMaxNextVertex(2) 224 workflowTaskScheduleToStart := NewHistoryEventEdge(workflowTaskSchedule, workflowTaskStart) 225 workflowTaskStartToComplete := NewHistoryEventEdge(workflowTaskStart, workflowTaskComplete) 226 workflowTaskStartToFail := NewHistoryEventEdge(workflowTaskStart, workflowTaskFail) 227 workflowTaskStartToTimedOut := NewHistoryEventEdge(workflowTaskStart, workflowTaskTimedOut) 228 workflowTaskFailToSchedule := NewHistoryEventEdge(workflowTaskFail, workflowTaskSchedule) 229 workflowTaskFailToSchedule.SetCondition(notPendingWorkflowTask) 230 workflowTaskTimedOutToSchedule := NewHistoryEventEdge(workflowTaskTimedOut, workflowTaskSchedule) 231 workflowTaskTimedOutToSchedule.SetCondition(notPendingWorkflowTask) 232 historyEventModel.AddEdge(workflowTaskScheduleToStart, workflowTaskStartToComplete, workflowTaskStartToFail, workflowTaskStartToTimedOut, 233 workflowTaskFailToSchedule, workflowTaskTimedOutToSchedule) 234 235 // Setup workflow model 236 workflowModel := NewHistoryEventModel() 237 238 workflowStart := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED.String()) 239 workflowStart.SetDataFunc(func(input ...interface{}) interface{} { 240 historyEvent := getDefaultHistoryEvent(1, defaultVersion) 241 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED 242 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionStartedEventAttributes{WorkflowExecutionStartedEventAttributes: &historypb.WorkflowExecutionStartedEventAttributes{ 243 WorkflowType: &commonpb.WorkflowType{ 244 Name: workflowType, 245 }, 246 TaskQueue: &taskqueuepb.TaskQueue{ 247 Name: taskQueue, 248 Kind: enumspb.TASK_QUEUE_KIND_NORMAL, 249 }, 250 WorkflowExecutionTimeout: durationpb.New(timeout), 251 WorkflowRunTimeout: durationpb.New(timeout), 252 WorkflowTaskTimeout: durationpb.New(timeout), 253 Identity: identity, 254 FirstExecutionRunId: uuid.New(), 255 Attempt: 1, 256 }} 257 return historyEvent 258 }) 259 workflowSignal := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED.String()) 260 workflowSignal.SetDataFunc(func(input ...interface{}) interface{} { 261 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 262 eventID := lastGeneratedEvent.GetEventId() + 1 263 version := input[2].(int64) 264 historyEvent := getDefaultHistoryEvent(eventID, version) 265 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED 266 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionSignaledEventAttributes{WorkflowExecutionSignaledEventAttributes: &historypb.WorkflowExecutionSignaledEventAttributes{ 267 SignalName: signal, 268 Identity: identity, 269 }} 270 return historyEvent 271 }) 272 workflowComplete := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED.String()) 273 workflowComplete.SetDataFunc(func(input ...interface{}) interface{} { 274 lastEvent := input[0].(*historypb.HistoryEvent) 275 eventID := lastEvent.GetEventId() + 1 276 version := input[2].(int64) 277 historyEvent := getDefaultHistoryEvent(eventID, version) 278 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED 279 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionCompletedEventAttributes{WorkflowExecutionCompletedEventAttributes: &historypb.WorkflowExecutionCompletedEventAttributes{ 280 WorkflowTaskCompletedEventId: lastEvent.EventId, 281 }} 282 return historyEvent 283 }) 284 continueAsNew := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_CONTINUED_AS_NEW.String()) 285 continueAsNew.SetDataFunc(func(input ...interface{}) interface{} { 286 lastEvent := input[0].(*historypb.HistoryEvent) 287 eventID := lastEvent.GetEventId() + 1 288 version := input[2].(int64) 289 historyEvent := getDefaultHistoryEvent(eventID, version) 290 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_CONTINUED_AS_NEW 291 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionContinuedAsNewEventAttributes{WorkflowExecutionContinuedAsNewEventAttributes: &historypb.WorkflowExecutionContinuedAsNewEventAttributes{ 292 NewExecutionRunId: uuid.New(), 293 WorkflowType: &commonpb.WorkflowType{ 294 Name: workflowType, 295 }, 296 TaskQueue: &taskqueuepb.TaskQueue{ 297 Name: taskQueue, 298 Kind: enumspb.TASK_QUEUE_KIND_NORMAL, 299 }, 300 WorkflowRunTimeout: durationpb.New(timeout), 301 WorkflowTaskTimeout: durationpb.New(timeout), 302 WorkflowTaskCompletedEventId: eventID - 1, 303 Initiator: enumspb.CONTINUE_AS_NEW_INITIATOR_WORKFLOW, 304 }} 305 return historyEvent 306 }) 307 workflowFail := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_FAILED.String()) 308 workflowFail.SetDataFunc(func(input ...interface{}) interface{} { 309 lastEvent := input[0].(*historypb.HistoryEvent) 310 eventID := lastEvent.GetEventId() + 1 311 version := input[2].(int64) 312 historyEvent := getDefaultHistoryEvent(eventID, version) 313 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_FAILED 314 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionFailedEventAttributes{WorkflowExecutionFailedEventAttributes: &historypb.WorkflowExecutionFailedEventAttributes{ 315 WorkflowTaskCompletedEventId: lastEvent.EventId, 316 }} 317 return historyEvent 318 }) 319 workflowCancel := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED.String()) 320 workflowCancel.SetDataFunc(func(input ...interface{}) interface{} { 321 lastEvent := input[0].(*historypb.HistoryEvent) 322 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 323 eventID := lastGeneratedEvent.GetEventId() + 1 324 version := input[2].(int64) 325 historyEvent := getDefaultHistoryEvent(eventID, version) 326 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED 327 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionCanceledEventAttributes{WorkflowExecutionCanceledEventAttributes: &historypb.WorkflowExecutionCanceledEventAttributes{ 328 WorkflowTaskCompletedEventId: lastEvent.EventId, 329 }} 330 return historyEvent 331 }) 332 workflowCancelRequest := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED.String()) 333 workflowCancelRequest.SetDataFunc(func(input ...interface{}) interface{} { 334 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 335 eventID := lastGeneratedEvent.GetEventId() + 1 336 version := input[2].(int64) 337 historyEvent := getDefaultHistoryEvent(eventID, version) 338 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_CANCEL_REQUESTED 339 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionCancelRequestedEventAttributes{WorkflowExecutionCancelRequestedEventAttributes: &historypb.WorkflowExecutionCancelRequestedEventAttributes{ 340 Cause: "", 341 ExternalInitiatedEventId: 1, 342 ExternalWorkflowExecution: &commonpb.WorkflowExecution{ 343 WorkflowId: externalWorkflowID, 344 RunId: uuid.New(), 345 }, 346 Identity: identity, 347 }} 348 return historyEvent 349 }) 350 workflowTerminate := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_TERMINATED.String()) 351 workflowTerminate.SetDataFunc(func(input ...interface{}) interface{} { 352 lastEvent := input[0].(*historypb.HistoryEvent) 353 eventID := lastEvent.GetEventId() + 1 354 version := input[2].(int64) 355 historyEvent := getDefaultHistoryEvent(eventID, version) 356 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_TERMINATED 357 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionTerminatedEventAttributes{WorkflowExecutionTerminatedEventAttributes: &historypb.WorkflowExecutionTerminatedEventAttributes{ 358 Identity: identity, 359 Reason: reason, 360 }} 361 return historyEvent 362 }) 363 workflowTimedOut := NewHistoryEventVertex(enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_TIMED_OUT.String()) 364 workflowTimedOut.SetDataFunc(func(input ...interface{}) interface{} { 365 lastEvent := input[0].(*historypb.HistoryEvent) 366 eventID := lastEvent.GetEventId() + 1 367 version := input[2].(int64) 368 historyEvent := getDefaultHistoryEvent(eventID, version) 369 historyEvent.EventType = enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_TIMED_OUT 370 historyEvent.Attributes = &historypb.HistoryEvent_WorkflowExecutionTimedOutEventAttributes{WorkflowExecutionTimedOutEventAttributes: &historypb.WorkflowExecutionTimedOutEventAttributes{ 371 RetryState: enumspb.RETRY_STATE_TIMEOUT, 372 }} 373 return historyEvent 374 }) 375 workflowStartToSignal := NewHistoryEventEdge(workflowStart, workflowSignal) 376 workflowStartToWorkflowTaskSchedule := NewHistoryEventEdge(workflowStart, workflowTaskSchedule) 377 workflowStartToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 378 workflowSignalToWorkflowTaskSchedule := NewHistoryEventEdge(workflowSignal, workflowTaskSchedule) 379 workflowSignalToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 380 workflowTaskCompleteToWorkflowComplete := NewHistoryEventEdge(workflowTaskComplete, workflowComplete) 381 workflowTaskCompleteToWorkflowComplete.SetCondition(containActivityComplete) 382 workflowTaskCompleteToWorkflowFailed := NewHistoryEventEdge(workflowTaskComplete, workflowFail) 383 workflowTaskCompleteToWorkflowFailed.SetCondition(containActivityComplete) 384 workflowTaskCompleteToCAN := NewHistoryEventEdge(workflowTaskComplete, continueAsNew) 385 workflowTaskCompleteToCAN.SetCondition(containActivityComplete) 386 workflowCancelRequestToCancel := NewHistoryEventEdge(workflowCancelRequest, workflowCancel) 387 workflowModel.AddEdge(workflowStartToSignal, workflowStartToWorkflowTaskSchedule, workflowSignalToWorkflowTaskSchedule, 388 workflowTaskCompleteToCAN, workflowTaskCompleteToWorkflowComplete, workflowTaskCompleteToWorkflowFailed, workflowCancelRequestToCancel) 389 390 // Setup activity model 391 activityModel := NewHistoryEventModel() 392 activitySchedule := NewHistoryEventVertex(enumspb.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED.String()) 393 activitySchedule.SetDataFunc(func(input ...interface{}) interface{} { 394 lastEvent := input[0].(*historypb.HistoryEvent) 395 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 396 eventID := lastGeneratedEvent.GetEventId() + 1 397 version := input[2].(int64) 398 historyEvent := getDefaultHistoryEvent(eventID, version) 399 historyEvent.EventType = enumspb.EVENT_TYPE_ACTIVITY_TASK_SCHEDULED 400 historyEvent.Attributes = &historypb.HistoryEvent_ActivityTaskScheduledEventAttributes{ActivityTaskScheduledEventAttributes: &historypb.ActivityTaskScheduledEventAttributes{ 401 ActivityId: uuid.New(), 402 ActivityType: &commonpb.ActivityType{Name: "activity"}, 403 TaskQueue: &taskqueuepb.TaskQueue{ 404 Name: taskQueue, 405 Kind: enumspb.TASK_QUEUE_KIND_NORMAL, 406 }, 407 ScheduleToCloseTimeout: durationpb.New(timeout), 408 ScheduleToStartTimeout: durationpb.New(timeout), 409 StartToCloseTimeout: durationpb.New(timeout), 410 WorkflowTaskCompletedEventId: lastEvent.EventId, 411 }} 412 return historyEvent 413 }) 414 activityStart := NewHistoryEventVertex(enumspb.EVENT_TYPE_ACTIVITY_TASK_STARTED.String()) 415 activityStart.SetDataFunc(func(input ...interface{}) interface{} { 416 lastEvent := input[0].(*historypb.HistoryEvent) 417 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 418 eventID := lastGeneratedEvent.GetEventId() + 1 419 version := input[2].(int64) 420 historyEvent := getDefaultHistoryEvent(eventID, version) 421 historyEvent.EventType = enumspb.EVENT_TYPE_ACTIVITY_TASK_STARTED 422 historyEvent.Attributes = &historypb.HistoryEvent_ActivityTaskStartedEventAttributes{ActivityTaskStartedEventAttributes: &historypb.ActivityTaskStartedEventAttributes{ 423 ScheduledEventId: lastEvent.EventId, 424 Identity: identity, 425 RequestId: uuid.New(), 426 Attempt: 1, 427 }} 428 return historyEvent 429 }) 430 activityComplete := NewHistoryEventVertex(enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED.String()) 431 activityComplete.SetDataFunc(func(input ...interface{}) interface{} { 432 lastEvent := input[0].(*historypb.HistoryEvent) 433 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 434 eventID := lastGeneratedEvent.GetEventId() + 1 435 version := input[2].(int64) 436 historyEvent := getDefaultHistoryEvent(eventID, version) 437 historyEvent.EventType = enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED 438 historyEvent.Attributes = &historypb.HistoryEvent_ActivityTaskCompletedEventAttributes{ActivityTaskCompletedEventAttributes: &historypb.ActivityTaskCompletedEventAttributes{ 439 ScheduledEventId: lastEvent.GetActivityTaskStartedEventAttributes().ScheduledEventId, 440 StartedEventId: lastEvent.EventId, 441 Identity: identity, 442 }} 443 return historyEvent 444 }) 445 activityFail := NewHistoryEventVertex(enumspb.EVENT_TYPE_ACTIVITY_TASK_FAILED.String()) 446 activityFail.SetDataFunc(func(input ...interface{}) interface{} { 447 lastEvent := input[0].(*historypb.HistoryEvent) 448 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 449 eventID := lastGeneratedEvent.GetEventId() + 1 450 version := input[2].(int64) 451 historyEvent := getDefaultHistoryEvent(eventID, version) 452 historyEvent.EventType = enumspb.EVENT_TYPE_ACTIVITY_TASK_FAILED 453 historyEvent.Attributes = &historypb.HistoryEvent_ActivityTaskFailedEventAttributes{ActivityTaskFailedEventAttributes: &historypb.ActivityTaskFailedEventAttributes{ 454 ScheduledEventId: lastEvent.GetActivityTaskStartedEventAttributes().ScheduledEventId, 455 StartedEventId: lastEvent.EventId, 456 Identity: identity, 457 Failure: failure.NewServerFailure(reason, false), 458 }} 459 return historyEvent 460 }) 461 activityTimedOut := NewHistoryEventVertex(enumspb.EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT.String()) 462 activityTimedOut.SetDataFunc(func(input ...interface{}) interface{} { 463 lastEvent := input[0].(*historypb.HistoryEvent) 464 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 465 eventID := lastGeneratedEvent.GetEventId() + 1 466 version := input[2].(int64) 467 historyEvent := getDefaultHistoryEvent(eventID, version) 468 historyEvent.EventType = enumspb.EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT 469 historyEvent.Attributes = &historypb.HistoryEvent_ActivityTaskTimedOutEventAttributes{ActivityTaskTimedOutEventAttributes: &historypb.ActivityTaskTimedOutEventAttributes{ 470 ScheduledEventId: lastEvent.GetActivityTaskStartedEventAttributes().ScheduledEventId, 471 StartedEventId: lastEvent.EventId, 472 Failure: &failurepb.Failure{ 473 FailureInfo: &failurepb.Failure_TimeoutFailureInfo{TimeoutFailureInfo: &failurepb.TimeoutFailureInfo{ 474 TimeoutType: enumspb.TIMEOUT_TYPE_SCHEDULE_TO_CLOSE, 475 }}, 476 }, 477 }} 478 return historyEvent 479 }) 480 activityCancelRequest := NewHistoryEventVertex(enumspb.EVENT_TYPE_ACTIVITY_TASK_CANCEL_REQUESTED.String()) 481 activityCancelRequest.SetDataFunc(func(input ...interface{}) interface{} { 482 lastEvent := input[0].(*historypb.HistoryEvent) 483 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 484 eventID := lastGeneratedEvent.GetEventId() + 1 485 version := input[2].(int64) 486 historyEvent := getDefaultHistoryEvent(eventID, version) 487 historyEvent.EventType = enumspb.EVENT_TYPE_ACTIVITY_TASK_CANCEL_REQUESTED 488 historyEvent.Attributes = &historypb.HistoryEvent_ActivityTaskCancelRequestedEventAttributes{ActivityTaskCancelRequestedEventAttributes: &historypb.ActivityTaskCancelRequestedEventAttributes{ 489 WorkflowTaskCompletedEventId: lastEvent.GetActivityTaskScheduledEventAttributes().WorkflowTaskCompletedEventId, 490 ScheduledEventId: lastEvent.GetEventId(), 491 }} 492 return historyEvent 493 }) 494 activityCancel := NewHistoryEventVertex(enumspb.EVENT_TYPE_ACTIVITY_TASK_CANCELED.String()) 495 activityCancel.SetDataFunc(func(input ...interface{}) interface{} { 496 lastEvent := input[0].(*historypb.HistoryEvent) 497 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 498 eventID := lastGeneratedEvent.GetEventId() + 1 499 version := input[2].(int64) 500 historyEvent := getDefaultHistoryEvent(eventID, version) 501 historyEvent.EventType = enumspb.EVENT_TYPE_ACTIVITY_TASK_CANCELED 502 historyEvent.Attributes = &historypb.HistoryEvent_ActivityTaskCanceledEventAttributes{ActivityTaskCanceledEventAttributes: &historypb.ActivityTaskCanceledEventAttributes{ 503 LatestCancelRequestedEventId: lastEvent.EventId, 504 ScheduledEventId: lastEvent.EventId, 505 StartedEventId: lastEvent.EventId, 506 Identity: identity, 507 }} 508 return historyEvent 509 }) 510 workflowTaskCompleteToATSchedule := NewHistoryEventEdge(workflowTaskComplete, activitySchedule) 511 512 activityScheduleToStart := NewHistoryEventEdge(activitySchedule, activityStart) 513 activityScheduleToStart.SetCondition(hasPendingActivity) 514 515 activityStartToComplete := NewHistoryEventEdge(activityStart, activityComplete) 516 activityStartToComplete.SetCondition(hasPendingActivity) 517 518 activityStartToFail := NewHistoryEventEdge(activityStart, activityFail) 519 activityStartToFail.SetCondition(hasPendingActivity) 520 521 activityStartToTimedOut := NewHistoryEventEdge(activityStart, activityTimedOut) 522 activityStartToTimedOut.SetCondition(hasPendingActivity) 523 524 activityCompleteToWorkflowTaskSchedule := NewHistoryEventEdge(activityComplete, workflowTaskSchedule) 525 activityCompleteToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 526 activityFailToWorkflowTaskSchedule := NewHistoryEventEdge(activityFail, workflowTaskSchedule) 527 activityFailToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 528 activityTimedOutToWorkflowTaskSchedule := NewHistoryEventEdge(activityTimedOut, workflowTaskSchedule) 529 activityTimedOutToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 530 activityCancelToWorkflowTaskSchedule := NewHistoryEventEdge(activityCancel, workflowTaskSchedule) 531 activityCancelToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 532 533 // TODO: bypass activity cancel request event. Support this event later. 534 // activityScheduleToActivityCancelRequest := NewHistoryEventEdge(activitySchedule, activityCancelRequest) 535 // activityScheduleToActivityCancelRequest.SetCondition(hasPendingActivity) 536 activityCancelReqToCancel := NewHistoryEventEdge(activityCancelRequest, activityCancel) 537 activityCancelReqToCancel.SetCondition(hasPendingActivity) 538 539 activityModel.AddEdge(workflowTaskCompleteToATSchedule, activityScheduleToStart, activityStartToComplete, 540 activityStartToFail, activityStartToTimedOut, workflowTaskCompleteToATSchedule, activityCompleteToWorkflowTaskSchedule, 541 activityFailToWorkflowTaskSchedule, activityTimedOutToWorkflowTaskSchedule, activityCancelReqToCancel, 542 activityCancelToWorkflowTaskSchedule) 543 544 // Setup timer model 545 timerModel := NewHistoryEventModel() 546 timerStart := NewHistoryEventVertex(enumspb.EVENT_TYPE_TIMER_STARTED.String()) 547 timerStart.SetDataFunc(func(input ...interface{}) interface{} { 548 lastEvent := input[0].(*historypb.HistoryEvent) 549 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 550 eventID := lastGeneratedEvent.GetEventId() + 1 551 version := input[2].(int64) 552 historyEvent := getDefaultHistoryEvent(eventID, version) 553 historyEvent.EventType = enumspb.EVENT_TYPE_TIMER_STARTED 554 historyEvent.Attributes = &historypb.HistoryEvent_TimerStartedEventAttributes{TimerStartedEventAttributes: &historypb.TimerStartedEventAttributes{ 555 TimerId: uuid.New(), 556 StartToFireTimeout: durationpb.New(10 * time.Second), 557 WorkflowTaskCompletedEventId: lastEvent.EventId, 558 }} 559 return historyEvent 560 }) 561 timerFired := NewHistoryEventVertex(enumspb.EVENT_TYPE_TIMER_FIRED.String()) 562 timerFired.SetDataFunc(func(input ...interface{}) interface{} { 563 lastEvent := input[0].(*historypb.HistoryEvent) 564 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 565 eventID := lastGeneratedEvent.GetEventId() + 1 566 version := input[2].(int64) 567 historyEvent := getDefaultHistoryEvent(eventID, version) 568 historyEvent.EventType = enumspb.EVENT_TYPE_TIMER_FIRED 569 historyEvent.Attributes = &historypb.HistoryEvent_TimerFiredEventAttributes{TimerFiredEventAttributes: &historypb.TimerFiredEventAttributes{ 570 TimerId: lastEvent.GetTimerStartedEventAttributes().TimerId, 571 StartedEventId: lastEvent.EventId, 572 }} 573 return historyEvent 574 }) 575 timerCancel := NewHistoryEventVertex(enumspb.EVENT_TYPE_TIMER_CANCELED.String()) 576 timerCancel.SetDataFunc(func(input ...interface{}) interface{} { 577 lastEvent := input[0].(*historypb.HistoryEvent) 578 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 579 eventID := lastGeneratedEvent.GetEventId() + 1 580 version := input[2].(int64) 581 historyEvent := getDefaultHistoryEvent(eventID, version) 582 historyEvent.EventType = enumspb.EVENT_TYPE_TIMER_CANCELED 583 historyEvent.Attributes = &historypb.HistoryEvent_TimerCanceledEventAttributes{TimerCanceledEventAttributes: &historypb.TimerCanceledEventAttributes{ 584 TimerId: lastEvent.GetTimerStartedEventAttributes().TimerId, 585 StartedEventId: lastEvent.EventId, 586 WorkflowTaskCompletedEventId: lastEvent.GetTimerStartedEventAttributes().WorkflowTaskCompletedEventId, 587 Identity: identity, 588 }} 589 return historyEvent 590 }) 591 timerStartToFire := NewHistoryEventEdge(timerStart, timerFired) 592 timerStartToCancel := NewHistoryEventEdge(timerStart, timerCancel) 593 594 workflowTaskCompleteToTimerStart := NewHistoryEventEdge(workflowTaskComplete, timerStart) 595 timerFiredToWorkflowTaskSchedule := NewHistoryEventEdge(timerFired, workflowTaskSchedule) 596 timerFiredToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 597 timerCancelToWorkflowTaskSchedule := NewHistoryEventEdge(timerCancel, workflowTaskSchedule) 598 timerCancelToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 599 timerModel.AddEdge(timerStartToFire, timerStartToCancel, workflowTaskCompleteToTimerStart, timerFiredToWorkflowTaskSchedule, timerCancelToWorkflowTaskSchedule) 600 601 // Setup child workflow model 602 childWorkflowModel := NewHistoryEventModel() 603 childWorkflowInitial := NewHistoryEventVertex(enumspb.EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_INITIATED.String()) 604 childWorkflowInitial.SetDataFunc(func(input ...interface{}) interface{} { 605 lastEvent := input[0].(*historypb.HistoryEvent) 606 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 607 eventID := lastGeneratedEvent.GetEventId() + 1 608 version := input[2].(int64) 609 historyEvent := getDefaultHistoryEvent(eventID, version) 610 historyEvent.EventType = enumspb.EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_INITIATED 611 historyEvent.Attributes = &historypb.HistoryEvent_StartChildWorkflowExecutionInitiatedEventAttributes{StartChildWorkflowExecutionInitiatedEventAttributes: &historypb.StartChildWorkflowExecutionInitiatedEventAttributes{ 612 Namespace: nsName.String(), 613 NamespaceId: nsID.String(), 614 WorkflowId: childWorkflowID, 615 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 616 TaskQueue: &taskqueuepb.TaskQueue{ 617 Name: taskQueue, 618 Kind: enumspb.TASK_QUEUE_KIND_NORMAL, 619 }, 620 WorkflowExecutionTimeout: durationpb.New(timeout), 621 WorkflowRunTimeout: durationpb.New(timeout), 622 WorkflowTaskTimeout: durationpb.New(timeout), 623 WorkflowTaskCompletedEventId: lastEvent.EventId, 624 WorkflowIdReusePolicy: enumspb.WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE, 625 }} 626 return historyEvent 627 }) 628 childWorkflowInitialFail := NewHistoryEventVertex(enumspb.EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_FAILED.String()) 629 childWorkflowInitialFail.SetDataFunc(func(input ...interface{}) interface{} { 630 lastEvent := input[0].(*historypb.HistoryEvent) 631 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 632 eventID := lastGeneratedEvent.GetEventId() + 1 633 version := input[2].(int64) 634 historyEvent := getDefaultHistoryEvent(eventID, version) 635 historyEvent.EventType = enumspb.EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_FAILED 636 historyEvent.Attributes = &historypb.HistoryEvent_StartChildWorkflowExecutionFailedEventAttributes{StartChildWorkflowExecutionFailedEventAttributes: &historypb.StartChildWorkflowExecutionFailedEventAttributes{ 637 Namespace: nsName.String(), 638 NamespaceId: nsID.String(), 639 WorkflowId: childWorkflowID, 640 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 641 Cause: enumspb.START_CHILD_WORKFLOW_EXECUTION_FAILED_CAUSE_WORKFLOW_ALREADY_EXISTS, 642 InitiatedEventId: lastEvent.EventId, 643 WorkflowTaskCompletedEventId: lastEvent.GetStartChildWorkflowExecutionInitiatedEventAttributes().WorkflowTaskCompletedEventId, 644 }} 645 return historyEvent 646 }) 647 childWorkflowStart := NewHistoryEventVertex(enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_STARTED.String()) 648 childWorkflowStart.SetDataFunc(func(input ...interface{}) interface{} { 649 lastEvent := input[0].(*historypb.HistoryEvent) 650 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 651 eventID := lastGeneratedEvent.GetEventId() + 1 652 version := input[2].(int64) 653 historyEvent := getDefaultHistoryEvent(eventID, version) 654 historyEvent.EventType = enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_STARTED 655 historyEvent.Attributes = &historypb.HistoryEvent_ChildWorkflowExecutionStartedEventAttributes{ChildWorkflowExecutionStartedEventAttributes: &historypb.ChildWorkflowExecutionStartedEventAttributes{ 656 Namespace: nsName.String(), 657 NamespaceId: nsID.String(), 658 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 659 InitiatedEventId: lastEvent.EventId, 660 WorkflowExecution: &commonpb.WorkflowExecution{ 661 WorkflowId: childWorkflowID, 662 RunId: uuid.New(), 663 }, 664 }} 665 return historyEvent 666 }) 667 childWorkflowCancel := NewHistoryEventVertex(enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_CANCELED.String()) 668 childWorkflowCancel.SetDataFunc(func(input ...interface{}) interface{} { 669 lastEvent := input[0].(*historypb.HistoryEvent) 670 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 671 eventID := lastGeneratedEvent.GetEventId() + 1 672 version := input[2].(int64) 673 historyEvent := getDefaultHistoryEvent(eventID, version) 674 historyEvent.EventType = enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_CANCELED 675 historyEvent.Attributes = &historypb.HistoryEvent_ChildWorkflowExecutionCanceledEventAttributes{ChildWorkflowExecutionCanceledEventAttributes: &historypb.ChildWorkflowExecutionCanceledEventAttributes{ 676 Namespace: nsName.String(), 677 NamespaceId: nsID.String(), 678 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 679 InitiatedEventId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().InitiatedEventId, 680 WorkflowExecution: &commonpb.WorkflowExecution{ 681 WorkflowId: childWorkflowID, 682 RunId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().GetWorkflowExecution().RunId, 683 }, 684 StartedEventId: lastEvent.EventId, 685 }} 686 return historyEvent 687 }) 688 childWorkflowComplete := NewHistoryEventVertex(enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_COMPLETED.String()) 689 childWorkflowComplete.SetDataFunc(func(input ...interface{}) interface{} { 690 lastEvent := input[0].(*historypb.HistoryEvent) 691 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 692 eventID := lastGeneratedEvent.GetEventId() + 1 693 version := input[2].(int64) 694 historyEvent := getDefaultHistoryEvent(eventID, version) 695 historyEvent.EventType = enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_COMPLETED 696 historyEvent.Attributes = &historypb.HistoryEvent_ChildWorkflowExecutionCompletedEventAttributes{ChildWorkflowExecutionCompletedEventAttributes: &historypb.ChildWorkflowExecutionCompletedEventAttributes{ 697 Namespace: nsName.String(), 698 NamespaceId: nsID.String(), 699 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 700 InitiatedEventId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().InitiatedEventId, 701 WorkflowExecution: &commonpb.WorkflowExecution{ 702 WorkflowId: childWorkflowID, 703 RunId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().GetWorkflowExecution().RunId, 704 }, 705 StartedEventId: lastEvent.EventId, 706 }} 707 return historyEvent 708 }) 709 childWorkflowFail := NewHistoryEventVertex(enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_FAILED.String()) 710 childWorkflowFail.SetDataFunc(func(input ...interface{}) interface{} { 711 lastEvent := input[0].(*historypb.HistoryEvent) 712 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 713 eventID := lastGeneratedEvent.GetEventId() + 1 714 version := input[2].(int64) 715 historyEvent := getDefaultHistoryEvent(eventID, version) 716 historyEvent.EventType = enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_FAILED 717 historyEvent.Attributes = &historypb.HistoryEvent_ChildWorkflowExecutionFailedEventAttributes{ChildWorkflowExecutionFailedEventAttributes: &historypb.ChildWorkflowExecutionFailedEventAttributes{ 718 Namespace: nsName.String(), 719 NamespaceId: nsID.String(), 720 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 721 InitiatedEventId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().InitiatedEventId, 722 WorkflowExecution: &commonpb.WorkflowExecution{ 723 WorkflowId: childWorkflowID, 724 RunId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().GetWorkflowExecution().RunId, 725 }, 726 StartedEventId: lastEvent.EventId, 727 }} 728 return historyEvent 729 }) 730 childWorkflowTerminate := NewHistoryEventVertex(enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TERMINATED.String()) 731 childWorkflowTerminate.SetDataFunc(func(input ...interface{}) interface{} { 732 lastEvent := input[0].(*historypb.HistoryEvent) 733 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 734 eventID := lastGeneratedEvent.GetEventId() + 1 735 version := input[2].(int64) 736 historyEvent := getDefaultHistoryEvent(eventID, version) 737 historyEvent.EventType = enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TERMINATED 738 historyEvent.Attributes = &historypb.HistoryEvent_ChildWorkflowExecutionTerminatedEventAttributes{ChildWorkflowExecutionTerminatedEventAttributes: &historypb.ChildWorkflowExecutionTerminatedEventAttributes{ 739 Namespace: nsName.String(), 740 NamespaceId: nsID.String(), 741 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 742 InitiatedEventId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().InitiatedEventId, 743 WorkflowExecution: &commonpb.WorkflowExecution{ 744 WorkflowId: childWorkflowID, 745 RunId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().GetWorkflowExecution().RunId, 746 }, 747 StartedEventId: lastEvent.EventId, 748 }} 749 return historyEvent 750 }) 751 childWorkflowTimedOut := NewHistoryEventVertex(enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TIMED_OUT.String()) 752 childWorkflowTimedOut.SetDataFunc(func(input ...interface{}) interface{} { 753 lastEvent := input[0].(*historypb.HistoryEvent) 754 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 755 eventID := lastGeneratedEvent.GetEventId() + 1 756 version := input[2].(int64) 757 historyEvent := getDefaultHistoryEvent(eventID, version) 758 historyEvent.EventType = enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TIMED_OUT 759 historyEvent.Attributes = &historypb.HistoryEvent_ChildWorkflowExecutionTimedOutEventAttributes{ChildWorkflowExecutionTimedOutEventAttributes: &historypb.ChildWorkflowExecutionTimedOutEventAttributes{ 760 Namespace: nsName.String(), 761 NamespaceId: nsID.String(), 762 WorkflowType: &commonpb.WorkflowType{Name: childWorkflowPrefix + workflowType}, 763 InitiatedEventId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().InitiatedEventId, 764 WorkflowExecution: &commonpb.WorkflowExecution{ 765 WorkflowId: childWorkflowID, 766 RunId: lastEvent.GetChildWorkflowExecutionStartedEventAttributes().GetWorkflowExecution().RunId, 767 }, 768 StartedEventId: lastEvent.EventId, 769 RetryState: enumspb.RETRY_STATE_TIMEOUT, 770 }} 771 return historyEvent 772 }) 773 workflowTaskCompleteToChildWorkflowInitial := NewHistoryEventEdge(workflowTaskComplete, childWorkflowInitial) 774 childWorkflowInitialToFail := NewHistoryEventEdge(childWorkflowInitial, childWorkflowInitialFail) 775 childWorkflowInitialToStart := NewHistoryEventEdge(childWorkflowInitial, childWorkflowStart) 776 childWorkflowStartToCancel := NewHistoryEventEdge(childWorkflowStart, childWorkflowCancel) 777 childWorkflowStartToFail := NewHistoryEventEdge(childWorkflowStart, childWorkflowFail) 778 childWorkflowStartToComplete := NewHistoryEventEdge(childWorkflowStart, childWorkflowComplete) 779 childWorkflowStartToTerminate := NewHistoryEventEdge(childWorkflowStart, childWorkflowTerminate) 780 childWorkflowStartToTimedOut := NewHistoryEventEdge(childWorkflowStart, childWorkflowTimedOut) 781 childWorkflowCancelToWorkflowTaskSchedule := NewHistoryEventEdge(childWorkflowCancel, workflowTaskSchedule) 782 childWorkflowCancelToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 783 childWorkflowFailToWorkflowTaskSchedule := NewHistoryEventEdge(childWorkflowFail, workflowTaskSchedule) 784 childWorkflowFailToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 785 childWorkflowCompleteToWorkflowTaskSchedule := NewHistoryEventEdge(childWorkflowComplete, workflowTaskSchedule) 786 childWorkflowCompleteToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 787 childWorkflowTerminateToWorkflowTaskSchedule := NewHistoryEventEdge(childWorkflowTerminate, workflowTaskSchedule) 788 childWorkflowTerminateToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 789 childWorkflowTimedOutToWorkflowTaskSchedule := NewHistoryEventEdge(childWorkflowTimedOut, workflowTaskSchedule) 790 childWorkflowTimedOutToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 791 childWorkflowInitialFailToWorkflowTaskSchedule := NewHistoryEventEdge(childWorkflowInitialFail, workflowTaskSchedule) 792 childWorkflowInitialFailToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 793 childWorkflowModel.AddEdge(workflowTaskCompleteToChildWorkflowInitial, childWorkflowInitialToFail, childWorkflowInitialToStart, 794 childWorkflowStartToCancel, childWorkflowStartToFail, childWorkflowStartToComplete, childWorkflowStartToTerminate, 795 childWorkflowStartToTimedOut, childWorkflowCancelToWorkflowTaskSchedule, childWorkflowFailToWorkflowTaskSchedule, 796 childWorkflowCompleteToWorkflowTaskSchedule, childWorkflowTerminateToWorkflowTaskSchedule, childWorkflowTimedOutToWorkflowTaskSchedule, 797 childWorkflowInitialFailToWorkflowTaskSchedule) 798 799 // Setup external workflow model 800 externalWorkflowModel := NewHistoryEventModel() 801 externalWorkflowSignal := NewHistoryEventVertex(enumspb.EVENT_TYPE_SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED.String()) 802 externalWorkflowSignal.SetDataFunc(func(input ...interface{}) interface{} { 803 lastEvent := input[0].(*historypb.HistoryEvent) 804 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 805 eventID := lastGeneratedEvent.GetEventId() + 1 806 version := input[2].(int64) 807 historyEvent := getDefaultHistoryEvent(eventID, version) 808 historyEvent.EventType = enumspb.EVENT_TYPE_SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED 809 historyEvent.Attributes = &historypb.HistoryEvent_SignalExternalWorkflowExecutionInitiatedEventAttributes{SignalExternalWorkflowExecutionInitiatedEventAttributes: &historypb.SignalExternalWorkflowExecutionInitiatedEventAttributes{ 810 WorkflowTaskCompletedEventId: lastEvent.EventId, 811 Namespace: nsName.String(), 812 NamespaceId: nsID.String(), 813 WorkflowExecution: &commonpb.WorkflowExecution{ 814 WorkflowId: externalWorkflowID, 815 RunId: uuid.New(), 816 }, 817 SignalName: "signal", 818 ChildWorkflowOnly: false, 819 }} 820 return historyEvent 821 }) 822 externalWorkflowSignalFailed := NewHistoryEventVertex(enumspb.EVENT_TYPE_SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_FAILED.String()) 823 externalWorkflowSignalFailed.SetDataFunc(func(input ...interface{}) interface{} { 824 lastEvent := input[0].(*historypb.HistoryEvent) 825 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 826 eventID := lastGeneratedEvent.GetEventId() + 1 827 version := input[2].(int64) 828 historyEvent := getDefaultHistoryEvent(eventID, version) 829 historyEvent.EventType = enumspb.EVENT_TYPE_SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_FAILED 830 historyEvent.Attributes = &historypb.HistoryEvent_SignalExternalWorkflowExecutionFailedEventAttributes{SignalExternalWorkflowExecutionFailedEventAttributes: &historypb.SignalExternalWorkflowExecutionFailedEventAttributes{ 831 Cause: enumspb.SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_FAILED_CAUSE_EXTERNAL_WORKFLOW_EXECUTION_NOT_FOUND, 832 WorkflowTaskCompletedEventId: lastEvent.GetSignalExternalWorkflowExecutionInitiatedEventAttributes().WorkflowTaskCompletedEventId, 833 Namespace: nsName.String(), 834 NamespaceId: nsID.String(), 835 WorkflowExecution: &commonpb.WorkflowExecution{ 836 WorkflowId: lastEvent.GetSignalExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().WorkflowId, 837 RunId: lastEvent.GetSignalExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().RunId, 838 }, 839 InitiatedEventId: lastEvent.EventId, 840 }} 841 return historyEvent 842 }) 843 externalWorkflowSignaled := NewHistoryEventVertex(enumspb.EVENT_TYPE_EXTERNAL_WORKFLOW_EXECUTION_SIGNALED.String()) 844 externalWorkflowSignaled.SetDataFunc(func(input ...interface{}) interface{} { 845 lastEvent := input[0].(*historypb.HistoryEvent) 846 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 847 eventID := lastGeneratedEvent.GetEventId() + 1 848 version := input[2].(int64) 849 historyEvent := getDefaultHistoryEvent(eventID, version) 850 historyEvent.EventType = enumspb.EVENT_TYPE_EXTERNAL_WORKFLOW_EXECUTION_SIGNALED 851 historyEvent.Attributes = &historypb.HistoryEvent_ExternalWorkflowExecutionSignaledEventAttributes{ExternalWorkflowExecutionSignaledEventAttributes: &historypb.ExternalWorkflowExecutionSignaledEventAttributes{ 852 InitiatedEventId: lastEvent.EventId, 853 Namespace: nsName.String(), 854 NamespaceId: nsID.String(), 855 WorkflowExecution: &commonpb.WorkflowExecution{ 856 WorkflowId: lastEvent.GetSignalExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().WorkflowId, 857 RunId: lastEvent.GetSignalExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().RunId, 858 }, 859 }} 860 return historyEvent 861 }) 862 externalWorkflowCancel := NewHistoryEventVertex(enumspb.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED.String()) 863 externalWorkflowCancel.SetDataFunc(func(input ...interface{}) interface{} { 864 lastEvent := input[0].(*historypb.HistoryEvent) 865 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 866 eventID := lastGeneratedEvent.GetEventId() + 1 867 version := input[2].(int64) 868 historyEvent := getDefaultHistoryEvent(eventID, version) 869 historyEvent.EventType = enumspb.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED 870 historyEvent.Attributes = &historypb.HistoryEvent_RequestCancelExternalWorkflowExecutionInitiatedEventAttributes{ 871 RequestCancelExternalWorkflowExecutionInitiatedEventAttributes: &historypb.RequestCancelExternalWorkflowExecutionInitiatedEventAttributes{ 872 WorkflowTaskCompletedEventId: lastEvent.EventId, 873 Namespace: nsName.String(), 874 NamespaceId: nsID.String(), 875 WorkflowExecution: &commonpb.WorkflowExecution{ 876 WorkflowId: externalWorkflowID, 877 RunId: uuid.New(), 878 }, 879 ChildWorkflowOnly: false, 880 }} 881 return historyEvent 882 }) 883 externalWorkflowCancelFail := NewHistoryEventVertex(enumspb.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_FAILED.String()) 884 externalWorkflowCancelFail.SetDataFunc(func(input ...interface{}) interface{} { 885 lastEvent := input[0].(*historypb.HistoryEvent) 886 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 887 eventID := lastGeneratedEvent.GetEventId() + 1 888 version := input[2].(int64) 889 historyEvent := getDefaultHistoryEvent(eventID, version) 890 historyEvent.EventType = enumspb.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_FAILED 891 historyEvent.Attributes = &historypb.HistoryEvent_RequestCancelExternalWorkflowExecutionFailedEventAttributes{RequestCancelExternalWorkflowExecutionFailedEventAttributes: &historypb.RequestCancelExternalWorkflowExecutionFailedEventAttributes{ 892 Cause: enumspb.CANCEL_EXTERNAL_WORKFLOW_EXECUTION_FAILED_CAUSE_EXTERNAL_WORKFLOW_EXECUTION_NOT_FOUND, 893 WorkflowTaskCompletedEventId: lastEvent.GetRequestCancelExternalWorkflowExecutionInitiatedEventAttributes().WorkflowTaskCompletedEventId, 894 Namespace: nsName.String(), 895 NamespaceId: nsID.String(), 896 WorkflowExecution: &commonpb.WorkflowExecution{ 897 WorkflowId: lastEvent.GetRequestCancelExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().WorkflowId, 898 RunId: lastEvent.GetRequestCancelExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().RunId, 899 }, 900 InitiatedEventId: lastEvent.EventId, 901 }} 902 return historyEvent 903 }) 904 externalWorkflowCanceled := NewHistoryEventVertex(enumspb.EVENT_TYPE_EXTERNAL_WORKFLOW_EXECUTION_CANCEL_REQUESTED.String()) 905 externalWorkflowCanceled.SetDataFunc(func(input ...interface{}) interface{} { 906 lastEvent := input[0].(*historypb.HistoryEvent) 907 lastGeneratedEvent := input[1].(*historypb.HistoryEvent) 908 eventID := lastGeneratedEvent.GetEventId() + 1 909 version := input[2].(int64) 910 historyEvent := getDefaultHistoryEvent(eventID, version) 911 historyEvent.EventType = enumspb.EVENT_TYPE_EXTERNAL_WORKFLOW_EXECUTION_CANCEL_REQUESTED 912 historyEvent.Attributes = &historypb.HistoryEvent_ExternalWorkflowExecutionCancelRequestedEventAttributes{ExternalWorkflowExecutionCancelRequestedEventAttributes: &historypb.ExternalWorkflowExecutionCancelRequestedEventAttributes{ 913 InitiatedEventId: lastEvent.EventId, 914 Namespace: nsName.String(), 915 NamespaceId: nsID.String(), 916 WorkflowExecution: &commonpb.WorkflowExecution{ 917 WorkflowId: lastEvent.GetRequestCancelExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().WorkflowId, 918 RunId: lastEvent.GetRequestCancelExternalWorkflowExecutionInitiatedEventAttributes().GetWorkflowExecution().RunId, 919 }, 920 }} 921 return historyEvent 922 }) 923 workflowTaskCompleteToExternalWorkflowSignal := NewHistoryEventEdge(workflowTaskComplete, externalWorkflowSignal) 924 workflowTaskCompleteToExternalWorkflowCancel := NewHistoryEventEdge(workflowTaskComplete, externalWorkflowCancel) 925 externalWorkflowSignalToFail := NewHistoryEventEdge(externalWorkflowSignal, externalWorkflowSignalFailed) 926 externalWorkflowSignalToSignaled := NewHistoryEventEdge(externalWorkflowSignal, externalWorkflowSignaled) 927 externalWorkflowCancelToFail := NewHistoryEventEdge(externalWorkflowCancel, externalWorkflowCancelFail) 928 externalWorkflowCancelToCanceled := NewHistoryEventEdge(externalWorkflowCancel, externalWorkflowCanceled) 929 externalWorkflowSignaledToWorkflowTaskSchedule := NewHistoryEventEdge(externalWorkflowSignaled, workflowTaskSchedule) 930 externalWorkflowSignaledToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 931 externalWorkflowSignalFailedToWorkflowTaskSchedule := NewHistoryEventEdge(externalWorkflowSignalFailed, workflowTaskSchedule) 932 externalWorkflowSignalFailedToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 933 externalWorkflowCanceledToWorkflowTaskSchedule := NewHistoryEventEdge(externalWorkflowCanceled, workflowTaskSchedule) 934 externalWorkflowCanceledToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 935 externalWorkflowCancelFailToWorkflowTaskSchedule := NewHistoryEventEdge(externalWorkflowCancelFail, workflowTaskSchedule) 936 externalWorkflowCancelFailToWorkflowTaskSchedule.SetCondition(notPendingWorkflowTask) 937 externalWorkflowModel.AddEdge(workflowTaskCompleteToExternalWorkflowSignal, workflowTaskCompleteToExternalWorkflowCancel, 938 externalWorkflowSignalToFail, externalWorkflowSignalToSignaled, externalWorkflowCancelToFail, externalWorkflowCancelToCanceled, 939 externalWorkflowSignaledToWorkflowTaskSchedule, externalWorkflowSignalFailedToWorkflowTaskSchedule, 940 externalWorkflowCanceledToWorkflowTaskSchedule, externalWorkflowCancelFailToWorkflowTaskSchedule) 941 942 // Config event generator 943 generator.SetBatchGenerationRule(canDoBatch) 944 generator.AddInitialEntryVertex(workflowStart) 945 generator.AddExitVertex(workflowComplete, workflowFail, workflowTerminate, workflowTimedOut, continueAsNew) 946 // generator.AddRandomEntryVertex(workflowSignal, workflowTerminate, workflowTimedOut) 947 generator.AddModel(historyEventModel) 948 generator.AddModel(workflowModel) 949 generator.AddModel(activityModel) 950 generator.AddModel(timerModel) 951 generator.AddModel(childWorkflowModel) 952 generator.AddModel(externalWorkflowModel) 953 return generator 954 } 955 956 func getDefaultHistoryEvent( 957 eventID int64, 958 version int64, 959 ) *historypb.HistoryEvent { 960 961 globalTaskID++ 962 return &historypb.HistoryEvent{ 963 EventId: eventID, 964 EventTime: timestamppb.New(time.Now().UTC()), 965 TaskId: globalTaskID, 966 Version: version, 967 } 968 } 969 970 func copyConnections( 971 originalMap map[string][]Edge, 972 ) map[string][]Edge { 973 974 newMap := make(map[string][]Edge) 975 for key, value := range originalMap { 976 newMap[key] = copyEdges(value) 977 } 978 return newMap 979 } 980 981 func copyExitVertices( 982 originalMap map[string]bool, 983 ) map[string]bool { 984 985 newMap := make(map[string]bool) 986 for key, value := range originalMap { 987 newMap[key] = value 988 } 989 return newMap 990 } 991 992 func copyVertex(vertex []Vertex) []Vertex { 993 newVertex := make([]Vertex, len(vertex)) 994 for idx, v := range vertex { 995 newVertex[idx] = v.DeepCopy() 996 } 997 return newVertex 998 } 999 1000 func copyEdges(edges []Edge) []Edge { 1001 newEdges := make([]Edge, len(edges)) 1002 for idx, e := range edges { 1003 newEdges[idx] = e.DeepCopy() 1004 } 1005 return newEdges 1006 }