{"action":"created","comment":{"url":"https://api.github.com/repos/spotify/scio/pulls/comments/109456038","pull_request_review_id":30559147,"id":109456038,"diff_hunk":"@@ -0,0 +1,98 @@\n+/*\n+ * Copyright 2017 Spotify AB.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied.  See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package com.spotify.scio.extra.transforms;\n+\n+import com.google.common.collect.Maps;\n+import com.google.common.collect.Queues;\n+import org.apache.beam.sdk.transforms.DoFn;\n+\n+import java.util.UUID;\n+import java.util.concurrent.ConcurrentLinkedQueue;\n+import java.util.concurrent.ConcurrentMap;\n+import java.util.concurrent.ExecutionException;\n+import java.util.function.Function;\n+\n+/**\n+ * A {@link DoFn} that handles asynchronous requests to an external service.\n+ */\n+public abstract class BaseAsyncDoFn<InputT, OutputT, ResourceT, FutureT>\n+    extends DoFnWithResource<InputT, OutputT, ResourceT> {\n+\n+  /**\n+   * Process an element asynchronously.\n+   */\n+  public abstract FutureT processElement(InputT input);\n+\n+  protected abstract void waitForFutures(Iterable<FutureT> futures)\n+      throws InterruptedException, ExecutionException;\n+  protected abstract FutureT addCallback(FutureT future,\n+                                         Function<OutputT, Void> onSuccess,\n+                                         Function<Throwable, Void> onFailure);\n+\n+  private final ConcurrentMap<UUID, FutureT> futures = Maps.newConcurrentMap();\n+  private final ConcurrentLinkedQueue<OutputT> results = Queues.newConcurrentLinkedQueue();\n+  private final ConcurrentLinkedQueue<Throwable> errors = Queues.newConcurrentLinkedQueue();\n+\n+  @StartBundle\n+  public void startBundle(Context c) {\n+    futures.clear();\n+    results.clear();\n+    errors.clear();\n+  }\n+\n+  @FinishBundle\n+  public void finishBundle(Context c) {\n+    if (!futures.isEmpty()) {\n+      try {\n+        waitForFutures(futures.values());\n+      } catch (InterruptedException e) {\n+        Thread.currentThread().interrupt();\n+        throw new RuntimeException(\"Failed to process futures\", e);\n+      } catch (ExecutionException e) {\n+        throw new RuntimeException(\"Failed to process futures\", e);\n+      }\n+    }\n+    flush(c);\n+  }\n+\n+  @ProcessElement\n+  public void processElement(ProcessContext c) {\n+    flush(c);\n+\n+    final UUID uuid = UUID.randomUUID();\n+    FutureT future = addCallback(processElement(c.element()), r -> {\n+      results.add(r);\n+      futures.remove(uuid);\n+      return null;\n+    }, t -> {\n+      errors.add(t);\n+      futures.remove(uuid);\n+      return null;\n+    });\n+    futures.put(uuid, future);","path":"scio-extra/src/main/java/com/spotify/scio/extra/transforms/BaseAsyncDoFn.java","position":87,"original_position":87,"commit_id":"03d9626e36db2f9bbf2c3b3285f5c5c27064cb75","original_commit_id":"03d9626e36db2f9bbf2c3b3285f5c5c27064cb75","user":{"login":"ravwojdyla","id":1419010,"avatar_url":"https://avatars3.githubusercontent.com/u/1419010?v=3","gravatar_id":"","url":"https://api.github.com/users/ravwojdyla","html_url":"https://github.com/ravwojdyla","followers_url":"https://api.github.com/users/ravwojdyla/followers","following_url":"https://api.github.com/users/ravwojdyla/following{/other_user}","gists_url":"https://api.github.com/users/ravwojdyla/gists{/gist_id}","starred_url":"https://api.github.com/users/ravwojdyla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ravwojdyla/subscriptions","organizations_url":"https://api.github.com/users/ravwojdyla/orgs","repos_url":"https://api.github.com/users/ravwojdyla/repos","events_url":"https://api.github.com/users/ravwojdyla/events{/privacy}","received_events_url":"https://api.github.com/users/ravwojdyla/received_events","type":"User","site_admin":false},"body":"Just checking - is there possible race condition here if [remove](https://github.com/spotify/scio/pull/509/files#diff-090a5f98b929a7979f715501ada577edR80) gets executed before this `put`?","created_at":"2017-04-03T16:01:33Z","updated_at":"2017-04-03T16:01:33Z","html_url":"https://github.com/spotify/scio/pull/509#discussion_r109456038","pull_request_url":"https://api.github.com/repos/spotify/scio/pulls/509","_links":{"self":{"href":"https://api.github.com/repos/spotify/scio/pulls/comments/109456038"},"html":{"href":"https://github.com/spotify/scio/pull/509#discussion_r109456038"},"pull_request":{"href":"https://api.github.com/repos/spotify/scio/pulls/509"}}},"pull_request":{"url":"https://api.github.com/repos/spotify/scio/pulls/509","id":113086435,"html_url":"https://github.com/spotify/scio/pull/509","diff_url":"https://github.com/spotify/scio/pull/509.diff","patch_url":"https://github.com/spotify/scio/pull/509.patch","issue_url":"https://api.github.com/repos/spotify/scio/issues/509","number":509,"state":"open","locked":false,"title":"add some extra DoFns","user":{"login":"nevillelyh","id":468514,"avatar_url":"https://avatars3.githubusercontent.com/u/468514?v=3","gravatar_id":"","url":"https://api.github.com/users/nevillelyh","html_url":"https://github.com/nevillelyh","followers_url":"https://api.github.com/users/nevillelyh/followers","following_url":"https://api.github.com/users/nevillelyh/following{/other_user}","gists_url":"https://api.github.com/users/nevillelyh/gists{/gist_id}","starred_url":"https://api.github.com/users/nevillelyh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nevillelyh/subscriptions","organizations_url":"https://api.github.com/users/nevillelyh/orgs","repos_url":"https://api.github.com/users/nevillelyh/repos","events_url":"https://api.github.com/users/nevillelyh/events{/privacy}","received_events_url":"https://api.github.com/users/nevillelyh/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-03-28T23:08:23Z","updated_at":"2017-04-03T16:01:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d26e14bbd4e74a652cfc8eb56353b59c50047984","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/spotify/scio/pulls/509/commits","review_comments_url":"https://api.github.com/repos/spotify/scio/pulls/509/comments","review_comment_url":"https://api.github.com/repos/spotify/scio/pulls/comments{/number}","comments_url":"https://api.github.com/repos/spotify/scio/issues/509/comments","statuses_url":"https://api.github.com/repos/spotify/scio/statuses/03d9626e36db2f9bbf2c3b3285f5c5c27064cb75","head":{"label":"spotify:neville/dofns","ref":"neville/dofns","sha":"03d9626e36db2f9bbf2c3b3285f5c5c27064cb75","user":{"login":"spotify","id":251374,"avatar_url":"https://avatars1.githubusercontent.com/u/251374?v=3","gravatar_id":"","url":"https://api.github.com/users/spotify","html_url":"https://github.com/spotify","followers_url":"https://api.github.com/users/spotify/followers","following_url":"https://api.github.com/users/spotify/following{/other_user}","gists_url":"https://api.github.com/users/spotify/gists{/gist_id}","starred_url":"https://api.github.com/users/spotify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spotify/subscriptions","organizations_url":"https://api.github.com/users/spotify/orgs","repos_url":"https://api.github.com/users/spotify/repos","events_url":"https://api.github.com/users/spotify/events{/privacy}","received_events_url":"https://api.github.com/users/spotify/received_events","type":"Organization","site_admin":false},"repo":{"id":32946662,"name":"scio","full_name":"spotify/scio","owner":{"login":"spotify","id":251374,"avatar_url":"https://avatars1.githubusercontent.com/u/251374?v=3","gravatar_id":"","url":"https://api.github.com/users/spotify","html_url":"https://github.com/spotify","followers_url":"https://api.github.com/users/spotify/followers","following_url":"https://api.github.com/users/spotify/following{/other_user}","gists_url":"https://api.github.com/users/spotify/gists{/gist_id}","starred_url":"https://api.github.com/users/spotify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spotify/subscriptions","organizations_url":"https://api.github.com/users/spotify/orgs","repos_url":"https://api.github.com/users/spotify/repos","events_url":"https://api.github.com/users/spotify/events{/privacy}","received_events_url":"https://api.github.com/users/spotify/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/spotify/scio","description":"A Scala API for Apache Beam and Google Cloud Dataflow","fork":false,"url":"https://api.github.com/repos/spotify/scio","forks_url":"https://api.github.com/repos/spotify/scio/forks","keys_url":"https://api.github.com/repos/spotify/scio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/spotify/scio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/spotify/scio/teams","hooks_url":"https://api.github.com/repos/spotify/scio/hooks","issue_events_url":"https://api.github.com/repos/spotify/scio/issues/events{/number}","events_url":"https://api.github.com/repos/spotify/scio/events","assignees_url":"https://api.github.com/repos/spotify/scio/assignees{/user}","branches_url":"https://api.github.com/repos/spotify/scio/branches{/branch}","tags_url":"https://api.github.com/repos/spotify/scio/tags","blobs_url":"https://api.github.com/repos/spotify/scio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/spotify/scio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/spotify/scio/git/refs{/sha}","trees_url":"https://api.github.com/repos/spotify/scio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/spotify/scio/statuses/{sha}","languages_url":"https://api.github.com/repos/spotify/scio/languages","stargazers_url":"https://api.github.com/repos/spotify/scio/stargazers","contributors_url":"https://api.github.com/repos/spotify/scio/contributors","subscribers_url":"https://api.github.com/repos/spotify/scio/subscribers","subscription_url":"https://api.github.com/repos/spotify/scio/subscription","commits_url":"https://api.github.com/repos/spotify/scio/commits{/sha}","git_commits_url":"https://api.github.com/repos/spotify/scio/git/commits{/sha}","comments_url":"https://api.github.com/repos/spotify/scio/comments{/number}","issue_comment_url":"https://api.github.com/repos/spotify/scio/issues/comments{/number}","contents_url":"https://api.github.com/repos/spotify/scio/contents/{+path}","compare_url":"https://api.github.com/repos/spotify/scio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/spotify/scio/merges","archive_url":"https://api.github.com/repos/spotify/scio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/spotify/scio/downloads","issues_url":"https://api.github.com/repos/spotify/scio/issues{/number}","pulls_url":"https://api.github.com/repos/spotify/scio/pulls{/number}","milestones_url":"https://api.github.com/repos/spotify/scio/milestones{/number}","notifications_url":"https://api.github.com/repos/spotify/scio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/spotify/scio/labels{/name}","releases_url":"https://api.github.com/repos/spotify/scio/releases{/id}","deployments_url":"https://api.github.com/repos/spotify/scio/deployments","created_at":"2015-03-26T19:07:34Z","updated_at":"2017-04-03T15:26:32Z","pushed_at":"2017-04-03T15:27:12Z","git_url":"git://github.com/spotify/scio.git","ssh_url":"git@github.com:spotify/scio.git","clone_url":"https://github.com/spotify/scio.git","svn_url":"https://github.com/spotify/scio","homepage":"http://spotify.github.io/scio","size":4670,"stargazers_count":474,"watchers_count":474,"language":"Scala","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":65,"mirror_url":null,"open_issues_count":22,"forks":65,"open_issues":22,"watchers":474,"default_branch":"master"}},"base":{"label":"spotify:master","ref":"master","sha":"b3c79d1b7a24f3d23f1e3bfc3641d31495de257a","user":{"login":"spotify","id":251374,"avatar_url":"https://avatars1.githubusercontent.com/u/251374?v=3","gravatar_id":"","url":"https://api.github.com/users/spotify","html_url":"https://github.com/spotify","followers_url":"https://api.github.com/users/spotify/followers","following_url":"https://api.github.com/users/spotify/following{/other_user}","gists_url":"https://api.github.com/users/spotify/gists{/gist_id}","starred_url":"https://api.github.com/users/spotify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spotify/subscriptions","organizations_url":"https://api.github.com/users/spotify/orgs","repos_url":"https://api.github.com/users/spotify/repos","events_url":"https://api.github.com/users/spotify/events{/privacy}","received_events_url":"https://api.github.com/users/spotify/received_events","type":"Organization","site_admin":false},"repo":{"id":32946662,"name":"scio","full_name":"spotify/scio","owner":{"login":"spotify","id":251374,"avatar_url":"https://avatars1.githubusercontent.com/u/251374?v=3","gravatar_id":"","url":"https://api.github.com/users/spotify","html_url":"https://github.com/spotify","followers_url":"https://api.github.com/users/spotify/followers","following_url":"https://api.github.com/users/spotify/following{/other_user}","gists_url":"https://api.github.com/users/spotify/gists{/gist_id}","starred_url":"https://api.github.com/users/spotify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spotify/subscriptions","organizations_url":"https://api.github.com/users/spotify/orgs","repos_url":"https://api.github.com/users/spotify/repos","events_url":"https://api.github.com/users/spotify/events{/privacy}","received_events_url":"https://api.github.com/users/spotify/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/spotify/scio","description":"A Scala API for Apache Beam and Google Cloud Dataflow","fork":false,"url":"https://api.github.com/repos/spotify/scio","forks_url":"https://api.github.com/repos/spotify/scio/forks","keys_url":"https://api.github.com/repos/spotify/scio/keys{/key_id}","collaborators_url":"https://api.github.com/repos/spotify/scio/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/spotify/scio/teams","hooks_url":"https://api.github.com/repos/spotify/scio/hooks","issue_events_url":"https://api.github.com/repos/spotify/scio/issues/events{/number}","events_url":"https://api.github.com/repos/spotify/scio/events","assignees_url":"https://api.github.com/repos/spotify/scio/assignees{/user}","branches_url":"https://api.github.com/repos/spotify/scio/branches{/branch}","tags_url":"https://api.github.com/repos/spotify/scio/tags","blobs_url":"https://api.github.com/repos/spotify/scio/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/spotify/scio/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/spotify/scio/git/refs{/sha}","trees_url":"https://api.github.com/repos/spotify/scio/git/trees{/sha}","statuses_url":"https://api.github.com/repos/spotify/scio/statuses/{sha}","languages_url":"https://api.github.com/repos/spotify/scio/languages","stargazers_url":"https://api.github.com/repos/spotify/scio/stargazers","contributors_url":"https://api.github.com/repos/spotify/scio/contributors","subscribers_url":"https://api.github.com/repos/spotify/scio/subscribers","subscription_url":"https://api.github.com/repos/spotify/scio/subscription","commits_url":"https://api.github.com/repos/spotify/scio/commits{/sha}","git_commits_url":"https://api.github.com/repos/spotify/scio/git/commits{/sha}","comments_url":"https://api.github.com/repos/spotify/scio/comments{/number}","issue_comment_url":"https://api.github.com/repos/spotify/scio/issues/comments{/number}","contents_url":"https://api.github.com/repos/spotify/scio/contents/{+path}","compare_url":"https://api.github.com/repos/spotify/scio/compare/{base}...{head}","merges_url":"https://api.github.com/repos/spotify/scio/merges","archive_url":"https://api.github.com/repos/spotify/scio/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/spotify/scio/downloads","issues_url":"https://api.github.com/repos/spotify/scio/issues{/number}","pulls_url":"https://api.github.com/repos/spotify/scio/pulls{/number}","milestones_url":"https://api.github.com/repos/spotify/scio/milestones{/number}","notifications_url":"https://api.github.com/repos/spotify/scio/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/spotify/scio/labels{/name}","releases_url":"https://api.github.com/repos/spotify/scio/releases{/id}","deployments_url":"https://api.github.com/repos/spotify/scio/deployments","created_at":"2015-03-26T19:07:34Z","updated_at":"2017-04-03T15:26:32Z","pushed_at":"2017-04-03T15:27:12Z","git_url":"git://github.com/spotify/scio.git","ssh_url":"git@github.com:spotify/scio.git","clone_url":"https://github.com/spotify/scio.git","svn_url":"https://github.com/spotify/scio","homepage":"http://spotify.github.io/scio","size":4670,"stargazers_count":474,"watchers_count":474,"language":"Scala","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":65,"mirror_url":null,"open_issues_count":22,"forks":65,"open_issues":22,"watchers":474,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/spotify/scio/pulls/509"},"html":{"href":"https://github.com/spotify/scio/pull/509"},"issue":{"href":"https://api.github.com/repos/spotify/scio/issues/509"},"comments":{"href":"https://api.github.com/repos/spotify/scio/issues/509/comments"},"review_comments":{"href":"https://api.github.com/repos/spotify/scio/pulls/509/comments"},"review_comment":{"href":"https://api.github.com/repos/spotify/scio/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/spotify/scio/pulls/509/commits"},"statuses":{"href":"https://api.github.com/repos/spotify/scio/statuses/03d9626e36db2f9bbf2c3b3285f5c5c27064cb75"}}}}
{"actor":{"display_login":"ravwojdyla"}}