Skip to content

Commit

Permalink
deadlock mitigation redux (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson authored Dec 11, 2024
1 parent 7d76fa7 commit f7e0736
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 65 deletions.
40 changes: 23 additions & 17 deletions engine/storage/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ WHERE
c.step_id IS NULL AND
s.workflow_name = ?;

-- name: DeleteWorkflowStepHavingNoCommandsByStepID :exec
DELETE
s
FROM
steps s
LEFT JOIN id_commands c
ON s.id = c.step_id
WHERE
c.step_id IS NULL AND
s.id = ?;

-- name: UpdateIDCommandTimestamp :exec
UPDATE
id_commands
Expand Down Expand Up @@ -116,26 +127,21 @@ FROM
WHERE
id = ?;

-- name: GetIDCommandsByStepID :many
-- name: GetIDCommandsByStepIDAndLock :many
SELECT
command_uuid,
request_type,
result
ic.command_uuid,
ic.request_type,
ic.result
FROM
id_commands
WHERE
enrollment_id = ? AND
step_id = ? AND
completed != 0;

-- name: LockIDCommandsByStepID :exec
SELECT
command_uuid
FROM
id_commands
id_commands ic
INNER JOIN steps s
ON ic.step_id = s.id
LEFT JOIN step_commands sc
ON sc.step_id = s.id
WHERE
enrollment_id = ? AND
step_id = ?
ic.enrollment_id = ? AND
s.id = ? AND
ic.completed != 0
FOR UPDATE;

-- name: RemoveIDCommandsByStepID :exec
Expand Down
73 changes: 37 additions & 36 deletions engine/storage/mysql/sqlc/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions engine/storage/mysql/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,9 @@ func (s *MySQLStorage) StoreCommandResponseAndRetrieveCompletedStep(ctx context.
Commands: []storage.StepCommandResult{*sc},
}

err = qtx.LockIDCommandsByStepID(ctx, sqlc.LockIDCommandsByStepIDParams{
cmdR, err := qtx.GetIDCommandsByStepIDAndLock(ctx, sqlc.GetIDCommandsByStepIDAndLockParams{
EnrollmentID: id,
StepID: cmdCt.StepID,
})
if err != nil {
return fmt.Errorf("lock commands by step by id (%d): %w", cmdCt.StepID, err)
}

cmdR, err := qtx.GetIDCommandsByStepID(ctx, sqlc.GetIDCommandsByStepIDParams{
EnrollmentID: id,
StepID: cmdCt.StepID,
ID: cmdCt.StepID,
})
if err != nil {
return fmt.Errorf("get id commands by step by id (%d): %w", cmdCt.StepID, err)
Expand All @@ -130,9 +122,9 @@ func (s *MySQLStorage) StoreCommandResponseAndRetrieveCompletedStep(ctx context.
return fmt.Errorf("remove id commands by step by id (%d): %w", cmdCt.StepID, err)
}

err = qtx.DeleteWorkflowStepHavingNoCommandsByWorkflowName(ctx, sd.WorkflowName)
err = qtx.DeleteWorkflowStepHavingNoCommandsByStepID(ctx, cmdCt.StepID)
if err != nil {
return fmt.Errorf("delete workflow with no commands (%s): %w", sd.WorkflowName, err)
return fmt.Errorf("delete workflow with no commands (%d): %w", cmdCt.StepID, err)
}

return nil
Expand Down

0 comments on commit f7e0736

Please sign in to comment.