From 0d6925ced15dde2924b4d8735ce4628b56b2f75e Mon Sep 17 00:00:00 2001 From: Gus Eggert Date: Fri, 22 Oct 2021 17:59:57 -0400 Subject: [PATCH] feat: add contexts on datastore methods --- go.mod | 2 +- go.sum | 44 ++++++++++++++++++++++++++++++++++----- measure.go | 61 +++++++++++++++++++++++++++--------------------------- 3 files changed, 71 insertions(+), 36 deletions(-) diff --git a/go.mod b/go.mod index ce755fa..dbf194f 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/ipfs/go-ds-measure require ( - github.com/ipfs/go-datastore v0.4.1 + github.com/ipfs/go-datastore v0.5.0 github.com/ipfs/go-metrics-interface v0.0.1 ) diff --git a/go.sum b/go.sum index db3d0b4..13611f1 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,51 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/ipfs/go-datastore v0.4.1 h1:W4ZfzyhNi3xmuU5dQhjfuRn/wFuqEE1KnOmmQiOevEY= -github.com/ipfs/go-datastore v0.4.1/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA= +github.com/ipfs/go-datastore v0.5.0 h1:rQicVCEacWyk4JZ6G5bD9TKR7lZEG1MWcG7UdWYrFAU= +github.com/ipfs/go-datastore v0.5.0/go.mod h1:9zhEApYMTl17C8YDp7JmU7sQZi2/wqiYh73hakZ90Bk= +github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk= +github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fGD6n0jO4kdg= github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j/b/tL7HTWtJ4VPgWY= -github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8 h1:bspPhN+oKYFk5fcGNuQzp6IGzYQSenLEgH3s6jkXrWw= -github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA= +github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o= +github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= +go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/measure.go b/measure.go index 98cd598..e3fef8d 100644 --- a/measure.go +++ b/measure.go @@ -3,6 +3,7 @@ package measure import ( + "context" "io" "time" @@ -171,31 +172,31 @@ func recordLatency(h metrics.Histogram, start time.Time) { h.Observe(elapsed.Seconds()) } -func (m *measure) Put(key datastore.Key, value []byte) error { +func (m *measure) Put(ctx context.Context, key datastore.Key, value []byte) error { defer recordLatency(m.putLatency, time.Now()) m.putNum.Inc() m.putSize.Observe(float64(len(value))) - err := m.backend.Put(key, value) + err := m.backend.Put(ctx, key, value) if err != nil { m.putErr.Inc() } return err } -func (m *measure) Sync(prefix datastore.Key) error { +func (m *measure) Sync(ctx context.Context, prefix datastore.Key) error { defer recordLatency(m.syncLatency, time.Now()) m.syncNum.Inc() - err := m.backend.Sync(prefix) + err := m.backend.Sync(ctx, prefix) if err != nil { m.syncErr.Inc() } return err } -func (m *measure) Get(key datastore.Key) (value []byte, err error) { +func (m *measure) Get(ctx context.Context, key datastore.Key) (value []byte, err error) { defer recordLatency(m.getLatency, time.Now()) m.getNum.Inc() - value, err = m.backend.Get(key) + value, err = m.backend.Get(ctx, key) switch err { case nil: m.getSize.Observe(float64(len(value))) @@ -207,20 +208,20 @@ func (m *measure) Get(key datastore.Key) (value []byte, err error) { return value, err } -func (m *measure) Has(key datastore.Key) (exists bool, err error) { +func (m *measure) Has(ctx context.Context, key datastore.Key) (exists bool, err error) { defer recordLatency(m.hasLatency, time.Now()) m.hasNum.Inc() - exists, err = m.backend.Has(key) + exists, err = m.backend.Has(ctx, key) if err != nil { m.hasErr.Inc() } return exists, err } -func (m *measure) GetSize(key datastore.Key) (size int, err error) { +func (m *measure) GetSize(ctx context.Context, key datastore.Key) (size int, err error) { defer recordLatency(m.getsizeLatency, time.Now()) m.getsizeNum.Inc() - size, err = m.backend.GetSize(key) + size, err = m.backend.GetSize(ctx, key) switch err { case nil, datastore.ErrNotFound: // Not really an error. @@ -230,31 +231,31 @@ func (m *measure) GetSize(key datastore.Key) (size int, err error) { return size, err } -func (m *measure) Delete(key datastore.Key) error { +func (m *measure) Delete(ctx context.Context, key datastore.Key) error { defer recordLatency(m.deleteLatency, time.Now()) m.deleteNum.Inc() - err := m.backend.Delete(key) + err := m.backend.Delete(ctx, key) if err != nil { m.deleteErr.Inc() } return err } -func (m *measure) Query(q query.Query) (query.Results, error) { +func (m *measure) Query(ctx context.Context, q query.Query) (query.Results, error) { defer recordLatency(m.queryLatency, time.Now()) m.queryNum.Inc() - res, err := m.backend.Query(q) + res, err := m.backend.Query(ctx, q) if err != nil { m.queryErr.Inc() } return res, err } -func (m *measure) Check() error { +func (m *measure) Check(ctx context.Context) error { defer recordLatency(m.checkLatency, time.Now()) m.checkNum.Inc() if c, ok := m.backend.(datastore.CheckedDatastore); ok { - err := c.Check() + err := c.Check(ctx) if err != nil { m.checkErr.Inc() } @@ -263,11 +264,11 @@ func (m *measure) Check() error { return nil } -func (m *measure) Scrub() error { +func (m *measure) Scrub(ctx context.Context) error { defer recordLatency(m.scrubLatency, time.Now()) m.scrubNum.Inc() if c, ok := m.backend.(datastore.ScrubbedDatastore); ok { - err := c.Scrub() + err := c.Scrub(ctx) if err != nil { m.scrubErr.Inc() } @@ -276,11 +277,11 @@ func (m *measure) Scrub() error { return nil } -func (m *measure) CollectGarbage() error { +func (m *measure) CollectGarbage(ctx context.Context) error { defer recordLatency(m.gcLatency, time.Now()) m.gcNum.Inc() if c, ok := m.backend.(datastore.GCDatastore); ok { - err := c.CollectGarbage() + err := c.CollectGarbage(ctx) if err != nil { m.gcErr.Inc() } @@ -289,10 +290,10 @@ func (m *measure) CollectGarbage() error { return nil } -func (m *measure) DiskUsage() (uint64, error) { +func (m *measure) DiskUsage(ctx context.Context) (uint64, error) { defer recordLatency(m.duLatency, time.Now()) m.duNum.Inc() - size, err := datastore.DiskUsage(m.backend) + size, err := datastore.DiskUsage(ctx, m.backend) if err != nil { m.duErr.Inc() } @@ -304,12 +305,12 @@ type measuredBatch struct { m *measure } -func (m *measure) Batch() (datastore.Batch, error) { +func (m *measure) Batch(ctx context.Context) (datastore.Batch, error) { bds, ok := m.backend.(datastore.Batching) if !ok { return nil, datastore.ErrBatchUnsupported } - batch, err := bds.Batch() + batch, err := bds.Batch(ctx) if err != nil { return nil, err } @@ -320,31 +321,31 @@ func (m *measure) Batch() (datastore.Batch, error) { }, nil } -func (mt *measuredBatch) Put(key datastore.Key, val []byte) error { +func (mt *measuredBatch) Put(ctx context.Context, key datastore.Key, val []byte) error { defer recordLatency(mt.m.batchPutLatency, time.Now()) mt.m.batchPutNum.Inc() mt.m.batchPutSize.Observe(float64(len(val))) - err := mt.b.Put(key, val) + err := mt.b.Put(ctx, key, val) if err != nil { mt.m.batchPutErr.Inc() } return err } -func (mt *measuredBatch) Delete(key datastore.Key) error { +func (mt *measuredBatch) Delete(ctx context.Context, key datastore.Key) error { defer recordLatency(mt.m.batchDeleteLatency, time.Now()) mt.m.batchDeleteNum.Inc() - err := mt.b.Delete(key) + err := mt.b.Delete(ctx, key) if err != nil { mt.m.batchDeleteErr.Inc() } return err } -func (mt *measuredBatch) Commit() error { +func (mt *measuredBatch) Commit(ctx context.Context) error { defer recordLatency(mt.m.batchCommitLatency, time.Now()) mt.m.batchCommitNum.Inc() - err := mt.b.Commit() + err := mt.b.Commit(ctx) if err != nil { mt.m.batchCommitErr.Inc() }