Skip to content

Commit

Permalink
wal: add "etcd_wal_writes_bytes_total"
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed Apr 1, 2020
1 parent b68f8ff commit e99399d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
12 changes: 8 additions & 4 deletions wal/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func (e *encoder) encode(rec *walpb.Record) error {
if padBytes != 0 {
data = append(data, make([]byte, padBytes)...)
}
_, err = e.bw.Write(data)
n, err = e.bw.Write(data)
walWriteBytes.Add(float64(n))
return err
}

Expand All @@ -108,13 +109,16 @@ func encodeFrameSize(dataBytes int) (lenField uint64, padBytes int) {

func (e *encoder) flush() error {
e.mu.Lock()
defer e.mu.Unlock()
return e.bw.Flush()
n, err := e.bw.FlushN()
e.mu.Unlock()
walWriteBytes.Add(float64(n))
return err
}

func writeUint64(w io.Writer, n uint64, buf []byte) error {
// http://golang.org/src/encoding/binary/binary.go
binary.LittleEndian.PutUint64(buf, n)
_, err := w.Write(buf)
nv, err := w.Write(buf)
walWriteBytes.Add(float64(nv))
return err
}
8 changes: 8 additions & 0 deletions wal/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ var (
// highest bucket start of 0.001 sec * 2^13 == 8.192 sec
Buckets: prometheus.ExponentialBuckets(0.001, 2, 14),
})

walWriteBytes = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "etcd",
Subsystem: "disk",
Name: "wal_write_bytes_total",
Help: "Total number of bytes written in WAL.",
})
)

func init() {
prometheus.MustRegister(walFsyncSec)
prometheus.MustRegister(walWriteBytes)
}
4 changes: 3 additions & 1 deletion wal/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"io"
"os"
"path/filepath"
"time"

"go.etcd.io/etcd/pkg/fileutil"
"go.etcd.io/etcd/wal/walpb"

"go.uber.org/zap"
)

Expand Down Expand Up @@ -105,6 +105,7 @@ func Repair(lg *zap.Logger, dirpath string) bool {
return false
}

start := time.Now()
if err = fileutil.Fsync(f.File); err != nil {
if lg != nil {
lg.Warn("failed to fsync", zap.String("path", f.Name()), zap.Error(err))
Expand All @@ -113,6 +114,7 @@ func Repair(lg *zap.Logger, dirpath string) bool {
}
return false
}
walFsyncSec.Observe(time.Since(start).Seconds())

if lg != nil {
lg.Info("repaired", zap.String("path", f.Name()), zap.Error(io.ErrUnexpectedEOF))
Expand Down
5 changes: 5 additions & 0 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
}
return nil, perr
}
start := time.Now()
if perr = fileutil.Fsync(pdir); perr != nil {
if lg != nil {
lg.Warn(
Expand All @@ -215,6 +216,8 @@ func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error) {
}
return nil, perr
}
walFsyncSec.Observe(time.Since(start).Seconds())

if perr = pdir.Close(); perr != nil {
if lg != nil {
lg.Warn(
Expand Down Expand Up @@ -667,9 +670,11 @@ func (w *WAL) cut() error {
if err = os.Rename(newTail.Name(), fpath); err != nil {
return err
}
start := time.Now()
if err = fileutil.Fsync(w.dirFile); err != nil {
return err
}
walFsyncSec.Observe(time.Since(start).Seconds())

// reopen newTail with its new path so calls to Name() match the wal filename format
newTail.Close()
Expand Down

0 comments on commit e99399d

Please sign in to comment.