-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Better error recovery in device mapper #3470
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,35 +118,59 @@ func (p *PoolDevice) CreateThinDevice(ctx context.Context, deviceName string, vi | |
| State: Unknown, | ||
| } | ||
|
|
||
| // Save initial device metadata and allocate new device ID from store | ||
| if err := p.metadata.AddDevice(ctx, info); err != nil { | ||
| return errors.Wrapf(err, "failed to save initial metadata for new thin device %q", deviceName) | ||
| } | ||
| var ( | ||
| metaErr error | ||
| devErr error | ||
| activeErr error | ||
| ) | ||
|
|
||
| defer func() { | ||
| if retErr == nil { | ||
| // We've created a devmapper device, but failed to activate it, try rollback everything | ||
| if activeErr != nil { | ||
| // Delete the device first. | ||
| delErr := p.deleteDevice(ctx, info) | ||
| if delErr != nil { | ||
| // Failed to rollback, mark the device as faulty and keep metadata in order to | ||
| // preserve the faulty device ID | ||
| retErr = multierror.Append(retErr, delErr, p.metadata.MarkFaulty(ctx, info.Name)) | ||
| return | ||
| } | ||
|
|
||
| // The devmapper device has been successfully deleted, deallocate device ID | ||
| if err := p.RemoveDevice(ctx, info.Name); err != nil { | ||
| retErr = multierror.Append(retErr, err) | ||
| return | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Rollback metadata | ||
| retErr = multierror.Append(retErr, p.metadata.RemoveDevice(ctx, info.Name)) | ||
| // We're unable to create the devmapper device, most likely something wrong with the deviceID | ||
| if devErr != nil { | ||
| retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, info.Name)) | ||
| return | ||
| } | ||
| }() | ||
|
|
||
| // Create thin device | ||
| if err := p.createDevice(ctx, info); err != nil { | ||
| return err | ||
| // Save initial device metadata and allocate new device ID from store | ||
| metaErr = p.metadata.AddDevice(ctx, info) | ||
| if metaErr != nil { | ||
| return metaErr | ||
| } | ||
|
|
||
| defer func() { | ||
| if retErr == nil { | ||
| return | ||
| } | ||
| // Create thin device | ||
| devErr = p.createDevice(ctx, info) | ||
| if devErr != nil { | ||
| return devErr | ||
| } | ||
|
|
||
| // Rollback creation | ||
| retErr = multierror.Append(retErr, p.deleteDevice(ctx, info)) | ||
| }() | ||
| // Activate thin device | ||
| activeErr = p.activateDevice(ctx, info) | ||
| if activeErr != nil { | ||
| return activeErr | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, What if crash happens in between these steps and rollback code doesn't get chance to run? This possibility also applies to other places to change device or metadata, right?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Each device mapper operation is wrapped with state transition function (have a look on 1 and 2). If we're failed to activate a device and crash happens, so no
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Essentially each operation with devmapper device is recorded (https://github.com/containerd/containerd/blob/master/snapshots/devmapper/device_info.go#L34), so you can get a clear picture about device state and last operation at any time. For example, for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, thanks for your detailed explanation ;-) Yes, I know, in the crash case, the metadata DB can reflect correctly the device state before crashing, such as creating, activating etc. Actually, my question was, in that case, there is no chance to the rollback code to mark the device as If so, after machine comes up and containerd get started again, we still have "object already exists" error from the AddDevice() - 878a320#diff-4132e94f99d36d21b09c446fb20687faL100.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you crash right in between create and rollback, then yes, a device won't be marked as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a crash happens, containerd must restart again, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hi, I just come up this idea. I'd like to know what do you think?thanks. diff --git a/snapshots/devmapper/pool_device.go b/snapshots/devmapper/pool_device.go
index 22e78c1..4eaab6e 100644
--- a/snapshots/devmapper/pool_device.go
+++ b/snapshots/devmapper/pool_device.go
@@ -150,6 +150,16 @@ func (p *PoolDevice) CreateThinDevice(ctx context.Context, deviceName string, vi
retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, info.Name))
return
}
+
+ // If ErrAlreadyExists returns, it indicates the device name is already in metadata,
+ // but not aware by upper layer. This may occur when crash happens and there is no
+ // chance to mark the device faulty. So, mark it here and upper layer will retry with
+ // the same device name.
+ if metaErr == ErrAlreadyExists {
+ log.G(ctx).Warnf("conflicting thin device %s, mark it faulty", info.Name)
+ retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, info.Name))
+ return
+ }
}()
// Save initial device metadata and allocate new device ID from store
@@ -241,6 +251,12 @@ func (p *PoolDevice) CreateSnapshotDevice(ctx context.Context, deviceName string
retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, snapInfo.Name))
return
}
+
+ if metaErr == ErrAlreadyExists {
+ log.G(ctx).Warnf("conflicting snapshot %s, mark it faulty", snapInfo.Name)
+ retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, snapInfo.Name))
+ return
+ }
}()
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've addressed this use case in #3489. In case of crash, the snapshotter will mark devices as faulty after restart. Can you check if it works for you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Great, thanks! |
||
| return p.activateDevice(ctx, info) | ||
| return nil | ||
| } | ||
|
|
||
| // createDevice creates thin device | ||
|
|
@@ -185,36 +209,59 @@ func (p *PoolDevice) CreateSnapshotDevice(ctx context.Context, deviceName string | |
| State: Unknown, | ||
| } | ||
|
|
||
| // Save snapshot metadata and allocate new device ID | ||
| if err := p.metadata.AddDevice(ctx, snapInfo); err != nil { | ||
| return errors.Wrapf(err, "failed to save initial metadata for snapshot %q", snapshotName) | ||
| } | ||
| var ( | ||
| metaErr error | ||
| devErr error | ||
| activeErr error | ||
| ) | ||
|
|
||
| defer func() { | ||
| if retErr == nil { | ||
| // We've created a devmapper device, but failed to activate it, try rollback everything | ||
| if activeErr != nil { | ||
| // Delete the device first. | ||
| delErr := p.deleteDevice(ctx, snapInfo) | ||
| if delErr != nil { | ||
| // Failed to rollback, mark the device as faulty and keep metadata in order to | ||
| // preserve the faulty device ID | ||
| retErr = multierror.Append(retErr, delErr, p.metadata.MarkFaulty(ctx, snapInfo.Name)) | ||
| return | ||
| } | ||
|
|
||
| // The devmapper device has been successfully deleted, deallocate device ID | ||
| if err := p.RemoveDevice(ctx, snapInfo.Name); err != nil { | ||
| retErr = multierror.Append(retErr, err) | ||
| return | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // Rollback metadata | ||
| retErr = multierror.Append(retErr, p.metadata.RemoveDevice(ctx, snapInfo.Name)) | ||
| // We're unable to create the devmapper device, most likely something wrong with the deviceID | ||
| if devErr != nil { | ||
| retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, snapInfo.Name)) | ||
| return | ||
| } | ||
| }() | ||
|
|
||
| // Create thin device snapshot | ||
| if err := p.createSnapshot(ctx, baseInfo, snapInfo); err != nil { | ||
| return err | ||
| // Save snapshot metadata and allocate new device ID | ||
| metaErr = p.metadata.AddDevice(ctx, snapInfo) | ||
| if metaErr != nil { | ||
| return metaErr | ||
| } | ||
|
|
||
| defer func() { | ||
| if retErr == nil { | ||
| return | ||
| } | ||
| // Create thin device snapshot | ||
| devErr = p.createSnapshot(ctx, baseInfo, snapInfo) | ||
| if devErr != nil { | ||
| return devErr | ||
| } | ||
|
|
||
| // Rollback snapshot creation | ||
| retErr = multierror.Append(retErr, p.deleteDevice(ctx, snapInfo)) | ||
| }() | ||
| // Activate the snapshot device | ||
| activeErr = p.activateDevice(ctx, snapInfo) | ||
| if activeErr != nil { | ||
| return activeErr | ||
| } | ||
|
|
||
| // Activate snapshot device | ||
| return p.activateDevice(ctx, snapInfo) | ||
| return nil | ||
| } | ||
|
|
||
| func (p *PoolDevice) createSnapshot(ctx context.Context, baseInfo, snapInfo *DeviceInfo) error { | ||
|
|
@@ -317,7 +364,7 @@ func (p *PoolDevice) RemoveDevice(ctx context.Context, deviceName string) error | |
| return errors.Wrapf(err, "can't query metadata for device %q", deviceName) | ||
| } | ||
|
|
||
| if err := p.DeactivateDevice(ctx, deviceName, true, true); err != nil { | ||
| if err := p.DeactivateDevice(ctx, deviceName, false, true); err != nil { | ||
| return err | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.