Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

accounting: clear finished transfer in stats-reset

Maciej Zimnochf5443ac939ec
diff --git a/docs/content/rc.md b/docs/content/rc.md
index 7185efa3a..22f46ec8f 100644
--- a/docs/content/rc.md
+++ b/docs/content/rc.md
@@ -521,8 +521,8 @@ The value for "eta" is null if an eta cannot be determined.
### core/stats-reset: Reset stats. {#core/stats-reset}
-This clears counters and errors for all stats or specific stats group if group
-is provided.
+This clears counters, errors and finished transfers for all stats or specific
+stats group if group is provided.
Parameters
diff --git a/fs/accounting/stats.go b/fs/accounting/stats.go
index 438c4a554..f13d340a6 100644
--- a/fs/accounting/stats.go
+++ b/fs/accounting/stats.go
@@ -627,6 +627,20 @@ func (s *StatsInfo) RemoveTransfer(transfer *Transfer) {
s.mu.Unlock()
}
+// PruneAllTransfers removes all finished transfers.
+func (s *StatsInfo) PruneAllTransfers() {
+ s.mu.Lock()
+ for i := 0; i < len(s.startedTransfers); i++ {
+ tr := s.startedTransfers[i]
+ if tr.IsDone() {
+ s.removeTransfer(tr, i)
+ // i'th element is removed, recover iterator to not skip next element.
+ i--
+ }
+ }
+ s.mu.Unlock()
+}
+
// PruneTransfers makes sure there aren't too many old transfers by removing
// single finished transfer.
func (s *StatsInfo) PruneTransfers() {
diff --git a/fs/accounting/stats_groups.go b/fs/accounting/stats_groups.go
index 69175d160..d905bc3ab 100644
--- a/fs/accounting/stats_groups.go
+++ b/fs/accounting/stats_groups.go
@@ -59,8 +59,10 @@ func resetStats(ctx context.Context, in rc.Params) (rc.Params, error) {
}
if group != "" {
- groups.get(group).ResetCounters()
- groups.get(group).ResetErrors()
+ stats := groups.get(group)
+ stats.ResetCounters()
+ stats.ResetErrors()
+ stats.PruneAllTransfers()
} else {
groups.clear()
}
@@ -190,8 +192,8 @@ Returns the following values:
Fn: resetStats,
Title: "Reset stats.",
Help: `
-This clears counters and errors for all stats or specific stats group if group
-is provided.
+This clears counters, errors and finished transfers for all stats or specific
+stats group if group is provided.
Parameters
@@ -333,6 +335,7 @@ func (sg *statsGroups) clear() {
for _, stats := range sg.m {
stats.ResetErrors()
stats.ResetCounters()
+ stats.PruneAllTransfers()
}
sg.m = make(map[string]*StatsInfo)
diff --git a/fs/accounting/stats_test.go b/fs/accounting/stats_test.go
index 2e462cb74..a6acf3faf 100644
--- a/fs/accounting/stats_test.go
+++ b/fs/accounting/stats_test.go
@@ -431,3 +431,25 @@ func TestPruneTransfers(t *testing.T) {
})
}
}
+
+func TestPruneAllTransfers(t *testing.T) {
+ const transfers = 10
+
+ s := NewStats()
+ for i := int64(1); i <= int64(transfers); i++ {
+ s.AddTransfer(&Transfer{
+ startedAt: time.Unix(i, 0),
+ completedAt: time.Unix(i+1, 0),
+ })
+ }
+
+ s.mu.Lock()
+ assert.Equal(t, transfers, len(s.startedTransfers))
+ s.mu.Unlock()
+
+ s.PruneAllTransfers()
+
+ s.mu.Lock()
+ assert.Empty(t, s.startedTransfers)
+ s.mu.Unlock()
+}