Mark faulty device in one transaction

Signed-off-by: Maksym Pavlenko <makpav@amazon.com>
This commit is contained in:
Maksym Pavlenko 2019-07-30 16:03:09 -07:00
parent 878a3205cd
commit 4d5a0e19eb
3 changed files with 21 additions and 20 deletions

View File

@ -24,7 +24,6 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors" "github.com/pkg/errors"
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
) )
@ -127,23 +126,25 @@ func (m *PoolMetadata) AddDevice(ctx context.Context, info *DeviceInfo) error {
// The snapshotter might attempt to recreate a device in 'Faulty' state with another devmapper ID in // The snapshotter might attempt to recreate a device in 'Faulty' state with another devmapper ID in
// subsequent calls, and in case of success it's status will be changed to 'Created' or 'Activated'. // subsequent calls, and in case of success it's status will be changed to 'Created' or 'Activated'.
// The devmapper dev ID will remain in 'deviceFaulty' state until manually handled by a user. // The devmapper dev ID will remain in 'deviceFaulty' state until manually handled by a user.
func (m *PoolMetadata) MarkFaulty(ctx context.Context, info *DeviceInfo) error { func (m *PoolMetadata) MarkFaulty(ctx context.Context, name string) error {
var result error return m.db.Update(func(tx *bolt.Tx) error {
var (
device = DeviceInfo{}
devBucket = tx.Bucket(devicesBucketName)
)
if err := m.UpdateDevice(ctx, info.Name, func(deviceInfo *DeviceInfo) error { if err := getObject(devBucket, name, &device); err != nil {
deviceInfo.State = Faulty return err
return nil }
}); err != nil {
result = multierror.Append(result, err)
}
if err := m.db.Update(func(tx *bolt.Tx) error { device.State = Faulty
return markDeviceID(tx, info.DeviceID, deviceFaulty)
}); err != nil {
result = multierror.Append(result, err)
}
return result if err := putObject(devBucket, name, &device, true); err != nil {
return err
}
return markDeviceID(tx, device.DeviceID, deviceFaulty)
})
} }
// getNextDeviceID finds the next free device ID by taking a cursor // getNextDeviceID finds the next free device ID by taking a cursor

View File

@ -162,7 +162,7 @@ func TestPoolMetadata_MarkFaulty(t *testing.T) {
err := store.AddDevice(testCtx, info) err := store.AddDevice(testCtx, info)
assert.NilError(t, err) assert.NilError(t, err)
err = store.MarkFaulty(testCtx, info) err = store.MarkFaulty(testCtx, "test")
assert.NilError(t, err) assert.NilError(t, err)
saved, err := store.GetDevice(testCtx, info.Name) saved, err := store.GetDevice(testCtx, info.Name)

View File

@ -132,7 +132,7 @@ func (p *PoolDevice) CreateThinDevice(ctx context.Context, deviceName string, vi
if delErr != nil { if delErr != nil {
// Failed to rollback, mark the device as faulty and keep metadata in order to // Failed to rollback, mark the device as faulty and keep metadata in order to
// preserve the faulty device ID // preserve the faulty device ID
retErr = multierror.Append(retErr, delErr, p.metadata.MarkFaulty(ctx, info)) retErr = multierror.Append(retErr, delErr, p.metadata.MarkFaulty(ctx, info.Name))
return return
} }
@ -147,7 +147,7 @@ func (p *PoolDevice) CreateThinDevice(ctx context.Context, deviceName string, vi
// We're unable to create the devmapper device, most likely something wrong with the deviceID // We're unable to create the devmapper device, most likely something wrong with the deviceID
if devErr != nil { if devErr != nil {
retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, info)) retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, info.Name))
return return
} }
}() }()
@ -223,7 +223,7 @@ func (p *PoolDevice) CreateSnapshotDevice(ctx context.Context, deviceName string
if delErr != nil { if delErr != nil {
// Failed to rollback, mark the device as faulty and keep metadata in order to // Failed to rollback, mark the device as faulty and keep metadata in order to
// preserve the faulty device ID // preserve the faulty device ID
retErr = multierror.Append(retErr, delErr, p.metadata.MarkFaulty(ctx, snapInfo)) retErr = multierror.Append(retErr, delErr, p.metadata.MarkFaulty(ctx, snapInfo.Name))
return return
} }
@ -238,7 +238,7 @@ func (p *PoolDevice) CreateSnapshotDevice(ctx context.Context, deviceName string
// We're unable to create the devmapper device, most likely something wrong with the deviceID // We're unable to create the devmapper device, most likely something wrong with the deviceID
if devErr != nil { if devErr != nil {
retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, snapInfo)) retErr = multierror.Append(retErr, p.metadata.MarkFaulty(ctx, snapInfo.Name))
return return
} }
}() }()