| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| murgatroid99 | eeb02ba | 2016-01-13 20:43:33 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <cassert> |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 35 | #include <forward_list> |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 36 | #include <functional> |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 37 | #include <list> |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 38 | #include <memory> |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 39 | #include <mutex> |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 40 | #include <sstream> |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 41 | #include <string> |
| 42 | #include <thread> |
| 43 | #include <vector> |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 44 | |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 45 | #include <gflags/gflags.h> |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 46 | #include <grpc++/client_context.h> |
| Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 47 | #include <grpc++/generic/generic_stub.h> |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 48 | #include <grpc/grpc.h> |
| 49 | #include <grpc/support/histogram.h> |
| 50 | #include <grpc/support/log.h> |
| yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 51 | |
| Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 52 | #include "src/proto/grpc/testing/services.grpc.pb.h" |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 53 | #include "test/cpp/qps/client.h" |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 54 | #include "test/cpp/qps/timer.h" |
| yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 55 | #include "test/cpp/util/create_test_channel.h" |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 56 | |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 57 | namespace grpc { |
| 58 | namespace testing { |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 59 | |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 60 | typedef std::list<grpc_time> deadline_list; |
| 61 | |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 62 | class ClientRpcContext { |
| 63 | public: |
| Yang Gao | b6d57e7 | 2015-06-09 22:11:12 -0700 | [diff] [blame] | 64 | explicit ClientRpcContext(int ch) : channel_id_(ch) {} |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 65 | virtual ~ClientRpcContext() {} |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 66 | // next state, return false if done. Collect stats when appropriate |
| Vijay Pai | 1da729a | 2015-03-23 12:52:56 -0700 | [diff] [blame] | 67 | virtual bool RunNextState(bool, Histogram* hist) = 0; |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 68 | virtual ClientRpcContext* StartNewClone() = 0; |
| Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 69 | static void* tag(ClientRpcContext* c) { return reinterpret_cast<void*>(c); } |
| 70 | static ClientRpcContext* detag(void* t) { |
| 71 | return reinterpret_cast<ClientRpcContext*>(t); |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 72 | } |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 73 | |
| 74 | deadline_list::iterator deadline_posn() const { return deadline_posn_; } |
| 75 | void set_deadline_posn(const deadline_list::iterator& it) { |
| 76 | deadline_posn_ = it; |
| 77 | } |
| 78 | virtual void Start(CompletionQueue* cq) = 0; |
| 79 | int channel_id() const { return channel_id_; } |
| 80 | |
| 81 | protected: |
| 82 | int channel_id_; |
| 83 | |
| 84 | private: |
| 85 | deadline_list::iterator deadline_posn_; |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 86 | }; |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 87 | |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 88 | template <class RequestType, class ResponseType> |
| 89 | class ClientRpcContextUnaryImpl : public ClientRpcContext { |
| 90 | public: |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 91 | ClientRpcContextUnaryImpl( |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 92 | int channel_id, BenchmarkService::Stub* stub, const RequestType& req, |
| vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 93 | std::function< |
| 94 | std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>( |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 95 | BenchmarkService::Stub*, grpc::ClientContext*, const RequestType&, |
| Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 96 | CompletionQueue*)> start_req, |
| Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 97 | std::function<void(grpc::Status, ResponseType*)> on_done) |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 98 | : ClientRpcContext(channel_id), |
| 99 | context_(), |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 100 | stub_(stub), |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 101 | req_(req), |
| 102 | response_(), |
| Craig Tiller | 3676b38 | 2015-05-06 13:01:05 -0700 | [diff] [blame] | 103 | next_state_(&ClientRpcContextUnaryImpl::RespDone), |
| Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 104 | callback_(on_done), |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 105 | start_req_(start_req) {} |
| 106 | void Start(CompletionQueue* cq) GRPC_OVERRIDE { |
| 107 | start_ = Timer::Now(); |
| 108 | response_reader_ = start_req_(stub_, &context_, req_, cq); |
| Craig Tiller | 3676b38 | 2015-05-06 13:01:05 -0700 | [diff] [blame] | 109 | response_reader_->Finish(&response_, &status_, ClientRpcContext::tag(this)); |
| 110 | } |
| vjpai | 3c11066 | 2015-02-27 07:17:16 -0800 | [diff] [blame] | 111 | ~ClientRpcContextUnaryImpl() GRPC_OVERRIDE {} |
| Vijay Pai | 1da729a | 2015-03-23 12:52:56 -0700 | [diff] [blame] | 112 | bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE { |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 113 | bool ret = (this->*next_state_)(ok); |
| 114 | if (!ret) { |
| 115 | hist->Add((Timer::Now() - start_) * 1e9); |
| 116 | } |
| 117 | return ret; |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 120 | ClientRpcContext* StartNewClone() GRPC_OVERRIDE { |
| 121 | return new ClientRpcContextUnaryImpl(channel_id_, stub_, req_, start_req_, |
| 122 | callback_); |
| Craig Tiller | ef63839 | 2015-03-04 12:23:12 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 125 | private: |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 126 | bool RespDone(bool) { |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 127 | next_state_ = &ClientRpcContextUnaryImpl::DoCallBack; |
| 128 | return false; |
| 129 | } |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 130 | bool DoCallBack(bool) { |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 131 | callback_(status_, &response_); |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 132 | return true; // we're done, this'll be ignored |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 133 | } |
| 134 | grpc::ClientContext context_; |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 135 | BenchmarkService::Stub* stub_; |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 136 | RequestType req_; |
| 137 | ResponseType response_; |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 138 | bool (ClientRpcContextUnaryImpl::*next_state_)(bool); |
| Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 139 | std::function<void(grpc::Status, ResponseType*)> callback_; |
| Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 140 | std::function<std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>( |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 141 | BenchmarkService::Stub*, grpc::ClientContext*, const RequestType&, |
| Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 142 | CompletionQueue*)> start_req_; |
| Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 143 | grpc::Status status_; |
| 144 | double start_; |
| 145 | std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>> |
| 146 | response_reader_; |
| 147 | }; |
| vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 148 | |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 149 | typedef std::forward_list<ClientRpcContext*> context_list; |
| 150 | |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 151 | template <class StubType, class RequestType> |
| Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 152 | class AsyncClient : public ClientImpl<StubType, RequestType> { |
| 153 | // Specify which protected members we are using since there is no |
| 154 | // member name resolution until the template types are fully resolved |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 155 | public: |
| Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 156 | using Client::SetupLoadTest; |
| 157 | using Client::NextIssueTime; |
| 158 | using Client::closed_loop_; |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 159 | using ClientImpl<StubType, RequestType>::channels_; |
| 160 | using ClientImpl<StubType, RequestType>::request_; |
| Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 161 | AsyncClient(const ClientConfig& config, |
| 162 | std::function<ClientRpcContext*(int, StubType*, |
| 163 | const RequestType&)> setup_ctx, |
| 164 | std::function<std::unique_ptr<StubType>(std::shared_ptr<Channel>)> |
| 165 | create_stub) |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 166 | : ClientImpl<StubType, RequestType>(config, create_stub), |
| Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 167 | channel_lock_(new std::mutex[config.client_channels()]), |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 168 | contexts_(config.client_channels()), |
| 169 | max_outstanding_per_channel_(config.outstanding_rpcs_per_channel()), |
| 170 | channel_count_(config.client_channels()), |
| 171 | pref_channel_inc_(config.async_client_threads()) { |
| 172 | SetupLoadTest(config, config.async_client_threads()); |
| 173 | |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 174 | for (int i = 0; i < config.async_client_threads(); i++) { |
| 175 | cli_cqs_.emplace_back(new CompletionQueue); |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 176 | if (!closed_loop_) { |
| 177 | rpc_deadlines_.emplace_back(); |
| 178 | next_channel_.push_back(i % channel_count_); |
| Vijay Pai | e2980cd | 2015-07-01 09:38:20 -0700 | [diff] [blame] | 179 | issue_allowed_.emplace_back(true); |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 180 | |
| 181 | grpc_time next_issue; |
| 182 | NextIssueTime(i, &next_issue); |
| 183 | next_issue_.push_back(next_issue); |
| 184 | } |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 185 | } |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 186 | |
| vjpai | 6a60802 | 2015-05-18 09:16:53 -0700 | [diff] [blame] | 187 | int t = 0; |
| 188 | for (int i = 0; i < config.outstanding_rpcs_per_channel(); i++) { |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 189 | for (int ch = 0; ch < channel_count_; ch++) { |
| Vijay Pai | 7b172b2 | 2015-06-05 02:03:18 -0700 | [diff] [blame] | 190 | auto* cq = cli_cqs_[t].get(); |
| 191 | t = (t + 1) % cli_cqs_.size(); |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 192 | auto ctx = setup_ctx(ch, channels_[ch].get_stub(), request_); |
| 193 | if (closed_loop_) { |
| 194 | ctx->Start(cq); |
| 195 | } else { |
| 196 | contexts_[ch].push_front(ctx); |
| 197 | } |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 200 | } |
| Vijay Pai | e10ebf15 | 2015-04-30 13:12:31 -0700 | [diff] [blame] | 201 | virtual ~AsyncClient() { |
| Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 202 | for (auto cq = cli_cqs_.begin(); cq != cli_cqs_.end(); cq++) { |
| 203 | (*cq)->Shutdown(); |
| Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 204 | void* got_tag; |
| Craig Tiller | ef63839 | 2015-03-04 12:23:12 -0800 | [diff] [blame] | 205 | bool ok; |
| Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 206 | while ((*cq)->Next(&got_tag, &ok)) { |
| Craig Tiller | ef63839 | 2015-03-04 12:23:12 -0800 | [diff] [blame] | 207 | delete ClientRpcContext::detag(got_tag); |
| 208 | } |
| 209 | } |
| Vijay Pai | 5606199 | 2015-07-01 13:39:48 -0700 | [diff] [blame] | 210 | // Now clear out all the pre-allocated idle contexts |
| 211 | for (int ch = 0; ch < channel_count_; ch++) { |
| Vijay Pai | 13735d5 | 2015-07-01 14:00:08 -0700 | [diff] [blame] | 212 | while (!contexts_[ch].empty()) { |
| Vijay Pai | 5606199 | 2015-07-01 13:39:48 -0700 | [diff] [blame] | 213 | // Get an idle context from the front of the list |
| 214 | auto* ctx = *(contexts_[ch].begin()); |
| 215 | contexts_[ch].pop_front(); |
| 216 | delete ctx; |
| 217 | } |
| 218 | } |
| Vijay Pai | eed63fa | 2015-08-05 23:08:34 +0000 | [diff] [blame] | 219 | delete[] channel_lock_; |
| Craig Tiller | ef63839 | 2015-03-04 12:23:12 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 222 | bool ThreadFunc(Histogram* histogram, |
| 223 | size_t thread_idx) GRPC_OVERRIDE GRPC_FINAL { |
| Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 224 | void* got_tag; |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 225 | bool ok; |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 226 | grpc_time deadline, short_deadline; |
| 227 | if (closed_loop_) { |
| 228 | deadline = grpc_time_source::now() + std::chrono::seconds(1); |
| 229 | short_deadline = deadline; |
| 230 | } else { |
| 231 | if (rpc_deadlines_[thread_idx].empty()) { |
| 232 | deadline = grpc_time_source::now() + std::chrono::seconds(1); |
| 233 | } else { |
| 234 | deadline = *(rpc_deadlines_[thread_idx].begin()); |
| 235 | } |
| 236 | short_deadline = |
| 237 | issue_allowed_[thread_idx] ? next_issue_[thread_idx] : deadline; |
| 238 | } |
| 239 | |
| 240 | bool got_event; |
| 241 | |
| 242 | switch (cli_cqs_[thread_idx]->AsyncNext(&got_tag, &ok, short_deadline)) { |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 243 | case CompletionQueue::SHUTDOWN: |
| 244 | return false; |
| vjpai | 37f7257 | 2015-05-12 10:29:07 -0700 | [diff] [blame] | 245 | case CompletionQueue::TIMEOUT: |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 246 | got_event = false; |
| 247 | break; |
| vjpai | 37f7257 | 2015-05-12 10:29:07 -0700 | [diff] [blame] | 248 | case CompletionQueue::GOT_EVENT: |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 249 | got_event = true; |
| 250 | break; |
| 251 | default: |
| 252 | GPR_ASSERT(false); |
| Vijay Pai | 9dc5c15 | 2015-06-03 11:34:53 -0700 | [diff] [blame] | 253 | break; |
| Craig Tiller | 1c61af7 | 2015-04-09 12:08:44 -0700 | [diff] [blame] | 254 | } |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 255 | if (got_event) { |
| 256 | ClientRpcContext* ctx = ClientRpcContext::detag(got_tag); |
| 257 | if (ctx->RunNextState(ok, histogram) == false) { |
| 258 | // call the callback and then clone the ctx |
| 259 | ctx->RunNextState(ok, histogram); |
| 260 | ClientRpcContext* clone_ctx = ctx->StartNewClone(); |
| 261 | if (closed_loop_) { |
| 262 | clone_ctx->Start(cli_cqs_[thread_idx].get()); |
| 263 | } else { |
| 264 | // Remove the entry from the rpc deadlines list |
| 265 | rpc_deadlines_[thread_idx].erase(ctx->deadline_posn()); |
| 266 | // Put the clone_ctx in the list of idle contexts for this channel |
| 267 | // Under lock |
| 268 | int ch = clone_ctx->channel_id(); |
| 269 | std::lock_guard<std::mutex> g(channel_lock_[ch]); |
| 270 | contexts_[ch].push_front(clone_ctx); |
| 271 | } |
| 272 | // delete the old version |
| 273 | delete ctx; |
| 274 | } |
| 275 | if (!closed_loop_) |
| 276 | issue_allowed_[thread_idx] = |
| 277 | true; // may be ok now even if it hadn't been |
| 278 | } |
| 279 | if (!closed_loop_ && issue_allowed_[thread_idx] && |
| 280 | grpc_time_source::now() >= next_issue_[thread_idx]) { |
| 281 | // Attempt to issue |
| 282 | bool issued = false; |
| 283 | for (int num_attempts = 0, channel_attempt = next_channel_[thread_idx]; |
| 284 | num_attempts < channel_count_ && !issued; num_attempts++) { |
| 285 | bool can_issue = false; |
| 286 | ClientRpcContext* ctx = nullptr; |
| 287 | { |
| 288 | std::lock_guard<std::mutex> g(channel_lock_[channel_attempt]); |
| 289 | if (!contexts_[channel_attempt].empty()) { |
| 290 | // Get an idle context from the front of the list |
| 291 | ctx = *(contexts_[channel_attempt].begin()); |
| 292 | contexts_[channel_attempt].pop_front(); |
| 293 | can_issue = true; |
| 294 | } |
| 295 | } |
| 296 | if (can_issue) { |
| 297 | // do the work to issue |
| 298 | rpc_deadlines_[thread_idx].emplace_back(grpc_time_source::now() + |
| 299 | std::chrono::seconds(1)); |
| 300 | auto it = rpc_deadlines_[thread_idx].end(); |
| 301 | --it; |
| 302 | ctx->set_deadline_posn(it); |
| 303 | ctx->Start(cli_cqs_[thread_idx].get()); |
| 304 | issued = true; |
| 305 | // If we did issue, then next time, try our thread's next |
| 306 | // preferred channel |
| 307 | next_channel_[thread_idx] += pref_channel_inc_; |
| 308 | if (next_channel_[thread_idx] >= channel_count_) |
| 309 | next_channel_[thread_idx] = (thread_idx % channel_count_); |
| 310 | } else { |
| 311 | // Do a modular increment of channel attempt if we couldn't issue |
| 312 | channel_attempt = (channel_attempt + 1) % channel_count_; |
| 313 | } |
| 314 | } |
| 315 | if (issued) { |
| 316 | // We issued one; see when we can issue the next |
| 317 | grpc_time next_issue; |
| 318 | NextIssueTime(thread_idx, &next_issue); |
| 319 | next_issue_[thread_idx] = next_issue; |
| 320 | } else { |
| 321 | issue_allowed_[thread_idx] = false; |
| 322 | } |
| 323 | } |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 324 | return true; |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 325 | } |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 326 | |
| Vijay Pai | e10ebf15 | 2015-04-30 13:12:31 -0700 | [diff] [blame] | 327 | private: |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 328 | class boolean { // exists only to avoid data-race on vector<bool> |
| Vijay Pai | e2980cd | 2015-07-01 09:38:20 -0700 | [diff] [blame] | 329 | public: |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 330 | boolean() : val_(false) {} |
| 331 | boolean(bool b) : val_(b) {} |
| 332 | operator bool() const { return val_; } |
| 333 | boolean& operator=(bool b) { |
| 334 | val_ = b; |
| 335 | return *this; |
| 336 | } |
| 337 | |
| Vijay Pai | e2980cd | 2015-07-01 09:38:20 -0700 | [diff] [blame] | 338 | private: |
| 339 | bool val_; |
| 340 | }; |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 341 | std::vector<std::unique_ptr<CompletionQueue>> cli_cqs_; |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 342 | |
| 343 | std::vector<deadline_list> rpc_deadlines_; // per thread deadlines |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 344 | std::vector<int> next_channel_; // per thread round-robin channel ctr |
| 345 | std::vector<boolean> issue_allowed_; // may this thread attempt to issue |
| 346 | std::vector<grpc_time> next_issue_; // when should it issue? |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 347 | |
| Vijay Pai | 90e7369 | 2015-08-05 19:15:36 -0700 | [diff] [blame] | 348 | std::mutex* |
| 349 | channel_lock_; // a vector, but avoid std::vector for old compilers |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 350 | std::vector<context_list> contexts_; // per-channel list of idle contexts |
| 351 | int max_outstanding_per_channel_; |
| 352 | int channel_count_; |
| 353 | int pref_channel_inc_; |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 354 | }; |
| 355 | |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 356 | static std::unique_ptr<BenchmarkService::Stub> BenchmarkStubCreator( |
| 357 | std::shared_ptr<Channel> ch) { |
| 358 | return BenchmarkService::NewStub(ch); |
| 359 | } |
| 360 | |
| 361 | class AsyncUnaryClient GRPC_FINAL |
| 362 | : public AsyncClient<BenchmarkService::Stub, SimpleRequest> { |
| Vijay Pai | e10ebf15 | 2015-04-30 13:12:31 -0700 | [diff] [blame] | 363 | public: |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 364 | explicit AsyncUnaryClient(const ClientConfig& config) |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 365 | : AsyncClient(config, SetupCtx, BenchmarkStubCreator) { |
| Vijay Pai | e10ebf15 | 2015-04-30 13:12:31 -0700 | [diff] [blame] | 366 | StartThreads(config.async_client_threads()); |
| 367 | } |
| 368 | ~AsyncUnaryClient() GRPC_OVERRIDE { EndThreads(); } |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 369 | |
| 370 | private: |
| vjpai | 09d0b0c | 2015-07-31 08:39:54 -0700 | [diff] [blame] | 371 | static void CheckDone(grpc::Status s, SimpleResponse* response) {} |
| 372 | static std::unique_ptr<grpc::ClientAsyncResponseReader<SimpleResponse>> |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 373 | StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx, |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 374 | const SimpleRequest& request, CompletionQueue* cq) { |
| vjpai | 09d0b0c | 2015-07-31 08:39:54 -0700 | [diff] [blame] | 375 | return stub->AsyncUnaryCall(ctx, request, cq); |
| 376 | }; |
| Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 377 | static ClientRpcContext* SetupCtx(int channel_id, |
| 378 | BenchmarkService::Stub* stub, |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 379 | const SimpleRequest& req) { |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 380 | return new ClientRpcContextUnaryImpl<SimpleRequest, SimpleResponse>( |
| 381 | channel_id, stub, req, AsyncUnaryClient::StartReq, |
| 382 | AsyncUnaryClient::CheckDone); |
| Vijay Pai | e10ebf15 | 2015-04-30 13:12:31 -0700 | [diff] [blame] | 383 | } |
| 384 | }; |
| 385 | |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 386 | template <class RequestType, class ResponseType> |
| 387 | class ClientRpcContextStreamingImpl : public ClientRpcContext { |
| 388 | public: |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 389 | ClientRpcContextStreamingImpl( |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 390 | int channel_id, BenchmarkService::Stub* stub, const RequestType& req, |
| Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 391 | std::function<std::unique_ptr< |
| 392 | grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>( |
| 393 | BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*, |
| Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 394 | void*)> start_req, |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 395 | std::function<void(grpc::Status, ResponseType*)> on_done) |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 396 | : ClientRpcContext(channel_id), |
| 397 | context_(), |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 398 | stub_(stub), |
| 399 | req_(req), |
| 400 | response_(), |
| 401 | next_state_(&ClientRpcContextStreamingImpl::ReqSent), |
| 402 | callback_(on_done), |
| 403 | start_req_(start_req), |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 404 | start_(Timer::Now()) {} |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 405 | ~ClientRpcContextStreamingImpl() GRPC_OVERRIDE {} |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 406 | bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE { |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 407 | return (this->*next_state_)(ok, hist); |
| 408 | } |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 409 | ClientRpcContext* StartNewClone() GRPC_OVERRIDE { |
| 410 | return new ClientRpcContextStreamingImpl(channel_id_, stub_, req_, |
| 411 | start_req_, callback_); |
| 412 | } |
| 413 | void Start(CompletionQueue* cq) GRPC_OVERRIDE { |
| 414 | stream_ = start_req_(stub_, &context_, cq, ClientRpcContext::tag(this)); |
| Vijay Pai | 7b172b2 | 2015-06-05 02:03:18 -0700 | [diff] [blame] | 415 | } |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 416 | |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 417 | private: |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 418 | bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); } |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 419 | bool StartWrite(bool ok) { |
| 420 | if (!ok) { |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 421 | return (false); |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 422 | } |
| 423 | start_ = Timer::Now(); |
| 424 | next_state_ = &ClientRpcContextStreamingImpl::WriteDone; |
| 425 | stream_->Write(req_, ClientRpcContext::tag(this)); |
| 426 | return true; |
| 427 | } |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 428 | bool WriteDone(bool ok, Histogram*) { |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 429 | if (!ok) { |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 430 | return (false); |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 431 | } |
| 432 | next_state_ = &ClientRpcContextStreamingImpl::ReadDone; |
| Vijay Pai | e10ebf15 | 2015-04-30 13:12:31 -0700 | [diff] [blame] | 433 | stream_->Read(&response_, ClientRpcContext::tag(this)); |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 434 | return true; |
| 435 | } |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 436 | bool ReadDone(bool ok, Histogram* hist) { |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 437 | hist->Add((Timer::Now() - start_) * 1e9); |
| 438 | return StartWrite(ok); |
| 439 | } |
| 440 | grpc::ClientContext context_; |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 441 | BenchmarkService::Stub* stub_; |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 442 | RequestType req_; |
| 443 | ResponseType response_; |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 444 | bool (ClientRpcContextStreamingImpl::*next_state_)(bool, Histogram*); |
| 445 | std::function<void(grpc::Status, ResponseType*)> callback_; |
| Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 446 | std::function< |
| 447 | std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>( |
| 448 | BenchmarkService::Stub*, grpc::ClientContext*, CompletionQueue*, |
| 449 | void*)> start_req_; |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 450 | grpc::Status status_; |
| 451 | double start_; |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 452 | std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>> |
| 453 | stream_; |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 454 | }; |
| 455 | |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 456 | class AsyncStreamingClient GRPC_FINAL |
| 457 | : public AsyncClient<BenchmarkService::Stub, SimpleRequest> { |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 458 | public: |
| Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 459 | explicit AsyncStreamingClient(const ClientConfig& config) |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 460 | : AsyncClient(config, SetupCtx, BenchmarkStubCreator) { |
| vjpai | 754751e | 2015-10-28 09:16:22 -0700 | [diff] [blame] | 461 | // async streaming currently only supports closed loop |
| 462 | GPR_ASSERT(closed_loop_); |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 463 | |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 464 | StartThreads(config.async_client_threads()); |
| 465 | } |
| 466 | |
| Vijay Pai | e10ebf15 | 2015-04-30 13:12:31 -0700 | [diff] [blame] | 467 | ~AsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); } |
| Vijay Pai | cf3fb09 | 2015-06-05 03:41:30 -0700 | [diff] [blame] | 468 | |
| 469 | private: |
| vjpai | 09d0b0c | 2015-07-31 08:39:54 -0700 | [diff] [blame] | 470 | static void CheckDone(grpc::Status s, SimpleResponse* response) {} |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 471 | static std::unique_ptr< |
| 472 | grpc::ClientAsyncReaderWriter<SimpleRequest, SimpleResponse>> |
| vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 473 | StartReq(BenchmarkService::Stub* stub, grpc::ClientContext* ctx, |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 474 | CompletionQueue* cq, void* tag) { |
| vjpai | 09d0b0c | 2015-07-31 08:39:54 -0700 | [diff] [blame] | 475 | auto stream = stub->AsyncStreamingCall(ctx, cq, tag); |
| 476 | return stream; |
| 477 | }; |
| Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 478 | static ClientRpcContext* SetupCtx(int channel_id, |
| 479 | BenchmarkService::Stub* stub, |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 480 | const SimpleRequest& req) { |
| Vijay Pai | 372fd87 | 2015-06-08 13:30:08 -0700 | [diff] [blame] | 481 | return new ClientRpcContextStreamingImpl<SimpleRequest, SimpleResponse>( |
| Vijay Pai | ab1dba7 | 2015-07-31 09:09:09 -0700 | [diff] [blame] | 482 | channel_id, stub, req, AsyncStreamingClient::StartReq, |
| 483 | AsyncStreamingClient::CheckDone); |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 484 | } |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 485 | }; |
| 486 | |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 487 | class ClientRpcContextGenericStreamingImpl : public ClientRpcContext { |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 488 | public: |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 489 | ClientRpcContextGenericStreamingImpl( |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 490 | int channel_id, grpc::GenericStub* stub, const ByteBuffer& req, |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 491 | std::function<std::unique_ptr<grpc::GenericClientAsyncReaderWriter>( |
| 492 | grpc::GenericStub*, grpc::ClientContext*, |
| Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 493 | const grpc::string& method_name, CompletionQueue*, void*)> start_req, |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 494 | std::function<void(grpc::Status, ByteBuffer*)> on_done) |
| 495 | : ClientRpcContext(channel_id), |
| 496 | context_(), |
| 497 | stub_(stub), |
| 498 | req_(req), |
| 499 | response_(), |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 500 | next_state_(&ClientRpcContextGenericStreamingImpl::ReqSent), |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 501 | callback_(on_done), |
| 502 | start_req_(start_req), |
| 503 | start_(Timer::Now()) {} |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 504 | ~ClientRpcContextGenericStreamingImpl() GRPC_OVERRIDE {} |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 505 | bool RunNextState(bool ok, Histogram* hist) GRPC_OVERRIDE { |
| 506 | return (this->*next_state_)(ok, hist); |
| 507 | } |
| 508 | ClientRpcContext* StartNewClone() GRPC_OVERRIDE { |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 509 | return new ClientRpcContextGenericStreamingImpl(channel_id_, stub_, req_, |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 510 | start_req_, callback_); |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 511 | } |
| 512 | void Start(CompletionQueue* cq) GRPC_OVERRIDE { |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 513 | const grpc::string kMethodName( |
| 514 | "/grpc.testing.BenchmarkService/StreamingCall"); |
| 515 | stream_ = start_req_(stub_, &context_, kMethodName, cq, |
| 516 | ClientRpcContext::tag(this)); |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | private: |
| 520 | bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); } |
| 521 | bool StartWrite(bool ok) { |
| 522 | if (!ok) { |
| 523 | return (false); |
| 524 | } |
| 525 | start_ = Timer::Now(); |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 526 | next_state_ = &ClientRpcContextGenericStreamingImpl::WriteDone; |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 527 | stream_->Write(req_, ClientRpcContext::tag(this)); |
| 528 | return true; |
| 529 | } |
| 530 | bool WriteDone(bool ok, Histogram*) { |
| 531 | if (!ok) { |
| 532 | return (false); |
| 533 | } |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 534 | next_state_ = &ClientRpcContextGenericStreamingImpl::ReadDone; |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 535 | stream_->Read(&response_, ClientRpcContext::tag(this)); |
| 536 | return true; |
| 537 | } |
| 538 | bool ReadDone(bool ok, Histogram* hist) { |
| 539 | hist->Add((Timer::Now() - start_) * 1e9); |
| 540 | return StartWrite(ok); |
| 541 | } |
| 542 | grpc::ClientContext context_; |
| 543 | grpc::GenericStub* stub_; |
| 544 | ByteBuffer req_; |
| 545 | ByteBuffer response_; |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 546 | bool (ClientRpcContextGenericStreamingImpl::*next_state_)(bool, Histogram*); |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 547 | std::function<void(grpc::Status, ByteBuffer*)> callback_; |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 548 | std::function<std::unique_ptr<grpc::GenericClientAsyncReaderWriter>( |
| 549 | grpc::GenericStub*, grpc::ClientContext*, const grpc::string&, |
| Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 550 | CompletionQueue*, void*)> start_req_; |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 551 | grpc::Status status_; |
| 552 | double start_; |
| 553 | std::unique_ptr<grpc::GenericClientAsyncReaderWriter> stream_; |
| 554 | }; |
| 555 | |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 556 | static std::unique_ptr<grpc::GenericStub> GenericStubCreator( |
| 557 | std::shared_ptr<Channel> ch) { |
| 558 | return std::unique_ptr<grpc::GenericStub>(new grpc::GenericStub(ch)); |
| 559 | } |
| 560 | |
| 561 | class GenericAsyncStreamingClient GRPC_FINAL |
| 562 | : public AsyncClient<grpc::GenericStub, ByteBuffer> { |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 563 | public: |
| 564 | explicit GenericAsyncStreamingClient(const ClientConfig& config) |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 565 | : AsyncClient(config, SetupCtx, GenericStubCreator) { |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 566 | // async streaming currently only supports closed loop |
| 567 | GPR_ASSERT(closed_loop_); |
| 568 | |
| 569 | StartThreads(config.async_client_threads()); |
| 570 | } |
| 571 | |
| 572 | ~GenericAsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); } |
| 573 | |
| 574 | private: |
| 575 | static void CheckDone(grpc::Status s, ByteBuffer* response) {} |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 576 | static std::unique_ptr<grpc::GenericClientAsyncReaderWriter> StartReq( |
| 577 | grpc::GenericStub* stub, grpc::ClientContext* ctx, |
| 578 | const grpc::string& method_name, CompletionQueue* cq, void* tag) { |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 579 | auto stream = stub->Call(ctx, method_name, cq, tag); |
| 580 | return stream; |
| 581 | }; |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 582 | static ClientRpcContext* SetupCtx(int channel_id, grpc::GenericStub* stub, |
| Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 583 | const ByteBuffer& req) { |
| Vijay Pai | 9f991e2 | 2016-01-07 14:08:09 -0800 | [diff] [blame] | 584 | return new ClientRpcContextGenericStreamingImpl( |
| Vijay Pai | e488668 | 2015-12-30 11:56:19 -0800 | [diff] [blame] | 585 | channel_id, stub, req, GenericAsyncStreamingClient::StartReq, |
| 586 | GenericAsyncStreamingClient::CheckDone); |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 587 | } |
| 588 | }; |
| 589 | |
| Vijay Pai | 1da729a | 2015-03-23 12:52:56 -0700 | [diff] [blame] | 590 | std::unique_ptr<Client> CreateAsyncUnaryClient(const ClientConfig& args) { |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 591 | return std::unique_ptr<Client>(new AsyncUnaryClient(args)); |
| 592 | } |
| Vijay Pai | 1da729a | 2015-03-23 12:52:56 -0700 | [diff] [blame] | 593 | std::unique_ptr<Client> CreateAsyncStreamingClient(const ClientConfig& args) { |
| vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 594 | return std::unique_ptr<Client>(new AsyncStreamingClient(args)); |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 595 | } |
| Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 596 | std::unique_ptr<Client> CreateGenericAsyncStreamingClient( |
| 597 | const ClientConfig& args) { |
| vjpai | b6df94a | 2015-11-30 15:52:50 -0800 | [diff] [blame] | 598 | return std::unique_ptr<Client>(new GenericAsyncStreamingClient(args)); |
| 599 | } |
| Craig Tiller | 8856875 | 2015-03-04 10:50:43 -0800 | [diff] [blame] | 600 | |
| Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 601 | } // namespace testing |
| murgatroid99 | eeb02ba | 2016-01-13 20:43:33 -0800 | [diff] [blame] | 602 | } // namespace grpc |