Replace fileutil.ReadDir with ioutil.ReadDir (#7029) (#7033)

* tsdb: Replace fileutil.ReadDir with ioutil.ReadDir (#7029)

Signed-off-by: Brad Walker <brad@bradmwalker.com>

* tsdb: Remove fileutil.ReadDir (#7029)

Signed-off-by: Brad Walker <brad@bradmwalker.com>
This commit is contained in:
Brad Walker 2020-04-06 07:34:20 -06:00 committed by GitHub
parent 05442b31c8
commit 3348930df5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 37 deletions

View file

@ -601,14 +601,14 @@ func (s *Reader) Chunk(ref uint64) (chunkenc.Chunk, error) {
} }
func nextSequenceFile(dir string) (string, int, error) { func nextSequenceFile(dir string) (string, int, error) {
names, err := fileutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
return "", 0, err return "", 0, err
} }
i := uint64(0) i := uint64(0)
for _, n := range names { for _, f := range files {
j, err := strconv.ParseUint(n, 10, 64) j, err := strconv.ParseUint(f.Name(), 10, 64)
if err != nil { if err != nil {
continue continue
} }

View file

@ -1427,14 +1427,14 @@ func sequenceFiles(dir string) ([]string, error) {
} }
func nextSequenceFile(dir string) (string, int, error) { func nextSequenceFile(dir string) (string, int, error) {
names, err := fileutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
return "", 0, err return "", 0, err
} }
i := uint64(0) i := uint64(0)
for _, n := range names { for _, f := range files {
j, err := strconv.ParseUint(n, 10, 64) j, err := strconv.ParseUint(f.Name(), 10, 64)
if err != nil { if err != nil {
continue continue
} }

View file

@ -21,7 +21,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
) )
@ -91,21 +90,6 @@ func readDirs(src string) ([]string, error) {
return files, nil return files, nil
} }
// ReadDir returns the filenames in the given directory in sorted order.
func ReadDir(dirpath string) ([]string, error) {
dir, err := os.Open(dirpath)
if err != nil {
return nil, err
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
// Rename safely renames a file. // Rename safely renames a file.
func Rename(from, to string) error { func Rename(from, to string) error {
if err := os.Rename(from, to); err != nil { if err := os.Rename(from, to); err != nil {

View file

@ -24,7 +24,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/record" "github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/util/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
@ -92,9 +91,13 @@ func TestDeleteCheckpoints(t *testing.T) {
testutil.Ok(t, DeleteCheckpoints(dir, 2)) testutil.Ok(t, DeleteCheckpoints(dir, 2))
files, err := fileutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
testutil.Ok(t, err) testutil.Ok(t, err)
testutil.Equals(t, []string{"checkpoint.02", "checkpoint.03"}, files) fns := []string{}
for _, f := range files {
fns = append(fns, f.Name())
}
testutil.Equals(t, []string{"checkpoint.02", "checkpoint.03"}, fns)
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777)) testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777))
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777)) testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777))
@ -102,9 +105,13 @@ func TestDeleteCheckpoints(t *testing.T) {
testutil.Ok(t, DeleteCheckpoints(dir, 100000000)) testutil.Ok(t, DeleteCheckpoints(dir, 100000000))
files, err = fileutil.ReadDir(dir) files, err = ioutil.ReadDir(dir)
testutil.Ok(t, err) testutil.Ok(t, err)
testutil.Equals(t, []string{"checkpoint.100000000", "checkpoint.100000001"}, files) fns = []string{}
for _, f := range files {
fns = append(fns, f.Name())
}
testutil.Equals(t, []string{"checkpoint.100000000", "checkpoint.100000001"}, fns)
} }
func TestCheckpoint(t *testing.T) { func TestCheckpoint(t *testing.T) {
@ -178,10 +185,10 @@ func TestCheckpoint(t *testing.T) {
testutil.Ok(t, DeleteCheckpoints(w.Dir(), 106)) testutil.Ok(t, DeleteCheckpoints(w.Dir(), 106))
// Only the new checkpoint should be left. // Only the new checkpoint should be left.
files, err := fileutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
testutil.Ok(t, err) testutil.Ok(t, err)
testutil.Equals(t, 1, len(files)) testutil.Equals(t, 1, len(files))
testutil.Equals(t, "checkpoint.00000106", files[0]) testutil.Equals(t, "checkpoint.00000106", files[0].Name())
sr, err := NewSegmentsReader(filepath.Join(dir, "checkpoint.00000106")) sr, err := NewSegmentsReader(filepath.Join(dir, "checkpoint.00000106"))
testutil.Ok(t, err) testutil.Ok(t, err)

View file

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"hash/crc32" "hash/crc32"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -764,11 +765,12 @@ type segmentRef struct {
} }
func listSegments(dir string) (refs []segmentRef, err error) { func listSegments(dir string) (refs []segmentRef, err error) {
files, err := fileutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, fn := range files { for _, f := range files {
fn := f.Name()
k, err := strconv.Atoi(fn) k, err := strconv.Atoi(fn)
if err != nil { if err != nil {
continue continue

View file

@ -16,6 +16,7 @@ package wal
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"os" "os"
"path" "path"
@ -29,7 +30,6 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/record" "github.com/prometheus/prometheus/tsdb/record"
) )
@ -293,15 +293,15 @@ func (w *Watcher) firstAndLast() (int, int, error) {
// Copied from tsdb/wal/wal.go so we do not have to open a WAL. // Copied from tsdb/wal/wal.go so we do not have to open a WAL.
// Plan is to move WAL watcher to TSDB and dedupe these implementations. // Plan is to move WAL watcher to TSDB and dedupe these implementations.
func (w *Watcher) segments(dir string) ([]int, error) { func (w *Watcher) segments(dir string) ([]int, error) {
files, err := fileutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var refs []int var refs []int
var last int var last int
for _, fn := range files { for _, f := range files {
k, err := strconv.Atoi(fn) k, err := strconv.Atoi(f.Name())
if err != nil { if err != nil {
continue continue
} }

View file

@ -28,7 +28,6 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/record" "github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/tsdb/tombstones" "github.com/prometheus/prometheus/tsdb/tombstones"
"github.com/prometheus/prometheus/tsdb/wal" "github.com/prometheus/prometheus/tsdb/wal"
@ -303,8 +302,12 @@ func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
_, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil) _, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil)
testutil.Ok(t, err) testutil.Ok(t, err)
fns, err := fileutil.ReadDir(dir) files, err := ioutil.ReadDir(dir)
testutil.Ok(t, err) testutil.Ok(t, err)
fns := []string{}
for _, f := range files {
fns = append(fns, f.Name())
}
testutil.Equals(t, []string{"000000"}, fns) testutil.Equals(t, []string{"000000"}, fns)
} }