PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/development/elasticsearch.md

https://gitlab.com/xhang/gitlab
Markdown | 246 lines | 152 code | 94 blank | 0 comment | 0 complexity | 89a58378a6e264e3e8cb18451d1d9038 MD5 | raw file
  1. # Elasticsearch knowledge **(STARTER ONLY)**
  2. This area is to maintain a compendium of useful information when working with elasticsearch.
  3. Information on how to enable Elasticsearch and perform the initial indexing is kept in ../integration/elasticsearch.md#enabling-elasticsearch
  4. ## Deep Dive
  5. In June 2019, Mario de la Ossa hosted a [Deep Dive] on GitLab's [Elasticsearch integration] to share his domain specific knowledge with anyone who may work in this part of the code base in the future. You can find the [recording on YouTube], and the slides on [Google Slides] and in [PDF]. Everything covered in this deep dive was accurate as of GitLab 12.0, and while specific details may have changed since then, it should still serve as a good introduction.
  6. [Deep Dive]: https://gitlab.com/gitlab-org/create-stage/issues/1
  7. [Elasticsearch integration]: ../integration/elasticsearch.md
  8. [recording on YouTube]: https://www.youtube.com/watch?v=vrvl-tN2EaA
  9. [Google Slides]: https://docs.google.com/presentation/d/1H-pCzI_LNrgrL5pJAIQgvLX8Ji0-jIKOg1QeJQzChug/edit
  10. [PDF]: https://gitlab.com/gitlab-org/create-stage/uploads/c5aa32b6b07476fa8b597004899ec538/Elasticsearch_Deep_Dive.pdf
  11. ## Initial installation on OS X
  12. It is recommended to use the Docker image. After installing docker you can immediately spin up an instance with
  13. ```
  14. docker run --name elastic56 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:5.6.12
  15. ```
  16. and use `docker stop elastic56` and `docker start elastic56` to stop/start it.
  17. ### Installing on the host
  18. We currently only support Elasticsearch [5.6 to 6.x](../integration/elasticsearch.md#version-requirements)
  19. Version 5.6 is available on homebrew and is the recommended version to use in order to test compatibility.
  20. ```
  21. brew install elasticsearch@5.6
  22. ```
  23. There is no need to install any plugins
  24. ## New repo indexer (beta)
  25. If you're interested on working with the new beta repo indexer, all you need to do is:
  26. ```sh
  27. git clone git@gitlab.com:gitlab-org/gitlab-elasticsearch-indexer.git
  28. make
  29. make install
  30. ```
  31. this adds `gitlab-elasticsearch-indexer` to `$GOPATH/bin`, please make sure that is in your `$PATH`. After that GitLab will find it and you'll be able to enable it in the admin settings area.
  32. **note:** `make` will not recompile the executable unless you do `make clean` beforehand
  33. ## Helpful rake tasks
  34. - `gitlab:elastic:test:index_size`: Tells you how much space the current index is using, as well as how many documents are in the index.
  35. - `gitlab:elastic:test:index_size_change`: Outputs index size, reindexes, and outputs index size again. Useful when testing improvements to indexing size.
  36. Additionally, if you need large repos or multiple forks for testing, please consider [following these instructions](rake_tasks.md#extra-project-seed-options)
  37. ## How does it work?
  38. The Elasticsearch integration depends on an external indexer. We ship an [indexer written in Go](https://gitlab.com/gitlab-org/gitlab-elasticsearch-indexer). The user must trigger the initial indexing via a rake task but, after this is done, GitLab itself will trigger reindexing when required via `after_` callbacks on create, update, and destroy that are inherited from [/ee/app/models/concerns/elastic/application_search.rb](https://gitlab.com/gitlab-org/gitlab/blob/master/ee/app/models/concerns/elastic/application_search.rb).
  39. All indexing after the initial one is done via `ElasticIndexerWorker` (sidekiq jobs).
  40. Search queries are generated by the concerns found in [ee/app/models/concerns/elastic](https://gitlab.com/gitlab-org/gitlab/tree/master/ee/app/models/concerns/elastic). These concerns are also in charge of access control, and have been a historic source of security bugs so please pay close attention to them!
  41. ## Existing Analyzers/Tokenizers/Filters
  42. These are all defined in <https://gitlab.com/gitlab-org/gitlab/blob/master/ee/lib/elasticsearch/git/model.rb>
  43. ### Analyzers
  44. #### `path_analyzer`
  45. Used when indexing blobs' paths. Uses the `path_tokenizer` and the `lowercase` and `asciifolding` filters.
  46. Please see the `path_tokenizer` explanation below for an example.
  47. #### `sha_analyzer`
  48. Used in blobs and commits. Uses the `sha_tokenizer` and the `lowercase` and `asciifolding` filters.
  49. Please see the `sha_tokenizer` explanation later below for an example.
  50. #### `code_analyzer`
  51. Used when indexing a blob's filename and content. Uses the `whitespace` tokenizer and the filters: `code`, `edgeNGram_filter`, `lowercase`, and `asciifolding`
  52. The `whitespace` tokenizer was selected in order to have more control over how tokens are split. For example the string `Foo::bar(4)` needs to generate tokens like `Foo` and `bar(4)` in order to be properly searched.
  53. Please see the `code` filter for an explanation on how tokens are split.
  54. #### `code_search_analyzer`
  55. Not directly used for indexing, but rather used to transform a search input. Uses the `whitespace` tokenizer and the `lowercase` and `asciifolding` filters.
  56. ### Tokenizers
  57. #### `sha_tokenizer`
  58. This is a custom tokenizer that uses the [`edgeNGram` tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/analysis-edgengram-tokenizer.html) to allow SHAs to be searcheable by any sub-set of it (minimum of 5 chars).
  59. Example:
  60. `240c29dc7e` becomes:
  61. - `240c2`
  62. - `240c29`
  63. - `240c29d`
  64. - `240c29dc`
  65. - `240c29dc7`
  66. - `240c29dc7e`
  67. #### `path_tokenizer`
  68. This is a custom tokenizer that uses the [`path_hierarchy` tokenizer](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/analysis-pathhierarchy-tokenizer.html) with `reverse: true` in order to allow searches to find paths no matter how much or how little of the path is given as input.
  69. Example:
  70. `'/some/path/application.js'` becomes:
  71. - `'/some/path/application.js'`
  72. - `'some/path/application.js'`
  73. - `'path/application.js'`
  74. - `'application.js'`
  75. ### Filters
  76. #### `code`
  77. Uses a [Pattern Capture token filter](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/analysis-pattern-capture-tokenfilter.html) to split tokens into more easily searched versions of themselves.
  78. Patterns:
  79. - `"(\\p{Ll}+|\\p{Lu}\\p{Ll}+|\\p{Lu}+)"`: captures CamelCased and lowedCameCased strings as separate tokens
  80. - `"(\\d+)"`: extracts digits
  81. - `"(?=([\\p{Lu}]+[\\p{L}]+))"`: captures CamelCased strings recursively. Ex: `ThisIsATest` => `[ThisIsATest, IsATest, ATest, Test]`
  82. - `'"((?:\\"|[^"]|\\")*)"'`: captures terms inside quotes, removing the quotes
  83. - `"'((?:\\'|[^']|\\')*)'"`: same as above, for single-quotes
  84. - `'\.([^.]+)(?=\.|\s|\Z)'`: separate terms with periods in-between
  85. - `'\/?([^\/]+)(?=\/|\b)'`: separate path terms `like/this/one`
  86. #### `edgeNGram_filter`
  87. Uses an [Edge NGram token filter](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/analysis-edgengram-tokenfilter.html) to allow inputs with only parts of a token to find the token. For example it would turn `glasses` into permutations starting with `gl` and ending with `glasses`, which would allow a search for "`glass`" to find the original token `glasses`
  88. ## Gotchas
  89. - Searches can have their own analyzers. Remember to check when editing analyzers
  90. - `Character` filters (as opposed to token filters) always replace the original character, so they're not a good choice as they can hinder exact searches
  91. ## Zero downtime reindexing with multiple indices
  92. Currently GitLab can only handle a single version of setting. Any setting/schema changes would require reindexing everything from scratch. Since reindexing can take a long time, this can cause search functionality downtime.
  93. To avoid downtime, GitLab is working to support multiple indices that
  94. can function at the same time. Whenever the schema changes, the admin
  95. will be able to create a new index and reindex to it, while searches
  96. continue to go to the older, stable index. Any data updates will be
  97. forwarded to both indices. Once the new index is ready, an admin can
  98. mark it active, which will direct all searches to it, and remove the old
  99. index.
  100. This is also helpful for migrating to new servers, e.g. moving to/from AWS.
  101. Currently we are on the process of migrating to this new design. Everything is hardwired to work with one single version for now.
  102. ### Architecture
  103. The traditional setup, provided by `elasticsearch-rails`, is to communicate through its internal proxy classes. Developers would write model-specific logic in a module for the model to include in (e.g. `SnippetsSearch`). The `__elasticsearch__` methods would return a proxy object, e.g.:
  104. - `Issue.__elasticsearch__` returns an instance of `Elasticsearch::Model::Proxy::ClassMethodsProxy`
  105. - `Issue.first.__elasticsearch__` returns an instance of `Elasticsearch::Model::Proxy::InstanceMethodsProxy`.
  106. These proxy objects would talk to Elasticsearch server directly (see top half of the diagram).
  107. ![Elasticsearch Architecture](img/elasticsearch_architecture.svg)
  108. In the planned new design, each model would have a pair of corresponding subclassed proxy objects, in which model-specific logic is located. For example, `Snippet` would have `SnippetClassProxy` and `SnippetInstanceProxy` (being subclass of `Elasticsearch::Model::Proxy::ClassMethodsProxy` and `Elasticsearch::Model::Proxy::InstanceMethodsProxy`, respectively).
  109. `__elasticsearch__` would represent another layer of proxy object, keeping track of multiple actual proxy objects. It would forward method calls to the appropriate index. For example:
  110. - `model.__elasticsearch__.search` would be forwarded to the one stable index, since it is a read operation.
  111. - `model.__elasticsearch__.update_document` would be forwarded to all indices, to keep all indices up-to-date.
  112. The global configurations per version are now in the `Elastic::(Version)::Config` class. You can change mappings there.
  113. ### Creating new version of schema
  114. NOTE: **Note:** this is not applicable yet as multiple indices functionality is not fully implemented.
  115. Folders like `ee/lib/elastic/v12p1` contain snapshots of search logic from different versions. To keep a continuous Git history, the latest version lives under `ee/lib/elastic/latest`, but its classes are aliased under an actual version (e.g. `ee/lib/elastic/v12p3`). When referencing these classes, never use the `Latest` namespace directly, but use the actual version (e.g. `V12p3`).
  116. The version name basically follows GitLab's release version. If setting is changed in 12.3, we will create a new namespace called `V12p3` (p stands for "point"). Raise an issue if there is a need to name a version differently.
  117. If the current version is `v12p1`, and we need to create a new version for `v12p3`, the steps are as follows:
  118. 1. Copy the entire folder of `v12p1` as `v12p3`
  119. 1. Change the namespace for files under `v12p3` folder from `V12p1` to `V12p3` (which are still aliased to `Latest`)
  120. 1. Delete `v12p1` folder
  121. 1. Copy the entire folder of `latest` as `v12p1`
  122. 1. Change the namespace for files under `v12p1` folder from `Latest` to `V12p1`
  123. 1. Make changes to files under the `latest` folder as needed
  124. ## Troubleshooting
  125. ### Getting `flood stage disk watermark [95%] exceeded`
  126. You might get an error such as
  127. ```
  128. [2018-10-31T15:54:19,762][WARN ][o.e.c.r.a.DiskThresholdMonitor] [pval5Ct]
  129. flood stage disk watermark [95%] exceeded on
  130. [pval5Ct7SieH90t5MykM5w][pval5Ct][/usr/local/var/lib/elasticsearch/nodes/0] free: 56.2gb[3%],
  131. all indices on this node will be marked read-only
  132. ```
  133. This is because you've exceeded the disk space threshold - it thinks you don't have enough disk space left, based on the default 95% threshold.
  134. In addition, the `read_only_allow_delete` setting will be set to `true`. It will block indexing, `forcemerge`, etc
  135. ```
  136. curl "http://localhost:9200/gitlab-development/_settings?pretty"
  137. ```
  138. Add this to your `elasticsearch.yml` file:
  139. ```
  140. # turn off the disk allocator
  141. cluster.routing.allocation.disk.threshold_enabled: false
  142. ```
  143. _or_
  144. ```
  145. # set your own limits
  146. cluster.routing.allocation.disk.threshold_enabled: true
  147. cluster.routing.allocation.disk.watermark.flood_stage: 5gb # ES 6.x only
  148. cluster.routing.allocation.disk.watermark.low: 15gb
  149. cluster.routing.allocation.disk.watermark.high: 10gb
  150. ```
  151. Restart Elasticsearch, and the `read_only_allow_delete` will clear on it's own.
  152. _from "Disk-based Shard Allocation | Elasticsearch Reference" [5.6](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/disk-allocator.html#disk-allocator) and [6.x](https://www.elastic.co/guide/en/elasticsearch/reference/6.x/disk-allocator.html)_