-
Notifications
You must be signed in to change notification settings - Fork 2
/
elasticsearch_command.go
167 lines (147 loc) · 5.14 KB
/
elasticsearch_command.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
package esasg
import (
"context"
"sort"
"strings"
"sync"
elastic "github.com/olivere/elastic/v7" // Elasticsearch client
"github.com/tidwall/gjson" // Just-In-Time JSON parsing
"go.uber.org/zap" // Logging
"github.com/mintel/elasticsearch-asg/pkg/es" // Elasticsearch client extensions
)
const shardAllocExcludeSetting = "cluster.routing.allocation.exclude"
// ElasticsearchCommandService implements methods that write to Elasticsearch endpoints.
type ElasticsearchCommandService struct {
client *elastic.Client
logger *zap.Logger
settingsMu sync.Mutex // Elasticsearch doesn't provide an atomic way to modify settings
}
// NewElasticsearchCommandService returns a new ElasticsearchCommandService.
func NewElasticsearchCommandService(client *elastic.Client) *ElasticsearchCommandService {
return &ElasticsearchCommandService{
client: client,
logger: zap.L().Named("ElasticsearchCommandService"),
}
}
// Drain excludes a node from shard allocation, which will cause Elasticsearch
// to remove shards from the node until empty.
//
// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/allocation-filtering.html
func (s *ElasticsearchCommandService) Drain(ctx context.Context, nodeName string) error {
s.settingsMu.Lock()
defer s.settingsMu.Unlock()
resp, err := es.NewClusterGetSettingsService(s.client).Do(ctx)
if err != nil {
return err
}
settings := newShardAllocationExcludeSettings(resp.Transient)
sort.Strings(settings.Name)
i := sort.SearchStrings(settings.Name, nodeName) // Index in sorted slice where nodeName should be.
if i < len(settings.Name) && settings.Name[i] == nodeName { // Node is already excluded from allocation.
return nil
}
// Insert nodeName into slice (https://github.com/golang/go/wiki/SliceTricks#insert)
settings.Name = append(settings.Name, "")
copy(settings.Name[i+1:], settings.Name[i:])
settings.Name[i] = nodeName
// Ignore all node exclusion attributes other than node name.
settings.IP = nil
settings.Host = nil
settings.Attr = nil
// Update cluster settings with new shard allocation exclusions.
settingsMap := settings.Map()
body := map[string]map[string]*string{"transient": settingsMap}
_, err = es.NewClusterPutSettingsService(s.client).BodyJSON(body).Do(ctx)
return err
}
// Undrain reverses Drain.
//
// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/allocation-filtering.html
func (s *ElasticsearchCommandService) Undrain(ctx context.Context, nodeName string) error {
s.settingsMu.Lock()
defer s.settingsMu.Unlock()
resp, err := es.NewClusterGetSettingsService(s.client).Do(ctx)
if err != nil {
return err
}
settings := newShardAllocationExcludeSettings(resp.Transient)
sort.Strings(settings.Name)
i := sort.SearchStrings(settings.Name, nodeName) // Index in sorted slice where nodeName should be.
if i == len(settings.Name) || settings.Name[i] != nodeName { // Node is already not in the shard allocation exclusion list.
return nil
}
// Remove nodeName from slice (https://github.com/golang/go/wiki/SliceTricks#delete)
settings.Name = settings.Name[:i+copy(settings.Name[i:], settings.Name[i+1:])]
// Ignore all node exclusion attributes other than node name.
settings.IP = nil
settings.Host = nil
settings.Attr = nil
// Update cluster settings with new shard allocation exclusions.
settingsMap := settings.Map()
body := map[string]map[string]*string{"transient": settingsMap}
_, err = es.NewClusterPutSettingsService(s.client).BodyJSON(body).Do(ctx)
return err
}
// shardAllocationExcludeSettings represents the transient shard allocation exclusions
// of an Elasticsearch cluster.
type shardAllocationExcludeSettings struct {
Name, Host, IP []string
Attr map[string][]string
}
// newShardAllocationExcludeSettings creates a new shardAllocationExcludeSettings.
func newShardAllocationExcludeSettings(settings *gjson.Result) *shardAllocationExcludeSettings {
s := &shardAllocationExcludeSettings{
Attr: make(map[string][]string),
}
settings.Get(shardAllocExcludeSetting).ForEach(func(key, value gjson.Result) bool {
k := key.String()
v := strings.Split(value.String(), ",")
switch k {
case "_name":
s.Name = v
case "_ip":
s.IP = v
case "_host":
s.Host = v
default:
s.Attr[k] = v
}
return true
})
return s
}
func (s *shardAllocationExcludeSettings) Map() map[string]*string {
m := make(map[string]*string)
if s.Name != nil {
if len(s.Name) == 0 {
m[shardAllocExcludeSetting+"._name"] = nil
} else {
m[shardAllocExcludeSetting+"._name"] = strPtr(strings.Join(s.Name, ","))
}
}
if s.Host != nil {
if len(s.Host) == 0 {
m[shardAllocExcludeSetting+"._host"] = nil
} else {
m[shardAllocExcludeSetting+"._host"] = strPtr(strings.Join(s.Host, ","))
}
}
if s.IP != nil {
if len(s.IP) == 0 {
m[shardAllocExcludeSetting+"._ip"] = nil
} else {
m[shardAllocExcludeSetting+"._ip"] = strPtr(strings.Join(s.IP, ","))
}
}
for k, v := range s.Attr {
if len(v) == 0 {
m[shardAllocExcludeSetting+"."+k] = nil
} else {
m[shardAllocExcludeSetting+"."+k] = strPtr(strings.Join(v, ","))
}
}
return m
}
func strPtr(s string) *string {
return &s
}