Skip to content

Commit

Permalink
[Disk Manager] fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2411 committed Nov 26, 2024
1 parent 37a847d commit b66e1d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
28 changes: 13 additions & 15 deletions cloud/disk_manager/internal/pkg/resources/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,28 +311,26 @@ func (s *storageYDB) createImage(
image ImageMeta,
) (ImageMeta, error) {

var emptyImageMeta ImageMeta

tx, err := session.BeginRWTransaction(ctx)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

defer tx.Rollback(ctx)

// HACK: see NBS-974 for details.
snapshotExists, err := s.snapshotExists(ctx, tx, image.ID)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

if snapshotExists {
err = tx.Commit(ctx)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

return emptyImageMeta, errors.NewNonCancellableErrorf(
return ImageMeta{}, errors.NewNonCancellableErrorf(
"image with id %v can't be created, because snapshot with id %v already exists",
image.ID,
image.ID,
Expand All @@ -341,7 +339,7 @@ func (s *storageYDB) createImage(

createRequest, err := proto.Marshal(image.CreateRequest)
if err != nil {
return emptyImageMeta, errors.NewNonRetriableErrorf(
return ImageMeta{}, errors.NewNonRetriableErrorf(
"failed to marshal create request for image with id %v: %w",
image.ID,
err,
Expand All @@ -360,27 +358,27 @@ func (s *storageYDB) createImage(
persistence.ValueParam("$id", persistence.UTF8Value(image.ID)),
)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

defer res.Close()

states, err := scanImageStates(ctx, res)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

if len(states) != 0 {
err = tx.Commit(ctx)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

state := states[0]

if state.status >= imageStatusDeleting {
logging.Info(ctx, "can't create already deleting/deleted image with id %v", image.ID)
return emptyImageMeta, errors.NewSilentNonRetriableErrorf(
return ImageMeta{}, errors.NewSilentNonRetriableErrorf(
"can't create already deleting/deleted image with id %v",
image.ID,
)
Expand All @@ -394,7 +392,7 @@ func (s *storageYDB) createImage(
return *state.toImageMeta(), nil
}

return emptyImageMeta, errors.NewNonCancellableErrorf(
return ImageMeta{}, errors.NewNonCancellableErrorf(
"image with different params already exists, old=%v, new=%v",
state,
image,
Expand Down Expand Up @@ -425,7 +423,7 @@ func (s *storageYDB) createImage(
case nil:
state.encryptionKeyHash = nil
default:
return emptyImageMeta, errors.NewNonRetriableErrorf(
return ImageMeta{}, errors.NewNonRetriableErrorf(
"unknown key %s",
key,
)
Expand All @@ -447,12 +445,12 @@ func (s *storageYDB) createImage(
persistence.ValueParam("$states", persistence.ListValue(state.structValue())),
)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

err = tx.Commit(ctx)
if err != nil {
return emptyImageMeta, err
return ImageMeta{}, err
}

return *state.toImageMeta(), nil
Expand Down
28 changes: 13 additions & 15 deletions cloud/disk_manager/internal/pkg/resources/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,25 @@ func (s *storageYDB) createSnapshot(
snapshot SnapshotMeta,
) (SnapshotMeta, error) {

var emptySnapshotMeta SnapshotMeta

tx, err := session.BeginRWTransaction(ctx)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}
defer tx.Rollback(ctx)

// HACK: see NBS-974 for details.
imageExists, err := s.imageExists(ctx, tx, snapshot.ID)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}

if imageExists {
err = tx.Commit(ctx)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}

return emptySnapshotMeta, errors.NewNonCancellableErrorf(
return SnapshotMeta{}, errors.NewNonCancellableErrorf(
"snapshot with id %v can't be created, because image with id %v already exists",
snapshot.ID,
snapshot.ID,
Expand All @@ -327,7 +325,7 @@ func (s *storageYDB) createSnapshot(

createRequest, err := proto.Marshal(snapshot.CreateRequest)
if err != nil {
return emptySnapshotMeta, errors.NewNonRetriableErrorf(
return SnapshotMeta{}, errors.NewNonRetriableErrorf(
"failed to marshal create request for snapshot with id %v: %w",
snapshot.ID,
err,
Expand All @@ -346,26 +344,26 @@ func (s *storageYDB) createSnapshot(
persistence.ValueParam("$id", persistence.UTF8Value(snapshot.ID)),
)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}
defer res.Close()

states, err := scanSnapshotStates(ctx, res)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}

if len(states) != 0 {
err = tx.Commit(ctx)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}

state := states[0]

if state.status >= snapshotStatusDeleting {
logging.Info(ctx, "can't create already deleting/deleted snapshot with id %v", snapshot.ID)
return emptySnapshotMeta, errors.NewSilentNonRetriableErrorf(
return SnapshotMeta{}, errors.NewSilentNonRetriableErrorf(
"can't create already deleting/deleted snapshot with id %v",
snapshot.ID,
)
Expand All @@ -379,7 +377,7 @@ func (s *storageYDB) createSnapshot(
return *state.toSnapshotMeta(), nil
}

return emptySnapshotMeta, errors.NewNonCancellableErrorf(
return SnapshotMeta{}, errors.NewNonCancellableErrorf(
"snapshot with different params already exists, old=%v, new=%v",
state,
snapshot,
Expand Down Expand Up @@ -410,7 +408,7 @@ func (s *storageYDB) createSnapshot(
case nil:
state.encryptionKeyHash = nil
default:
return emptySnapshotMeta, errors.NewNonRetriableErrorf(
return SnapshotMeta{}, errors.NewNonRetriableErrorf(
"unknown key %s",
key,
)
Expand All @@ -432,12 +430,12 @@ func (s *storageYDB) createSnapshot(
persistence.ValueParam("$states", persistence.ListValue(state.structValue())),
)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}

err = tx.Commit(ctx)
if err != nil {
return emptySnapshotMeta, err
return SnapshotMeta{}, err
}

return *state.toSnapshotMeta(), nil
Expand Down

0 comments on commit b66e1d7

Please sign in to comment.