Skip to content

Extend use of ZFS pre-caching #1941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3718,6 +3718,17 @@ func (d *lxc) RenderFull(hostInterfaces []net.Interface) (*api.InstanceFull, any
return nil, nil, fmt.Errorf("RenderFull only works with containers")
}

// Pre-fetch the data.
pool, err := d.getStoragePool()
if err != nil {
return nil, nil, err
}

err = pool.CacheInstanceSnapshots(d)
if err != nil {
return nil, nil, err
}

// Get the Container struct
base, etag, err := d.Render()
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -8108,6 +8108,17 @@ func (d *qemu) RenderFull(hostInterfaces []net.Interface) (*api.InstanceFull, an
return nil, nil, fmt.Errorf("RenderFull doesn't work with snapshots")
}

// Pre-fetch the data.
pool, err := d.getStoragePool()
if err != nil {
return nil, nil, err
}

err = pool.CacheInstanceSnapshots(d)
if err != nil {
return nil, nil, err
}

// Get the Instance struct.
base, etag, err := d.Render()
if err != nil {
Expand Down
29 changes: 16 additions & 13 deletions internal/server/storage/drivers/driver_zfs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1652,19 +1652,22 @@ func (d *zfs) CacheVolumeSnapshots(vol Volume) error {
d.cacheMu.Lock()
defer d.cacheMu.Unlock()

if d.cache == nil {
d.cache = map[string]map[string]int64{}
// Check if we've already cached the data.
if d.cache != nil {
return nil
}

// Get the usage data.
out, err := subprocess.RunCommand("zfs", "list", "-H", "-p", "-o", "name,used,referenced", "-t", "snapshot", d.dataset(vol, false))
out, err := subprocess.RunCommand("zfs", "list", "-H", "-p", "-o", "name,used,referenced", "-r", "-t", "all", d.dataset(vol, false))
if err != nil {
d.logger.Warn("Coulnd't list volume snapshots", logger.Ctx{"err": err})

// The cache is an optional performance improvement, don't block on failure.
return nil
}

// Parse and update the cache.
d.cache = map[string]map[string]int64{}
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line == "" {
Expand Down Expand Up @@ -1718,18 +1721,18 @@ func (d *zfs) GetVolumeUsage(vol Volume) (int64, error) {

return int64(stat.Blocks-stat.Bfree) * int64(stat.Bsize), nil
}
} else {
// Use the snapshot cache if present.
d.cacheMu.Lock()
defer d.cacheMu.Unlock()
}

if d.cache != nil {
cache, ok := d.cache[d.dataset(vol, false)]
// Try to use the cached data.
d.cacheMu.Lock()
defer d.cacheMu.Unlock()

if d.cache != nil {
cache, ok := d.cache[d.dataset(vol, false)]
if ok {
value, ok := cache[key]
if ok {
value, ok := cache[key]
if ok {
return value, nil
}
return value, nil
}
}
}
Expand Down