kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ale...@apache.org
Subject [kudu] 02/02: [monotime] deprecating MonoTime::GetDeltaSince()
Date Thu, 19 Dec 2019 23:09:59 GMT
This is an automated email from the ASF dual-hosted git repository.

alexey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 643bf32bb82e6f08c02cae94d0dab5cac9d61216
Author: Alexey Serbin <alexey@apache.org>
AuthorDate: Wed Dec 18 00:25:35 2019 -0800

    [monotime] deprecating MonoTime::GetDeltaSince()
    
    I find that
    
       MonoTime t0 = ...;
       MonoTime t1 = ...;
       MonoDelta delta = t1 - t0;
    
    is easier to write, read, and understand than
    
       MonoTime t0 = ...;
       MonoTime t1 = ...;
       MonoDelta delta = t1.GetDeltaSince(t0);
    
    So, I replaced usage of the MonoTime::GetDeltaSince() with
    the operator and marked the former method as deprecated.
    
    This patch doesn't contain any functional modifications.
    
    Change-Id: I9611c8a20e02b117757842bc3b46b8faf82d9700
    Reviewed-on: http://gerrit.cloudera.org:8080/14932
    Reviewed-by: Adar Dembo <adar@cloudera.com>
    Reviewed-by: Yingchun Lai <405403881@qq.com>
    Tested-by: Alexey Serbin <aserbin@cloudera.com>
---
 src/kudu/rpc/result_tracker.cc               |  3 +--
 src/kudu/rpc/rpc-test-base.h                 |  2 +-
 src/kudu/tserver/tablet_copy_service-test.cc |  2 +-
 src/kudu/tserver/tablet_service.cc           |  4 +---
 src/kudu/util/monotime-test.cc               | 14 +++++---------
 src/kudu/util/monotime.cc                    | 12 ++++++------
 src/kudu/util/monotime.h                     | 12 +++++++++---
 7 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/src/kudu/rpc/result_tracker.cc b/src/kudu/rpc/result_tracker.cc
index d26ff87..f8f5c46 100644
--- a/src/kudu/rpc/result_tracker.cc
+++ b/src/kudu/rpc/result_tracker.cc
@@ -556,8 +556,7 @@ void ResultTracker::ClientState::GCCompletionRecords(
 }
 
 string ResultTracker::ClientState::ToString() const {
-  auto since_last_heard =
-      MonoTime::Now().GetDeltaSince(last_heard_from);
+  auto since_last_heard = MonoTime::Now() - last_heard_from;
   string result = Substitute("Client State[Last heard from: $0s ago, "
                              "$1 CompletionRecords:",
                              since_last_heard.ToString(),
diff --git a/src/kudu/rpc/rpc-test-base.h b/src/kudu/rpc/rpc-test-base.h
index 2fb742e..e26bc9d 100644
--- a/src/kudu/rpc/rpc-test-base.h
+++ b/src/kudu/rpc/rpc-test-base.h
@@ -212,7 +212,7 @@ class GenericCalculatorService : public ServiceIf {
 
     LOG(INFO) << "got call: " << pb_util::SecureShortDebugString(req);
     SleepFor(MonoDelta::FromMicroseconds(req.sleep_micros()));
-    MonoDelta duration(MonoTime::Now().GetDeltaSince(incoming->GetTimeReceived()));
+    MonoDelta duration(MonoTime::Now() - incoming->GetTimeReceived());
     CHECK_GE(duration.ToMicroseconds(), req.sleep_micros());
     SleepResponsePB resp;
     incoming->RespondSuccess(resp);
diff --git a/src/kudu/tserver/tablet_copy_service-test.cc b/src/kudu/tserver/tablet_copy_service-test.cc
index 1dc0db6..25f076c 100644
--- a/src/kudu/tserver/tablet_copy_service-test.cc
+++ b/src/kudu/tserver/tablet_copy_service-test.cc
@@ -510,7 +510,7 @@ TEST_F(TabletCopyServiceTest, TestSessionTimeout) {
       break;
     }
     SleepFor(MonoDelta::FromMilliseconds(1)); // 1 ms
-  } while (MonoTime::Now().GetDeltaSince(start_time).ToSeconds() < 10);
+  } while ((MonoTime::Now() - start_time).ToSeconds() < 10);
 
   ASSERT_FALSE(resp.session_is_active()) << "Tablet Copy session did not time out!";
 }
diff --git a/src/kudu/tserver/tablet_service.cc b/src/kudu/tserver/tablet_service.cc
index 2839d42..97a4cdd 100644
--- a/src/kudu/tserver/tablet_service.cc
+++ b/src/kudu/tserver/tablet_service.cc
@@ -31,7 +31,6 @@
 
 #include <boost/optional/optional.hpp>
 #include <gflags/gflags.h>
-#include <gflags/gflags_declare.h>
 #include <glog/logging.h>
 
 #include "kudu/clock/clock.h"
@@ -91,7 +90,6 @@
 #include "kudu/tserver/tserver_admin.pb.h"
 #include "kudu/tserver/tserver_service.pb.h"
 #include "kudu/util/auto_release_pool.h"
-#include "kudu/util/bitset.h"
 #include "kudu/util/crc.h"
 #include "kudu/util/debug/trace_event.h"
 #include "kudu/util/faststring.h"
@@ -2713,7 +2711,7 @@ namespace {
 // Helper to clamp a client deadline for a scan to the max supported by the server.
 MonoTime ClampScanDeadlineForWait(const MonoTime& deadline, bool* was_clamped) {
   MonoTime now = MonoTime::Now();
-  if (deadline.GetDeltaSince(now).ToMilliseconds() > FLAGS_scanner_max_wait_ms) {
+  if ((deadline - now).ToMilliseconds() > FLAGS_scanner_max_wait_ms) {
     *was_clamped = true;
     return now + MonoDelta::FromMilliseconds(FLAGS_scanner_max_wait_ms);
   }
diff --git a/src/kudu/util/monotime-test.cc b/src/kudu/util/monotime-test.cc
index 755dac5..0fba667 100644
--- a/src/kudu/util/monotime-test.cc
+++ b/src/kudu/util/monotime-test.cc
@@ -106,7 +106,7 @@ TEST(TestMonoTime, TestTimeSpec) {
   ts.tv_sec = 1;
   ts.tv_nsec = 1;
   MonoTime one_sec_one_nano_actual(ts);
-  ASSERT_EQ(0, one_sec_one_nano_expected.GetDeltaSince(one_sec_one_nano_actual).ToNanoseconds());
+  ASSERT_EQ(0, (one_sec_one_nano_expected - one_sec_one_nano_actual).ToNanoseconds());
 
   MonoDelta zero_sec_two_nanos(MonoDelta::FromNanoseconds(2L));
   zero_sec_two_nanos.ToTimeSpec(&ts);
@@ -127,11 +127,9 @@ TEST(TestMonoTime, TestDeltas) {
   alarm(360);
   const MonoDelta max_delta(MonoDelta::FromSeconds(0.1));
   MonoTime prev(MonoTime::Now());
-  MonoTime next;
   MonoDelta cur_delta;
   do {
-    next = MonoTime::Now();
-    cur_delta = next.GetDeltaSince(prev);
+    cur_delta = MonoTime::Now() - prev;
   } while (cur_delta.LessThan(max_delta));
   alarm(0);
 }
@@ -153,11 +151,9 @@ static void DoTestMonoTimePerf() {
   const MonoDelta max_delta(MonoDelta::FromMilliseconds(500));
   uint64_t num_calls = 0;
   MonoTime prev(MonoTime::Now());
-  MonoTime next;
   MonoDelta cur_delta;
   do {
-    next = MonoTime::Now();
-    cur_delta = next.GetDeltaSince(prev);
+    cur_delta = MonoTime::Now() - prev;
     num_calls++;
   } while (cur_delta.LessThan(max_delta));
   LOG(INFO) << "DoTestMonoTimePerf():"
@@ -170,7 +166,7 @@ TEST(TestMonoTime, TestSleepFor) {
   MonoDelta sleep = MonoDelta::FromMilliseconds(100);
   SleepFor(sleep);
   MonoTime end = MonoTime::Now();
-  MonoDelta actualSleep = end.GetDeltaSince(start);
+  MonoDelta actualSleep = end - start;
   ASSERT_GE(actualSleep.ToNanoseconds(), sleep.ToNanoseconds());
 }
 
@@ -186,7 +182,7 @@ TEST(TestMonoTime, TestSleepForOverflow) {
   MonoDelta sleep = MonoDelta::FromNanoseconds(1L << 32);
   SleepFor(sleep);
   MonoTime end = MonoTime::Now();
-  MonoDelta actualSleep = end.GetDeltaSince(start);
+  MonoDelta actualSleep = end - start;
   ASSERT_GE(actualSleep.ToNanoseconds(), sleep.ToNanoseconds());
 }
 
diff --git a/src/kudu/util/monotime.cc b/src/kudu/util/monotime.cc
index 89c795d..b6ffa65 100644
--- a/src/kudu/util/monotime.cc
+++ b/src/kudu/util/monotime.cc
@@ -201,11 +201,7 @@ bool MonoTime::Initialized() const {
 }
 
 MonoDelta MonoTime::GetDeltaSince(const MonoTime &rhs) const {
-  DCHECK(Initialized());
-  DCHECK(rhs.Initialized());
-  int64_t delta(nanos_);
-  delta -= rhs.nanos_;
-  return MonoDelta(delta);
+  return rhs - *this;
 }
 
 void MonoTime::AddDelta(const MonoDelta &delta) {
@@ -328,7 +324,11 @@ MonoTime operator-(const MonoTime& t, const MonoDelta& delta)
{
 }
 
 MonoDelta operator-(const MonoTime& t_end, const MonoTime& t_beg) {
-  return t_end.GetDeltaSince(t_beg);
+  DCHECK(t_beg.Initialized());
+  DCHECK(t_end.Initialized());
+  int64_t delta(t_end.nanos_);
+  delta -= t_beg.nanos_;
+  return MonoDelta(delta);
 }
 
 } // namespace kudu
diff --git a/src/kudu/util/monotime.h b/src/kudu/util/monotime.h
index bb8ec35..f0013a1 100644
--- a/src/kudu/util/monotime.h
+++ b/src/kudu/util/monotime.h
@@ -131,6 +131,7 @@ class KUDU_EXPORT MonoDelta {
   static const int64_t kUninitialized;
 
   friend class MonoTime;
+  friend MonoDelta operator-(const class MonoTime&, const class MonoTime&);
   FRIEND_TEST(TestMonoTime, TestDeltaConversions);
 
   explicit MonoDelta(int64_t delta);
@@ -169,6 +170,8 @@ class KUDU_EXPORT MonoTime {
 
   /// Select the earliest between the specified time points.
   ///
+  /// @deprecated Use @c use std::min() instead.
+  ///
   /// @param [in] a
   ///   The first MonoTime object to select from.
   /// @param [in] b
@@ -187,11 +190,15 @@ class KUDU_EXPORT MonoTime {
   /// Compute time interval between the point in time specified by this
   /// and the specified object.
   ///
+  /// @deprecated Use @c kudu::operator-(const MonoTime&, const MonoTime&)
+  ///   instead.
+  ///
   /// @param [in] rhs
   ///   The object that corresponds to the left boundary of the time interval,
   ///   where this object corresponds to the right boundary of the interval.
   /// @return The resulting time interval represented as a MonoDelta object.
-  MonoDelta GetDeltaSince(const MonoTime &rhs) const;
+  MonoDelta GetDeltaSince(const MonoTime &rhs) const ATTRIBUTE_DEPRECATED(
+      "use kudu::operator-(const MonoTime&, const MonoTime&) instead");
 
   /// Advance this object's time specification by the specified interval.
   ///
@@ -246,6 +253,7 @@ class KUDU_EXPORT MonoTime {
 
  private:
   friend class MonoDelta;
+  friend MonoDelta operator-(const MonoTime&, const MonoTime&);
   FRIEND_TEST(TestMonoTime, TestTimeSpec);
   FRIEND_TEST(TestMonoTime, TestDeltaConversions);
 
@@ -403,8 +411,6 @@ MonoTime KUDU_EXPORT operator-(const MonoTime& t, const MonoDelta&
delta);
 
 /// Compute the time interval between the specified points in time.
 ///
-/// Semantically, this is equivalent to t0.GetDeltaSince(t1).
-///
 /// @param [in] t_end
 ///   The second point in time.  Semantically corresponds to the end
 ///   of the resulting time interval.


Mime
View raw message