Skip to content

Commit 8582f5b

Browse files
committed
cleanup
1 parent fa84411 commit 8582f5b

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

hack/okteto.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ dev:
1010
BUILD_BINARY_PATH: "/workspace/bin/blobcache"
1111
CONFIG_PATH: /workspace/config.yaml
1212
forward:
13-
- 6060:6060
13+
- 6666:6666

pkg/blobfs_prefetch.go

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ import (
88
)
99

1010
const (
11-
prefetchEvictionInterval = 5 * time.Second
12-
prefetchSegmentIdleTTL = 10 * time.Second // remove stale segments if no reads in the past 30s
1311
preemptiveFetchThresholdBytes = 16 * 1024 * 1024 // if the next segment is within 16MB of where we are reading, start fetching it
1412
)
1513

1614
type PrefetchManager struct {
17-
ctx context.Context
18-
config BlobCacheConfig
19-
buffers sync.Map
20-
client *BlobCacheClient
21-
currentPrefetchSizeBytes uint64
15+
ctx context.Context
16+
config BlobCacheConfig
17+
buffers sync.Map
18+
client *BlobCacheClient
19+
segmentIdleTTL time.Duration
20+
evictionInterval time.Duration
2221
}
2322

2423
func NewPrefetchManager(ctx context.Context, config BlobCacheConfig, client *BlobCacheClient) *PrefetchManager {
2524
return &PrefetchManager{
26-
ctx: ctx,
27-
config: config,
28-
buffers: sync.Map{},
29-
client: client,
30-
currentPrefetchSizeBytes: 0,
25+
ctx: ctx,
26+
config: config,
27+
buffers: sync.Map{},
28+
client: client,
29+
segmentIdleTTL: time.Duration(config.BlobFs.Prefetch.IdleTtlS) * time.Second,
30+
evictionInterval: time.Duration(config.BlobFs.Prefetch.EvictionIntervalS) * time.Second,
3131
}
3232
}
3333

@@ -62,15 +62,15 @@ func (pm *PrefetchManager) evictIdleBuffers() {
6262
select {
6363
case <-pm.ctx.Done():
6464
return
65-
case <-time.After(prefetchEvictionInterval):
65+
case <-time.After(pm.evictionInterval):
6666
pm.buffers.Range(func(key, value any) bool {
6767
buffer := value.(*PrefetchBuffer)
6868

69-
// If no reads have happened in any segments in the buffer
69+
// If no reads have happened in any windows in the buffer
7070
// stop any fetch operations and clear the buffer so it can
7171
// be garbage collected
72-
unused := buffer.evictIdle()
73-
if unused {
72+
idle := buffer.IsIdle()
73+
if idle {
7474
buffer.Clear()
7575
pm.buffers.Delete(key)
7676
}
@@ -149,12 +149,23 @@ func (pb *PrefetchBuffer) fetch(offset uint64, bufferSize uint64) {
149149
}
150150
}
151151

152-
w := &window{
153-
index: bufferIndex,
154-
data: make([]byte, 0, bufferSize),
155-
readLength: 0,
156-
lastRead: time.Now(),
157-
fetching: true,
152+
existingWindow := pb.prevWindow
153+
var w *window
154+
if existingWindow != nil {
155+
w = existingWindow
156+
w.index = bufferIndex
157+
w.readLength = 0
158+
w.data = make([]byte, 0, bufferSize)
159+
w.lastRead = time.Now()
160+
w.fetching = true
161+
} else {
162+
w = &window{
163+
index: bufferIndex,
164+
data: make([]byte, 0, bufferSize),
165+
readLength: 0,
166+
lastRead: time.Now(),
167+
fetching: true,
168+
}
158169
}
159170

160171
// Slide windows
@@ -192,24 +203,21 @@ func (pb *PrefetchBuffer) fetch(offset uint64, bufferSize uint64) {
192203
}
193204
}
194205

195-
func (pb *PrefetchBuffer) evictIdle() bool {
196-
unused := true
206+
func (pb *PrefetchBuffer) IsIdle() bool {
207+
idle := true
197208

198209
pb.mu.Lock()
199210
windows := []*window{pb.prevWindow, pb.currentWindow, pb.nextWindow}
200-
for i, w := range windows {
201-
if w != nil && time.Since(w.lastRead) > prefetchSegmentIdleTTL && !w.fetching {
202-
Logger.Debugf("Evicting segment %s-%d", pb.hash, w.index)
203-
w.data = nil
204-
windows[i] = nil
211+
for _, w := range windows {
212+
if w != nil && time.Since(w.lastRead) > pb.manager.segmentIdleTTL && !w.fetching {
213+
continue
205214
} else {
206-
unused = false
215+
idle = false
207216
}
208217
}
209-
pb.prevWindow, pb.currentWindow, pb.nextWindow = windows[0], windows[1], windows[2]
210218
pb.mu.Unlock()
211219

212-
return unused
220+
return idle
213221
}
214222

215223
func (pb *PrefetchBuffer) Clear() {
@@ -218,6 +226,8 @@ func (pb *PrefetchBuffer) Clear() {
218226
pb.mu.Lock()
219227
defer pb.mu.Unlock()
220228

229+
Logger.Infof("Evicting idle prefetch buffer - %s", pb.hash)
230+
221231
// Clear all window data
222232
windows := []*window{pb.prevWindow, pb.currentWindow, pb.nextWindow}
223233
for _, window := range windows {

pkg/config.default.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ options: []
1414
blobfs:
1515
prefetch:
1616
enabled: false
17-
idleTtlS: 60
17+
idleTtlS: 5
18+
evictionIntervalS: 5
1819
minFileSizeBytes: 1048576 # 1MB
1920
windowSizeBytes: 134217728 # 128MB
2021
dataTimeoutS: 30

pkg/types.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ type BlobFsConfig struct {
103103
}
104104

105105
type BlobFsPrefetchConfig struct {
106-
Enabled bool `key:"enabled" json:"enabled"`
107-
MinFileSizeBytes uint64 `key:"minFileSizeBytes" json:"min_file_size_bytes"`
108-
IdleTtlS int `key:"idleTtlS" json:"idle_ttl_s"`
109-
WindowSizeBytes uint64 `key:"windowSizeBytes" json:"window_size_bytes"`
110-
IgnoreFileExt []string `key:"ignoreFileExt" json:"ignore_file_ext"`
111-
DataTimeoutS int `key:"dataTimeoutS" json:"data_timeout_s"`
106+
Enabled bool `key:"enabled" json:"enabled"`
107+
MinFileSizeBytes uint64 `key:"minFileSizeBytes" json:"min_file_size_bytes"`
108+
EvictionIntervalS int `key:"evictionIntervalS" json:"eviction_interval_s"`
109+
IdleTtlS int `key:"idleTtlS" json:"idle_ttl_s"`
110+
WindowSizeBytes uint64 `key:"windowSizeBytes" json:"window_size_bytes"`
111+
IgnoreFileExt []string `key:"ignoreFileExt" json:"ignore_file_ext"`
112+
DataTimeoutS int `key:"dataTimeoutS" json:"data_timeout_s"`
112113
}
113114

114115
type SourceConfig struct {

0 commit comments

Comments
 (0)