From f180bd1e742ac0f43c25edd6fc21749735e014a0 Mon Sep 17 00:00:00 2001 From: zzy987 <67889264+zzy987@users.noreply.github.com> Date: Tue, 24 Aug 2021 11:07:35 +0800 Subject: [PATCH] Add a function to check task existence in cdn (#570) * fix: replace get with exist in gc Signed-off-by: zzy987 <67889264+zzy987@users.noreply.github.com> * fix: no error in the if branch Signed-off-by: zzy987 <67889264+zzy987@users.noreply.github.com> * fix: move accessTimeMap.Add from getTask into Get Signed-off-by: zzy987 <67889264+zzy987@users.noreply.github.com> * fix: change return value Signed-off-by: zzy987 <67889264+zzy987@users.noreply.github.com> * fix: remove log error for Exist don't return error Signed-off-by: zzy987 <67889264+zzy987@users.noreply.github.com> * chore: use mockgen Signed-off-by: zzy987 <67889264+zzy987@users.noreply.github.com> * chore: merge submodule Signed-off-by: zzy987 <67889264+zzy987@users.noreply.github.com> --- cdnsystem/supervisor/cdn/storage/disk/disk.go | 5 +---- cdnsystem/supervisor/mock/mock_task_mgr.go | 14 ++++++++++++++ cdnsystem/supervisor/task/manager.go | 16 +++++++++++----- cdnsystem/supervisor/task_mgr.go | 3 +++ 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/cdnsystem/supervisor/cdn/storage/disk/disk.go b/cdnsystem/supervisor/cdn/storage/disk/disk.go index c35377c4bbb..f058c5c8bf9 100644 --- a/cdnsystem/supervisor/cdn/storage/disk/disk.go +++ b/cdnsystem/supervisor/cdn/storage/disk/disk.go @@ -131,10 +131,7 @@ func (s *diskStorageMgr) GC() error { for _, taskID := range gcTaskIDs { synclock.Lock(taskID, false) // try to ensure the taskID is not using again - if _, err := s.taskMgr.Get(taskID); err == nil || !cdnerrors.IsDataNotFound(err) { - if err != nil { - logger.GcLogger.With("type", "disk").Errorf("failed to get taskID(%s): %v", taskID, err) - } + if s.taskMgr.Exist(taskID) { synclock.UnLock(taskID, false) continue } diff --git a/cdnsystem/supervisor/mock/mock_task_mgr.go b/cdnsystem/supervisor/mock/mock_task_mgr.go index caf229d7377..ab4517797e0 100644 --- a/cdnsystem/supervisor/mock/mock_task_mgr.go +++ b/cdnsystem/supervisor/mock/mock_task_mgr.go @@ -50,6 +50,20 @@ func (mr *MockSeedTaskMgrMockRecorder) Delete(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockSeedTaskMgr)(nil).Delete), arg0) } +// Exist mocks base method. +func (m *MockSeedTaskMgr) Exist(arg0 string) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Exist", arg0) + ret0, _ := ret[0].(bool) + return ret0 +} + +// Exist indicates an expected call of Exist. +func (mr *MockSeedTaskMgrMockRecorder) Exist(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Exist", reflect.TypeOf((*MockSeedTaskMgr)(nil).Exist), arg0) +} + // Get mocks base method. func (m *MockSeedTaskMgr) Get(arg0 string) (*types.SeedTask, error) { m.ctrl.T.Helper() diff --git a/cdnsystem/supervisor/task/manager.go b/cdnsystem/supervisor/task/manager.go index 60c17f7781f..538feb95284 100644 --- a/cdnsystem/supervisor/task/manager.go +++ b/cdnsystem/supervisor/task/manager.go @@ -165,10 +165,6 @@ func (tm *Manager) getTask(taskID string) (*types.SeedTask, error) { } return nil, err } - // update accessTime for taskID - if err := tm.accessTimeMap.Add(taskID, time.Now()); err != nil { - logger.WithTaskID(taskID).Warnf("failed to update accessTime: %v", err) - } // type assertion if info, ok := v.(*types.SeedTask); ok { return info, nil @@ -177,7 +173,17 @@ func (tm *Manager) getTask(taskID string) (*types.SeedTask, error) { } func (tm Manager) Get(taskID string) (*types.SeedTask, error) { - return tm.getTask(taskID) + task, err := tm.getTask(taskID) + // update accessTime for taskID + if err := tm.accessTimeMap.Add(taskID, time.Now()); err != nil { + logger.WithTaskID(taskID).Warnf("failed to update accessTime: %v", err) + } + return task, err +} + +func (tm Manager) Exist(taskID string) bool { + _, err := tm.taskStore.Get(taskID) + return err == nil || !cdnerrors.IsDataNotFound(err) } func (tm Manager) GetAccessTime() (*syncmap.SyncMap, error) { diff --git a/cdnsystem/supervisor/task_mgr.go b/cdnsystem/supervisor/task_mgr.go index 358117178d8..1b383ec313b 100644 --- a/cdnsystem/supervisor/task_mgr.go +++ b/cdnsystem/supervisor/task_mgr.go @@ -35,6 +35,9 @@ type SeedTaskMgr interface { // Get get task Info with specified taskId. Get(string) (*types.SeedTask, error) + // Exist check task existence with specified taskId. + Exist(string) bool + // GetAccessTime get all tasks accessTime. GetAccessTime() (*syncmap.SyncMap, error)