Browse Source

tsdb/fileutil: use Go standard errors

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
pull/13142/head
Matthieu MOREL 1 year ago
parent
commit
e3041740e4
  1. 9
      tsdb/fileutil/mmap.go
  2. 9
      tsdb/fileutil/preallocate_linux.go

9
tsdb/fileutil/mmap.go

@ -14,9 +14,8 @@
package fileutil package fileutil
import ( import (
"fmt"
"os" "os"
"github.com/pkg/errors"
) )
type MmapFile struct { type MmapFile struct {
@ -31,7 +30,7 @@ func OpenMmapFile(path string) (*MmapFile, error) {
func OpenMmapFileWithSize(path string, size int) (mf *MmapFile, retErr error) { func OpenMmapFileWithSize(path string, size int) (mf *MmapFile, retErr error) {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "try lock file") return nil, fmt.Errorf("try lock file: %w", err)
} }
defer func() { defer func() {
if retErr != nil { if retErr != nil {
@ -41,14 +40,14 @@ func OpenMmapFileWithSize(path string, size int) (mf *MmapFile, retErr error) {
if size <= 0 { if size <= 0 {
info, err := f.Stat() info, err := f.Stat()
if err != nil { if err != nil {
return nil, errors.Wrap(err, "stat") return nil, fmt.Errorf("stat: %w", err)
} }
size = int(info.Size()) size = int(info.Size())
} }
b, err := mmap(f, size) b, err := mmap(f, size)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "mmap, size %d", size) return nil, fmt.Errorf("mmap, size %d: %w", size, err)
} }
return &MmapFile{f: f, b: b}, nil return &MmapFile{f: f, b: b}, nil

9
tsdb/fileutil/preallocate_linux.go

@ -15,6 +15,7 @@
package fileutil package fileutil
import ( import (
"errors"
"os" "os"
"syscall" "syscall"
) )
@ -23,10 +24,10 @@ func preallocExtend(f *os.File, sizeInBytes int64) error {
// use mode = 0 to change size // use mode = 0 to change size
err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes) err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes)
if err != nil { if err != nil {
errno, ok := err.(syscall.Errno) var errno syscall.Errno
// not supported; fallback // not supported; fallback
// fallocate EINTRs frequently in some environments; fallback // fallocate EINTRs frequently in some environments; fallback
if ok && (errno == syscall.ENOTSUP || errno == syscall.EINTR) { if errors.As(err, &errno) && (errno == syscall.ENOTSUP || errno == syscall.EINTR) {
return preallocExtendTrunc(f, sizeInBytes) return preallocExtendTrunc(f, sizeInBytes)
} }
} }
@ -37,9 +38,9 @@ func preallocFixed(f *os.File, sizeInBytes int64) error {
// use mode = 1 to keep size; see FALLOC_FL_KEEP_SIZE // use mode = 1 to keep size; see FALLOC_FL_KEEP_SIZE
err := syscall.Fallocate(int(f.Fd()), 1, 0, sizeInBytes) err := syscall.Fallocate(int(f.Fd()), 1, 0, sizeInBytes)
if err != nil { if err != nil {
errno, ok := err.(syscall.Errno) var errno syscall.Errno
// treat not supported as nil error // treat not supported as nil error
if ok && errno == syscall.ENOTSUP { if errors.As(err, &errno) && errno == syscall.ENOTSUP {
return nil return nil
} }
} }

Loading…
Cancel
Save