Skip to content

Commit

Permalink
Modify lock interface. (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannatao authored Mar 16, 2021
1 parent 2aaad72 commit f95c6e0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 44 deletions.
31 changes: 14 additions & 17 deletions mock/plugin/lock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions plugin/default/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 5 additions & 9 deletions plugin/default/lock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
15 changes: 6 additions & 9 deletions plugin/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit f95c6e0

Please sign in to comment.