{"action":"created","comment":{"url":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/comments/28934861","id":28934861,"diff_hunk":"@@ -0,0 +1,121 @@\n+package net.opentsdb.examples;\n+\n+import java.util.HashMap;\n+import java.util.Map;\n+\n+import com.stumbleupon.async.Callback;\n+import com.stumbleupon.async.Deferred;\n+\n+import net.opentsdb.core.TSDB;\n+import net.opentsdb.core.WritableDataPoints;\n+\n+\n+/**\n+ * A class to manage writing multiple data points to one or more timeseries.  This is specifically for the case when\n+ * we may try to write two data to the same timeseries at the same time (in ms).  Rather than writing over the first\n+ * point though, what we'd like is to \"bump\" the second point later (in time) by 1 ms.  That way, it will be as close\n+ * as possible to its correct time, but we won't lose the point that was written first.\n+ * More explicitly, if we recieve two data, d1, and d2 both at time=t1.  Then d1 will be written to t1 and d2 will be \n+ * written to t1+1, where t1 is given in milliseconds.  \n+ *\n+ */\n+public class TimeSeriesManager {\n+\n+\tprivate final TSDB tsdb;\n+\tprivate final boolean batchImport;\n+\tprivate final short bufferingTime;\n+\tprivate HashMap<String, WritableDataPoints> wdpPool;\n+\t\n+\n+\t/**\n+\t * \n+\t * @param tsdb TSDB object\n+\t * @param batchImport Whether to set batchImport on our instantiation of WritableDataPoints \n+\t * @param bufferingTime What buffering time to use if batchImport is true.\n+\t */\n+\tpublic TimeSeriesManager(TSDB tsdb, boolean batchImport, short bufferingTime) {\n+\t\tthis.tsdb = tsdb;\n+\t\tthis.batchImport = batchImport;\n+\t\tthis.bufferingTime = bufferingTime;\n+\t\tthis.wdpPool = new HashMap<String, WritableDataPoints>();\n+\t}\n+\n+\t/**\n+\t * Add a point to the input metric for the input tags.  Metric + tags define the timeseries.\n+\t * Internally, this will first check to see if we already have an instance of WritableDataPoints for the desired\n+\t * timeseries.  If so it will reuse it, otherwise it will create a new one and cache it for later use.  \n+\t * @param metricName\n+\t * @param tags\n+\t * @param timestamp\n+\t * @param value\n+\t * @return\n+\t * @throws InterruptedException\n+\t * @throws Exception\n+\t */\n+\tpublic Deferred<Object> addPoint(String metricName, Map<String, String> tags, long timestamp, long value) {\n+\t\tString id = makeId(metricName, tags);\n+\t\tWritableDataPoints wdp;\n+\t\tif (wdpPool.containsKey(id) ) {\n+\t\t\twdp = wdpPool.get(id);\n+\t\t} else {\n+\t\t\twdp = tsdb.newDataPoints();\n+\t\t\twdp.setBatchImport(batchImport);\n+\t\t\twdp.setBufferingTime(bufferingTime);\n+\t\t\twdp.setSeries(metricName, tags);\n+\t\t\twdpPool.put(id, wdp);\n+\t\t}\n+\t\t\n+\t\treturn retryAddPointUntilSuccess(wdp, timestamp, value);\n+\t}\n+\n+\t/**\n+\t * Will try to write the point at the input timestamp.  However, if an IllegalArgumentException is thrown, as will\n+\t * be the case when there already was a data point written to that timeseries at that specific time, this will add\n+\t * 1 to the time and try again until it succeeds. \n+\t * @param wdp\n+\t * @param timestamp\n+\t * @param value\n+\t * @return\n+\t * @throws InterruptedException\n+\t * @throws Exception\n+\t */\n+\tprivate Deferred<Object> retryAddPointUntilSuccess(final WritableDataPoints wdp, final long timestamp, \n+\t\t\tfinal long value) {\n+\t\tDeferred<Object> deferred = null;\n+\t\t\n+\t\tboolean accepted = false;\n+\t\tfor (long t = timestamp; !accepted ; t++) {\n+\t\t\ttry {\n+\t\t\t\tdeferred = wdp.addPoint(t, value);\n+\t\t\t\t\n+\t\t\t\tdeferred.addErrback(new RetryErrBack(wdp, timestamp, value, 10, 0));\n+\t\t\t\t\n+\t\t\t\tdeferred.addCallback(new Callback<Object, Object>() {\n+\t\t\t\t\t\tpublic Object call(Object results) {\n+\t\t\t\t\t\t\tif (results == null) {\n+\t\t\t\t\t\t\t\t// log success to debug\n+\t\t\t\t\t\t\t} else {\n+\t\t\t\t\t\t\t\tif (results.toString().equals(\"MultiActionSuccess\")) {\n+\t\t\t\t\t\t\t\t\t// log to debug\n+\t\t\t\t\t\t\t\t} else {\n+\t\t\t\t\t\t\t\t\t// log to error\n+\t\t\t\t\t\t\t\t\tSystem.err.println(\">>>>>>>>>>>\" + results.getClass() + \">>>>>>>>>>>\");","path":"src/examples/TimeSeriesManager.java","position":102,"original_position":102,"commit_id":"11149cde6240e9a9497b6b037c9f8fd8fa60fdbe","original_commit_id":"11149cde6240e9a9497b6b037c9f8fd8fa60fdbe","user":{"login":"manolama","id":1377700,"avatar_url":"https://avatars.githubusercontent.com/u/1377700?v=3","gravatar_id":"","url":"https://api.github.com/users/manolama","html_url":"https://github.com/manolama","followers_url":"https://api.github.com/users/manolama/followers","following_url":"https://api.github.com/users/manolama/following{/other_user}","gists_url":"https://api.github.com/users/manolama/gists{/gist_id}","starred_url":"https://api.github.com/users/manolama/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/manolama/subscriptions","organizations_url":"https://api.github.com/users/manolama/orgs","repos_url":"https://api.github.com/users/manolama/repos","events_url":"https://api.github.com/users/manolama/events{/privacy}","received_events_url":"https://api.github.com/users/manolama/received_events","type":"User","site_admin":false},"body":"Holy Tabs Batman! 2 space indent please per TSD :)","created_at":"2015-04-23T03:44:05Z","updated_at":"2015-04-23T03:44:05Z","html_url":"https://github.com/OpenTSDB/opentsdb/pull/464#discussion_r28934861","pull_request_url":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464","_links":{"self":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/comments/28934861"},"html":{"href":"https://github.com/OpenTSDB/opentsdb/pull/464#discussion_r28934861"},"pull_request":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464"}}},"pull_request":{"url":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464","id":31192756,"html_url":"https://github.com/OpenTSDB/opentsdb/pull/464","diff_url":"https://github.com/OpenTSDB/opentsdb/pull/464.diff","patch_url":"https://github.com/OpenTSDB/opentsdb/pull/464.patch","issue_url":"https://api.github.com/repos/OpenTSDB/opentsdb/issues/464","number":464,"state":"open","locked":false,"title":"Added examples for retrying to write a datapoint in the errback","user":{"login":"bryan4887","id":3506461,"avatar_url":"https://avatars.githubusercontent.com/u/3506461?v=3","gravatar_id":"","url":"https://api.github.com/users/bryan4887","html_url":"https://github.com/bryan4887","followers_url":"https://api.github.com/users/bryan4887/followers","following_url":"https://api.github.com/users/bryan4887/following{/other_user}","gists_url":"https://api.github.com/users/bryan4887/gists{/gist_id}","starred_url":"https://api.github.com/users/bryan4887/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bryan4887/subscriptions","organizations_url":"https://api.github.com/users/bryan4887/orgs","repos_url":"https://api.github.com/users/bryan4887/repos","events_url":"https://api.github.com/users/bryan4887/events{/privacy}","received_events_url":"https://api.github.com/users/bryan4887/received_events","type":"User","site_admin":false},"body":"- Main method in RetryAddPointExample.java\r\n- The purpose of this example is to show how to retry writing a data\r\npoint when an exception is returned in its Deferred result.\r\n\r\nMore context explained in: https://groups.google.com/forum/#!topic/opentsdb/_zjTW0DxlAg","created_at":"2015-03-15T00:17:53Z","updated_at":"2015-04-23T03:44:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8d769ed3d09b669d3ce49a0f82762fc67d13be43","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464/commits","review_comments_url":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464/comments","review_comment_url":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/comments{/number}","comments_url":"https://api.github.com/repos/OpenTSDB/opentsdb/issues/464/comments","statuses_url":"https://api.github.com/repos/OpenTSDB/opentsdb/statuses/11149cde6240e9a9497b6b037c9f8fd8fa60fdbe","head":{"label":"bryan4887:retryAddPointExample","ref":"retryAddPointExample","sha":"11149cde6240e9a9497b6b037c9f8fd8fa60fdbe","user":{"login":"bryan4887","id":3506461,"avatar_url":"https://avatars.githubusercontent.com/u/3506461?v=3","gravatar_id":"","url":"https://api.github.com/users/bryan4887","html_url":"https://github.com/bryan4887","followers_url":"https://api.github.com/users/bryan4887/followers","following_url":"https://api.github.com/users/bryan4887/following{/other_user}","gists_url":"https://api.github.com/users/bryan4887/gists{/gist_id}","starred_url":"https://api.github.com/users/bryan4887/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bryan4887/subscriptions","organizations_url":"https://api.github.com/users/bryan4887/orgs","repos_url":"https://api.github.com/users/bryan4887/repos","events_url":"https://api.github.com/users/bryan4887/events{/privacy}","received_events_url":"https://api.github.com/users/bryan4887/received_events","type":"User","site_admin":false},"repo":{"id":27757989,"name":"opentsdb","full_name":"bryan4887/opentsdb","owner":{"login":"bryan4887","id":3506461,"avatar_url":"https://avatars.githubusercontent.com/u/3506461?v=3","gravatar_id":"","url":"https://api.github.com/users/bryan4887","html_url":"https://github.com/bryan4887","followers_url":"https://api.github.com/users/bryan4887/followers","following_url":"https://api.github.com/users/bryan4887/following{/other_user}","gists_url":"https://api.github.com/users/bryan4887/gists{/gist_id}","starred_url":"https://api.github.com/users/bryan4887/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bryan4887/subscriptions","organizations_url":"https://api.github.com/users/bryan4887/orgs","repos_url":"https://api.github.com/users/bryan4887/repos","events_url":"https://api.github.com/users/bryan4887/events{/privacy}","received_events_url":"https://api.github.com/users/bryan4887/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bryan4887/opentsdb","description":"A scalable, distributed Time Series Database.","fork":true,"url":"https://api.github.com/repos/bryan4887/opentsdb","forks_url":"https://api.github.com/repos/bryan4887/opentsdb/forks","keys_url":"https://api.github.com/repos/bryan4887/opentsdb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bryan4887/opentsdb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bryan4887/opentsdb/teams","hooks_url":"https://api.github.com/repos/bryan4887/opentsdb/hooks","issue_events_url":"https://api.github.com/repos/bryan4887/opentsdb/issues/events{/number}","events_url":"https://api.github.com/repos/bryan4887/opentsdb/events","assignees_url":"https://api.github.com/repos/bryan4887/opentsdb/assignees{/user}","branches_url":"https://api.github.com/repos/bryan4887/opentsdb/branches{/branch}","tags_url":"https://api.github.com/repos/bryan4887/opentsdb/tags","blobs_url":"https://api.github.com/repos/bryan4887/opentsdb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bryan4887/opentsdb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bryan4887/opentsdb/git/refs{/sha}","trees_url":"https://api.github.com/repos/bryan4887/opentsdb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bryan4887/opentsdb/statuses/{sha}","languages_url":"https://api.github.com/repos/bryan4887/opentsdb/languages","stargazers_url":"https://api.github.com/repos/bryan4887/opentsdb/stargazers","contributors_url":"https://api.github.com/repos/bryan4887/opentsdb/contributors","subscribers_url":"https://api.github.com/repos/bryan4887/opentsdb/subscribers","subscription_url":"https://api.github.com/repos/bryan4887/opentsdb/subscription","commits_url":"https://api.github.com/repos/bryan4887/opentsdb/commits{/sha}","git_commits_url":"https://api.github.com/repos/bryan4887/opentsdb/git/commits{/sha}","comments_url":"https://api.github.com/repos/bryan4887/opentsdb/comments{/number}","issue_comment_url":"https://api.github.com/repos/bryan4887/opentsdb/issues/comments{/number}","contents_url":"https://api.github.com/repos/bryan4887/opentsdb/contents/{+path}","compare_url":"https://api.github.com/repos/bryan4887/opentsdb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bryan4887/opentsdb/merges","archive_url":"https://api.github.com/repos/bryan4887/opentsdb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bryan4887/opentsdb/downloads","issues_url":"https://api.github.com/repos/bryan4887/opentsdb/issues{/number}","pulls_url":"https://api.github.com/repos/bryan4887/opentsdb/pulls{/number}","milestones_url":"https://api.github.com/repos/bryan4887/opentsdb/milestones{/number}","notifications_url":"https://api.github.com/repos/bryan4887/opentsdb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bryan4887/opentsdb/labels{/name}","releases_url":"https://api.github.com/repos/bryan4887/opentsdb/releases{/id}","created_at":"2014-12-09T08:58:34Z","updated_at":"2015-02-23T05:24:08Z","pushed_at":"2015-03-23T01:21:13Z","git_url":"git://github.com/bryan4887/opentsdb.git","ssh_url":"git@github.com:bryan4887/opentsdb.git","clone_url":"https://github.com/bryan4887/opentsdb.git","svn_url":"https://github.com/bryan4887/opentsdb","homepage":"http://opentsdb.net","size":27905,"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":"OpenTSDB:master","ref":"master","sha":"791c9e304c95595b3a324ee56808d33402200dc8","user":{"login":"OpenTSDB","id":2086220,"avatar_url":"https://avatars.githubusercontent.com/u/2086220?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenTSDB","html_url":"https://github.com/OpenTSDB","followers_url":"https://api.github.com/users/OpenTSDB/followers","following_url":"https://api.github.com/users/OpenTSDB/following{/other_user}","gists_url":"https://api.github.com/users/OpenTSDB/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenTSDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenTSDB/subscriptions","organizations_url":"https://api.github.com/users/OpenTSDB/orgs","repos_url":"https://api.github.com/users/OpenTSDB/repos","events_url":"https://api.github.com/users/OpenTSDB/events{/privacy}","received_events_url":"https://api.github.com/users/OpenTSDB/received_events","type":"Organization","site_admin":false},"repo":{"id":865412,"name":"opentsdb","full_name":"OpenTSDB/opentsdb","owner":{"login":"OpenTSDB","id":2086220,"avatar_url":"https://avatars.githubusercontent.com/u/2086220?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenTSDB","html_url":"https://github.com/OpenTSDB","followers_url":"https://api.github.com/users/OpenTSDB/followers","following_url":"https://api.github.com/users/OpenTSDB/following{/other_user}","gists_url":"https://api.github.com/users/OpenTSDB/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenTSDB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenTSDB/subscriptions","organizations_url":"https://api.github.com/users/OpenTSDB/orgs","repos_url":"https://api.github.com/users/OpenTSDB/repos","events_url":"https://api.github.com/users/OpenTSDB/events{/privacy}","received_events_url":"https://api.github.com/users/OpenTSDB/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/OpenTSDB/opentsdb","description":"A scalable, distributed Time Series Database.","fork":false,"url":"https://api.github.com/repos/OpenTSDB/opentsdb","forks_url":"https://api.github.com/repos/OpenTSDB/opentsdb/forks","keys_url":"https://api.github.com/repos/OpenTSDB/opentsdb/keys{/key_id}","collaborators_url":"https://api.github.com/repos/OpenTSDB/opentsdb/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/OpenTSDB/opentsdb/teams","hooks_url":"https://api.github.com/repos/OpenTSDB/opentsdb/hooks","issue_events_url":"https://api.github.com/repos/OpenTSDB/opentsdb/issues/events{/number}","events_url":"https://api.github.com/repos/OpenTSDB/opentsdb/events","assignees_url":"https://api.github.com/repos/OpenTSDB/opentsdb/assignees{/user}","branches_url":"https://api.github.com/repos/OpenTSDB/opentsdb/branches{/branch}","tags_url":"https://api.github.com/repos/OpenTSDB/opentsdb/tags","blobs_url":"https://api.github.com/repos/OpenTSDB/opentsdb/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/OpenTSDB/opentsdb/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/OpenTSDB/opentsdb/git/refs{/sha}","trees_url":"https://api.github.com/repos/OpenTSDB/opentsdb/git/trees{/sha}","statuses_url":"https://api.github.com/repos/OpenTSDB/opentsdb/statuses/{sha}","languages_url":"https://api.github.com/repos/OpenTSDB/opentsdb/languages","stargazers_url":"https://api.github.com/repos/OpenTSDB/opentsdb/stargazers","contributors_url":"https://api.github.com/repos/OpenTSDB/opentsdb/contributors","subscribers_url":"https://api.github.com/repos/OpenTSDB/opentsdb/subscribers","subscription_url":"https://api.github.com/repos/OpenTSDB/opentsdb/subscription","commits_url":"https://api.github.com/repos/OpenTSDB/opentsdb/commits{/sha}","git_commits_url":"https://api.github.com/repos/OpenTSDB/opentsdb/git/commits{/sha}","comments_url":"https://api.github.com/repos/OpenTSDB/opentsdb/comments{/number}","issue_comment_url":"https://api.github.com/repos/OpenTSDB/opentsdb/issues/comments{/number}","contents_url":"https://api.github.com/repos/OpenTSDB/opentsdb/contents/{+path}","compare_url":"https://api.github.com/repos/OpenTSDB/opentsdb/compare/{base}...{head}","merges_url":"https://api.github.com/repos/OpenTSDB/opentsdb/merges","archive_url":"https://api.github.com/repos/OpenTSDB/opentsdb/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/OpenTSDB/opentsdb/downloads","issues_url":"https://api.github.com/repos/OpenTSDB/opentsdb/issues{/number}","pulls_url":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls{/number}","milestones_url":"https://api.github.com/repos/OpenTSDB/opentsdb/milestones{/number}","notifications_url":"https://api.github.com/repos/OpenTSDB/opentsdb/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/OpenTSDB/opentsdb/labels{/name}","releases_url":"https://api.github.com/repos/OpenTSDB/opentsdb/releases{/id}","created_at":"2010-08-27T02:05:20Z","updated_at":"2015-04-23T02:18:14Z","pushed_at":"2015-04-23T03:11:46Z","git_url":"git://github.com/OpenTSDB/opentsdb.git","ssh_url":"git@github.com:OpenTSDB/opentsdb.git","clone_url":"https://github.com/OpenTSDB/opentsdb.git","svn_url":"https://github.com/OpenTSDB/opentsdb","homepage":"http://opentsdb.net","size":34411,"stargazers_count":1249,"watchers_count":1249,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":456,"mirror_url":null,"open_issues_count":160,"forks":456,"open_issues":160,"watchers":1249,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464"},"html":{"href":"https://github.com/OpenTSDB/opentsdb/pull/464"},"issue":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/issues/464"},"comments":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/issues/464/comments"},"review_comments":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464/comments"},"review_comment":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/pulls/464/commits"},"statuses":{"href":"https://api.github.com/repos/OpenTSDB/opentsdb/statuses/11149cde6240e9a9497b6b037c9f8fd8fa60fdbe"}}}}