Skip to content

Commit

Permalink
Replace fileutil.ReadDir with ioutil.ReadDir (prometheus#7029) (prome…
Browse files Browse the repository at this point in the history
…theus#7033)

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

Signed-off-by: Brad Walker <[email protected]>

* tsdb: Remove fileutil.ReadDir (prometheus#7029)

Signed-off-by: Brad Walker <[email protected]>
  • Loading branch information
Brad Walker authored Apr 6, 2020
1 parent 05442b3 commit 3348930
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
6 changes: 3 additions & 3 deletions tsdb/chunks/chunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,14 @@ func (s *Reader) Chunk(ref uint64) (chunkenc.Chunk, error) {
}

func nextSequenceFile(dir string) (string, int, error) {
names, err := fileutil.ReadDir(dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", 0, err
}

i := uint64(0)
for _, n := range names {
j, err := strconv.ParseUint(n, 10, 64)
for _, f := range files {
j, err := strconv.ParseUint(f.Name(), 10, 64)
if err != nil {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions tsdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1427,14 +1427,14 @@ func sequenceFiles(dir string) ([]string, error) {
}

func nextSequenceFile(dir string) (string, int, error) {
names, err := fileutil.ReadDir(dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", 0, err
}

i := uint64(0)
for _, n := range names {
j, err := strconv.ParseUint(n, 10, 64)
for _, f := range files {
j, err := strconv.ParseUint(f.Name(), 10, 64)
if err != nil {
continue
}
Expand Down
16 changes: 0 additions & 16 deletions tsdb/fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)

Expand Down Expand Up @@ -91,21 +90,6 @@ func readDirs(src string) ([]string, error) {
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.
func Rename(from, to string) error {
if err := os.Rename(from, to); err != nil {
Expand Down
21 changes: 14 additions & 7 deletions tsdb/wal/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/pkg/errors"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/util/testutil"
)
Expand Down Expand Up @@ -92,19 +91,27 @@ func TestDeleteCheckpoints(t *testing.T) {

testutil.Ok(t, DeleteCheckpoints(dir, 2))

files, err := fileutil.ReadDir(dir)
files, err := ioutil.ReadDir(dir)
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.100000000"), 0777))
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000001"), 0777))

testutil.Ok(t, DeleteCheckpoints(dir, 100000000))

files, err = fileutil.ReadDir(dir)
files, err = ioutil.ReadDir(dir)
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) {
Expand Down Expand Up @@ -178,10 +185,10 @@ func TestCheckpoint(t *testing.T) {
testutil.Ok(t, DeleteCheckpoints(w.Dir(), 106))

// Only the new checkpoint should be left.
files, err := fileutil.ReadDir(dir)
files, err := ioutil.ReadDir(dir)
testutil.Ok(t, err)
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"))
testutil.Ok(t, err)
Expand Down
6 changes: 4 additions & 2 deletions tsdb/wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -764,11 +765,12 @@ type segmentRef struct {
}

func listSegments(dir string) (refs []segmentRef, err error) {
files, err := fileutil.ReadDir(dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
for _, fn := range files {
for _, f := range files {
fn := f.Name()
k, err := strconv.Atoi(fn)
if err != nil {
continue
Expand Down
8 changes: 4 additions & 4 deletions tsdb/wal/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package wal
import (
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/record"
)

Expand Down Expand Up @@ -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.
// Plan is to move WAL watcher to TSDB and dedupe these implementations.
func (w *Watcher) segments(dir string) ([]int, error) {
files, err := fileutil.ReadDir(dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}

var refs []int
var last int
for _, fn := range files {
k, err := strconv.Atoi(fn)
for _, f := range files {
k, err := strconv.Atoi(f.Name())
if err != nil {
continue
}
Expand Down
7 changes: 5 additions & 2 deletions tsdb/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/record"
"github.com/prometheus/prometheus/tsdb/tombstones"
"github.com/prometheus/prometheus/tsdb/wal"
Expand Down Expand Up @@ -303,8 +302,12 @@ func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
_, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil)
testutil.Ok(t, err)

fns, err := fileutil.ReadDir(dir)
files, err := ioutil.ReadDir(dir)
testutil.Ok(t, err)
fns := []string{}
for _, f := range files {
fns = append(fns, f.Name())
}
testutil.Equals(t, []string{"000000"}, fns)
}

Expand Down

0 comments on commit 3348930

Please sign in to comment.