diff --git a/mock/plugin/lock.go b/mock/plugin/lock.go index 260ea9dd..9f5cb130 100644 --- a/mock/plugin/lock.go +++ b/mock/plugin/lock.go @@ -33,12 +33,11 @@ func (m *MockLocker) EXPECT() *MockLockerMockRecorder { } // Lock mocks base method -func (m *MockLocker) Lock(arg0 string) (bool, error) { +func (m *MockLocker) Lock(arg0 string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Lock", arg0) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret0, _ := ret[0].(error) + return ret0 } // Lock indicates an expected call of Lock @@ -48,12 +47,11 @@ func (mr *MockLockerMockRecorder) Lock(arg0 interface{}) *gomock.Call { } // LockWithExpireTime mocks base method -func (m *MockLocker) LockWithExpireTime(arg0 string, arg1 int64) (bool, error) { +func (m *MockLocker) LockWithExpireTime(arg0 string, arg1 int64) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "LockWithExpireTime", arg0, arg1) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret0, _ := ret[0].(error) + return ret0 } // LockWithExpireTime indicates an expected call of LockWithExpireTime @@ -62,17 +60,16 @@ func (mr *MockLockerMockRecorder) LockWithExpireTime(arg0, arg1 interface{}) *go return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LockWithExpireTime", reflect.TypeOf((*MockLocker)(nil).LockWithExpireTime), arg0, arg1) } -// ReleaseLock mocks base method -func (m *MockLocker) ReleaseLock(arg0 string) (bool, error) { +// UnLock mocks base method +func (m *MockLocker) UnLock(arg0 string) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ReleaseLock", arg0) - ret0, _ := ret[0].(bool) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret := m.ctrl.Call(m, "UnLock", arg0) + ret0, _ := ret[0].(error) + return ret0 } -// ReleaseLock indicates an expected call of ReleaseLock -func (mr *MockLockerMockRecorder) ReleaseLock(arg0 interface{}) *gomock.Call { +// UnLock indicates an expected call of UnLock +func (mr *MockLockerMockRecorder) UnLock(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReleaseLock", reflect.TypeOf((*MockLocker)(nil).ReleaseLock), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnLock", reflect.TypeOf((*MockLocker)(nil).UnLock), arg0) } diff --git a/plugin/default/lock/lock.go b/plugin/default/lock/lock.go index b75d872d..3ec63374 100644 --- a/plugin/default/lock/lock.go +++ b/plugin/default/lock/lock.go @@ -36,27 +36,28 @@ func New() (plugin.Plugin, error) { } // Lock lock the resource DefaultExpireTime seconds -func (l *taskLocker) Lock(name string) (bool, error) { +func (l *taskLocker) Lock(name string) error { return l.LockWithExpireTime(name, DefaultExpireTime) } // LockWithExpireTime lock the resource expireTime seconds -func (l *taskLocker) LockWithExpireTime(name string, expireTime int64) (bool, error) { +func (l *taskLocker) LockWithExpireTime(name string, expireTime int64) error { t, err := l.task.GetTask(name) if err != nil { - return false, err + return err } if t == nil { - return false, nil + return nil } t.ExpireTime = expireTime - return l.task.AcquireTaskLock(t) + _, err = l.task.AcquireTaskLock(t) + return err } -// ReleaseLook releaseLock -func (l *taskLocker) ReleaseLock(name string) (bool, error) { +// Unlock unlock +func (l *taskLocker) Unlock(name string) error { return l.LockWithExpireTime(name, 0) } diff --git a/plugin/default/lock/lock_test.go b/plugin/default/lock/lock_test.go index f61547e9..a99cab20 100644 --- a/plugin/default/lock/lock_test.go +++ b/plugin/default/lock/lock_test.go @@ -26,10 +26,8 @@ func TestTaskLocker_Lock(t *testing.T) { mockTask.EXPECT().AcquireTaskLock(task).Return(true, nil) mockTask.EXPECT().GetTask(task.Name).Return(task, nil) - res, err := locker.Lock(task.Name) + err := locker.Lock(task.Name) assert.NoError(t, err) - assert.Equal(t, true, res) - } func TestTaskLocker_LockWithExpireTime(t *testing.T) { @@ -48,19 +46,18 @@ func TestTaskLocker_LockWithExpireTime(t *testing.T) { mockTask.EXPECT().GetTask(task.Name).Return(task, nil) mockTask.EXPECT().AcquireTaskLock(task).Return(false, fmt.Errorf("lock error")) - _, err := locker.LockWithExpireTime(task.Name, 10) + err := locker.LockWithExpireTime(task.Name, 10) assert.Error(t, err) assert.Equal(t, "lock error", err.Error()) mockTask.EXPECT().GetTask(task.Name).Return(task, fmt.Errorf("get task error")) - _, err = locker.LockWithExpireTime(task.Name, 10) + err = locker.LockWithExpireTime(task.Name, 10) assert.Error(t, err) assert.Equal(t, "get task error", err.Error()) mockTask.EXPECT().GetTask(task.Name).Return(nil, nil) - res, err := locker.LockWithExpireTime(task.Name, 10) + err = locker.LockWithExpireTime(task.Name, 10) assert.NoError(t, err) - assert.False(t, res) } func TestTaskLocker_ReleaseLock(t *testing.T) { @@ -79,10 +76,9 @@ func TestTaskLocker_ReleaseLock(t *testing.T) { mockTask.EXPECT().GetTask(task.Name).Return(task, nil) mockTask.EXPECT().AcquireTaskLock(task).Return(false, fmt.Errorf("failed")) - res, err := locker.ReleaseLock(task.Name) + err := locker.Unlock(task.Name) assert.Error(t, err) assert.Equal(t, "failed", err.Error()) - assert.False(t, res) } func TestTaskLocker_Close(t *testing.T) { diff --git a/plugin/lock.go b/plugin/lock.go index 6817e4cc..e5ce2289 100644 --- a/plugin/lock.go +++ b/plugin/lock.go @@ -5,28 +5,25 @@ package plugin // Locker - the lock manager for baetyl cloud type Locker interface { - // Lock lock the resource + // Lock lock the resource, Lock should be paired with Unlock. // PARAMS: // - name: the lock's name // RETURNS: - // true: if locked success // error: if has error else nil - Lock(name string) (bool, error) + Lock(name string) error - // LockWithExpireTime lock the resouce with expire time + // LockWithExpireTime lock the resource with expire time // PARAMS: // - name: the lock's name // - expireTime(seconds): the expire time of the lock, if acquired the lock // RETURNS: - // true: if locked success // error: if has error else nil - LockWithExpireTime(name string, expireTime int64) (bool, error) + LockWithExpireTime(name string, expireTime int64) error - // ReleaseLock release the lock by name + // Unlock release the lock by name // PARAMS: // - name: the lock's name // RETURNS: - // true: if lock is released // error: if has error else nil - ReleaseLock(name string) (bool, error) + Unlock(name string) error } diff --git a/task/task.go b/task/task.go index 1703cdd8..e374889f 100644 --- a/task/task.go +++ b/task/task.go @@ -89,8 +89,8 @@ func (m *TaskManager) RunTasks() { func (m *TaskManager) runTask(task *models.Task) { defer func() { <-m.concurrency }() - lock, err := m.lock.LockWithExpireTime(task.Name, m.config.Lock.ExpireTime) - if err != nil || !lock { + err := m.lock.LockWithExpireTime(task.Name, m.config.Lock.ExpireTime) + if err != nil { log.L().Error("get lock error", log.Any("name", task.Name), log.Any("namespace", task.Namespace),