-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_cleanup.go
More file actions
252 lines (216 loc) · 6.66 KB
/
cache_cleanup.go
File metadata and controls
252 lines (216 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package velox
import (
"fmt"
"os"
"path/filepath"
"sort"
"time"
"go.uber.org/zap"
)
const (
bytesPerGB = 1024 * 1024 * 1024
)
// evictionCandidate represents a cache entry candidate for eviction
type evictionCandidate struct {
hash string
metadataPath string
binaryPath string
fileSize int64
createdAt int64
lastAccessed int64
accessCount int
}
// Cleanup performs cache cleanup based on configured limits
func (cm *CacheManager) Cleanup() error {
cm.log.Debug("starting cache cleanup")
startTime := time.Now()
// Scan cache directory for all metadata files
candidates, err := cm.scanCacheEntries()
if err != nil {
return fmt.Errorf("failed to scan cache entries: %w", err)
}
if len(candidates) == 0 {
cm.log.Debug("no cache entries found")
return nil
}
// Calculate current cache statistics
var totalSize int64
for _, candidate := range candidates {
totalSize += candidate.fileSize
}
entriesCount := len(candidates)
initialCount := entriesCount
initialSize := totalSize
cm.log.Debug("cache statistics before cleanup",
zap.Int("entries", entriesCount),
zap.Int64("total_size_bytes", totalSize),
zap.Float64("total_size_gb", float64(totalSize)/bytesPerGB),
)
// Collect entries to evict
toEvict := make([]evictionCandidate, 0)
// 1. Remove entries older than max age
if cm.cfg.MaxAgeDays > 0 {
maxAge := time.Now().Unix() - int64(cm.cfg.MaxAgeDays*24*60*60)
for i := len(candidates) - 1; i >= 0; i-- {
if candidates[i].createdAt < maxAge {
toEvict = append(toEvict, candidates[i])
totalSize -= candidates[i].fileSize
candidates = append(candidates[:i], candidates[i+1:]...)
entriesCount--
}
}
if len(toEvict) > 0 {
cm.log.Debug("found expired entries",
zap.Int("count", len(toEvict)),
)
}
}
// 2. Check if we exceed size limit
if cm.cfg.MaxSizeGB > 0 {
maxSizeBytes := int64(cm.cfg.MaxSizeGB * bytesPerGB)
if totalSize > maxSizeBytes {
// Sort by eviction policy and remove until under limit
cm.sortByEvictionPolicy(candidates)
for len(candidates) > 0 && totalSize > maxSizeBytes {
candidate := candidates[0]
candidates = candidates[1:]
toEvict = append(toEvict, candidate)
totalSize -= candidate.fileSize
entriesCount--
}
cm.log.Debug("evicting entries due to size limit",
zap.Float64("max_size_gb", cm.cfg.MaxSizeGB),
zap.Int("evicted", len(toEvict)),
)
}
}
// 3. Check if we exceed entry count limit
if cm.cfg.MaxEntries > 0 && entriesCount > cm.cfg.MaxEntries {
// Sort by eviction policy if not already sorted
if cm.cfg.MaxSizeGB <= 0 {
cm.sortByEvictionPolicy(candidates)
}
excess := entriesCount - cm.cfg.MaxEntries
for i := 0; i < excess && len(candidates) > 0; i++ {
candidate := candidates[0]
candidates = candidates[1:]
toEvict = append(toEvict, candidate)
totalSize -= candidate.fileSize
entriesCount--
}
cm.log.Debug("evicting entries due to count limit",
zap.Int("max_entries", cm.cfg.MaxEntries),
zap.Int("evicted", excess),
)
}
// Perform evictions
evictedCount := 0
var evictedSize int64
for _, candidate := range toEvict {
if err := cm.evictEntry(candidate); err != nil {
cm.log.Warn("failed to evict cache entry",
zap.Error(err),
zap.String("hash", candidate.hash),
)
continue
}
evictedCount++
evictedSize += candidate.fileSize
}
duration := time.Since(startTime)
cm.log.Info("cache cleanup completed",
zap.Int("initial_entries", initialCount),
zap.Int("final_entries", entriesCount),
zap.Int("evicted_entries", evictedCount),
zap.Int64("initial_size_bytes", initialSize),
zap.Int64("final_size_bytes", totalSize),
zap.Int64("freed_bytes", evictedSize),
zap.Float64("freed_gb", float64(evictedSize)/bytesPerGB),
zap.Duration("duration", duration),
)
return nil
}
// scanCacheEntries scans the cache directory and returns all cache entries
func (cm *CacheManager) scanCacheEntries() ([]evictionCandidate, error) {
entries, err := os.ReadDir(cm.cacheDir)
if err != nil {
return nil, fmt.Errorf("failed to read cache directory: %w", err)
}
candidates := make([]evictionCandidate, 0)
for _, entry := range entries {
// Skip if not a metadata file
if !entry.IsDir() && filepath.Ext(entry.Name()) == metadataFileSuffix {
metadataPath := filepath.Join(cm.cacheDir, entry.Name())
// Load metadata
metadata, err := cm.loadMetadata(metadataPath)
if err != nil {
cm.log.Warn("failed to load metadata during scan, skipping",
zap.Error(err),
zap.String("file", entry.Name()),
)
continue
}
// Check if binary exists
binaryPath := filepath.Join(cm.cacheDir, metadata.BinaryFile)
binaryInfo, err := os.Stat(binaryPath)
if err != nil {
cm.log.Warn("binary file missing, removing orphaned metadata",
zap.String("hash", metadata.Hash),
)
_ = os.Remove(metadataPath)
continue
}
candidates = append(candidates, evictionCandidate{
hash: metadata.Hash,
metadataPath: metadataPath,
binaryPath: binaryPath,
fileSize: binaryInfo.Size(),
createdAt: metadata.CreatedAt,
lastAccessed: metadata.LastAccessed,
accessCount: metadata.AccessCount,
})
}
}
return candidates, nil
}
// sortByEvictionPolicy sorts candidates based on the configured eviction policy
func (cm *CacheManager) sortByEvictionPolicy(candidates []evictionCandidate) {
switch cm.cfg.EvictionPolicy {
case "lru": // Least Recently Used
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].lastAccessed < candidates[j].lastAccessed
})
case "lfu": // Least Frequently Used
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].accessCount < candidates[j].accessCount
})
case "fifo": // First In First Out
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].createdAt < candidates[j].createdAt
})
default:
// Default to LRU
cm.log.Warn("unknown eviction policy, using LRU",
zap.String("policy", cm.cfg.EvictionPolicy),
)
sort.Slice(candidates, func(i, j int) bool {
return candidates[i].lastAccessed < candidates[j].lastAccessed
})
}
}
// evictEntry removes a cache entry (both binary and metadata)
func (cm *CacheManager) evictEntry(candidate evictionCandidate) error {
// Delete binary file first
if err := os.Remove(candidate.binaryPath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to delete binary file: %w", err)
}
// Delete metadata file
if err := os.Remove(candidate.metadataPath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to delete metadata file: %w", err)
}
cm.log.Debug("cache entry evicted",
zap.String("hash", candidate.hash),
zap.Int64("file_size", candidate.fileSize),
)
return nil
}