-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-heal-ui.go
444 lines (392 loc) · 11.7 KB
/
admin-heal-ui.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
// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"errors"
"fmt"
"math"
"strings"
"time"
humanize "github.com/dustin/go-humanize"
"github.com/fatih/color"
json "github.com/minio/colorjson"
"github.com/minio/madmin-go"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/console"
)
const (
lineWidth = 80
)
var (
hColOrder = []col{colRed, colYellow, colGreen}
hColTable = map[int][]int{
1: {0, -1, 1},
2: {0, 1, 2},
3: {1, 2, 3},
4: {1, 2, 4},
5: {1, 3, 5},
6: {2, 4, 6},
7: {2, 4, 7},
8: {2, 5, 8},
}
)
func getHColCode(surplusShards, parityShards int) (c col, err error) {
if parityShards < 1 || parityShards > 8 || surplusShards > parityShards {
return c, fmt.Errorf("Invalid parity shard count/surplus shard count given")
}
if surplusShards < 0 {
return colGrey, err
}
colRow := hColTable[parityShards]
for index, val := range colRow {
if val != -1 && surplusShards <= val {
return hColOrder[index], err
}
}
return c, fmt.Errorf("cannot get a heal color code")
}
type uiData struct {
Bucket, Prefix string
Client *madmin.AdminClient
ClientToken string
ForceStart bool
HealOpts *madmin.HealOpts
LastItem *hri
// Total time since heal start
HealDuration time.Duration
// Accumulated statistics of heal result records
BytesScanned int64
// Counter for objects, and another counter for all kinds of
// items
ObjectsScanned, ItemsScanned int64
// Counters for healed objects and all kinds of healed items
ObjectsHealed, ItemsHealed int64
// Map from online drives to number of objects with that many
// online drives.
ObjectsByOnlineDrives map[int]int64
// Map of health color code to number of objects with that
// health color code.
HealthCols map[col]int64
// channel to receive a prompt string to indicate activity on
// the terminal
CurChan (<-chan string)
}
func (ui *uiData) updateStats(i madmin.HealResultItem) error {
if i.Type == madmin.HealItemObject {
// Objects whose size could not be found have -1 size
// returned.
if i.ObjectSize >= 0 {
ui.BytesScanned += i.ObjectSize
}
ui.ObjectsScanned++
}
ui.ItemsScanned++
beforeUp, afterUp := i.GetOnlineCounts()
if afterUp > beforeUp {
if i.Type == madmin.HealItemObject {
ui.ObjectsHealed++
}
ui.ItemsHealed++
}
ui.ObjectsByOnlineDrives[afterUp]++
// Update health color stats:
// Fetch health color after heal:
var err error
var afterCol col
h := newHRI(&i)
switch h.Type {
case madmin.HealItemMetadata, madmin.HealItemBucket:
_, afterCol, err = h.getReplicatedFileHCCChange()
default:
_, afterCol, err = h.getObjectHCCChange()
}
if err != nil {
return err
}
ui.HealthCols[afterCol]++
return nil
}
func (ui *uiData) updateDuration(s *madmin.HealTaskStatus) {
ui.HealDuration = UTCNow().Sub(s.StartTime)
}
func (ui *uiData) getProgress() (oCount, objSize, duration string) {
oCount = humanize.Comma(ui.ObjectsScanned)
duration = ui.HealDuration.Round(time.Second).String()
bytesScanned := float64(ui.BytesScanned)
// Compute unit for object size
magnitudes := []float64{1 << 10, 1 << 20, 1 << 30, 1 << 40, 1 << 50, 1 << 60}
units := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
var i int
for i = 0; i < len(magnitudes); i++ {
if bytesScanned <= magnitudes[i] {
break
}
}
numUnits := int(bytesScanned * (1 << 10) / magnitudes[i])
objSize = fmt.Sprintf("%d %s", numUnits, units[i])
return
}
func (ui *uiData) getPercentsNBars() (p map[col]float64, b map[col]string) {
// barChar, emptyBarChar := "█", "░"
barChar, emptyBarChar := "█", " "
barLen := 12
sum := float64(ui.ItemsScanned)
cols := []col{colGrey, colRed, colYellow, colGreen}
p = make(map[col]float64, len(cols))
b = make(map[col]string, len(cols))
var filledLen int
for _, col := range cols {
v := float64(ui.HealthCols[col])
if sum == 0 {
p[col] = 0
filledLen = 0
} else {
p[col] = v * 100 / sum
// round up the filled part
filledLen = int(math.Ceil(float64(barLen) * v / sum))
}
b[col] = strings.Repeat(barChar, filledLen) +
strings.Repeat(emptyBarChar, barLen-filledLen)
}
return
}
func (ui *uiData) printItemsQuietly(s *madmin.HealTaskStatus) (err error) {
lpad := func(s col) string {
return fmt.Sprintf("%-6s", string(s))
}
rpad := func(s col) string {
return fmt.Sprintf("%6s", string(s))
}
printColStr := func(before, after col) {
console.PrintC("[" + lpad(before) + " -> " + rpad(after) + "] ")
}
var b, a col
for _, item := range s.Items {
h := newHRI(&item)
switch h.Type {
case madmin.HealItemMetadata, madmin.HealItemBucket:
b, a, err = h.getReplicatedFileHCCChange()
default:
b, a, err = h.getObjectHCCChange()
}
if err != nil {
return err
}
printColStr(b, a)
hrStr := h.getHealResultStr()
switch h.Type {
case madmin.HealItemMetadata, madmin.HealItemBucketMetadata:
console.PrintC(fmt.Sprintln("**", hrStr, "**"))
default:
console.PrintC(hrStr, "\n")
}
}
return nil
}
func (ui *uiData) printStatsQuietly(s *madmin.HealTaskStatus) {
totalObjects, totalSize, totalTime := ui.getProgress()
healedStr := fmt.Sprintf("Healed:\t%s/%s objects; %s in %s\n",
humanize.Comma(ui.ObjectsHealed), totalObjects,
totalSize, totalTime)
console.PrintC(healedStr)
}
func (ui *uiData) printItemsJSON(s *madmin.HealTaskStatus) (err error) {
type healRec struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Before struct {
Color string `json:"color"`
Offline int `json:"offline"`
Online int `json:"online"`
Missing int `json:"missing"`
Corrupted int `json:"corrupted"`
Drives []madmin.HealDriveInfo `json:"drives"`
} `json:"before"`
After struct {
Color string `json:"color"`
Offline int `json:"offline"`
Online int `json:"online"`
Missing int `json:"missing"`
Corrupted int `json:"corrupted"`
Drives []madmin.HealDriveInfo `json:"drives"`
} `json:"after"`
Size int64 `json:"size"`
}
makeHR := func(h *hri) (r healRec) {
r.Status = "success"
r.Type, r.Name = h.getHRTypeAndName()
var b, a col
var err error
switch h.Type {
case madmin.HealItemMetadata, madmin.HealItemBucket:
b, a, err = h.getReplicatedFileHCCChange()
default:
if h.Type == madmin.HealItemObject {
r.Size = h.ObjectSize
}
b, a, err = h.getObjectHCCChange()
}
if err != nil {
r.Error = err.Error()
}
r.Before.Color = strings.ToLower(string(b))
r.After.Color = strings.ToLower(string(a))
r.Before.Online, r.After.Online = h.GetOnlineCounts()
r.Before.Missing, r.After.Missing = h.GetMissingCounts()
r.Before.Corrupted, r.After.Corrupted = h.GetCorruptedCounts()
r.Before.Offline, r.After.Offline = h.GetOfflineCounts()
r.Before.Drives = h.Before.Drives
r.After.Drives = h.After.Drives
return r
}
for _, item := range s.Items {
h := newHRI(&item)
jsonBytes, err := json.MarshalIndent(makeHR(h), "", " ")
fatalIf(probe.NewError(err), "Unable to marshal to JSON.")
console.Println(string(jsonBytes))
}
return nil
}
func (ui *uiData) printStatsJSON(s *madmin.HealTaskStatus) {
var summary struct {
Status string `json:"status"`
Error string `json:"error,omitempty"`
Type string `json:"type"`
ObjectsScanned int64 `json:"objects_scanned"`
ObjectsHealed int64 `json:"objects_healed"`
ItemsScanned int64 `json:"items_scanned"`
ItemsHealed int64 `json:"items_healed"`
Size int64 `json:"size"`
ElapsedTime int64 `json:"duration"`
}
summary.Status = "success"
summary.Type = "summary"
summary.ObjectsScanned = ui.ObjectsScanned
summary.ObjectsHealed = ui.ObjectsHealed
summary.ItemsScanned = ui.ItemsScanned
summary.ItemsHealed = ui.ItemsHealed
summary.Size = ui.BytesScanned
summary.ElapsedTime = int64(ui.HealDuration.Round(time.Second).Seconds())
jBytes, err := json.MarshalIndent(summary, "", " ")
fatalIf(probe.NewError(err), "Unable to marshal to JSON.")
console.Println(string(jBytes))
}
func (ui *uiData) updateUI(s *madmin.HealTaskStatus) (err error) {
itemCount := len(s.Items)
h := ui.LastItem
if itemCount > 0 {
item := s.Items[itemCount-1]
h = newHRI(&item)
ui.LastItem = h
}
scannedStr := "** waiting for status from server **"
if h != nil {
scannedStr = lineTrunc(h.makeHealEntityString(), lineWidth-len("Scanned: "))
}
totalObjects, totalSize, totalTime := ui.getProgress()
healedStr := fmt.Sprintf("%s/%s objects; %s in %s",
humanize.Comma(ui.ObjectsHealed), totalObjects,
totalSize, totalTime)
console.Print(console.Colorize("HealUpdateUI", fmt.Sprintf(" %s", <-ui.CurChan)))
console.PrintC(fmt.Sprintf(" %s\n", scannedStr))
console.PrintC(fmt.Sprintf(" %s\n", healedStr))
dspOrder := []col{colGreen, colYellow, colRed, colGrey}
printColors := []*color.Color{}
for _, c := range dspOrder {
printColors = append(printColors, getPrintCol(c))
}
t := console.NewTable(printColors, []bool{false, true, true}, 4)
percentMap, barMap := ui.getPercentsNBars()
cellText := make([][]string, len(dspOrder))
for i := range cellText {
cellText[i] = []string{
string(dspOrder[i]),
fmt.Sprint(humanize.Comma(ui.HealthCols[dspOrder[i]])),
fmt.Sprintf("%5.1f%% %s", percentMap[dspOrder[i]], barMap[dspOrder[i]]),
}
}
t.DisplayTable(cellText)
return nil
}
func (ui *uiData) UpdateDisplay(s *madmin.HealTaskStatus) (err error) {
// Update state
ui.updateDuration(s)
for _, i := range s.Items {
ui.updateStats(i)
}
// Update display
switch {
case globalJSON:
err = ui.printItemsJSON(s)
case globalQuiet:
err = ui.printItemsQuietly(s)
default:
err = ui.updateUI(s)
}
return
}
func (ui *uiData) healResumeMsg(aliasedURL string) string {
var flags string
if ui.HealOpts.Recursive {
flags += "--recursive "
}
if ui.HealOpts.DryRun {
flags += "--dry-run "
}
return fmt.Sprintf("Healing is backgrounded, to resume watching use `mc admin heal %s %s`", flags, aliasedURL)
}
func (ui *uiData) DisplayAndFollowHealStatus(aliasedURL string) (res madmin.HealTaskStatus, err error) {
quitMsg := ui.healResumeMsg(aliasedURL)
firstIter := true
for {
select {
case <-globalContext.Done():
return res, errors.New(quitMsg)
default:
_, res, err = ui.Client.Heal(globalContext, ui.Bucket, ui.Prefix, *ui.HealOpts,
ui.ClientToken, ui.ForceStart, false)
if err != nil {
return res, err
}
if firstIter {
firstIter = false
} else {
if !globalQuiet && !globalJSON {
console.RewindLines(8)
}
}
err = ui.UpdateDisplay(&res)
if err != nil {
return res, err
}
if res.Summary == "finished" {
if globalJSON {
ui.printStatsJSON(&res)
} else if globalQuiet {
ui.printStatsQuietly(&res)
}
return res, nil
}
if res.Summary == "stopped" {
return res, fmt.Errorf("Heal had an error - %s", res.FailureDetail)
}
time.Sleep(time.Second)
}
}
}