feat: add thread-safe access to CountingWriter byte total (#22620)

Use atomic operations to update and report CountingWriter.Total through a new method.

closes https://github.com/influxdata/influxdb/issues/22618
pull/22638/head
davidby-influx 2021-10-04 16:48:54 -07:00 committed by GitHub
parent 3c811bcd81
commit 022b6e866b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import (
"path/filepath"
"sort"
"strings"
"sync/atomic"
"github.com/gogo/protobuf/proto"
internal "github.com/influxdata/influxdb/cmd/influxd/backup_util/internal"
@ -209,10 +210,14 @@ type CountingWriter struct {
func (w *CountingWriter) Write(p []byte) (n int, err error) {
n, err = w.Writer.Write(p)
w.Total += int64(n)
atomic.AddInt64(&w.Total, int64(n))
return
}
func (w *CountingWriter) BytesWritten() int64 {
return atomic.LoadInt64(&w.Total)
}
// retentionAndShardFromPath will take the shard relative path and split it into the
// retention policy name and shard ID. The first part of the path should be the database name.
func DBRetentionAndShardFromPath(path string) (db, retention, shard string, err error) {