Skip to content

Commit

Permalink
Perform file sync outside of lock on Commit
Browse files Browse the repository at this point in the history
Signed-off-by: Xinyang Ge <xinyang.ge@databricks.com>
  • Loading branch information
xinyangge-db committed Apr 24, 2024
1 parent c4c3c6e commit cf2d7cc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/content/content.go
Expand Up @@ -163,6 +163,9 @@ type Writer interface {

// Truncate updates the size of the target blob
Truncate(size int64) error

// Sync flushes the in-flight writes to the disk (when applicable)
Sync() error
}

// Opt is used to alter the mutable properties of content
Expand Down
4 changes: 4 additions & 0 deletions core/content/proxy/content_writer.go
Expand Up @@ -146,3 +146,7 @@ func (rw *remoteWriter) Truncate(size int64) error {
func (rw *remoteWriter) Close() error {
return rw.client.CloseSend()
}

func (rw *remoteWriter) Sync() error {
return nil
}
14 changes: 14 additions & 0 deletions core/metadata/content.go
Expand Up @@ -574,6 +574,13 @@ func (nw *namespacedWriter) Commit(ctx context.Context, size int64, expected dig

var innerErr error

// We pre-sync the in-flight writes to the disk. This avoids the [subsequent fp.Sync() call]
// (https://github.com/containerd/containerd/blob/c4c3c6ea568ce0cfbcf754863abadeea37d77c8f/plugins/content/local/writer.go#L95)
// from taking too long (10s+) while holding the metadata database lock as in the following
// `update` transaction. We intentionally ignore any error on Sync() because it will be
// handled by the subsequent `fp.Sync` anyway.
nw.Sync()

if err := update(ctx, nw.db, func(tx *bolt.Tx) error {
dgst, err := nw.commit(ctx, tx, size, expected, opts...)
if err != nil {
Expand All @@ -599,6 +606,13 @@ func (nw *namespacedWriter) Commit(ctx context.Context, size int64, expected dig
return innerErr
}

func (nw *namespacedWriter) Sync() error {
if nw.w != nil {
return nw.w.Sync()
}
return nil
}

func (nw *namespacedWriter) commit(ctx context.Context, tx *bolt.Tx, size int64, expected digest.Digest, opts ...content.Opt) (digest.Digest, error) {
var base content.Info
for _, opt := range opts {
Expand Down
4 changes: 4 additions & 0 deletions core/remotes/docker/pusher.go
Expand Up @@ -544,6 +544,10 @@ func (pw *pushWriter) Truncate(size int64) error {
return errors.New("cannot truncate remote upload")
}

func (pw *pushWriter) Sync() error {
return nil
}

func requestWithMountFrom(req *request, mount, from string) *request {
creq := *req

Expand Down
8 changes: 8 additions & 0 deletions plugins/content/local/writer.go
Expand Up @@ -206,3 +206,11 @@ func (w *writer) Truncate(size int64) error {
}
return w.fp.Truncate(0)
}

func (w *writer) Sync() error {
if w.fp != nil {
return w.fp.Sync()
}

return nil
}

0 comments on commit cf2d7cc

Please sign in to comment.