{"action":"created","comment":{"url":"https://api.github.com/repos/datastax/java-driver/pulls/comments/54954744","id":54954744,"diff_hunk":"@@ -15,25 +15,121 @@\n  */\n package com.datastax.driver.core;\n \n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import java.util.concurrent.atomic.AtomicReference;\n+\n+import static java.util.concurrent.TimeUnit.*;\n+\n /**\n- * This interface allows us not to have a direct call to {@code System.currentTimeMillis()} for testing purposes\n+ * A small abstraction around system clock that aims to provide microsecond precision with the best accuracy possible.\n  */\n interface Clock {\n+\n     /**\n-     * Returns the current time in milliseconds\n+     * Returns the current time in microseconds.\n      *\n-     * @return the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.\n-     * @see System#currentTimeMillis()\n+     * @return the difference, measured in microseconds, between the current time and and the Epoch\n+     * (that is, midnight, January 1, 1970 UTC).\n      */\n-    long currentTime();\n+    long currentTimeMicros();\n+}\n+\n+/**\n+ * Factory that returns the best Clock implementation depending on what native libraries are available in the system.\n+ * If LibC is available through JNR, and if the system property {@code com.datastax.driver.USE_NATIVE_CLOCK} is set to {@code true}\n+ * (which is the default value), then {@link NativeClock} is returned, otherwise {@link SystemClock} is returned.\n+ */\n+class ClockFactory {\n+\n+    private static final Logger LOGGER = LoggerFactory.getLogger(ClockFactory.class);\n+\n+    private static final String USE_NATIVE_CLOCK_SYSTEM_PROPERTY = \"com.datastax.driver.USE_NATIVE_CLOCK\";\n+\n+    static Clock newInstance() {\n+        if (Native.isLibCLoaded() && SystemProperties.getBoolean(USE_NATIVE_CLOCK_SYSTEM_PROPERTY, true)) {\n+            LOGGER.info(\"Using native clock to generate timestamps.\");\n+            return new NativeClock();\n+        } else {\n+            LOGGER.info(\"Using java.lang.System clock to generate timestamps.\");\n+            return new SystemClock();\n+        }\n+    }\n+\n }\n \n /**\n- * Default implementation of a clock that delegate its calls to the system clock.\n+ * Default implementation of a clock that delegates its calls to the system clock.\n+ *\n+ * @see System#currentTimeMillis()\n  */\n class SystemClock implements Clock {\n+\n     @Override\n-    public long currentTime() {\n-        return System.currentTimeMillis();\n+    public long currentTimeMicros() {\n+        return System.currentTimeMillis() * 1000;\n     }\n-}\n\\ No newline at end of file\n+\n+}\n+\n+/**\n+ * Provides the current time with microseconds precision with some reasonable accuracy through\n+ * the use of {@link Native#currentTimeMicros()}.\n+ * <p/>\n+ * Because calling JNR methods is slightly expensive,\n+ * we only call it once per second and add the number of nanoseconds since the last call\n+ * to get the current time, which is good enough an accuracy for our purpose (see CASSANDRA-6106).\n+ * <p/>\n+ * This reduces the cost of the call to {@link NativeClock#currentTimeMicros()} to levels comparable\n+ * to those of a call to {@link System#currentTimeMillis()}.\n+ */\n+class NativeClock implements Clock {\n+\n+    private static final long ONE_SECOND_NS = NANOSECONDS.convert(1, SECONDS);\n+    private static final long ONE_MILLISECOND_NS = NANOSECONDS.convert(1, MILLISECONDS);\n+\n+    /**\n+     * Records a time in micros along with the System.nanoTime() value at the time the\n+     * time is fetched.\n+     */\n+    private static class FetchedTime {\n+\n+        private final long timeInMicros;\n+        private final long nanoTimeAtCheck;\n+\n+        private FetchedTime(long timeInMicros, long nanoTimeAtCheck) {\n+            this.timeInMicros = timeInMicros;\n+            this.nanoTimeAtCheck = nanoTimeAtCheck;\n+        }\n+    }\n+\n+    private final AtomicReference<FetchedTime> lastFetchedTime = new AtomicReference<FetchedTime>(fetchTimeMicros());\n+\n+    @Override\n+    public long currentTimeMicros() {\n+        FetchedTime spec = lastFetchedTime.get();\n+        long curNano = System.nanoTime();\n+        if (curNano > spec.nanoTimeAtCheck + ONE_SECOND_NS) {\n+            spec = fetchTimeMicros();","path":"driver-core/src/main/java/com/datastax/driver/core/Clock.java","position":null,"original_position":110,"commit_id":"14f1076ff91003184f808468208521dbfabd7372","original_commit_id":"1b5a0bee7d7bc98fa6b684c9c1211245959bbdc1","user":{"login":"adutra","id":463876,"avatar_url":"https://avatars.githubusercontent.com/u/463876?v=3","gravatar_id":"","url":"https://api.github.com/users/adutra","html_url":"https://github.com/adutra","followers_url":"https://api.github.com/users/adutra/followers","following_url":"https://api.github.com/users/adutra/following{/other_user}","gists_url":"https://api.github.com/users/adutra/gists{/gist_id}","starred_url":"https://api.github.com/users/adutra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adutra/subscriptions","organizations_url":"https://api.github.com/users/adutra/orgs","repos_url":"https://api.github.com/users/adutra/repos","events_url":"https://api.github.com/users/adutra/events{/privacy}","received_events_url":"https://api.github.com/users/adutra/received_events","type":"User","site_admin":false},"body":"Went ahead and introduced a CAS like in my example.","created_at":"2016-03-03T21:53:21Z","updated_at":"2016-03-03T21:53:21Z","html_url":"https://github.com/datastax/java-driver/pull/591#discussion_r54954744","pull_request_url":"https://api.github.com/repos/datastax/java-driver/pulls/591","_links":{"self":{"href":"https://api.github.com/repos/datastax/java-driver/pulls/comments/54954744"},"html":{"href":"https://github.com/datastax/java-driver/pull/591#discussion_r54954744"},"pull_request":{"href":"https://api.github.com/repos/datastax/java-driver/pulls/591"}}},"pull_request":{"url":"https://api.github.com/repos/datastax/java-driver/pulls/591","id":59059462,"html_url":"https://github.com/datastax/java-driver/pull/591","diff_url":"https://github.com/datastax/java-driver/pull/591.diff","patch_url":"https://github.com/datastax/java-driver/pull/591.patch","issue_url":"https://api.github.com/repos/datastax/java-driver/issues/591","number":591,"state":"open","locked":false,"title":"JAVA-727: Monotonic Timestamp generators should not provide the same timestamp more than once..","user":{"login":"adutra","id":463876,"avatar_url":"https://avatars.githubusercontent.com/u/463876?v=3","gravatar_id":"","url":"https://api.github.com/users/adutra","html_url":"https://github.com/adutra","followers_url":"https://api.github.com/users/adutra/followers","following_url":"https://api.github.com/users/adutra/following{/other_user}","gists_url":"https://api.github.com/users/adutra/gists{/gist_id}","starred_url":"https://api.github.com/users/adutra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adutra/subscriptions","organizations_url":"https://api.github.com/users/adutra/orgs","repos_url":"https://api.github.com/users/adutra/repos","events_url":"https://api.github.com/users/adutra/events{/privacy}","received_events_url":"https://api.github.com/users/adutra/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-02-11T16:50:40Z","updated_at":"2016-03-03T21:53:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b6b3e087795201878d97a224536fba1e6af6c7d1","assignee":null,"milestone":{"url":"https://api.github.com/repos/datastax/java-driver/milestones/5","html_url":"https://github.com/datastax/java-driver/milestones/2.1.10","labels_url":"https://api.github.com/repos/datastax/java-driver/milestones/5/labels","id":1421423,"number":5,"title":"2.1.10","description":"","creator":{"login":"adutra","id":463876,"avatar_url":"https://avatars.githubusercontent.com/u/463876?v=3","gravatar_id":"","url":"https://api.github.com/users/adutra","html_url":"https://github.com/adutra","followers_url":"https://api.github.com/users/adutra/followers","following_url":"https://api.github.com/users/adutra/following{/other_user}","gists_url":"https://api.github.com/users/adutra/gists{/gist_id}","starred_url":"https://api.github.com/users/adutra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adutra/subscriptions","organizations_url":"https://api.github.com/users/adutra/orgs","repos_url":"https://api.github.com/users/adutra/repos","events_url":"https://api.github.com/users/adutra/events{/privacy}","received_events_url":"https://api.github.com/users/adutra/received_events","type":"User","site_admin":false},"open_issues":9,"closed_issues":27,"state":"open","created_at":"2015-11-20T09:09:17Z","updated_at":"2016-03-03T19:23:23Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/datastax/java-driver/pulls/591/commits","review_comments_url":"https://api.github.com/repos/datastax/java-driver/pulls/591/comments","review_comment_url":"https://api.github.com/repos/datastax/java-driver/pulls/comments{/number}","comments_url":"https://api.github.com/repos/datastax/java-driver/issues/591/comments","statuses_url":"https://api.github.com/repos/datastax/java-driver/statuses/14f1076ff91003184f808468208521dbfabd7372","head":{"label":"datastax:java727","ref":"java727","sha":"14f1076ff91003184f808468208521dbfabd7372","user":{"login":"datastax","id":573369,"avatar_url":"https://avatars.githubusercontent.com/u/573369?v=3","gravatar_id":"","url":"https://api.github.com/users/datastax","html_url":"https://github.com/datastax","followers_url":"https://api.github.com/users/datastax/followers","following_url":"https://api.github.com/users/datastax/following{/other_user}","gists_url":"https://api.github.com/users/datastax/gists{/gist_id}","starred_url":"https://api.github.com/users/datastax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/datastax/subscriptions","organizations_url":"https://api.github.com/users/datastax/orgs","repos_url":"https://api.github.com/users/datastax/repos","events_url":"https://api.github.com/users/datastax/events{/privacy}","received_events_url":"https://api.github.com/users/datastax/received_events","type":"Organization","site_admin":false},"repo":{"id":6765281,"name":"java-driver","full_name":"datastax/java-driver","owner":{"login":"datastax","id":573369,"avatar_url":"https://avatars.githubusercontent.com/u/573369?v=3","gravatar_id":"","url":"https://api.github.com/users/datastax","html_url":"https://github.com/datastax","followers_url":"https://api.github.com/users/datastax/followers","following_url":"https://api.github.com/users/datastax/following{/other_user}","gists_url":"https://api.github.com/users/datastax/gists{/gist_id}","starred_url":"https://api.github.com/users/datastax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/datastax/subscriptions","organizations_url":"https://api.github.com/users/datastax/orgs","repos_url":"https://api.github.com/users/datastax/repos","events_url":"https://api.github.com/users/datastax/events{/privacy}","received_events_url":"https://api.github.com/users/datastax/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/datastax/java-driver","description":"DataStax Java Driver for Apache Cassandra","fork":false,"url":"https://api.github.com/repos/datastax/java-driver","forks_url":"https://api.github.com/repos/datastax/java-driver/forks","keys_url":"https://api.github.com/repos/datastax/java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/datastax/java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/datastax/java-driver/teams","hooks_url":"https://api.github.com/repos/datastax/java-driver/hooks","issue_events_url":"https://api.github.com/repos/datastax/java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/datastax/java-driver/events","assignees_url":"https://api.github.com/repos/datastax/java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/datastax/java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/datastax/java-driver/tags","blobs_url":"https://api.github.com/repos/datastax/java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/datastax/java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/datastax/java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/datastax/java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/datastax/java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/datastax/java-driver/languages","stargazers_url":"https://api.github.com/repos/datastax/java-driver/stargazers","contributors_url":"https://api.github.com/repos/datastax/java-driver/contributors","subscribers_url":"https://api.github.com/repos/datastax/java-driver/subscribers","subscription_url":"https://api.github.com/repos/datastax/java-driver/subscription","commits_url":"https://api.github.com/repos/datastax/java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/datastax/java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/datastax/java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/datastax/java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/datastax/java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/datastax/java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/datastax/java-driver/merges","archive_url":"https://api.github.com/repos/datastax/java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/datastax/java-driver/downloads","issues_url":"https://api.github.com/repos/datastax/java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/datastax/java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/datastax/java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/datastax/java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/datastax/java-driver/labels{/name}","releases_url":"https://api.github.com/repos/datastax/java-driver/releases{/id}","deployments_url":"https://api.github.com/repos/datastax/java-driver/deployments","created_at":"2012-11-19T18:44:41Z","updated_at":"2016-03-03T13:58:17Z","pushed_at":"2016-03-03T21:52:04Z","git_url":"git://github.com/datastax/java-driver.git","ssh_url":"git@github.com:datastax/java-driver.git","clone_url":"https://github.com/datastax/java-driver.git","svn_url":"https://github.com/datastax/java-driver","homepage":null,"size":21555,"stargazers_count":588,"watchers_count":588,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":427,"mirror_url":null,"open_issues_count":36,"forks":427,"open_issues":36,"watchers":588,"default_branch":"3.0"}},"base":{"label":"datastax:2.1","ref":"2.1","sha":"dce2291ee35ab028515f05394e070b0872776cb6","user":{"login":"datastax","id":573369,"avatar_url":"https://avatars.githubusercontent.com/u/573369?v=3","gravatar_id":"","url":"https://api.github.com/users/datastax","html_url":"https://github.com/datastax","followers_url":"https://api.github.com/users/datastax/followers","following_url":"https://api.github.com/users/datastax/following{/other_user}","gists_url":"https://api.github.com/users/datastax/gists{/gist_id}","starred_url":"https://api.github.com/users/datastax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/datastax/subscriptions","organizations_url":"https://api.github.com/users/datastax/orgs","repos_url":"https://api.github.com/users/datastax/repos","events_url":"https://api.github.com/users/datastax/events{/privacy}","received_events_url":"https://api.github.com/users/datastax/received_events","type":"Organization","site_admin":false},"repo":{"id":6765281,"name":"java-driver","full_name":"datastax/java-driver","owner":{"login":"datastax","id":573369,"avatar_url":"https://avatars.githubusercontent.com/u/573369?v=3","gravatar_id":"","url":"https://api.github.com/users/datastax","html_url":"https://github.com/datastax","followers_url":"https://api.github.com/users/datastax/followers","following_url":"https://api.github.com/users/datastax/following{/other_user}","gists_url":"https://api.github.com/users/datastax/gists{/gist_id}","starred_url":"https://api.github.com/users/datastax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/datastax/subscriptions","organizations_url":"https://api.github.com/users/datastax/orgs","repos_url":"https://api.github.com/users/datastax/repos","events_url":"https://api.github.com/users/datastax/events{/privacy}","received_events_url":"https://api.github.com/users/datastax/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/datastax/java-driver","description":"DataStax Java Driver for Apache Cassandra","fork":false,"url":"https://api.github.com/repos/datastax/java-driver","forks_url":"https://api.github.com/repos/datastax/java-driver/forks","keys_url":"https://api.github.com/repos/datastax/java-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/datastax/java-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/datastax/java-driver/teams","hooks_url":"https://api.github.com/repos/datastax/java-driver/hooks","issue_events_url":"https://api.github.com/repos/datastax/java-driver/issues/events{/number}","events_url":"https://api.github.com/repos/datastax/java-driver/events","assignees_url":"https://api.github.com/repos/datastax/java-driver/assignees{/user}","branches_url":"https://api.github.com/repos/datastax/java-driver/branches{/branch}","tags_url":"https://api.github.com/repos/datastax/java-driver/tags","blobs_url":"https://api.github.com/repos/datastax/java-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/datastax/java-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/datastax/java-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/datastax/java-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/datastax/java-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/datastax/java-driver/languages","stargazers_url":"https://api.github.com/repos/datastax/java-driver/stargazers","contributors_url":"https://api.github.com/repos/datastax/java-driver/contributors","subscribers_url":"https://api.github.com/repos/datastax/java-driver/subscribers","subscription_url":"https://api.github.com/repos/datastax/java-driver/subscription","commits_url":"https://api.github.com/repos/datastax/java-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/datastax/java-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/datastax/java-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/datastax/java-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/datastax/java-driver/contents/{+path}","compare_url":"https://api.github.com/repos/datastax/java-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/datastax/java-driver/merges","archive_url":"https://api.github.com/repos/datastax/java-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/datastax/java-driver/downloads","issues_url":"https://api.github.com/repos/datastax/java-driver/issues{/number}","pulls_url":"https://api.github.com/repos/datastax/java-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/datastax/java-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/datastax/java-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/datastax/java-driver/labels{/name}","releases_url":"https://api.github.com/repos/datastax/java-driver/releases{/id}","deployments_url":"https://api.github.com/repos/datastax/java-driver/deployments","created_at":"2012-11-19T18:44:41Z","updated_at":"2016-03-03T13:58:17Z","pushed_at":"2016-03-03T21:52:04Z","git_url":"git://github.com/datastax/java-driver.git","ssh_url":"git@github.com:datastax/java-driver.git","clone_url":"https://github.com/datastax/java-driver.git","svn_url":"https://github.com/datastax/java-driver","homepage":null,"size":21555,"stargazers_count":588,"watchers_count":588,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":427,"mirror_url":null,"open_issues_count":36,"forks":427,"open_issues":36,"watchers":588,"default_branch":"3.0"}},"_links":{"self":{"href":"https://api.github.com/repos/datastax/java-driver/pulls/591"},"html":{"href":"https://github.com/datastax/java-driver/pull/591"},"issue":{"href":"https://api.github.com/repos/datastax/java-driver/issues/591"},"comments":{"href":"https://api.github.com/repos/datastax/java-driver/issues/591/comments"},"review_comments":{"href":"https://api.github.com/repos/datastax/java-driver/pulls/591/comments"},"review_comment":{"href":"https://api.github.com/repos/datastax/java-driver/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/datastax/java-driver/pulls/591/commits"},"statuses":{"href":"https://api.github.com/repos/datastax/java-driver/statuses/14f1076ff91003184f808468208521dbfabd7372"}}}}