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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://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.13"/>
  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">7.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.13 -->
  34. <script type="text/javascript">
  35. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  36. </script>
  37. <script type="text/javascript" src="menudata.js"></script>
  38. <script type="text/javascript" src="menu.js"></script>
  39. <script type="text/javascript">
  40. $(function() {
  41. initMenu('',true,false,'search.php','Search');
  42. $(document).ready(function() { init_search(); });
  43. });
  44. </script>
  45. <div id="main-nav"></div>
  46. <!-- window showing the filter options -->
  47. <div id="MSearchSelectWindow"
  48. onmouseover="return searchBox.OnSearchSelectShow()"
  49. onmouseout="return searchBox.OnSearchSelectHide()"
  50. onkeydown="return searchBox.OnSearchSelectKey(event)">
  51. </div>
  52. <!-- iframe showing the search results (closed by default) -->
  53. <div id="MSearchResultsWindow">
  54. <iframe src="javascript:void(0)" frameborder="0"
  55. name="MSearchResults" id="MSearchResults">
  56. </iframe>
  57. </div>
  58. </div><!-- top -->
  59. <div class="header">
  60. <div class="headertitle">
  61. <div class="title">Polling Engine Usage on gRPC client and Server </div> </div>
  62. </div><!--header-->
  63. <div class="contents">
  64. <div class="textblock"><p><em>Author: Sree Kuchibhotla () - Sep 2018</em></p>
  65. <p>This document talks about how polling engine is used in gRPC core (both on client and server code paths).</p>
  66. <h2>gRPC client</h2>
  67. <h3>Relation between Call, Channel (sub-channels), Completion queue, <code>grpc_pollset</code></h3>
  68. <ul>
  69. <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>
  70. <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>
  71. </ul>
  72. <div class="image">
  73. <img src="../images/grpc-call-channel-cq.png" alt="image"/>
  74. </div>
  75. <h3>Making progress on Async <code>connect()</code> on sub-channels (<code>grpc_pollset_set</code> usecase)</h3>
  76. <ul>
  77. <li>A gRPC channel is created between a client and a 'target'. The 'target' may resolve in to one or more backend servers.</li>
  78. <li>A sub-channel is the 'connection' from a client to the backend server</li>
  79. <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>
  80. <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>
  81. <li>To accomplish this, the <code>grpc_pollset_set</code> is used the following way (see picture below)</li>
  82. </ul>
  83. </li>
  84. </ul>
  85. <div class="image">
  86. <img src="../images/grpc-client-lb-pss.png" alt="image"/>
  87. </div>
  88. <h2>gRPC server</h2>
  89. <ul>
  90. <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>
  91. <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>
  92. </ul>
  93. <div class="image">
  94. <img src="../images/grpc-server-cq-fds.png" alt="image"/>
  95. </div>
  96. </div></div><!-- contents -->
  97. <!-- start footer part -->
  98. <hr class="footer"/><address class="footer"><small>
  99. Generated on Wed Jul 3 2019 14:51:26 for GRPC Core by &#160;<a href="http://www.doxygen.org/index.html">
  100. <img class="footer" src="doxygen.png" alt="doxygen"/>
  101. </a> 1.8.13
  102. </small></address>
  103. </body>
  104. </html>