Skip to content

Commit

Permalink
feat(managerclient): display bandwidth in sctool progress
Browse files Browse the repository at this point in the history
Fixes #4042
  • Loading branch information
Michal-Leszczynski committed Nov 4, 2024
1 parent 19912d3 commit 4f72868
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
68 changes: 66 additions & 2 deletions v3/pkg/managerclient/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,9 @@ Duration: {{ FormatDuration .StartTime .EndTime }}
{{ end -}}
{{ with .Progress }}Progress: {{ if ne .Size 0 }}{{ FormatRestoreProgress .Size .Restored .Downloaded .Failed }}{{else}}-{{ end }}
Snapshot Tag: {{ .SnapshotTag }}
Bandwidth:
- Download: {{ avgDownload .Hosts }}
- Load&stream: {{ avgStream .Hosts }}
{{ else }}Progress: 0%
{{ end }}
{{- if .Errors -}}
Expand All @@ -1038,6 +1041,8 @@ func (rp RestoreProgress) addHeader(w io.Writer) error {
"FormatError": FormatError,
"FormatRestoreProgress": FormatRestoreProgress,
"status": rp.status,
"avgDownload": avgDownload,
"avgStream": avgStream,
}).Parse(restoreProgressTemplate))
return temp.Execute(w, rp)
}
Expand All @@ -1055,6 +1060,37 @@ func (rp RestoreProgress) status() string {
return s
}

func avgDownload(hosts []*models.RestoreHostProgress) string {
var bytes, milliseconds, shards int64
for _, hp := range hosts {
bytes += hp.DownloadedBytes
milliseconds += hp.DownloadDuration
shards += hp.ShardCnt
}
return formatBandwidth(bytes, milliseconds, shards)
}

func avgStream(hosts []*models.RestoreHostProgress) string {
var bytes, milliseconds, shards int64
for _, hp := range hosts {
bytes += hp.StreamedBytes
milliseconds += hp.StreamDuration
shards += hp.ShardCnt
}
return formatBandwidth(bytes, milliseconds, shards)
}

func formatBandwidth(bytes, milliseconds, shards int64) string {
if milliseconds <= 0 {
return "unknown"
}
bs := bytes * 1000 / milliseconds
if shards <= 0 {
return FormatSizeSuffix(bs) + "/s"
}
return FormatSizeSuffix(bs/shards) + "/s/shard"
}

func (rp RestoreProgress) hideKeyspace(keyspace string) bool {
if rp.KeyspaceFilter.Size() > 0 {
return rp.KeyspaceFilter.FirstMatch(keyspace) == -1
Expand Down Expand Up @@ -1082,10 +1118,15 @@ func (rp RestoreProgress) Render(w io.Writer) error {
}
}

if rp.Detailed && rp.Progress.Size > 0 {
if err := rp.addTableProgress(w); err != nil {
if rp.Detailed {
if err := rp.addHostProgress(w); err != nil {
return err
}
if rp.Progress.Size > 0 {
if err := rp.addTableProgress(w); err != nil {
return err
}
}
}

// Check if there is repair progress to display
Expand Down Expand Up @@ -1211,6 +1252,29 @@ func (rp RestoreProgress) addTableProgress(w io.Writer) error {
return nil
}

func (rp RestoreProgress) addHostProgress(w io.Writer) error {
_, _ = fmt.Fprintf(w, "\nHosts info\n")
t := table.New("Host", "Shards", "Download bandwidth", "Download duration", "Load&stream bandwidth", "Load&stream duration")
for i, hp := range rp.Progress.Hosts {
if i > 0 {
t.AddSeparator()
}
t.AddRow(
hp.Host,
hp.ShardCnt,
formatBandwidth(hp.DownloadedBytes, hp.DownloadDuration, hp.ShardCnt),
FormatMsDuration(hp.DownloadDuration),
formatBandwidth(hp.StreamedBytes, hp.StreamDuration, hp.ShardCnt),
FormatMsDuration(hp.StreamDuration),
)
}
t.SetColumnAlignment(termtables.AlignRight, 1, 2, 3, 4, 5)
if _, err := w.Write([]byte(t.String())); err != nil {
return err
}
return nil
}

func (rp RestoreProgress) addViewProgress(w io.Writer) {
if len(rp.Progress.Views) == 0 {
return
Expand Down
6 changes: 3 additions & 3 deletions v3/pkg/managerclient/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ func FormatDuration(t0, t1 strfmt.DateTime) string {
return d.Truncate(time.Second).String()
}

// FormatMsDuration returns string representation of duration as number of
// milliseconds.
// FormatMsDuration returns string representation of given amount of milliseconds
// as duration rounded up to a full second.
func FormatMsDuration(d int64) string {
return (time.Duration(d) * time.Millisecond).Truncate(time.Second).String()
return (time.Duration(d)*time.Millisecond + time.Second - 1).Truncate(time.Second).String()
}

func isZero(t strfmt.DateTime) bool {
Expand Down

0 comments on commit 4f72868

Please sign in to comment.