MB-20819: Use std::chrono::system_clock for HLC 38/68038/13
authorJim Walker <jim@couchbase.com>
Thu, 29 Sep 2016 14:56:24 +0000 (15:56 +0100)
committerDave Rigby <daver@couchbase.com>
Wed, 12 Oct 2016 07:42:46 +0000 (07:42 +0000)
Use a clock that follows the system clock for generation of HLC
time-stamps.

Change-Id: Ibb928c3ea4b83fbcf0862e8c610b1c49154eb0ed
Reviewed-on: http://review.couchbase.org/68038
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
Reviewed-by: Dave Rigby <daver@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
src/vbucket.cc

index 5719214..d66a8d5 100644 (file)
@@ -604,7 +604,14 @@ size_t VBucket::getNumOfKeysInFilter() {
 }
 
 uint64_t VBucket::nextHLCCas() {
-    hrtime_t now = gethrtime();
+    // Create a monotonic timestamp using part of the HLC algorithm by.
+    // a) Reading system time (now)
+    // b) dropping 16-bits (masking 65355 ┬Ás)
+    // c) comparing it with the last known time (max_cas)
+    // d) returning either now or max_cas + 1
+    auto now = std::chrono::duration_cast<std::chrono::microseconds>
+            (std::chrono::system_clock::now().time_since_epoch()).count();
+
     uint64_t now48bits = ((uint64_t)now) & ~((1 << 16) - 1);
     uint64_t local_max_cas = max_cas.load();