This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
/
node_operations.go
342 lines (309 loc) · 12.6 KB
/
node_operations.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
// Copyright 2019 Orange
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cassandracluster
import (
"errors"
"fmt"
"regexp"
"context"
"github.com/sirupsen/logrus"
"github.com/swarvanusg/go_jolokia"
funk "github.com/thoas/go-funk"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var localSystemKeyspaces = []string{"system", "system_schema"}
/*JolokiaURL returns the url used to connect to a Jolokia server based on a host and a port*/
func JolokiaURL(host string, port int) string {
return fmt.Sprintf("http://%s:%d/jolokia/", host, port)
}
// JolokiaClient is a structure that exposes a host and a jolokia client
type JolokiaClient struct {
client *go_jolokia.JolokiaClient
host string
}
func (jolokiaClient *JolokiaClient) executeReadRequest(jolokiaRequest *go_jolokia.JolokiaRequest) (*go_jolokia.JolokiaReadResponse, error) {
return (*go_jolokia.JolokiaClient)(jolokiaClient.client).ExecuteReadRequest(jolokiaRequest)
}
func (jolokiaClient *JolokiaClient) executeOperation(mBean, operation string,
arguments interface{}, pattern string) (*go_jolokia.JolokiaReadResponse, error) {
return (*go_jolokia.JolokiaClient)(jolokiaClient.client).ExecuteOperation(mBean, operation, arguments, pattern)
}
/*NewJolokiaClient returns a new Joloka client for the host name and port provided*/
func NewJolokiaClient(host string, port int, rcc *ReconcileCassandraCluster,
secretRef v1.LocalObjectReference, namespace string) (*JolokiaClient, error) {
jolokiaClient := JolokiaClient{go_jolokia.NewJolokiaClient(JolokiaURL(host, port)), host}
logrus.WithFields(logrus.Fields{"host": host, "port": port,
"secretRef": secretRef, "namespace": namespace}).Debug("Creating Jolokia connection")
if (secretRef != v1.LocalObjectReference{}) {
logrus.WithFields(logrus.Fields{"host": host, "port": port,
"secretRef": secretRef, "namespace": namespace}).Debug("Using Secret for Jolokia connection")
secret := &v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: secretRef.Name,
Namespace: namespace,
},
}
err := rcc.client.Get(context.TODO(), types.NamespacedName{Name: secretRef.Name, Namespace: namespace}, secret)
if err != nil {
logrus.WithFields(logrus.Fields{"host": host, "port": port,
"secretRef": secretRef, "namespace": namespace}).Error("Can't get Jolokia secret")
return nil, err
}
jolokiaClient.client.SetCredential(string(secret.Data["username"]), string(secret.Data["password"]))
}
return (*JolokiaClient)(&jolokiaClient), nil
}
func checkJolokiaErrors(resp *go_jolokia.JolokiaReadResponse, err error) (*go_jolokia.JolokiaReadResponse, error) {
if err != nil {
return nil, err
}
if resp.Error != "" {
return nil, errors.New(resp.Error)
}
return resp, nil
}
func (jolokiaClient *JolokiaClient) leavingNodes() ([]string, error) {
request := go_jolokia.NewJolokiaRequest(go_jolokia.READ, "org.apache.cassandra.db:type=StorageService", nil, "LeavingNodes")
result, err := checkJolokiaErrors(jolokiaClient.client.ExecuteReadRequest(request))
if err != nil {
return nil, fmt.Errorf("Cannot get list of leaving nodes: %v", err.Error())
}
v, isSlice := result.Value.([]interface{})
if isSlice {
leavingNodes := []string{}
for _, value := range v {
str, isString := value.(string)
if isString {
leavingNodes = append(leavingNodes, str)
}
}
logrus.WithFields(logrus.Fields{"leavingNodes": leavingNodes}).Debug("List of leaving nodes")
return leavingNodes, nil
}
return nil, fmt.Errorf("Value returned by Jolokia is not a slice: %v", result.Value)
}
func (jolokiaClient *JolokiaClient) hostIDMap() (map[string]string, error) {
request := go_jolokia.NewJolokiaRequest(go_jolokia.READ, "org.apache.cassandra.db:type=StorageService", nil, "HostIdMap")
result, err := checkJolokiaErrors(jolokiaClient.client.ExecuteReadRequest(request))
if err != nil {
return nil, fmt.Errorf("Cannot get host id map: %v", err.Error())
}
if m, ok := result.Value.(map[string]interface{}); ok {
hostIDMap := map[string]string{}
for k, v := range m {
str, isString := v.(string)
if isString {
hostIDMap[k] = str
}
}
logrus.WithFields(logrus.Fields{"hostIDMap": hostIDMap}).Debug("Map of hosts IPs and IDs")
return hostIDMap, nil
}
return nil, fmt.Errorf("Value returned by Jolokia is not a map: %v", result.Value)
}
func (jolokiaClient *JolokiaClient) keyspaces() ([]string, error) {
request := go_jolokia.NewJolokiaRequest(go_jolokia.READ, "org.apache.cassandra.db:type=StorageService", nil, "Keyspaces")
result, err := checkJolokiaErrors(jolokiaClient.client.ExecuteReadRequest(request))
if err != nil {
return nil, fmt.Errorf("Cannot get list of keyspaces: %v", err.Error())
}
v, isSlice := result.Value.([]interface{})
if isSlice {
keyspaces := []string{}
for _, value := range v {
str, isString := value.(string)
if isString {
keyspaces = append(keyspaces, str)
}
}
return keyspaces, nil
}
return nil, fmt.Errorf("Value returned by Jolokia is not a slice: %v", result.Value)
}
func (jolokiaClient *JolokiaClient) nonLocalKeyspaces() ([]string, error) {
keyspaces, err := jolokiaClient.keyspaces()
if err != nil {
return nil, err
}
nonLocalKeyspaces := []string{}
for _, keyspace := range keyspaces {
if !funk.Contains(localSystemKeyspaces, keyspace) {
nonLocalKeyspaces = append(nonLocalKeyspaces, keyspace)
}
}
return nonLocalKeyspaces, nil
}
/*NodeCleanup triggers a cleanup of all keyspaces on the pod using a jolokia client and return the index of the last keyspace accessed and any error*/
func (jolokiaClient *JolokiaClient) NodeCleanup() error {
keyspaces, err := jolokiaClient.nonLocalKeyspaces()
if err != nil {
return err
}
return jolokiaClient.NodeCleanupKeyspaces(keyspaces)
}
/*NodeCleanupKeyspaces triggers a cleanup of each keyspaces on the pod using a jolokia client and returns the index of the last keyspace accessed and any error*/
func (jolokiaClient *JolokiaClient) NodeCleanupKeyspaces(keyspaces []string) error {
for _, keyspace := range keyspaces {
logrus.Infof("[%s]: Cleanup of keyspace %s", jolokiaClient.host, keyspace)
_, err := checkJolokiaErrors(jolokiaClient.executeOperation("org.apache.cassandra.db:type=StorageService",
"forceKeyspaceCleanup(java.lang.String,[Ljava.lang.String;)",
[]interface{}{keyspace, []string{}}, ""))
if err != nil {
logrus.Errorf("Cleanup of keyspace %s failed: %v", keyspace, err.Error())
return err
}
}
return nil
}
/*NodeUpgradeSSTables triggers an upgradeSSTables of each keyspaces through a jolokia client and returns any error*/
func (jolokiaClient *JolokiaClient) NodeUpgradeSSTables(threads int) error {
keyspaces, err := jolokiaClient.keyspaces()
if err != nil {
return err
}
return jolokiaClient.NodeUpgradeSSTablesKeyspaces(keyspaces, threads)
}
/*NodeUpgradeSSTablesKeyspaces triggers an upgradeSSTables for a list of keyspaces through a jolokia connection and returns any error*/
func (jolokiaClient *JolokiaClient) NodeUpgradeSSTablesKeyspaces(keyspaces []string,
threads int) error {
for _, keyspace := range keyspaces {
logrus.Infof("[%s]: Upgrade SSTables of keyspace %s", jolokiaClient.host, keyspace)
_, err := checkJolokiaErrors(jolokiaClient.executeOperation("org.apache.cassandra.db:type=StorageService",
"upgradeSSTables(java.lang.String,boolean,int,[Ljava.lang.String;)",
[]interface{}{keyspace, true, threads, []string{}}, ""))
if err != nil {
logrus.Errorf("Upgrade SSTables of keyspace %s failed: %v", keyspace, err.Error())
return err
}
}
return nil
}
/*NodeRebuild triggers a rebuild of all keyspaces on the pod using a jolokia client and returns any error*/
func (jolokiaClient *JolokiaClient) NodeRebuild(dc string) error {
_, err := checkJolokiaErrors(jolokiaClient.executeOperation("org.apache.cassandra.db:type=StorageService",
"rebuild(java.lang.String)",
[]interface{}{dc}, ""))
if err != nil {
return fmt.Errorf("Cannot rebuild from %s: %v", dc, err.Error())
}
return nil
}
/*NodeDecommission decommissions a node using a jolokia client and returns any error*/
func (jolokiaClient *JolokiaClient) NodeDecommission() error {
_, err := checkJolokiaErrors(jolokiaClient.executeOperation("org.apache.cassandra.db:type=StorageService",
"decommission", []interface{}{}, ""))
if err != nil {
return fmt.Errorf("Cannot decommission: %v", err.Error())
}
return nil
}
/*NodeRemove remove node hostid on the pod using a jolokia client and returns any error*/
func (jolokiaClient *JolokiaClient) NodeRemove(hostid string) error {
_, err := checkJolokiaErrors(jolokiaClient.executeOperation("org.apache.cassandra.db:type=StorageService",
"removeNode",
[]interface{}{hostid}, ""))
if err != nil {
return fmt.Errorf("Cannot remove node %s: %v", hostid, err.Error())
}
return nil
}
/*NodeOperationMode returns OperationMode of a node using a jolokia client and returns any error*/
func (jolokiaClient *JolokiaClient) NodeOperationMode() (string, error) {
request := go_jolokia.NewJolokiaRequest(go_jolokia.READ, "org.apache.cassandra.db:type=StorageService", nil, "OperationMode")
result, err := checkJolokiaErrors(jolokiaClient.executeReadRequest(request))
if err != nil {
return "", fmt.Errorf("Cannot get OperationMode: %v", err.Error())
}
v, _ := result.Value.(string)
return v, nil
}
func (jolokiaClient *JolokiaClient) hasStreamingSessions() (bool, error) {
request := go_jolokia.NewJolokiaRequest(go_jolokia.READ, "org.apache.cassandra.net:type=StreamManager", nil, "CurrentStreams")
result, err := checkJolokiaErrors(jolokiaClient.executeReadRequest(request))
if err != nil {
return true, fmt.Errorf("Cannot get list of current streams: %v", err.Error())
}
val, _ := result.Value.([]interface{})
return len(val) > 0, nil
}
func (jolokiaClient *JolokiaClient) hasCompactions(name string) (bool, error) {
request := go_jolokia.NewJolokiaRequest(go_jolokia.READ, "org.apache.cassandra.db:type=CompactionManager", nil, "Compactions")
result, err := checkJolokiaErrors(jolokiaClient.executeReadRequest(request))
if err != nil {
logrus.Error(err.Error())
return true, fmt.Errorf("Cannot get list of current compactions: %v", err.Error())
}
compactions, _ := result.Value.([]interface{})
for _, compaction := range compactions {
c := compaction.(map[string]interface{})
if c["taskType"] == name {
return true, nil
}
}
return false, nil
}
func (jolokiaClient *JolokiaClient) hasCleanupCompactions() (bool, error) {
return jolokiaClient.hasCompactions("Cleanup")
}
func (jolokiaClient *JolokiaClient) hasUpgradeSSTablesCompactions() (bool, error) {
return jolokiaClient.hasCompactions("Upgrade sstables")
}
func (jolokiaClient *JolokiaClient) hasLeavingNodes() (bool, error) {
leavingNodes, err := jolokiaClient.leavingNodes()
if err != nil {
return false, err
}
return len(leavingNodes) > 0, nil
}
/*HasDataInDC checks partition ranges of all non local keyspaces and ensure no data is replicated to the chosen datacenter*/
func (jolokiaClient *JolokiaClient) HasDataInDC(dc string) ([]string, error) {
keyspaces, err := jolokiaClient.nonLocalKeyspaces()
keyspacesWithDataInDC := []string{}
if err != nil {
return nil, err
}
for _, keyspace := range keyspaces {
dataFound, err := jolokiaClient.hasKeyspaceDataInDC(keyspace, dc)
// Returns if there is an error
if err != nil {
return nil, err
}
if dataFound {
keyspacesWithDataInDC = append(keyspacesWithDataInDC, keyspace)
}
}
return keyspacesWithDataInDC, nil
}
func (jolokiaClient *JolokiaClient) hasKeyspaceDataInDC(keyspace, dc string) (bool, error) {
result, err := checkJolokiaErrors(jolokiaClient.executeOperation("org.apache.cassandra.db:type=StorageService",
"describeRingJMX", []interface{}{keyspace}, ""))
if err != nil {
return false, fmt.Errorf("Cannot describe ring using keyspace %s: %v", keyspace, err.Error())
}
regexDc := regexp.MustCompile(fmt.Sprintf("datacenter:%s", dc))
tokenRanges, _ := result.Value.([]interface{})
for _, tokenRange := range tokenRanges {
// Returns true as soon as we find one token range that is replicated to the chosen datacenter
if regexDc.MatchString(tokenRange.(string)) {
return true, nil
}
}
return false, nil
}