Skip to content

disk: introduce data cache#1621

Open
huww98 wants to merge 3 commits into
kubernetes-sigs:masterfrom
huww98:local-disk-cache
Open

disk: introduce data cache#1621
huww98 wants to merge 3 commits into
kubernetes-sigs:masterfrom
huww98:local-disk-cache

Conversation

@huww98

@huww98 huww98 commented Jan 20, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Use a faster disk as a cache layer over cloud disk.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?

disk: support using a faster disk as a cache layer over cloud disk.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 20, 2026
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: huww98

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 20, 2026
@huww98 huww98 force-pushed the local-disk-cache branch 3 times, most recently from 09b5bf8 to 0e9f46b Compare January 23, 2026 03:57
mowangdk

This comment was marked as outdated.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 22, 2026
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 22, 2026
@k8s-triage-robot

Copy link
Copy Markdown

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 23, 2026

@mowangdk mowangdk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review

Summary: Introduces dm-cache (device-mapper cache) support for cloud disks, using local NVMe/SSD as a cache tier via loop devices. Includes setup, resize, and teardown of dm-cache backed by fallocated files.

Issues Found:

  • Unsafe pointer usage: The dmi_t struct uses unsafe.Pointer with unix.Syscall for ioctl calls. While necessary for dm-ioctl, ensure the struct layout exactly matches the kernel's expectations — any padding mismatch could corrupt memory.
  • Error handling in allocCacheFile: If IoctlLoopConfigure fails, the loop device fd is leaked (opened but never closed). Add a defer loggedClose(logger, loop) before the configure call, or close on error path.
  • Race condition potential: loop_get_free() gets a free loop device number, but between getting the number and opening it, another process could claim it. Consider retrying on EBUSY.
  • structs.HostLayout: This is a Go 1.23+ feature. Confirm the project's minimum Go version supports this.
  • The DataCachePath constant uses /var/alibaba-cloud-csi/data-cache — this is a host path, so the Helm chart correctly adds the volume mount (seen in the template diff).

Suggestions:

  • Add cleanup logic (or documentation) for what happens when a node reboots — loop devices configured with LO_FLAGS_AUTOCLEAR will disappear, but the dm-cache device entries may not persist across reboots either.
  • Consider adding metrics for cache hit/miss ratios to help users understand cache effectiveness.
  • Unit tests for getDataCacheOpts parsing would be valuable.

Overall: Complex but well-implemented feature leveraging dm-cache for performance. The low-level kernel interface code is inherently delicate — careful testing on target kernels is essential. The fd leak in the error path should be fixed.

@mowangdk mowangdk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Code Review

Summary: Introduces dm-cache support as a local cache layer over cloud disks using device-mapper, loop devices, and fallocate.

Overall: Powerful feature with careful dm-cache management. Some concerns about resource leaks on error paths and unsafe pointer usage.

Comment thread pkg/disk/data_cache.go
Flags: flags,
}
copy(dm.Name[:], volumeID)
_, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), action, uintptr(unsafe.Pointer(&dm)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allocCacheFile opens the loop device fd (loop) but never closes it on error paths after Open succeeds. If IoctlLoopConfigure fails, the loop fd leaks. Add a deferred close that is cancelled on success, or restructure so the fd is always returned.

Comment thread pkg/disk/data_cache.go
}
copy(dmi.Name[:], volumeID)
copy(dmi.Args[:], args)
_, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(dmCtrl), unix.DM_TABLE_LOAD, uintptr(unsafe.Pointer(&dmi)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correctness of passing unsafe.Pointer(&dmi) to SYS_IOCTL depends on the struct layout matching what the kernel expects exactly. Consider adding a compile-time size assertion (e.g., var _ [4096 - unsafe.Sizeof(dmi_t{})]byte) to catch layout drift if the Go struct definitions change.

Comment thread pkg/disk/data_cache.go
return "/dev/mapper/" + volumeID
}

func setupDataCache(logger klog.Logger, d *dataCache, device, volumeID string) (string, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getBlockDeviceCapacity(device) is called but there's no error handling visible. If it returns 0 (e.g., device not ready), dm-cache is set up with size 0, which would be invalid. Consider validating the return value before calling setupDmCache.

Comment thread pkg/disk/nodeserver.go
}

// Teardown DataCache
if cacheErr := teardownDataCache(logger, req.VolumeId); cacheErr != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data cache teardown happens after unmount. If teardownDataCache fails, the function returns an error but the volume is already unmounted. This leaves a lingering dm-cache device that blocks future unstage attempts. Consider whether teardown failures should be non-fatal (log and continue) to avoid permanently blocking the unstage flow.

@k8s-triage-robot

Copy link
Copy Markdown

The Kubernetes project currently lacks enough active contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle rotten
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@kubernetes-prow kubernetes-prow Bot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants