feature: remove unsafe code in client/daemon/storage (#258)
Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
parent
a777c8882c
commit
a033e5f5be
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue