wal: record metrics

This commit is contained in:
Xiang Li 2015-01-29 13:07:23 -08:00 committed by Yicheng Qin
parent 2e078582f9
commit 9e63b1fb63

View File

@ -23,7 +23,9 @@ import (
"os"
"path"
"reflect"
"time"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codahale/metrics"
"github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/pkg/pbutil"
"github.com/coreos/etcd/raft"
@ -49,8 +51,15 @@ var (
ErrSnapshotMismatch = errors.New("wal: snapshot mismatch")
ErrSnapshotNotFound = errors.New("wal: snapshot not found")
crcTable = crc32.MakeTable(crc32.Castagnoli)
// metrics
syncHdr *metrics.Histogram
)
func init() {
syncHdr = metrics.NewHistogram("wal.syncInMicrosecond", 0, 10*int64(time.Second/time.Microsecond), 3)
}
// WAL is a logical repersentation of the stable storage.
// WAL is either in read mode or append mode but not both.
// A newly created WAL is in append mode, and ready for appending records.
@ -269,6 +278,7 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.
// create encoder (chain crc with the decoder), enable appending
w.encoder = newEncoder(w.f, w.decoder.lastCRC())
w.decoder = nil
metrics.Gauge("wal.lastIndexSaved").Set(int64(w.enti))
return metadata, state, ents, err
}
@ -317,7 +327,11 @@ func (w *WAL) sync() error {
return err
}
}
return w.f.Sync()
start := time.Now()
err := w.f.Sync()
syncHdr.RecordValue(time.Since(start).Nanoseconds() / int64(time.Microsecond))
metrics.Counter("wal.syncs").Add()
return err
}
// ReleaseLockTo releases the locks w is holding, which
@ -362,12 +376,14 @@ func (w *WAL) Close() error {
}
func (w *WAL) saveEntry(e *raftpb.Entry) error {
metrics.Counter("wal.saveEntryOps").Add()
b := pbutil.MustMarshal(e)
rec := &walpb.Record{Type: entryType, Data: b}
if err := w.encoder.encode(rec); err != nil {
return err
}
w.enti = e.Index
metrics.Gauge("wal.lastIndexSaved").Set(int64(w.enti))
return nil
}
@ -375,6 +391,7 @@ func (w *WAL) saveState(s *raftpb.HardState) error {
if raft.IsEmptyHardState(*s) {
return nil
}
metrics.Counter("wal.saveStateOps").Add()
w.state = *s
b := pbutil.MustMarshal(s)
rec := &walpb.Record{Type: stateType, Data: b}
@ -382,6 +399,11 @@ func (w *WAL) saveState(s *raftpb.HardState) error {
}
func (w *WAL) Save(st raftpb.HardState, ents []raftpb.Entry) error {
// short cut, do not call sync
if raft.IsEmptyHardState(st) && len(ents) == 0 {
return nil
}
// TODO(xiangli): no more reference operator
if err := w.saveState(&st); err != nil {
return err
@ -400,10 +422,12 @@ func (w *WAL) SaveSnapshot(e walpb.Snapshot) error {
if err := w.encoder.encode(rec); err != nil {
return err
}
metrics.Counter("wal.saveSnapOps").Add()
// update enti only when snapshot is ahead of last index
if w.enti < e.Index {
w.enti = e.Index
}
metrics.Gauge("wal.lastIndexSaved").Set(int64(w.enti))
return w.sync()
}