Skip to content

Commit

Permalink
fix ruby lock logic
Browse files Browse the repository at this point in the history
  • Loading branch information
terrywbrady committed Aug 23, 2024
1 parent 01801b7 commit ff89c9d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
20 changes: 14 additions & 6 deletions src/main/ruby/lib/merritt_zk_locks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,38 @@ def self.unlock_collection(zk, mnemonic)
# no action
end

def self.store_lock_path(ark)
"#{LOCKS_STORAGE}/#{ark.gsub(/:?\//, '_')}"
end

def self.lock_object_storage(zk, ark)
create_ephemeral_lock(zk, "#{LOCKS_STORAGE}/#{ark.gsub(':?/', '_')}")
create_ephemeral_lock(zk, store_lock_path(ark))
end

def self.check_lock_object_storage(zk, ark)
zk.exists?("#{LOCKS_STORAGE}/#{ark.gsub(':?/', '_')}")
zk.exists?(store_lock_path(ark))
end

def self.unlock_object_storage(zk, ark)
zk.delete("#{LOCKS_STORAGE}/#{ark.gsub(':?/', '_')}")
zk.delete(store_lock_path(ark))
rescue StandardError
# no action
end

def self.inv_lock_path(ark)
"#{LOCKS_INVENTORY}/#{ark.gsub(/:?\//, '_')}"
end

def self.lock_object_inventory(zk, ark)
create_ephemeral_lock(zk, "#{LOCKS_INVENTORY}/#{ark.gsub(':?/', '_')}")
create_ephemeral_lock(zk, inv_lock_path(ark))
end

def self.check_lock_object_inventory(zk, ark)
zk.exists?("#{LOCKS_INVENTORY}/#{ark.gsub(':?/', '_')}")
zk.exists?(inv_lock_path(ark))
end

def self.unlock_object_inventory(zk, ark)
zk.delete("#{LOCKS_INVENTORY}/#{ark.gsub(':?/', '_')}")
zk.delete(inv_lock_path(ark))
rescue StandardError
# no action
end
Expand Down
35 changes: 21 additions & 14 deletions src/main/ruby/spec/zk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,8 @@ def make_batch_json(s = 'bar', u = 'bid-uuid')

it :lock_collection do |_x|
expect(MerrittZK::Locks.check_lock_collection(@zk, 'foo')).to be(false)
expect(MerrittZK::Locks.lock_collection(@zk, 'foo')).to be(true)
MerrittZK::Locks.lock_collection(@zk, 'foo')
expect(MerrittZK::Locks.check_lock_collection(@zk, 'foo')).to be(true)
expect(MerrittZK::Locks.lock_collection(@zk, 'foo')).to be(false)
MerrittZK::Locks.unlock_collection(@zk, 'foo')
expect(MerrittZK::Locks.lock_collection(@zk, 'foo')).to be(true)

Expand All @@ -860,30 +859,38 @@ def make_batch_json(s = 'bar', u = 'bid-uuid')

it :lock_store do |_x|
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(false)
expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')).to be(true)
MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(true)
expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')).to be(false)
MerrittZK::Locks.unlock_object_storage(@zk, 'ark:/aaa/111')
expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')).to be(true)
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(false)
MerrittZK::Locks.lock_object_storage(@zk, 'ark:/aaa/111')
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/aaa/111')).to be(true)

expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')).to be(true)
expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')).to be(false)
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(false)
MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(true)
MerrittZK::Locks.unlock_object_storage(@zk, 'ark:/bbb/222')
expect(MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')).to be(true)
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(false)
MerrittZK::Locks.lock_object_storage(@zk, 'ark:/bbb/222')
expect(MerrittZK::Locks.check_lock_object_storage(@zk, 'ark:/bbb/222')).to be(true)
end

it :lock_inventory do |_x|
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(false)
expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true)
MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true)
expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')).to be(false)
MerrittZK::Locks.unlock_object_inventory(@zk, 'ark:/aaa/111')
expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true)
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(false)
MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/aaa/111')
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/aaa/111')).to be(true)

expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true)
expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')).to be(false)
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(false)
MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true)
MerrittZK::Locks.unlock_object_inventory(@zk, 'ark:/bbb/222')
expect(MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true)
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(false)
MerrittZK::Locks.lock_object_inventory(@zk, 'ark:/bbb/222')
expect(MerrittZK::Locks.check_lock_object_inventory(@zk, 'ark:/bbb/222')).to be(true)
end
end

Expand Down

0 comments on commit ff89c9d

Please sign in to comment.