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

Fix Backend.GetWorkflowTask lock bug in PSQL backend #18

Merged
merged 5 commits into from
Nov 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: ci

on: [ push ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.22.x' ]
steps:
- uses: actions/checkout@v4
- name: Setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Build & Test
run: ./build.sh
9 changes: 6 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/bin/sh
#!/bin/bash

set -o pipefail
set -eo pipefail

go clean
go clean -testcache
go build ./...
go test ./...
go test -race ./...
go test -tags e2e -p 1 ./test/e2e/...

if [[ $ENABLE_E2E_TEST ]]; then
go test -tags e2e -p 1 ./test/e2e/...
fi
6 changes: 3 additions & 3 deletions pkg/backend/psql/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (b *be) GetWorkflowTask(ctx context.Context) (result *task.WorkflowTask, er
return nil, HandleSQLError(err)
}
currentTimestampUTC := b.getCurrentTimestampLocal()
t, err := b.taskRepo.GetAndLockAvailableTask(uowCtx, task.TaskTypeWorkflow, b.lockedBy, b.lockExpirationDuration)
t, previouslyLockedBy, err := b.taskRepo.GetAndLockAvailableTask(uowCtx, task.TaskTypeWorkflow, b.lockedBy, b.lockExpirationDuration)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, worker.ErrNoTask
Expand All @@ -245,7 +245,7 @@ func (b *be) GetWorkflowTask(ctx context.Context) (result *task.WorkflowTask, er
if err != nil {
return nil, err
}
pEvents, err := b.eventRepo.GetAvailableWorkflowEventsAndLock(uowCtx, t.WorkflowID, b.lockedBy)
pEvents, err := b.eventRepo.GetAvailableWorkflowEventsAndLock(uowCtx, t.WorkflowID, b.lockedBy, previouslyLockedBy)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -481,7 +481,7 @@ func (b *be) GetActivityTask(ctx context.Context) (result *task.ActivityTask, er
if err != nil {
return nil, HandleSQLError(err)
}
t, err := b.taskRepo.GetAndLockAvailableTask(uowCtx, task.TaskTypeActivity, b.lockedBy, b.lockExpirationDuration)
t, _, err := b.taskRepo.GetAndLockAvailableTask(uowCtx, task.TaskTypeActivity, b.lockedBy, b.lockExpirationDuration)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, worker.ErrNoTask
Expand Down
6 changes: 3 additions & 3 deletions pkg/backend/psql/persistent/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type EventRepository interface {
DeleteEventsByWorkflowID(ctx context.Context, workflowID string) (int64, error)
DeleteEventsByWorkflowIDAndHeldBy(ctx context.Context, workflowID string, heldBy string) (int64, error)
ReleaseEventsByWorkflowIDAndHeldBy(ctx context.Context, workflowID string, heldBy string) (int64, error)
GetAvailableWorkflowEventsAndLock(ctx context.Context, workflowID string, heldBy string) ([]*Event, error)
GetAvailableWorkflowEventsAndLock(ctx context.Context, workflowID string, heldBy string, previouslyHeldBy *string) ([]*Event, error)
}

type eventRepository struct {
Expand Down Expand Up @@ -61,13 +61,13 @@ func (r *eventRepository) ReleaseEventsByWorkflowIDAndHeldBy(ctx context.Context
return result.RowsAffected, result.Error
}

func (r *eventRepository) GetAvailableWorkflowEventsAndLock(ctx context.Context, workflowID string, heldBy string) ([]*Event, error) {
func (r *eventRepository) GetAvailableWorkflowEventsAndLock(ctx context.Context, workflowID string, heldBy string, previouslyHeldBy *string) ([]*Event, error) {
uow := r.UnitOfWork(ctx)
now := time.Now().UnixMilli()
result := uow.Tx.Model(&Event{}).Where(
"workflow_id = ? AND (held_by IS NULL OR held_by = ?) AND visible_at < ?",
workflowID,
heldBy,
previouslyHeldBy,
now,
).Clauses().Updates(map[string]interface{}{
"held_by": heldBy,
Expand Down
11 changes: 6 additions & 5 deletions pkg/backend/psql/persistent/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type TaskRepository interface {
ReleaseTask(ctx context.Context, workflowID string, taskID string, taskType task.TaskType, lockedBy string, reason *string, nextScheduleTimestamp *int64) error
DeleteTask(ctx context.Context, workflowID string, taskID string, taskType task.TaskType, lockedBy string) error
DeleteTaskUnsafe(ctx context.Context, workflowID string, taskID string, taskType task.TaskType) error
GetAndLockAvailableTask(ctx context.Context, taskType task.TaskType, lockedBy string, lockExpirationDuration time.Duration) (*Task, error)
GetAndLockAvailableTask(ctx context.Context, taskType task.TaskType, lockedBy string, lockExpirationDuration time.Duration) (*Task, *string, error)
ResetTaskLastTouchTimestamp(ctx context.Context, workflowID string, taskID string) error
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func (r *taskRepository) DeleteTaskUnsafe(ctx context.Context, workflowID string
return nil
}

func (r *taskRepository) GetAndLockAvailableTask(ctx context.Context, taskType task.TaskType, lockedBy string, lockExpirationDuration time.Duration) (*Task, error) {
func (r *taskRepository) GetAndLockAvailableTask(ctx context.Context, taskType task.TaskType, lockedBy string, lockExpirationDuration time.Duration) (*Task, *string, error) {
uow := r.UnitOfWork(ctx)
now := time.Now().UnixMilli()
t := &Task{}
Expand All @@ -136,8 +136,9 @@ func (r *taskRepository) GetAndLockAvailableTask(ctx context.Context, taskType t
).
Order("last_touch ASC").First(&t)
if result.Error != nil {
return nil, result.Error
return nil, nil, result.Error
}
previousLockedBy := t.LockedBy
result = uow.Tx.
Model(&Task{}).
Where("workflow_id = ? AND task_id = ?", t.WorkflowID, t.TaskID).
Expand All @@ -147,9 +148,9 @@ func (r *taskRepository) GetAndLockAvailableTask(ctx context.Context, taskType t
"last_touch": now,
}).First(&t)
if result.Error != nil {
return nil, result.Error
return nil, nil, result.Error
}
return t, nil
return t, previousLockedBy, nil
}

func (r *taskRepository) ResetTaskLastTouchTimestamp(ctx context.Context, workflowID string, taskID string) error {
Expand Down