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
feat(db) implement the db:cluster_mutex() utility #3685
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
local helpers = require "spec.helpers" | ||
|
||
|
||
for _, strategy in helpers.each_strategy("cassandra") do | ||
describe("kong.db [#" .. strategy .. "]", function() | ||
local db | ||
|
||
|
||
setup(function() | ||
local _ | ||
_, db, _ = helpers.get_db_utils(strategy) | ||
end) | ||
|
||
|
||
describe("db:cluster_mutex()", function() | ||
it("returns 'true' when mutex ran and 'false' otherwise", function() | ||
local t1 = ngx.thread.spawn(function() | ||
local ok, err = db:cluster_mutex("my_key", nil, function() | ||
ngx.sleep(0.1) | ||
end) | ||
assert.is_nil(err) | ||
assert.equal(true, ok) | ||
end) | ||
|
||
local t2 = ngx.thread.spawn(function() | ||
local ok, err = db:cluster_mutex("my_key", nil, function() end) | ||
assert.is_nil(err) | ||
assert.equal(false, ok) | ||
end) | ||
|
||
ngx.thread.wait(t1) | ||
ngx.thread.wait(t2) | ||
end) | ||
|
||
|
||
it("mutex ensures only one callback gets called", function() | ||
local cb1 = spy.new(function() end) | ||
local cb2 = spy.new(function() ngx.sleep(0.3) end) | ||
|
||
local t1 = ngx.thread.spawn(function() | ||
ngx.sleep(0.2) | ||
|
||
local _, err = db:cluster_mutex("my_key_2", { owner = "1" }, cb1) | ||
assert.is_nil(err) | ||
end) | ||
|
||
local t2 = ngx.thread.spawn(function() | ||
local _, err = db:cluster_mutex("my_key_2", { owner = "2" }, cb2) | ||
assert.is_nil(err) | ||
end) | ||
|
||
ngx.thread.wait(t1) | ||
ngx.thread.wait(t2) | ||
|
||
assert.spy(cb2).was_called() | ||
assert.spy(cb1).was_not_called() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since cb2 takes 0.2 to run and t1 waits 0.2 until it acquires, isn't there a chance we get unlucky in this test and t2 releases the lock before t1 tries to acquire? (in other words, shouldn't the sleep in cb2 be a little longer than t1 to be on the safe side, or am I misunderstanding the test?) |
||
end) | ||
|
||
|
||
it("mutex can be subsequently acquired once released", function() | ||
local cb1 = spy.new(function() end) | ||
local cb2 = spy.new(function() end) | ||
|
||
local t1 = ngx.thread.spawn(function() | ||
local _, err = db:cluster_mutex("my_key_3", nil, cb1) | ||
assert.is_nil(err) | ||
end) | ||
|
||
local t2 = ngx.thread.spawn(function() | ||
local _, err = db:cluster_mutex("my_key_3", nil, cb2) | ||
assert.is_nil(err) | ||
end) | ||
|
||
ngx.thread.wait(t1) | ||
ngx.thread.wait(t2) | ||
|
||
assert.spy(cb1).was_called() | ||
assert.spy(cb2).was_called() | ||
end) | ||
|
||
|
||
it("mutex cannot be held for longer than opts.ttl across nodes (DB lock)", function() | ||
local cb1 = spy.new(function() | ||
-- remove worker lock | ||
ngx.shared.kong_locks:delete("my_key_5") | ||
-- make DB lock expire | ||
ngx.sleep(1) | ||
end) | ||
|
||
local cb2 = spy.new(function() end) | ||
|
||
local t1 = ngx.thread.spawn(function() | ||
local ok, err = db:cluster_mutex("my_key_5", { ttl = 0.5 }, cb1) | ||
assert.is_nil(err) | ||
assert.equal(true, ok) | ||
end) | ||
|
||
local t2 = ngx.thread.spawn(function() | ||
local ok, err = db:cluster_mutex("my_key_5", { ttl = 0.5 }, cb2) | ||
assert.is_nil(ok) | ||
assert.equal("timeout", err) | ||
end) | ||
|
||
ngx.thread.wait(t1) | ||
ngx.thread.wait(t2) | ||
|
||
assert.spy(cb1).was_called() | ||
assert.spy(cb2).was_not_called() | ||
end) | ||
end) | ||
end) | ||
end |
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.
Shouldn't the above be in a migration? Not as much for this instance as well as creating a precedent.
Unless the intent is to use this lock, during migrations?
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 indeed