Browse Source

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

yangg 10 years ago
parent
commit
87da1b937d

+ 5 - 2
include/grpc++/client_context.h

@@ -38,8 +38,9 @@
 #include <string>
 #include <string>
 #include <vector>
 #include <vector>
 
 
-#include <grpc++/config.h>
 #include <grpc/support/log.h>
 #include <grpc/support/log.h>
+#include <grpc/support/time.h>
+#include <grpc++/config.h>
 
 
 using std::chrono::system_clock;
 using std::chrono::system_clock;
 
 
@@ -78,9 +79,11 @@ class ClientContext {
   grpc_completion_queue *cq() { return cq_; }
   grpc_completion_queue *cq() { return cq_; }
   void set_cq(grpc_completion_queue *cq) { cq_ = cq; }
   void set_cq(grpc_completion_queue *cq) { cq_ = cq; }
 
 
+  gpr_timespec RawDeadline() { return absolute_deadline_; }
+
   grpc_call *call_;
   grpc_call *call_;
   grpc_completion_queue *cq_;
   grpc_completion_queue *cq_;
-  system_clock::time_point absolute_deadline_;
+  gpr_timespec absolute_deadline_;
   std::vector<std::pair<grpc::string, grpc::string> > metadata_;
   std::vector<std::pair<grpc::string, grpc::string> > metadata_;
 };
 };
 
 

+ 8 - 14
src/cpp/client/channel.cc

@@ -39,12 +39,10 @@
 #include <grpc/grpc.h>
 #include <grpc/grpc.h>
 #include <grpc/support/log.h>
 #include <grpc/support/log.h>
 #include <grpc/support/slice.h>
 #include <grpc/support/slice.h>
-#include <grpc/support/time.h>
 
 
 #include "src/cpp/rpc_method.h"
 #include "src/cpp/rpc_method.h"
 #include "src/cpp/proto/proto_utils.h"
 #include "src/cpp/proto/proto_utils.h"
 #include "src/cpp/stream/stream_context.h"
 #include "src/cpp/stream/stream_context.h"
-#include "src/cpp/util/time.h"
 #include <grpc++/config.h>
 #include <grpc++/config.h>
 #include <google/protobuf/message.h>
 #include <google/protobuf/message.h>
 #include <grpc++/client_context.h>
 #include <grpc++/client_context.h>
@@ -80,12 +78,10 @@ Status Channel::StartBlockingRpc(const RpcMethod& method,
                                  const google::protobuf::Message& request,
                                  const google::protobuf::Message& request,
                                  google::protobuf::Message* result) {
                                  google::protobuf::Message* result) {
   Status status;
   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);
   context->set_call(call);
   grpc_event* ev;
   grpc_event* ev;
   void* finished_tag = reinterpret_cast<char*>(call);
   void* finished_tag = reinterpret_cast<char*>(call);
@@ -156,12 +152,10 @@ StreamContextInterface* Channel::CreateStream(const RpcMethod& method,
                                               ClientContext* context,
                                               ClientContext* context,
                                               const google::protobuf::Message* request,
                                               const google::protobuf::Message* request,
                                               google::protobuf::Message* result) {
                                               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);
   context->set_call(call);
   grpc_completion_queue* cq = grpc_completion_queue_create();
   grpc_completion_queue* cq = grpc_completion_queue_create();
   context->set_cq(cq);
   context->set_cq(cq);

+ 4 - 5
src/cpp/client/client_context.cc

@@ -34,15 +34,14 @@
 #include <grpc++/client_context.h>
 #include <grpc++/client_context.h>
 
 
 #include <grpc/grpc.h>
 #include <grpc/grpc.h>
+#include "src/cpp/util/time.h"
 
 
 using std::chrono::system_clock;
 using std::chrono::system_clock;
 
 
 namespace grpc {
 namespace grpc {
 
 
 ClientContext::ClientContext()
 ClientContext::ClientContext()
-    : call_(nullptr),
-      cq_(nullptr),
-      absolute_deadline_(system_clock::time_point::max()) {}
+    : call_(nullptr), cq_(nullptr), absolute_deadline_(gpr_inf_future) {}
 
 
 ClientContext::~ClientContext() {
 ClientContext::~ClientContext() {
   if (call_) {
   if (call_) {
@@ -64,11 +63,11 @@ ClientContext::~ClientContext() {
 
 
 void ClientContext::set_absolute_deadline(
 void ClientContext::set_absolute_deadline(
     const system_clock::time_point& deadline) {
     const system_clock::time_point& deadline) {
-  absolute_deadline_ = deadline;
+  Timepoint2Timespec(deadline, &absolute_deadline_);
 }
 }
 
 
 system_clock::time_point ClientContext::absolute_deadline() {
 system_clock::time_point ClientContext::absolute_deadline() {
-  return absolute_deadline_;
+  return Timespec2Timepoint(absolute_deadline_);
 }
 }
 
 
 void ClientContext::AddMetadata(const grpc::string& meta_key,
 void ClientContext::AddMetadata(const grpc::string& meta_key,

+ 4 - 4
src/cpp/server/completion_queue.cc

@@ -86,10 +86,10 @@ CompletionQueue::CompletionType CompletionQueue::Next(void** tag) {
       if (!ev->call) {
       if (!ev->call) {
         *tag = nullptr;
         *tag = nullptr;
       } else {
       } 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;
       return_type = SERVER_RPC_NEW;
       break;
       break;

+ 4 - 3
src/cpp/util/time.cc

@@ -42,8 +42,9 @@ using std::chrono::system_clock;
 
 
 namespace grpc {
 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();
   system_clock::duration deadline = from.time_since_epoch();
   seconds secs = duration_cast<seconds>(deadline);
   seconds secs = duration_cast<seconds>(deadline);
   nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
   nanoseconds nsecs = duration_cast<nanoseconds>(deadline - secs);
@@ -51,7 +52,7 @@ void AbsoluteDeadlineTimepoint2Timespec(const system_clock::time_point& from,
   to->tv_nsec = nsecs.count();
   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;
   system_clock::time_point tp;
   tp += seconds(t.tv_sec);
   tp += seconds(t.tv_sec);
   tp += nanoseconds(t.tv_nsec);
   tp += nanoseconds(t.tv_nsec);

+ 3 - 4
src/cpp/util/time.h

@@ -41,11 +41,10 @@
 namespace grpc {
 namespace grpc {
 
 
 // from and to should be absolute time.
 // 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
 }  // namespace grpc
 
 

+ 3 - 5
test/cpp/util/time_test.cc

@@ -52,14 +52,12 @@ TEST_F(TimeTest, AbsolutePointTest) {
   long us = 10000000L;
   long us = 10000000L;
   gpr_timespec ts = gpr_time_from_micros(us);
   gpr_timespec ts = gpr_time_from_micros(us);
   system_clock::time_point tp{microseconds(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;
   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_sec == ts_converted.tv_sec);
   EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec);
   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);
   EXPECT_TRUE(tp == tp_converted_2);
   EXPECT_TRUE(tp == tp_converted_2);
 }
 }