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: prevent db error if no async calls to execute #2081

Merged
merged 3 commits into from
Jul 16, 2024

Conversation

matt2e
Copy link
Collaborator

@matt2e matt2e commented Jul 16, 2024

When a controller tries to get a lease on the next async call to execute, the lease creation code should first check to see if there was a call found.
Before when a controller tried to get a hold on the next async call to execute, it would result in this error:

2024-07-15 16:00:08 UTC:10.2.111.21(46692):adminuser@tbd:[60308]:ERROR:  null value in column "key" of relation "leases" violates not-null constraint
2024-07-15 16:00:08 UTC:10.2.111.21(46692):adminuser@tbd:[60308]:DETAIL:  Failing row contains (1632753, 37a8d114-73ca-46a1-b03b-a17331d17181, null, 2024-07-15 16:00:08.260772+00, 2024-07-15 16:00:13.260772+00, null).
2024-07-15 16:00:08 UTC:10.2.111.21(46692):adminuser@tbd:[60308]:STATEMENT:  -- name: AcquireAsyncCall :one
	WITH async_call AS (
	  SELECT id
	  FROM async_calls
	  WHERE state = 'pending' AND scheduled_at <= (NOW() AT TIME ZONE 'utc')
	  LIMIT 1
	  FOR UPDATE SKIP LOCKED
	), lease AS (
	  INSERT INTO leases (idempotency_key, key, expires_at)
	  VALUES (gen_random_uuid(), '/system/async_call/' || (SELECT id FROM async_call), (NOW() AT TIME ZONE 'utc') + $1::interval)
	  RETURNING id, idempotency_key, key, created_at, expires_at, metadata
	)
	UPDATE async_calls
	SET state = 'executing', lease_id = (SELECT id FROM lease)
	WHERE id = (SELECT id FROM async_call)
	RETURNING
	  id AS async_call_id,
	  (SELECT idempotency_key FROM lease) AS lease_idempotency_key,
	  (SELECT key FROM lease) AS lease_key,
	  origin,
	  verb,
	  request,
	  scheduled_at,
	  remaining_attempts,
	  backoff,
	  max_backoff

@matt2e matt2e requested review from alecthomas and a team as code owners July 16, 2024 03:33
@matt2e matt2e requested review from worstell and removed request for a team July 16, 2024 03:33
@ftl-robot ftl-robot mentioned this pull request Jul 16, 2024
@matt2e matt2e force-pushed the matt2e/no-async-call-cleanup branch from 0cd11c6 to f0829b8 Compare July 16, 2024 03:39
@matt2e matt2e requested a review from gak July 16, 2024 03:49
Copy link
Contributor

@gak gak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, maybe a unit test to show it working if it's trivial enough.

@matt2e matt2e force-pushed the matt2e/no-async-call-cleanup branch from 44ebf14 to efbe116 Compare July 16, 2024 05:58
@matt2e matt2e merged commit 4278a09 into main Jul 16, 2024
55 checks passed
@matt2e matt2e deleted the matt2e/no-async-call-cleanup branch July 16, 2024 06:10
@worstell worstell added the approved Marks an already closed PR as approved label Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Marks an already closed PR as approved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants