Debian Codegraph @

cluster_state.go

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