threading.asciidoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. [[modules-network-threading-model]]
  2. ==== Networking threading model
  3. This section describes the threading model used by the networking subsystem in
  4. {es}. This information isn't required to use {es}, but it may be useful to
  5. advanced users who are diagnosing network problems in a cluster.
  6. {es} nodes communicate over a collection of TCP channels that together form a
  7. transport connection. {es} clients communicate with the cluster over HTTP,
  8. which also uses one or more TCP channels. Each of these TCP channels is owned
  9. by exactly one of the `transport_worker` threads in the node. This owning
  10. thread is chosen when the channel is opened and remains the same for the
  11. lifetime of the channel.
  12. Each `transport_worker` thread has sole responsibility for sending and
  13. receiving data over the channels it owns. Additionally, each http and transport
  14. server socket is assigned to one of the `transport_worker` threads. That worker
  15. has the responsibility of accepting new incoming connections to the server
  16. socket it owns.
  17. If a thread in {es} wants to send data over a particular channel, it passes the
  18. data to the owning `transport_worker` thread for the actual transmission.
  19. Normally the `transport_worker` threads will not completely handle the messages
  20. they receive. Instead, they will do a small amount of preliminary processing
  21. and then dispatch (hand off) the message to a different
  22. <<modules-threadpool,threadpool>> for the rest of their handling. For instance,
  23. bulk messages are dispatched to the `write` threadpool, searches are dispatched
  24. to one of the `search` threadpools, and requests for statistics and other
  25. management tasks are mostly dispatched to the `management` threadpool. However
  26. in some cases the processing of a message is expected to be so quick that {es}
  27. will do all of the processing on the `transport_worker` thread rather than
  28. incur the overhead of dispatching it elsewhere.
  29. By default, there is one `transport_worker` thread per CPU. In contrast, there
  30. may sometimes be tens-of-thousands of TCP channels. If data arrives on a TCP
  31. channel and its owning `transport_worker` thread is busy, the data isn't
  32. processed until the thread finishes whatever it is doing. Similarly, outgoing
  33. data are not sent over a channel until the owning `transport_worker` thread is
  34. free. This means that we require every `transport_worker` thread to be idle
  35. frequently. An idle `transport_worker` looks something like this in a stack
  36. dump:
  37. [source,text]
  38. ----
  39. "elasticsearch[instance-0000000004][transport_worker][T#1]" #32 daemon prio=5 os_prio=0 cpu=9645.94ms elapsed=501.63s tid=0x00007fb83b6307f0 nid=0x1c4 runnable [0x00007fb7b8ffe000]
  40. java.lang.Thread.State: RUNNABLE
  41. at sun.nio.ch.EPoll.wait(java.base@17.0.2/Native Method)
  42. at sun.nio.ch.EPollSelectorImpl.doSelect(java.base@17.0.2/EPollSelectorImpl.java:118)
  43. at sun.nio.ch.SelectorImpl.lockAndDoSelect(java.base@17.0.2/SelectorImpl.java:129)
  44. - locked <0x00000000c443c518> (a sun.nio.ch.Util$2)
  45. - locked <0x00000000c38f7700> (a sun.nio.ch.EPollSelectorImpl)
  46. at sun.nio.ch.SelectorImpl.select(java.base@17.0.2/SelectorImpl.java:146)
  47. at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:813)
  48. at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:460)
  49. at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
  50. at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
  51. at java.lang.Thread.run(java.base@17.0.2/Thread.java:833)
  52. ----
  53. In the <<cluster-nodes-hot-threads>> API an idle `transport_worker` thread is
  54. reported like this:
  55. [source,text]
  56. ----
  57. 0.0% [cpu=0.0%, idle=100.0%] (500ms out of 500ms) cpu usage by thread 'elasticsearch[instance-0000000004][transport_worker][T#1]'
  58. 10/10 snapshots sharing following 9 elements
  59. java.base@17.0.2/sun.nio.ch.EPoll.wait(Native Method)
  60. java.base@17.0.2/sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:118)
  61. java.base@17.0.2/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:129)
  62. java.base@17.0.2/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:146)
  63. io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:813)
  64. io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:460)
  65. io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
  66. io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
  67. java.base@17.0.2/java.lang.Thread.run(Thread.java:833)
  68. ----
  69. Note that `transport_worker` threads should always be in state `RUNNABLE`, even
  70. when waiting for input, because they block in the native `EPoll#wait` method. The `idle=`
  71. time reports the proportion of time the thread spent waiting for input, whereas the `cpu=` time
  72. reports the proportion of time the thread spent processing input it has received.
  73. If a `transport_worker` thread is not frequently idle, it may build up a
  74. backlog of work. This can cause delays in processing messages on the channels
  75. that it owns. It's hard to predict exactly which work will be delayed:
  76. * There are many more channels than threads. If work related to one channel is
  77. causing delays to its worker thread, all other channels owned by that thread
  78. will also suffer delays.
  79. * The mapping from TCP channels to worker threads is fixed but arbitrary. Each
  80. channel is assigned an owning thread in a round-robin fashion when the channel
  81. is opened. Each worker thread is responsible for many different kinds of
  82. channel.
  83. * There are many channels open between each pair of nodes. For each request,
  84. {es} will choose from the appropriate channels in a round-robin fashion. Some
  85. requests may end up on a channel owned by a delayed worker while other
  86. identical requests will be sent on a channel that's working smoothly.
  87. If the backlog builds up too far, some messages may be delayed by many seconds.
  88. The node might even <<cluster-fault-detection,fail its health checks>> and be
  89. removed from the cluster. Sometimes, you can find evidence of busy
  90. `transport_worker` threads using the <<cluster-nodes-hot-threads>> API.
  91. However, this API itself sends network messages so may not work correctly if
  92. the `transport_worker` threads are too busy. It is more reliable to use
  93. `jstack` to obtain stack dumps or use Java Flight Recorder to obtain a
  94. profiling trace. These tools are independent of any work the JVM is performing.
  95. It may also be possible to identify some reasons for delays from the server
  96. logs, particularly looking at warnings from
  97. `org.elasticsearch.transport.InboundHandler` and
  98. `org.elasticsearch.transport.OutboundHandler`. Warnings about long processing
  99. times from the `InboundHandler` are particularly indicative of incorrect
  100. threading behaviour, whereas the transmission time reported by the
  101. `OutboundHandler` includes time spent waiting for network congestion and the
  102. `transport_worker` thread is free to do other work during this time.