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

[ci-matrix] Distribute unknown packages between shards. #339

Open
wants to merge 1 commit into
base: main
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
41 changes: 39 additions & 2 deletions cmd/tool/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package matrix

import (
"bufio"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -187,14 +189,32 @@ func closeFiles(files []*os.File) {
}
}

// consistentThreshold sets a duration of package test run under which packages
// are assigned to buckets in a consistent way instead of using a bucket of
// minium total duration. This helps to avoid situation when many short-running
// or unknown packages are assigned to a single bucket.
const consistentThreshold = 5 * time.Millisecond

func bucketPackages(timing map[string]time.Duration, packages []string, n uint) []bucket {
sort.SliceStable(packages, func(i, j int) bool {
return timing[packages[i]] >= timing[packages[j]]
})

buckets := make([]bucket, n)
var (
buckets = make([]bucket, n)
allBucketsUsed = false
)
for _, pkg := range packages {
i := minBucket(buckets)
var i int
if !allBucketsUsed {
allBucketsUsed = isAllBucketsUsed(buckets)
}
if timing[pkg] > consistentThreshold || !allBucketsUsed {
i = minBucket(buckets)
} else {
i = consistentBucket(pkg, n)
}

buckets[i].Total += timing[pkg]
buckets[i].Packages = append(buckets[i].Packages, pkg)
log.Debugf("adding %v (%v) to bucket %v with total %v",
Expand All @@ -218,6 +238,23 @@ func minBucket(buckets []bucket) int {
return n
}

func isAllBucketsUsed(buckets []bucket) bool {
for _, b := range buckets {
if b.Total == 0 {
return false
}
}
return true
}

func consistentBucket(pkg string, n uint) int {
h := md5.New()
io.WriteString(h, pkg)
hash := fmt.Sprintf("%x", h.Sum(nil))
decimal, _ := strconv.ParseUint(hash[:10], 16, 64)
return int(decimal) % int(n)
Comment on lines +249 to +255
Copy link

Choose a reason for hiding this comment

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

Maybe use maphash.Hash{} ?

Suggested change
func consistentBucket(pkg string, n uint) int {
h := md5.New()
io.WriteString(h, pkg)
hash := fmt.Sprintf("%x", h.Sum(nil))
decimal, _ := strconv.ParseUint(hash[:10], 16, 64)
return int(decimal) % int(n)
var hashSeed = maphash.MakeSeed()
func consistentBucket(pkg string, n uint) int {
return int(maphash.String(hashSeed, str)) % int(n)

Maphash was designed specifically for this case.

Props:

  1. fastest hash from standart library.
  2. zero allocation.

Thanks!

}

type bucket struct {
Total time.Duration
Packages []string
Expand Down
23 changes: 12 additions & 11 deletions cmd/tool/matrix/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestBucketPackages(t *testing.T) {
}

run := func(t *testing.T, tc testCase) {
t.Helper()
buckets := bucketPackages(timing, packages, tc.n)
assert.DeepEqual(t, buckets, tc.expected)
}
Expand All @@ -75,25 +76,25 @@ func TestBucketPackages(t *testing.T) {
{
n: 2,
expected: []bucket{
0: {Total: 4440 * ms, Packages: []string{"four", "two", "one", "five"}},
1: {Total: 4406 * ms, Packages: []string{"three", "six", "new2", "new1"}},
0: {Total: 4440 * ms, Packages: []string{"four", "two", "one", "five", "new2", "new1"}},
1: {Total: 4406 * ms, Packages: []string{"three", "six"}},
},
},
{
n: 3,
expected: []bucket{
0: {Total: 4000 * ms, Packages: []string{"four"}},
1: {Total: 3800 * ms, Packages: []string{"three"}},
2: {Total: 1046 * ms, Packages: []string{"six", "two", "one", "five", "new1", "new2"}},
1: {Total: 3800 * ms, Packages: []string{"three", "new1"}},
2: {Total: 1046 * ms, Packages: []string{"six", "two", "one", "five", "new2"}},
},
},
{
n: 4,
expected: []bucket{
0: {Total: 4000 * ms, Packages: []string{"four"}},
0: {Total: 4000 * ms, Packages: []string{"four", "new1"}},
1: {Total: 3800 * ms, Packages: []string{"three"}},
2: {Total: 606 * ms, Packages: []string{"six"}},
3: {Total: 440 * ms, Packages: []string{"two", "one", "five", "new2", "new1"}},
2: {Total: 606 * ms, Packages: []string{"six", "new2"}},
3: {Total: 440 * ms, Packages: []string{"two", "one", "five"}},
},
},
{
Expand Down Expand Up @@ -232,16 +233,16 @@ var expectedMatrix = `{
"packages": "pkg2"
},
{
"description": "1 - pkg1",
"description": "1 - pkg1 and 1 others",
"estimatedRuntime": "4s",
"id": 1,
"packages": "pkg1"
"packages": "pkg1 other"
},
{
"description": "2 - pkg0 and 1 others",
"description": "2 - pkg0",
"estimatedRuntime": "2s",
"id": 2,
"packages": "pkg0 other"
"packages": "pkg0"
}
]
}`
Expand Down