From 87da1b937dc478a1562c1e0ed6c80256e7ee2c53 Mon Sep 17 00:00:00 2001
From: yangg <yangg@google.com>
Date: Thu, 11 Dec 2014 15:57:37 -0800
Subject: [PATCH] Make the default deadline gpr_inf_future to avoid mismatch
 between gpr_inf_future and time_point::max(). The redundant AbsoluteDeadline
 prefix is removed from the utility function names.

Now the ClientContext holds a gpr_timespec instead of a time_point.

A test will be added in the server side deadline support cl.
	Change on 2014/12/11 by yangg <yangg@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=81920769
---
 include/grpc++/client_context.h    |  7 +++++--
 src/cpp/client/channel.cc          | 22 ++++++++--------------
 src/cpp/client/client_context.cc   |  9 ++++-----
 src/cpp/server/completion_queue.cc |  8 ++++----
 src/cpp/util/time.cc               |  7 ++++---
 src/cpp/util/time.h                |  7 +++----
 test/cpp/util/time_test.cc         |  8 +++-----
 7 files changed, 31 insertions(+), 37 deletions(-)

diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h
index ec1a4c2782..0cf6bdc647 100644
--- a/include/grpc++/client_context.h
+++ b/include/grpc++/client_context.h
@@ -38,8 +38,9 @@
 #include <string>
 #include <vector>
 
-#include <grpc++/config.h>
 #include <grpc/support/log.h>
+#include <grpc/support/time.h>
+#include <grpc++/config.h>
 
 using std::chrono::system_clock;
 
@@ -78,9 +79,11 @@ class ClientContext {
   grpc_completion_queue *cq() { return cq_; }
   void set_cq(grpc_completion_queue *cq) { cq_ = cq; }
 
+  gpr_timespec RawDeadline() { return absolute_deadline_; }
+
   grpc_call *call_;
   grpc_completion_queue *cq_;
-  system_clock::time_point absolute_deadline_;
+  gpr_timespec absolute_deadline_;
   std::vector<std::pair<grpc::string, grpc::string> > metadata_;
 };
 
diff --git a/src/cpp/client/channel.cc b/src/cpp/client/channel.cc
index 3792869d83..3686596e77 100644
--- a/src/cpp/client/channel.cc
+++ b/src/cpp/client/channel.cc
@@ -39,12 +39,10 @@
 #include <grpc/grpc.h>
 #include <grpc/support/log.h>
 #include <grpc/support/slice.h>
-#include <grpc/support/time.h>
 
 #include "src/cpp/rpc_method.h"
 #include "src/cpp/proto/proto_utils.h"
 #include "src/cpp/stream/stream_context.h"
-#include "src/cpp/util/time.h"
 #include <grpc++/config.h>
 #include <google/protobuf/message.h>
 #include <grpc++/client_context.h>
@@ -80,12 +78,10 @@ Status Channel::StartBlockingRpc(const RpcMethod& method,
                                  const google::protobuf::Message& request,
                                  google::protobuf::Message* result) {
   Status status;
-  gpr_timespec absolute_deadline;
-  AbsoluteDeadlineTimepoint2Timespec(context->absolute_deadline(),
-                                     &absolute_deadline);
-  grpc_call* call = grpc_channel_create_call(c_channel_, method.name(),
-                                             // FIXME(yangg)
-                                             "localhost", absolute_deadline);
+  grpc_call* call =
+      grpc_channel_create_call(c_channel_, method.name(),
+                               // FIXME(yangg)
+                               "localhost", context->RawDeadline());
   context->set_call(call);
   grpc_event* ev;
   void* finished_tag = reinterpret_cast<char*>(call);
@@ -156,12 +152,10 @@ StreamContextInterface* Channel::CreateStream(const RpcMethod& method,
                                               ClientContext* context,
                                               const google::protobuf::Message* request,
                                               google::protobuf::Message* result) {
-  gpr_timespec absolute_deadline;
-  AbsoluteDeadlineTimepoint2Timespec(context->absolute_deadline(),
-                                     &absolute_deadline);
-  grpc_call* call = grpc_channel_create_call(c_channel_, method.name(),
-                                             // FIXME(yangg)
-                                             "localhost", absolute_deadline);
+  grpc_call* call =
+      grpc_channel_create_call(c_channel_, method.name(),
+                               // FIXME(yangg)
+                               "localhost", context->RawDeadline());
   context->set_call(call);
   grpc_completion_queue* cq = grpc_completion_queue_create();
   context->set_cq(cq);
diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc
index 58a8ad252b..505b7d89b4 100644
--- a/src/cpp/client/client_context.cc
+++ b/src/cpp/client/client_context.cc
@@ -34,15 +34,14 @@
 #include <grpc++/client_context.h>
 
 #include <grpc/grpc.h>
+#include "src/cpp/util/time.h"
 
 using std::chrono::system_clock;
 
 namespace grpc {
 
 ClientContext::ClientContext()
-    : call_(nullptr),
-      cq_(nullptr),
-      absolute_deadline_(system_clock::time_point::max()) {}
+    : call_(nullptr), cq_(nullptr), absolute_deadline_(gpr_inf_future) {}
 
 ClientContext::~ClientContext() {
   if (call_) {
@@ -64,11 +63,11 @@ ClientContext::~ClientContext() {
 
 void ClientContext::set_absolute_deadline(
     const system_clock::time_point& deadline) {
-  absolute_deadline_ = deadline;
+  Timepoint2Timespec(deadline, &absolute_deadline_);
 }
 
 system_clock::time_point ClientContext::absolute_deadline() {
-  return absolute_deadline_;
+  return Timespec2Timepoint(absolute_deadline_);
 }
 
 void ClientContext::AddMetadata(const grpc::string& meta_key,
diff --git a/src/cpp/server/completion_queue.cc b/src/cpp/server/completion_queue.cc
index 04eb301f7e..56d165c9a6 100644
--- a/src/cpp/server/completion_queue.cc
+++ b/src/cpp/server/completion_queue.cc
@@ -86,10 +86,10 @@ CompletionQueue::CompletionType CompletionQueue::Next(void** tag) {
       if (!ev->call) {
         *tag = nullptr;
       } else {
-        *tag = new AsyncServerContext(ev->call, ev->data.server_rpc_new.method,
-                                      ev->data.server_rpc_new.host,
-                                      AbsoluteDeadlineTimespec2Timepoint(
-                                          ev->data.server_rpc_new.deadline));
+        *tag = new AsyncServerContext(
+            ev->call, ev->data.server_rpc_new.method,
+            ev->data.server_rpc_new.host,
+            Timespec2Timepoint(ev->data.server_rpc_new.deadline));
       }
       return_type = SERVER_RPC_NEW;
       break;
diff --git a/src/cpp/util/time.cc b/src/cpp/util/time.cc
index 207bebf568..150ec8ed5a 100644
--- a/src/cpp/util/time.cc
+++ b/src/cpp/util/time.cc
@@ -42,8 +42,9 @@ using std::chrono::system_clock;
 
 namespace grpc {
 
-void AbsoluteDeadlineTimepoint2Timespec(const system_clock::time_point& from,
-                                        gpr_timespec* to) {
+// TODO(yangg) prevent potential overflow.
+void Timepoint2Timespec(const system_clock::time_point& from,
+                        gpr_timespec* to) {
   system_clock::duration deadline = from.time_since_epoch();
   seconds secs = duration_cast<seconds>(deadline);
   nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
@@ -51,7 +52,7 @@ void AbsoluteDeadlineTimepoint2Timespec(const system_clock::time_point& from,
   to->tv_nsec = nsecs.count();
 }
 
-system_clock::time_point AbsoluteDeadlineTimespec2Timepoint(gpr_timespec t) {
+system_clock::time_point Timespec2Timepoint(gpr_timespec t) {
   system_clock::time_point tp;
   tp += seconds(t.tv_sec);
   tp += nanoseconds(t.tv_nsec);
diff --git a/src/cpp/util/time.h b/src/cpp/util/time.h
index c21fba7ec3..338c4f5119 100644
--- a/src/cpp/util/time.h
+++ b/src/cpp/util/time.h
@@ -41,11 +41,10 @@
 namespace grpc {
 
 // from and to should be absolute time.
-void AbsoluteDeadlineTimepoint2Timespec(
-    const std::chrono::system_clock::time_point& from, gpr_timespec* to);
+void Timepoint2Timespec(const std::chrono::system_clock::time_point& from,
+                        gpr_timespec* to);
 
-std::chrono::system_clock::time_point AbsoluteDeadlineTimespec2Timepoint(
-    gpr_timespec t);
+std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t);
 
 }  // namespace grpc
 
diff --git a/test/cpp/util/time_test.cc b/test/cpp/util/time_test.cc
index 4c633f34f5..97499fed28 100644
--- a/test/cpp/util/time_test.cc
+++ b/test/cpp/util/time_test.cc
@@ -52,14 +52,12 @@ TEST_F(TimeTest, AbsolutePointTest) {
   long us = 10000000L;
   gpr_timespec ts = gpr_time_from_micros(us);
   system_clock::time_point tp{microseconds(us)};
-  system_clock::time_point tp_converted =
-      AbsoluteDeadlineTimespec2Timepoint(ts);
+  system_clock::time_point tp_converted = Timespec2Timepoint(ts);
   gpr_timespec ts_converted;
-  AbsoluteDeadlineTimepoint2Timespec(tp_converted, &ts_converted);
+  Timepoint2Timespec(tp_converted, &ts_converted);
   EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec);
   EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec);
-  system_clock::time_point tp_converted_2 =
-      AbsoluteDeadlineTimespec2Timepoint(ts_converted);
+  system_clock::time_point tp_converted_2 = Timespec2Timepoint(ts_converted);
   EXPECT_TRUE(tp == tp_converted);
   EXPECT_TRUE(tp == tp_converted_2);
 }
-- 
GitLab