From 022b6e866be1098bc22152b1e679e118f20221b8 Mon Sep 17 00:00:00 2001 From: davidby-influx <72418212+davidby-influx@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:48:54 -0700 Subject: [PATCH] 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 --- cmd/influxd/backup_util/backup_util.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/influxd/backup_util/backup_util.go b/cmd/influxd/backup_util/backup_util.go index a8fc5ce1a77..936ccf02c2c 100644 --- a/cmd/influxd/backup_util/backup_util.go +++ b/cmd/influxd/backup_util/backup_util.go @@ -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) {