Skip to content

Commit

Permalink
Annotate some errors with context canceled/deadline exceeded info
Browse files Browse the repository at this point in the history
  • Loading branch information
mostynb committed Apr 11, 2024
1 parent 930227d commit d8e258b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions cache/disk/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_library(
"//cache/disk/casblob:go_default_library",
"//cache/disk/zstdimpl:go_default_library",
"//genproto/build/bazel/remote/execution/v2:go_default_library",
"//utils/annotate:go_default_library",
"//utils/tempfile:go_default_library",
"//utils/validate:go_default_library",
"@com_github_djherbis_atime//:go_default_library",
Expand Down
9 changes: 5 additions & 4 deletions cache/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/buchgr/bazel-remote/v2/cache"
"github.com/buchgr/bazel-remote/v2/cache/disk/casblob"
"github.com/buchgr/bazel-remote/v2/cache/disk/zstdimpl"
"github.com/buchgr/bazel-remote/v2/utils/annotate"
"github.com/buchgr/bazel-remote/v2/utils/tempfile"
"github.com/buchgr/bazel-remote/v2/utils/validate"

Expand Down Expand Up @@ -315,7 +316,7 @@ func (c *diskCache) Put(ctx context.Context, kind cache.EntryKind, hash string,
removeTempfile = true

var sizeOnDisk int64
sizeOnDisk, err = c.writeAndCloseFile(r, kind, hash, size, tf)
sizeOnDisk, err = c.writeAndCloseFile(ctx, r, kind, hash, size, tf)
if err != nil {
return internalErr(err)
}
Expand All @@ -340,7 +341,7 @@ func (c *diskCache) Put(ctx context.Context, kind cache.EntryKind, hash string,
return nil
}

func (c *diskCache) writeAndCloseFile(r io.Reader, kind cache.EntryKind, hash string, size int64, f *os.File) (int64, error) {
func (c *diskCache) writeAndCloseFile(ctx context.Context, r io.Reader, kind cache.EntryKind, hash string, size int64, f *os.File) (int64, error) {
closeFile := true
defer func() {
if closeFile {
Expand All @@ -354,14 +355,14 @@ func (c *diskCache) writeAndCloseFile(r io.Reader, kind cache.EntryKind, hash st
if kind == cache.CAS && c.storageMode != casblob.Identity {
sizeOnDisk, err = casblob.WriteAndClose(c.zstd, r, f, c.storageMode, hash, size)
if err != nil {
return -1, fmt.Errorf("Failed to write compressed CAS blob to disk: %w", err)
return -1, annotate.Err(ctx, "Failed to write compressed CAS blob to disk", err)
}
closeFile = false
return sizeOnDisk, nil
}

if sizeOnDisk, err = io.Copy(f, r); err != nil {
return -1, fmt.Errorf("Failed to copy data to disk: %w", err)
return -1, annotate.Err(ctx, "Failed to copy data to disk", err)
}

if isSizeMismatch(sizeOnDisk, size) {
Expand Down
8 changes: 8 additions & 0 deletions utils/annotate/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["annotate.go"],
importpath = "github.com/buchgr/bazel-remote/v2/utils/annotate",
visibility = ["//visibility:public"],
)
18 changes: 18 additions & 0 deletions utils/annotate/annotate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package annotate

import (
"context"
"fmt"
)

func Err(ctx context.Context, prefix string, err error) error {
ctxErr := ctx.Err()

if ctxErr == nil {
// The context is still valid/not done, we don't have extra info to add.
return fmt.Errorf("%s: %w", prefix, err)
}

// Add info that the context was cancelled or deadline exceeded.
return fmt.Errorf("%s: %w (%w)", prefix, err, ctxErr)
}

0 comments on commit d8e258b

Please sign in to comment.