Skip to content

Commit

Permalink
fix: fix diskcache fallback error handling (#50)
Browse files Browse the repository at this point in the history
* fix: fix diskcache fallback error handling
  • Loading branch information
coanor authored Oct 30, 2023
1 parent 0157a15 commit d5d2c40
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
10 changes: 6 additions & 4 deletions diskcache/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ retry:
if err = fn(databuf); err != nil {
// seek back
if !c.noFallbackOnError {
if _, err = c.rfd.Seek(-int64(dataHeaderLen+nbytes), io.SeekCurrent); err != nil {
return err
if _, serr := c.rfd.Seek(-int64(dataHeaderLen+nbytes), io.SeekCurrent); serr != nil {
return serr
}

seekBackVec.WithLabelValues(c.path).Inc()
goto __end // do not update .pos
}
}
Expand All @@ -121,8 +123,8 @@ __updatePos:
// update seek position
if !c.noPos {
c.pos.Seek += int64(dataHeaderLen + nbytes)
if err = c.pos.dumpFile(); err != nil {
return err
if derr := c.pos.dumpFile(); derr != nil {
return derr
}
}

Expand Down
23 changes: 20 additions & 3 deletions diskcache/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

func TestFallbackOnError(t *T.T) {
t.Run(`fallback-on-error`, func(t *T.T) {
ResetMetrics()

p := t.TempDir()
c, err := Open(WithPath(p))
assert.NoError(t, err)
Expand All @@ -28,15 +30,30 @@ func TestFallbackOnError(t *T.T) {

assert.NoError(t, c.rotate())

c.Get(func(_ []byte) error { //nolint: errcheck
// should get error when callback fail
require.Error(t, c.Get(func(_ []byte) error {
return fmt.Errorf("get error")
})
}))

assert.Equal(t, int64(0), c.pos.Seek)

c.Get(func(x []byte) error { // nolint:errcheck
// should no error when callback ok
assert.NoError(t, c.Get(func(x []byte) error {
assert.Equal(t, data, x)
return nil
}))

reg := prometheus.NewRegistry()
register(reg)
mfs, err := reg.Gather()
require.NoError(t, err)

assert.Equalf(t, float64(1),
metrics.GetMetricOnLabels(mfs, "diskcache_seek_back", c.path).GetCounter().GetValue(),
"got metrics\n%s", metrics.MetricFamily2Text(mfs))

t.Cleanup(func() {
ResetMetrics()
})
})

Expand Down
14 changes: 14 additions & 0 deletions diskcache/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
getVec,
putBytesVec,
wakeupVec,
seekBackVec,
getBytesVec *prometheus.CounterVec

sizeVec,
Expand Down Expand Up @@ -126,6 +127,15 @@ func setupMetrics() {
[]string{"path"},
)

seekBackVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: ns,
Name: "seek_back_total",
Help: "Seek back when Get() got any error",
},
[]string{"path"},
)

getBytesVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: ns,
Expand Down Expand Up @@ -213,6 +223,7 @@ func setupMetrics() {
getVec,
putBytesVec,
wakeupVec,
seekBackVec,
getBytesVec,

openTimeVec,
Expand All @@ -237,6 +248,7 @@ func register(reg *prometheus.Registry) {
getVec,
putBytesVec,
wakeupVec,
seekBackVec,
getBytesVec,

capVec,
Expand All @@ -258,6 +270,7 @@ func ResetMetrics() {
getVec.Reset()
putBytesVec.Reset()
wakeupVec.Reset()
seekBackVec.Reset()
getBytesVec.Reset()
capVec.Reset()
batchSizeVec.Reset()
Expand All @@ -283,6 +296,7 @@ func Metrics() []prometheus.Collector {
getVec,
putBytesVec,
wakeupVec,
seekBackVec,
getBytesVec,

sizeVec,
Expand Down
1 change: 1 addition & 0 deletions point/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
func SortByTime(pts []*Point) {
slices.SortFunc(pts, func(l, r *Point) int {
diff := l.Time().Sub(r.Time())
// nolint: gocritic
if diff == 0 {
return 0
} else if diff > 0 {
Expand Down

0 comments on commit d5d2c40

Please sign in to comment.