{"action":"created","comment":{"url":"https://api.github.com/repos/druid-io/druid/pulls/comments/62949073","id":62949073,"diff_hunk":"@@ -0,0 +1,243 @@\n+/*\n+ *\n+ *  Licensed to Metamarkets Group Inc. (Metamarkets) under one\n+ *  or more contributor license agreements. See the NOTICE file\n+ *  distributed with this work for additional information\n+ *  regarding copyright ownership. Metamarkets licenses this file\n+ *  to you under the Apache License, Version 2.0 (the\n+ *  \"License\"); you may not use this file except in compliance\n+ *  with the License. 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+\n+package io.druid.server.lookup;\n+\n+import com.google.common.base.Preconditions;\n+import com.google.common.base.Strings;\n+import com.google.common.util.concurrent.ListenableFuture;\n+import com.google.common.util.concurrent.ListeningScheduledExecutorService;\n+import com.google.common.util.concurrent.MoreExecutors;\n+import com.google.common.util.concurrent.ThreadFactoryBuilder;\n+import com.metamx.common.ISE;\n+import com.metamx.common.logger.Logger;\n+import io.druid.query.lookup.LookupExtractor;\n+import io.druid.server.lookup.cache.polling.OnHeapPollingCache;\n+import io.druid.server.lookup.cache.polling.PollingCache;\n+import io.druid.server.lookup.cache.polling.PollingCacheFactory;\n+import org.joda.time.Period;\n+\n+import javax.validation.constraints.NotNull;\n+import java.util.List;\n+import java.util.concurrent.Executors;\n+import java.util.concurrent.TimeUnit;\n+import java.util.concurrent.atomic.AtomicBoolean;\n+import java.util.concurrent.atomic.AtomicReference;\n+\n+public class PollingLookup extends LookupExtractor\n+{\n+  private static final Logger LOGGER = new Logger(PollingLookup.class);\n+\n+  private final Period pollPeriod;\n+\n+  private final DataFetcher dataFetcher;\n+\n+  private final PollingCacheFactory cacheFactory;\n+\n+  private final AtomicReference<CacheRefKeeper> refOfCacheKeeper = new AtomicReference<>();\n+\n+\n+  private final ListeningScheduledExecutorService scheduledExecutorService;\n+\n+  private final AtomicBoolean isOpen = new AtomicBoolean(false);\n+\n+  private final ListenableFuture<?> pollFuture;\n+\n+  public PollingLookup(\n+      Period pollPeriod,\n+      DataFetcher dataFetcher,\n+      PollingCacheFactory cacheFactory\n+  )\n+  {\n+\n+    this.pollPeriod = pollPeriod == null ? Period.ZERO : pollPeriod;\n+    this.dataFetcher = Preconditions.checkNotNull(dataFetcher);\n+    this.cacheFactory = cacheFactory == null ? new OnHeapPollingCache.OnHeapPollingCacheProvider() : cacheFactory;\n+    refOfCacheKeeper.set(new CacheRefKeeper(this.cacheFactory.makeOf(dataFetcher.fetchAll())));\n+    if (getPollMs() > 0) {\n+      scheduledExecutorService = MoreExecutors.listeningDecorator(Executors.newSingleThreadScheduledExecutor(\n+          new ThreadFactoryBuilder()\n+              .setDaemon(true)\n+              .setNameFormat(\"PollingLookup-%d\")\n+              .setPriority(Thread.MIN_PRIORITY)\n+              .build()\n+      ));\n+      pollFuture = scheduledExecutorService.scheduleAtFixedRate(\n+          pollAndSwap(),\n+          getPollMs(),\n+          getPollMs(),\n+          TimeUnit.MILLISECONDS\n+      );\n+    } else {\n+      scheduledExecutorService = null;\n+      pollFuture = null;\n+    }\n+    this.isOpen.set(true);\n+  }\n+\n+\n+  public void close()\n+  {\n+    LOGGER.info(\"Closing lookup\");\n+    synchronized (isOpen) {\n+      isOpen.getAndSet(false);\n+      CacheRefKeeper cacheRefKeeper = refOfCacheKeeper.getAndSet(null);\n+      if (cacheRefKeeper != null) {\n+        cacheRefKeeper.doneWithIt();\n+      }\n+      if (pollFuture != null) {\n+        pollFuture.cancel(true);\n+        scheduledExecutorService.shutdown();\n+      }\n+    }\n+  }\n+\n+  @Override\n+  public String apply(@NotNull String key)\n+  {\n+    CacheRefKeeper cacheRefKeeper = refOfCacheKeeper.get();\n+    if (cacheRefKeeper == null) {\n+      throw new ISE(\"Cache reference is null WTF\");\n+    }\n+    PollingCache cache = cacheRefKeeper.get();\n+    try {\n+      if (cache == null) {\n+        // it must've been closed after swapping while I was getting it.  Try again.\n+        return this.apply(key);","path":"extensions-core/druid-lookups/src/main/java/io/druid/server/lookup/PollingLookup.java","position":120,"original_position":124,"commit_id":"76e520c8eb34e8ccbda4835adb615537e22208ce","original_commit_id":"34d2eb135ab03a6138bb00115d9afc04b2e1f5e3","user":{"login":"drcrallen","id":8213081,"avatar_url":"https://avatars.githubusercontent.com/u/8213081?v=3","gravatar_id":"","url":"https://api.github.com/users/drcrallen","html_url":"https://github.com/drcrallen","followers_url":"https://api.github.com/users/drcrallen/followers","following_url":"https://api.github.com/users/drcrallen/following{/other_user}","gists_url":"https://api.github.com/users/drcrallen/gists{/gist_id}","starred_url":"https://api.github.com/users/drcrallen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drcrallen/subscriptions","organizations_url":"https://api.github.com/users/drcrallen/orgs","repos_url":"https://api.github.com/users/drcrallen/repos","events_url":"https://api.github.com/users/drcrallen/events{/privacy}","received_events_url":"https://api.github.com/users/drcrallen/received_events","type":"User","site_admin":false},"body":"If `refOfCacheKeeper.get()` returns an object, but that object's `.get()` method returns null, this will get into a loop where it will stack overflow very fast. ","created_at":"2016-05-12T00:22:01Z","updated_at":"2016-05-12T00:22:01Z","html_url":"https://github.com/druid-io/druid/pull/2819#discussion_r62949073","pull_request_url":"https://api.github.com/repos/druid-io/druid/pulls/2819","_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/comments/62949073"},"html":{"href":"https://github.com/druid-io/druid/pull/2819#discussion_r62949073"},"pull_request":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2819"}}},"pull_request":{"url":"https://api.github.com/repos/druid-io/druid/pulls/2819","id":66146528,"html_url":"https://github.com/druid-io/druid/pull/2819","diff_url":"https://github.com/druid-io/druid/pull/2819.diff","patch_url":"https://github.com/druid-io/druid/pull/2819.patch","issue_url":"https://api.github.com/repos/druid-io/druid/issues/2819","number":2819,"state":"open","locked":false,"title":"[QTL]Cached lookup module for JDBC connectors.","user":{"login":"b-slim","id":7376396,"avatar_url":"https://avatars.githubusercontent.com/u/7376396?v=3","gravatar_id":"","url":"https://api.github.com/users/b-slim","html_url":"https://github.com/b-slim","followers_url":"https://api.github.com/users/b-slim/followers","following_url":"https://api.github.com/users/b-slim/following{/other_user}","gists_url":"https://api.github.com/users/b-slim/gists{/gist_id}","starred_url":"https://api.github.com/users/b-slim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/b-slim/subscriptions","organizations_url":"https://api.github.com/users/b-slim/orgs","repos_url":"https://api.github.com/users/b-slim/repos","events_url":"https://api.github.com/users/b-slim/events{/privacy}","received_events_url":"https://api.github.com/users/b-slim/received_events","type":"User","site_admin":false},"body":"In This PR introduces a cached Lookup leveraging eviction and various caching strategies.\r\nThis Lookup implementation is meant to be user with large Lookup tables that need evictions.\r\n\r\n \r\n","created_at":"2016-04-12T12:11:27Z","updated_at":"2016-05-12T00:22:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1765a366142a28adc5c20ee83a897c008b0b3203","assignee":null,"milestone":{"url":"https://api.github.com/repos/druid-io/druid/milestones/20","html_url":"https://github.com/druid-io/druid/milestones/0.9.2","labels_url":"https://api.github.com/repos/druid-io/druid/milestones/20/labels","id":1571409,"number":20,"title":"0.9.2","description":"","creator":{"login":"fjy","id":428325,"avatar_url":"https://avatars.githubusercontent.com/u/428325?v=3","gravatar_id":"","url":"https://api.github.com/users/fjy","html_url":"https://github.com/fjy","followers_url":"https://api.github.com/users/fjy/followers","following_url":"https://api.github.com/users/fjy/following{/other_user}","gists_url":"https://api.github.com/users/fjy/gists{/gist_id}","starred_url":"https://api.github.com/users/fjy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjy/subscriptions","organizations_url":"https://api.github.com/users/fjy/orgs","repos_url":"https://api.github.com/users/fjy/repos","events_url":"https://api.github.com/users/fjy/events{/privacy}","received_events_url":"https://api.github.com/users/fjy/received_events","type":"User","site_admin":false},"open_issues":22,"closed_issues":3,"state":"open","created_at":"2016-02-08T22:01:21Z","updated_at":"2016-05-10T23:04:38Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/druid-io/druid/pulls/2819/commits","review_comments_url":"https://api.github.com/repos/druid-io/druid/pulls/2819/comments","review_comment_url":"https://api.github.com/repos/druid-io/druid/pulls/comments{/number}","comments_url":"https://api.github.com/repos/druid-io/druid/issues/2819/comments","statuses_url":"https://api.github.com/repos/druid-io/druid/statuses/76e520c8eb34e8ccbda4835adb615537e22208ce","head":{"label":"b-slim:new_lookup_module","ref":"new_lookup_module","sha":"76e520c8eb34e8ccbda4835adb615537e22208ce","user":{"login":"b-slim","id":7376396,"avatar_url":"https://avatars.githubusercontent.com/u/7376396?v=3","gravatar_id":"","url":"https://api.github.com/users/b-slim","html_url":"https://github.com/b-slim","followers_url":"https://api.github.com/users/b-slim/followers","following_url":"https://api.github.com/users/b-slim/following{/other_user}","gists_url":"https://api.github.com/users/b-slim/gists{/gist_id}","starred_url":"https://api.github.com/users/b-slim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/b-slim/subscriptions","organizations_url":"https://api.github.com/users/b-slim/orgs","repos_url":"https://api.github.com/users/b-slim/repos","events_url":"https://api.github.com/users/b-slim/events{/privacy}","received_events_url":"https://api.github.com/users/b-slim/received_events","type":"User","site_admin":false},"repo":{"id":26687338,"name":"druid","full_name":"b-slim/druid","owner":{"login":"b-slim","id":7376396,"avatar_url":"https://avatars.githubusercontent.com/u/7376396?v=3","gravatar_id":"","url":"https://api.github.com/users/b-slim","html_url":"https://github.com/b-slim","followers_url":"https://api.github.com/users/b-slim/followers","following_url":"https://api.github.com/users/b-slim/following{/other_user}","gists_url":"https://api.github.com/users/b-slim/gists{/gist_id}","starred_url":"https://api.github.com/users/b-slim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/b-slim/subscriptions","organizations_url":"https://api.github.com/users/b-slim/orgs","repos_url":"https://api.github.com/users/b-slim/repos","events_url":"https://api.github.com/users/b-slim/events{/privacy}","received_events_url":"https://api.github.com/users/b-slim/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/b-slim/druid","description":"Real²time Exploratory Analytics on Large Datasets","fork":true,"url":"https://api.github.com/repos/b-slim/druid","forks_url":"https://api.github.com/repos/b-slim/druid/forks","keys_url":"https://api.github.com/repos/b-slim/druid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/b-slim/druid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/b-slim/druid/teams","hooks_url":"https://api.github.com/repos/b-slim/druid/hooks","issue_events_url":"https://api.github.com/repos/b-slim/druid/issues/events{/number}","events_url":"https://api.github.com/repos/b-slim/druid/events","assignees_url":"https://api.github.com/repos/b-slim/druid/assignees{/user}","branches_url":"https://api.github.com/repos/b-slim/druid/branches{/branch}","tags_url":"https://api.github.com/repos/b-slim/druid/tags","blobs_url":"https://api.github.com/repos/b-slim/druid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/b-slim/druid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/b-slim/druid/git/refs{/sha}","trees_url":"https://api.github.com/repos/b-slim/druid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/b-slim/druid/statuses/{sha}","languages_url":"https://api.github.com/repos/b-slim/druid/languages","stargazers_url":"https://api.github.com/repos/b-slim/druid/stargazers","contributors_url":"https://api.github.com/repos/b-slim/druid/contributors","subscribers_url":"https://api.github.com/repos/b-slim/druid/subscribers","subscription_url":"https://api.github.com/repos/b-slim/druid/subscription","commits_url":"https://api.github.com/repos/b-slim/druid/commits{/sha}","git_commits_url":"https://api.github.com/repos/b-slim/druid/git/commits{/sha}","comments_url":"https://api.github.com/repos/b-slim/druid/comments{/number}","issue_comment_url":"https://api.github.com/repos/b-slim/druid/issues/comments{/number}","contents_url":"https://api.github.com/repos/b-slim/druid/contents/{+path}","compare_url":"https://api.github.com/repos/b-slim/druid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/b-slim/druid/merges","archive_url":"https://api.github.com/repos/b-slim/druid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/b-slim/druid/downloads","issues_url":"https://api.github.com/repos/b-slim/druid/issues{/number}","pulls_url":"https://api.github.com/repos/b-slim/druid/pulls{/number}","milestones_url":"https://api.github.com/repos/b-slim/druid/milestones{/number}","notifications_url":"https://api.github.com/repos/b-slim/druid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/b-slim/druid/labels{/name}","releases_url":"https://api.github.com/repos/b-slim/druid/releases{/id}","deployments_url":"https://api.github.com/repos/b-slim/druid/deployments","created_at":"2014-11-15T17:20:52Z","updated_at":"2016-02-09T13:51:50Z","pushed_at":"2016-05-11T23:42:36Z","git_url":"git://github.com/b-slim/druid.git","ssh_url":"git@github.com:b-slim/druid.git","clone_url":"https://github.com/b-slim/druid.git","svn_url":"https://github.com/b-slim/druid","homepage":"http://druid.io","size":106029,"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":"druid-io:master","ref":"master","sha":"81cab8a7bb86ce3e7d8d00f601db692ed01f7606","user":{"login":"druid-io","id":4203474,"avatar_url":"https://avatars.githubusercontent.com/u/4203474?v=3","gravatar_id":"","url":"https://api.github.com/users/druid-io","html_url":"https://github.com/druid-io","followers_url":"https://api.github.com/users/druid-io/followers","following_url":"https://api.github.com/users/druid-io/following{/other_user}","gists_url":"https://api.github.com/users/druid-io/gists{/gist_id}","starred_url":"https://api.github.com/users/druid-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/druid-io/subscriptions","organizations_url":"https://api.github.com/users/druid-io/orgs","repos_url":"https://api.github.com/users/druid-io/repos","events_url":"https://api.github.com/users/druid-io/events{/privacy}","received_events_url":"https://api.github.com/users/druid-io/received_events","type":"Organization","site_admin":false},"repo":{"id":6358188,"name":"druid","full_name":"druid-io/druid","owner":{"login":"druid-io","id":4203474,"avatar_url":"https://avatars.githubusercontent.com/u/4203474?v=3","gravatar_id":"","url":"https://api.github.com/users/druid-io","html_url":"https://github.com/druid-io","followers_url":"https://api.github.com/users/druid-io/followers","following_url":"https://api.github.com/users/druid-io/following{/other_user}","gists_url":"https://api.github.com/users/druid-io/gists{/gist_id}","starred_url":"https://api.github.com/users/druid-io/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/druid-io/subscriptions","organizations_url":"https://api.github.com/users/druid-io/orgs","repos_url":"https://api.github.com/users/druid-io/repos","events_url":"https://api.github.com/users/druid-io/events{/privacy}","received_events_url":"https://api.github.com/users/druid-io/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/druid-io/druid","description":"Column oriented distributed data store ideal for powering interactive applications","fork":false,"url":"https://api.github.com/repos/druid-io/druid","forks_url":"https://api.github.com/repos/druid-io/druid/forks","keys_url":"https://api.github.com/repos/druid-io/druid/keys{/key_id}","collaborators_url":"https://api.github.com/repos/druid-io/druid/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/druid-io/druid/teams","hooks_url":"https://api.github.com/repos/druid-io/druid/hooks","issue_events_url":"https://api.github.com/repos/druid-io/druid/issues/events{/number}","events_url":"https://api.github.com/repos/druid-io/druid/events","assignees_url":"https://api.github.com/repos/druid-io/druid/assignees{/user}","branches_url":"https://api.github.com/repos/druid-io/druid/branches{/branch}","tags_url":"https://api.github.com/repos/druid-io/druid/tags","blobs_url":"https://api.github.com/repos/druid-io/druid/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/druid-io/druid/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/druid-io/druid/git/refs{/sha}","trees_url":"https://api.github.com/repos/druid-io/druid/git/trees{/sha}","statuses_url":"https://api.github.com/repos/druid-io/druid/statuses/{sha}","languages_url":"https://api.github.com/repos/druid-io/druid/languages","stargazers_url":"https://api.github.com/repos/druid-io/druid/stargazers","contributors_url":"https://api.github.com/repos/druid-io/druid/contributors","subscribers_url":"https://api.github.com/repos/druid-io/druid/subscribers","subscription_url":"https://api.github.com/repos/druid-io/druid/subscription","commits_url":"https://api.github.com/repos/druid-io/druid/commits{/sha}","git_commits_url":"https://api.github.com/repos/druid-io/druid/git/commits{/sha}","comments_url":"https://api.github.com/repos/druid-io/druid/comments{/number}","issue_comment_url":"https://api.github.com/repos/druid-io/druid/issues/comments{/number}","contents_url":"https://api.github.com/repos/druid-io/druid/contents/{+path}","compare_url":"https://api.github.com/repos/druid-io/druid/compare/{base}...{head}","merges_url":"https://api.github.com/repos/druid-io/druid/merges","archive_url":"https://api.github.com/repos/druid-io/druid/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/druid-io/druid/downloads","issues_url":"https://api.github.com/repos/druid-io/druid/issues{/number}","pulls_url":"https://api.github.com/repos/druid-io/druid/pulls{/number}","milestones_url":"https://api.github.com/repos/druid-io/druid/milestones{/number}","notifications_url":"https://api.github.com/repos/druid-io/druid/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/druid-io/druid/labels{/name}","releases_url":"https://api.github.com/repos/druid-io/druid/releases{/id}","deployments_url":"https://api.github.com/repos/druid-io/druid/deployments","created_at":"2012-10-23T19:08:07Z","updated_at":"2016-05-11T21:47:57Z","pushed_at":"2016-05-11T23:42:37Z","git_url":"git://github.com/druid-io/druid.git","ssh_url":"git@github.com:druid-io/druid.git","clone_url":"https://github.com/druid-io/druid.git","svn_url":"https://github.com/druid-io/druid","homepage":"http://druid.io","size":129816,"stargazers_count":3207,"watchers_count":3207,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":706,"mirror_url":null,"open_issues_count":419,"forks":706,"open_issues":419,"watchers":3207,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2819"},"html":{"href":"https://github.com/druid-io/druid/pull/2819"},"issue":{"href":"https://api.github.com/repos/druid-io/druid/issues/2819"},"comments":{"href":"https://api.github.com/repos/druid-io/druid/issues/2819/comments"},"review_comments":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2819/comments"},"review_comment":{"href":"https://api.github.com/repos/druid-io/druid/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/druid-io/druid/pulls/2819/commits"},"statuses":{"href":"https://api.github.com/repos/druid-io/druid/statuses/76e520c8eb34e8ccbda4835adb615537e22208ce"}}}}