Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean some linter errors and remove use of ioutils as it's deprecated #529

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions file_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"hash"
"hash/crc64"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -245,7 +244,7 @@ func (f *FileSnapshotStore) List() ([]*SnapshotMeta, error) {
// getSnapshots returns all the known snapshots.
func (f *FileSnapshotStore) getSnapshots() ([]*fileSnapshotMeta, error) {
// Get the eligible snapshots
snapshots, err := ioutil.ReadDir(f.path)
snapshots, err := os.ReadDir(f.path)
if err != nil {
f.logger.Error("failed to scan snapshot directory", "error", err)
return nil, err
Expand Down
21 changes: 10 additions & 11 deletions file_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package raft
import (
"bytes"
"io"
"io/ioutil"
"os"
"reflect"
"runtime"
Expand All @@ -28,13 +27,13 @@ func TestFileSnapshotSinkImpl(t *testing.T) {
}

func TestFileSS_CreateSnapshotMissingParentDir(t *testing.T) {
parent, err := ioutil.TempDir("", "raft")
parent, err := os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
defer os.RemoveAll(parent)

dir, err := ioutil.TempDir(parent, "raft")
dir, err := os.MkdirTemp(parent, "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
Expand All @@ -54,7 +53,7 @@ func TestFileSS_CreateSnapshotMissingParentDir(t *testing.T) {
}
func TestFileSS_CreateSnapshot(t *testing.T) {
// Create a test dir
dir, err := ioutil.TempDir("", "raft")
dir, err := os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
Expand Down Expand Up @@ -162,7 +161,7 @@ func TestFileSS_CreateSnapshot(t *testing.T) {

func TestFileSS_CancelSnapshot(t *testing.T) {
// Create a test dir
dir, err := ioutil.TempDir("", "raft")
dir, err := os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
Expand Down Expand Up @@ -200,7 +199,7 @@ func TestFileSS_Retention(t *testing.T) {
var err error
// Create a test dir
var dir string
dir, err = ioutil.TempDir("", "raft")
dir, err = os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
Expand Down Expand Up @@ -253,15 +252,15 @@ func TestFileSS_BadPerm(t *testing.T) {

// Create a temp dir
var dir1 string
dir1, err = ioutil.TempDir("", "raft")
dir1, err = os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(dir1)

// Create a sub dir and remove all permissions
var dir2 string
dir2, err = ioutil.TempDir(dir1, "badperm")
dir2, err = os.MkdirTemp(dir1, "badperm")
if err != nil {
t.Fatalf("err: %s", err)
}
Expand All @@ -277,13 +276,13 @@ func TestFileSS_BadPerm(t *testing.T) {
}

func TestFileSS_MissingParentDir(t *testing.T) {
parent, err := ioutil.TempDir("", "raft")
parent, err := os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
defer os.RemoveAll(parent)

dir, err := ioutil.TempDir(parent, "raft")
dir, err := os.MkdirTemp(parent, "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
Expand All @@ -297,7 +296,7 @@ func TestFileSS_MissingParentDir(t *testing.T) {

func TestFileSS_Ordering(t *testing.T) {
// Create a test dir
dir, err := ioutil.TempDir("", "raft")
dir, err := os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
Expand Down
3 changes: 1 addition & 2 deletions fuzzy/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -294,7 +293,7 @@ func (c *cluster) VerifyFSM(t *testing.T) {
}

func (c *cluster) RecordState(t *testing.T) {
td, _ := ioutil.TempDir(os.Getenv("TEST_FAIL_DIR"), "failure")
td, _ := os.MkdirTemp(os.Getenv("TEST_FAIL_DIR"), "failure")
sd, _ := resolveDirectory("data", false)
copyDir(td, sd)
dump := func(n *raftNode) {
Expand Down
23 changes: 0 additions & 23 deletions fuzzy/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,21 @@ github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.9.1 h1:9PZfAcVEvez4yhLH2TBU64/h/z4xlFI80cWXRrxuKuM=
github.com/hashicorp/go-hclog v0.9.1/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.16.0 h1:uCeOEwSWGMwhJUdpUjk+1cVKIEfGu2/1nFXukimi2MU=
github.com/hashicorp/go-hclog v0.16.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-immutable-radix v1.0.0 h1:AKDB1HM5PWEA7i4nhcpwOrO2byshxBjXVn/J/3+z5/0=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.5 h1:i9R9JSrqIz0QVLz3sz+i3YJdT7TTSLcfLLzJi9aZTuI=
github.com/hashicorp/go-msgpack v0.5.5/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-msgpack v1.1.5 h1:9byZdVjKTe5mce63pRVNP1L7UAmdHOTEMGehn6KvJWs=
github.com/hashicorp/go-msgpack v1.1.5/go.mod h1:gWVc3sv/wbDmR3rQsj1CAktEZzoz1YNK9NfGLXJ69/4=
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea h1:xykPFhrBAS2J0VBzVa5e80b5ZtYuNQtgXjN40qBZlD4=
github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea/go.mod h1:pNv7Wc3ycL6F5oOWn+tPGo2gWD4a5X+yp/ntwdKLjRk=
github.com/hashicorp/raft-boltdb v0.0.0-20210409134258-03c10cc3d4ea h1:RxcPJuutPRM8PUOyiweMmkuNO+RJyfy2jds2gfvgNmU=
github.com/hashicorp/raft-boltdb v0.0.0-20210409134258-03c10cc3d4ea/go.mod h1:qRd6nFJYYS6Iqnc/8HcUmko2/2Gw8qTFEmxDLii6W5I=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
Expand All @@ -52,17 +39,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190523142557-0e01d883c5c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0 h1:g9s1Ppvvun/fI+BptTMj909BBIcGrzQ32k9FNlcevOE=
golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190424220101-1e8e1cfdf96b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
3 changes: 1 addition & 2 deletions inmem_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"sync"
)

Expand Down Expand Up @@ -88,7 +87,7 @@ func (m *InmemSnapshotStore) Open(id string) (*SnapshotMeta, io.ReadCloser, erro
// Make a copy of the contents, since a bytes.Buffer can only be read
// once.
contents := bytes.NewBuffer(m.latest.contents.Bytes())
return &m.latest.meta, ioutil.NopCloser(contents), nil
return &m.latest.meta, io.NopCloser(contents), nil
}

// Write appends the given bytes to the snapshot contents
Expand Down
3 changes: 1 addition & 2 deletions integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package raft
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -77,7 +76,7 @@ func MakeRaft(t *testing.T, conf *Config, bootstrap bool) *RaftEnv {
conf = inmemConfig(t)
}

dir, err := ioutil.TempDir("", "raft")
dir, err := os.MkdirTemp("", "raft")
if err != nil {
t.Fatalf("err: %v ", err)
}
Expand Down
6 changes: 3 additions & 3 deletions net_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (n *NetworkTransport) getStreamContext() context.Context {
return n.streamCtx
}

// SetHeartbeatHandler is used to setup a heartbeat handler
// SetHeartbeatHandler is used to set up a heartbeat handler
// as a fast-pass. This is to avoid head-of-line blocking from
// disk IO.
func (n *NetworkTransport) SetHeartbeatHandler(cb func(rpc RPC)) {
Expand Down Expand Up @@ -367,7 +367,7 @@ func (n *NetworkTransport) returnConn(conn *netConn) {
defer n.connPoolLock.Unlock()

key := conn.target
conns, _ := n.connPool[key]
conns := n.connPool[key]

if !n.IsShutdown() && len(conns) < n.maxPool {
n.connPool[key] = append(conns, conn)
Expand Down Expand Up @@ -793,7 +793,7 @@ func (n *netPipeline) Consumer() <-chan AppendFuture {
return n.doneCh
}

// Closed is used to shutdown the pipeline connection.
// Close is used to shut down the pipeline connection.
func (n *netPipeline) Close() error {
n.shutdownLock.Lock()
defer n.shutdownLock.Unlock()
Expand Down
6 changes: 3 additions & 3 deletions peersjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package raft
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
)

// ReadPeersJSON consumes a legacy peers.json file in the format of the old JSON
Expand All @@ -17,7 +17,7 @@ import (
// support for these, nor non-voter suffrage types.
func ReadPeersJSON(path string) (Configuration, error) {
// Read in the file.
buf, err := ioutil.ReadFile(path)
buf, err := os.ReadFile(path)
if err != nil {
return Configuration{}, err
}
Expand Down Expand Up @@ -66,7 +66,7 @@ type configEntry struct {
// versions that use server IDs.
func ReadConfigJSON(path string) (Configuration, error) {
// Read in the file.
buf, err := ioutil.ReadFile(path)
buf, err := os.ReadFile(path)
if err != nil {
return Configuration{}, err
}
Expand Down
13 changes: 6 additions & 7 deletions peersjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package raft

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -15,14 +14,14 @@ import (
func TestPeersJSON_BadConfiguration(t *testing.T) {
var err error
var base string
base, err = ioutil.TempDir("", "")
base, err = os.MkdirTemp("", "")
if err != nil {
t.Fatalf("err: %v", err)
}
defer os.RemoveAll(base)

peers := filepath.Join(base, "peers.json")
if err = ioutil.WriteFile(peers, []byte("null"), 0666); err != nil {
if err = os.WriteFile(peers, []byte("null"), 0666); err != nil {
t.Fatalf("err: %v", err)
}

Expand All @@ -35,7 +34,7 @@ func TestPeersJSON_BadConfiguration(t *testing.T) {
func TestPeersJSON_ReadPeersJSON(t *testing.T) {
var err error
var base string
base, err = ioutil.TempDir("", "")
base, err = os.MkdirTemp("", "")
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -47,7 +46,7 @@ func TestPeersJSON_ReadPeersJSON(t *testing.T) {
"127.0.0.3:123"]
`)
peers := filepath.Join(base, "peers.json")
if err = ioutil.WriteFile(peers, content, 0666); err != nil {
if err = os.WriteFile(peers, content, 0666); err != nil {
t.Fatalf("err: %v", err)
}
var configuration Configuration
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestPeersJSON_ReadPeersJSON(t *testing.T) {
func TestPeersJSON_ReadConfigJSON(t *testing.T) {
var err error
var base string
base, err = ioutil.TempDir("", "")
base, err = os.MkdirTemp("", "")
if err != nil {
t.Fatalf("err: %v", err)
}
Expand All @@ -108,7 +107,7 @@ func TestPeersJSON_ReadConfigJSON(t *testing.T) {
]
`)
peers := filepath.Join(base, "peers.json")
if err = ioutil.WriteFile(peers, content, 0666); err != nil {
if err = os.WriteFile(peers, content, 0666); err != nil {
t.Fatalf("err: %v", err)
}

Expand Down
9 changes: 3 additions & 6 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"container/list"
"fmt"
"io"
"io/ioutil"
"sync/atomic"
"time"

Expand Down Expand Up @@ -217,7 +216,7 @@ func (r *Raft) runFollower() {

// Check if we have had a successful contact
lastContact := r.LastContact()
if time.Now().Sub(lastContact) < hbTimeout {
if time.Since(lastContact) < hbTimeout {
continue
}

Expand Down Expand Up @@ -1511,7 +1510,6 @@ func (r *Raft) appendEntries(rpc RPC, a *AppendEntriesRequest) {
// Everything went well, set success
resp.Success = true
r.setLastContact()
return
}

// processConfigurationLogEntry takes a log entry and updates the latest
Expand Down Expand Up @@ -1631,7 +1629,7 @@ func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) {
// Check if we've voted in this election before
if lastVoteTerm == req.Term && lastVoteCandBytes != nil {
r.logger.Info("duplicate requestVote for same term", "term", req.Term)
if bytes.Compare(lastVoteCandBytes, candidateBytes) == 0 {
if bytes.Equal(lastVoteCandBytes, candidateBytes) {
r.logger.Warn("duplicate requestVote from", "candidate", candidate)
resp.Granted = true
}
Expand Down Expand Up @@ -1664,7 +1662,6 @@ func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) {

resp.Granted = true
r.setLastContact()
return
}

// installSnapshot is invoked when we get a InstallSnapshot RPC call.
Expand All @@ -1680,7 +1677,7 @@ func (r *Raft) installSnapshot(rpc RPC, req *InstallSnapshotRequest) {
}
var rpcErr error
defer func() {
io.Copy(ioutil.Discard, rpc.Reader) // ensure we always consume all the snapshot data from the stream [see issue #212]
_, _ = io.Copy(io.Discard, rpc.Reader) // ensure we always consume all the snapshot data from the stream [see issue #212]
rpc.Respond(resp, rpcErr)
}()

Expand Down