{"action":"created","comment":{"url":"https://api.github.com/repos/druid-io/tranquility/pulls/comments/47127296","id":47127296,"diff_hunk":"@@ -0,0 +1,302 @@\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+package com.metamx.tranquility.tranquilizer\n+\n+import com.metamx.common.lifecycle.LifecycleStart\n+import com.metamx.common.lifecycle.LifecycleStop\n+import com.metamx.common.scala.Logging\n+import com.metamx.common.scala.Predef._\n+import com.metamx.common.scala.concurrent.loggingThread\n+import com.metamx.tranquility.beam.Beam\n+import com.twitter.util.Await\n+import com.twitter.util.Future\n+import com.twitter.util.Promise\n+import com.twitter.util.Return\n+import com.twitter.util.Throw\n+import scala.collection.mutable.ArrayBuffer\n+import scala.collection.mutable.Buffer\n+\n+class Tranquilizer[MessageType] private(\n+  beam: Beam[MessageType],\n+  maxBatchSize: Int,\n+  maxPendingBatches: Int,\n+  lingerMillis: Long\n+) extends Logging\n+{\n+  require(maxBatchSize >= 1, \"batchSize >= 1\")\n+  require(maxPendingBatches >= 1, \"maxPendingBatches >= 1\")\n+  require(lingerMillis >= 0, \"lingerMillis >= 0\")\n+\n+  @volatile private var started: Boolean = false\n+\n+  case class MessageHolder(message: MessageType, future: Promise[Unit])\n+\n+  private val lock             : AnyRef                = new AnyRef\n+  private var buffer           : Buffer[MessageHolder] = new ArrayBuffer[MessageHolder]\n+  private var bufferStartMillis: Long                  = 0L\n+  private var pendingBatches   : Int                   = 0\n+\n+  private val sendThread = loggingThread {\n+    try {\n+      while (!Thread.currentThread().isInterrupted) {\n+        val exBuffer = lock.synchronized {\n+          while (\n+            buffer.isEmpty ||\n+              pendingBatches >= maxPendingBatches ||\n+              (lingerMillis > 0 && System.currentTimeMillis() < bufferStartMillis + lingerMillis)\n+          ) {\n+            if (lingerMillis == 0) {","path":"core/src/main/scala/com/metamx/tranquility/tranquilizer/Tranquilizer.scala","position":65,"original_position":65,"commit_id":"5e06f08ab49701ee33a36b180cda8f672aace398","original_commit_id":"5e06f08ab49701ee33a36b180cda8f672aace398","user":{"login":"xvrl","id":815147,"avatar_url":"https://avatars.githubusercontent.com/u/815147?v=3","gravatar_id":"","url":"https://api.github.com/users/xvrl","html_url":"https://github.com/xvrl","followers_url":"https://api.github.com/users/xvrl/followers","following_url":"https://api.github.com/users/xvrl/following{/other_user}","gists_url":"https://api.github.com/users/xvrl/gists{/gist_id}","starred_url":"https://api.github.com/users/xvrl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xvrl/subscriptions","organizations_url":"https://api.github.com/users/xvrl/orgs","repos_url":"https://api.github.com/users/xvrl/repos","events_url":"https://api.github.com/users/xvrl/events{/privacy}","received_events_url":"https://api.github.com/users/xvrl/received_events","type":"User","site_admin":false},"body":"minor optimization, you could add `|| bufferStartMillis == 0` here to avoid waking the thread every milliseconds on empty buffers.","created_at":"2015-12-09T18:02:48Z","updated_at":"2015-12-09T18:02:48Z","html_url":"https://github.com/druid-io/tranquility/pull/57#discussion_r47127296","pull_request_url":"https://api.github.com/repos/druid-io/tranquility/pulls/57","_links":{"self":{"href":"https://api.github.com/repos/druid-io/tranquility/pulls/comments/47127296"},"html":{"href":"https://github.com/druid-io/tranquility/pull/57#discussion_r47127296"},"pull_request":{"href":"https://api.github.com/repos/druid-io/tranquility/pulls/57"}}},"pull_request":{"url":"https://api.github.com/repos/druid-io/tranquility/pulls/57","id":52448758,"html_url":"https://github.com/druid-io/tranquility/pull/57","diff_url":"https://github.com/druid-io/tranquility/pull/57.diff","patch_url":"https://github.com/druid-io/tranquility/pull/57.patch","issue_url":"https://api.github.com/repos/druid-io/tranquility/issues/57","number":57,"state":"open","locked":false,"title":"Server and Tranquilizers.","user":{"login":"gianm","id":1214075,"avatar_url":"https://avatars.githubusercontent.com/u/1214075?v=3","gravatar_id":"","url":"https://api.github.com/users/gianm","html_url":"https://github.com/gianm","followers_url":"https://api.github.com/users/gianm/followers","following_url":"https://api.github.com/users/gianm/following{/other_user}","gists_url":"https://api.github.com/users/gianm/gists{/gist_id}","starred_url":"https://api.github.com/users/gianm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gianm/subscriptions","organizations_url":"https://api.github.com/users/gianm/orgs","repos_url":"https://api.github.com/users/gianm/repos","events_url":"https://api.github.com/users/gianm/events{/privacy}","received_events_url":"https://api.github.com/users/gianm/received_events","type":"User","site_admin":false},"body":"This PR has a bunch of related stuff in it as separate commits.\r\n\r\nTranquilizer is a probably-easier-to-use API than Beam. It does batching and pipelining on its own and exposes two APIs: a future-oriented API where you can get the status of each individual message, and a \"simple\" API where you get overall stats and overall exceptions but not associated with any individual message.\r\n\r\nBeamPacketizer is deprecated now as Tranquilizer is a replacement for it.\r\n\r\nTranquility Server is a server that you can send events to and have them forwarded along to Druid. It's a good way to use tranquility if you don't want to embed it or can't embed it (because you aren't using a JVM language).\r\n\r\nFixes #55","created_at":"2015-12-02T20:21:40Z","updated_at":"2015-12-09T18:02:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1ffcbc18cb893f34dc0c436b7a0d6c847b594c5c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/druid-io/tranquility/pulls/57/commits","review_comments_url":"https://api.github.com/repos/druid-io/tranquility/pulls/57/comments","review_comment_url":"https://api.github.com/repos/druid-io/tranquility/pulls/comments{/number}","comments_url":"https://api.github.com/repos/druid-io/tranquility/issues/57/comments","statuses_url":"https://api.github.com/repos/druid-io/tranquility/statuses/5e06f08ab49701ee33a36b180cda8f672aace398","head":{"label":"gianm:server","ref":"server","sha":"5e06f08ab49701ee33a36b180cda8f672aace398","user":{"login":"gianm","id":1214075,"avatar_url":"https://avatars.githubusercontent.com/u/1214075?v=3","gravatar_id":"","url":"https://api.github.com/users/gianm","html_url":"https://github.com/gianm","followers_url":"https://api.github.com/users/gianm/followers","following_url":"https://api.github.com/users/gianm/following{/other_user}","gists_url":"https://api.github.com/users/gianm/gists{/gist_id}","starred_url":"https://api.github.com/users/gianm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gianm/subscriptions","organizations_url":"https://api.github.com/users/gianm/orgs","repos_url":"https://api.github.com/users/gianm/repos","events_url":"https://api.github.com/users/gianm/events{/privacy}","received_events_url":"https://api.github.com/users/gianm/received_events","type":"User","site_admin":false},"repo":{"id":35352803,"name":"tranquility","full_name":"gianm/tranquility","owner":{"login":"gianm","id":1214075,"avatar_url":"https://avatars.githubusercontent.com/u/1214075?v=3","gravatar_id":"","url":"https://api.github.com/users/gianm","html_url":"https://github.com/gianm","followers_url":"https://api.github.com/users/gianm/followers","following_url":"https://api.github.com/users/gianm/following{/other_user}","gists_url":"https://api.github.com/users/gianm/gists{/gist_id}","starred_url":"https://api.github.com/users/gianm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gianm/subscriptions","organizations_url":"https://api.github.com/users/gianm/orgs","repos_url":"https://api.github.com/users/gianm/repos","events_url":"https://api.github.com/users/gianm/events{/privacy}","received_events_url":"https://api.github.com/users/gianm/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/gianm/tranquility","description":"Regenerates all nearby group members for 2569 to 2986 every 2 seconds for 10 sec.","fork":true,"url":"https://api.github.com/repos/gianm/tranquility","forks_url":"https://api.github.com/repos/gianm/tranquility/forks","keys_url":"https://api.github.com/repos/gianm/tranquility/keys{/key_id}","collaborators_url":"https://api.github.com/repos/gianm/tranquility/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/gianm/tranquility/teams","hooks_url":"https://api.github.com/repos/gianm/tranquility/hooks","issue_events_url":"https://api.github.com/repos/gianm/tranquility/issues/events{/number}","events_url":"https://api.github.com/repos/gianm/tranquility/events","assignees_url":"https://api.github.com/repos/gianm/tranquility/assignees{/user}","branches_url":"https://api.github.com/repos/gianm/tranquility/branches{/branch}","tags_url":"https://api.github.com/repos/gianm/tranquility/tags","blobs_url":"https://api.github.com/repos/gianm/tranquility/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/gianm/tranquility/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/gianm/tranquility/git/refs{/sha}","trees_url":"https://api.github.com/repos/gianm/tranquility/git/trees{/sha}","statuses_url":"https://api.github.com/repos/gianm/tranquility/statuses/{sha}","languages_url":"https://api.github.com/repos/gianm/tranquility/languages","stargazers_url":"https://api.github.com/repos/gianm/tranquility/stargazers","contributors_url":"https://api.github.com/repos/gianm/tranquility/contributors","subscribers_url":"https://api.github.com/repos/gianm/tranquility/subscribers","subscription_url":"https://api.github.com/repos/gianm/tranquility/subscription","commits_url":"https://api.github.com/repos/gianm/tranquility/commits{/sha}","git_commits_url":"https://api.github.com/repos/gianm/tranquility/git/commits{/sha}","comments_url":"https://api.github.com/repos/gianm/tranquility/comments{/number}","issue_comment_url":"https://api.github.com/repos/gianm/tranquility/issues/comments{/number}","contents_url":"https://api.github.com/repos/gianm/tranquility/contents/{+path}","compare_url":"https://api.github.com/repos/gianm/tranquility/compare/{base}...{head}","merges_url":"https://api.github.com/repos/gianm/tranquility/merges","archive_url":"https://api.github.com/repos/gianm/tranquility/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/gianm/tranquility/downloads","issues_url":"https://api.github.com/repos/gianm/tranquility/issues{/number}","pulls_url":"https://api.github.com/repos/gianm/tranquility/pulls{/number}","milestones_url":"https://api.github.com/repos/gianm/tranquility/milestones{/number}","notifications_url":"https://api.github.com/repos/gianm/tranquility/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/gianm/tranquility/labels{/name}","releases_url":"https://api.github.com/repos/gianm/tranquility/releases{/id}","created_at":"2015-05-10T01:47:31Z","updated_at":"2015-09-04T15:54:29Z","pushed_at":"2015-12-09T16:14:36Z","git_url":"git://github.com/gianm/tranquility.git","ssh_url":"git@github.com:gianm/tranquility.git","clone_url":"https://github.com/gianm/tranquility.git","svn_url":"https://github.com/gianm/tranquility","homepage":"","size":541,"stargazers_count":0,"watchers_count":0,"language":"Scala","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":"6f6005f608fc1eed765ab239c8bb22ea1f147bbe","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":16817857,"name":"tranquility","full_name":"druid-io/tranquility","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/tranquility","description":"Tranquility helps you send real-time event streams to Druid and handles partitioning, replication, service discovery, and schema rollover, seamlessly and without downtime.","fork":false,"url":"https://api.github.com/repos/druid-io/tranquility","forks_url":"https://api.github.com/repos/druid-io/tranquility/forks","keys_url":"https://api.github.com/repos/druid-io/tranquility/keys{/key_id}","collaborators_url":"https://api.github.com/repos/druid-io/tranquility/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/druid-io/tranquility/teams","hooks_url":"https://api.github.com/repos/druid-io/tranquility/hooks","issue_events_url":"https://api.github.com/repos/druid-io/tranquility/issues/events{/number}","events_url":"https://api.github.com/repos/druid-io/tranquility/events","assignees_url":"https://api.github.com/repos/druid-io/tranquility/assignees{/user}","branches_url":"https://api.github.com/repos/druid-io/tranquility/branches{/branch}","tags_url":"https://api.github.com/repos/druid-io/tranquility/tags","blobs_url":"https://api.github.com/repos/druid-io/tranquility/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/druid-io/tranquility/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/druid-io/tranquility/git/refs{/sha}","trees_url":"https://api.github.com/repos/druid-io/tranquility/git/trees{/sha}","statuses_url":"https://api.github.com/repos/druid-io/tranquility/statuses/{sha}","languages_url":"https://api.github.com/repos/druid-io/tranquility/languages","stargazers_url":"https://api.github.com/repos/druid-io/tranquility/stargazers","contributors_url":"https://api.github.com/repos/druid-io/tranquility/contributors","subscribers_url":"https://api.github.com/repos/druid-io/tranquility/subscribers","subscription_url":"https://api.github.com/repos/druid-io/tranquility/subscription","commits_url":"https://api.github.com/repos/druid-io/tranquility/commits{/sha}","git_commits_url":"https://api.github.com/repos/druid-io/tranquility/git/commits{/sha}","comments_url":"https://api.github.com/repos/druid-io/tranquility/comments{/number}","issue_comment_url":"https://api.github.com/repos/druid-io/tranquility/issues/comments{/number}","contents_url":"https://api.github.com/repos/druid-io/tranquility/contents/{+path}","compare_url":"https://api.github.com/repos/druid-io/tranquility/compare/{base}...{head}","merges_url":"https://api.github.com/repos/druid-io/tranquility/merges","archive_url":"https://api.github.com/repos/druid-io/tranquility/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/druid-io/tranquility/downloads","issues_url":"https://api.github.com/repos/druid-io/tranquility/issues{/number}","pulls_url":"https://api.github.com/repos/druid-io/tranquility/pulls{/number}","milestones_url":"https://api.github.com/repos/druid-io/tranquility/milestones{/number}","notifications_url":"https://api.github.com/repos/druid-io/tranquility/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/druid-io/tranquility/labels{/name}","releases_url":"https://api.github.com/repos/druid-io/tranquility/releases{/id}","created_at":"2014-02-13T21:30:05Z","updated_at":"2015-12-09T08:11:00Z","pushed_at":"2015-12-09T16:16:00Z","git_url":"git://github.com/druid-io/tranquility.git","ssh_url":"git@github.com:druid-io/tranquility.git","clone_url":"https://github.com/druid-io/tranquility.git","svn_url":"https://github.com/druid-io/tranquility","homepage":"","size":541,"stargazers_count":98,"watchers_count":98,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":36,"mirror_url":null,"open_issues_count":20,"forks":36,"open_issues":20,"watchers":98,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/druid-io/tranquility/pulls/57"},"html":{"href":"https://github.com/druid-io/tranquility/pull/57"},"issue":{"href":"https://api.github.com/repos/druid-io/tranquility/issues/57"},"comments":{"href":"https://api.github.com/repos/druid-io/tranquility/issues/57/comments"},"review_comments":{"href":"https://api.github.com/repos/druid-io/tranquility/pulls/57/comments"},"review_comment":{"href":"https://api.github.com/repos/druid-io/tranquility/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/druid-io/tranquility/pulls/57/commits"},"statuses":{"href":"https://api.github.com/repos/druid-io/tranquility/statuses/5e06f08ab49701ee33a36b180cda8f672aace398"}}}}