/
elastic_data.go
388 lines (341 loc) · 9.69 KB
/
elastic_data.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
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"encoding/json"
"errors"
"fmt"
"github.com/oracle/coherence-cli/pkg/config"
"github.com/oracle/coherence-cli/pkg/constants"
"github.com/oracle/coherence-cli/pkg/fetcher"
"github.com/oracle/coherence-cli/pkg/utils"
"github.com/spf13/cobra"
"strings"
"sync"
"time"
)
const (
ram = "RamJournalRM"
flash = "FlashJournalRM"
noElasticData = "elastic data is not configured"
flashString = "flash"
)
var (
ElasticDataMessage = fmt.Sprintf("name must be %s or %s", ram, flash)
ErrInvalidType = errors.New(ElasticDataMessage)
nodeIDsED string
)
// getElasticDataCmd represents the get elastic-data command.
var getElasticDataCmd = &cobra.Command{
Use: "elastic-data",
Short: "display elastic data information for a cluster",
Long: `The 'get elastic-data' command displays the Flash Journal and RAM
Journal details for the cluster.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
dataFetcher fetcher.Fetcher
finalResult string
connection string
err error
wg sync.WaitGroup
errorSink ErrorSink
ramResult []byte
flashResult []byte
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
for {
errorSink = createErrorSink()
wg.Add(2)
go func() {
defer wg.Done()
var err1 error
ramResult, err1 = dataFetcher.GetElasticDataDetails("ram")
if err1 != nil {
errorSink.AppendError(err1)
}
}()
go func() {
defer wg.Done()
var err1 error
flashResult, err1 = dataFetcher.GetElasticDataDetails(flashString)
if err1 != nil {
errorSink.AppendError(err1)
}
}()
wg.Wait()
errorList := errorSink.GetErrors()
if len(errorList) > 0 {
return utils.GetErrors(errorList)
}
if strings.Contains(OutputFormat, constants.JSONPATH) || OutputFormat == constants.JSON {
finalResult, err := utils.CombineByteArraysForJSON([][]byte{flashResult, ramResult},
[]string{constants.FlashJournal, constants.RAMJournal})
if err != nil {
return err
}
if err = processJSONOutput(cmd, finalResult); err != nil {
return err
}
} else {
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
finalResult, err = getElasticDataResult(flashResult, ramResult)
if err != nil {
return err
}
if finalResult == "" {
return nil
}
cmd.Println(finalResult)
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
// describeElasticDataCmd represents the describe elastic-data command.
var describeElasticDataCmd = &cobra.Command{
Use: "elastic-data {" + flash + "|" + ram + "}",
Short: "describe a flash or ram journal",
Long: `The 'describe elastic-data' command shows information related to a specific journal type.
The allowable values are ` + ram + ` or ` + flash + `.`,
ValidArgs: []string{ram, flash},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, ElasticDataMessage)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
queryType string
result []byte
err error
header string
dataFetcher fetcher.Fetcher
edValues = config.ElasticDataValues{}
connection string
journalType = args[0]
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
if journalType == ram {
queryType = "ram"
header = "RAM JOURNAL DETAILS"
} else if journalType == flash {
queryType = flashString
header = "FLASH JOURNAL DETAILS"
} else {
return ErrInvalidType
}
result, err = dataFetcher.GetElasticDataDetails(queryType)
if err != nil {
return err
}
if len(result) == 0 {
return errors.New(noElasticData)
}
if strings.Contains(OutputFormat, constants.JSONPATH) {
jsonPathResult, err := utils.GetJSONPathResults(result, OutputFormat)
if err != nil {
return err
}
cmd.Println(jsonPathResult)
return nil
} else if OutputFormat == constants.JSON {
cmd.Println(string(result))
} else {
if len(result) == 0 {
return nil
}
err = json.Unmarshal(result, &edValues)
if err != nil {
return utils.GetError("unable to elastic data details", err)
}
cmd.Println(FormatCurrentCluster(connection))
cmd.Println(header)
headerLen := len(header)
underline := make([]byte, headerLen)
for i := 0; i < headerLen; i++ {
underline[i] = '-'
}
cmd.Println(string(underline))
cmd.Println(FormatElasticData(edValues.ElasticData, false))
}
return nil
},
}
// compactElasticDataCmd represents the compact elastic-data command.
var compactElasticDataCmd = &cobra.Command{
Use: "elastic-data {" + flash + "|" + ram + "}",
Short: "compact a flash or ram journal",
Long: `The 'compact elastic-data' command compacts (garbage collects) a specific journal type
for all or specific nodes. ` + `The allowable values are ` + ram + ` or ` + flash + `.`,
ValidArgs: []string{ram, flash},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, ElasticDataMessage)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
queryType string
err error
dataFetcher fetcher.Fetcher
connection string
journalType = args[0]
nodeIDArray []string
nodeIds []string
confirmMessage string
errorSink = createErrorSink()
wg sync.WaitGroup
result []byte
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
if journalType == ram {
queryType = "ram"
} else if journalType == flash {
queryType = flashString
} else {
return ErrInvalidType
}
result, err = dataFetcher.GetElasticDataDetails(queryType)
if err != nil {
return err
}
if len(result) == 0 {
return errors.New(noElasticData)
}
// validate the nodes
nodeIDArray, err = GetNodeIds(dataFetcher)
if err != nil {
return err
}
if nodeIDsED == all {
nodeIds = append(nodeIds, nodeIDArray...)
confirmMessage = fmt.Sprintf("all %d nodes", len(nodeIds))
} else {
if nodeIds, err = getNodeIDs(nodeIDsED, nodeIDArray); err != nil {
return err
}
confirmMessage = fmt.Sprintf("%d node(s)", len(nodeIds))
}
cmd.Println(FormatCurrentCluster(connection))
// confirm the operation
if !confirmOperation(cmd, fmt.Sprintf("Are you sure you want to compact %s for %s? (y/n) ",
queryType, confirmMessage)) {
return nil
}
wg.Add(len(nodeIds))
for _, value := range nodeIds {
go func(nodeId string) {
var err1 error
defer wg.Done()
_, err1 = dataFetcher.CompactElasticData(queryType, nodeId)
if err1 != nil {
errorSink.AppendError(err1)
}
}(value)
}
wg.Wait()
errorList := errorSink.GetErrors()
if len(errorList) > 0 {
return utils.GetErrors(errorList)
}
cmd.Println(OperationCompleted)
return nil
},
}
// getElasticDataResult returns elastic data results.
func getElasticDataResult(flashResult, ramResult []byte) (string, error) {
var (
flashValues = config.ElasticDataValues{}
ramValues = config.ElasticDataValues{}
err error
)
if len(flashResult) > 0 {
err = json.Unmarshal(flashResult, &flashValues)
if err != nil {
return "", utils.GetError("unable to flash details", err)
}
}
if len(ramResult) > 0 {
err = json.Unmarshal(ramResult, &ramValues)
if err != nil {
return "", utils.GetError("unable to ram details", err)
}
}
// get combined results
combinedFlash := combineElasticData(flashValues)
combinedRAM := combineElasticData(ramValues)
// only display if details exist
var (
lenRAM = len(combinedRAM)
lenFlash = len(combinedFlash)
)
if lenRAM == 1 || lenFlash == 1 {
finalEd := make([]config.ElasticData, 0)
if lenFlash == 1 {
finalEd = append(finalEd, combinedFlash[0])
}
if lenRAM == 1 {
finalEd = append(finalEd, combinedRAM[0])
}
return FormatElasticData(finalEd, true), nil
}
return "", nil
}
// combineElasticData combines all the elastic data details together to give a summary.
func combineElasticData(elasticData config.ElasticDataValues) []config.ElasticData {
var finalData = make([]config.ElasticData, 0)
for _, value := range elasticData.ElasticData {
if len(finalData) == 0 {
// no entries so add it anyway
finalData = append(finalData, value)
} else {
var foundIndex = -1
for i, v := range finalData {
if v.Type == value.Type {
foundIndex = i
break
}
}
if foundIndex >= 0 {
// update the existing elastic Data
finalData[foundIndex].FileCount += value.FileCount
finalData[foundIndex].MaxJournalFilesNumber += value.MaxJournalFilesNumber
finalData[foundIndex].TotalDataSize += value.TotalDataSize
finalData[foundIndex].CompactionCount += value.CompactionCount
finalData[foundIndex].ExhaustiveCompactionCount += value.ExhaustiveCompactionCount
if value.HighestLoadFactor > finalData[foundIndex].HighestLoadFactor {
finalData[foundIndex].HighestLoadFactor = value.HighestLoadFactor
}
} else {
finalData = append(finalData, value)
}
}
}
return finalData
}
func init() {
compactElasticDataCmd.Flags().BoolVarP(&automaticallyConfirm, "yes", "y", false, confirmOptionMessage)
compactElasticDataCmd.Flags().StringVarP(&nodeIDsED, "node", "n", all, commaSeparatedIDMessage)
}