flush.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. [[indices-flush]]
  2. === Flush
  3. Flushing an index is the process of making sure that any data that is currently
  4. only stored in the <<index-modules-translog,transaction log>> is also
  5. permanently stored in the Lucene index. When restarting, {es} replays any
  6. unflushed operations from the transaction log into the Lucene index to bring it
  7. back into the state that it was in before the restart. {es} automatically
  8. triggers flushes as needed, using heuristics that trade off the size of the
  9. unflushed transaction log against the cost of performing each flush.
  10. Once each operation has been flushed it is permanently stored in the Lucene
  11. index. This may mean that there is no need to maintain an additional copy of it
  12. in the transaction log, unless <<index-modules-translog-retention,it is retained
  13. for some other reason>>. The transaction log is made up of multiple files,
  14. called _generations_, and {es} will delete any generation files once they are no
  15. longer needed, freeing up disk space.
  16. It is also possible to trigger a flush on one or more indices using the flush
  17. API, although it is rare for users to need to call this API directly. If you
  18. call the flush API after indexing some documents then a successful response
  19. indicates that {es} has flushed all the documents that were indexed before the
  20. flush API was called.
  21. [source,console]
  22. --------------------------------------------------
  23. POST twitter/_flush
  24. --------------------------------------------------
  25. // TEST[setup:twitter]
  26. [float]
  27. [[flush-parameters]]
  28. ==== Request Parameters
  29. The flush API accepts the following request parameters:
  30. [horizontal]
  31. `wait_if_ongoing`:: If set to `true` the flush operation will block until the
  32. flush can be executed if another flush operation is already executing. If set to
  33. `false` then an exception will be thrown on the shard level if another flush
  34. operation is already running. Defaults to `true`.
  35. `force`:: Whether a flush should be forced even if it is not necessarily needed
  36. i.e. if no changes will be committed to the index. This can be used to force
  37. the generation number of the transaction log to be incremented even if no
  38. uncommitted changes are present. This parameter should be considered internal.
  39. [float]
  40. [[flush-multi-index]]
  41. ==== Multi Index
  42. The flush API can be applied to more than one index with a single call, or even
  43. on `_all` the indices.
  44. [source,console]
  45. --------------------------------------------------
  46. POST kimchy,elasticsearch/_flush
  47. POST _flush
  48. --------------------------------------------------
  49. // TEST[s/^/PUT kimchy\nPUT elasticsearch\n/]
  50. [[synced-flush-api]]
  51. ==== Synced Flush
  52. {es} keeps track of which shards have received indexing activity recently, and
  53. considers shards that have not received any indexing operations for 5 minutes to
  54. be inactive. When a shard becomes inactive {es} performs a special kind of flush
  55. known as a _synced flush_. A synced flush performs a normal
  56. <<indices-flush,flush>> on each copy of the shard, and then adds a marker known
  57. as the `sync_id` to each copy to indicate that these copies have identical
  58. Lucene indices. Comparing the `sync_id` markers of the two copies is a very
  59. efficient way to check whether they have identical contents.
  60. When allocating shard copies, {es} must ensure that each replica contains the
  61. same data as the primary. If the shard copies have been synced-flushed and the
  62. replica shares a `sync_id` with the primary then {es} knows that the two copies
  63. have identical contents. This means there is no need to copy any segment files
  64. from the primary to the replica, which saves a good deal of time during
  65. recoveries and restarts.
  66. This is particularly useful for clusters having lots of indices which are very
  67. rarely updated, such as with time-based indices. Without the synced flush
  68. marker, recovery of this kind of cluster would be much slower.
  69. To check whether a shard has a `sync_id` marker or not, look for the `commit`
  70. section of the shard stats returned by the <<indices-stats,indices stats>> API:
  71. [source,console]
  72. --------------------------------------------------
  73. GET twitter/_stats?filter_path=**.commit&level=shards <1>
  74. --------------------------------------------------
  75. // TEST[s/^/PUT twitter\nPOST twitter\/_flush\/synced\n/]
  76. <1> `filter_path` is used to reduce the verbosity of the response, but is entirely optional
  77. which returns something similar to:
  78. [source,console-result]
  79. --------------------------------------------------
  80. {
  81. "indices": {
  82. "twitter": {
  83. "shards": {
  84. "0": [
  85. {
  86. "commit" : {
  87. "id" : "3M3zkw2GHMo2Y4h4/KFKCg==",
  88. "generation" : 3,
  89. "user_data" : {
  90. "translog_uuid" : "hnOG3xFcTDeoI_kvvvOdNA",
  91. "history_uuid" : "XP7KDJGiS1a2fHYiFL5TXQ",
  92. "local_checkpoint" : "-1",
  93. "translog_generation" : "2",
  94. "max_seq_no" : "-1",
  95. "sync_id" : "AVvFY-071siAOuFGEO9P", <1>
  96. "max_unsafe_auto_id_timestamp" : "-1",
  97. "min_retained_seq_no" : "0"
  98. },
  99. "num_docs" : 0
  100. }
  101. }
  102. ]
  103. }
  104. }
  105. }
  106. }
  107. --------------------------------------------------
  108. // TESTRESPONSE[s/"id" : "3M3zkw2GHMo2Y4h4\/KFKCg=="/"id": $body.indices.twitter.shards.0.0.commit.id/]
  109. // TESTRESPONSE[s/"translog_uuid" : "hnOG3xFcTDeoI_kvvvOdNA"/"translog_uuid": $body.indices.twitter.shards.0.0.commit.user_data.translog_uuid/]
  110. // TESTRESPONSE[s/"history_uuid" : "XP7KDJGiS1a2fHYiFL5TXQ"/"history_uuid": $body.indices.twitter.shards.0.0.commit.user_data.history_uuid/]
  111. // TESTRESPONSE[s/"sync_id" : "AVvFY-071siAOuFGEO9P"/"sync_id": $body.indices.twitter.shards.0.0.commit.user_data.sync_id/]
  112. <1> the `sync id` marker
  113. NOTE: The `sync_id` marker is removed as soon as the shard is flushed again, and
  114. {es} may trigger an automatic flush of a shard at any time if there are
  115. unflushed operations in the shard's translog. In practice this means that one
  116. should consider any indexing operation on an index as having removed its
  117. `sync_id` markers.
  118. [float]
  119. ==== Synced Flush API
  120. The Synced Flush API allows an administrator to initiate a synced flush
  121. manually. This can be particularly useful for a planned cluster restart where
  122. you can stop indexing but don't want to wait for 5 minutes until all indices
  123. are marked as inactive and automatically sync-flushed.
  124. You can request a synced flush even if there is ongoing indexing activity, and
  125. {es} will perform the synced flush on a "best-effort" basis: shards that do not
  126. have any ongoing indexing activity will be successfully sync-flushed, and other
  127. shards will fail to sync-flush. The successfully sync-flushed shards will have
  128. faster recovery times as long as the `sync_id` marker is not removed by a
  129. subsequent flush.
  130. [source,console]
  131. --------------------------------------------------
  132. POST twitter/_flush/synced
  133. --------------------------------------------------
  134. // TEST[setup:twitter]
  135. The response contains details about how many shards were successfully
  136. sync-flushed and information about any failure.
  137. Here is what it looks like when all shards of a two shards and one replica
  138. index successfully sync-flushed:
  139. [source,console-result]
  140. --------------------------------------------------
  141. {
  142. "_shards": {
  143. "total": 2,
  144. "successful": 2,
  145. "failed": 0
  146. },
  147. "twitter": {
  148. "total": 2,
  149. "successful": 2,
  150. "failed": 0
  151. }
  152. }
  153. --------------------------------------------------
  154. // TESTRESPONSE[s/"successful": 2/"successful": 1/]
  155. Here is what it looks like when one shard group failed due to pending
  156. operations:
  157. [source,js]
  158. --------------------------------------------------
  159. {
  160. "_shards": {
  161. "total": 4,
  162. "successful": 2,
  163. "failed": 2
  164. },
  165. "twitter": {
  166. "total": 4,
  167. "successful": 2,
  168. "failed": 2,
  169. "failures": [
  170. {
  171. "shard": 1,
  172. "reason": "[2] ongoing operations on primary"
  173. }
  174. ]
  175. }
  176. }
  177. --------------------------------------------------
  178. // NOTCONSOLE
  179. NOTE: The above error is shown when the synced flush fails due to concurrent
  180. indexing operations. The HTTP status code in that case will be `409 Conflict`.
  181. Sometimes the failures are specific to a shard copy. The copies that failed
  182. will not be eligible for fast recovery but those that succeeded still will be.
  183. This case is reported as follows:
  184. [source,js]
  185. --------------------------------------------------
  186. {
  187. "_shards": {
  188. "total": 4,
  189. "successful": 1,
  190. "failed": 1
  191. },
  192. "twitter": {
  193. "total": 4,
  194. "successful": 3,
  195. "failed": 1,
  196. "failures": [
  197. {
  198. "shard": 1,
  199. "reason": "unexpected error",
  200. "routing": {
  201. "state": "STARTED",
  202. "primary": false,
  203. "node": "SZNr2J_ORxKTLUCydGX4zA",
  204. "relocating_node": null,
  205. "shard": 1,
  206. "index": "twitter"
  207. }
  208. }
  209. ]
  210. }
  211. }
  212. --------------------------------------------------
  213. // NOTCONSOLE
  214. NOTE: When a shard copy fails to sync-flush, the HTTP status code returned will
  215. be `409 Conflict`.
  216. The synced flush API can be applied to more than one index with a single call,
  217. or even on `_all` the indices.
  218. [source,console]
  219. --------------------------------------------------
  220. POST kimchy,elasticsearch/_flush/synced
  221. POST _flush/synced
  222. --------------------------------------------------