Skip to content

Commit

Permalink
check lock methods
Browse files Browse the repository at this point in the history
  • Loading branch information
terrywbrady committed Jun 21, 2024
1 parent 7385ccd commit d618f38
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/org/cdlib/mrt/zk/MerrittLocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,29 @@ 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);
}
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);
}
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());
Expand All @@ -66,17 +75,27 @@ 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());
}
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());
}
}
24 changes: 24 additions & 0 deletions src/main/ruby/lib/merritt_zk_locks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/main/ruby/spec/zk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/cdlib/mrt/zk/ZKTestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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"));
Expand All @@ -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"));
Expand Down

0 comments on commit d618f38

Please sign in to comment.