Skip to content

Commit

Permalink
Merge pull request #10128 from xinyangge-db/lockless_sync
Browse files Browse the repository at this point in the history
Perform file sync outside of lock on Commit
  • Loading branch information
mxpv committed May 1, 2024
2 parents 0aa610c + 4167416 commit 2ec82c4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/content/content.go
Expand Up @@ -165,6 +165,11 @@ type Writer interface {
Truncate(size int64) error
}

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

// Opt is used to alter the mutable properties of content
type Opt func(*Info) error

Expand Down
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 syncer, ok := nw.w.(content.Syncer); ok {
return syncer.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
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 2ec82c4

Please sign in to comment.