Skip to content

Commit

Permalink
Add an aggregate metric for the theoretical write capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeler committed Jan 25, 2022
1 parent 7d34b9f commit 80a55b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
12 changes: 11 additions & 1 deletion bolt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (b *BoltStore) StoreLog(log *raft.Log) error {

// StoreLogs is used to store a set of raft logs
func (b *BoltStore) StoreLogs(logs []*raft.Log) error {
defer metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, time.Now())
now := time.Now()
tx, err := b.conn.Begin(true)
if err != nil {
return err
Expand All @@ -194,6 +194,16 @@ func (b *BoltStore) StoreLogs(logs []*raft.Log) error {

metrics.AddSample([]string{"raft", "boltdb", "logsPerBatch"}, float32(len(logs)))
metrics.AddSample([]string{"raft", "boltdb", "logBatchSize"}, float32(batchSize))
// Both the deferral and the inline function are important for this metrics
// accuracy. Deferral allows us to calculate the metric after the tx.Commit
// has finished and thus account for all the processing of the operation.
// The inlined function ensures that we do not calculate the time.Since(now)
// at the time of deferral but rather when the go runtime executes the
// deferred function.
defer func() {
metrics.AddSample([]string{"raft", "boltdb", "writeCapacity"}, (float32(1_000_000_000)/float32(time.Since(now).Nanoseconds()))*float32(len(logs)))
metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, now)
}()

return tx.Commit()
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hashicorp/raft-boltdb

go 1.12
go 1.16

require (
github.com/armon/go-metrics v0.3.8 // indirect
Expand Down
13 changes: 12 additions & 1 deletion v2/bolt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ func (b *BoltStore) StoreLog(log *raft.Log) error {

// StoreLogs is used to store a set of raft logs
func (b *BoltStore) StoreLogs(logs []*raft.Log) error {
defer metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, time.Now())
now := time.Now()

tx, err := b.conn.Begin(true)
if err != nil {
return err
Expand All @@ -203,6 +204,16 @@ func (b *BoltStore) StoreLogs(logs []*raft.Log) error {

metrics.AddSample([]string{"raft", "boltdb", "logsPerBatch"}, float32(len(logs)))
metrics.AddSample([]string{"raft", "boltdb", "logBatchSize"}, float32(batchSize))
// Both the deferral and the inline function are important for this metrics
// accuracy. Deferral allows us to calculate the metric after the tx.Commit
// has finished and thus account for all the processing of the operation.
// The inlined function ensures that we do not calculate the time.Since(now)
// at the time of deferral but rather when the go runtime executes the
// deferred function.
defer func() {
metrics.AddSample([]string{"raft", "boltdb", "writeCapacity"}, (float32(1_000_000_000)/float32(time.Since(now).Nanoseconds()))*float32(len(logs)))
metrics.MeasureSince([]string{"raft", "boltdb", "storeLogs"}, now)
}()

return tx.Commit()
}
Expand Down

0 comments on commit 80a55b2

Please sign in to comment.