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

fix: refine lock related error message #189

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
const RESTORE_LOCK LockType = 1
const DELETION_LOCK LockType = 2

type Operation string

const BackupOperationCreateRestore Operation = "create/restore"
const BackupOperationDelete Operation = "delete"
const BackupOperationUndefined Operation = "undefined"

type FileLock struct {
Name string
Type LockType
Expand Down Expand Up @@ -80,6 +86,14 @@
func (lock *FileLock) Lock() error {
lock.mutex.Lock()
defer lock.mutex.Unlock()

operation := BackupOperationUndefined
if lock.Type == BACKUP_LOCK || lock.Type == RESTORE_LOCK {
operation = BackupOperationCreateRestore
} else if lock.Type == DELETION_LOCK {
operation = BackupOperationDelete
}

if lock.Acquired {
atomic.AddInt32(&lock.count, 1)
_ = saveLock(lock)
Expand Down Expand Up @@ -107,16 +121,16 @@
if !lock.canAcquire() {
file := getLockFilePath(lock.volume, lock.Name)
_ = removeLock(lock)
return fmt.Errorf("failed lock %v type %v acquisition", file, lock.Type)
return fmt.Errorf("failed to acquire lock %v when performing backup %v, please try again later.", file, operation)
}

file := getLockFilePath(lock.volume, lock.Name)
log.Infof("Acquired lock %v type %v on backupstore", file, lock.Type)
log.Infof("Acquired lock %v for backup %v on backupstore", file, operation)
derekbit marked this conversation as resolved.
Show resolved Hide resolved
lock.Acquired = true
atomic.AddInt32(&lock.count, 1)
if err := saveLock(lock); err != nil {
_ = removeLock(lock)
return errors.Wrapf(err, "failed to store updated lock %v type %v after acquisition", file, lock.Type)
return errors.Wrapf(err, "failed to store updated lock %v when performing backup %v, please try again later", file, operation)
}

// enable lock refresh
Expand All @@ -133,7 +147,7 @@
if lock.Acquired {
if err := saveLock(lock); err != nil {
// nothing we can do here, that's why the lock acquisition time is 2x lock refresh interval
log.WithError(err).Warnf("Failed to refresh acquired lock %v type %v", file, lock.Type)
log.WithError(err).Warnf("Failed to refresh acquired lock %v when performing backup %v, please try again later", file, operation)
}
}
lock.mutex.Unlock()
Expand Down Expand Up @@ -199,7 +213,7 @@
return strings.Compare(a.Name, b.Name)
} else if a.serverTime.UTC().Before(b.serverTime.UTC()) {
return -1
} else {

Check notice on line 216 in lock.go

View check run for this annotation

codefactor.io / CodeFactor

lock.go#L216

If block ends with a return statement, so drop this else and outdent its block. (indent-error-flow)
return 1
}
} else if a.Acquired {
Expand Down