Skip to content
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
14 changes: 13 additions & 1 deletion mettle/worker/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
go_library(
name = "worker",
srcs = glob(["*.go"]),
srcs = glob(
["*.go"],
exclude = ["*_test.go"],
),
visibility = ["//mettle/..."],
deps = [
"///third_party/go/cloud.google.com_go_pubsub//apiv1",
Expand Down Expand Up @@ -43,3 +46,12 @@ go_library(
"//third_party/proto/resourceusage",
],
)

go_test(
name = "worker_test",
srcs = glob(["*_test.go"]),
deps = [
":worker",
"///third_party/go/github.com_stretchr_testify//assert",
],
)
42 changes: 42 additions & 0 deletions mettle/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -438,6 +439,7 @@ type worker struct {
fileDownload time.Duration
lastURL string // This is reset somewhat lazily.
stdout, stderr bytes.Buffer
platformProps map[string]string

// For limiting parallelism during download / write actions
limiter, iolimiter chan struct{}
Expand Down Expand Up @@ -477,6 +479,7 @@ func (w *worker) RunTask(ctx context.Context) (*pb.ExecuteResponse, error) {
}
w.currentMsg = nil
w.actionDigest = nil
w.platformProps = nil
return response, err
}

Expand Down Expand Up @@ -517,6 +520,7 @@ func (w *worker) runTask(msg *pubsub.Message) *pb.ExecuteResponse {
Status: status(err, codes.FailedPrecondition, "Badly serialised request: %s", err),
}
}
w.platformProps = copyPlatformProperties(msg.Metadata)
return w.runTaskRequest(req)
}

Expand All @@ -534,6 +538,7 @@ func (w *worker) runTaskRequest(req *pb.ExecuteRequest) *pb.ExecuteResponse {
logr.WithFields(logrus.Fields{
"hash": w.actionDigest.Hash,
}).Debug("Received task for action digest")
w.logPlatformProperties()
w.lastURL = w.actionURL()

action, command, status := w.fetchRequestBlobs(req)
Expand Down Expand Up @@ -562,6 +567,43 @@ func (w *worker) runTaskRequest(req *pb.ExecuteRequest) *pb.ExecuteResponse {
return w.execute(req, action, command)
}

func copyPlatformProperties(metadata map[string]string) map[string]string {
if len(metadata) == 0 {
return nil
}
ret := make(map[string]string, len(metadata))
for k, v := range metadata {
ret[k] = v
}
return ret
}

func formatPlatformProperties(props map[string]string) string {
if len(props) == 0 {
return ""
}
keys := make([]string, 0, len(props))
for key := range props {
keys = append(keys, key)
}
sort.Strings(keys)
formatted := make([]string, 0, len(keys))
for _, key := range keys {
formatted = append(formatted, key+"="+props[key])
}
return strings.Join(formatted, ", ")
}

func (w *worker) logPlatformProperties() {
if len(w.platformProps) == 0 {
return
}
logr.WithFields(logrus.Fields{
"hash": w.actionDigest.Hash,
"platformProperties": formatPlatformProperties(w.platformProps),
}).Info("Action specifies platform properties; would use them to select sandbox filesystem")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: platform properties may be used for many things so I would shorten this just to list the platform properties and not mention the sandbox

}

// forceShutdown sends any shutdown reports and calls log.Fatal() to shut down the worker
func (w *worker) forceShutdown(shutdownMsg string) {
w.Report(false, false, false, "%s", shutdownMsg)
Expand Down
39 changes: 39 additions & 0 deletions mettle/worker/worker_platform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package worker

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCopyPlatformProperties(t *testing.T) {
original := map[string]string{
"filesystem": "disk",
"worker_pool": "large",
}
copied := copyPlatformProperties(original)

assert.Equal(t, original, copied)

original["filesystem"] = "tmpfs"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need this line here?

assert.Equal(t, "disk", copied["filesystem"])
}

func TestCopyPlatformPropertiesNilWhenEmpty(t *testing.T) {
assert.Nil(t, copyPlatformProperties(nil))
assert.Nil(t, copyPlatformProperties(map[string]string{}))
}

func TestFormatPlatformProperties(t *testing.T) {
props := map[string]string{
"worker_pool": "large",
"filesystem": "disk",
}

assert.Equal(t, "filesystem=disk, worker_pool=large", formatPlatformProperties(props))
}

func TestFormatPlatformPropertiesEmpty(t *testing.T) {
assert.Equal(t, "", formatPlatformProperties(nil))
assert.Equal(t, "", formatPlatformProperties(map[string]string{}))
}
Loading