Debian Codegraph @

cluster_health.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 // ClusterHealthService allows to get a very simple status on the health of the cluster.
17 //
18 // See http://www.elastic.co/guide/en/elasticsearch/reference/5.2/cluster-health.html
19 // for details.
20 type ClusterHealthService struct {
21 client *Client
22 pretty bool
23 indices []string
24 level string
25 local *bool
26 masterTimeout string
27 timeout string
28 waitForActiveShards *int
29 waitForNodes string
30 waitForNoRelocatingShards *bool
31 waitForStatus string
32 }
33
34 // NewClusterHealthService creates a new ClusterHealthService.
35 func NewClusterHealthService(client *Client) *ClusterHealthService {
36 return &ClusterHealthService{
37 client: client,
38 indices: make([]string, 0),
39 }
40 }
41
42 // Index limits the information returned to specific indices.
43 func (s *ClusterHealthService) Index(indices ...string) *ClusterHealthService {
44 s.indices = append(s.indices, indices...)
45 return s
46 }
47
48 // Level specifies the level of detail for returned information.
49 func (s *ClusterHealthService) Level(level string) *ClusterHealthService {
50 s.level = level
51 return s
52 }
53
54 // Local indicates whether to return local information. If it is true,
55 // we do not retrieve the state from master node (default: false).
56 func (s *ClusterHealthService) Local(local bool) *ClusterHealthService {
57 s.local = &local
58 return s
59 }
60
61 // MasterTimeout specifies an explicit operation timeout for connection to master node.
62 func (s *ClusterHealthService) MasterTimeout(masterTimeout string) *ClusterHealthService {
63 s.masterTimeout = masterTimeout
64 return s
65 }
66
67 // Timeout specifies an explicit operation timeout.
68 func (s *ClusterHealthService) Timeout(timeout string) *ClusterHealthService {
69 s.timeout = timeout
70 return s
71 }
72
73 // WaitForActiveShards can be used to wait until the specified number of shards are active.
74 func (s *ClusterHealthService) WaitForActiveShards(waitForActiveShards int) *ClusterHealthService {
75 s.waitForActiveShards = &waitForActiveShards
76 return s
77 }
78
79 // WaitForNodes can be used to wait until the specified number of nodes are available.
80 // Example: "12" to wait for exact values, ">12" and "<12" for ranges.
81 func (s *ClusterHealthService) WaitForNodes(waitForNodes string) *ClusterHealthService {
82 s.waitForNodes = waitForNodes
83 return s
84 }
85
86 // WaitForNoRelocatingShards can be used to wait until all shard relocations are finished.
87 func (s *ClusterHealthService) WaitForNoRelocatingShards(waitForNoRelocatingShards bool) *ClusterHealthService {
88 s.waitForNoRelocatingShards = &waitForNoRelocatingShards
89 return s
90 }
91
92 // WaitForStatus can be used to wait until the cluster is in a specific state.
93 // Valid values are: green, yellow, or red.
94 func (s *ClusterHealthService) WaitForStatus(waitForStatus string) *ClusterHealthService {
95 s.waitForStatus = waitForStatus
96 return s
97 }
98
99 // WaitForGreenStatus will wait for the "green" state.
100 func (s *ClusterHealthService) WaitForGreenStatus() *ClusterHealthService {
101 return s.WaitForStatus("green")
102 }
103
104 // WaitForYellowStatus will wait for the "yellow" state.
105 func (s *ClusterHealthService) WaitForYellowStatus() *ClusterHealthService {
106 return s.WaitForStatus("yellow")
107 }
108
109 // Pretty indicates that the JSON response be indented and human readable.
110 func (s *ClusterHealthService) Pretty(pretty bool) *ClusterHealthService {
111 s.pretty = pretty
112 return s
113 }
114
115 // buildURL builds the URL for the operation.
116 func (s *ClusterHealthService) buildURL() (string, url.Values, error) {
117 // Build URL
118 var err error
119 var path string
120 if len(s.indices) > 0 {
121 path, err = uritemplates.Expand("/_cluster/health/{index}", map[string]string{
122 "index": strings.Join(s.indices, ","),
123 })
124 } else {
125 path = "/_cluster/health"
126 }
127 if err != nil {
128 return "", url.Values{}, err
129 }
130
131 // Add query string parameters
132 params := url.Values{}
133 if s.pretty {
134 params.Set("pretty", "1")
135 }
136 if s.level != "" {
137 params.Set("level", s.level)
138 }
139 if s.local != nil {
140 params.Set("local", fmt.Sprintf("%v", *s.local))
141 }
142 if s.masterTimeout != "" {
143 params.Set("master_timeout", s.masterTimeout)
144 }
145 if s.timeout != "" {
146 params.Set("timeout", s.timeout)
147 }
148 if s.waitForActiveShards != nil {
149 params.Set("wait_for_active_shards", fmt.Sprintf("%v", s.waitForActiveShards))
150 }
151 if s.waitForNodes != "" {
152 params.Set("wait_for_nodes", s.waitForNodes)
153 }
154 if s.waitForNoRelocatingShards != nil {
155 params.Set("wait_for_no_relocating_shards", fmt.Sprintf("%v", *s.waitForNoRelocatingShards))
156 }
157 if s.waitForStatus != "" {
158 params.Set("wait_for_status", s.waitForStatus)
159 }
160 return path, params, nil
161 }
162
163 // Validate checks if the operation is valid.
164 func (s *ClusterHealthService) Validate() error {
165 return nil
166 }
167
168 // Do executes the operation.
169 func (s *ClusterHealthService) Do(ctx context.Context) (*ClusterHealthResponse, error) {
170 // Check pre-conditions
171 if err := s.Validate(); err != nil {
172 return nil, err
173 }
174
175 // Get URL for request
176 path, params, err := s.buildURL()
177 if err != nil {
178 return nil, err
179 }
180
181 // Get HTTP response
182 res, err := s.client.PerformRequest(ctx, "GET", path, params, nil)
183 if err != nil {
184 return nil, err
185 }
186
187 // Return operation response
188 ret := new(ClusterHealthResponse)
189 if err := s.client.decoder.Decode(res.Body, ret); err != nil {
190 return nil, err
191 }
192 return ret, nil
193 }
194
195 // ClusterHealthResponse is the response of ClusterHealthService.Do.
196 type ClusterHealthResponse struct {
197 ClusterName string `json:"cluster_name"`
198 Status string `json:"status"`
199 TimedOut bool `json:"timed_out"`
200 NumberOfNodes int `json:"number_of_nodes"`
201 NumberOfDataNodes int `json:"number_of_data_nodes"`
202 ActivePrimaryShards int `json:"active_primary_shards"`
203 ActiveShards int `json:"active_shards"`
204 RelocatingShards int `json:"relocating_shards"`
205 InitializingShards int `json:"initializing_shards"`
206 UnassignedShards int `json:"unassigned_shards"`
207 DelayedUnassignedShards int `json:"delayed_unassigned_shards"`
208 NumberOfPendingTasks int `json:"number_of_pending_tasks"`
209 NumberOfInFlightFetch int `json:"number_of_in_flight_fetch"`
210 TaskMaxWaitTimeInQueueInMillis int `json:"task_max_waiting_in_queue_millis"`
211 ActiveShardsPercentAsNumber float64 `json:"active_shards_percent_as_number"`
212
213 // Validation failures -> index name -> array of validation failures
214 ValidationFailures []map[string][]string `json:"validation_failures"`
215
216 // Index name -> index health
217 Indices map[string]*ClusterIndexHealth `json:"indices"`
218 }
219
220 // ClusterIndexHealth will be returned as part of ClusterHealthResponse.
221 type ClusterIndexHealth struct {
222 Status string `json:"status"`
223 NumberOfShards int `json:"number_of_shards"`
224 NumberOfReplicas int `json:"number_of_replicas"`
225 ActivePrimaryShards int `json:"active_primary_shards"`
226 ActiveShards int `json:"active_shards"`
227 RelocatingShards int `json:"relocating_shards"`
228 InitializingShards int `json:"initializing_shards"`
229 UnassignedShards int `json:"unassigned_shards"`
230 // Validation failures
231 ValidationFailures []string `json:"validation_failures"`
232 // Shards by id, e.g. "0" or "1"
233 Shards map[string]*ClusterShardHealth `json:"shards"`
234 }
235
236 // ClusterShardHealth will be returned as part of ClusterHealthResponse.
237 type ClusterShardHealth struct {
238 Status string `json:"status"`
239 PrimaryActive bool `json:"primary_active"`
240 ActiveShards int `json:"active_shards"`
241 RelocatingShards int `json:"relocating_shards"`
242 InitializingShards int `json:"initializing_shards"`
243 UnassignedShards int `json:"unassigned_shards"`
244 }