md_doc_core_grpc-client-server-polling-engine-usage.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  6. <meta name="generator" content="Doxygen 1.8.17"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <title>GRPC Core: Polling Engine Usage on gRPC client and Server</title>
  9. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  10. <script type="text/javascript" src="jquery.js"></script>
  11. <script type="text/javascript" src="dynsections.js"></script>
  12. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  13. <script type="text/javascript" src="search/searchdata.js"></script>
  14. <script type="text/javascript" src="search/search.js"></script>
  15. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  16. </head>
  17. <body>
  18. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  19. <div id="titlearea">
  20. <table cellspacing="0" cellpadding="0">
  21. <tbody>
  22. <tr style="height: 56px;">
  23. <td id="projectalign" style="padding-left: 0.5em;">
  24. <div id="projectname">GRPC Core
  25. &#160;<span id="projectnumber">15.0.0</span>
  26. </div>
  27. </td>
  28. </tr>
  29. </tbody>
  30. </table>
  31. </div>
  32. <!-- end header part -->
  33. <!-- Generated by Doxygen 1.8.17 -->
  34. <script type="text/javascript">
  35. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  36. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  37. /* @license-end */
  38. </script>
  39. <script type="text/javascript" src="menudata.js"></script>
  40. <script type="text/javascript" src="menu.js"></script>
  41. <script type="text/javascript">
  42. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  43. $(function() {
  44. initMenu('',true,false,'search.php','Search');
  45. $(document).ready(function() { init_search(); });
  46. });
  47. /* @license-end */</script>
  48. <div id="main-nav"></div>
  49. <!-- window showing the filter options -->
  50. <div id="MSearchSelectWindow"
  51. onmouseover="return searchBox.OnSearchSelectShow()"
  52. onmouseout="return searchBox.OnSearchSelectHide()"
  53. onkeydown="return searchBox.OnSearchSelectKey(event)">
  54. </div>
  55. <!-- iframe showing the search results (closed by default) -->
  56. <div id="MSearchResultsWindow">
  57. <iframe src="javascript:void(0)" frameborder="0"
  58. name="MSearchResults" id="MSearchResults">
  59. </iframe>
  60. </div>
  61. </div><!-- top -->
  62. <div class="PageDoc"><div class="header">
  63. <div class="headertitle">
  64. <div class="title">Polling Engine Usage on gRPC client and Server </div> </div>
  65. </div><!--header-->
  66. <div class="contents">
  67. <div class="textblock"><p><em>Author: Sree Kuchibhotla (@sreecha) - Sep 2018</em></p>
  68. <p>This document talks about how polling engine is used in gRPC core (both on client and server code paths).</p>
  69. <h1><a class="anchor" id="autotoc_md94"></a>
  70. gRPC client</h1>
  71. <h2><a class="anchor" id="autotoc_md95"></a>
  72. Relation between Call, Channel (sub-channels), Completion queue, &lt;tt&gt;grpc_pollset&lt;/tt&gt;</h2>
  73. <ul>
  74. <li>A gRPC Call is tied to a channel (more specifically a sub-channel) and a completion queue for the lifetime of the call.</li>
  75. <li>Once a <em>sub-channel</em> is picked for the call, the file-descriptor (socket fd in case of TCP channels) is added to the pollset corresponding to call's completion queue. (Recall that as per <a class="el" href="grpc-cq_8md.html">grpc-cq</a>, a completion queue has a pollset by default)</li>
  76. </ul>
  77. <p><img src="../images/grpc-call-channel-cq.png" alt="image" class="inline"/></p>
  78. <h2><a class="anchor" id="autotoc_md96"></a>
  79. Making progress on Async &lt;tt&gt;connect()&lt;/tt&gt; on sub-channels (&lt;tt&gt;grpc_pollset_set&lt;/tt&gt; usecase)</h2>
  80. <ul>
  81. <li>A gRPC channel is created between a client and a 'target'. The 'target' may resolve in to one or more backend servers.</li>
  82. <li>A sub-channel is the 'connection' from a client to the backend server</li>
  83. <li>While establishing sub-channels (i.e connections) to the backends, gRPC issues async <a href="https://github.com/grpc/grpc/blob/v1.15.1/src/core/lib/iomgr/tcp_client_posix.cc#L296"><code>connect()</code></a> calls which may not complete right away. When the <code>connect()</code> eventually succeeds, the socket fd is make 'writable'<ul>
  84. <li>This means that the polling engine must be monitoring all these sub-channel <code>fd</code>s for writable events and we need to make sure there is a polling thread that monitors all these fds</li>
  85. <li>To accomplish this, the <code>grpc_pollset_set</code> is used the following way (see picture below)</li>
  86. </ul>
  87. </li>
  88. </ul>
  89. <p><img src="../images/grpc-client-lb-pss.png" alt="image" class="inline"/></p>
  90. <h1><a class="anchor" id="autotoc_md97"></a>
  91. gRPC server</h1>
  92. <ul>
  93. <li>The listening fd (i.e., the socket fd corresponding to the server listening port) is added to each of the server completion queues. Note that in gRPC we use SO_REUSEPORT option and create multiple listening fds but all of them map to the same listening port</li>
  94. <li>A new incoming channel is assigned to some server completion queue picked randomly (note that we currently <a href="https://github.com/grpc/grpc/blob/v1.15.1/src/core/lib/iomgr/tcp_server_posix.cc#L231">round-robin</a> over the server completion queues)</li>
  95. </ul>
  96. <p><img src="../images/grpc-server-cq-fds.png" alt="image" class="inline"/> </p>
  97. </div></div><!-- contents -->
  98. </div><!-- PageDoc -->
  99. <!-- start footer part -->
  100. <hr class="footer"/><address class="footer"><small>
  101. Generated on Wed Mar 3 2021 19:17:11 for GRPC Core by &#160;<a href="http://www.doxygen.org/index.html">
  102. <img class="footer" src="doxygen.png" alt="doxygen"/>
  103. </a> 1.8.17
  104. </small></address>
  105. </body>
  106. </html>