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 a busy_handler_timeout setter #443

Merged
merged 12 commits into from
Jan 3, 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
20 changes: 20 additions & 0 deletions lib/sqlite3/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def initialize file, options = {}, zvfs = nil
@type_translator = make_type_translator @type_translation
@readonly = mode & Constants::Open::READONLY != 0
@default_transaction_mode = options[:default_transaction_mode] || :deferred
@timeout_deadline = nil

if block_given?
begin
Expand Down Expand Up @@ -691,6 +692,25 @@ def readonly?
@readonly
end

# Sets a #busy_handler that releases the GVL between retries,
# but only retries up to the indicated number of +milliseconds+.
# This is an alternative to #busy_timeout, which holds the GVL
# while SQLite sleeps and retries.
def busy_handler_timeout=( milliseconds )
timeout_seconds = milliseconds.fdiv(1000)

busy_handler do |count|
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
if count.zero?
@timeout_deadline = now + timeout_seconds
elsif now > @timeout_deadline
next false
else
sleep(0.001)
Copy link
Member

Choose a reason for hiding this comment

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

Is this sleep necessary? I think the VM is interruptible on any method call so the calls to clock_gettime etc should do the trick (I think).

Copy link
Contributor

Choose a reason for hiding this comment

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

@tenderlove it's not about being interruptible, it's about forcibly releasing the GVL. The assumption is that the client that is currently holding the SQLite3 write lock might be another thread in the same process, hence we should try to switch back to it rather than busy loop for 100ms until the thread scheduler quantum is reached.

And even if it's not in that process, yielding the GVL allow other unrelated threads to proceed.

NB: for the "in same process case" a shared Ruby mutex would be way more efficient, but we'd need some way to tell two clients are pointing that the same database, hence should share a single Mutex.

Copy link
Member

Choose a reason for hiding this comment

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

The assumption is that the client that is currently holding the SQLite3 write lock might be another thread in the same process, hence we should try to switch back to it

I see. In that case wouldn't Thread.pass also do the trick?

Copy link
Contributor

Choose a reason for hiding this comment

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

It would yes, but then the risk is that there's no other ready thread, causing the process to essentially be busy looping, which would pin one CPU to 100% and may not be desirable. A very short sleep actually make sense IMO.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me!

end
end
end

# A helper class for dealing with custom functions (see #create_function,
# #create_aggregate, and #create_aggregate_handler). It encapsulates the
# opaque function object that represents the current invocation. It also
Expand Down
44 changes: 44 additions & 0 deletions test/test_integration_pending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,48 @@ def test_busy_timeout

assert time.real*1000 >= 1000
end

def test_busy_timeout_blocks_gvl
threads = [1, 2].map do
Thread.new do
begin
db = SQLite3::Database.new("test.db")
db.busy_timeout = 3000
db.transaction(:immediate) do
db.execute "insert into foo ( b ) values ( ? )", rand(1000).to_s
sleep 1
db.execute "insert into foo ( b ) values ( ? )", rand(1000).to_s
end
ensure
db.close if db
end
end
end

assert_raise( SQLite3::BusyException ) do
threads.each(&:join)
end
end

def test_busy_handler_timeout_releases_gvl
threads = [1, 2].map do
Thread.new do
begin
db = SQLite3::Database.new("test.db")
db.busy_handler_timeout = 3000
db.transaction(:immediate) do
db.execute "insert into foo ( b ) values ( ? )", rand(1000).to_s
sleep 1
db.execute "insert into foo ( b ) values ( ? )", rand(1000).to_s
end
ensure
db.close if db
end
end
end

assert_nothing_raised do
threads.each(&:join)
end
end
end