Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snapshots): choose numCPU if less than max parallel value #3069

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions snapshot/snapshotfs/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,11 @@ func (u *Uploader) maybeIgnoreCachedEntry(ctx context.Context, ent fs.Entry) fs.

func (u *Uploader) effectiveParallelFileReads(pol *policy.Policy) int {
p := u.ParallelUploads

if p > runtime.NumCPU() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that uploads are not entirely CPU bound there may be benefits to allowing p to be greater than the number of cores to allow max number of cores to do hashing while others are waiting for uploads

p = runtime.NumCPU()
}

if p > 0 {
// command-line override takes precedence.
return p
Expand Down
65 changes: 63 additions & 2 deletions snapshot/snapshotfs/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"runtime/debug"
"sort"
"strings"
Expand Down Expand Up @@ -688,7 +689,7 @@ func TestUploadWithCheckpointing(t *testing.T) {
}
}

func TestParallelUploadUploadsBlobsInParallel(t *testing.T) {
func TestParallelUploadUploadsBlobsInParallelOverNumCPU(t *testing.T) {
ctx := testlogging.Context(t)
th := newUploadTestHarness(ctx, t)

Expand Down Expand Up @@ -718,7 +719,67 @@ func TestParallelUploadUploadsBlobsInParallel(t *testing.T) {

policyTree := policy.BuildTree(nil, policy.DefaultPolicy)

require.Equal(t, 13, u.effectiveParallelFileReads(policyTree.EffectivePolicy()))
n := runtime.NumCPU()

require.Equal(t, n, u.effectiveParallelFileReads(policyTree.EffectivePolicy()))

si := snapshot.SourceInfo{
UserName: "user",
Host: "host",
Path: "path",
}

// add a bunch of very large files which can be hashed in parallel and will trigger parallel
// uploads
th.sourceDir.AddFile("d1/large1", randomBytes(1e7), defaultPermissions)
th.sourceDir.AddFile("d1/large2", randomBytes(2e7), defaultPermissions)
th.sourceDir.AddFile("d1/large3", randomBytes(2e7), defaultPermissions)
th.sourceDir.AddFile("d1/large4", randomBytes(1e7), defaultPermissions)

th.sourceDir.AddFile("d2/large1", randomBytes(1e7), defaultPermissions)
th.sourceDir.AddFile("d2/large2", randomBytes(1e7), defaultPermissions)
th.sourceDir.AddFile("d2/large3", randomBytes(1e7), defaultPermissions)
th.sourceDir.AddFile("d2/large4", randomBytes(1e7), defaultPermissions)

_, err := u.Upload(ctx, th.sourceDir, policyTree, si)
require.NoError(t, err)

require.NoError(t, th.repo.Flush(ctx))

require.Greater(t, maxParallelCalls.Load(), int32(0))
}

func TestParallelUploadUploadsBlobsInParallelWithinNumCPU(t *testing.T) {
ctx := testlogging.Context(t)
th := newUploadTestHarness(ctx, t)

u := NewUploader(th.repo)
u.ParallelUploads = 2

// no faults for first blob write - session marker.
th.faulty.AddFault(blobtesting.MethodPutBlob)

var currentParallelCalls, maxParallelCalls atomic.Int32

// measure concurrency of PutBlob calls
th.faulty.AddFault(blobtesting.MethodPutBlob).Repeat(10).Before(func() {
v := currentParallelCalls.Add(1)
max := maxParallelCalls.Load()
if v > max {
maxParallelCalls.CompareAndSwap(max, v)
}

time.Sleep(100 * time.Millisecond)
currentParallelCalls.Add(-1)
})

// create a channel that will be sent to whenever checkpoint completes.
u.checkpointFinished = make(chan struct{})
u.disableEstimation = true

policyTree := policy.BuildTree(nil, policy.DefaultPolicy)

require.Equal(t, 2, u.effectiveParallelFileReads(policyTree.EffectivePolicy()))

si := snapshot.SourceInfo{
UserName: "user",
Expand Down