From d618f3870bb6df74a78b1e55176ee83934dbd92f Mon Sep 17 00:00:00 2001 From: Terry Brady Date: Fri, 21 Jun 2024 10:27:58 -0700 Subject: [PATCH] check lock methods --- .../java/org/cdlib/mrt/zk/MerrittLocks.java | 19 +++++++++++++++ src/main/ruby/lib/merritt_zk_locks.rb | 24 +++++++++++++++++++ src/main/ruby/spec/zk_spec.rb | 10 ++++++++ src/test/java/org/cdlib/mrt/zk/ZKTestIT.java | 8 +++++++ 4 files changed, 61 insertions(+) diff --git a/src/main/java/org/cdlib/mrt/zk/MerrittLocks.java b/src/main/java/org/cdlib/mrt/zk/MerrittLocks.java index 4235b07..7cdb331 100644 --- a/src/main/java/org/cdlib/mrt/zk/MerrittLocks.java +++ b/src/main/java/org/cdlib/mrt/zk/MerrittLocks.java @@ -44,6 +44,9 @@ public static boolean lockIngestQueue(ZooKeeper client) { public static void unlockIngestQueue(ZooKeeper client) throws InterruptedException, KeeperException { QueueItemHelper.delete(client, QueueItem.ZkPaths.LocksQueueIngest.path); } + public static boolean checkLockIngestQueue(ZooKeeper client) throws KeeperException, InterruptedException { + return QueueItemHelper.exists(client, QueueItem.ZkPaths.LocksQueueIngest.path); + } public static boolean lockLargeAccessQueue(ZooKeeper client) { return createLock(client, QueueItem.ZkPaths.LocksQueueAccessLarge.path); @@ -51,6 +54,9 @@ public static boolean lockLargeAccessQueue(ZooKeeper client) { public static void unlockLargeAccessQueue(ZooKeeper client) throws InterruptedException, KeeperException { QueueItemHelper.delete(client, QueueItem.ZkPaths.LocksQueueAccessLarge.path); } + public static boolean checkLockLargeAccessQueue(ZooKeeper client) throws KeeperException, InterruptedException { + return QueueItemHelper.exists(client, QueueItem.ZkPaths.LocksQueueAccessLarge.path); + } public static boolean lockSmallAccessQueue(ZooKeeper client) { return createLock(client, QueueItem.ZkPaths.LocksQueueAccessSmall.path); @@ -58,6 +64,9 @@ public static boolean lockSmallAccessQueue(ZooKeeper client) { public static void unlockSmallAccessQueue(ZooKeeper client) throws InterruptedException, KeeperException { QueueItemHelper.delete(client, QueueItem.ZkPaths.LocksQueueAccessSmall.path); } + public static boolean checkLockSmallAccessQueue(ZooKeeper client) throws KeeperException, InterruptedException { + return QueueItemHelper.exists(client, QueueItem.ZkPaths.LocksQueueAccessSmall.path); + } public static boolean lockCollection(ZooKeeper client, String mnemonic) { return createLock(client, Paths.get(QueueItem.ZkPaths.LocksCollections.path, mnemonic).toString()); @@ -66,6 +75,9 @@ public static boolean lockCollection(ZooKeeper client, String mnemonic) { public static void unlockCollection(ZooKeeper client, String mnemonic) throws InterruptedException, KeeperException { QueueItemHelper.delete(client, Paths.get(QueueItem.ZkPaths.LocksCollections.path, mnemonic).toString()); } + public static boolean checkLockCollection(ZooKeeper client, String mnemonic) throws KeeperException, InterruptedException { + return QueueItemHelper.exists(client, Paths.get(QueueItem.ZkPaths.LocksCollections.path, mnemonic).toString()); + } public static boolean lockObjectStorage(ZooKeeper client, String ark) { return createEphemeralLock(client, Paths.get(QueueItem.ZkPaths.LocksStorage.path, ark.replaceAll("\\/", "_")).toString()); @@ -73,10 +85,17 @@ public static boolean lockObjectStorage(ZooKeeper client, String ark) { public static void unlockObjectStorage(ZooKeeper client, String ark) throws InterruptedException, KeeperException { QueueItemHelper.delete(client, Paths.get(QueueItem.ZkPaths.LocksStorage.path, ark.replaceAll("\\/", "_")).toString()); } + public static boolean checkLockObjectStorage(ZooKeeper client, String ark) throws KeeperException, InterruptedException { + return QueueItemHelper.exists(client, Paths.get(QueueItem.ZkPaths.LocksStorage.path, ark.replaceAll("\\/", "_")).toString()); + } + public static boolean lockObjectInventory(ZooKeeper client, String ark) { return createEphemeralLock(client, Paths.get(QueueItem.ZkPaths.LocksInventory.path, ark.replaceAll("\\/", "_")).toString()); } public static void unlockObjectInventory(ZooKeeper client, String ark) throws InterruptedException, KeeperException { QueueItemHelper.delete(client, Paths.get(QueueItem.ZkPaths.LocksInventory.path, ark.replaceAll("\\/", "_")).toString()); } + public static boolean checkLockObjectInventory(ZooKeeper client, String ark) throws KeeperException, InterruptedException { + return QueueItemHelper.exists(client, Paths.get(QueueItem.ZkPaths.LocksInventory.path, ark.replaceAll("\\/", "_")).toString()); + } } diff --git a/src/main/ruby/lib/merritt_zk_locks.rb b/src/main/ruby/lib/merritt_zk_locks.rb index 8b2f571..c749402 100644 --- a/src/main/ruby/lib/merritt_zk_locks.rb +++ b/src/main/ruby/lib/merritt_zk_locks.rb @@ -47,6 +47,10 @@ def self.lock_ingest_queue(zk) create_lock(zk, LOCKS_QUEUE_INGEST) end + def self.check_lock_ingest_queue(zk) + zk.exists?(LOCKS_QUEUE_INGEST) + end + def self.unlock_ingest_queue(zk) zk.delete(LOCKS_QUEUE_INGEST) rescue StandardError @@ -57,6 +61,10 @@ def self.lock_large_access_queue(zk) create_lock(zk, LOCKS_QUEUE_ACCESS_LARGE) end + def self.check_lock_large_access_queue(zk) + zk.exists?(LOCKS_QUEUE_ACCESS_LARGE) + end + def self.unlock_large_access_queue(zk) zk.delete(LOCKS_QUEUE_ACCESS_LARGE) rescue StandardError @@ -67,6 +75,10 @@ def self.lock_small_access_queue(zk) create_lock(zk, LOCKS_QUEUE_ACCESS_SMALL) end + def self.check_lock_small_access_queue(zk) + zk.exists?(LOCKS_QUEUE_ACCESS_SMALL) + end + def self.unlock_small_access_queue(zk) zk.delete(LOCKS_QUEUE_ACCESS_SMALL) rescue StandardError @@ -77,6 +89,10 @@ def self.lock_collection(zk, mnemonic) create_lock(zk, "#{LOCKS_COLLECTION}/#{mnemonic}") end + def self.check_lock_collection(zk, mnemonic) + zk.exists?("#{LOCKS_COLLECTION}/#{mnemonic}") + end + def self.unlock_collection(zk, mnemonic) zk.delete("#{LOCKS_COLLECTION}/#{mnemonic}") rescue StandardError @@ -87,6 +103,10 @@ def self.lock_object_storage(zk, ark) create_ephemeral_lock(zk, "#{LOCKS_STORAGE}/#{ark.gsub('/', '_')}") end + def self.check_lock_object_storage(zk, ark) + zk.exists?("#{LOCKS_STORAGE}/#{ark.gsub('/', '_')}") + end + def self.unlock_object_storage(zk, ark) zk.delete("#{LOCKS_STORAGE}/#{ark.gsub('/', '_')}") rescue StandardError @@ -97,6 +117,10 @@ def self.lock_object_inventory(zk, ark) create_ephemeral_lock(zk, "#{LOCKS_INVENTORY}/#{ark.gsub('/', '_')}") end + def self.check_lock_object_inventory(zk, ark) + zk.exists?("#{LOCKS_INVENTORY}/#{ark.gsub('/', '_')}") + end + def self.unlock_object_inventory(zk, ark) zk.delete("#{LOCKS_INVENTORY}/#{ark.gsub('/', '_')}") rescue StandardError diff --git a/src/main/ruby/spec/zk_spec.rb b/src/main/ruby/spec/zk_spec.rb index 6a05c8f..8198087 100644 --- a/src/main/ruby/spec/zk_spec.rb +++ b/src/main/ruby/spec/zk_spec.rb @@ -822,14 +822,18 @@ def make_batch_json(s = 'bar', u = 'bid-uuid') it :lock_ingest do |_x| MerrittZK::Locks.unlock_ingest_queue(@zk) + expect(MerrittZK::Locks.check_lock_ingest_queue(@zk)).to be(false) expect(MerrittZK::Locks.lock_ingest_queue(@zk)).to be(true) + expect(MerrittZK::Locks.check_lock_ingest_queue(@zk)).to be(true) expect(MerrittZK::Locks.lock_ingest_queue(@zk)).to be(false) MerrittZK::Locks.unlock_ingest_queue(@zk) expect(MerrittZK::Locks.lock_ingest_queue(@zk)).to be(true) end it :lock_access do |_x| + expect(MerrittZK::Locks.check_lock_large_access_queue(@zk)).to be(false) expect(MerrittZK::Locks.lock_large_access_queue(@zk)).to be(true) + expect(MerrittZK::Locks.check_lock_large_access_queue(@zk)).to be(true) expect(MerrittZK::Locks.lock_large_access_queue(@zk)).to be(false) MerrittZK::Locks.unlock_large_access_queue(@zk) expect(MerrittZK::Locks.lock_large_access_queue(@zk)).to be(true) @@ -841,7 +845,9 @@ def make_batch_json(s = 'bar', u = 'bid-uuid') end 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) + 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) @@ -853,7 +859,9 @@ def make_batch_json(s = 'bar', u = 'bid-uuid') end 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) + 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) @@ -865,7 +873,9 @@ def make_batch_json(s = 'bar', u = 'bid-uuid') 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) + 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) diff --git a/src/test/java/org/cdlib/mrt/zk/ZKTestIT.java b/src/test/java/org/cdlib/mrt/zk/ZKTestIT.java index d01b396..7361c7e 100644 --- a/src/test/java/org/cdlib/mrt/zk/ZKTestIT.java +++ b/src/test/java/org/cdlib/mrt/zk/ZKTestIT.java @@ -1230,7 +1230,9 @@ public void lockIngestQueue() throws KeeperException, InterruptedException, Merr //ignore if lock does not exist MerrittLocks.unlockIngestQueue(zk); + assertFalse(MerrittLocks.checkLockIngestQueue(zk)); assertTrue(MerrittLocks.lockIngestQueue(zk)); + assertTrue(MerrittLocks.checkLockIngestQueue(zk)); assertFalse(MerrittLocks.lockIngestQueue(zk)); MerrittLocks.unlockIngestQueue(zk); assertTrue(MerrittLocks.lockIngestQueue(zk)); @@ -1239,7 +1241,9 @@ public void lockIngestQueue() throws KeeperException, InterruptedException, Merr @Test public void lockAccessQueue() throws KeeperException, InterruptedException, MerrittZKNodeInvalid{ load(Tests.lock_access); + assertFalse(MerrittLocks.checkLockLargeAccessQueue(zk)); assertTrue(MerrittLocks.lockLargeAccessQueue(zk)); + assertTrue(MerrittLocks.checkLockLargeAccessQueue(zk)); assertFalse(MerrittLocks.lockLargeAccessQueue(zk)); MerrittLocks.unlockLargeAccessQueue(zk); assertTrue(MerrittLocks.lockLargeAccessQueue(zk)); @@ -1253,7 +1257,9 @@ public void lockAccessQueue() throws KeeperException, InterruptedException, Merr @Test public void lockCollection() throws KeeperException, InterruptedException, MerrittZKNodeInvalid{ load(Tests.lock_collection); + assertFalse(MerrittLocks.checkLockCollection(zk, "foo")); assertTrue(MerrittLocks.lockCollection(zk, "foo")); + assertTrue(MerrittLocks.checkLockCollection(zk, "foo")); assertFalse(MerrittLocks.lockCollection(zk, "foo")); MerrittLocks.unlockCollection(zk, "foo"); assertTrue(MerrittLocks.lockCollection(zk, "foo")); @@ -1267,7 +1273,9 @@ public void lockCollection() throws KeeperException, InterruptedException, Merri @Test public void lockStore() throws KeeperException, InterruptedException, MerrittZKNodeInvalid{ load(Tests.lock_store); + assertFalse(MerrittLocks.checkLockObjectStorage(zk, "ark:/aaa/111")); assertTrue(MerrittLocks.lockObjectStorage(zk, "ark:/aaa/111")); + assertTrue(MerrittLocks.checkLockObjectStorage(zk, "ark:/aaa/111")); assertFalse(MerrittLocks.lockObjectStorage(zk, "ark:/aaa/111")); MerrittLocks.unlockObjectStorage(zk, "ark:/aaa/111"); assertTrue(MerrittLocks.lockObjectStorage(zk, "ark:/aaa/111"));