Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional service protection against reordering #1490

Merged
merged 2 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion service/history/mutablestate/history_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,10 @@ func (b *HistoryBuilder) FlushBufferToCurrentBatch() map[int64]int64 {
b.dbBufferBatch = nil
b.memBufferBatch = nil

// 0th reorder events in case casandra reorder the buffered events
// TODO eventually remove this ordering
bufferBatch = b.reorderBuffer(bufferBatch)

// 1st assign event ID
for _, event := range bufferBatch {
event.EventId = b.nextEventID
Expand Down Expand Up @@ -1240,7 +1244,6 @@ func (b *HistoryBuilder) finishEvent(
func (b *HistoryBuilder) wireEventIDs(
bufferEvents []*historypb.HistoryEvent,
) {

for _, event := range bufferEvents {
switch event.GetEventType() {
case enumspb.EVENT_TYPE_ACTIVITY_TASK_STARTED:
Expand Down Expand Up @@ -1306,6 +1309,31 @@ func (b *HistoryBuilder) wireEventIDs(
// * HasActivityFinishEvent
// * hasActivityFinishEvent

func (b *HistoryBuilder) reorderBuffer(
bufferEvents []*historypb.HistoryEvent,
) []*historypb.HistoryEvent {
reorderBuffer := make([]*historypb.HistoryEvent, 0, len(bufferEvents))
reorderEvents := make([]*historypb.HistoryEvent, 0, len(bufferEvents))
for _, event := range bufferEvents {
switch event.GetEventType() {
case enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED,
enumspb.EVENT_TYPE_ACTIVITY_TASK_FAILED,
enumspb.EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT,
enumspb.EVENT_TYPE_ACTIVITY_TASK_CANCELED,
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_COMPLETED,
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_FAILED,
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TIMED_OUT,
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_CANCELED,
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TERMINATED:
reorderBuffer = append(reorderBuffer, event)
default:
reorderEvents = append(reorderEvents, event)
}
}

return append(reorderEvents, reorderBuffer...)
}

func (b *HistoryBuilder) HasActivityFinishEvent(
scheduleID int64,
) bool {
Expand Down
37 changes: 37 additions & 0 deletions service/history/mutablestate/history_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,43 @@ OtherEventsLoop:
)
}

func (s *historyBuilderSuite) TestReorder() {
reorderEventTypes := map[enumspb.EventType]struct{}{
enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED: {},
enumspb.EVENT_TYPE_ACTIVITY_TASK_FAILED: {},
enumspb.EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT: {},
enumspb.EVENT_TYPE_ACTIVITY_TASK_CANCELED: {},
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_COMPLETED: {},
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_FAILED: {},
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TIMED_OUT: {},
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_CANCELED: {},
enumspb.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TERMINATED: {},
}
var reorderEvents []*historypb.HistoryEvent
for eventType := range reorderEventTypes {
reorderEvents = append(reorderEvents, &historypb.HistoryEvent{
EventType: eventType,
})
}

var nonReorderEvents []*historypb.HistoryEvent
for eventTypeValue := range enumspb.EventType_name {
eventType := enumspb.EventType(eventTypeValue)
if _, ok := reorderEventTypes[eventType]; ok || eventType == enumspb.EVENT_TYPE_UNSPECIFIED {
continue
}

nonReorderEvents = append(nonReorderEvents, &historypb.HistoryEvent{
EventType: eventType,
})
}

s.Equal(
append(nonReorderEvents, reorderEvents...),
s.historyBuilder.reorderBuffer(append(reorderEvents, nonReorderEvents...)),
)
}

func (s *historyBuilderSuite) assertEventIDTaskID(
historyMutation *HistoryMutation,
) {
Expand Down