|
| 1 | +package fs |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/OpenListTeam/OpenList/v4/internal/driver" |
| 8 | + "github.com/OpenListTeam/OpenList/v4/internal/op" |
| 9 | +) |
| 10 | + |
| 11 | +// BatchTracker manages batch operations for cache refresh optimization |
| 12 | +// It aggregates multiple file operations by target directory and only refreshes |
| 13 | +// the cache once when all operations in a directory are completed |
| 14 | +type BatchTracker struct { |
| 15 | + mu sync.Mutex |
| 16 | + dirTasks map[string]*dirTaskInfo // dstStoragePath+dstDirPath -> dirTaskInfo |
| 17 | + pendingTasks map[string]string // taskID -> dstStoragePath+dstDirPath |
| 18 | + lastCleanup time.Time // last cleanup time |
| 19 | + name string // tracker name for debugging |
| 20 | +} |
| 21 | + |
| 22 | +type dirTaskInfo struct { |
| 23 | + dstStorage driver.Driver |
| 24 | + dstDirPath string |
| 25 | + pendingTasks map[string]bool // taskID -> true |
| 26 | + lastActivity time.Time // last activity time (used for detecting abnormal situations) |
| 27 | +} |
| 28 | + |
| 29 | +// NewBatchTracker creates a new batch tracker instance |
| 30 | +func NewBatchTracker(name string) *BatchTracker { |
| 31 | + return &BatchTracker{ |
| 32 | + dirTasks: make(map[string]*dirTaskInfo), |
| 33 | + pendingTasks: make(map[string]string), |
| 34 | + lastCleanup: time.Now(), |
| 35 | + name: name, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// getDirKey generates unique key for target directory |
| 40 | +func (bt *BatchTracker) getDirKey(dstStorage driver.Driver, dstDirPath string) string { |
| 41 | + return dstStorage.GetStorage().MountPath + ":" + dstDirPath |
| 42 | +} |
| 43 | + |
| 44 | +// RegisterTask registers a task to target directory for batch tracking |
| 45 | +func (bt *BatchTracker) RegisterTask(taskID string, dstStorage driver.Driver, dstDirPath string) { |
| 46 | + bt.mu.Lock() |
| 47 | + defer bt.mu.Unlock() |
| 48 | + |
| 49 | + // Periodically clean up expired entries |
| 50 | + bt.cleanupIfNeeded() |
| 51 | + |
| 52 | + dirKey := bt.getDirKey(dstStorage, dstDirPath) |
| 53 | + |
| 54 | + // Record task to directory mapping |
| 55 | + bt.pendingTasks[taskID] = dirKey |
| 56 | + |
| 57 | + // Initialize or update directory task information |
| 58 | + if info, exists := bt.dirTasks[dirKey]; exists { |
| 59 | + info.pendingTasks[taskID] = true |
| 60 | + info.lastActivity = time.Now() |
| 61 | + } else { |
| 62 | + bt.dirTasks[dirKey] = &dirTaskInfo{ |
| 63 | + dstStorage: dstStorage, |
| 64 | + dstDirPath: dstDirPath, |
| 65 | + pendingTasks: map[string]bool{taskID: true}, |
| 66 | + lastActivity: time.Now(), |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// MarkTaskCompleted marks a task as completed and returns whether cache refresh is needed |
| 72 | +// Returns (shouldRefresh, dstStorage, dstDirPath) |
| 73 | +func (bt *BatchTracker) MarkTaskCompleted(taskID string) (bool, driver.Driver, string) { |
| 74 | + bt.mu.Lock() |
| 75 | + defer bt.mu.Unlock() |
| 76 | + |
| 77 | + dirKey, exists := bt.pendingTasks[taskID] |
| 78 | + if !exists { |
| 79 | + return false, nil, "" |
| 80 | + } |
| 81 | + |
| 82 | + // Remove from pending tasks |
| 83 | + delete(bt.pendingTasks, taskID) |
| 84 | + |
| 85 | + info, exists := bt.dirTasks[dirKey] |
| 86 | + if !exists { |
| 87 | + return false, nil, "" |
| 88 | + } |
| 89 | + |
| 90 | + // Remove from directory tasks |
| 91 | + delete(info.pendingTasks, taskID) |
| 92 | + |
| 93 | + // If no pending tasks left in this directory, trigger cache refresh |
| 94 | + if len(info.pendingTasks) == 0 { |
| 95 | + dstStorage := info.dstStorage |
| 96 | + dstDirPath := info.dstDirPath |
| 97 | + delete(bt.dirTasks, dirKey) // Delete directly, no need to update lastActivity |
| 98 | + return true, dstStorage, dstDirPath |
| 99 | + } |
| 100 | + |
| 101 | + // Only update lastActivity when there are other tasks (indicating the directory still has active tasks) |
| 102 | + info.lastActivity = time.Now() |
| 103 | + return false, nil, "" |
| 104 | +} |
| 105 | + |
| 106 | +// MarkTaskCompletedWithRefresh marks a task as completed and automatically refreshes cache if needed |
| 107 | +func (bt *BatchTracker) MarkTaskCompletedWithRefresh(taskID string) { |
| 108 | + shouldRefresh, dstStorage, dstDirPath := bt.MarkTaskCompleted(taskID) |
| 109 | + if shouldRefresh { |
| 110 | + op.ClearCache(dstStorage, dstDirPath) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// cleanupIfNeeded checks if cleanup is needed and executes cleanup if necessary |
| 115 | +func (bt *BatchTracker) cleanupIfNeeded() { |
| 116 | + now := time.Now() |
| 117 | + // Clean up every 10 minutes |
| 118 | + if now.Sub(bt.lastCleanup) > 10*time.Minute { |
| 119 | + bt.cleanupStaleEntries() |
| 120 | + bt.lastCleanup = now |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// cleanupStaleEntries cleans up timed-out tasks to prevent memory leaks |
| 125 | +// Mainly used to clean up residual entries caused by abnormal situations (such as task crashes, process restarts, etc.) |
| 126 | +func (bt *BatchTracker) cleanupStaleEntries() { |
| 127 | + now := time.Now() |
| 128 | + for dirKey, info := range bt.dirTasks { |
| 129 | + // If no activity for more than 1 hour, it may indicate an abnormal situation, clean up this entry |
| 130 | + // Under normal circumstances, MarkTaskCompleted will be called when the task is completed and the entire entry will be deleted |
| 131 | + if now.Sub(info.lastActivity) > time.Hour { |
| 132 | + // Clean up related pending tasks |
| 133 | + for taskID := range info.pendingTasks { |
| 134 | + delete(bt.pendingTasks, taskID) |
| 135 | + } |
| 136 | + delete(bt.dirTasks, dirKey) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// GetPendingTaskCount returns the number of pending tasks for debugging |
| 142 | +func (bt *BatchTracker) GetPendingTaskCount() int { |
| 143 | + bt.mu.Lock() |
| 144 | + defer bt.mu.Unlock() |
| 145 | + return len(bt.pendingTasks) |
| 146 | +} |
| 147 | + |
| 148 | +// GetDirTaskCount returns the number of directories being tracked for debugging |
| 149 | +func (bt *BatchTracker) GetDirTaskCount() int { |
| 150 | + bt.mu.Lock() |
| 151 | + defer bt.mu.Unlock() |
| 152 | + return len(bt.dirTasks) |
| 153 | +} |
0 commit comments