-
Notifications
You must be signed in to change notification settings - Fork 1
fix(satellite): wait for backing block device before mkfs/attach (zfs/udev race) #170
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package satellite | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| intent "github.com/cozystack/blockstor/pkg/satellite/intent" | ||
| "github.com/cozystack/blockstor/pkg/storage" | ||
| ) | ||
|
|
||
| // errFakeNoDevice is the canned "backing device not present yet" error the | ||
| // fake exec returns for blockdev probes before the (simulated) udev | ||
| // /dev/zvol symlink appears. | ||
| var errFakeNoDevice = errors.New("blockdev: cannot open device: No such file or directory") | ||
|
|
||
| // zvolWaitFakeProvider is a no-op storage.Provider whose VolumeStatus | ||
| // reports a fixed device path. It lets the applyStorage device-wait tests | ||
| // drive the post-`zfs create` udev race without a real ZFS/LVM backend. | ||
| // UsableKib is large so applyStorage never takes the resize branch. | ||
| type zvolWaitFakeProvider struct{ dev string } | ||
|
|
||
| func (zvolWaitFakeProvider) Kind() string { return ProviderKindZFSThin } | ||
|
|
||
| func (zvolWaitFakeProvider) PoolStatus(context.Context) (storage.PoolStatus, error) { | ||
| return storage.PoolStatus{}, nil | ||
| } | ||
|
|
||
| func (zvolWaitFakeProvider) CreateVolume(context.Context, storage.Volume) error { return nil } | ||
| func (zvolWaitFakeProvider) DeleteVolume(context.Context, storage.Volume) error { return nil } | ||
| func (zvolWaitFakeProvider) ResizeVolume(context.Context, storage.Volume) error { return nil } | ||
|
|
||
| func (p zvolWaitFakeProvider) VolumeStatus(context.Context, storage.Volume) (storage.VolumeStatus, error) { | ||
| return storage.VolumeStatus{DevicePath: p.dev, UsableKib: 1 << 30, State: "PROVISIONED"}, nil | ||
| } | ||
|
|
||
| func (zvolWaitFakeProvider) CreateSnapshot(context.Context, storage.Snapshot) error { return nil } | ||
| func (zvolWaitFakeProvider) DeleteSnapshot(context.Context, storage.Snapshot) error { return nil } | ||
|
|
||
| func (zvolWaitFakeProvider) RestoreVolumeFromSnapshot( | ||
| context.Context, storage.Volume, storage.Snapshot, | ||
| ) error { | ||
| return nil | ||
| } | ||
|
|
||
| // countCalls counts how many recorded FakeExec calls match name + args | ||
| // exactly. Safe to read fx.Calls directly here: applyStorage runs | ||
| // synchronously in the test goroutine, no product goroutine is live. | ||
| func countCalls(fx *storage.FakeExec, name string, args ...string) int { | ||
| want := strings.Join(args, " ") | ||
|
|
||
| n := 0 | ||
| for _, c := range fx.Calls { | ||
| if c.Name == name && strings.Join(c.Args, " ") == want { | ||
| n++ | ||
| } | ||
| } | ||
|
|
||
| return n | ||
| } | ||
|
|
||
| // TestApplyStorageWaitsForZvolDevice is the regression guard for the | ||
| // zfs-create/udev race: `zfs create -V` returns before udev has created | ||
| // the /dev/zvol/<pool>/<vol> symlink, so the storage-only (local SC) path | ||
| // used to run mkfs immediately on a device that did not exist yet | ||
| // ("mkfs.ext4 ...: The file ... does not exist"). applyStorage must now | ||
| // poll `blockdev --getsize64` until the backing device is usable before | ||
| // returning it to the mkfs / DRBD-attach consumers. | ||
| // | ||
| // On the pre-fix code applyStorage never probes the device, so the | ||
| // blockdev call count is 0 and this test fails — exactly the fail-on-bug | ||
| // property the fix must carry. | ||
| func TestApplyStorageWaitsForZvolDevice(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const dev = "/dev/zvol/data/pvc-zvolwait_00000" | ||
|
|
||
| fx := storage.NewFakeExec() | ||
| // blockdev fails twice (udev has not made the symlink yet), then the | ||
| // device appears and it succeeds. Later calls fall through to fx | ||
| // (empty success). The sequencedFakeExec helper still records every | ||
| // forwarded call in fx.Calls for the count assertion below. | ||
| seq := &sequencedFakeExec{ | ||
| fx: fx, | ||
| seqKey: "blockdev --getsize64 " + dev, | ||
| seqResps: []storage.FakeResponse{ | ||
| {Err: errFakeNoDevice}, | ||
| {Err: errFakeNoDevice}, | ||
| {Stdout: []byte("1073741824\n")}, | ||
| }, | ||
| } | ||
|
|
||
| rec := NewReconciler(ReconcilerConfig{ | ||
| Providers: map[string]storage.Provider{"p1": zvolWaitFakeProvider{dev: dev}}, | ||
| Exec: seq, | ||
| DeviceWaitPoll: time.Millisecond, | ||
| DeviceWaitTimeout: 5 * time.Second, | ||
| }) | ||
|
|
||
| dr := &intent.DesiredResource{ | ||
| Name: "pvc-zvolwait", | ||
| Volumes: []*intent.DesiredVolume{{VolumeNumber: 0, SizeKib: 1024, StoragePool: "p1"}}, | ||
| } | ||
|
|
||
| devices, _, _, err := rec.applyStorage(context.Background(), dr) | ||
| if err != nil { | ||
| t.Fatalf("applyStorage: unexpected error: %v", err) | ||
| } | ||
|
|
||
| if devices[0] != dev { | ||
| t.Fatalf("device path: got %q, want %q", devices[0], dev) | ||
| } | ||
|
|
||
| got := countCalls(fx, "blockdev", "--getsize64", dev) | ||
| if got < 3 { | ||
| t.Fatalf("blockdev --getsize64 calls: got %d, want >=3 "+ | ||
| "(applyStorage must poll for the backing device before returning)", got) | ||
| } | ||
| } | ||
|
|
||
| // TestApplyStorageFailsWhenZvolDeviceNeverAppears pins the timeout side of | ||
| // the wait: if the backing device never materialises, applyStorage must | ||
| // surface an error so the resource is re-queued rather than mkfs-ing / | ||
| // attaching a path that is not there. | ||
| func TestApplyStorageFailsWhenZvolDeviceNeverAppears(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const dev = "/dev/zvol/data/pvc-never_00000" | ||
|
|
||
| fx := storage.NewFakeExec() | ||
| fx.Expect("blockdev --getsize64 "+dev, storage.FakeResponse{Err: errFakeNoDevice}) | ||
|
|
||
| rec := NewReconciler(ReconcilerConfig{ | ||
| Providers: map[string]storage.Provider{"p1": zvolWaitFakeProvider{dev: dev}}, | ||
| Exec: fx, | ||
| DeviceWaitPoll: time.Millisecond, | ||
| DeviceWaitTimeout: 50 * time.Millisecond, | ||
| }) | ||
|
|
||
| dr := &intent.DesiredResource{ | ||
| Name: "pvc-never", | ||
| Volumes: []*intent.DesiredVolume{{VolumeNumber: 0, SizeKib: 1024, StoragePool: "p1"}}, | ||
| } | ||
|
|
||
| _, _, _, err := rec.applyStorage(context.Background(), dr) //nolint:dogsled // only the returned error is relevant here | ||
| if err == nil { | ||
| t.Fatal("applyStorage: want error when the backing device never appears, got nil") | ||
| } | ||
|
|
||
| if !strings.Contains(err.Error(), "did not appear") { | ||
| t.Fatalf("error: got %v, want it to report the device never appeared", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
udevadm settlein a polling loop is problematic:udevadm settleis a global barrier that blocks until the entire system's udev event queue is empty. On a busy Kubernetes node with concurrent volume attachments, network interface changes, or other hardware events, the queue may frequently be active. This causesudevadm settleto block for its full timeout (5 seconds) even if the target zvol's symlink is already created or unrelated to those events.DeviceWaitTimeoutto expire prematurely under system load.udevadmis missing or permissions are restricted (e.g., certain containerized setups), spawning a non-existent or failing process every 500ms wastes CPU cycles and floods system logs.udevadm settledoes not speed up or trigger udev processing; it is purely passive. The udev daemon will process the events asynchronously in the background anyway.Recommendation:
Remove
udevadm settlefrom the polling loop entirely. Pollingblockdev --getsize64with a 500ms interval is extremely lightweight, safe, and sufficient.