-
Notifications
You must be signed in to change notification settings - Fork 12
warn if max_claims is greater than or equal to connection pool size #109
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
Open
bKP451
wants to merge
7
commits into
Betterment:main
Choose a base branch
from
bKP451:bikash/improve-worker-observability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0101b39
wip
bKP451 8e3a182
verbose logging
bKP451 2ec85e1
revert explicit raise of thread error
bKP451 f472191
chore: revert unnecessary changes from PR (debug logs, thread_pool_si…
bKP451 bc6c96f
feat: warn at startup when max_claims >= DB connection pool size
bKP451 dea0914
fix: resolve RSpec/SubjectStub offense in connection pool config chec…
bKP451 bcc4212
document connection pool size constraint for max_claims
bKP451 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There may be a way to more proactively (e.g. during worker boot/initialization) establish if
Delayed::Worker.max_claims > Delayed::Job.connection_pool.sizerather than performing thisthread_pool_sizelogic on every pickup loop. (My understanding is thatDelayed::Job.connection_pool.sizeis informed by the pool size config indatabase.yml, and should not change once the app has loaded.)The current pickup strategy is also intended to avoid picking up more work than the worker can immediately begin working off (to avoid holding unworked jobs in memory), so it may make sense to
raiseorwarnup front (again, during boot / worker initialization) if a misconfiguration is detected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smudge,
Yeah it is a great way to
raiseonwarnupfront during app initializer ( config/initializers/delayed.rb ) forDelayed::Worker.max_claims > Delayed::Job.connection_pool.sizeOn our project, our actual problem was max_claims being equal to pool_size. On investigation we found that main worker ( i.e server ) also needs to connect to DB to keep track of threads, locking, unlocking, polling etc. Therefor a worker thread will throw
ActiveRecord::ConnectionTimeoutErrorif it cannot get DB connection after awaiting for a checkout_time ( i.eActiveRecord::Base.connection_pool.checkout_timeout). For long running jobs, DB connection won't get free for that worker thread and exception is raisedJob thread crashed with ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.001 seconds); all pooled connections were in useHere 5.000 seconds is checkout_time
Problem illustration
We solved it by
reducing concurrent worker threads to pool minus 1Override MAX_CLAIMS at config/initializers/delayed.rb
WDYT ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's a good callout -- we always include a buffer in our connection pool size (both for web and worker counts). We also sometimes need more than 1 connection per thread, but the same general rule applies there too -- basically, if your code generally needs N connections, and your max claims is M, you want N * (M + 1) connections. The most common case is
N=1though, and I think that would be easy enough to detect with>=(rather than the>I had originally proposed):