test_proxy_hwm.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. #include <string.h>
  27. #include <unity.h>
  28. #include <assert.h>
  29. #include <unistd.h>
  30. //
  31. // Asynchronous proxy test using ZMQ_XPUB_NODROP and HWM:
  32. //
  33. // Topology:
  34. //
  35. // XPUB SUB
  36. // | |
  37. // \-----> XSUB -> XPUB -----/
  38. // ^^^^^^^^^^^^^^
  39. // ZMQ proxy
  40. //
  41. // All connections use "inproc" transport and have artificially-low HWMs set.
  42. // Then the PUB socket starts flooding the Proxy. The SUB is artificially slow
  43. // at receiving messages.
  44. // This scenario simulates what happens when a SUB is slower than
  45. // its (X)PUB: since ZMQ_XPUB_NODROP=1, the XPUB will block and then
  46. // also the (X)PUB socket will block.
  47. // The exact number of the messages that go through before (X)PUB blocks depends
  48. // on ZeroMQ internals and how the OS will schedule the different threads.
  49. // In the meanwhile asking statistics to the Proxy must NOT be blocking.
  50. //
  51. #define HWM 10
  52. #define NUM_BYTES_PER_MSG 50000
  53. typedef struct
  54. {
  55. void *context;
  56. const char *frontend_endpoint;
  57. const char *backend_endpoint;
  58. const char *control_endpoint;
  59. void *subscriber_received_all;
  60. } proxy_hwm_cfg_t;
  61. static void lower_hwm (void *skt_)
  62. {
  63. int send_hwm = HWM;
  64. TEST_ASSERT_SUCCESS_ERRNO (
  65. zmq_setsockopt (skt_, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm)));
  66. TEST_ASSERT_SUCCESS_ERRNO (
  67. zmq_setsockopt (skt_, ZMQ_RCVHWM, &send_hwm, sizeof (send_hwm)));
  68. }
  69. static void publisher_thread_main (void *pvoid_)
  70. {
  71. const proxy_hwm_cfg_t *const cfg =
  72. static_cast<const proxy_hwm_cfg_t *> (pvoid_);
  73. void *pubsocket = zmq_socket (cfg->context, ZMQ_XPUB);
  74. assert (pubsocket);
  75. lower_hwm (pubsocket);
  76. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (pubsocket, cfg->frontend_endpoint));
  77. int optval = 1;
  78. TEST_ASSERT_SUCCESS_ERRNO (
  79. zmq_setsockopt (pubsocket, ZMQ_XPUB_NODROP, &optval, sizeof (optval)));
  80. // Wait before starting TX operations till 1 subscriber has subscribed
  81. // (in this test there's 1 subscriber only)
  82. const char subscription_to_all_topics[] = {1, 0};
  83. recv_string_expect_success (pubsocket, subscription_to_all_topics, 0);
  84. uint64_t send_count = 0;
  85. while (true) {
  86. zmq_msg_t msg;
  87. int rc = zmq_msg_init_size (&msg, NUM_BYTES_PER_MSG);
  88. assert (rc == 0);
  89. /* Fill in message content with 'AAAAAA' */
  90. memset (zmq_msg_data (&msg), 'A', NUM_BYTES_PER_MSG);
  91. /* Send the message to the socket */
  92. rc = zmq_msg_send (&msg, pubsocket, ZMQ_DONTWAIT);
  93. if (rc != -1) {
  94. send_count++;
  95. } else {
  96. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  97. break;
  98. }
  99. }
  100. // VERIFY EXPECTED RESULTS
  101. // EXPLANATION FOR TX TO BE CONSIDERED SUCCESSFUL:
  102. // this test has 3 threads doing I/O across 2 queues. Depending on the scheduling,
  103. // it might happen that 20, 30 or 40 messages go through before the pub blocks.
  104. // That's because the receiver thread gets kicked once every (hwm_ + 1) / 2 sent
  105. // messages (search for zeromq sources compute_lwm function).
  106. // So depending on the scheduling of the second thread, the publisher might get one,
  107. // two or three more batches in. The ceiling is 40 as there's 2 queues.
  108. //
  109. assert (4 * HWM >= send_count && 2 * HWM <= send_count);
  110. // CLEANUP
  111. zmq_close (pubsocket);
  112. }
  113. static void subscriber_thread_main (void *pvoid_)
  114. {
  115. const proxy_hwm_cfg_t *const cfg =
  116. static_cast<const proxy_hwm_cfg_t *> (pvoid_);
  117. void *subsocket = zmq_socket (cfg->context, ZMQ_SUB);
  118. assert (subsocket);
  119. lower_hwm (subsocket);
  120. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (subsocket, ZMQ_SUBSCRIBE, 0, 0));
  121. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (subsocket, cfg->backend_endpoint));
  122. // receive all sent messages
  123. uint64_t rxsuccess = 0;
  124. bool success = true;
  125. while (success) {
  126. zmq_msg_t msg;
  127. int rc = zmq_msg_init (&msg);
  128. assert (rc == 0);
  129. rc = zmq_msg_recv (&msg, subsocket, 0);
  130. if (rc != -1) {
  131. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  132. rxsuccess++;
  133. // after receiving 1st message, set a finite timeout (default is infinite)
  134. int timeout_ms = 100;
  135. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  136. subsocket, ZMQ_RCVTIMEO, &timeout_ms, sizeof (timeout_ms)));
  137. } else {
  138. break;
  139. }
  140. msleep (100);
  141. }
  142. // VERIFY EXPECTED RESULTS
  143. // EXPLANATION FOR RX TO BE CONSIDERED SUCCESSFUL:
  144. // see publisher thread why we have 3 possible outcomes as number of RX messages
  145. assert (4 * HWM >= rxsuccess && 2 * HWM <= rxsuccess);
  146. // INFORM THAT WE COMPLETED:
  147. zmq_atomic_counter_inc (cfg->subscriber_received_all);
  148. // CLEANUP
  149. zmq_close (subsocket);
  150. }
  151. bool recv_stat (void *sock_, bool last_, uint64_t *res_)
  152. {
  153. zmq_msg_t stats_msg;
  154. int rc = zmq_msg_init (&stats_msg);
  155. assert (rc == 0);
  156. rc = zmq_msg_recv (&stats_msg, sock_, 0); //ZMQ_DONTWAIT);
  157. if (rc == -1 && errno == EAGAIN) {
  158. rc = zmq_msg_close (&stats_msg);
  159. assert (rc == 0);
  160. return false; // cannot retrieve the stat
  161. }
  162. assert (rc == sizeof (uint64_t));
  163. memcpy (res_, zmq_msg_data (&stats_msg), zmq_msg_size (&stats_msg));
  164. rc = zmq_msg_close (&stats_msg);
  165. assert (rc == 0);
  166. int more;
  167. size_t moresz = sizeof more;
  168. rc = zmq_getsockopt (sock_, ZMQ_RCVMORE, &more, &moresz);
  169. assert (rc == 0);
  170. assert ((last_ && !more) || (!last_ && more));
  171. return true;
  172. }
  173. // Utility function to interrogate the proxy:
  174. typedef struct
  175. {
  176. uint64_t msg_in;
  177. uint64_t bytes_in;
  178. uint64_t msg_out;
  179. uint64_t bytes_out;
  180. } zmq_socket_stats_t;
  181. typedef struct
  182. {
  183. zmq_socket_stats_t frontend;
  184. zmq_socket_stats_t backend;
  185. } zmq_proxy_stats_t;
  186. bool check_proxy_stats (void *control_proxy_)
  187. {
  188. zmq_proxy_stats_t total_stats;
  189. int rc;
  190. rc = zmq_send (control_proxy_, "STATISTICS", 10, ZMQ_DONTWAIT);
  191. assert (rc == 10 || (rc == -1 && errno == EAGAIN));
  192. if (rc == -1 && errno == EAGAIN) {
  193. return false;
  194. }
  195. // first frame of the reply contains FRONTEND stats:
  196. if (!recv_stat (control_proxy_, false, &total_stats.frontend.msg_in)) {
  197. return false;
  198. }
  199. recv_stat (control_proxy_, false, &total_stats.frontend.bytes_in);
  200. recv_stat (control_proxy_, false, &total_stats.frontend.msg_out);
  201. recv_stat (control_proxy_, false, &total_stats.frontend.bytes_out);
  202. // second frame of the reply contains BACKEND stats:
  203. recv_stat (control_proxy_, false, &total_stats.backend.msg_in);
  204. recv_stat (control_proxy_, false, &total_stats.backend.bytes_in);
  205. recv_stat (control_proxy_, false, &total_stats.backend.msg_out);
  206. recv_stat (control_proxy_, true, &total_stats.backend.bytes_out);
  207. return true;
  208. }
  209. static void proxy_stats_asker_thread_main (void *pvoid_)
  210. {
  211. const proxy_hwm_cfg_t *const cfg =
  212. static_cast<const proxy_hwm_cfg_t *> (pvoid_);
  213. // CONTROL REQ
  214. void *control_req =
  215. zmq_socket (cfg->context,
  216. ZMQ_REQ); // this one can be used to send command to the proxy
  217. assert (control_req);
  218. // connect CONTROL-REQ: a socket to which send commands
  219. int rc = zmq_connect (control_req, cfg->control_endpoint);
  220. assert (rc == 0);
  221. // IMPORTANT: by setting the tx/rx timeouts, we avoid getting blocked when interrogating a proxy which is
  222. // itself blocked in a zmq_msg_send() on its XPUB socket having ZMQ_XPUB_NODROP=1!
  223. int optval = 10;
  224. rc = zmq_setsockopt (control_req, ZMQ_SNDTIMEO, &optval, sizeof (optval));
  225. assert (rc == 0);
  226. rc = zmq_setsockopt (control_req, ZMQ_RCVTIMEO, &optval, sizeof (optval));
  227. assert (rc == 0);
  228. optval = 10;
  229. rc =
  230. zmq_setsockopt (control_req, ZMQ_REQ_CORRELATE, &optval, sizeof (optval));
  231. assert (rc == 0);
  232. rc =
  233. zmq_setsockopt (control_req, ZMQ_REQ_RELAXED, &optval, sizeof (optval));
  234. assert (rc == 0);
  235. // Start!
  236. while (!zmq_atomic_counter_value (cfg->subscriber_received_all)) {
  237. check_proxy_stats (control_req);
  238. usleep (1000); // 1ms -> in best case we will get 1000updates/second
  239. }
  240. // Ask the proxy to exit: the subscriber has received all messages
  241. rc = zmq_send (control_req, "TERMINATE", 9, 0);
  242. assert (rc == 9);
  243. zmq_close (control_req);
  244. }
  245. static void proxy_thread_main (void *pvoid_)
  246. {
  247. const proxy_hwm_cfg_t *const cfg =
  248. static_cast<const proxy_hwm_cfg_t *> (pvoid_);
  249. int rc;
  250. // FRONTEND SUB
  251. void *frontend_xsub = zmq_socket (
  252. cfg->context,
  253. ZMQ_XSUB); // the frontend is the one exposed to internal threads (INPROC)
  254. assert (frontend_xsub);
  255. lower_hwm (frontend_xsub);
  256. // bind FRONTEND
  257. rc = zmq_bind (frontend_xsub, cfg->frontend_endpoint);
  258. assert (rc == 0);
  259. // BACKEND PUB
  260. void *backend_xpub = zmq_socket (
  261. cfg->context,
  262. ZMQ_XPUB); // the backend is the one exposed to the external world (TCP)
  263. assert (backend_xpub);
  264. int optval = 1;
  265. rc =
  266. zmq_setsockopt (backend_xpub, ZMQ_XPUB_NODROP, &optval, sizeof (optval));
  267. assert (rc == 0);
  268. lower_hwm (backend_xpub);
  269. // bind BACKEND
  270. rc = zmq_bind (backend_xpub, cfg->backend_endpoint);
  271. assert (rc == 0);
  272. // CONTROL REP
  273. void *control_rep = zmq_socket (
  274. cfg->context,
  275. ZMQ_REP); // this one is used by the proxy to receive&reply to commands
  276. assert (control_rep);
  277. // bind CONTROL
  278. rc = zmq_bind (control_rep, cfg->control_endpoint);
  279. assert (rc == 0);
  280. // start proxying!
  281. zmq_proxy_steerable (frontend_xsub, backend_xpub, NULL, control_rep);
  282. zmq_close (frontend_xsub);
  283. zmq_close (backend_xpub);
  284. zmq_close (control_rep);
  285. }
  286. // The main thread simply starts several clients and a server, and then
  287. // waits for the server to finish.
  288. int main (void)
  289. {
  290. setup_test_environment ();
  291. void *context = zmq_ctx_new ();
  292. assert (context);
  293. // START ALL SECONDARY THREADS
  294. proxy_hwm_cfg_t cfg;
  295. cfg.context = context;
  296. cfg.frontend_endpoint = "inproc://frontend";
  297. cfg.backend_endpoint = "inproc://backend";
  298. cfg.control_endpoint = "inproc://ctrl";
  299. cfg.subscriber_received_all = zmq_atomic_counter_new ();
  300. void *proxy = zmq_threadstart (&proxy_thread_main, (void *) &cfg);
  301. assert (proxy != 0);
  302. void *publisher = zmq_threadstart (&publisher_thread_main, (void *) &cfg);
  303. assert (publisher != 0);
  304. void *subscriber = zmq_threadstart (&subscriber_thread_main, (void *) &cfg);
  305. assert (subscriber != 0);
  306. void *asker =
  307. zmq_threadstart (&proxy_stats_asker_thread_main, (void *) &cfg);
  308. assert (asker != 0);
  309. // CLEANUP
  310. zmq_threadclose (publisher);
  311. zmq_threadclose (subscriber);
  312. zmq_threadclose (asker);
  313. zmq_threadclose (proxy);
  314. int rc = zmq_ctx_term (context);
  315. assert (rc == 0);
  316. zmq_atomic_counter_destroy (&cfg.subscriber_received_all);
  317. return 0;
  318. }