Skip to content

Commit

Permalink
fix: golangci-lint error
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <poan.yang@suse.com>
  • Loading branch information
FrankYang0529 committed Apr 25, 2024
1 parent be5e072 commit 55d34d1
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 91 deletions.
4 changes: 3 additions & 1 deletion app/cmd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ func startController(c *cli.Context) error {
control.GRPCAddress = util.GetGRPCAddress(listen)
control.GRPCServer = controllerrpc.GetControllerGRPCServer(volumeName, engineInstanceName, control)

control.StartGRPCServer()
if err = control.StartGRPCServer(); err != nil {
return err
}
return control.WaitForShutdown()
}

Expand Down
1 change: 0 additions & 1 deletion app/cmd/restore_to_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func CleanupTempFiles(outputFile string, files ...string) {
continue
}
}
return
}

func ConvertImage(srcFilepath, dstFilepath, format string) error {
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func longhornCli() {
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
if err = pprof.StartCPUProfile(f); err != nil {
logrus.Fatal(err)
}
defer pprof.StopCPUProfile()
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,9 @@ func (c *Controller) monitoring(address string, backend types.Backend) {
err := <-monitorChan
if err != nil {
logrus.WithError(err).Errorf("Backend %v monitoring failed, mark as ERR", address)
c.SetReplicaMode(address, types.ERR)
if err = c.SetReplicaMode(address, types.ERR); err != nil {
logrus.WithError(err).Errorf("Failed to set replica %v to ERR", address)
}
}
logrus.Infof("Monitoring stopped %v", address)
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/dataconn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ func (c *Client) nextSeq() uint32 {
}

func (c *Client) replyError(req *Message, err error) {
journal.RemovePendingOp(req.ID, false)
if opErr := journal.RemovePendingOp(req.ID, false); opErr != nil {
logrus.WithError(opErr).WithFields(logrus.Fields{
"seq": req.Seq,
"id": req.ID,
}).Error("Error removing pending operation")
}
delete(c.messages, req.Seq)
req.Type = TypeError
req.Data = []byte(err.Error())
Expand All @@ -226,7 +231,13 @@ func (c *Client) handleRequest(req *Message) {

func (c *Client) handleResponse(resp *Message) {
if req, ok := c.messages[resp.Seq]; ok {
journal.RemovePendingOp(req.ID, true)
err := journal.RemovePendingOp(req.ID, true)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"seq": resp.Seq,
"id": req.ID,
}).Error("Error removing pending operation")
}
delete(c.messages, resp.Seq)
req.Type = resp.Type
req.Size = resp.Size
Expand Down
4 changes: 3 additions & 1 deletion pkg/dataconn/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ func (s *Server) write() {
Type: TypeClose,
}
//Best effort to notify client to close connection
s.wire.Write(msg)
if err := s.wire.Write(msg); err != nil {
logrus.WithError(err).Error("Failed to write")
}
}
}
}
5 changes: 3 additions & 2 deletions pkg/frontend/rest/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ func (d *Device) start() error {

log.Infof("Rest Frontend listening on %s", listen)

var err error
go func() {
http.ListenAndServe(listen, router)
err = http.ListenAndServe(listen, router)
}()
return nil
return err
}

func (d *Device) stop() error {
Expand Down
7 changes: 5 additions & 2 deletions pkg/frontend/socket/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ func (t *Socket) startSocketServer(rwu types.ReaderWriterUnmapperAt) error {
}
}

var err error
t.socketPath = socketPath
go t.startSocketServerListen(rwu)
return nil
go func() {
err = t.startSocketServerListen(rwu)
}()
return err
}

func (t *Socket) startSocketServerListen(rwu types.ReaderWriterUnmapperAt) error {
Expand Down
5 changes: 1 addition & 4 deletions pkg/replica/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ func (rb *BackupStatus) HasSnapshot(snapID, volumeID string) bool {
}
id := diskutil.GenerateSnapshotDiskName(snapID)
to := rb.findIndex(id)
if to < 0 {
return false
}
return true
return to >= 0
}

func (rb *BackupStatus) OpenSnapshot(snapID, volumeID string) error {
Expand Down
2 changes: 2 additions & 0 deletions pkg/replica/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (s *TestSuite) testBackupWithBackups(c *C, backingFile *backingfile.Backing

// Test 003 -> 002
err = rb.OpenSnapshot(snap3, volume)
c.Assert(err, IsNil)
mappings, err = rb.CompareSnapshot(snap3, snap2, volume)
c.Assert(err, IsNil)
c.Assert(len(mappings.Mappings), Equals, 2)
Expand All @@ -182,6 +183,7 @@ func (s *TestSuite) testBackupWithBackups(c *C, backingFile *backingfile.Backing
c.Assert(mappings.Mappings[1].Offset, Equals, int64(8*mb))
c.Assert(mappings.Mappings[1].Size, Equals, int64(2*mb))
err = rb.CloseSnapshot(snap3, volume)
c.Assert(err, IsNil)

// Test 002 -> 001
err = rb.OpenSnapshot(snap2, volume)
Expand Down
1 change: 0 additions & 1 deletion pkg/replica/diff_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (d *diffDisk) Expand(size int64) {

d.location = append(d.location, make([]byte, newLocationSize-len(d.location))...)
d.size = size
return
}

func (d *diffDisk) WriteAt(buf []byte, offset int64) (int, error) {
Expand Down
30 changes: 22 additions & 8 deletions pkg/replica/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ type SnapshotHashJob struct {
SnapshotName string
Rehash bool

file *os.File

SnapshotHashStatus
}

Expand Down Expand Up @@ -95,7 +93,12 @@ func (t *SnapshotHashJob) LockFile() (fileLock *flock.Flock, err error) {
}

func (t *SnapshotHashJob) UnlockFile(fileLock *flock.Flock) {
fileLock.Unlock()
if err := fileLock.Unlock(); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"snapshot": t.SnapshotName,
"file": fileLock.Path(),
}).Error("Failed to unlock the file lock")
}
}

func (t *SnapshotHashJob) Execute() (err error) {
Expand All @@ -120,23 +123,34 @@ func (t *SnapshotHashJob) Execute() (err error) {
return
}

SetSnapshotHashInfoToChecksumFile(t.SnapshotName, &xattrType.SnapshotHashInfo{
if err = SetSnapshotHashInfoToChecksumFile(t.SnapshotName, &xattrType.SnapshotHashInfo{
Method: defaultHashMethod,
Checksum: checksum,
ChangeTime: changeTime,
LastHashedAt: lastHashedAt,
SilentlyCorrupted: silentlyCorrupted,
})
}); err != nil {
logrus.WithError(err).Errorf("failed to set snapshot %s hash info to checksum file", t.SnapshotName)
t.State = ProgressStateError
t.Error = err.Error()
return
}

remain, err := t.isChangeTimeRemain(changeTime)
var remain bool
remain, err = t.isChangeTimeRemain(changeTime)
if !remain {
if err == nil {
err = fmt.Errorf("snapshot %v modification time is changed", t.SnapshotName)
}
// Do the best to delete the useless checksum file.
// The deletion failure is acceptable, because the mismatching timestamps
// will trigger the rehash in the next hash request.
DeleteSnapshotHashInfoChecksumFile(t.SnapshotName)
if deleteErr := DeleteSnapshotHashInfoChecksumFile(t.SnapshotName); deleteErr != nil {
logrus.WithError(deleteErr).Errorf("failed to delete snapshot %v hash info checksum file", t.SnapshotName)
t.State = ProgressStateError
t.Error = deleteErr.Error()
return
}
}
}

Expand Down Expand Up @@ -172,8 +186,8 @@ func (t *SnapshotHashJob) Execute() (err error) {
return nil
}

requireRehash := true
if !t.Rehash {
var requireRehash bool
requireRehash, checksum, err = t.isRehashRequired(changeTime)
if err != nil {
return err
Expand Down
14 changes: 10 additions & 4 deletions pkg/replica/replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,12 +870,16 @@ func (r *Replica) revertDisk(parentDiskFileName, created string) (*Replica, erro
info.Parent = newHeadDisk.Parent

if _, err := r.encodeToFile(&info, volumeMetaData); err != nil {
r.encodeToFile(&r.info, volumeMetaData)
if _, err = r.encodeToFile(&r.info, volumeMetaData); err != nil {
return nil, err
}
return nil, err
}

// Need to execute before r.Reload() to update r.diskChildrenMap
r.rmDisk(oldHead)
if err = r.rmDisk(oldHead); err != nil {
return nil, err
}

rNew, err := r.Reload()
if err != nil {
Expand Down Expand Up @@ -911,7 +915,7 @@ func (r *Replica) createDisk(name string, userCreated bool, created string, labe
rollbackFuncList := []func() error{}
defer func() {
if err == nil {
r.rmDisk(oldHead)
err = r.rmDisk(oldHead)
return
}

Expand Down Expand Up @@ -1176,7 +1180,9 @@ func (r *Replica) Delete() error {

for name := range r.diskData {
if name != r.info.BackingFilePath {
r.rmDisk(name)
if err := r.rmDisk(name); err != nil {
return err
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/replica/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,11 +833,13 @@ func (s *TestSuite) TestSnapshotReadWrite(c *C) {
fill(buf[b:2*b], 2)
count, err = r.WriteAt(buf[b:2*b], b)
c.Assert(count, Equals, b)
c.Assert(err, IsNil)
err = r.Snapshot("001", true, getNow(), nil)
c.Assert(err, IsNil)

fill(buf[:b], 1)
count, err = r.WriteAt(buf[:b], 0)
c.Assert(err, IsNil)
c.Assert(count, Equals, b)
err = r.Snapshot("002", true, getNow(), nil)
c.Assert(err, IsNil)
Expand Down
13 changes: 2 additions & 11 deletions pkg/replica/rest/replica.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package rest

import (
"net/http"

"github.com/rancher/go-rancher/api"
"github.com/rancher/go-rancher/client"
"net/http"
)

func (s *Server) ListReplicas(rw http.ResponseWriter, req *http.Request) error {
Expand All @@ -19,13 +20,3 @@ func (s *Server) Replica(apiContext *api.ApiContext) *Replica {
state, info := s.s.Status()
return NewReplica(apiContext, state, info, s.s.Replica())
}

func (s *Server) doOp(req *http.Request, err error) error {
if err != nil {
return err
}

apiContext := api.GetApiContext(req)
apiContext.Write(s.Replica(apiContext))
return nil
}
5 changes: 4 additions & 1 deletion pkg/replica/rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gorilla/mux"
"github.com/rancher/go-rancher/api"
"github.com/rancher/go-rancher/client"
"github.com/sirupsen/logrus"

// add pprof endpoint
_ "net/http/pprof"
Expand Down Expand Up @@ -37,7 +38,9 @@ func NewRouter(s *Server) *mux.Router {
f := HandleError

router.Methods("GET").Path("/ping").Handler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("pong"))
if _, err := rw.Write([]byte("pong")); err != nil {
logrus.WithError(err).Error("Failed to write response")
}
}))

// API framework routes
Expand Down
8 changes: 6 additions & 2 deletions pkg/replica/rpc/dataserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ func (s *DataServer) listenAndServeTCP() error {

go func(conn net.Conn) {
server := dataconn.NewServer(conn, s.s)
server.Handle()
if err = server.Handle(); err != nil {
logrus.WithError(err).Error("failed to handle data server")
}
}(conn)
}
}
Expand All @@ -83,7 +85,9 @@ func (s *DataServer) listenAndServeUNIX() error {
logrus.Infof("New connection from: %v", conn.RemoteAddr())
go func(conn net.Conn) {
server := dataconn.NewServer(conn, s.s)
server.Handle()
if err = server.Handle(); err != nil {
logrus.WithError(err).Error("failed to handle data server")
}
}(conn)
}
}
2 changes: 0 additions & 2 deletions pkg/replica/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ func (s *Server) SetUnmapMarkDiskChainRemoved(enabled bool) {
if s.r != nil {
s.r.SetUnmapMarkDiskChainRemoved(enabled)
}

return
}

func (s *Server) SetSnapshotMaxCount(count int) {
Expand Down
11 changes: 7 additions & 4 deletions pkg/sync/rpc/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ func (s *TestSuite) TestSnapshotHashListCRUD(c *C) {
snapshotName := "snapshot0"
ctx, cancel := context.WithCancel(context.Background())
task := replica.NewSnapshotHashJob(ctx, cancel, snapshotName, false)
list.Add(snapshotName, task)
err := list.Add(snapshotName, task)
c.Assert(err, IsNil)

_, err := list.Get(snapshotName)
_, err = list.Get(snapshotName)
c.Assert(err, IsNil)

_, err = list.Get("nonexistence")
Expand All @@ -49,7 +50,8 @@ func (s *TestSuite) TestSnapshotHashListRefreshTriggerByAdd(c *C) {
task := replica.NewSnapshotHashJob(ctx, cancel, snapshotName, false)
task.State = replica.ProgressStateComplete

list.Add(snapshotName, task)
err := list.Add(snapshotName, task)
c.Assert(err, IsNil)

size := list.GetSize()
if i < MaxSnapshotHashJobSize {
Expand All @@ -68,7 +70,8 @@ func (s *TestSuite) TestSnapshotHashListRefreshTriggerByGet(c *C) {
snapshotName := "snapshot" + strconv.Itoa(i)
ctx, cancel := context.WithCancel(context.Background())
task := replica.NewSnapshotHashJob(ctx, cancel, snapshotName, false)
list.Add(snapshotName, task)
err := list.Add(snapshotName, task)
c.Assert(err, IsNil)
}

for i := 0; i < numSnapshots; i++ {
Expand Down
Loading

0 comments on commit 55d34d1

Please sign in to comment.