Skip to content

Commit

Permalink
feature: remove unsafe code in client/daemon/storage (#258)
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Ma <majinjing3@gmail.com>
  • Loading branch information
jim3ma authored and gaius-qi committed Jun 28, 2023
1 parent a777c88 commit a033e5f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
10 changes: 5 additions & 5 deletions client/daemon/storage/local_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

"d7y.io/dragonfly/v2/client/clientutil"
"d7y.io/dragonfly/v2/pkg/dferrors"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions client/daemon/storage/local_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions client/daemon/storage/storage_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a033e5f

Please sign in to comment.