cluster_state.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // ClusterStateService allows to get a comprehensive state information of the whole cluster.
  13. //
  14. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/cluster-state.html
  15. // for details.
  16. type ClusterStateService struct {
  17. client *Client
  18. pretty bool
  19. indices []string
  20. metrics []string
  21. allowNoIndices *bool
  22. expandWildcards string
  23. flatSettings *bool
  24. ignoreUnavailable *bool
  25. local *bool
  26. masterTimeout string
  27. }
  28. // NewClusterStateService creates a new ClusterStateService.
  29. func NewClusterStateService(client *Client) *ClusterStateService {
  30. return &ClusterStateService{
  31. client: client,
  32. indices: make([]string, 0),
  33. metrics: make([]string, 0),
  34. }
  35. }
  36. // Index is a list of index names. Use _all or an empty string to
  37. // perform the operation on all indices.
  38. func (s *ClusterStateService) Index(indices ...string) *ClusterStateService {
  39. s.indices = append(s.indices, indices...)
  40. return s
  41. }
  42. // Metric limits the information returned to the specified metric.
  43. // It can be one of: version, master_node, nodes, routing_table, metadata,
  44. // blocks, or customs.
  45. func (s *ClusterStateService) Metric(metrics ...string) *ClusterStateService {
  46. s.metrics = append(s.metrics, metrics...)
  47. return s
  48. }
  49. // AllowNoIndices indicates whether to ignore if a wildcard indices
  50. // expression resolves into no concrete indices.
  51. // (This includes `_all` string or when no indices have been specified).
  52. func (s *ClusterStateService) AllowNoIndices(allowNoIndices bool) *ClusterStateService {
  53. s.allowNoIndices = &allowNoIndices
  54. return s
  55. }
  56. // ExpandWildcards indicates whether to expand wildcard expression to
  57. // concrete indices that are open, closed or both..
  58. func (s *ClusterStateService) ExpandWildcards(expandWildcards string) *ClusterStateService {
  59. s.expandWildcards = expandWildcards
  60. return s
  61. }
  62. // FlatSettings, when set, returns settings in flat format (default: false).
  63. func (s *ClusterStateService) FlatSettings(flatSettings bool) *ClusterStateService {
  64. s.flatSettings = &flatSettings
  65. return s
  66. }
  67. // IgnoreUnavailable indicates whether specified concrete indices should be
  68. // ignored when unavailable (missing or closed).
  69. func (s *ClusterStateService) IgnoreUnavailable(ignoreUnavailable bool) *ClusterStateService {
  70. s.ignoreUnavailable = &ignoreUnavailable
  71. return s
  72. }
  73. // Local indicates whether to return local information. When set, it does not
  74. // retrieve the state from master node (default: false).
  75. func (s *ClusterStateService) Local(local bool) *ClusterStateService {
  76. s.local = &local
  77. return s
  78. }
  79. // MasterTimeout specifies timeout for connection to master.
  80. func (s *ClusterStateService) MasterTimeout(masterTimeout string) *ClusterStateService {
  81. s.masterTimeout = masterTimeout
  82. return s
  83. }
  84. // Pretty indicates that the JSON response be indented and human readable.
  85. func (s *ClusterStateService) Pretty(pretty bool) *ClusterStateService {
  86. s.pretty = pretty
  87. return s
  88. }
  89. // buildURL builds the URL for the operation.
  90. func (s *ClusterStateService) buildURL() (string, url.Values, error) {
  91. // Build URL
  92. metrics := strings.Join(s.metrics, ",")
  93. if metrics == "" {
  94. metrics = "_all"
  95. }
  96. indices := strings.Join(s.indices, ",")
  97. if indices == "" {
  98. indices = "_all"
  99. }
  100. path, err := uritemplates.Expand("/_cluster/state/{metrics}/{indices}", map[string]string{
  101. "metrics": metrics,
  102. "indices": indices,
  103. })
  104. if err != nil {
  105. return "", url.Values{}, err
  106. }
  107. // Add query string parameters
  108. params := url.Values{}
  109. if s.pretty {
  110. params.Set("pretty", "1")
  111. }
  112. if s.allowNoIndices != nil {
  113. params.Set("allow_no_indices", fmt.Sprintf("%v", *s.allowNoIndices))
  114. }
  115. if s.expandWildcards != "" {
  116. params.Set("expand_wildcards", s.expandWildcards)
  117. }
  118. if s.flatSettings != nil {
  119. params.Set("flat_settings", fmt.Sprintf("%v", *s.flatSettings))
  120. }
  121. if s.ignoreUnavailable != nil {
  122. params.Set("ignore_unavailable", fmt.Sprintf("%v", *s.ignoreUnavailable))
  123. }
  124. if s.local != nil {
  125. params.Set("local", fmt.Sprintf("%v", *s.local))
  126. }
  127. if s.masterTimeout != "" {
  128. params.Set("master_timeout", s.masterTimeout)
  129. }
  130. return path, params, nil
  131. }
  132. // Validate checks if the operation is valid.
  133. func (s *ClusterStateService) Validate() error {
  134. return nil
  135. }
  136. // Do executes the operation.
  137. func (s *ClusterStateService) Do(ctx context.Context) (*ClusterStateResponse, error) {
  138. // Check pre-conditions
  139. if err := s.Validate(); err != nil {
  140. return nil, err
  141. }
  142. // Get URL for request
  143. path, params, err := s.buildURL()
  144. if err != nil {
  145. return nil, err
  146. }
  147. // Get HTTP response
  148. res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
  149. if err != nil {
  150. return nil, err
  151. }
  152. // Return operation response
  153. ret := new(ClusterStateResponse)
  154. if err := s.client.decoder.Decode(res.Body, ret); err != nil {
  155. return nil, err
  156. }
  157. return ret, nil
  158. }
  159. // ClusterStateResponse is the response of ClusterStateService.Do.
  160. type ClusterStateResponse struct {
  161. ClusterName string `json:"cluster_name"`
  162. Version int64 `json:"version"`
  163. StateUUID string `json:"state_uuid"`
  164. MasterNode string `json:"master_node"`
  165. Blocks map[string]*clusterBlocks `json:"blocks"`
  166. Nodes map[string]*discoveryNode `json:"nodes"`
  167. Metadata *clusterStateMetadata `json:"metadata"`
  168. RoutingTable map[string]*clusterStateRoutingTable `json:"routing_table"`
  169. RoutingNodes *clusterStateRoutingNode `json:"routing_nodes"`
  170. Customs map[string]interface{} `json:"customs"`
  171. }
  172. type clusterBlocks struct {
  173. Global map[string]*clusterBlock `json:"global"` // id -> cluster block
  174. Indices map[string]*clusterBlock `json:"indices"` // index name -> cluster block
  175. }
  176. type clusterBlock struct {
  177. Description string `json:"description"`
  178. Retryable bool `json:"retryable"`
  179. DisableStatePersistence bool `json:"disable_state_persistence"`
  180. Levels []string `json:"levels"`
  181. }
  182. type clusterStateMetadata struct {
  183. ClusterUUID string `json:"cluster_uuid"`
  184. Templates map[string]*indexTemplateMetaData `json:"templates"` // template name -> index template metadata
  185. Indices map[string]*indexMetaData `json:"indices"` // index name _> meta data
  186. RoutingTable struct {
  187. Indices map[string]*indexRoutingTable `json:"indices"` // index name -> routing table
  188. } `json:"routing_table"`
  189. RoutingNodes struct {
  190. Unassigned []*shardRouting `json:"unassigned"`
  191. Nodes []*shardRouting `json:"nodes"`
  192. } `json:"routing_nodes"`
  193. Customs map[string]interface{} `json:"customs"`
  194. }
  195. type discoveryNode struct {
  196. Name string `json:"name"` // server name, e.g. "es1"
  197. TransportAddress string `json:"transport_address"` // e.g. inet[/1.2.3.4:9300]
  198. Attributes map[string]interface{} `json:"attributes"` // e.g. { "data": true, "master": true }
  199. }
  200. type clusterStateRoutingTable struct {
  201. Indices map[string]interface{} `json:"indices"`
  202. }
  203. type clusterStateRoutingNode struct {
  204. Unassigned []*shardRouting `json:"unassigned"`
  205. // Node Id -> shardRouting
  206. Nodes map[string][]*shardRouting `json:"nodes"`
  207. }
  208. type indexTemplateMetaData struct {
  209. Template string `json:"template"` // e.g. "store-*"
  210. Order int `json:"order"`
  211. Settings map[string]interface{} `json:"settings"` // index settings
  212. Mappings map[string]interface{} `json:"mappings"` // type name -> mapping
  213. }
  214. type indexMetaData struct {
  215. State string `json:"state"`
  216. Settings map[string]interface{} `json:"settings"`
  217. Mappings map[string]interface{} `json:"mappings"`
  218. Aliases []string `json:"aliases"` // e.g. [ "alias1", "alias2" ]
  219. }
  220. type indexRoutingTable struct {
  221. Shards map[string]*shardRouting `json:"shards"`
  222. }
  223. type shardRouting struct {
  224. State string `json:"state"`
  225. Primary bool `json:"primary"`
  226. Node string `json:"node"`
  227. RelocatingNode string `json:"relocating_node"`
  228. Shard int `json:"shard"`
  229. Index string `json:"index"`
  230. Version int64 `json:"version"`
  231. RestoreSource *RestoreSource `json:"restore_source"`
  232. AllocationId *allocationId `json:"allocation_id"`
  233. UnassignedInfo *unassignedInfo `json:"unassigned_info"`
  234. }
  235. type RestoreSource struct {
  236. Repository string `json:"repository"`
  237. Snapshot string `json:"snapshot"`
  238. Version string `json:"version"`
  239. Index string `json:"index"`
  240. }
  241. type allocationId struct {
  242. Id string `json:"id"`
  243. RelocationId string `json:"relocation_id"`
  244. }
  245. type unassignedInfo struct {
  246. Reason string `json:"reason"`
  247. At string `json:"at"`
  248. Details string `json:"details"`
  249. }