|
|
|
@ -109,6 +109,60 @@ class WorkerImpl GRPC_FINAL : public Worker::Service { |
|
|
|
|
return Status(RESOURCE_EXHAUSTED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grpc_profiler_start("qps_client.prof"); |
|
|
|
|
Status ret = RunTestBody(ctx,stream); |
|
|
|
|
grpc_profiler_stop(); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status RunServer(ServerContext* ctx, |
|
|
|
|
ServerReaderWriter<ServerStatus, ServerArgs>* stream) |
|
|
|
|
GRPC_OVERRIDE { |
|
|
|
|
InstanceGuard g(this); |
|
|
|
|
if (!g.Acquired()) { |
|
|
|
|
return Status(RESOURCE_EXHAUSTED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grpc_profiler_start("qps_server.prof"); |
|
|
|
|
Status ret = RunServerBody(ctx,stream); |
|
|
|
|
grpc_profiler_stop(); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
// Protect against multiple clients using this worker at once.
|
|
|
|
|
class InstanceGuard { |
|
|
|
|
public: |
|
|
|
|
InstanceGuard(WorkerImpl* impl) |
|
|
|
|
: impl_(impl), acquired_(impl->TryAcquireInstance()) {} |
|
|
|
|
~InstanceGuard() { |
|
|
|
|
if (acquired_) { |
|
|
|
|
impl_->ReleaseInstance(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Acquired() const { return acquired_; } |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
WorkerImpl* const impl_; |
|
|
|
|
const bool acquired_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
bool TryAcquireInstance() { |
|
|
|
|
std::lock_guard<std::mutex> g(mu_); |
|
|
|
|
if (acquired_) return false; |
|
|
|
|
acquired_ = true; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ReleaseInstance() { |
|
|
|
|
std::lock_guard<std::mutex> g(mu_); |
|
|
|
|
GPR_ASSERT(acquired_); |
|
|
|
|
acquired_ = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status RunTestBody(ServerContext* ctx, |
|
|
|
|
ServerReaderWriter<ClientStatus, ClientArgs>* stream) { |
|
|
|
|
ClientArgs args; |
|
|
|
|
if (!stream->Read(&args)) { |
|
|
|
|
return Status(INVALID_ARGUMENT); |
|
|
|
@ -135,14 +189,8 @@ class WorkerImpl GRPC_FINAL : public Worker::Service { |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status RunServer(ServerContext* ctx, |
|
|
|
|
ServerReaderWriter<ServerStatus, ServerArgs>* stream) |
|
|
|
|
GRPC_OVERRIDE { |
|
|
|
|
InstanceGuard g(this); |
|
|
|
|
if (!g.Acquired()) { |
|
|
|
|
return Status(RESOURCE_EXHAUSTED); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Status RunServerBody(ServerContext* ctx, |
|
|
|
|
ServerReaderWriter<ServerStatus, ServerArgs>* stream) { |
|
|
|
|
ServerArgs args; |
|
|
|
|
if (!stream->Read(&args)) { |
|
|
|
|
return Status(INVALID_ARGUMENT); |
|
|
|
@ -170,38 +218,6 @@ class WorkerImpl GRPC_FINAL : public Worker::Service { |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
// Protect against multiple clients using this worker at once.
|
|
|
|
|
class InstanceGuard { |
|
|
|
|
public: |
|
|
|
|
InstanceGuard(WorkerImpl* impl) |
|
|
|
|
: impl_(impl), acquired_(impl->TryAcquireInstance()) {} |
|
|
|
|
~InstanceGuard() { |
|
|
|
|
if (acquired_) { |
|
|
|
|
impl_->ReleaseInstance(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Acquired() const { return acquired_; } |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
WorkerImpl* const impl_; |
|
|
|
|
const bool acquired_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
bool TryAcquireInstance() { |
|
|
|
|
std::lock_guard<std::mutex> g(mu_); |
|
|
|
|
if (acquired_) return false; |
|
|
|
|
acquired_ = true; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ReleaseInstance() { |
|
|
|
|
std::lock_guard<std::mutex> g(mu_); |
|
|
|
|
GPR_ASSERT(acquired_); |
|
|
|
|
acquired_ = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::mutex mu_; |
|
|
|
|
bool acquired_; |
|
|
|
|
}; |
|
|
|
|