forked from github/vulcanizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes.go
607 lines (471 loc) · 19.5 KB
/
es.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package vulcanizer
import (
"errors"
"fmt"
"net/http"
"sort"
"strings"
"time"
"github.com/jeremywohl/flatten"
"github.com/parnurzeal/gorequest"
"github.com/tidwall/gjson"
)
//Hold the values for what values are in the cluster.allocation.exclude settings.
//Relevant Elasticsearch documentation: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/allocation-filtering.html
type ExcludeSettings struct {
Ips, Hosts, Names []string
}
//Hold connection information to a Elasticsearch cluster.
type Client struct {
Host string
Port int
}
//Holds information about an Elasticsearch node, based on the _cat/nodes API: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cat-nodes.html
type Node struct {
Name string `json:"name"`
Ip string `json:"ip"`
Id string `json:"id"`
Role string `json:"role"`
Master string `json:"master"`
Jdk string `json:"jdk"`
}
//Holds information about an Elasticsearch index, based on the _cat/indices API: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cat-indices.html
type Index struct {
Health string `json:"health"`
Status string `json:"status"`
Name string `json:"index"`
PrimaryShards int `json:"pri,string"`
ReplicaCount int `json:"rep,string"`
IndexSize string `json:"store.size"`
DocumentCount int `json:"docs.count,string"`
}
//Holds information about the health of an Elasticsearch cluster, based on the cluster health API: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cluster-health.html
type ClusterHealth struct {
Cluster string `json:"cluster_name"`
Status string `json:"status"`
ActiveShards int `json:"active_shards"`
RelocatingShards int `json:"relocating_shards"`
InitializingShards int `json:"initializing_shards"`
UnassignedShards int `json:"unassigned_shards"`
ActiveShardsPercentage float64 `json:"active_shards_percent_as_number"`
Message string
RawIndices map[string]IndexHealth `json:"indices"`
HealthyIndices []IndexHealth
UnhealthyIndices []IndexHealth
}
//Holds information about the health of an Elasticsearch index, based on the index level of the cluster health API: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cluster-health.html
type IndexHealth struct {
Name string
Status string `json:"status"`
ActiveShards int `json:"active_shards"`
RelocatingShards int `json:"relocating_shards"`
InitializingShards int `json:"initializing_shards"`
UnassignedShards int `json:"unassigned_shards"`
}
//Holds slices for persistent and transient cluster settings.
type ClusterSettings struct {
PersistentSettings []ClusterSetting
TransientSettings []ClusterSetting
}
//A setting name and value with the setting name to be a "collapsed" version of the setting. A setting of:
// { "indices": { "recovery" : { "max_bytes_per_sec": "10mb" } } }
//would be represented by:
// ClusterSetting{ Setting: "indices.recovery.max_bytes_per_sec", Value: "10mb" }
type ClusterSetting struct {
Setting, Value string
}
type snapshotWrapper struct {
Snapshots []Snapshot `json:"snapshots"`
}
type acknowledgedResponse struct {
Acknowledged bool `json:"acknowledged"`
}
//Holds information about an Elasticsearch snapshot, based on the snapshot API: https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html
type Snapshot struct {
State string `json:"state"`
Name string `json:"snapshot"`
StartTime time.Time `json:"start_time,string"`
EndTime time.Time `json:"end_time,string"`
DurationMillis int `json:"duration_in_millis"`
Indices []string `json:"indices"`
Shards struct {
Total int `json:"total"`
Failed int `json:"failed"`
Successful int `json:"successful"`
} `json:"shards"`
Failures []struct {
Index string `json:"index"`
ShardID int `json:"shard_id"`
Reason string `json:"reason"`
NodeID string `json:"node_id"`
Status string `json:"status"`
} `json:"failures"`
}
//Holds information about an Elasticsearch snapshot repository.
type Repository struct {
Name string
Type string
Settings map[string]interface{}
}
//Initialize a new vulcanizer client to use.
func NewClient(host string, port int) *Client {
return &Client{host, port}
}
const clusterSettingsPath = "_cluster/settings"
func settingsToStructs(rawJson string) ([]ClusterSetting, error) {
flatSettings, err := flatten.FlattenString(rawJson, "", flatten.DotStyle)
if err != nil {
return nil, err
}
settingsMap, _ := gjson.Parse(flatSettings).Value().(map[string]interface{})
keys := []string{}
for k, v := range settingsMap {
strValue := v.(string)
if strValue != "" {
keys = append(keys, k)
}
}
sort.Strings(keys)
var clusterSettings []ClusterSetting
for _, k := range keys {
setting := ClusterSetting{
Setting: k,
Value: settingsMap[k].(string),
}
clusterSettings = append(clusterSettings, setting)
}
return clusterSettings, nil
}
func handleErrWithBytes(s *gorequest.SuperAgent) ([]byte, error) {
response, body, errs := s.EndBytes()
if len(errs) > 0 {
return nil, combineErrors(errs)
}
if response.StatusCode != http.StatusOK {
errorMessage := fmt.Sprintf("Bad HTTP Status from Elasticsearch: %v, %s", response.StatusCode, body)
return nil, errors.New(errorMessage)
}
return body, nil
}
func handleErrWithStruct(s *gorequest.SuperAgent, v interface{}) error {
response, body, errs := s.EndStruct(v)
if len(errs) > 0 {
return combineErrors(errs)
}
if response.StatusCode != http.StatusOK {
errorMessage := fmt.Sprintf("Bad HTTP Status from Elasticsearch: %v, %s", response.StatusCode, body)
return errors.New(errorMessage)
}
return nil
}
func (c *Client) buildGetRequest(path string) *gorequest.SuperAgent {
return gorequest.New().Get(fmt.Sprintf("http://%s:%v/%s", c.Host, c.Port, path)).Set("Accept", "application/json")
}
func (c *Client) buildPutRequest(path string) *gorequest.SuperAgent {
return gorequest.New().Put(fmt.Sprintf("http://%s:%v/%s", c.Host, c.Port, path))
}
func (c *Client) buildDeleteRequest(path string) *gorequest.SuperAgent {
return gorequest.New().Delete(fmt.Sprintf("http://%s:%v/%s", c.Host, c.Port, path))
}
func (c *Client) buildPostRequest(path string) *gorequest.SuperAgent {
return gorequest.New().Post(fmt.Sprintf("http://%s:%v/%s", c.Host, c.Port, path))
}
// Get current cluster settings for shard allocation exclusion rules.
func (c *Client) GetClusterExcludeSettings() (ExcludeSettings, error) {
body, err := handleErrWithBytes(c.buildGetRequest(clusterSettingsPath))
if err != nil {
return ExcludeSettings{}, err
}
excludedArray := gjson.GetManyBytes(body, "transient.cluster.routing.allocation.exclude._ip", "transient.cluster.routing.allocation.exclude._name", "transient.cluster.routing.allocation.exclude._host")
excludeSettings := excludeSettingsFromJson(excludedArray)
return excludeSettings, nil
}
//Set shard allocation exclusion rules such that the Elasticsearch node with the name `serverToDrain` is excluded. This should cause Elasticsearch to migrate shards away from that node.
//
//Use case: You need to restart an Elasticsearch node. In order to do so safely, you should migrate data away from it. Calling `DrainServer` with the node name will move data off of the specified node.
func (c *Client) DrainServer(serverToDrain string) (ExcludeSettings, error) {
excludeSettings, err := c.GetClusterExcludeSettings()
if err != nil {
return ExcludeSettings{}, err
}
excludeSettings.Names = append(excludeSettings.Names, serverToDrain)
agent := c.buildPutRequest(clusterSettingsPath).
Set("Content-Type", "application/json").
Send(fmt.Sprintf(`{"transient" : { "cluster.routing.allocation.exclude._name" : "%s"}}`, strings.Join(excludeSettings.Names, ",")))
_, err = handleErrWithBytes(agent)
if err != nil {
return ExcludeSettings{}, err
}
return excludeSettings, nil
}
//Set shard allocation exclusion rules such that the Elasticsearch node with the name `serverToFill` is no longer being excluded. This should cause Elasticsearch to migrate shards to that node.
//
//Use case: You have completed maintenance on an Elasticsearch node and it's ready to hold data again. Calling `FillOneServer` with the node name will remove that node name from the shard exclusion rules and allow data to be relocated onto the node.
func (c *Client) FillOneServer(serverToFill string) (ExcludeSettings, error) {
// Get the current list of strings
excludeSettings, err := c.GetClusterExcludeSettings()
if err != nil {
return ExcludeSettings{}, err
}
serverToFill = strings.TrimSpace(serverToFill)
newNamesDrained := []string{}
for _, s := range excludeSettings.Names {
if s != serverToFill {
newNamesDrained = append(newNamesDrained, s)
}
}
agent := c.buildPutRequest(clusterSettingsPath).
Set("Content-Type", "application/json").
Send(fmt.Sprintf(`{"transient" : { "cluster.routing.allocation.exclude._name" : "%s"}}`, strings.Join(newNamesDrained, ",")))
_, err = handleErrWithBytes(agent)
if err != nil {
return ExcludeSettings{}, err
}
return c.GetClusterExcludeSettings()
}
//Removes all shard allocation exclusion rules.
//
//Use case: You had been performing maintenance on a number of Elasticsearch nodes. They are all ready to receive data again. Calling `FillAll` will remove all the allocation exclusion rules on the cluster, allowing Elasticsearch to freely allocate shards on the previously excluded nodes.
func (c *Client) FillAll() (ExcludeSettings, error) {
agent := c.buildPutRequest(clusterSettingsPath).
Set("Content-Type", "application/json").
Send(`{"transient" : { "cluster.routing.allocation.exclude" : { "_name" : "", "_ip" : "", "_host" : ""}}}`)
body, err := handleErrWithBytes(agent)
if err != nil {
return ExcludeSettings{}, err
}
excludedArray := gjson.GetManyBytes(body, "transient.cluster.routing.allocation.exclude._ip", "transient.cluster.routing.allocation.exclude._name", "transient.cluster.routing.allocation.exclude._host")
return excludeSettingsFromJson(excludedArray), nil
}
//Get all the nodes in the cluster.
//
//Use case: You want to see what nodes Elasticsearch considers part of the cluster.
func (c *Client) GetNodes() ([]Node, error) {
var nodes []Node
agent := c.buildGetRequest("_cat/nodes?h=master,role,name,ip,id,jdk")
err := handleErrWithStruct(agent, &nodes)
if err != nil {
return nil, err
}
return nodes, nil
}
//Get all the indices in the cluster.
//
//Use case: You want to see some basic info on all the indices of the cluster.
func (c *Client) GetIndices() ([]Index, error) {
var indices []Index
err := handleErrWithStruct(c.buildGetRequest("_cat/indices?h=health,status,index,pri,rep,store.size,docs.count"), &indices)
if err != nil {
return nil, err
}
return indices, nil
}
//Get the health of the cluster.
//
//Use case: You want to see information needed to determine if the Elasticsearch cluster is healthy (green) or not (yellow/red).
func (c *Client) GetHealth() (ClusterHealth, error) {
var health ClusterHealth
err := handleErrWithStruct(c.buildGetRequest("_cluster/health?level=indices"), &health)
if err != nil {
return ClusterHealth{}, err
}
for indexName, index := range health.RawIndices {
index.Name = indexName
if index.Status == "green" {
health.HealthyIndices = append(health.HealthyIndices, index)
} else {
health.UnhealthyIndices = append(health.UnhealthyIndices, index)
}
}
health.Message = captionHealth(health)
return health, nil
}
//Get all the persistent and transient cluster settings.
//
//Use case: You want to see the current settings in the cluster.
func (c *Client) GetSettings() (ClusterSettings, error) {
clusterSettings := ClusterSettings{}
body, err := handleErrWithBytes(c.buildGetRequest(clusterSettingsPath))
if err != nil {
return clusterSettings, err
}
rawPersistentSettings := gjson.GetBytes(body, "persistent").Raw
rawTransientSettings := gjson.GetBytes(body, "transient").Raw
persisentSettings, err := settingsToStructs(rawPersistentSettings)
if err != nil {
return clusterSettings, err
}
transientSetting, err := settingsToStructs(rawTransientSettings)
if err != nil {
return clusterSettings, err
}
clusterSettings.PersistentSettings = persisentSettings
clusterSettings.TransientSettings = transientSetting
return clusterSettings, nil
}
//Enables or disables allocation for the cluster.
//
//Use case: You are performing an operation the cluster where nodes may be dropping in and out. Elasticsearch will typically try to rebalance immediately but you want the cluster to hold off rebalancing until you complete your task. Calling `SetAllocation("disable")` will disable allocation so Elasticsearch won't move/relocate any shards. Once you complete your task, calling `SetAllocation("enable")` will allow Elasticsearch to relocate shards again.
func (c *Client) SetAllocation(allocation string) (string, error) {
var allocationSetting string
if allocation == "enable" {
allocationSetting = "all"
} else {
allocationSetting = "none"
}
agent := c.buildPutRequest(clusterSettingsPath).
Set("Content-Type", "application/json").
Send(fmt.Sprintf(`{"transient" : { "cluster.routing.allocation.enable" : "%s"}}`, allocationSetting))
body, err := handleErrWithBytes(agent)
if err != nil {
return "", err
}
allocationVal := gjson.GetBytes(body, "transient.cluster.routing.allocation.enable")
return allocationVal.String(), nil
}
//Set a new value for a cluster setting
//
//Use case: You've doubled the number of nodes in your cluster and you want to increase the number of shards the cluster can relocate at one time. Calling `SetSetting("cluster.routing.allocation.cluster_concurrent_rebalance", "100")` will update that value with the cluster. Once data relocation is complete you can decrease the setting by calling `SetSetting("cluster.routing.allocation.cluster_concurrent_rebalance", "20")`.
func (c *Client) SetSetting(setting string, value string) (string, string, error) {
settingsBody, err := handleErrWithBytes(c.buildGetRequest(clusterSettingsPath))
if err != nil {
return "", "", err
}
existingValues := gjson.GetManyBytes(settingsBody, fmt.Sprintf("transient.%s", setting), fmt.Sprintf("persistent.%s", setting))
agent := c.buildPutRequest(clusterSettingsPath).
Set("Content-Type", "application/json").
Send(fmt.Sprintf(`{"transient" : { "%s" : "%s"}}`, setting, value))
body, err := handleErrWithBytes(agent)
if err != nil {
return "", "", err
}
newValue := gjson.GetBytes(body, fmt.Sprintf("transient.%s", setting)).String()
var existingValue string
if existingValues[0].String() == "" {
existingValue = existingValues[1].String()
} else {
existingValue = existingValues[0].String()
}
return existingValue, newValue, nil
}
//List the snapshots of the given repository.
//
//Use case: You want to see information on snapshots in a repository.
func (c *Client) GetSnapshots(repository string) ([]Snapshot, error) {
var snapshotWrapper snapshotWrapper
err := handleErrWithStruct(c.buildGetRequest(fmt.Sprintf("_snapshot/%s/_all", repository)), &snapshotWrapper)
if err != nil {
return nil, err
}
return snapshotWrapper.Snapshots, nil
}
//Get detailed information about a particular snapshot.
//
//Use case: You had a snapshot fail and you want to see the reason why and what shards/nodes the error occurred on.
func (c *Client) GetSnapshotStatus(repository string, snapshot string) (Snapshot, error) {
var snapshotWrapper snapshotWrapper
err := handleErrWithStruct(c.buildGetRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot)), &snapshotWrapper)
if err != nil {
return Snapshot{}, err
}
return snapshotWrapper.Snapshots[0], nil
}
//Delete a snapshot
//
//Use case: You want to delete older snapshots so that they don't take up extra space.
func (c *Client) DeleteSnapshot(repository string, snapshot string) error {
var response acknowledgedResponse
err := handleErrWithStruct(c.buildDeleteRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot)).Timeout(10*time.Minute), &response)
if err != nil {
return err
}
if !response.Acknowledged {
return fmt.Errorf(`Request to delete snapshot "%s" on repository "%s" was not acknowledged. %+v`, snapshot, repository, response)
}
return nil
}
//Verify a snapshot repository
//
//Use case: Have Elasticsearch verify a repository to make sure that all nodes can access the snapshot location correctly.
func (c *Client) VerifyRepository(repository string) (bool, error) {
_, err := handleErrWithBytes(c.buildPostRequest(fmt.Sprintf("_snapshot/%s/_verify", repository)))
if err != nil {
return false, err
}
return true, nil
}
type repo struct {
Type string `json:"type"`
Settings map[string]interface{} `json:"settings"`
}
//List snapshot respositories on the cluster
//
//Use case: You want to see all of the configured backup repositories on the given cluster, what types they are and if they are verified.
func (c *Client) GetRepositories() ([]Repository, error) {
var repos map[string]repo
var repositories []Repository
err := handleErrWithStruct(c.buildGetRequest("_snapshot/_all"), &repos)
if err != nil {
return nil, err
}
for name, r := range repos {
// Sanitize AWS secrets if they exist in the settings
delete(r.Settings, "access_key")
delete(r.Settings, "secret_key")
repositories = append(repositories, Repository{
Name: name,
Type: r.Type,
Settings: r.Settings,
})
}
return repositories, nil
}
//Take a snapshot of specific indices on the cluster to the given repository
//
//Use case: You want to backup certain indices on the cluster to the given repository.
func (c *Client) SnapshotIndices(repository string, snapshot string, indices []string) error {
if repository == "" {
return fmt.Errorf("Empty string for repository is not allowed.")
}
if snapshot == "" {
return fmt.Errorf("Empty string for snapshot is not allowed.")
}
if len(indices) == 0 {
return fmt.Errorf("No indices provided to snapshot.")
}
agent := c.buildPutRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot)).
Set("Content-Type", "application/json").
Send(fmt.Sprintf(`{"indices" : "%s"}`, strings.Join(indices, ",")))
_, err := handleErrWithBytes(agent)
return err
}
//Take a snapshot of all indices on the cluster to the given repository
//
//Use case: You want to backup all of the indices on the cluster to the given repository.
func (c *Client) SnapshotAllIndices(repository string, snapshot string) error {
if repository == "" {
return fmt.Errorf("Empty string for repository is not allowed.")
}
if snapshot == "" {
return fmt.Errorf("Empty string for snapshot is not allowed.")
}
agent := c.buildPutRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot))
_, err := handleErrWithBytes(agent)
return err
}
//Restore an index or indices on the cluster
//
//Use case: You want to restore a particular index or indices onto your cluster with a new name.
func (c *Client) RestoreSnapshotIndices(repository string, snapshot string, indices []string, restoredIndexPrefix string) error {
if repository == "" {
return fmt.Errorf("Empty string for repository is not allowed.")
}
if snapshot == "" {
return fmt.Errorf("Empty string for snapshot is not allowed.")
}
agent := c.buildPostRequest(fmt.Sprintf("_snapshot/%s/%s/_restore", repository, snapshot)).
Set("Content-Type", "application/json").
Send(fmt.Sprintf(`{"indices" : "%s","rename_pattern":"(.+)","rename_replacement":"%s$1"}`, strings.Join(indices, ","), restoredIndexPrefix))
_, err := handleErrWithBytes(agent)
return err
}