server.cc 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. //
  2. // Copyright 2015-2016 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include <grpc/support/port_platform.h>
  17. #include "src/core/lib/surface/server.h"
  18. #include <limits.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <algorithm>
  22. #include <atomic>
  23. #include <iterator>
  24. #include <list>
  25. #include <queue>
  26. #include <utility>
  27. #include <vector>
  28. #include "absl/memory/memory.h"
  29. #include "absl/types/optional.h"
  30. #include <grpc/support/alloc.h>
  31. #include <grpc/support/log.h>
  32. #include <grpc/support/string_util.h>
  33. #include "src/core/lib/channel/channel_args.h"
  34. #include "src/core/lib/channel/channelz.h"
  35. #include "src/core/lib/channel/connected_channel.h"
  36. #include "src/core/lib/debug/stats.h"
  37. #include "src/core/lib/gpr/spinlock.h"
  38. #include "src/core/lib/gpr/string.h"
  39. #include "src/core/lib/gprpp/mpscq.h"
  40. #include "src/core/lib/iomgr/executor.h"
  41. #include "src/core/lib/iomgr/iomgr.h"
  42. #include "src/core/lib/slice/slice_internal.h"
  43. #include "src/core/lib/surface/api_trace.h"
  44. #include "src/core/lib/surface/call.h"
  45. #include "src/core/lib/surface/channel.h"
  46. #include "src/core/lib/surface/completion_queue.h"
  47. #include "src/core/lib/surface/init.h"
  48. #include "src/core/lib/transport/metadata.h"
  49. #include "src/core/lib/transport/static_metadata.h"
  50. namespace grpc_core {
  51. TraceFlag grpc_server_channel_trace(false, "server_channel");
  52. //
  53. // Server::RequestedCall
  54. //
  55. struct Server::RequestedCall {
  56. enum class Type { BATCH_CALL, REGISTERED_CALL };
  57. RequestedCall(void* tag_arg, grpc_completion_queue* call_cq,
  58. grpc_call** call_arg, grpc_metadata_array* initial_md,
  59. grpc_call_details* details)
  60. : type(Type::BATCH_CALL),
  61. tag(tag_arg),
  62. cq_bound_to_call(call_cq),
  63. call(call_arg),
  64. initial_metadata(initial_md) {
  65. details->reserved = nullptr;
  66. data.batch.details = details;
  67. }
  68. RequestedCall(void* tag_arg, grpc_completion_queue* call_cq,
  69. grpc_call** call_arg, grpc_metadata_array* initial_md,
  70. RegisteredMethod* rm, gpr_timespec* deadline,
  71. grpc_byte_buffer** optional_payload)
  72. : type(Type::REGISTERED_CALL),
  73. tag(tag_arg),
  74. cq_bound_to_call(call_cq),
  75. call(call_arg),
  76. initial_metadata(initial_md) {
  77. data.registered.method = rm;
  78. data.registered.deadline = deadline;
  79. data.registered.optional_payload = optional_payload;
  80. }
  81. MultiProducerSingleConsumerQueue::Node mpscq_node;
  82. const Type type;
  83. void* const tag;
  84. grpc_completion_queue* const cq_bound_to_call;
  85. grpc_call** const call;
  86. grpc_cq_completion completion;
  87. grpc_metadata_array* const initial_metadata;
  88. union {
  89. struct {
  90. grpc_call_details* details;
  91. } batch;
  92. struct {
  93. RegisteredMethod* method;
  94. gpr_timespec* deadline;
  95. grpc_byte_buffer** optional_payload;
  96. } registered;
  97. } data;
  98. };
  99. //
  100. // Server::RegisteredMethod
  101. //
  102. struct Server::RegisteredMethod {
  103. RegisteredMethod(
  104. const char* method_arg, const char* host_arg,
  105. grpc_server_register_method_payload_handling payload_handling_arg,
  106. uint32_t flags_arg)
  107. : method(method_arg == nullptr ? "" : method_arg),
  108. host(host_arg == nullptr ? "" : host_arg),
  109. payload_handling(payload_handling_arg),
  110. flags(flags_arg) {}
  111. ~RegisteredMethod() = default;
  112. const std::string method;
  113. const std::string host;
  114. const grpc_server_register_method_payload_handling payload_handling;
  115. const uint32_t flags;
  116. // One request matcher per method.
  117. std::unique_ptr<RequestMatcherInterface> matcher;
  118. };
  119. //
  120. // Server::RequestMatcherInterface
  121. //
  122. // RPCs that come in from the transport must be matched against RPC requests
  123. // from the application. An incoming request from the application can be matched
  124. // to an RPC that has already arrived or can be queued up for later use.
  125. // Likewise, an RPC coming in from the transport can either be matched to a
  126. // request that already arrived from the application or can be queued up for
  127. // later use (marked pending). If there is a match, the request's tag is posted
  128. // on the request's notification CQ.
  129. //
  130. // RequestMatcherInterface is the base class to provide this functionality.
  131. class Server::RequestMatcherInterface {
  132. public:
  133. virtual ~RequestMatcherInterface() {}
  134. // Unref the calls associated with any incoming RPCs in the pending queue (not
  135. // yet matched to an application-requested RPC).
  136. virtual void ZombifyPending() = 0;
  137. // Mark all application-requested RPCs failed if they have not been matched to
  138. // an incoming RPC. The error parameter indicates why the RPCs are being
  139. // failed (always server shutdown in all current implementations).
  140. virtual void KillRequests(grpc_error* error) = 0;
  141. // How many request queues are supported by this matcher. This is an abstract
  142. // concept that essentially maps to gRPC completion queues.
  143. virtual size_t request_queue_count() const = 0;
  144. // This function is invoked when the application requests a new RPC whose
  145. // information is in the call parameter. The request_queue_index marks the
  146. // queue onto which to place this RPC, and is typically associated with a gRPC
  147. // CQ. If there are pending RPCs waiting to be matched, publish one (match it
  148. // and notify the CQ).
  149. virtual void RequestCallWithPossiblePublish(size_t request_queue_index,
  150. RequestedCall* call) = 0;
  151. // This function is invoked on an incoming RPC, represented by the calld
  152. // object. The RequestMatcher will try to match it against an
  153. // application-requested RPC if possible or will place it in the pending queue
  154. // otherwise. To enable some measure of fairness between server CQs, the match
  155. // is done starting at the start_request_queue_index parameter in a cyclic
  156. // order rather than always starting at 0.
  157. virtual void MatchOrQueue(size_t start_request_queue_index,
  158. CallData* calld) = 0;
  159. // Returns the server associated with this request matcher
  160. virtual Server* server() const = 0;
  161. };
  162. // The RealRequestMatcher is an implementation of RequestMatcherInterface that
  163. // actually uses all the features of RequestMatcherInterface: expecting the
  164. // application to explicitly request RPCs and then matching those to incoming
  165. // RPCs, along with a slow path by which incoming RPCs are put on a locked
  166. // pending list if they aren't able to be matched to an application request.
  167. class Server::RealRequestMatcher : public RequestMatcherInterface {
  168. public:
  169. explicit RealRequestMatcher(Server* server)
  170. : server_(server), requests_per_cq_(server->cqs_.size()) {}
  171. ~RealRequestMatcher() override {
  172. for (LockedMultiProducerSingleConsumerQueue& queue : requests_per_cq_) {
  173. GPR_ASSERT(queue.Pop() == nullptr);
  174. }
  175. }
  176. void ZombifyPending() override {
  177. while (!pending_.empty()) {
  178. CallData* calld = pending_.front();
  179. calld->SetState(CallData::CallState::ZOMBIED);
  180. calld->KillZombie();
  181. pending_.pop();
  182. }
  183. }
  184. void KillRequests(grpc_error* error) override {
  185. for (size_t i = 0; i < requests_per_cq_.size(); i++) {
  186. RequestedCall* rc;
  187. while ((rc = reinterpret_cast<RequestedCall*>(
  188. requests_per_cq_[i].Pop())) != nullptr) {
  189. server_->FailCall(i, rc, GRPC_ERROR_REF(error));
  190. }
  191. }
  192. GRPC_ERROR_UNREF(error);
  193. }
  194. size_t request_queue_count() const override {
  195. return requests_per_cq_.size();
  196. }
  197. void RequestCallWithPossiblePublish(size_t request_queue_index,
  198. RequestedCall* call) override {
  199. if (requests_per_cq_[request_queue_index].Push(&call->mpscq_node)) {
  200. /* this was the first queued request: we need to lock and start
  201. matching calls */
  202. struct PendingCall {
  203. RequestedCall* rc = nullptr;
  204. CallData* calld;
  205. };
  206. auto pop_next_pending = [this, request_queue_index] {
  207. PendingCall pending_call;
  208. {
  209. MutexLock lock(&server_->mu_call_);
  210. if (!pending_.empty()) {
  211. pending_call.rc = reinterpret_cast<RequestedCall*>(
  212. requests_per_cq_[request_queue_index].Pop());
  213. if (pending_call.rc != nullptr) {
  214. pending_call.calld = pending_.front();
  215. pending_.pop();
  216. }
  217. }
  218. }
  219. return pending_call;
  220. };
  221. while (true) {
  222. PendingCall next_pending = pop_next_pending();
  223. if (next_pending.rc == nullptr) break;
  224. if (!next_pending.calld->MaybeActivate()) {
  225. // Zombied Call
  226. next_pending.calld->KillZombie();
  227. } else {
  228. next_pending.calld->Publish(request_queue_index, next_pending.rc);
  229. }
  230. }
  231. }
  232. }
  233. void MatchOrQueue(size_t start_request_queue_index,
  234. CallData* calld) override {
  235. for (size_t i = 0; i < requests_per_cq_.size(); i++) {
  236. size_t cq_idx = (start_request_queue_index + i) % requests_per_cq_.size();
  237. RequestedCall* rc =
  238. reinterpret_cast<RequestedCall*>(requests_per_cq_[cq_idx].TryPop());
  239. if (rc != nullptr) {
  240. GRPC_STATS_INC_SERVER_CQS_CHECKED(i);
  241. calld->SetState(CallData::CallState::ACTIVATED);
  242. calld->Publish(cq_idx, rc);
  243. return;
  244. }
  245. }
  246. // No cq to take the request found; queue it on the slow list.
  247. GRPC_STATS_INC_SERVER_SLOWPATH_REQUESTS_QUEUED();
  248. // We need to ensure that all the queues are empty. We do this under
  249. // the server mu_call_ lock to ensure that if something is added to
  250. // an empty request queue, it will block until the call is actually
  251. // added to the pending list.
  252. RequestedCall* rc = nullptr;
  253. size_t cq_idx = 0;
  254. size_t loop_count;
  255. {
  256. MutexLock lock(&server_->mu_call_);
  257. for (loop_count = 0; loop_count < requests_per_cq_.size(); loop_count++) {
  258. cq_idx =
  259. (start_request_queue_index + loop_count) % requests_per_cq_.size();
  260. rc = reinterpret_cast<RequestedCall*>(requests_per_cq_[cq_idx].Pop());
  261. if (rc != nullptr) {
  262. break;
  263. }
  264. }
  265. if (rc == nullptr) {
  266. calld->SetState(CallData::CallState::PENDING);
  267. pending_.push(calld);
  268. return;
  269. }
  270. }
  271. GRPC_STATS_INC_SERVER_CQS_CHECKED(loop_count + requests_per_cq_.size());
  272. calld->SetState(CallData::CallState::ACTIVATED);
  273. calld->Publish(cq_idx, rc);
  274. }
  275. Server* server() const override { return server_; }
  276. private:
  277. Server* const server_;
  278. std::queue<CallData*> pending_;
  279. std::vector<LockedMultiProducerSingleConsumerQueue> requests_per_cq_;
  280. };
  281. // AllocatingRequestMatchers don't allow the application to request an RPC in
  282. // advance or queue up any incoming RPC for later match. Instead, MatchOrQueue
  283. // will call out to an allocation function passed in at the construction of the
  284. // object. These request matchers are designed for the C++ callback API, so they
  285. // only support 1 completion queue (passed in at the constructor).
  286. class Server::AllocatingRequestMatcherBase : public RequestMatcherInterface {
  287. public:
  288. AllocatingRequestMatcherBase(Server* server, grpc_completion_queue* cq)
  289. : server_(server), cq_(cq) {
  290. size_t idx;
  291. for (idx = 0; idx < server->cqs_.size(); idx++) {
  292. if (server->cqs_[idx] == cq) {
  293. break;
  294. }
  295. }
  296. GPR_ASSERT(idx < server->cqs_.size());
  297. cq_idx_ = idx;
  298. }
  299. void ZombifyPending() override {}
  300. void KillRequests(grpc_error* error) override { GRPC_ERROR_UNREF(error); }
  301. size_t request_queue_count() const override { return 0; }
  302. void RequestCallWithPossiblePublish(size_t /*request_queue_index*/,
  303. RequestedCall* /*call*/) final {
  304. GPR_ASSERT(false);
  305. }
  306. Server* server() const override { return server_; }
  307. // Supply the completion queue related to this request matcher
  308. grpc_completion_queue* cq() const { return cq_; }
  309. // Supply the completion queue's index relative to the server.
  310. size_t cq_idx() const { return cq_idx_; }
  311. private:
  312. Server* const server_;
  313. grpc_completion_queue* const cq_;
  314. size_t cq_idx_;
  315. };
  316. // An allocating request matcher for non-registered methods (used for generic
  317. // API and unimplemented RPCs).
  318. class Server::AllocatingRequestMatcherBatch
  319. : public AllocatingRequestMatcherBase {
  320. public:
  321. AllocatingRequestMatcherBatch(Server* server, grpc_completion_queue* cq,
  322. std::function<BatchCallAllocation()> allocator)
  323. : AllocatingRequestMatcherBase(server, cq),
  324. allocator_(std::move(allocator)) {}
  325. void MatchOrQueue(size_t /*start_request_queue_index*/,
  326. CallData* calld) override {
  327. BatchCallAllocation call_info = allocator_();
  328. GPR_ASSERT(server()->ValidateServerRequest(
  329. cq(), static_cast<void*>(call_info.tag), nullptr, nullptr) ==
  330. GRPC_CALL_OK);
  331. RequestedCall* rc = new RequestedCall(
  332. static_cast<void*>(call_info.tag), call_info.cq, call_info.call,
  333. call_info.initial_metadata, call_info.details);
  334. calld->SetState(CallData::CallState::ACTIVATED);
  335. calld->Publish(cq_idx(), rc);
  336. }
  337. private:
  338. std::function<BatchCallAllocation()> allocator_;
  339. };
  340. // An allocating request matcher for registered methods.
  341. class Server::AllocatingRequestMatcherRegistered
  342. : public AllocatingRequestMatcherBase {
  343. public:
  344. AllocatingRequestMatcherRegistered(
  345. Server* server, grpc_completion_queue* cq, RegisteredMethod* rm,
  346. std::function<RegisteredCallAllocation()> allocator)
  347. : AllocatingRequestMatcherBase(server, cq),
  348. registered_method_(rm),
  349. allocator_(std::move(allocator)) {}
  350. void MatchOrQueue(size_t /*start_request_queue_index*/,
  351. CallData* calld) override {
  352. RegisteredCallAllocation call_info = allocator_();
  353. GPR_ASSERT(server()->ValidateServerRequest(
  354. cq(), call_info.tag, call_info.optional_payload,
  355. registered_method_) == GRPC_CALL_OK);
  356. RequestedCall* rc = new RequestedCall(
  357. call_info.tag, call_info.cq, call_info.call, call_info.initial_metadata,
  358. registered_method_, call_info.deadline, call_info.optional_payload);
  359. calld->SetState(CallData::CallState::ACTIVATED);
  360. calld->Publish(cq_idx(), rc);
  361. }
  362. private:
  363. RegisteredMethod* const registered_method_;
  364. std::function<RegisteredCallAllocation()> allocator_;
  365. };
  366. //
  367. // ChannelBroadcaster
  368. //
  369. namespace {
  370. class ChannelBroadcaster {
  371. public:
  372. // This can have an empty constructor and destructor since we want to control
  373. // when the actual setup and shutdown broadcast take place.
  374. // Copies over the channels from the locked server.
  375. void FillChannelsLocked(std::vector<grpc_channel*> channels) {
  376. GPR_DEBUG_ASSERT(channels_.empty());
  377. channels_ = std::move(channels);
  378. }
  379. // Broadcasts a shutdown on each channel.
  380. void BroadcastShutdown(bool send_goaway, grpc_error* force_disconnect) {
  381. for (grpc_channel* channel : channels_) {
  382. SendShutdown(channel, send_goaway, GRPC_ERROR_REF(force_disconnect));
  383. GRPC_CHANNEL_INTERNAL_UNREF(channel, "broadcast");
  384. }
  385. channels_.clear(); // just for safety against double broadcast
  386. GRPC_ERROR_UNREF(force_disconnect);
  387. }
  388. private:
  389. struct ShutdownCleanupArgs {
  390. grpc_closure closure;
  391. grpc_slice slice;
  392. };
  393. static void ShutdownCleanup(void* arg, grpc_error* /*error*/) {
  394. ShutdownCleanupArgs* a = static_cast<ShutdownCleanupArgs*>(arg);
  395. grpc_slice_unref_internal(a->slice);
  396. delete a;
  397. }
  398. static void SendShutdown(grpc_channel* channel, bool send_goaway,
  399. grpc_error* send_disconnect) {
  400. ShutdownCleanupArgs* sc = new ShutdownCleanupArgs;
  401. GRPC_CLOSURE_INIT(&sc->closure, ShutdownCleanup, sc,
  402. grpc_schedule_on_exec_ctx);
  403. grpc_transport_op* op = grpc_make_transport_op(&sc->closure);
  404. grpc_channel_element* elem;
  405. op->goaway_error =
  406. send_goaway
  407. ? grpc_error_set_int(
  408. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Server shutdown"),
  409. GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_OK)
  410. : GRPC_ERROR_NONE;
  411. op->set_accept_stream = true;
  412. sc->slice = grpc_slice_from_copied_string("Server shutdown");
  413. op->disconnect_with_error = send_disconnect;
  414. elem =
  415. grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
  416. elem->filter->start_transport_op(elem, op);
  417. }
  418. std::vector<grpc_channel*> channels_;
  419. };
  420. } // namespace
  421. //
  422. // Server
  423. //
  424. const grpc_channel_filter Server::kServerTopFilter = {
  425. Server::CallData::StartTransportStreamOpBatch,
  426. grpc_channel_next_op,
  427. sizeof(Server::CallData),
  428. Server::CallData::InitCallElement,
  429. grpc_call_stack_ignore_set_pollset_or_pollset_set,
  430. Server::CallData::DestroyCallElement,
  431. sizeof(Server::ChannelData),
  432. Server::ChannelData::InitChannelElement,
  433. Server::ChannelData::DestroyChannelElement,
  434. grpc_channel_next_get_info,
  435. "server",
  436. };
  437. namespace {
  438. grpc_resource_user* CreateDefaultResourceUser(const grpc_channel_args* args) {
  439. if (args != nullptr) {
  440. grpc_resource_quota* resource_quota =
  441. grpc_resource_quota_from_channel_args(args, false /* create */);
  442. if (resource_quota != nullptr) {
  443. return grpc_resource_user_create(resource_quota, "default");
  444. }
  445. }
  446. return nullptr;
  447. }
  448. RefCountedPtr<channelz::ServerNode> CreateChannelzNode(
  449. const grpc_channel_args* args) {
  450. RefCountedPtr<channelz::ServerNode> channelz_node;
  451. if (grpc_channel_args_find_bool(args, GRPC_ARG_ENABLE_CHANNELZ,
  452. GRPC_ENABLE_CHANNELZ_DEFAULT)) {
  453. size_t channel_tracer_max_memory = grpc_channel_args_find_integer(
  454. args, GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE,
  455. {GRPC_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE_DEFAULT, 0, INT_MAX});
  456. channelz_node =
  457. MakeRefCounted<channelz::ServerNode>(channel_tracer_max_memory);
  458. channelz_node->AddTraceEvent(
  459. channelz::ChannelTrace::Severity::Info,
  460. grpc_slice_from_static_string("Server created"));
  461. }
  462. return channelz_node;
  463. }
  464. } // namespace
  465. Server::Server(const grpc_channel_args* args)
  466. : channel_args_(grpc_channel_args_copy(args)),
  467. default_resource_user_(CreateDefaultResourceUser(args)),
  468. channelz_node_(CreateChannelzNode(args)) {}
  469. Server::~Server() {
  470. grpc_channel_args_destroy(channel_args_);
  471. // Remove the cq pollsets from the config_fetcher.
  472. if (started_ && config_fetcher_ != nullptr &&
  473. config_fetcher_->interested_parties() != nullptr) {
  474. for (grpc_pollset* pollset : pollsets_) {
  475. grpc_pollset_set_del_pollset(config_fetcher_->interested_parties(),
  476. pollset);
  477. }
  478. }
  479. for (size_t i = 0; i < cqs_.size(); i++) {
  480. GRPC_CQ_INTERNAL_UNREF(cqs_[i], "server");
  481. }
  482. }
  483. void Server::AddListener(OrphanablePtr<ListenerInterface> listener) {
  484. channelz::ListenSocketNode* listen_socket_node =
  485. listener->channelz_listen_socket_node();
  486. if (listen_socket_node != nullptr && channelz_node_ != nullptr) {
  487. channelz_node_->AddChildListenSocket(listen_socket_node->Ref());
  488. }
  489. listeners_.emplace_back(std::move(listener));
  490. }
  491. void Server::Start() {
  492. started_ = true;
  493. for (grpc_completion_queue* cq : cqs_) {
  494. if (grpc_cq_can_listen(cq)) {
  495. pollsets_.push_back(grpc_cq_pollset(cq));
  496. }
  497. }
  498. if (unregistered_request_matcher_ == nullptr) {
  499. unregistered_request_matcher_ = absl::make_unique<RealRequestMatcher>(this);
  500. }
  501. for (std::unique_ptr<RegisteredMethod>& rm : registered_methods_) {
  502. if (rm->matcher == nullptr) {
  503. rm->matcher = absl::make_unique<RealRequestMatcher>(this);
  504. }
  505. }
  506. {
  507. MutexLock lock(&mu_global_);
  508. starting_ = true;
  509. }
  510. // Register the interested parties from the config fetcher to the cq pollsets
  511. // before starting listeners so that config fetcher is being polled when the
  512. // listeners start watch the fetcher.
  513. if (config_fetcher_ != nullptr &&
  514. config_fetcher_->interested_parties() != nullptr) {
  515. for (grpc_pollset* pollset : pollsets_) {
  516. grpc_pollset_set_add_pollset(config_fetcher_->interested_parties(),
  517. pollset);
  518. }
  519. }
  520. for (auto& listener : listeners_) {
  521. listener.listener->Start(this, &pollsets_);
  522. }
  523. MutexLock lock(&mu_global_);
  524. starting_ = false;
  525. starting_cv_.Signal();
  526. }
  527. grpc_error* Server::SetupTransport(
  528. grpc_transport* transport, grpc_pollset* accepting_pollset,
  529. const grpc_channel_args* args,
  530. const RefCountedPtr<grpc_core::channelz::SocketNode>& socket_node,
  531. grpc_resource_user* resource_user) {
  532. // Create channel.
  533. grpc_error* error = GRPC_ERROR_NONE;
  534. grpc_channel* channel = grpc_channel_create(
  535. nullptr, args, GRPC_SERVER_CHANNEL, transport, resource_user, &error);
  536. if (channel == nullptr) {
  537. return error;
  538. }
  539. ChannelData* chand = static_cast<ChannelData*>(
  540. grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0)
  541. ->channel_data);
  542. // Set up CQs.
  543. size_t cq_idx;
  544. for (cq_idx = 0; cq_idx < cqs_.size(); cq_idx++) {
  545. if (grpc_cq_pollset(cqs_[cq_idx]) == accepting_pollset) break;
  546. }
  547. if (cq_idx == cqs_.size()) {
  548. // Completion queue not found. Pick a random one to publish new calls to.
  549. cq_idx = static_cast<size_t>(rand()) % cqs_.size();
  550. }
  551. // Set up channelz node.
  552. intptr_t channelz_socket_uuid = 0;
  553. if (socket_node != nullptr) {
  554. channelz_socket_uuid = socket_node->uuid();
  555. channelz_node_->AddChildSocket(socket_node);
  556. }
  557. // Initialize chand.
  558. chand->InitTransport(Ref(), channel, cq_idx, transport, channelz_socket_uuid);
  559. return GRPC_ERROR_NONE;
  560. }
  561. bool Server::HasOpenConnections() {
  562. MutexLock lock(&mu_global_);
  563. return !channels_.empty();
  564. }
  565. void Server::SetRegisteredMethodAllocator(
  566. grpc_completion_queue* cq, void* method_tag,
  567. std::function<RegisteredCallAllocation()> allocator) {
  568. RegisteredMethod* rm = static_cast<RegisteredMethod*>(method_tag);
  569. rm->matcher = absl::make_unique<AllocatingRequestMatcherRegistered>(
  570. this, cq, rm, std::move(allocator));
  571. }
  572. void Server::SetBatchMethodAllocator(
  573. grpc_completion_queue* cq, std::function<BatchCallAllocation()> allocator) {
  574. GPR_DEBUG_ASSERT(unregistered_request_matcher_ == nullptr);
  575. unregistered_request_matcher_ =
  576. absl::make_unique<AllocatingRequestMatcherBatch>(this, cq,
  577. std::move(allocator));
  578. }
  579. void Server::RegisterCompletionQueue(grpc_completion_queue* cq) {
  580. for (grpc_completion_queue* queue : cqs_) {
  581. if (queue == cq) return;
  582. }
  583. GRPC_CQ_INTERNAL_REF(cq, "server");
  584. cqs_.push_back(cq);
  585. }
  586. namespace {
  587. bool streq(const std::string& a, const char* b) {
  588. return (a.empty() && b == nullptr) ||
  589. ((b != nullptr) && !strcmp(a.c_str(), b));
  590. }
  591. } // namespace
  592. Server::RegisteredMethod* Server::RegisterMethod(
  593. const char* method, const char* host,
  594. grpc_server_register_method_payload_handling payload_handling,
  595. uint32_t flags) {
  596. if (!method) {
  597. gpr_log(GPR_ERROR,
  598. "grpc_server_register_method method string cannot be NULL");
  599. return nullptr;
  600. }
  601. for (std::unique_ptr<RegisteredMethod>& m : registered_methods_) {
  602. if (streq(m->method, method) && streq(m->host, host)) {
  603. gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method,
  604. host ? host : "*");
  605. return nullptr;
  606. }
  607. }
  608. if ((flags & ~GRPC_INITIAL_METADATA_USED_MASK) != 0) {
  609. gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x",
  610. flags);
  611. return nullptr;
  612. }
  613. registered_methods_.emplace_back(absl::make_unique<RegisteredMethod>(
  614. method, host, payload_handling, flags));
  615. return registered_methods_.back().get();
  616. }
  617. void Server::DoneRequestEvent(void* req, grpc_cq_completion* /*c*/) {
  618. delete static_cast<RequestedCall*>(req);
  619. }
  620. void Server::FailCall(size_t cq_idx, RequestedCall* rc, grpc_error* error) {
  621. *rc->call = nullptr;
  622. rc->initial_metadata->count = 0;
  623. GPR_ASSERT(error != GRPC_ERROR_NONE);
  624. grpc_cq_end_op(cqs_[cq_idx], rc->tag, error, DoneRequestEvent, rc,
  625. &rc->completion);
  626. }
  627. // Before calling MaybeFinishShutdown(), we must hold mu_global_ and not
  628. // hold mu_call_.
  629. void Server::MaybeFinishShutdown() {
  630. if (!shutdown_flag_.load(std::memory_order_acquire) || shutdown_published_) {
  631. return;
  632. }
  633. {
  634. MutexLock lock(&mu_call_);
  635. KillPendingWorkLocked(
  636. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Server Shutdown"));
  637. }
  638. if (!channels_.empty() || listeners_destroyed_ < listeners_.size()) {
  639. if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME),
  640. last_shutdown_message_time_),
  641. gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
  642. last_shutdown_message_time_ = gpr_now(GPR_CLOCK_REALTIME);
  643. gpr_log(GPR_DEBUG,
  644. "Waiting for %" PRIuPTR " channels and %" PRIuPTR "/%" PRIuPTR
  645. " listeners to be destroyed before shutting down server",
  646. channels_.size(), listeners_.size() - listeners_destroyed_,
  647. listeners_.size());
  648. }
  649. return;
  650. }
  651. shutdown_published_ = true;
  652. for (auto& shutdown_tag : shutdown_tags_) {
  653. Ref().release();
  654. grpc_cq_end_op(shutdown_tag.cq, shutdown_tag.tag, GRPC_ERROR_NONE,
  655. DoneShutdownEvent, this, &shutdown_tag.completion);
  656. }
  657. }
  658. void Server::KillPendingWorkLocked(grpc_error* error) {
  659. if (started_) {
  660. unregistered_request_matcher_->KillRequests(GRPC_ERROR_REF(error));
  661. unregistered_request_matcher_->ZombifyPending();
  662. for (std::unique_ptr<RegisteredMethod>& rm : registered_methods_) {
  663. rm->matcher->KillRequests(GRPC_ERROR_REF(error));
  664. rm->matcher->ZombifyPending();
  665. }
  666. }
  667. GRPC_ERROR_UNREF(error);
  668. }
  669. std::vector<grpc_channel*> Server::GetChannelsLocked() const {
  670. std::vector<grpc_channel*> channels;
  671. channels.reserve(channels_.size());
  672. for (const ChannelData* chand : channels_) {
  673. channels.push_back(chand->channel());
  674. GRPC_CHANNEL_INTERNAL_REF(chand->channel(), "broadcast");
  675. }
  676. return channels;
  677. }
  678. void Server::ListenerDestroyDone(void* arg, grpc_error* /*error*/) {
  679. Server* server = static_cast<Server*>(arg);
  680. MutexLock lock(&server->mu_global_);
  681. server->listeners_destroyed_++;
  682. server->MaybeFinishShutdown();
  683. }
  684. namespace {
  685. void DonePublishedShutdown(void* /*done_arg*/, grpc_cq_completion* storage) {
  686. delete storage;
  687. }
  688. } // namespace
  689. // - Kills all pending requests-for-incoming-RPC-calls (i.e., the requests made
  690. // via grpc_server_request_call() and grpc_server_request_registered_call()
  691. // will now be cancelled). See KillPendingWorkLocked().
  692. //
  693. // - Shuts down the listeners (i.e., the server will no longer listen on the
  694. // port for new incoming channels).
  695. //
  696. // - Iterates through all channels on the server and sends shutdown msg (see
  697. // ChannelBroadcaster::BroadcastShutdown() for details) to the clients via
  698. // the transport layer. The transport layer then guarantees the following:
  699. // -- Sends shutdown to the client (e.g., HTTP2 transport sends GOAWAY).
  700. // -- If the server has outstanding calls that are in the process, the
  701. // connection is NOT closed until the server is done with all those calls.
  702. // -- Once there are no more calls in progress, the channel is closed.
  703. void Server::ShutdownAndNotify(grpc_completion_queue* cq, void* tag) {
  704. ChannelBroadcaster broadcaster;
  705. {
  706. // Wait for startup to be finished. Locks mu_global.
  707. MutexLock lock(&mu_global_);
  708. WaitUntil(&starting_cv_, &mu_global_, [this] { return !starting_; });
  709. // Stay locked, and gather up some stuff to do.
  710. GPR_ASSERT(grpc_cq_begin_op(cq, tag));
  711. if (shutdown_published_) {
  712. grpc_cq_end_op(cq, tag, GRPC_ERROR_NONE, DonePublishedShutdown, nullptr,
  713. new grpc_cq_completion);
  714. return;
  715. }
  716. shutdown_tags_.emplace_back(tag, cq);
  717. if (shutdown_flag_.load(std::memory_order_acquire)) {
  718. return;
  719. }
  720. last_shutdown_message_time_ = gpr_now(GPR_CLOCK_REALTIME);
  721. broadcaster.FillChannelsLocked(GetChannelsLocked());
  722. shutdown_flag_.store(true, std::memory_order_release);
  723. // Collect all unregistered then registered calls.
  724. {
  725. MutexLock lock(&mu_call_);
  726. KillPendingWorkLocked(
  727. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Server Shutdown"));
  728. }
  729. MaybeFinishShutdown();
  730. }
  731. // Shutdown listeners.
  732. for (auto& listener : listeners_) {
  733. channelz::ListenSocketNode* channelz_listen_socket_node =
  734. listener.listener->channelz_listen_socket_node();
  735. if (channelz_node_ != nullptr && channelz_listen_socket_node != nullptr) {
  736. channelz_node_->RemoveChildListenSocket(
  737. channelz_listen_socket_node->uuid());
  738. }
  739. GRPC_CLOSURE_INIT(&listener.destroy_done, ListenerDestroyDone, this,
  740. grpc_schedule_on_exec_ctx);
  741. listener.listener->SetOnDestroyDone(&listener.destroy_done);
  742. listener.listener.reset();
  743. }
  744. broadcaster.BroadcastShutdown(/*send_goaway=*/true, GRPC_ERROR_NONE);
  745. }
  746. void Server::CancelAllCalls() {
  747. ChannelBroadcaster broadcaster;
  748. {
  749. MutexLock lock(&mu_global_);
  750. broadcaster.FillChannelsLocked(GetChannelsLocked());
  751. }
  752. broadcaster.BroadcastShutdown(
  753. /*send_goaway=*/false,
  754. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Cancelling all calls"));
  755. }
  756. void Server::Orphan() {
  757. {
  758. MutexLock lock(&mu_global_);
  759. GPR_ASSERT(shutdown_flag_.load(std::memory_order_acquire) ||
  760. listeners_.empty());
  761. GPR_ASSERT(listeners_destroyed_ == listeners_.size());
  762. }
  763. if (default_resource_user_ != nullptr) {
  764. grpc_resource_quota_unref(grpc_resource_user_quota(default_resource_user_));
  765. grpc_resource_user_shutdown(default_resource_user_);
  766. grpc_resource_user_unref(default_resource_user_);
  767. }
  768. Unref();
  769. }
  770. grpc_call_error Server::ValidateServerRequest(
  771. grpc_completion_queue* cq_for_notification, void* tag,
  772. grpc_byte_buffer** optional_payload, RegisteredMethod* rm) {
  773. if ((rm == nullptr && optional_payload != nullptr) ||
  774. ((rm != nullptr) && ((optional_payload == nullptr) !=
  775. (rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)))) {
  776. return GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH;
  777. }
  778. if (grpc_cq_begin_op(cq_for_notification, tag) == false) {
  779. return GRPC_CALL_ERROR_COMPLETION_QUEUE_SHUTDOWN;
  780. }
  781. return GRPC_CALL_OK;
  782. }
  783. grpc_call_error Server::ValidateServerRequestAndCq(
  784. size_t* cq_idx, grpc_completion_queue* cq_for_notification, void* tag,
  785. grpc_byte_buffer** optional_payload, RegisteredMethod* rm) {
  786. size_t idx;
  787. for (idx = 0; idx < cqs_.size(); idx++) {
  788. if (cqs_[idx] == cq_for_notification) {
  789. break;
  790. }
  791. }
  792. if (idx == cqs_.size()) {
  793. return GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE;
  794. }
  795. grpc_call_error error =
  796. ValidateServerRequest(cq_for_notification, tag, optional_payload, rm);
  797. if (error != GRPC_CALL_OK) {
  798. return error;
  799. }
  800. *cq_idx = idx;
  801. return GRPC_CALL_OK;
  802. }
  803. grpc_call_error Server::QueueRequestedCall(size_t cq_idx, RequestedCall* rc) {
  804. if (shutdown_flag_.load(std::memory_order_acquire)) {
  805. FailCall(cq_idx, rc,
  806. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Server Shutdown"));
  807. return GRPC_CALL_OK;
  808. }
  809. RequestMatcherInterface* rm;
  810. switch (rc->type) {
  811. case RequestedCall::Type::BATCH_CALL:
  812. rm = unregistered_request_matcher_.get();
  813. break;
  814. case RequestedCall::Type::REGISTERED_CALL:
  815. rm = rc->data.registered.method->matcher.get();
  816. break;
  817. }
  818. rm->RequestCallWithPossiblePublish(cq_idx, rc);
  819. return GRPC_CALL_OK;
  820. }
  821. grpc_call_error Server::RequestCall(grpc_call** call,
  822. grpc_call_details* details,
  823. grpc_metadata_array* request_metadata,
  824. grpc_completion_queue* cq_bound_to_call,
  825. grpc_completion_queue* cq_for_notification,
  826. void* tag) {
  827. size_t cq_idx;
  828. grpc_call_error error = ValidateServerRequestAndCq(
  829. &cq_idx, cq_for_notification, tag, nullptr, nullptr);
  830. if (error != GRPC_CALL_OK) {
  831. return error;
  832. }
  833. RequestedCall* rc =
  834. new RequestedCall(tag, cq_bound_to_call, call, request_metadata, details);
  835. return QueueRequestedCall(cq_idx, rc);
  836. }
  837. grpc_call_error Server::RequestRegisteredCall(
  838. RegisteredMethod* rm, grpc_call** call, gpr_timespec* deadline,
  839. grpc_metadata_array* request_metadata, grpc_byte_buffer** optional_payload,
  840. grpc_completion_queue* cq_bound_to_call,
  841. grpc_completion_queue* cq_for_notification, void* tag_new) {
  842. size_t cq_idx;
  843. grpc_call_error error = ValidateServerRequestAndCq(
  844. &cq_idx, cq_for_notification, tag_new, optional_payload, rm);
  845. if (error != GRPC_CALL_OK) {
  846. return error;
  847. }
  848. RequestedCall* rc =
  849. new RequestedCall(tag_new, cq_bound_to_call, call, request_metadata, rm,
  850. deadline, optional_payload);
  851. return QueueRequestedCall(cq_idx, rc);
  852. }
  853. //
  854. // Server::ChannelData::ConnectivityWatcher
  855. //
  856. class Server::ChannelData::ConnectivityWatcher
  857. : public AsyncConnectivityStateWatcherInterface {
  858. public:
  859. explicit ConnectivityWatcher(ChannelData* chand) : chand_(chand) {
  860. GRPC_CHANNEL_INTERNAL_REF(chand_->channel_, "connectivity");
  861. }
  862. ~ConnectivityWatcher() override {
  863. GRPC_CHANNEL_INTERNAL_UNREF(chand_->channel_, "connectivity");
  864. }
  865. private:
  866. void OnConnectivityStateChange(grpc_connectivity_state new_state,
  867. const absl::Status& /*status*/) override {
  868. // Don't do anything until we are being shut down.
  869. if (new_state != GRPC_CHANNEL_SHUTDOWN) return;
  870. // Shut down channel.
  871. MutexLock lock(&chand_->server_->mu_global_);
  872. chand_->Destroy();
  873. }
  874. ChannelData* chand_;
  875. };
  876. //
  877. // Server::ChannelData
  878. //
  879. Server::ChannelData::~ChannelData() {
  880. if (registered_methods_ != nullptr) {
  881. for (const ChannelRegisteredMethod& crm : *registered_methods_) {
  882. grpc_slice_unref_internal(crm.method);
  883. GPR_DEBUG_ASSERT(crm.method.refcount == &kNoopRefcount ||
  884. crm.method.refcount == nullptr);
  885. if (crm.has_host) {
  886. grpc_slice_unref_internal(crm.host);
  887. GPR_DEBUG_ASSERT(crm.host.refcount == &kNoopRefcount ||
  888. crm.host.refcount == nullptr);
  889. }
  890. }
  891. registered_methods_.reset();
  892. }
  893. if (server_ != nullptr) {
  894. if (server_->channelz_node_ != nullptr && channelz_socket_uuid_ != 0) {
  895. server_->channelz_node_->RemoveChildSocket(channelz_socket_uuid_);
  896. }
  897. {
  898. MutexLock lock(&server_->mu_global_);
  899. if (list_position_.has_value()) {
  900. server_->channels_.erase(*list_position_);
  901. list_position_.reset();
  902. }
  903. server_->MaybeFinishShutdown();
  904. }
  905. }
  906. }
  907. void Server::ChannelData::InitTransport(RefCountedPtr<Server> server,
  908. grpc_channel* channel, size_t cq_idx,
  909. grpc_transport* transport,
  910. intptr_t channelz_socket_uuid) {
  911. server_ = std::move(server);
  912. channel_ = channel;
  913. cq_idx_ = cq_idx;
  914. channelz_socket_uuid_ = channelz_socket_uuid;
  915. // Build a lookup table phrased in terms of mdstr's in this channels context
  916. // to quickly find registered methods.
  917. size_t num_registered_methods = server_->registered_methods_.size();
  918. if (num_registered_methods > 0) {
  919. uint32_t max_probes = 0;
  920. size_t slots = 2 * num_registered_methods;
  921. registered_methods_ =
  922. absl::make_unique<std::vector<ChannelRegisteredMethod>>(slots);
  923. for (std::unique_ptr<RegisteredMethod>& rm : server_->registered_methods_) {
  924. ExternallyManagedSlice host;
  925. ExternallyManagedSlice method(rm->method.c_str());
  926. const bool has_host = !rm->host.empty();
  927. if (has_host) {
  928. host = ExternallyManagedSlice(rm->host.c_str());
  929. }
  930. uint32_t hash =
  931. GRPC_MDSTR_KV_HASH(has_host ? host.Hash() : 0, method.Hash());
  932. uint32_t probes = 0;
  933. for (probes = 0; (*registered_methods_)[(hash + probes) % slots]
  934. .server_registered_method != nullptr;
  935. probes++) {
  936. }
  937. if (probes > max_probes) max_probes = probes;
  938. ChannelRegisteredMethod* crm =
  939. &(*registered_methods_)[(hash + probes) % slots];
  940. crm->server_registered_method = rm.get();
  941. crm->flags = rm->flags;
  942. crm->has_host = has_host;
  943. if (has_host) {
  944. crm->host = host;
  945. }
  946. crm->method = method;
  947. }
  948. GPR_ASSERT(slots <= UINT32_MAX);
  949. registered_method_max_probes_ = max_probes;
  950. }
  951. // Publish channel.
  952. {
  953. MutexLock lock(&server_->mu_global_);
  954. server_->channels_.push_front(this);
  955. list_position_ = server_->channels_.begin();
  956. }
  957. // Start accept_stream transport op.
  958. grpc_transport_op* op = grpc_make_transport_op(nullptr);
  959. op->set_accept_stream = true;
  960. op->set_accept_stream_fn = AcceptStream;
  961. op->set_accept_stream_user_data = this;
  962. op->start_connectivity_watch = MakeOrphanable<ConnectivityWatcher>(this);
  963. if (server_->shutdown_flag_.load(std::memory_order_acquire)) {
  964. op->disconnect_with_error =
  965. GRPC_ERROR_CREATE_FROM_STATIC_STRING("Server shutdown");
  966. }
  967. grpc_transport_perform_op(transport, op);
  968. }
  969. Server::ChannelRegisteredMethod* Server::ChannelData::GetRegisteredMethod(
  970. const grpc_slice& host, const grpc_slice& path, bool is_idempotent) {
  971. if (registered_methods_ == nullptr) return nullptr;
  972. /* TODO(ctiller): unify these two searches */
  973. /* check for an exact match with host */
  974. uint32_t hash = GRPC_MDSTR_KV_HASH(grpc_slice_hash_internal(host),
  975. grpc_slice_hash_internal(path));
  976. for (size_t i = 0; i <= registered_method_max_probes_; i++) {
  977. ChannelRegisteredMethod* rm =
  978. &(*registered_methods_)[(hash + i) % registered_methods_->size()];
  979. if (rm->server_registered_method == nullptr) break;
  980. if (!rm->has_host) continue;
  981. if (rm->host != host) continue;
  982. if (rm->method != path) continue;
  983. if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
  984. !is_idempotent) {
  985. continue;
  986. }
  987. return rm;
  988. }
  989. /* check for a wildcard method definition (no host set) */
  990. hash = GRPC_MDSTR_KV_HASH(0, grpc_slice_hash_internal(path));
  991. for (size_t i = 0; i <= registered_method_max_probes_; i++) {
  992. ChannelRegisteredMethod* rm =
  993. &(*registered_methods_)[(hash + i) % registered_methods_->size()];
  994. if (rm->server_registered_method == nullptr) break;
  995. if (rm->has_host) continue;
  996. if (rm->method != path) continue;
  997. if ((rm->flags & GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST) &&
  998. !is_idempotent) {
  999. continue;
  1000. }
  1001. return rm;
  1002. }
  1003. return nullptr;
  1004. }
  1005. void Server::ChannelData::AcceptStream(void* arg, grpc_transport* /*transport*/,
  1006. const void* transport_server_data) {
  1007. auto* chand = static_cast<Server::ChannelData*>(arg);
  1008. /* create a call */
  1009. grpc_call_create_args args;
  1010. args.channel = chand->channel_;
  1011. args.server = chand->server_.get();
  1012. args.parent = nullptr;
  1013. args.propagation_mask = 0;
  1014. args.cq = nullptr;
  1015. args.pollset_set_alternative = nullptr;
  1016. args.server_transport_data = transport_server_data;
  1017. args.add_initial_metadata = nullptr;
  1018. args.add_initial_metadata_count = 0;
  1019. args.send_deadline = GRPC_MILLIS_INF_FUTURE;
  1020. grpc_call* call;
  1021. grpc_error* error = grpc_call_create(&args, &call);
  1022. grpc_call_element* elem =
  1023. grpc_call_stack_element(grpc_call_get_call_stack(call), 0);
  1024. auto* calld = static_cast<Server::CallData*>(elem->call_data);
  1025. if (error != GRPC_ERROR_NONE) {
  1026. GRPC_ERROR_UNREF(error);
  1027. calld->FailCallCreation();
  1028. return;
  1029. }
  1030. calld->Start(elem);
  1031. }
  1032. void Server::ChannelData::FinishDestroy(void* arg, grpc_error* /*error*/) {
  1033. auto* chand = static_cast<Server::ChannelData*>(arg);
  1034. Server* server = chand->server_.get();
  1035. GRPC_CHANNEL_INTERNAL_UNREF(chand->channel_, "server");
  1036. server->Unref();
  1037. }
  1038. void Server::ChannelData::Destroy() {
  1039. if (!list_position_.has_value()) return;
  1040. GPR_ASSERT(server_ != nullptr);
  1041. server_->channels_.erase(*list_position_);
  1042. list_position_.reset();
  1043. server_->Ref().release();
  1044. server_->MaybeFinishShutdown();
  1045. GRPC_CLOSURE_INIT(&finish_destroy_channel_closure_, FinishDestroy, this,
  1046. grpc_schedule_on_exec_ctx);
  1047. if (GRPC_TRACE_FLAG_ENABLED(grpc_server_channel_trace)) {
  1048. gpr_log(GPR_INFO, "Disconnected client");
  1049. }
  1050. grpc_transport_op* op =
  1051. grpc_make_transport_op(&finish_destroy_channel_closure_);
  1052. op->set_accept_stream = true;
  1053. grpc_channel_next_op(
  1054. grpc_channel_stack_element(grpc_channel_get_channel_stack(channel_), 0),
  1055. op);
  1056. }
  1057. grpc_error* Server::ChannelData::InitChannelElement(
  1058. grpc_channel_element* elem, grpc_channel_element_args* args) {
  1059. GPR_ASSERT(args->is_first);
  1060. GPR_ASSERT(!args->is_last);
  1061. new (elem->channel_data) ChannelData();
  1062. return GRPC_ERROR_NONE;
  1063. }
  1064. void Server::ChannelData::DestroyChannelElement(grpc_channel_element* elem) {
  1065. auto* chand = static_cast<ChannelData*>(elem->channel_data);
  1066. chand->~ChannelData();
  1067. }
  1068. //
  1069. // Server::CallData
  1070. //
  1071. Server::CallData::CallData(grpc_call_element* elem,
  1072. const grpc_call_element_args& args,
  1073. RefCountedPtr<Server> server)
  1074. : server_(std::move(server)),
  1075. call_(grpc_call_from_top_element(elem)),
  1076. call_combiner_(args.call_combiner) {
  1077. GRPC_CLOSURE_INIT(&recv_initial_metadata_ready_, RecvInitialMetadataReady,
  1078. elem, grpc_schedule_on_exec_ctx);
  1079. GRPC_CLOSURE_INIT(&recv_trailing_metadata_ready_, RecvTrailingMetadataReady,
  1080. elem, grpc_schedule_on_exec_ctx);
  1081. }
  1082. Server::CallData::~CallData() {
  1083. GPR_ASSERT(state_.Load(MemoryOrder::RELAXED) != CallState::PENDING);
  1084. GRPC_ERROR_UNREF(recv_initial_metadata_error_);
  1085. if (host_.has_value()) {
  1086. grpc_slice_unref_internal(*host_);
  1087. }
  1088. if (path_.has_value()) {
  1089. grpc_slice_unref_internal(*path_);
  1090. }
  1091. grpc_metadata_array_destroy(&initial_metadata_);
  1092. grpc_byte_buffer_destroy(payload_);
  1093. }
  1094. void Server::CallData::SetState(CallState state) {
  1095. state_.Store(state, MemoryOrder::RELAXED);
  1096. }
  1097. bool Server::CallData::MaybeActivate() {
  1098. CallState expected = CallState::PENDING;
  1099. return state_.CompareExchangeStrong(&expected, CallState::ACTIVATED,
  1100. MemoryOrder::ACQ_REL,
  1101. MemoryOrder::RELAXED);
  1102. }
  1103. void Server::CallData::FailCallCreation() {
  1104. CallState expected_not_started = CallState::NOT_STARTED;
  1105. CallState expected_pending = CallState::PENDING;
  1106. if (state_.CompareExchangeStrong(&expected_not_started, CallState::ZOMBIED,
  1107. MemoryOrder::ACQ_REL,
  1108. MemoryOrder::ACQUIRE)) {
  1109. KillZombie();
  1110. } else if (state_.CompareExchangeStrong(&expected_pending, CallState::ZOMBIED,
  1111. MemoryOrder::ACQ_REL,
  1112. MemoryOrder::RELAXED)) {
  1113. // Zombied call will be destroyed when it's removed from the pending
  1114. // queue... later.
  1115. }
  1116. }
  1117. void Server::CallData::Start(grpc_call_element* elem) {
  1118. grpc_op op;
  1119. op.op = GRPC_OP_RECV_INITIAL_METADATA;
  1120. op.flags = 0;
  1121. op.reserved = nullptr;
  1122. op.data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_;
  1123. GRPC_CLOSURE_INIT(&recv_initial_metadata_batch_complete_,
  1124. RecvInitialMetadataBatchComplete, elem,
  1125. grpc_schedule_on_exec_ctx);
  1126. grpc_call_start_batch_and_execute(call_, &op, 1,
  1127. &recv_initial_metadata_batch_complete_);
  1128. }
  1129. void Server::CallData::Publish(size_t cq_idx, RequestedCall* rc) {
  1130. grpc_call_set_completion_queue(call_, rc->cq_bound_to_call);
  1131. *rc->call = call_;
  1132. cq_new_ = server_->cqs_[cq_idx];
  1133. GPR_SWAP(grpc_metadata_array, *rc->initial_metadata, initial_metadata_);
  1134. switch (rc->type) {
  1135. case RequestedCall::Type::BATCH_CALL:
  1136. GPR_ASSERT(host_.has_value());
  1137. GPR_ASSERT(path_.has_value());
  1138. rc->data.batch.details->host = grpc_slice_ref_internal(*host_);
  1139. rc->data.batch.details->method = grpc_slice_ref_internal(*path_);
  1140. rc->data.batch.details->deadline =
  1141. grpc_millis_to_timespec(deadline_, GPR_CLOCK_MONOTONIC);
  1142. rc->data.batch.details->flags = recv_initial_metadata_flags_;
  1143. break;
  1144. case RequestedCall::Type::REGISTERED_CALL:
  1145. *rc->data.registered.deadline =
  1146. grpc_millis_to_timespec(deadline_, GPR_CLOCK_MONOTONIC);
  1147. if (rc->data.registered.optional_payload != nullptr) {
  1148. *rc->data.registered.optional_payload = payload_;
  1149. payload_ = nullptr;
  1150. }
  1151. break;
  1152. default:
  1153. GPR_UNREACHABLE_CODE(return );
  1154. }
  1155. grpc_cq_end_op(cq_new_, rc->tag, GRPC_ERROR_NONE, Server::DoneRequestEvent,
  1156. rc, &rc->completion, true);
  1157. }
  1158. void Server::CallData::PublishNewRpc(void* arg, grpc_error* error) {
  1159. grpc_call_element* call_elem = static_cast<grpc_call_element*>(arg);
  1160. auto* calld = static_cast<Server::CallData*>(call_elem->call_data);
  1161. auto* chand = static_cast<Server::ChannelData*>(call_elem->channel_data);
  1162. RequestMatcherInterface* rm = calld->matcher_;
  1163. Server* server = rm->server();
  1164. if (error != GRPC_ERROR_NONE ||
  1165. server->shutdown_flag_.load(std::memory_order_acquire)) {
  1166. calld->state_.Store(CallState::ZOMBIED, MemoryOrder::RELAXED);
  1167. calld->KillZombie();
  1168. return;
  1169. }
  1170. rm->MatchOrQueue(chand->cq_idx(), calld);
  1171. }
  1172. namespace {
  1173. void KillZombieClosure(void* call, grpc_error* /*error*/) {
  1174. grpc_call_unref(static_cast<grpc_call*>(call));
  1175. }
  1176. } // namespace
  1177. void Server::CallData::KillZombie() {
  1178. GRPC_CLOSURE_INIT(&kill_zombie_closure_, KillZombieClosure, call_,
  1179. grpc_schedule_on_exec_ctx);
  1180. ExecCtx::Run(DEBUG_LOCATION, &kill_zombie_closure_, GRPC_ERROR_NONE);
  1181. }
  1182. void Server::CallData::StartNewRpc(grpc_call_element* elem) {
  1183. auto* chand = static_cast<ChannelData*>(elem->channel_data);
  1184. if (server_->shutdown_flag_.load(std::memory_order_acquire)) {
  1185. state_.Store(CallState::ZOMBIED, MemoryOrder::RELAXED);
  1186. KillZombie();
  1187. return;
  1188. }
  1189. // Find request matcher.
  1190. matcher_ = server_->unregistered_request_matcher_.get();
  1191. grpc_server_register_method_payload_handling payload_handling =
  1192. GRPC_SRM_PAYLOAD_NONE;
  1193. if (path_.has_value() && host_.has_value()) {
  1194. ChannelRegisteredMethod* rm =
  1195. chand->GetRegisteredMethod(*host_, *path_,
  1196. (recv_initial_metadata_flags_ &
  1197. GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST));
  1198. if (rm != nullptr) {
  1199. matcher_ = rm->server_registered_method->matcher.get();
  1200. payload_handling = rm->server_registered_method->payload_handling;
  1201. }
  1202. }
  1203. // Start recv_message op if needed.
  1204. switch (payload_handling) {
  1205. case GRPC_SRM_PAYLOAD_NONE:
  1206. PublishNewRpc(elem, GRPC_ERROR_NONE);
  1207. break;
  1208. case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: {
  1209. grpc_op op;
  1210. op.op = GRPC_OP_RECV_MESSAGE;
  1211. op.flags = 0;
  1212. op.reserved = nullptr;
  1213. op.data.recv_message.recv_message = &payload_;
  1214. GRPC_CLOSURE_INIT(&publish_, PublishNewRpc, elem,
  1215. grpc_schedule_on_exec_ctx);
  1216. grpc_call_start_batch_and_execute(call_, &op, 1, &publish_);
  1217. break;
  1218. }
  1219. }
  1220. }
  1221. void Server::CallData::RecvInitialMetadataBatchComplete(void* arg,
  1222. grpc_error* error) {
  1223. grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
  1224. auto* calld = static_cast<Server::CallData*>(elem->call_data);
  1225. if (error != GRPC_ERROR_NONE) {
  1226. calld->FailCallCreation();
  1227. return;
  1228. }
  1229. calld->StartNewRpc(elem);
  1230. }
  1231. void Server::CallData::StartTransportStreamOpBatchImpl(
  1232. grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
  1233. if (batch->recv_initial_metadata) {
  1234. GPR_ASSERT(batch->payload->recv_initial_metadata.recv_flags == nullptr);
  1235. recv_initial_metadata_ =
  1236. batch->payload->recv_initial_metadata.recv_initial_metadata;
  1237. original_recv_initial_metadata_ready_ =
  1238. batch->payload->recv_initial_metadata.recv_initial_metadata_ready;
  1239. batch->payload->recv_initial_metadata.recv_initial_metadata_ready =
  1240. &recv_initial_metadata_ready_;
  1241. batch->payload->recv_initial_metadata.recv_flags =
  1242. &recv_initial_metadata_flags_;
  1243. }
  1244. if (batch->recv_trailing_metadata) {
  1245. original_recv_trailing_metadata_ready_ =
  1246. batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready;
  1247. batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready =
  1248. &recv_trailing_metadata_ready_;
  1249. }
  1250. grpc_call_next_op(elem, batch);
  1251. }
  1252. void Server::CallData::RecvInitialMetadataReady(void* arg, grpc_error* error) {
  1253. grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
  1254. CallData* calld = static_cast<CallData*>(elem->call_data);
  1255. grpc_millis op_deadline;
  1256. if (error == GRPC_ERROR_NONE) {
  1257. GPR_DEBUG_ASSERT(calld->recv_initial_metadata_->idx.named.path != nullptr);
  1258. GPR_DEBUG_ASSERT(calld->recv_initial_metadata_->idx.named.authority !=
  1259. nullptr);
  1260. calld->path_.emplace(grpc_slice_ref_internal(
  1261. GRPC_MDVALUE(calld->recv_initial_metadata_->idx.named.path->md)));
  1262. calld->host_.emplace(grpc_slice_ref_internal(
  1263. GRPC_MDVALUE(calld->recv_initial_metadata_->idx.named.authority->md)));
  1264. grpc_metadata_batch_remove(calld->recv_initial_metadata_, GRPC_BATCH_PATH);
  1265. grpc_metadata_batch_remove(calld->recv_initial_metadata_,
  1266. GRPC_BATCH_AUTHORITY);
  1267. } else {
  1268. GRPC_ERROR_REF(error);
  1269. }
  1270. op_deadline = calld->recv_initial_metadata_->deadline;
  1271. if (op_deadline != GRPC_MILLIS_INF_FUTURE) {
  1272. calld->deadline_ = op_deadline;
  1273. }
  1274. if (calld->host_.has_value() && calld->path_.has_value()) {
  1275. /* do nothing */
  1276. } else {
  1277. /* Pass the error reference to calld->recv_initial_metadata_error */
  1278. grpc_error* src_error = error;
  1279. error = GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
  1280. "Missing :authority or :path", &src_error, 1);
  1281. GRPC_ERROR_UNREF(src_error);
  1282. calld->recv_initial_metadata_error_ = GRPC_ERROR_REF(error);
  1283. }
  1284. grpc_closure* closure = calld->original_recv_initial_metadata_ready_;
  1285. calld->original_recv_initial_metadata_ready_ = nullptr;
  1286. if (calld->seen_recv_trailing_metadata_ready_) {
  1287. GRPC_CALL_COMBINER_START(calld->call_combiner_,
  1288. &calld->recv_trailing_metadata_ready_,
  1289. calld->recv_trailing_metadata_error_,
  1290. "continue server recv_trailing_metadata_ready");
  1291. }
  1292. Closure::Run(DEBUG_LOCATION, closure, error);
  1293. }
  1294. void Server::CallData::RecvTrailingMetadataReady(void* arg, grpc_error* error) {
  1295. grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
  1296. CallData* calld = static_cast<CallData*>(elem->call_data);
  1297. if (calld->original_recv_initial_metadata_ready_ != nullptr) {
  1298. calld->recv_trailing_metadata_error_ = GRPC_ERROR_REF(error);
  1299. calld->seen_recv_trailing_metadata_ready_ = true;
  1300. GRPC_CLOSURE_INIT(&calld->recv_trailing_metadata_ready_,
  1301. RecvTrailingMetadataReady, elem,
  1302. grpc_schedule_on_exec_ctx);
  1303. GRPC_CALL_COMBINER_STOP(calld->call_combiner_,
  1304. "deferring server recv_trailing_metadata_ready "
  1305. "until after recv_initial_metadata_ready");
  1306. return;
  1307. }
  1308. error =
  1309. grpc_error_add_child(GRPC_ERROR_REF(error),
  1310. GRPC_ERROR_REF(calld->recv_initial_metadata_error_));
  1311. Closure::Run(DEBUG_LOCATION, calld->original_recv_trailing_metadata_ready_,
  1312. error);
  1313. }
  1314. grpc_error* Server::CallData::InitCallElement(
  1315. grpc_call_element* elem, const grpc_call_element_args* args) {
  1316. auto* chand = static_cast<ChannelData*>(elem->channel_data);
  1317. new (elem->call_data) Server::CallData(elem, *args, chand->server());
  1318. return GRPC_ERROR_NONE;
  1319. }
  1320. void Server::CallData::DestroyCallElement(
  1321. grpc_call_element* elem, const grpc_call_final_info* /*final_info*/,
  1322. grpc_closure* /*ignored*/) {
  1323. auto* calld = static_cast<CallData*>(elem->call_data);
  1324. calld->~CallData();
  1325. }
  1326. void Server::CallData::StartTransportStreamOpBatch(
  1327. grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
  1328. auto* calld = static_cast<CallData*>(elem->call_data);
  1329. calld->StartTransportStreamOpBatchImpl(elem, batch);
  1330. }
  1331. } // namespace grpc_core
  1332. //
  1333. // C-core API
  1334. //
  1335. grpc_server* grpc_server_create(const grpc_channel_args* args, void* reserved) {
  1336. grpc_core::ExecCtx exec_ctx;
  1337. GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved));
  1338. grpc_server* c_server = new grpc_server;
  1339. c_server->core_server = grpc_core::MakeOrphanable<grpc_core::Server>(args);
  1340. return c_server;
  1341. }
  1342. void grpc_server_register_completion_queue(grpc_server* server,
  1343. grpc_completion_queue* cq,
  1344. void* reserved) {
  1345. GRPC_API_TRACE(
  1346. "grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3,
  1347. (server, cq, reserved));
  1348. GPR_ASSERT(!reserved);
  1349. auto cq_type = grpc_get_cq_completion_type(cq);
  1350. if (cq_type != GRPC_CQ_NEXT && cq_type != GRPC_CQ_CALLBACK) {
  1351. gpr_log(GPR_INFO,
  1352. "Completion queue of type %d is being registered as a "
  1353. "server-completion-queue",
  1354. static_cast<int>(cq_type));
  1355. /* Ideally we should log an error and abort but ruby-wrapped-language API
  1356. calls grpc_completion_queue_pluck() on server completion queues */
  1357. }
  1358. server->core_server->RegisterCompletionQueue(cq);
  1359. }
  1360. void* grpc_server_register_method(
  1361. grpc_server* server, const char* method, const char* host,
  1362. grpc_server_register_method_payload_handling payload_handling,
  1363. uint32_t flags) {
  1364. GRPC_API_TRACE(
  1365. "grpc_server_register_method(server=%p, method=%s, host=%s, "
  1366. "flags=0x%08x)",
  1367. 4, (server, method, host, flags));
  1368. return server->core_server->RegisterMethod(method, host, payload_handling,
  1369. flags);
  1370. }
  1371. void grpc_server_start(grpc_server* server) {
  1372. grpc_core::ExecCtx exec_ctx;
  1373. GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server));
  1374. server->core_server->Start();
  1375. }
  1376. void grpc_server_shutdown_and_notify(grpc_server* server,
  1377. grpc_completion_queue* cq, void* tag) {
  1378. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  1379. grpc_core::ExecCtx exec_ctx;
  1380. GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3,
  1381. (server, cq, tag));
  1382. server->core_server->ShutdownAndNotify(cq, tag);
  1383. }
  1384. void grpc_server_cancel_all_calls(grpc_server* server) {
  1385. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  1386. grpc_core::ExecCtx exec_ctx;
  1387. GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server));
  1388. server->core_server->CancelAllCalls();
  1389. }
  1390. void grpc_server_destroy(grpc_server* server) {
  1391. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  1392. grpc_core::ExecCtx exec_ctx;
  1393. GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server));
  1394. delete server;
  1395. }
  1396. grpc_call_error grpc_server_request_call(
  1397. grpc_server* server, grpc_call** call, grpc_call_details* details,
  1398. grpc_metadata_array* request_metadata,
  1399. grpc_completion_queue* cq_bound_to_call,
  1400. grpc_completion_queue* cq_for_notification, void* tag) {
  1401. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  1402. grpc_core::ExecCtx exec_ctx;
  1403. GRPC_STATS_INC_SERVER_REQUESTED_CALLS();
  1404. GRPC_API_TRACE(
  1405. "grpc_server_request_call("
  1406. "server=%p, call=%p, details=%p, initial_metadata=%p, "
  1407. "cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)",
  1408. 7,
  1409. (server, call, details, request_metadata, cq_bound_to_call,
  1410. cq_for_notification, tag));
  1411. return server->core_server->RequestCall(call, details, request_metadata,
  1412. cq_bound_to_call, cq_for_notification,
  1413. tag);
  1414. }
  1415. grpc_call_error grpc_server_request_registered_call(
  1416. grpc_server* server, void* registered_method, grpc_call** call,
  1417. gpr_timespec* deadline, grpc_metadata_array* request_metadata,
  1418. grpc_byte_buffer** optional_payload,
  1419. grpc_completion_queue* cq_bound_to_call,
  1420. grpc_completion_queue* cq_for_notification, void* tag_new) {
  1421. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  1422. grpc_core::ExecCtx exec_ctx;
  1423. GRPC_STATS_INC_SERVER_REQUESTED_CALLS();
  1424. auto* rm =
  1425. static_cast<grpc_core::Server::RegisteredMethod*>(registered_method);
  1426. GRPC_API_TRACE(
  1427. "grpc_server_request_registered_call("
  1428. "server=%p, registered_method=%p, call=%p, deadline=%p, "
  1429. "request_metadata=%p, "
  1430. "optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, "
  1431. "tag=%p)",
  1432. 9,
  1433. (server, registered_method, call, deadline, request_metadata,
  1434. optional_payload, cq_bound_to_call, cq_for_notification, tag_new));
  1435. return server->core_server->RequestRegisteredCall(
  1436. rm, call, deadline, request_metadata, optional_payload, cq_bound_to_call,
  1437. cq_for_notification, tag_new);
  1438. }
  1439. void grpc_server_set_config_fetcher(
  1440. grpc_server* server, grpc_server_config_fetcher* server_config_fetcher) {
  1441. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  1442. grpc_core::ExecCtx exec_ctx;
  1443. GRPC_API_TRACE("grpc_server_set_config_fetcher(server=%p, config_fetcher=%p)",
  1444. 2, (server, server_config_fetcher));
  1445. server->core_server->set_config_fetcher(
  1446. std::unique_ptr<grpc_server_config_fetcher>(server_config_fetcher));
  1447. }
  1448. void grpc_server_config_fetcher_destroy(
  1449. grpc_server_config_fetcher* server_config_fetcher) {
  1450. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  1451. grpc_core::ExecCtx exec_ctx;
  1452. GRPC_API_TRACE("grpc_server_config_fetcher_destroy(config_fetcher=%p)", 1,
  1453. (server_config_fetcher));
  1454. delete server_config_fetcher;
  1455. }