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 <[email protected]>
  • Loading branch information
giuseppe committed Feb 12, 2024
1 parent b4abb43 commit d190380
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ type metadataStore interface {
rwMetadataStore
}

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

StagingDirectory string
DiffOutput *drivers.DriverWithDifferOutput
DiffOptions *drivers.ApplyDiffWithDifferOpts
}

// 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 @@ -320,6 +334,11 @@ type Store interface {
// ApplyDiffFromStagingDirectory uses stagingDirectory to create the diff.
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 @@ -2958,6 +2977,32 @@ 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.StagingDirectory, args.DiffOutput, args.DiffOptions); err != nil {
rlstore.Delete(layer.ID)
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 d190380

Please sign in to comment.