Skip to content

Commit

Permalink
Remove usage of ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
  • Loading branch information
saswatamcode committed Aug 2, 2022
1 parent 0febf14 commit fbb6946
Show file tree
Hide file tree
Showing 75 changed files with 265 additions and 312 deletions.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -395,7 +395,8 @@ github.com/prometheus/client_golang/prometheus.{DefaultGatherer,DefBuckets,NewUn
github.com/prometheus/client_golang/prometheus.{NewCounter,NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,\
NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec}=github.com/prometheus/client_golang/prometheus/promauto.{NewCounter,\
NewCounterVec,NewCounterVec,NewGauge,NewGaugeVec,NewGaugeFunc,NewHistorgram,NewHistogramVec,NewSummary,NewSummaryVec},\
sync/atomic=go.uber.org/atomic,github.com/cortexproject/cortex=github.com/thanos-io/thanos/internal/cortex" $(shell go list ./... | grep -v "internal/cortex")
sync/atomic=go.uber.org/atomic,github.com/cortexproject/cortex=github.com/thanos-io/thanos/internal/cortex,\
io/ioutil.{Discard,NopCloser,ReadAll,ReadDir,ReadFile,TempDir,TempFile,Writefile}" $(shell go list ./... | grep -v "internal/cortex")
@$(FAILLINT) -paths "fmt.{Print,Println,Sprint}" -ignore-tests ./...
@echo ">> linting all of the Go files GOGC=${GOGC}"
@$(GOLANGCI_LINT) run
Expand Down
5 changes: 2 additions & 3 deletions cmd/thanos/main_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -105,7 +104,7 @@ func (b *erroringBucket) Name() string {
// Testing for https://github.com/thanos-io/thanos/issues/4960.
func TestRegression4960_Deadlock(t *testing.T) {
logger := log.NewLogfmtLogger(os.Stderr)
dir, err := ioutil.TempDir("", "test-compact-cleanup")
dir, err := os.MkdirTemp("", "test-compact-cleanup")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()

Expand Down Expand Up @@ -168,7 +167,7 @@ func TestRegression4960_Deadlock(t *testing.T) {

func TestCleanupDownsampleCacheFolder(t *testing.T) {
logger := log.NewLogfmtLogger(os.Stderr)
dir, err := ioutil.TempDir("", "test-compact-cleanup")
dir, err := os.MkdirTemp("", "test-compact-cleanup")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()

Expand Down
3 changes: 1 addition & 2 deletions cmd/thanos/receive.go
Expand Up @@ -5,7 +5,6 @@ package main

import (
"context"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -692,7 +691,7 @@ func migrateLegacyStorage(logger log.Logger, dataDir, defaultTenantID string) er

level.Info(logger).Log("msg", "found legacy storage, migrating to multi-tsdb layout with default tenant", "defaultTenantID", defaultTenantID)

files, err := ioutil.ReadDir(dataDir)
files, err := os.ReadDir(dataDir)
if err != nil {
return errors.Wrapf(err, "read legacy data dir: %v", dataDir)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/thanos/tools_bucket.go
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -1153,7 +1152,7 @@ func registerBucketRewrite(app extkingpin.AppClause, objStoreConfig *extflag.Pat
ctx, cancel := context.WithCancel(context.Background())
g.Add(func() error {
chunkPool := chunkenc.NewPool()
changeLog := compactv2.NewChangeLog(ioutil.Discard)
changeLog := compactv2.NewChangeLog(io.Discard)
stubCounter := promauto.With(nil).NewCounter(prometheus.CounterOpts{})
for _, id := range ids {
// Delete series from block & modify.
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/api_test.go
Expand Up @@ -18,7 +18,7 @@ package api

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand All @@ -44,7 +44,7 @@ func TestRespondSuccess(t *testing.T) {
if err != nil {
t.Fatalf("Error on test request: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
defer func() { testutil.Ok(t, resp.Body.Close()) }()
if err != nil {
t.Fatalf("Error reading response body: %s", err)
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestRespondError(t *testing.T) {
if err != nil {
t.Fatalf("Error on test request: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
defer func() { testutil.Ok(t, resp.Body.Close()) }()
if err != nil {
t.Fatalf("Error reading response body: %s", err)
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/blocks/v1_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -94,7 +93,7 @@ func testEndpoint(t *testing.T, test endpointTestCase, name string, responseComp

func TestMarkBlockEndpoint(t *testing.T) {
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "test-read-mark")
tmpDir, err := os.MkdirTemp("", "test-read-mark")
testutil.Ok(t, err)

// create block
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/query/v1_test.go
Expand Up @@ -21,11 +21,11 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -680,7 +680,7 @@ func TestMetadataEndpoints(t *testing.T) {
},
}

dir, err := ioutil.TempDir("", "prometheus-test")
dir, err := os.MkdirTemp("", "prometheus-test")
testutil.Ok(t, err)

const chunkRange int64 = 600_000
Expand Down
15 changes: 10 additions & 5 deletions pkg/block/block.go
Expand Up @@ -9,7 +9,7 @@ import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -283,7 +283,7 @@ func DownloadMeta(ctx context.Context, logger log.Logger, bkt objstore.Bucket, i

var m metadata.Meta

obj, err := ioutil.ReadAll(rc)
obj, err := io.ReadAll(rc)
if err != nil {
return metadata.Meta{}, errors.Wrapf(err, "read meta.json for block %s", id.String())
}
Expand All @@ -303,7 +303,7 @@ func IsBlockDir(path string) (id ulid.ULID, ok bool) {
// GetSegmentFiles returns list of segment files for given block. Paths are relative to the chunks directory.
// In case of errors, nil is returned.
func GetSegmentFiles(blockDir string) []string {
files, err := ioutil.ReadDir(filepath.Join(blockDir, ChunksDirname))
files, err := os.ReadDir(filepath.Join(blockDir, ChunksDirname))
if err != nil {
return nil
}
Expand All @@ -318,14 +318,19 @@ func GetSegmentFiles(blockDir string) []string {

// GatherFileStats returns metadata.File entry for files inside TSDB block (index, chunks, meta.json).
func GatherFileStats(blockDir string, hf metadata.HashFunc, logger log.Logger) (res []metadata.File, _ error) {
files, err := ioutil.ReadDir(filepath.Join(blockDir, ChunksDirname))
files, err := os.ReadDir(filepath.Join(blockDir, ChunksDirname))
if err != nil {
return nil, errors.Wrapf(err, "read dir %v", filepath.Join(blockDir, ChunksDirname))
}
for _, f := range files {
fi, err := f.Info()
if err != nil {
return nil, errors.Wrapf(err, "getting file info %v", filepath.Join(ChunksDirname, f.Name()))
}

mf := metadata.File{
RelPath: filepath.Join(ChunksDirname, f.Name()),
SizeBytes: f.Size(),
SizeBytes: fi.Size(),
}
if hf != metadata.NoneFunc && !f.IsDir() {
h, err := metadata.CalculateHash(filepath.Join(blockDir, ChunksDirname, f.Name()), hf, logger)
Expand Down
13 changes: 6 additions & 7 deletions pkg/block/block_test.go
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -81,7 +80,7 @@ func TestUpload(t *testing.T) {

ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-block-upload")
tmpDir, err := os.MkdirTemp("", "test-block-upload")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down Expand Up @@ -234,7 +233,7 @@ func TestDelete(t *testing.T) {
defer testutil.TolerantVerifyLeak(t)
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-block-delete")
tmpDir, err := os.MkdirTemp("", "test-block-delete")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down Expand Up @@ -281,7 +280,7 @@ func TestMarkForDeletion(t *testing.T) {
defer testutil.TolerantVerifyLeak(t)
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-block-mark-for-delete")
tmpDir, err := os.MkdirTemp("", "test-block-mark-for-delete")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down Expand Up @@ -337,7 +336,7 @@ func TestMarkForNoCompact(t *testing.T) {
defer testutil.TolerantVerifyLeak(t)
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-block-mark-for-no-compact")
tmpDir, err := os.MkdirTemp("", "test-block-mark-for-no-compact")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down Expand Up @@ -397,7 +396,7 @@ func TestHashDownload(t *testing.T) {

ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-block-download")
tmpDir, err := os.MkdirTemp("", "test-block-download")
testutil.Ok(t, err)
t.Cleanup(func() {
testutil.Ok(t, os.RemoveAll(tmpDir))
Expand Down Expand Up @@ -493,7 +492,7 @@ func TestUploadCleanup(t *testing.T) {

ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-block-upload")
tmpDir, err := os.MkdirTemp("", "test-block-upload")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down
6 changes: 3 additions & 3 deletions pkg/block/fetcher.go
Expand Up @@ -6,7 +6,7 @@ package block
import (
"context"
"encoding/json"
"io/ioutil"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -267,7 +267,7 @@ func (f *BaseFetcher) loadMeta(ctx context.Context, id ulid.ULID) (*metadata.Met

defer runutil.CloseWithLogOnErr(f.logger, r, "close bkt meta get")

metaContent, err := ioutil.ReadAll(r)
metaContent, err := io.ReadAll(r)
if err != nil {
return nil, errors.Wrapf(err, "read meta file: %v", metaFile)
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func (f *BaseFetcher) fetchMetadata(ctx context.Context) (interface{}, error) {

// Best effort cleanup of disk-cached metas.
if f.cacheDir != "" {
fis, err := ioutil.ReadDir(f.cacheDir)
fis, err := os.ReadDir(f.cacheDir)
names := make([]string, 0, len(fis))
for _, fi := range fis {
names = append(names, fi.Name())
Expand Down
3 changes: 1 addition & 2 deletions pkg/block/fetcher_test.go
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
Expand Down Expand Up @@ -68,7 +67,7 @@ func TestMetaFetcher_Fetch(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

dir, err := ioutil.TempDir("", "test-meta-fetcher")
dir, err := os.MkdirTemp("", "test-meta-fetcher")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()

Expand Down
5 changes: 2 additions & 3 deletions pkg/block/index_test.go
Expand Up @@ -5,7 +5,6 @@ package block

import (
"context"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand All @@ -25,7 +24,7 @@ import (
func TestRewrite(t *testing.T) {
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-indexheader")
tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down Expand Up @@ -88,7 +87,7 @@ func TestRewrite(t *testing.T) {
}

func TestGatherIndexHealthStatsReturnsOutOfOrderChunksErr(t *testing.T) {
blockDir, err := ioutil.TempDir("", "test-ooo-index")
blockDir, err := os.MkdirTemp("", "test-ooo-index")
testutil.Ok(t, err)

err = testutil.PutOutOfOrderIndex(blockDir, 0, math.MaxInt64)
Expand Down
5 changes: 2 additions & 3 deletions pkg/block/indexheader/binary_reader.go
Expand Up @@ -10,7 +10,6 @@ import (
"hash"
"hash/crc32"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -143,7 +142,7 @@ func newChunkedIndexReader(ctx context.Context, bkt objstore.BucketReader, id ul
return nil, 0, errors.Wrapf(err, "get TOC from object storage of %s", indexFilepath)
}

b, err := ioutil.ReadAll(rc)
b, err := io.ReadAll(rc)
if err != nil {
runutil.CloseWithErrCapture(&err, rc, "close reader")
return nil, 0, errors.Wrapf(err, "get header from object storage of %s", indexFilepath)
Expand Down Expand Up @@ -185,7 +184,7 @@ func (r *chunkedIndexReader) readTOC() (*index.TOC, error) {
return nil, errors.Wrapf(err, "get TOC from object storage of %s", r.path)
}

tocBytes, err := ioutil.ReadAll(rc)
tocBytes, err := io.ReadAll(rc)
if err != nil {
runutil.CloseWithErrCapture(&err, rc, "close toc reader")
return nil, errors.Wrapf(err, "get TOC from object storage of %s", r.path)
Expand Down
9 changes: 4 additions & 5 deletions pkg/block/indexheader/header_test.go
Expand Up @@ -6,7 +6,6 @@ package indexheader
import (
"context"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
Expand All @@ -32,7 +31,7 @@ import (
func TestReaders(t *testing.T) {
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "test-indexheader")
tmpDir, err := os.MkdirTemp("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down Expand Up @@ -333,7 +332,7 @@ func prepareIndexV2Block(t testing.TB, tmpDir string, bkt objstore.Bucket) *meta
func BenchmarkBinaryWrite(t *testing.B) {
ctx := context.Background()

tmpDir, err := ioutil.TempDir("", "bench-indexheader")
tmpDir, err := os.MkdirTemp("", "bench-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand All @@ -352,7 +351,7 @@ func BenchmarkBinaryWrite(t *testing.B) {

func BenchmarkBinaryReader(t *testing.B) {
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "bench-indexheader")
tmpDir, err := os.MkdirTemp("", "bench-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()

Expand Down Expand Up @@ -385,7 +384,7 @@ func benchmarkBinaryReaderLookupSymbol(b *testing.B, numSeries int) {
ctx := context.Background()
logger := log.NewNopLogger()

tmpDir, err := ioutil.TempDir("", "benchmark-lookupsymbol")
tmpDir, err := os.MkdirTemp("", "benchmark-lookupsymbol")
testutil.Ok(b, err)
defer func() { testutil.Ok(b, os.RemoveAll(tmpDir)) }()

Expand Down

0 comments on commit fbb6946

Please sign in to comment.