Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
feature: add access time for taskID
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop authored and lowzj committed Aug 2, 2019
1 parent 82af5ce commit 0da3300
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target
.go/
release/
bin/
vendor/

#df-daemon

Expand Down
26 changes: 26 additions & 0 deletions common/util/sys_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package util

import (
"time"
)

// GetCurrentTimeMillis returns the time in millis for now.
func GetCurrentTimeMillis() int64 {
return time.Now().UnixNano() / time.Millisecond.Nanoseconds()
}
8 changes: 2 additions & 6 deletions supernode/daemon/mgr/cdn/cdn_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package cdn

import (
"fmt"
"time"

"github.com/dragonflyoss/Dragonfly/apis/types"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
)

var getCurrentTimeMillisFunc = getCurrentTimeMillis

func getCurrentTimeMillis() int64 {
return time.Now().UnixNano() / time.Millisecond.Nanoseconds()
}
var getCurrentTimeMillisFunc = cutil.GetCurrentTimeMillis

// getContentLengthByHeader calculates the piece content length by piece header.
func getContentLengthByHeader(pieceHeader uint32) int32 {
Expand Down
37 changes: 27 additions & 10 deletions supernode/daemon/mgr/task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ var getContentLength = cutil.GetContentLength
type Manager struct {
cfg *config.Config

taskStore *dutil.Store
taskLocker *util.LockerPool
taskStore *dutil.Store
taskLocker *util.LockerPool
accessTimeMap *cutil.SyncMap

peerMgr mgr.PeerMgr
dfgetTaskMgr mgr.DfgetTaskMgr
Expand All @@ -43,14 +44,15 @@ type Manager struct {
func NewManager(cfg *config.Config, peerMgr mgr.PeerMgr, dfgetTaskMgr mgr.DfgetTaskMgr,
progressMgr mgr.ProgressMgr, cdnMgr mgr.CDNMgr, schedulerMgr mgr.SchedulerMgr) (*Manager, error) {
return &Manager{
cfg: cfg,
taskStore: dutil.NewStore(),
taskLocker: util.NewLockerPool(),
peerMgr: peerMgr,
dfgetTaskMgr: dfgetTaskMgr,
progressMgr: progressMgr,
cdnMgr: cdnMgr,
schedulerMgr: schedulerMgr,
cfg: cfg,
taskStore: dutil.NewStore(),
taskLocker: util.NewLockerPool(),
peerMgr: peerMgr,
dfgetTaskMgr: dfgetTaskMgr,
progressMgr: progressMgr,
cdnMgr: cdnMgr,
schedulerMgr: schedulerMgr,
accessTimeMap: cutil.NewSyncMap(),
}, nil
}

Expand All @@ -70,6 +72,11 @@ func (tm *Manager) Register(ctx context.Context, req *types.TaskCreateRequest) (
logrus.Debugf("success to get task info: %+v", task)
// TODO: defer rollback the task update

// update accessTime for taskID
if err := tm.accessTimeMap.Add(task.ID, cutil.GetCurrentTimeMillis()); err != nil {
logrus.Warnf("failed to update accessTime for taskID(%s): %v", task.ID, err)
}

// Step3: add a new DfgetTask
dfgetTask, err := tm.addDfgetTask(ctx, req, task)
if err != nil {
Expand Down Expand Up @@ -111,6 +118,11 @@ func (tm *Manager) Get(ctx context.Context, taskID string) (*types.TaskInfo, err
return tm.getTask(taskID)
}

// GetAccessTime gets all task accessTime.
func (tm *Manager) GetAccessTime(ctx context.Context) (*cutil.SyncMap, error) {
return tm.accessTimeMap, nil
}

// List returns a list of tasks with filter.
// TODO: implement it.
func (tm *Manager) List(ctx context.Context, filter map[string]string) ([]*types.TaskInfo, error) {
Expand Down Expand Up @@ -165,6 +177,11 @@ func (tm *Manager) GetPieces(ctx context.Context, taskID, clientID string, req *
}
logrus.Debugf("success to get task: %+v", task)

// update accessTime for taskID
if err := tm.accessTimeMap.Add(task.ID, cutil.GetCurrentTimeMillis()); err != nil {
logrus.Warnf("failed to update accessTime for taskID(%s): %v", task.ID, err)
}

if dfgetTaskStatus == types.DfGetTaskStatusWAITING {
logrus.Debugf("start to process task(%s) start", taskID)
return tm.processTaskStart(ctx, clientID, task, dfgetTask)
Expand Down
4 changes: 4 additions & 0 deletions supernode/daemon/mgr/task_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/dragonflyoss/Dragonfly/apis/types"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
"github.com/dragonflyoss/Dragonfly/supernode/config"
)

Expand All @@ -26,6 +27,9 @@ type TaskMgr interface {
// Get the task Info with specified taskID.
Get(ctx context.Context, taskID string) (*types.TaskInfo, error)

// GetAccessTime gets all task accessTime.
GetAccessTime(ctx context.Context) (*cutil.SyncMap, error)

// List returns the list tasks with filter.
List(ctx context.Context, filter map[string]string) ([]*types.TaskInfo, error)

Expand Down

0 comments on commit 0da3300

Please sign in to comment.