|
|
|
@ -73,11 +73,15 @@ class ClientRpcContext { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
deadline_list::iterator deadline_posn() const { return deadline_posn_; } |
|
|
|
|
void set_deadline_posn(const deadline_list::iterator& it) {deadline_posn_ = it;} |
|
|
|
|
void set_deadline_posn(const deadline_list::iterator& it) { |
|
|
|
|
deadline_posn_ = it; |
|
|
|
|
} |
|
|
|
|
virtual void Start(CompletionQueue* cq) = 0; |
|
|
|
|
int channel_id() const { return channel_id_; } |
|
|
|
|
|
|
|
|
|
protected: |
|
|
|
|
int channel_id_; |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
deadline_list::iterator deadline_posn_; |
|
|
|
|
}; |
|
|
|
@ -85,22 +89,21 @@ class ClientRpcContext { |
|
|
|
|
template <class RequestType, class ResponseType> |
|
|
|
|
class ClientRpcContextUnaryImpl : public ClientRpcContext { |
|
|
|
|
public: |
|
|
|
|
ClientRpcContextUnaryImpl(int channel_id, |
|
|
|
|
TestService::Stub* stub, const RequestType& req, |
|
|
|
|
ClientRpcContextUnaryImpl( |
|
|
|
|
int channel_id, TestService::Stub* stub, const RequestType& req, |
|
|
|
|
std::function< |
|
|
|
|
std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>( |
|
|
|
|
TestService::Stub*, grpc::ClientContext*, const RequestType&, |
|
|
|
|
CompletionQueue*)> |
|
|
|
|
start_req, |
|
|
|
|
CompletionQueue*)> start_req, |
|
|
|
|
std::function<void(grpc::Status, ResponseType*)> on_done) |
|
|
|
|
: ClientRpcContext(channel_id), context_(), |
|
|
|
|
: ClientRpcContext(channel_id), |
|
|
|
|
context_(), |
|
|
|
|
stub_(stub), |
|
|
|
|
req_(req), |
|
|
|
|
response_(), |
|
|
|
|
next_state_(&ClientRpcContextUnaryImpl::RespDone), |
|
|
|
|
callback_(on_done), |
|
|
|
|
start_req_(start_req) { |
|
|
|
|
} |
|
|
|
|
start_req_(start_req) {} |
|
|
|
|
void Start(CompletionQueue* cq) GRPC_OVERRIDE { |
|
|
|
|
start_ = Timer::Now(); |
|
|
|
|
response_reader_ = start_req_(stub_, &context_, req_, cq); |
|
|
|
@ -116,8 +119,8 @@ class ClientRpcContextUnaryImpl : public ClientRpcContext { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ClientRpcContext* StartNewClone() GRPC_OVERRIDE { |
|
|
|
|
return new ClientRpcContextUnaryImpl(channel_id_, |
|
|
|
|
stub_, req_, start_req_, callback_); |
|
|
|
|
return new ClientRpcContextUnaryImpl(channel_id_, stub_, req_, start_req_, |
|
|
|
|
callback_); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
@ -136,8 +139,8 @@ class ClientRpcContextUnaryImpl : public ClientRpcContext { |
|
|
|
|
bool (ClientRpcContextUnaryImpl::*next_state_)(bool); |
|
|
|
|
std::function<void(grpc::Status, ResponseType*)> callback_; |
|
|
|
|
std::function<std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>>( |
|
|
|
|
TestService::Stub*, grpc::ClientContext*, |
|
|
|
|
const RequestType&, CompletionQueue *)> start_req_; |
|
|
|
|
TestService::Stub*, grpc::ClientContext*, const RequestType&, |
|
|
|
|
CompletionQueue*)> start_req_; |
|
|
|
|
grpc::Status status_; |
|
|
|
|
double start_; |
|
|
|
|
std::unique_ptr<grpc::ClientAsyncResponseReader<ResponseType>> |
|
|
|
@ -148,15 +151,16 @@ typedef std::forward_list<ClientRpcContext *> context_list; |
|
|
|
|
|
|
|
|
|
class AsyncClient : public Client { |
|
|
|
|
public: |
|
|
|
|
explicit AsyncClient(const ClientConfig& config, |
|
|
|
|
explicit AsyncClient( |
|
|
|
|
const ClientConfig& config, |
|
|
|
|
std::function<ClientRpcContext*(int, TestService::Stub*, |
|
|
|
|
const SimpleRequest&)> setup_ctx) : |
|
|
|
|
Client(config), channel_lock_(config.client_channels()), |
|
|
|
|
const SimpleRequest&)> setup_ctx) |
|
|
|
|
: Client(config), |
|
|
|
|
channel_lock_(config.client_channels()), |
|
|
|
|
contexts_(config.client_channels()), |
|
|
|
|
max_outstanding_per_channel_(config.outstanding_rpcs_per_channel()), |
|
|
|
|
channel_count_(config.client_channels()), |
|
|
|
|
pref_channel_inc_(config.async_client_threads()) { |
|
|
|
|
|
|
|
|
|
SetupLoadTest(config, config.async_client_threads()); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < config.async_client_threads(); i++) { |
|
|
|
@ -181,8 +185,7 @@ class AsyncClient : public Client { |
|
|
|
|
auto ctx = setup_ctx(ch, channel.get_stub(), request_); |
|
|
|
|
if (closed_loop_) { |
|
|
|
|
ctx->Start(cq); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
contexts_[ch].push_front(ctx); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -210,18 +213,18 @@ class AsyncClient : public Client { |
|
|
|
|
} else { |
|
|
|
|
if (rpc_deadlines_[thread_idx].empty()) { |
|
|
|
|
deadline = grpc_time_source::now() + std::chrono::seconds(1); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
deadline = *(rpc_deadlines_[thread_idx].begin()); |
|
|
|
|
} |
|
|
|
|
short_deadline = issue_allowed_[thread_idx] ? |
|
|
|
|
next_issue_[thread_idx] : deadline; |
|
|
|
|
short_deadline = |
|
|
|
|
issue_allowed_[thread_idx] ? next_issue_[thread_idx] : deadline; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool got_event; |
|
|
|
|
|
|
|
|
|
switch (cli_cqs_[thread_idx]->AsyncNext(&got_tag, &ok, short_deadline)) { |
|
|
|
|
case CompletionQueue::SHUTDOWN: return false; |
|
|
|
|
case CompletionQueue::SHUTDOWN: |
|
|
|
|
return false; |
|
|
|
|
case CompletionQueue::TIMEOUT: |
|
|
|
|
got_event = false; |
|
|
|
|
break; |
|
|
|
@ -246,8 +249,7 @@ class AsyncClient : public Client { |
|
|
|
|
ClientRpcContext* clone_ctx = ctx->StartNewClone(); |
|
|
|
|
if (closed_loop_) { |
|
|
|
|
clone_ctx->Start(cli_cqs_[thread_idx].get()); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
// Remove the entry from the rpc deadlines list
|
|
|
|
|
rpc_deadlines_[thread_idx].erase(ctx->deadline_posn()); |
|
|
|
|
// Put the clone_ctx in the list of idle contexts for this channel
|
|
|
|
@ -260,7 +262,8 @@ class AsyncClient : public Client { |
|
|
|
|
delete ctx; |
|
|
|
|
} |
|
|
|
|
if (!closed_loop_) |
|
|
|
|
issue_allowed_[thread_idx] = true; // may be ok now even if it hadn't been
|
|
|
|
|
issue_allowed_[thread_idx] = |
|
|
|
|
true; // may be ok now even if it hadn't been
|
|
|
|
|
} |
|
|
|
|
if (!closed_loop_ && issue_allowed_[thread_idx] && |
|
|
|
|
grpc_time_source::now() >= next_issue_[thread_idx]) { |
|
|
|
@ -271,8 +274,7 @@ class AsyncClient : public Client { |
|
|
|
|
bool can_issue = false; |
|
|
|
|
ClientRpcContext* ctx = nullptr; |
|
|
|
|
{ |
|
|
|
|
std::lock_guard<std::mutex> |
|
|
|
|
g(channel_lock_[channel_attempt]); |
|
|
|
|
std::lock_guard<std::mutex> g(channel_lock_[channel_attempt]); |
|
|
|
|
if (!contexts_[channel_attempt].empty()) { |
|
|
|
|
// Get an idle context from the front of the list
|
|
|
|
|
ctx = *(contexts_[channel_attempt].begin()); |
|
|
|
@ -282,8 +284,8 @@ class AsyncClient : public Client { |
|
|
|
|
} |
|
|
|
|
if (can_issue) { |
|
|
|
|
// do the work to issue
|
|
|
|
|
rpc_deadlines_[thread_idx].emplace_back( |
|
|
|
|
grpc_time_source::now() + std::chrono::seconds(1)); |
|
|
|
|
rpc_deadlines_[thread_idx].emplace_back(grpc_time_source::now() + |
|
|
|
|
std::chrono::seconds(1)); |
|
|
|
|
auto it = rpc_deadlines_[thread_idx].end(); |
|
|
|
|
--it; |
|
|
|
|
ctx->set_deadline_posn(it); |
|
|
|
@ -304,8 +306,7 @@ class AsyncClient : public Client { |
|
|
|
|
grpc_time next_issue; |
|
|
|
|
NextIssueTime(thread_idx, &next_issue); |
|
|
|
|
next_issue_[thread_idx] = next_issue; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
} else { |
|
|
|
|
issue_allowed_[thread_idx] = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -334,31 +335,28 @@ class AsyncUnaryClient GRPC_FINAL : public AsyncClient { |
|
|
|
|
StartThreads(config.async_client_threads()); |
|
|
|
|
} |
|
|
|
|
~AsyncUnaryClient() GRPC_OVERRIDE { EndThreads(); } |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
static ClientRpcContext *SetupCtx(int channel_id, |
|
|
|
|
TestService::Stub* stub, |
|
|
|
|
static ClientRpcContext* SetupCtx(int channel_id, TestService::Stub* stub, |
|
|
|
|
const SimpleRequest& req) { |
|
|
|
|
auto check_done = [](grpc::Status s, SimpleResponse* response) {}; |
|
|
|
|
auto start_req = [](TestService::Stub* stub, grpc::ClientContext* ctx, |
|
|
|
|
const SimpleRequest& request, CompletionQueue* cq) { |
|
|
|
|
return stub->AsyncUnaryCall(ctx, request, cq); |
|
|
|
|
}; |
|
|
|
|
return new ClientRpcContextUnaryImpl<SimpleRequest, |
|
|
|
|
SimpleResponse>(channel_id, stub, req, |
|
|
|
|
start_req, check_done); |
|
|
|
|
return new ClientRpcContextUnaryImpl<SimpleRequest, SimpleResponse>( |
|
|
|
|
channel_id, stub, req, start_req, check_done); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
template <class RequestType, class ResponseType> |
|
|
|
|
class ClientRpcContextStreamingImpl : public ClientRpcContext { |
|
|
|
|
public: |
|
|
|
|
ClientRpcContextStreamingImpl(int channel_id, |
|
|
|
|
TestService::Stub* stub, const RequestType& req, |
|
|
|
|
std::function<std::unique_ptr< |
|
|
|
|
grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>( |
|
|
|
|
TestService::Stub*, grpc::ClientContext*, CompletionQueue*, |
|
|
|
|
void*)> start_req, |
|
|
|
|
ClientRpcContextStreamingImpl( |
|
|
|
|
int channel_id, TestService::Stub* stub, const RequestType& req, |
|
|
|
|
std::function<std::unique_ptr<grpc::ClientAsyncReaderWriter< |
|
|
|
|
RequestType, ResponseType>>(TestService::Stub*, grpc::ClientContext*, |
|
|
|
|
CompletionQueue*, void*)> start_req, |
|
|
|
|
std::function<void(grpc::Status, ResponseType*)> on_done) |
|
|
|
|
: ClientRpcContext(channel_id), |
|
|
|
|
context_(), |
|
|
|
@ -374,12 +372,13 @@ class ClientRpcContextStreamingImpl : public ClientRpcContext { |
|
|
|
|
return (this->*next_state_)(ok, hist); |
|
|
|
|
} |
|
|
|
|
ClientRpcContext* StartNewClone() GRPC_OVERRIDE { |
|
|
|
|
return new ClientRpcContextStreamingImpl(channel_id_, |
|
|
|
|
stub_, req_, start_req_, callback_); |
|
|
|
|
return new ClientRpcContextStreamingImpl(channel_id_, stub_, req_, |
|
|
|
|
start_req_, callback_); |
|
|
|
|
} |
|
|
|
|
void Start(CompletionQueue* cq) GRPC_OVERRIDE { |
|
|
|
|
stream_ = start_req_(stub_, &context_, cq, ClientRpcContext::tag(this)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
bool ReqSent(bool ok, Histogram*) { return StartWrite(ok); } |
|
|
|
|
bool StartWrite(bool ok) { |
|
|
|
@ -411,8 +410,8 @@ class ClientRpcContextStreamingImpl : public ClientRpcContext { |
|
|
|
|
std::function<void(grpc::Status, ResponseType*)> callback_; |
|
|
|
|
std::function< |
|
|
|
|
std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>>( |
|
|
|
|
TestService::Stub*, grpc::ClientContext*, |
|
|
|
|
CompletionQueue *, void*)> start_req_; |
|
|
|
|
TestService::Stub*, grpc::ClientContext*, CompletionQueue*, void*)> |
|
|
|
|
start_req_; |
|
|
|
|
grpc::Status status_; |
|
|
|
|
double start_; |
|
|
|
|
std::unique_ptr<grpc::ClientAsyncReaderWriter<RequestType, ResponseType>> |
|
|
|
@ -430,9 +429,9 @@ class AsyncStreamingClient GRPC_FINAL : public AsyncClient { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
~AsyncStreamingClient() GRPC_OVERRIDE { EndThreads(); } |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
static ClientRpcContext *SetupCtx(int channel_id, |
|
|
|
|
TestService::Stub* stub, |
|
|
|
|
static ClientRpcContext* SetupCtx(int channel_id, TestService::Stub* stub, |
|
|
|
|
const SimpleRequest& req) { |
|
|
|
|
auto check_done = [](grpc::Status s, SimpleResponse* response) {}; |
|
|
|
|
auto start_req = [](TestService::Stub* stub, grpc::ClientContext* ctx, |
|
|
|
@ -440,10 +439,8 @@ private: |
|
|
|
|
auto stream = stub->AsyncStreamingCall(ctx, cq, tag); |
|
|
|
|
return stream; |
|
|
|
|
}; |
|
|
|
|
return new ClientRpcContextStreamingImpl<SimpleRequest, |
|
|
|
|
SimpleResponse>(channel_id, stub, |
|
|
|
|
req, start_req, |
|
|
|
|
check_done); |
|
|
|
|
return new ClientRpcContextStreamingImpl<SimpleRequest, SimpleResponse>( |
|
|
|
|
channel_id, stub, req, start_req, check_done); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|