From a033e5f5be87f2312b31c78a9c37f5d32a4195e8 Mon Sep 17 00:00:00 2001 From: Jim Ma Date: Tue, 25 May 2021 14:12:57 +0800 Subject: [PATCH] feature: remove unsafe code in client/daemon/storage (#258) Signed-off-by: Jim Ma --- client/daemon/storage/local_storage.go | 10 +++++----- client/daemon/storage/local_storage_test.go | 8 +++----- client/daemon/storage/storage_manager.go | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/client/daemon/storage/local_storage.go b/client/daemon/storage/local_storage.go index 963a88f42..d7fdbb67f 100644 --- a/client/daemon/storage/local_storage.go +++ b/client/daemon/storage/local_storage.go @@ -26,7 +26,6 @@ import ( "sync" "sync/atomic" "time" - "unsafe" "d7y.io/dragonfly/v2/client/clientutil" "d7y.io/dragonfly/v2/pkg/dferrors" @@ -47,14 +46,14 @@ type localTaskStore struct { metadataFilePath string expireTime time.Duration - lastAccess *time.Time + lastAccess int64 reclaimMarked bool gcCallback func(CommonTaskRequest) } func (t *localTaskStore) touch() { - access := time.Now() - atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&t.lastAccess)), unsafe.Pointer(&access)) + access := time.Now().UnixNano() + atomic.SwapInt64(&t.lastAccess, access) } func (t *localTaskStore) WritePiece(ctx context.Context, req *WritePieceRequest) (int64, error) { @@ -246,7 +245,8 @@ func (t *localTaskStore) GetPieces(ctx context.Context, req *base.PieceTaskReque } func (t *localTaskStore) CanReclaim() bool { - return t.lastAccess.Add(t.expireTime).Before(time.Now()) + access := time.Unix(0, t.lastAccess) + return access.Add(t.expireTime).Before(time.Now()) } // MarkReclaim will try to invoke gcCallback (normal leave peer task) diff --git a/client/daemon/storage/local_storage_test.go b/client/daemon/storage/local_storage_test.go index fe18a2074..b6ef9da2d 100644 --- a/client/daemon/storage/local_storage_test.go +++ b/client/daemon/storage/local_storage_test.go @@ -164,8 +164,7 @@ func TestLocalTaskStore_PutAndGetPiece_Simple(t *testing.T) { } // clean up test data - access := time.Now().Add(-1 * time.Hour) - ts.(*localTaskStore).lastAccess = &access + ts.(*localTaskStore).lastAccess = time.Now().Add(-1 * time.Hour).UnixNano() ok = ts.(Reclaimer).CanReclaim() assert.True(ok, "task should gc") err = ts.(Reclaimer).Reclaim() @@ -201,7 +200,7 @@ func TestLocalTaskStore_StoreTaskData_Simple(t *testing.T) { RWMutex: &sync.RWMutex{}, dataDir: test.DataDir, metadataFile: matadata, - lastAccess: &time.Time{}, + lastAccess: time.Now().UnixNano(), } err = ts.Store(context.Background(), &StoreRequest{ CommonTaskRequest: CommonTaskRequest{ @@ -339,8 +338,7 @@ func TestLocalTaskStore_PutAndGetPiece_Advance(t *testing.T) { } // clean up test data - access := time.Now().Add(-1 * time.Hour) - ts.(*localTaskStore).lastAccess = &access + ts.(*localTaskStore).lastAccess = time.Now().Add(-1 * time.Hour).UnixNano() ok = ts.(Reclaimer).CanReclaim() assert.True(ok, "task should gc") err = ts.(Reclaimer).Reclaim() diff --git a/client/daemon/storage/storage_manager.go b/client/daemon/storage/storage_manager.go index fcbab4dcb..155d7be1e 100644 --- a/client/daemon/storage/storage_manager.go +++ b/client/daemon/storage/storage_manager.go @@ -483,13 +483,13 @@ func (s *storageManager) TryGC() (bool, error) { return true }) sort.SliceStable(tasks, func(i, j int) bool { - return tasks[i].lastAccess.Before(*tasks[j].lastAccess) + return tasks[i].lastAccess < tasks[j].lastAccess }) for _, task := range tasks { task.MarkReclaim() markedTasks = append(markedTasks, PeerTaskMetaData{task.PeerID, task.TaskID}) logger.Infof("quota threshold reached, mark task %s/%s reclaimed, last access: %s, size: %s", - task.TaskID, task.PeerID, task.lastAccess.Format(time.RFC3339Nano), + task.TaskID, task.PeerID, time.Unix(0, task.lastAccess).Format(time.RFC3339Nano), units.BytesSize(float64(task.ContentLength))) totalNotMarkedSize -= task.ContentLength if totalNotMarkedSize < int64(s.storeOption.DiskGCThreshold) {