Skip to content

Commit

Permalink
feat: add thread-safe access to CountingWriter byte total (#22620)
Browse files Browse the repository at this point in the history
Use atomic operations to update and report CountingWriter.Total through a new method.

closes #22618
  • Loading branch information
davidby-influx authored Oct 4, 2021
1 parent 3c811bc commit 022b6e8
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/influxd/backup_util/backup_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 022b6e8

Please sign in to comment.