| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifndef RPCJOBS_H |
| 5 |
#define RPCJOBS_H |
| 6 |
|
| 7 |
#include "qrpcbench_common.h" |
| 8 |
|
| 9 |
#include <grpc/support/time.h> |
| 10 |
#include <proto/bench.grpc.pb.h> |
| 11 |
#include <proto/bench.pb.h> |
| 12 |
|
| 13 |
#include <grpcpp/server_context.h> |
| 14 |
|
| 15 |
using qt::bench::BenchmarkService; |
| 16 |
|
| 17 |
class RpcJob |
| 18 |
{ |
| 19 |
public: |
| 20 |
RpcJob() = default; |
| 21 |
virtual ~RpcJob() = default; |
| 22 |
|
| 23 |
RpcJob(const RpcJob &) = delete; |
| 24 |
RpcJob &operator=(const RpcJob &) = delete; |
| 25 |
|
| 26 |
RpcJob(RpcJob &&) = delete; |
| 27 |
RpcJob &operator=(RpcJob &&) = delete; |
| 28 |
|
| 29 |
virtual void onProcess(bool ok) = 0; |
| 30 |
virtual void onDone() = 0; |
| 31 |
|
| 32 |
[[nodiscard]] bool isDone() const noexcept { return mOnDoneCalled; } |
| 33 |
|
| 34 |
private: |
| 35 |
std::atomic<bool> mOnDoneCalled = false; |
| 36 |
friend class NotifyWhenDone; |
| 37 |
}; |
| 38 |
|
| 39 |
class NotifyWhenDone final : public RpcJob |
| 40 |
{ |
| 41 |
public: |
| 42 |
explicit NotifyWhenDone(RpcJob *parent) : mParent(parent) { } |
| 43 |
|
| 44 |
void onProcess(bool ) override |
| 45 |
{ |
| 46 |
assert(mParent); |
| 47 |
mParent->mOnDoneCalled = true; |
| 48 |
onDone(); |
| 49 |
} |
| 50 |
|
| 51 |
void onDone() override { delete this; } |
| 52 |
|
| 53 |
private: |
| 54 |
RpcJob *mParent; |
| 55 |
}; |
| 56 |
|
| 57 |
class UnaryCall final : public RpcJob |
| 58 |
{ |
| 59 |
using Request = qt::bench::UnaryCallRequest; |
| 60 |
using Response = qt::bench::UnaryCallResponse; |
| 61 |
|
| 62 |
public: |
| 63 |
explicit UnaryCall(grpc::ServerCompletionQueue *cq, BenchmarkService::AsyncService *service) |
| 64 |
: mCQ(cq), mService(service), mWriter(&mContext) |
| 65 |
{ |
| 66 |
mService->RequestUnaryCall(&mContext, &mRequest, &mWriter, cq, cq, this); |
| 67 |
mContext.AsyncNotifyWhenDone(new NotifyWhenDone(this)); |
| 68 |
} |
| 69 |
|
| 70 |
void onProcess(bool ok) override |
| 71 |
{ |
| 72 |
if (!ok || isDone()) |
| 73 |
return onDone(); |
| 74 |
|
| 75 |
if (mState == State::Created) { |
| 76 |
new UnaryCall(mCQ, mService); |
| 77 |
*mResponse.mutable_timestamp() = getTimestamp(); |
| 78 |
mResponse.set_request_latency_nanos(calculateLatencyNanos(mRequest.timestamp(), |
| 79 |
mResponse.timestamp())); |
| 80 |
if (mRequest.has_payload()) |
| 81 |
mResponse.mutable_payload()->swap(*mRequest.mutable_payload()); |
| 82 |
mWriter.Finish(mResponse, grpc::Status::OK, this); |
| 83 |
mState = State::Processed; |
| 84 |
} else { |
| 85 |
return onDone(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
void onDone() override { delete this; } |
| 90 |
|
| 91 |
private: |
| 92 |
grpc::ServerContext mContext; |
| 93 |
grpc::ServerCompletionQueue *mCQ; |
| 94 |
BenchmarkService::AsyncService *mService; |
| 95 |
|
| 96 |
Request mRequest; |
| 97 |
Response mResponse; |
| 98 |
grpc::ServerAsyncResponseWriter<Response> mWriter; |
| 99 |
|
| 100 |
enum class State { Created, Processed }; |
| 101 |
State mState = State::Created; |
| 102 |
}; |
| 103 |
|
| 104 |
class ServerStreaming final : public RpcJob |
| 105 |
{ |
| 106 |
using Request = qt::bench::ServerStreamingRequest; |
| 107 |
using Response = qt::bench::ServerStreamingResponse; |
| 108 |
|
| 109 |
public: |
| 110 |
explicit ServerStreaming(grpc::ServerCompletionQueue *cq, |
| 111 |
BenchmarkService::AsyncService *service) |
| 112 |
: mCQ(cq), mService(service), mServerStream(&mContext) |
| 113 |
{ |
| 114 |
mService->RequestServerStreaming(&mContext, &mRequest, &mServerStream, mCQ, mCQ, this); |
| 115 |
mContext.AsyncNotifyWhenDone(new NotifyWhenDone(this)); |
| 116 |
} |
| 117 |
|
| 118 |
void onProcess(bool ok) override |
| 119 |
{ |
| 120 |
if (!ok || isDone()) |
| 121 |
return onDone(); |
| 122 |
|
| 123 |
switch (mState) { |
| 124 |
case State::Created: { |
| 125 |
new ServerStreaming(mCQ, mService); |
| 126 |
|
| 127 |
if (mRequest.has_payload()) |
| 128 |
mResponse.mutable_payload()->assign(mRequest.payload().size(), 'x'); |
| 129 |
mResponse.set_pong(mPongCount); |
| 130 |
|
| 131 |
mServerStream.Write(mResponse, this); |
| 132 |
mState = State::Writing; |
| 133 |
} break; |
| 134 |
case State::Writing: { |
| 135 |
if (++mPongCount >= mRequest.ping()) { |
| 136 |
mServerStream.Finish(grpc::Status::OK, this); |
| 137 |
mState = State::Finished; |
| 138 |
} else { |
| 139 |
mResponse.set_pong(mPongCount); |
| 140 |
mServerStream.Write(mResponse, this); |
| 141 |
} |
| 142 |
} break; |
| 143 |
case State::Finished: { |
| 144 |
|
| 145 |
} break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void onDone() override |
| 150 |
{ |
| 151 |
std::cout << std::format("serverstream done, {} pongs\n", mPongCount); |
| 152 |
delete this; |
| 153 |
} |
| 154 |
|
| 155 |
private: |
| 156 |
grpc::ServerContext mContext; |
| 157 |
grpc::ServerCompletionQueue *mCQ; |
| 158 |
BenchmarkService::AsyncService *mService; |
| 159 |
grpc::ServerAsyncWriter<Response> mServerStream; |
| 160 |
|
| 161 |
enum class State { Created, Writing, Finished }; |
| 162 |
State mState = State::Created; |
| 163 |
|
| 164 |
Request mRequest; |
| 165 |
Response mResponse; |
| 166 |
uint64_t mPongCount = 0; |
| 167 |
}; |
| 168 |
|
| 169 |
class ClientStreaming final : public RpcJob |
| 170 |
{ |
| 171 |
using Request = qt::bench::ClientStreamingRequest; |
| 172 |
using Response = qt::bench::ClientStreamingResponse; |
| 173 |
|
| 174 |
public: |
| 175 |
explicit ClientStreaming(grpc::ServerCompletionQueue *cq, |
| 176 |
BenchmarkService::AsyncService *service) |
| 177 |
: mCQ(cq), mService(service), mClientStream(&mContext) |
| 178 |
{ |
| 179 |
mService->RequestClientStreaming(&mContext, &mClientStream, mCQ, mCQ, this); |
| 180 |
mContext.AsyncNotifyWhenDone(new NotifyWhenDone(this)); |
| 181 |
} |
| 182 |
|
| 183 |
void onProcess(bool ok) override |
| 184 |
{ |
| 185 |
if (isDone()) |
| 186 |
return onDone(); |
| 187 |
|
| 188 |
switch (mState) { |
| 189 |
case State::Created: { |
| 190 |
new ClientStreaming(mCQ, mService); |
| 191 |
if (!ok) |
| 192 |
return onDone(); |
| 193 |
mClientStream.Read(&mRequest, this); |
| 194 |
mState = State::Reading; |
| 195 |
} break; |
| 196 |
case State::Reading: { |
| 197 |
if (!ok) { |
| 198 |
mResponse.set_pong(mPingCount); |
| 199 |
mClientStream.Finish(mResponse, grpc::Status::OK, this); |
| 200 |
mState = State::Finished; |
| 201 |
} else { |
| 202 |
++mPingCount; |
| 203 |
if (mRequest.has_payload() && !mResponse.has_payload()) |
| 204 |
mResponse.mutable_payload()->assign(mRequest.payload().size(), 'x'); |
| 205 |
mRequest.set_ping(mPingCount); |
| 206 |
mClientStream.Read(&mRequest, this); |
| 207 |
} |
| 208 |
} break; |
| 209 |
case State::Finished: { |
| 210 |
|
| 211 |
} break; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void onDone() override |
| 216 |
{ |
| 217 |
std::cout << std::format("clientstream done, {} pings\n", mPingCount); |
| 218 |
delete this; |
| 219 |
} |
| 220 |
|
| 221 |
private: |
| 222 |
grpc::ServerContext mContext; |
| 223 |
grpc::ServerCompletionQueue *mCQ; |
| 224 |
BenchmarkService::AsyncService *mService; |
| 225 |
grpc::ServerAsyncReader<Response, Request> mClientStream; |
| 226 |
|
| 227 |
enum class State { Created, Reading, Finished }; |
| 228 |
State mState = State::Created; |
| 229 |
|
| 230 |
Request mRequest; |
| 231 |
Response mResponse; |
| 232 |
uint64_t mPingCount = 0; |
| 233 |
}; |
| 234 |
|
| 235 |
class BiDiStreaming final : public RpcJob |
| 236 |
{ |
| 237 |
using Request = qt::bench::BiDiStreamingRequest; |
| 238 |
using Response = qt::bench::BiDiStreamingResponse; |
| 239 |
public: |
| 240 |
explicit BiDiStreaming(grpc::ServerCompletionQueue *cq, BenchmarkService::AsyncService *service) |
| 241 |
: mCQ(cq), mService(service), mStream(&mContext) |
| 242 |
{ |
| 243 |
mService->RequestBiDiStreaming(&mContext, &mStream, mCQ, mCQ, this); |
| 244 |
mContext.AsyncNotifyWhenDone(new NotifyWhenDone(this)); |
| 245 |
} |
| 246 |
|
| 247 |
void onProcess(bool ok) override |
| 248 |
{ |
| 249 |
if (isDone()) |
| 250 |
return onDone(); |
| 251 |
|
| 252 |
switch (mState) { |
| 253 |
case State::Created: { |
| 254 |
new BiDiStreaming(mCQ, mService); |
| 255 |
if (!ok) |
| 256 |
return onDone(); |
| 257 |
auto md = mContext.client_metadata(); |
| 258 |
if (const auto it = md.find("write-queries"); it == md.cend()) { |
| 259 |
std::cout << "requested 'write-queries' not found in metadata\n"; |
| 260 |
return onDone(); |
| 261 |
} else { |
| 262 |
mWriteQueries = std::stoul({ it->second.data() }); |
| 263 |
} |
| 264 |
if (const auto it = md.find("write-size"); it != md.cend()) |
| 265 |
mWriteSize = std::stoul({ it->second.data() }); |
| 266 |
mState = State::Processing; |
| 267 |
mReadOperation = new ReadOperation(this); |
| 268 |
mWriteOperation = new WriteOperation(this); |
| 269 |
} break; |
| 270 |
case State::Processing: { |
| 271 |
} break; |
| 272 |
case State::Finished: { |
| 273 |
|
| 274 |
} break; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void onDone() override |
| 279 |
{ |
| 280 |
std::cout << "bidistream done\n"; |
| 281 |
delete mReadOperation; |
| 282 |
delete mWriteOperation; |
| 283 |
delete this; |
| 284 |
} |
| 285 |
|
| 286 |
private: |
| 287 |
grpc::ServerContext mContext; |
| 288 |
grpc::ServerCompletionQueue *mCQ; |
| 289 |
BenchmarkService::AsyncService *mService; |
| 290 |
|
| 291 |
grpc::ServerAsyncReaderWriter<Response, Request> mStream; |
| 292 |
|
| 293 |
enum class State { Created, Processing, Finished }; |
| 294 |
State mState = State::Created; |
| 295 |
|
| 296 |
class ReadOperation final : public RpcJob |
| 297 |
{ |
| 298 |
public: |
| 299 |
explicit ReadOperation(BiDiStreaming *parent) : mParent(parent) |
| 300 |
{ |
| 301 |
mParent->mStream.Read(&mRequest, this); |
| 302 |
} |
| 303 |
|
| 304 |
~ReadOperation() override = default; |
| 305 |
|
| 306 |
void onProcess(bool ok) override |
| 307 |
{ |
| 308 |
if (mParent->mState != State::Processing) |
| 309 |
return; |
| 310 |
|
| 311 |
if (!ok) { |
| 312 |
mParent->mReadDone = true; |
| 313 |
if (mParent->mWriteDone) { |
| 314 |
mParent->mState = State::Finished; |
| 315 |
mParent->mStream.Finish(grpc::Status::OK, mParent); |
| 316 |
} |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
mParent->mStream.Read(&mRequest, this); |
| 321 |
} |
| 322 |
|
| 323 |
void onDone() override { delete this; } |
| 324 |
|
| 325 |
private: |
| 326 |
BiDiStreaming *mParent; |
| 327 |
Request mRequest; |
| 328 |
}; |
| 329 |
|
| 330 |
class WriteOperation final : public RpcJob |
| 331 |
{ |
| 332 |
public: |
| 333 |
explicit WriteOperation(BiDiStreaming *parent) : mParent(parent) |
| 334 |
{ |
| 335 |
if (mParent->mWriteSize > 0) |
| 336 |
mResponse.set_payload(std::string(mParent->mWriteSize, 'x')); |
| 337 |
mParent->mStream.Write(mResponse, this); |
| 338 |
++mWritten; |
| 339 |
} |
| 340 |
|
| 341 |
~WriteOperation() override = default; |
| 342 |
|
| 343 |
void onProcess(bool ok) override |
| 344 |
{ |
| 345 |
if (mParent->mState == State::Finished) |
| 346 |
return; |
| 347 |
|
| 348 |
if (mWritten >= mParent->mWriteQueries) { |
| 349 |
if (mParent->mReadDone || !ok) { |
| 350 |
mParent->mState = State::Finished; |
| 351 |
mParent->mStream.Finish(grpc::Status::OK, mParent); |
| 352 |
} |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
mParent->mStream.Write(mResponse, this); |
| 357 |
++mWritten; |
| 358 |
} |
| 359 |
|
| 360 |
void onDone() override { delete this; } |
| 361 |
|
| 362 |
private: |
| 363 |
BiDiStreaming *mParent; |
| 364 |
Response mResponse; |
| 365 |
size_t mWritten = 0; |
| 366 |
}; |
| 367 |
|
| 368 |
friend class ReadOperation; |
| 369 |
friend class WriteOperation; |
| 370 |
ReadOperation *mReadOperation{}; |
| 371 |
WriteOperation *mWriteOperation{}; |
| 372 |
size_t mWriteQueries = 0; |
| 373 |
size_t mWriteSize = 0; |
| 374 |
std::atomic<bool> mReadDone = false; |
| 375 |
std::atomic<bool> mWriteDone = false; |
| 376 |
}; |
| 377 |
|
| 378 |
#endif |