{"action":"created","comment":{"url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/comments/44585162","id":44585162,"diff_hunk":"@@ -0,0 +1,120 @@\n+package org.sagebionetworks.workers.util.semaphore;\r\n+\r\n+import org.apache.logging.log4j.LogManager;\r\n+import org.apache.logging.log4j.Logger;\r\n+import org.sagebionetworks.common.util.Clock;\r\n+import org.sagebionetworks.common.util.progress.ProgressCallback;\r\n+import org.sagebionetworks.common.util.progress.ProgressingCallable;\r\n+import org.sagebionetworks.common.util.progress.ThrottlingProgressCallback;\r\n+import org.sagebionetworks.database.semaphore.WriteReadSemaphore;\r\n+\r\n+public class WriteReadSemaphoreRunnerImpl implements WriteReadSemaphoreRunner {\r\n+\r\n+\tprivate static final Logger log = LogManager\r\n+\t\t\t.getLogger(SemaphoreGatedRunnerImpl.class);\r\n+\t\r\n+\tWriteReadSemaphore writeReadSemaphore;\r\n+\tClock clock;\r\n+\t\r\n+\t/**\r\n+\t * Create a new runner for each use.\r\n+\t * @param writeReadSemaphore\r\n+\t * @param clock\r\n+\t */\r\n+\tpublic WriteReadSemaphoreRunnerImpl(WriteReadSemaphore writeReadSemaphore, Clock clock) {\r\n+\t\tsuper();\r\n+\t\tthis.writeReadSemaphore = writeReadSemaphore;\r\n+\t\tthis.clock = clock;\r\n+\t}\r\n+\r\n+\t/*\r\n+\t * (non-Javadoc)\r\n+\t * @see org.sagebionetworks.workers.util.semaphore.WriteReadSemaphoreRunner#tryRunWithWriteLock(java.lang.String, long, org.sagebionetworks.workers.util.progress.ProgressingCallable)\r\n+\t */\r\n+\t@Override\r\n+\tpublic <R, T> R tryRunWithWriteLock(final ProgressCallback<T> callback, final String lockKey, final int lockTimeoutSec,\r\n+\t\t\tProgressingCallable<R,T> callable) throws Exception {\r\n+\t\tif(lockKey == null){\r\n+\t\t\tthrow new IllegalArgumentException(\"LockKey cannot be null\");\r\n+\t\t}\r\n+\t\tif(lockTimeoutSec < 2){\r\n+\t\t\tthrow new IllegalArgumentException(\"LockTimeout cannot be less than 2 seconds\");\r\n+\t\t}\r\n+\t\tlong halfTimeoutMs = (lockTimeoutSec/2)*1000;\r\n+\t\tif(callable == null){\r\n+\t\t\tthrow new IllegalArgumentException(\"Callable cannot be null\");\r\n+\t\t}\r\n+\t\t\r\n+\t\tString precursorToken = this.writeReadSemaphore.acquireWriteLockPrecursor(lockKey, lockTimeoutSec);\r\n+\t\tif(precursorToken == null){\r\n+\t\t\tthrow new LockUnavilableException(\"Cannot get an write lock for key:\"+lockKey);\r\n+\t\t}\r\n+\t\t// while holding the precursor attempt to get the write lock\r\n+\t\tString writeToken = null;\r\n+\t\twhile(writeToken == null){\r\n+\t\t\twriteToken = this.writeReadSemaphore.acquireWriteLock(lockKey, precursorToken, lockTimeoutSec);\r\n+\t\t\tif(writeToken == null){\r\n+\t\t\t\tlog.debug(\"Waiting for write lock on key: \"+lockKey+\"...\");\r\n+\t\t\t\tclock.sleep(halfTimeoutMs);\r\n+\t\t\t}\r\n+\t\t}\r\n+\t\tfinal String finalWriteToken = writeToken;\r\n+\t\t// once we have the write lock we are ready to run\r\n+\t\ttry{\r\n+\t\t\treturn callable.call((new ThrottlingProgressCallback<T>(new ProgressCallback<T>() {\r\n+\t\t\t\t@Override\r\n+\t\t\t\tpublic void progressMade(T t) {\r\n+\t\t\t\t\t// as progress is made refresh the write lock\r\n+\t\t\t\t\twriteReadSemaphore.refreshWriteLock(lockKey, finalWriteToken, lockTimeoutSec);\r\n+\t\t\t\t\t// forward if a callabck was provided.\r\n+\t\t\t\t\tif(callback != null){\r\n+\t\t\t\t\t\tcallback.progressMade(t);\r\n+\t\t\t\t\t}\r\n+\t\t\t\t}\r\n+\t\t\t}, halfTimeoutMs, clock)));\r\n+\t\t}finally{\r\n+\t\t\tif(writeToken != null){\r\n+\t\t\t\twriteReadSemaphore.releaseWriteLock(lockKey, writeToken);\r\n+\t\t\t}\r\n+\t\t}\r\n+\t}\r\n+\r\n+\t/*\r\n+\t * (non-Javadoc)\r\n+\t * @see org.sagebionetworks.workers.util.semaphore.WriteReadSemaphoreRunner#tryRunWithReadLock(java.lang.String, long, org.sagebionetworks.workers.util.progress.ProgressingCallable)\r\n+\t */\r\n+\t@Override\r\n+\tpublic <R,T> R tryRunWithReadLock(final ProgressCallback<T> callback, final String lockKey, final int lockTimeoutSec,\r\n+\t\t\tfinal ProgressingCallable<R,T> callable) throws Exception {\r\n+\t\tif(lockKey == null){\r\n+\t\t\tthrow new IllegalArgumentException(\"LockKey cannot be null\");\r\n+\t\t}\r\n+\t\tif(lockTimeoutSec < 2){\r\n+\t\t\tthrow new IllegalArgumentException(\"LockTimeout cannot be less than 2 seconds\");\r\n+\t\t}\r\n+\t\tlong halfTimeoutMs = (lockTimeoutSec/2)*1000;\r\n+\t\tif(callable == null){\r\n+\t\t\tthrow new IllegalArgumentException(\"Callable cannot be null\");\r\n+\t\t}\r\n+\t\tfinal String readToken = this.writeReadSemaphore.acquireReadLock(lockKey, lockTimeoutSec);\r\n+\t\tif(readToken == null){\r\n+\t\t\tthrow new LockUnavilableException(\"Cannot get an write lock for key:\"+lockKey);\r","path":"src/main/java/org/sagebionetworks/workers/util/semaphore/WriteReadSemaphoreRunnerImpl.java","position":101,"original_position":101,"commit_id":"f14211bf7383e493163e7928722996a1109439a3","original_commit_id":"f14211bf7383e493163e7928722996a1109439a3","user":{"login":"kimyen","id":4404426,"avatar_url":"https://avatars.githubusercontent.com/u/4404426?v=3","gravatar_id":"","url":"https://api.github.com/users/kimyen","html_url":"https://github.com/kimyen","followers_url":"https://api.github.com/users/kimyen/followers","following_url":"https://api.github.com/users/kimyen/following{/other_user}","gists_url":"https://api.github.com/users/kimyen/gists{/gist_id}","starred_url":"https://api.github.com/users/kimyen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kimyen/subscriptions","organizations_url":"https://api.github.com/users/kimyen/orgs","repos_url":"https://api.github.com/users/kimyen/repos","events_url":"https://api.github.com/users/kimyen/events{/privacy}","received_events_url":"https://api.github.com/users/kimyen/received_events","type":"User","site_admin":false},"body":"throw new LockUnavilableException(\"Cannot get an read lock for key:\"+lockKey);","created_at":"2015-11-11T20:57:40Z","updated_at":"2015-11-11T20:57:40Z","html_url":"https://github.com/Sage-Bionetworks/worker-utilities/pull/21#discussion_r44585162","pull_request_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21","_links":{"self":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/comments/44585162"},"html":{"href":"https://github.com/Sage-Bionetworks/worker-utilities/pull/21#discussion_r44585162"},"pull_request":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21"}}},"pull_request":{"url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21","id":50338591,"html_url":"https://github.com/Sage-Bionetworks/worker-utilities/pull/21","diff_url":"https://github.com/Sage-Bionetworks/worker-utilities/pull/21.diff","patch_url":"https://github.com/Sage-Bionetworks/worker-utilities/pull/21.patch","issue_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/issues/21","number":21,"state":"open","locked":false,"title":"Write read","user":{"login":"john-hill","id":1728181,"avatar_url":"https://avatars.githubusercontent.com/u/1728181?v=3","gravatar_id":"","url":"https://api.github.com/users/john-hill","html_url":"https://github.com/john-hill","followers_url":"https://api.github.com/users/john-hill/followers","following_url":"https://api.github.com/users/john-hill/following{/other_user}","gists_url":"https://api.github.com/users/john-hill/gists{/gist_id}","starred_url":"https://api.github.com/users/john-hill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/john-hill/subscriptions","organizations_url":"https://api.github.com/users/john-hill/orgs","repos_url":"https://api.github.com/users/john-hill/repos","events_url":"https://api.github.com/users/john-hill/events{/privacy}","received_events_url":"https://api.github.com/users/john-hill/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-11T01:50:45Z","updated_at":"2015-11-11T20:57:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ddc0e89dc23319d0036462c7f16362d6dde0726e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21/commits","review_comments_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21/comments","review_comment_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/issues/21/comments","statuses_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/statuses/f14211bf7383e493163e7928722996a1109439a3","head":{"label":"john-hill:write-read","ref":"write-read","sha":"f14211bf7383e493163e7928722996a1109439a3","user":{"login":"john-hill","id":1728181,"avatar_url":"https://avatars.githubusercontent.com/u/1728181?v=3","gravatar_id":"","url":"https://api.github.com/users/john-hill","html_url":"https://github.com/john-hill","followers_url":"https://api.github.com/users/john-hill/followers","following_url":"https://api.github.com/users/john-hill/following{/other_user}","gists_url":"https://api.github.com/users/john-hill/gists{/gist_id}","starred_url":"https://api.github.com/users/john-hill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/john-hill/subscriptions","organizations_url":"https://api.github.com/users/john-hill/orgs","repos_url":"https://api.github.com/users/john-hill/repos","events_url":"https://api.github.com/users/john-hill/events{/privacy}","received_events_url":"https://api.github.com/users/john-hill/received_events","type":"User","site_admin":false},"repo":{"id":37153054,"name":"worker-utilities","full_name":"john-hill/worker-utilities","owner":{"login":"john-hill","id":1728181,"avatar_url":"https://avatars.githubusercontent.com/u/1728181?v=3","gravatar_id":"","url":"https://api.github.com/users/john-hill","html_url":"https://github.com/john-hill","followers_url":"https://api.github.com/users/john-hill/followers","following_url":"https://api.github.com/users/john-hill/following{/other_user}","gists_url":"https://api.github.com/users/john-hill/gists{/gist_id}","starred_url":"https://api.github.com/users/john-hill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/john-hill/subscriptions","organizations_url":"https://api.github.com/users/john-hill/orgs","repos_url":"https://api.github.com/users/john-hill/repos","events_url":"https://api.github.com/users/john-hill/events{/privacy}","received_events_url":"https://api.github.com/users/john-hill/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/john-hill/worker-utilities","description":"","fork":true,"url":"https://api.github.com/repos/john-hill/worker-utilities","forks_url":"https://api.github.com/repos/john-hill/worker-utilities/forks","keys_url":"https://api.github.com/repos/john-hill/worker-utilities/keys{/key_id}","collaborators_url":"https://api.github.com/repos/john-hill/worker-utilities/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/john-hill/worker-utilities/teams","hooks_url":"https://api.github.com/repos/john-hill/worker-utilities/hooks","issue_events_url":"https://api.github.com/repos/john-hill/worker-utilities/issues/events{/number}","events_url":"https://api.github.com/repos/john-hill/worker-utilities/events","assignees_url":"https://api.github.com/repos/john-hill/worker-utilities/assignees{/user}","branches_url":"https://api.github.com/repos/john-hill/worker-utilities/branches{/branch}","tags_url":"https://api.github.com/repos/john-hill/worker-utilities/tags","blobs_url":"https://api.github.com/repos/john-hill/worker-utilities/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/john-hill/worker-utilities/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/john-hill/worker-utilities/git/refs{/sha}","trees_url":"https://api.github.com/repos/john-hill/worker-utilities/git/trees{/sha}","statuses_url":"https://api.github.com/repos/john-hill/worker-utilities/statuses/{sha}","languages_url":"https://api.github.com/repos/john-hill/worker-utilities/languages","stargazers_url":"https://api.github.com/repos/john-hill/worker-utilities/stargazers","contributors_url":"https://api.github.com/repos/john-hill/worker-utilities/contributors","subscribers_url":"https://api.github.com/repos/john-hill/worker-utilities/subscribers","subscription_url":"https://api.github.com/repos/john-hill/worker-utilities/subscription","commits_url":"https://api.github.com/repos/john-hill/worker-utilities/commits{/sha}","git_commits_url":"https://api.github.com/repos/john-hill/worker-utilities/git/commits{/sha}","comments_url":"https://api.github.com/repos/john-hill/worker-utilities/comments{/number}","issue_comment_url":"https://api.github.com/repos/john-hill/worker-utilities/issues/comments{/number}","contents_url":"https://api.github.com/repos/john-hill/worker-utilities/contents/{+path}","compare_url":"https://api.github.com/repos/john-hill/worker-utilities/compare/{base}...{head}","merges_url":"https://api.github.com/repos/john-hill/worker-utilities/merges","archive_url":"https://api.github.com/repos/john-hill/worker-utilities/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/john-hill/worker-utilities/downloads","issues_url":"https://api.github.com/repos/john-hill/worker-utilities/issues{/number}","pulls_url":"https://api.github.com/repos/john-hill/worker-utilities/pulls{/number}","milestones_url":"https://api.github.com/repos/john-hill/worker-utilities/milestones{/number}","notifications_url":"https://api.github.com/repos/john-hill/worker-utilities/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/john-hill/worker-utilities/labels{/name}","releases_url":"https://api.github.com/repos/john-hill/worker-utilities/releases{/id}","created_at":"2015-06-09T19:20:03Z","updated_at":"2015-06-10T01:54:24Z","pushed_at":"2015-11-11T05:55:49Z","git_url":"git://github.com/john-hill/worker-utilities.git","ssh_url":"git@github.com:john-hill/worker-utilities.git","clone_url":"https://github.com/john-hill/worker-utilities.git","svn_url":"https://github.com/john-hill/worker-utilities","homepage":null,"size":234,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"Sage-Bionetworks:develop","ref":"develop","sha":"a9411270aee01121e79a6a4047b42619f99c4c30","user":{"login":"Sage-Bionetworks","id":1250897,"avatar_url":"https://avatars.githubusercontent.com/u/1250897?v=3","gravatar_id":"","url":"https://api.github.com/users/Sage-Bionetworks","html_url":"https://github.com/Sage-Bionetworks","followers_url":"https://api.github.com/users/Sage-Bionetworks/followers","following_url":"https://api.github.com/users/Sage-Bionetworks/following{/other_user}","gists_url":"https://api.github.com/users/Sage-Bionetworks/gists{/gist_id}","starred_url":"https://api.github.com/users/Sage-Bionetworks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sage-Bionetworks/subscriptions","organizations_url":"https://api.github.com/users/Sage-Bionetworks/orgs","repos_url":"https://api.github.com/users/Sage-Bionetworks/repos","events_url":"https://api.github.com/users/Sage-Bionetworks/events{/privacy}","received_events_url":"https://api.github.com/users/Sage-Bionetworks/received_events","type":"Organization","site_admin":false},"repo":{"id":37152996,"name":"worker-utilities","full_name":"Sage-Bionetworks/worker-utilities","owner":{"login":"Sage-Bionetworks","id":1250897,"avatar_url":"https://avatars.githubusercontent.com/u/1250897?v=3","gravatar_id":"","url":"https://api.github.com/users/Sage-Bionetworks","html_url":"https://github.com/Sage-Bionetworks","followers_url":"https://api.github.com/users/Sage-Bionetworks/followers","following_url":"https://api.github.com/users/Sage-Bionetworks/following{/other_user}","gists_url":"https://api.github.com/users/Sage-Bionetworks/gists{/gist_id}","starred_url":"https://api.github.com/users/Sage-Bionetworks/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sage-Bionetworks/subscriptions","organizations_url":"https://api.github.com/users/Sage-Bionetworks/orgs","repos_url":"https://api.github.com/users/Sage-Bionetworks/repos","events_url":"https://api.github.com/users/Sage-Bionetworks/events{/privacy}","received_events_url":"https://api.github.com/users/Sage-Bionetworks/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Sage-Bionetworks/worker-utilities","description":"","fork":false,"url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities","forks_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/forks","keys_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/teams","hooks_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/hooks","issue_events_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/issues/events{/number}","events_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/events","assignees_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/assignees{/user}","branches_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/branches{/branch}","tags_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/tags","blobs_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/git/refs{/sha}","trees_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/statuses/{sha}","languages_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/languages","stargazers_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/stargazers","contributors_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/contributors","subscribers_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/subscribers","subscription_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/subscription","commits_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/commits{/sha}","git_commits_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/git/commits{/sha}","comments_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/comments{/number}","issue_comment_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/issues/comments{/number}","contents_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/contents/{+path}","compare_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/merges","archive_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/downloads","issues_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/issues{/number}","pulls_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls{/number}","milestones_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/milestones{/number}","notifications_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/labels{/name}","releases_url":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/releases{/id}","created_at":"2015-06-09T19:19:07Z","updated_at":"2015-08-10T19:36:25Z","pushed_at":"2015-11-11T05:55:50Z","git_url":"git://github.com/Sage-Bionetworks/worker-utilities.git","ssh_url":"git@github.com:Sage-Bionetworks/worker-utilities.git","clone_url":"https://github.com/Sage-Bionetworks/worker-utilities.git","svn_url":"https://github.com/Sage-Bionetworks/worker-utilities","homepage":null,"size":328,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":0,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21"},"html":{"href":"https://github.com/Sage-Bionetworks/worker-utilities/pull/21"},"issue":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/issues/21"},"comments":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/issues/21/comments"},"review_comments":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21/comments"},"review_comment":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/pulls/21/commits"},"statuses":{"href":"https://api.github.com/repos/Sage-Bionetworks/worker-utilities/statuses/f14211bf7383e493163e7928722996a1109439a3"}}}}