Skip to content

Commit

Permalink
store: new API PutLayerFromStagingDirectory
Browse files Browse the repository at this point in the history
Add a race-condition-free alternative to using CreateLayer and
ApplyDiffFromStagingDirectory, ensuring the store is locked for the
entire duration while the layer is being created and populated.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed Feb 12, 2024
1 parent 52420f2 commit 0a76801
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ type metadataStore interface {
rwMetadataStore
}

// PutLayerFromStagingDirectoryOptions contains options to pass to PutLayerFromStagingDirectory
type PutLayerFromStagingDirectoryOptions struct {
ID string // Mandatory
ParentLayer string // Optional
Names []string // Optional
MountLabel string // Optional
Writeable bool // Optional
LayerOptions *LayerOptions // Optional

DiffOutput *drivers.DriverWithDifferOutput //Mandatory
DiffOptions *drivers.ApplyDiffWithDifferOpts //Mandatory
}

// An roBigDataStore wraps up the read-only big-data related methods of the
// various types of file-based lookaside stores that we implement.
type roBigDataStore interface {
Expand Down Expand Up @@ -321,6 +334,11 @@ type Store interface {
// Deprecated: it will be removed soon.
ApplyDiffFromStagingDirectory(to, stagingDirectory string, diffOutput *drivers.DriverWithDifferOutput, options *drivers.ApplyDiffWithDifferOpts) error

// PutLayerFromStagingDirectory combines the functions of CreateLayer and ApplyDiffFromStagingDirectory,
// marking the layer for automatic removal if applying the diff fails
// for any reason.
PutLayerFromStagingDirectory(args PutLayerFromStagingDirectoryOptions) (*Layer, error)

// CleanupStagingDirectory cleanups the staging directory. It can be used to cleanup the staging directory on errors
CleanupStagingDirectory(stagingDirectory string) error

Expand Down Expand Up @@ -2962,6 +2980,35 @@ func (s *store) ApplyDiffFromStagingDirectory(to, stagingDirectory string, diffO
return err
}

func (s *store) PutLayerFromStagingDirectory(args PutLayerFromStagingDirectoryOptions) (*Layer, error) {
rlstore, rlstores, err := s.bothLayerStoreKinds()
if err != nil {
return nil, err
}
if err := rlstore.startWriting(); err != nil {
return nil, err
}
defer rlstore.stopWriting()
if err := s.containerStore.startWriting(); err != nil {
return nil, err
}
defer s.containerStore.stopWriting()

layer, _, err := s.putLayerLocked(rlstore, rlstores, args.ID, args.ParentLayer, args.Names, args.MountLabel, args.Writeable, args.LayerOptions, nil)
if err != nil {
return layer, err
}

if err := rlstore.ApplyDiffFromStagingDirectory(layer.ID, args.DiffOutput, args.DiffOptions); err != nil {
err2 := rlstore.Delete(layer.ID)
if err2 != nil {
err = multierror.Append(err, err2)
}
return nil, err
}
return layer, err
}

func (s *store) CleanupStagingDirectory(stagingDirectory string) error {
_, err := writeToLayerStore(s, func(rlstore rwLayerStore) (struct{}, error) {
return struct{}{}, rlstore.CleanupStagingDirectory(stagingDirectory)
Expand Down

0 comments on commit 0a76801

Please sign in to comment.