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 <[email protected]>
  • Loading branch information
FrankYang0529 committed Apr 26, 2024
1 parent e41df5d commit 4a70cf8
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 115 deletions.
6 changes: 3 additions & 3 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ RUN apt-get update && \
rm -f /bin/sh && ln -s /bin/bash /bin/sh

ENV DOCKER_URL_amd64=https://get.docker.com/builds/Linux/x86_64/docker-1.10.3 \
DOCKER_URL_arm=https://github.com/rancher/docker/releases/download/v1.10.3-ros1/docker-1.10.3_arm \
DOCKER_URL_arm64=https://github.com/rancher/docker/releases/download/v1.10.3-ros1/docker-1.10.3_arm \
DOCKER_URL=DOCKER_URL_${ARCH}

RUN wget -O - ${!DOCKER_URL} > /usr/bin/docker && chmod +x /usr/bin/docker

ENV GOLANG_ARCH_amd64=amd64 GOLANG_ARCH_arm=armv6l GOLANG_ARCH=GOLANG_ARCH_${ARCH} \
ENV GOLANG_ARCH_amd64=amd64 GOLANG_ARCH_arm64=arm64 GOLANG_ARCH=GOLANG_ARCH_${ARCH} \
GOPATH=/go PATH=/go/bin:/usr/local/go/bin:${PATH} SHELL=/bin/bash

RUN wget -O - https://storage.googleapis.com/golang/go1.17.10.linux-${!GOLANG_ARCH}.tar.gz | tar -xzf - -C /usr/local
RUN wget -O - https://storage.googleapis.com/golang/go1.21.3.linux-${!GOLANG_ARCH}.tar.gz | tar -xzf - -C /usr/local
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2

ENV DAPPER_SOURCE /go/src/github.com/longhorn/backupstore
Expand Down
38 changes: 31 additions & 7 deletions backupbackingimage/backupbackingimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ func CreateBackingImageBackup(config *BackupConfig, backupBackingImage *BackupBa
return err
}

defer lock.Unlock()
if err := lock.Lock(); err != nil {
return err
}
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
log.WithError(unlockErr).Warn("Failed to unlock backup backing image")
}
}()

exists, err := addBackingImageConfigInBackupStore(bsDriver, backupBackingImage)
if err != nil {
Expand All @@ -123,7 +127,11 @@ func CreateBackingImageBackup(config *BackupConfig, backupBackingImage *BackupBa

go func() {
defer backupOperation.CloseFile()
defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
log.WithError(unlockErr).Warn("Failed to unlock backup backing image")
}
}()

backupOperation.UpdateBackupProgress(string(common.ProgressStateInProgress), 0, "", "")

Expand Down Expand Up @@ -322,10 +330,14 @@ func RestoreBackingImageBackup(config *RestoreConfig, restoreOperation RestoreOp
return err
}

defer lock.Unlock()
if err := lock.Lock(); err != nil {
return err
}
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock restore")
}
}()

backupBackingImage, err := loadBackingImageConfigInBackupStore(bsDriver, backingImageName)
if err != nil {
Expand Down Expand Up @@ -356,8 +368,16 @@ func RestoreBackingImageBackup(config *RestoreConfig, restoreOperation RestoreOp
}

go func() {
defer backingImageFile.Close()
defer lock.Unlock()
defer func() {
if closeErr := backingImageFile.Close(); closeErr != nil {
logrus.WithError(closeErr).Warn("Failed to close backing image file")
}
}()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock restore")
}
}()

progress := &common.Progress{
TotalBlockCounts: int64(len(backupBackingImage.Blocks)),
Expand Down Expand Up @@ -416,7 +436,7 @@ func checkBackingImageFile(backingImageFilePath string, backupBackingImage *Back
// We want to truncate regular files, but not device
if stat.Mode()&os.ModeType == 0 {
if err := backingImageFile.Truncate(backupBackingImage.Size); err != nil {
errors.Wrapf(err, "failed to truncate backing image")
err = errors.Wrapf(err, "failed to truncate backing image")
return nil, err
}
}
Expand Down Expand Up @@ -519,7 +539,11 @@ func RemoveBackingImageBackup(backupURL string) (err error) {
if err := lock.Lock(); err != nil {
return err
}
defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock restore")
}
}()

// If we fail to load the backup we still want to proceed with the deletion of the backup file
backupBackingImage, err := loadBackingImageConfigInBackupStore(bsDriver, backingImageName)
Expand Down
2 changes: 0 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func getVolumeNames(jobQueues *workerpool.WorkerPool, driver BackupStoreDriver)
Payload: lv2Paths,
Err: nil,
}
return
})
}

Expand Down Expand Up @@ -258,7 +257,6 @@ func getVolumeNames(jobQueues *workerpool.WorkerPool, driver BackupStoreDriver)
Payload: volumeNames,
Err: nil,
}
return
})
}
}
Expand Down
92 changes: 71 additions & 21 deletions deltablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ func CreateDeltaBlockBackup(backupName string, config *DeltaBackupConfig) (isInc
defer func() {
if err != nil {
log.WithError(err).Error("Failed to create delta block backup")
deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateError), 0, "", err.Error())
if updateErr := deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateError), 0, "", err.Error()); updateErr != nil {
log.WithError(updateErr).Error("Failed to update backup status")
}
}
}()

Expand All @@ -152,7 +154,11 @@ func CreateDeltaBlockBackup(backupName string, config *DeltaBackupConfig) (isInc
return false, err
}

defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock")
}
}()
if err := lock.Lock(); err != nil {
return false, err
}
Expand Down Expand Up @@ -218,13 +224,17 @@ func CreateDeltaBlockBackup(backupName string, config *DeltaBackupConfig) (isInc

delta, err := deltaOps.CompareSnapshot(snapshot.Name, backupRequest.getLastSnapshotName(), volume.Name)
if err != nil {
deltaOps.CloseSnapshot(snapshot.Name, volume.Name)
if closeErr := deltaOps.CloseSnapshot(snapshot.Name, volume.Name); closeErr != nil {
err = errors.Wrapf(err, "during handling err %+v, close snapshot returns err %+v", err, closeErr)
}
return backupRequest.isIncrementalBackup(), err
}
if delta.BlockSize != DEFAULT_BLOCK_SIZE {
deltaOps.CloseSnapshot(snapshot.Name, volume.Name)
return backupRequest.isIncrementalBackup(),
fmt.Errorf("driver doesn't support block sizes other than %v", DEFAULT_BLOCK_SIZE)
err = fmt.Errorf("driver doesn't support block sizes other than %v", DEFAULT_BLOCK_SIZE)
if closeErr := deltaOps.CloseSnapshot(snapshot.Name, volume.Name); closeErr != nil {
err = errors.Wrapf(err, "during handling err %+v, close snapshot returns err %+v", err, closeErr)
}
return backupRequest.isIncrementalBackup(), err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonComplete,
Expand Down Expand Up @@ -258,22 +268,38 @@ func CreateDeltaBlockBackup(backupName string, config *DeltaBackupConfig) (isInc

// keep lock alive for async go routine.
if err := lock.Lock(); err != nil {
deltaOps.CloseSnapshot(snapshot.Name, volume.Name)
if closeErr := deltaOps.CloseSnapshot(snapshot.Name, volume.Name); closeErr != nil {
err = errors.Wrapf(err, "during handling err %+v, close snapshot returns err %+v", err, closeErr)
}
return backupRequest.isIncrementalBackup(), err
}
go func() {
defer deltaOps.CloseSnapshot(snapshot.Name, volume.Name)
defer lock.Unlock()
defer func() {
if closeErr := deltaOps.CloseSnapshot(snapshot.Name, volume.Name); closeErr != nil {
logrus.WithError(closeErr).Error("Failed to close snapshot")
}
}()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock")
}
}()

deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), 0, "", "")
if updateErr := deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), 0, "", ""); updateErr != nil {
logrus.WithError(updateErr).Error("Failed to update backup status")
}

log.Info("Performing delta block backup")

if progress, backup, err := performBackup(bsDriver, config, delta, deltaBackup, backupRequest.lastBackup); err != nil {
logrus.WithError(err).Errorf("Failed to perform backup for volume %v snapshot %v", volume.Name, snapshot.Name)
deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), progress, "", err.Error())
if updateErr := deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), progress, "", err.Error()); updateErr != nil {
logrus.WithError(updateErr).Error("Failed to update backup status")
}
} else {
deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), progress, backup, "")
if updateErr := deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), progress, backup, ""); updateErr != nil {
logrus.WithError(updateErr).Error("Failed to update backup status")
}
}
}()
return backupRequest.isIncrementalBackup(), nil
Expand Down Expand Up @@ -369,7 +395,9 @@ func backupBlock(bsDriver BackupStoreDriver, config *DeltaBackupConfig,
deltaBackup.Lock()
defer deltaBackup.Unlock()
updateBlocksAndProgress(deltaBackup, progress, checksum, newBlock)
deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), progress.progress, "", "")
if updateErr := deltaOps.UpdateBackupStatus(snapshot.Name, volume.Name, string(types.ProgressStateInProgress), progress.progress, "", ""); updateErr != nil {
logrus.WithError(updateErr).Error("Failed to update backup status")
}
}()

blkFile := getBlockFilePath(volume.Name, checksum)
Expand Down Expand Up @@ -640,7 +668,11 @@ func RestoreDeltaBlockBackup(ctx context.Context, config *DeltaRestoreConfig) er
return err
}

defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock")
}
}()
if err := lock.Lock(); err != nil {
return err
}
Expand Down Expand Up @@ -699,7 +731,9 @@ func RestoreDeltaBlockBackup(ctx context.Context, config *DeltaRestoreConfig) er
defer func() {
_ = deltaOps.CloseVolumeDev(volDev)
deltaOps.UpdateRestoreStatus(volDevName, currentProgress, err)
lock.Unlock()
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock")
}
}()

progress := &progress{
Expand Down Expand Up @@ -791,7 +825,11 @@ func RestoreDeltaBlockBackupIncrementally(ctx context.Context, config *DeltaRest
if err := lock.Lock(); err != nil {
return err
}
defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock")
}
}()

vol, err := loadVolume(bsDriver, srcVolumeName)
if err != nil {
Expand Down Expand Up @@ -858,7 +896,11 @@ func RestoreDeltaBlockBackupIncrementally(ctx context.Context, config *DeltaRest
}
go func() {
defer volDev.Close()
defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(err).Warn("Failed to unlock")
}
}()

// This pre-truncate is to ensure the XFS speculatively
// preallocates post-EOF blocks get reclaimed when volDev is
Expand Down Expand Up @@ -1079,7 +1121,11 @@ func DeleteBackupVolume(volumeName string, destURL string) error {
if err := lock.Lock(); err != nil {
return err
}
defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock")
}
}()
return removeVolume(volumeName, bsDriver)
}

Expand All @@ -1100,7 +1146,7 @@ func checkBlockReferenceCount(blockInfos map[string]*BlockInfo, backup *Backup,
func getLatestBackup(backup *Backup, lastBackup *Backup) error {
if lastBackup.SnapshotCreatedAt == "" {
// FIXME - go lint points out that this copies a potentially locked sync.mutex
*lastBackup = *backup
*lastBackup = *backup // nolint:govet
return nil
}

Expand All @@ -1116,7 +1162,7 @@ func getLatestBackup(backup *Backup, lastBackup *Backup) error {

if backupTime.After(lastBackupTime) {
// FIXME - go lint points out that this copies a potentially locked sync.mutex
*lastBackup = *backup
*lastBackup = *backup // nolint:govet
}

return nil
Expand Down Expand Up @@ -1144,7 +1190,11 @@ func DeleteDeltaBlockBackup(backupURL string) error {
if err := lock.Lock(); err != nil {
return err
}
defer lock.Unlock()
defer func() {
if unlockErr := lock.Unlock(); unlockErr != nil {
logrus.WithError(unlockErr).Warn("Failed to unlock")
}
}()

// If we fail to load the backup we still want to proceed with the deletion of the backup file
backupToBeDeleted, err := loadBackup(bsDriver, backupName, volumeName)
Expand Down
4 changes: 3 additions & 1 deletion fsops/fsops.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func (f *FileSystemOperator) List(path string) ([]string, error) {
func (f *FileSystemOperator) Upload(src, dst string) error {
tmpDst := dst + ".tmp" + "." + strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
if f.FileExists(tmpDst) {
f.Remove(tmpDst)
if err := f.Remove(tmpDst); err != nil {
return err
}
}
if err := f.preparePath(dst); err != nil {
return err
Expand Down
Loading

0 comments on commit 4a70cf8

Please sign in to comment.