Skip to content

Commit d4e4f4e

Browse files
committed
buildlet: add context argument to most of the remaining Client methods
(And update all the callers in the tree.) Updates golang/go#35707 Change-Id: I54769bbe374f31ae1dd07776b27818db91ce8c70 Reviewed-on: https://go-review.googlesource.com/c/build/+/208157 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent cc0e72b commit d4e4f4e

File tree

14 files changed

+127
-111
lines changed

14 files changed

+127
-111
lines changed

buildlet/buildletclient.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (c *Client) heartbeatLoop() {
323323
return
324324
case <-time.After(10 * time.Second):
325325
t0 := time.Now()
326-
if _, err := c.Status(); err != nil {
326+
if _, err := c.Status(context.Background()); err != nil {
327327
failInARow++
328328
if failInARow == 3 {
329329
log.Printf("Buildlet %v failed three heartbeats; final error: %v", c, err)
@@ -406,20 +406,20 @@ func (c *Client) doOK(req *http.Request) error {
406406
// If dir is empty, they're placed at the root of the buildlet's work directory.
407407
// The dir is created if necessary.
408408
// The Reader must be of a tar.gz file.
409-
func (c *Client) PutTar(r io.Reader, dir string) error {
409+
func (c *Client) PutTar(ctx context.Context, r io.Reader, dir string) error {
410410
req, err := http.NewRequest("PUT", c.URL()+"/writetgz?dir="+url.QueryEscape(dir), r)
411411
if err != nil {
412412
return err
413413
}
414-
return c.doOK(req)
414+
return c.doOK(req.WithContext(ctx))
415415
}
416416

417417
// PutTarFromURL tells the buildlet to download the tar.gz file from tarURL
418418
// and write it to dir, a relative directory from the workdir.
419419
// If dir is empty, they're placed at the root of the buildlet's work directory.
420420
// The dir is created if necessary.
421421
// The url must be of a tar.gz file.
422-
func (c *Client) PutTarFromURL(tarURL, dir string) error {
422+
func (c *Client) PutTarFromURL(ctx context.Context, tarURL, dir string) error {
423423
form := url.Values{
424424
"url": {tarURL},
425425
}
@@ -428,11 +428,11 @@ func (c *Client) PutTarFromURL(tarURL, dir string) error {
428428
return err
429429
}
430430
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
431-
return c.doOK(req)
431+
return c.doOK(req.WithContext(ctx))
432432
}
433433

434434
// Put writes the provided file to path (relative to workdir) and sets mode.
435-
func (c *Client) Put(r io.Reader, path string, mode os.FileMode) error {
435+
func (c *Client) Put(ctx context.Context, r io.Reader, path string, mode os.FileMode) error {
436436
param := url.Values{
437437
"path": {path},
438438
"mode": {fmt.Sprint(int64(mode))},
@@ -441,7 +441,7 @@ func (c *Client) Put(r io.Reader, path string, mode os.FileMode) error {
441441
if err != nil {
442442
return err
443443
}
444-
return c.doOK(req)
444+
return c.doOK(req.WithContext(ctx))
445445
}
446446

447447
// GetTar returns a .tar.gz stream of the given directory, relative to the buildlet's work dir.
@@ -614,7 +614,7 @@ func (c *Client) Exec(ctx context.Context, cmd string, opts ExecOpts) (remoteErr
614614
}
615615

616616
// RemoveAll deletes the provided paths, relative to the work directory.
617-
func (c *Client) RemoveAll(paths ...string) error {
617+
func (c *Client) RemoveAll(ctx context.Context, paths ...string) error {
618618
if len(paths) == 0 {
619619
return nil
620620
}
@@ -624,7 +624,7 @@ func (c *Client) RemoveAll(paths ...string) error {
624624
return err
625625
}
626626
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
627-
return c.doOK(req)
627+
return c.doOK(req.WithContext(ctx))
628628
}
629629

630630
// DestroyVM shuts down the buildlet and destroys the VM instance.
@@ -681,7 +681,7 @@ type Status struct {
681681
}
682682

683683
// Status returns an Status value describing this buildlet.
684-
func (c *Client) Status() (Status, error) {
684+
func (c *Client) Status(ctx context.Context) (Status, error) {
685685
select {
686686
case <-c.peerDead:
687687
return Status{}, c.deadErr
@@ -692,6 +692,7 @@ func (c *Client) Status() (Status, error) {
692692
if err != nil {
693693
return Status{}, err
694694
}
695+
req = req.WithContext(ctx)
695696
resp, err := c.doHeaderTimeout(req, 10*time.Second) // plenty of time
696697
if err != nil {
697698
return Status{}, err
@@ -712,11 +713,12 @@ func (c *Client) Status() (Status, error) {
712713
}
713714

714715
// WorkDir returns the absolute path to the buildlet work directory.
715-
func (c *Client) WorkDir() (string, error) {
716+
func (c *Client) WorkDir(ctx context.Context) (string, error) {
716717
req, err := http.NewRequest("GET", c.URL()+"/workdir", nil)
717718
if err != nil {
718719
return "", err
719720
}
721+
req = req.WithContext(ctx)
720722
resp, err := c.doHeaderTimeout(req, 10*time.Second) // plenty of time
721723
if err != nil {
722724
return "", err
@@ -800,7 +802,7 @@ type ListDirOpts struct {
800802
// ListDir lists the contents of a directory.
801803
// The fn callback is run for each entry.
802804
// The directory dir itself is not included.
803-
func (c *Client) ListDir(dir string, opts ListDirOpts, fn func(DirEntry)) error {
805+
func (c *Client) ListDir(ctx context.Context, dir string, opts ListDirOpts, fn func(DirEntry)) error {
804806
param := url.Values{
805807
"dir": {dir},
806808
"recursive": {fmt.Sprint(opts.Recursive)},
@@ -811,7 +813,7 @@ func (c *Client) ListDir(dir string, opts ListDirOpts, fn func(DirEntry)) error
811813
if err != nil {
812814
return err
813815
}
814-
resp, err := c.do(req)
816+
resp, err := c.do(req.WithContext(ctx))
815817
if err != nil {
816818
return err
817819
}

cmd/coordinator/coordinator.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ func (st *buildStatus) build() error {
18661866

18671867
if st.useSnapshot() {
18681868
sp := st.CreateSpan("write_snapshot_tar")
1869-
if err := bc.PutTarFromURL(st.SnapshotURL(buildEnv), "go"); err != nil {
1869+
if err := bc.PutTarFromURL(st.ctx, st.SnapshotURL(buildEnv), "go"); err != nil {
18701870
return sp.Done(fmt.Errorf("failed to put snapshot to buildlet: %v", err))
18711871
}
18721872
sp.Done(nil)
@@ -2217,7 +2217,7 @@ func (st *buildStatus) writeGoSource() error {
22172217
func (st *buildStatus) writeGoSourceTo(bc *buildlet.Client) error {
22182218
// Write the VERSION file.
22192219
sp := st.CreateSpan("write_version_tar")
2220-
if err := bc.PutTar(buildgo.VersionTgz(st.Rev), "go"); err != nil {
2220+
if err := bc.PutTar(st.ctx, buildgo.VersionTgz(st.Rev), "go"); err != nil {
22212221
return sp.Done(fmt.Errorf("writing VERSION tgz: %v", err))
22222222
}
22232223

@@ -2226,7 +2226,7 @@ func (st *buildStatus) writeGoSourceTo(bc *buildlet.Client) error {
22262226
return err
22272227
}
22282228
sp = st.CreateSpan("write_go_src_tar")
2229-
if err := bc.PutTar(srcTar, "go"); err != nil {
2229+
if err := bc.PutTar(st.ctx, srcTar, "go"); err != nil {
22302230
return sp.Done(fmt.Errorf("writing tarball from Gerrit: %v", err))
22312231
}
22322232
return sp.Done(nil)
@@ -2239,12 +2239,12 @@ func (st *buildStatus) writeBootstrapToolchain() error {
22392239
}
22402240
const bootstrapDir = "go1.4" // might be newer; name is the default
22412241
sp := st.CreateSpan("write_go_bootstrap_tar")
2242-
return sp.Done(st.bc.PutTarFromURL(u, bootstrapDir))
2242+
return sp.Done(st.bc.PutTarFromURL(st.ctx, u, bootstrapDir))
22432243
}
22442244

22452245
func (st *buildStatus) cleanForSnapshot(bc *buildlet.Client) error {
22462246
sp := st.CreateSpan("clean_for_snapshot")
2247-
return sp.Done(bc.RemoveAll(
2247+
return sp.Done(bc.RemoveAll(st.ctx,
22482248
"go/doc/gopher",
22492249
"go/pkg/bootstrap",
22502250
))
@@ -2257,7 +2257,7 @@ func (st *buildStatus) writeSnapshot(bc *buildlet.Client) (err error) {
22572257
// a couple times at 1 minute. Some buildlets might be far
22582258
// away on the network, so be more lenient. The timeout mostly
22592259
// is here to prevent infinite hangs.
2260-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
2260+
ctx, cancel := context.WithTimeout(st.ctx, 5*time.Minute)
22612261
defer cancel()
22622262

22632263
tsp := st.CreateSpan("fetch_snapshot_reader_from_buildlet")
@@ -2295,7 +2295,7 @@ func (st *buildStatus) reportErr(err error) {
22952295
}
22962296

22972297
func (st *buildStatus) distTestList() (names []string, remoteErr, err error) {
2298-
workDir, err := st.bc.WorkDir()
2298+
workDir, err := st.bc.WorkDir(st.ctx)
22992299
if err != nil {
23002300
err = fmt.Errorf("distTestList, WorkDir: %v", err)
23012301
return
@@ -2441,7 +2441,7 @@ func getTestStats(sl spanlog.Logger) *buildstats.TestStats {
24412441
func (st *buildStatus) runSubrepoTests() (remoteErr, err error) {
24422442
st.LogEventTime("fetching_subrepo", st.SubName)
24432443

2444-
workDir, err := st.bc.WorkDir()
2444+
workDir, err := st.bc.WorkDir(st.ctx)
24452445
if err != nil {
24462446
err = fmt.Errorf("error discovering workdir for helper %s: %v", st.bc.IPPort(), err)
24472447
return nil, err
@@ -2451,7 +2451,7 @@ func (st *buildStatus) runSubrepoTests() (remoteErr, err error) {
24512451

24522452
// Check out the provided sub-repo to the buildlet's workspace.
24532453
// Need to do this first, so we can run go env GOMOD in it.
2454-
err = buildgo.FetchSubrepo(st, st.bc, st.SubName, st.SubRev)
2454+
err = buildgo.FetchSubrepo(st.ctx, st, st.bc, st.SubName, st.SubRev)
24552455
if err != nil {
24562456
return nil, err
24572457
}
@@ -2492,7 +2492,7 @@ func (st *buildStatus) runSubrepoTests() (remoteErr, err error) {
24922492
// Look for inner modules, in order to test them too. See golang.org/issue/32528.
24932493
repoPath := importPathOfRepo(st.SubName)
24942494
sp := st.CreateSpan("listing_subrepo_modules", st.SubName)
2495-
err = st.bc.ListDir("gopath/src/"+repoPath, buildlet.ListDirOpts{Recursive: true}, func(e buildlet.DirEntry) {
2495+
err = st.bc.ListDir(st.ctx, "gopath/src/"+repoPath, buildlet.ListDirOpts{Recursive: true}, func(e buildlet.DirEntry) {
24962496
goModFile := path.Base(e.Name()) == "go.mod" && !e.IsDir()
24972497
if !goModFile {
24982498
return
@@ -2645,7 +2645,7 @@ func (st *buildStatus) fetchDependenciesToGOPATHWorkspace(goroot, gopath string)
26452645
// fetch checks out the provided sub-repo to the buildlet's workspace.
26462646
fetch := func(repo, rev string) error {
26472647
fetched[repo] = true
2648-
return buildgo.FetchSubrepo(st, st.bc, repo, rev)
2648+
return buildgo.FetchSubrepo(st.ctx, st, st.bc, repo, rev)
26492649
}
26502650

26512651
// findDeps uses 'go list' on the checked out repo to find its
@@ -2855,7 +2855,7 @@ func (st *buildStatus) runTests(helpers <-chan *buildlet.Client) (remoteErr, err
28552855
if rev == "" {
28562856
rev = "master" // should happen rarely; ok if it does.
28572857
}
2858-
b, err := st.goBuilder().EnumerateBenchmarks(st.bc, rev, st.trySet.affectedPkgs())
2858+
b, err := st.goBuilder().EnumerateBenchmarks(st.ctx, st.bc, rev, st.trySet.affectedPkgs())
28592859
sp.Done(err)
28602860
if err == nil {
28612861
benches = b
@@ -2871,7 +2871,7 @@ func (st *buildStatus) runTests(helpers <-chan *buildlet.Client) (remoteErr, err
28712871
st.LogEventTime("starting_tests", fmt.Sprintf("%d tests", len(set.items)))
28722872
startTime := time.Now()
28732873

2874-
workDir, err := st.bc.WorkDir()
2874+
workDir, err := st.bc.WorkDir(st.ctx)
28752875
if err != nil {
28762876
return nil, fmt.Errorf("error discovering workdir for main buildlet, %s: %v", st.bc.Name(), err)
28772877
}
@@ -2916,11 +2916,11 @@ func (st *buildStatus) runTests(helpers <-chan *buildlet.Client) (remoteErr, err
29162916
defer st.LogEventTime("DEV_HELPER_SLEEP", bc.Name())
29172917
}
29182918
st.LogEventTime("got_empty_test_helper", bc.String())
2919-
if err := bc.PutTarFromURL(st.SnapshotURL(buildEnv), "go"); err != nil {
2919+
if err := bc.PutTarFromURL(st.ctx, st.SnapshotURL(buildEnv), "go"); err != nil {
29202920
log.Printf("failed to extract snapshot for helper %s: %v", bc.Name(), err)
29212921
return
29222922
}
2923-
workDir, err := bc.WorkDir()
2923+
workDir, err := bc.WorkDir(st.ctx)
29242924
if err != nil {
29252925
log.Printf("error discovering workdir for helper %s: %v", bc.Name(), err)
29262926
return

cmd/coordinator/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ func handleIncomingSSHPostAuth(s ssh.Session) {
628628
err = <-errc
629629
}()
630630
}
631-
workDir, err := rb.buildlet.WorkDir()
631+
workDir, err := rb.buildlet.WorkDir(ctx)
632632
if err != nil {
633633
fmt.Fprintf(s, "Error getting WorkDir: %v\n", err)
634634
return

cmd/coordinator/reverse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (p *reverseBuildletPool) healthCheckBuildlet(b *reverseBuildlet) bool {
250250
b.inUseTime = time.Now()
251251
res := make(chan error, 1)
252252
go func() {
253-
_, err := b.client.Status()
253+
_, err := b.client.Status(context.Background())
254254
res <- err
255255
}()
256256
p.mu.Unlock()
@@ -328,7 +328,7 @@ func (p *reverseBuildletPool) GetBuildlet(ctx context.Context, hostType string,
328328
func (p *reverseBuildletPool) cleanedBuildlet(b *buildlet.Client, lg logger) (*buildlet.Client, error) {
329329
// Clean up any files from previous builds.
330330
sp := lg.CreateSpan("clean_buildlet", b.String())
331-
err := b.RemoveAll(".")
331+
err := b.RemoveAll(context.Background(), ".")
332332
sp.Done(err)
333333
if err != nil {
334334
b.Close()
@@ -598,7 +598,7 @@ func handleReverse(w http.ResponseWriter, r *http.Request) {
598598
}
599599
}()
600600
tstatus := time.Now()
601-
status, err := client.Status()
601+
status, err := client.Status(context.Background())
602602
if err != nil {
603603
log.Printf("Reverse connection %s/%s for %s did not answer status after %v: %v",
604604
hostname, r.RemoteAddr, hostType, time.Since(tstatus), err)

cmd/debugnewvm/debugnewvm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func main() {
124124
if err != nil {
125125
log.Fatalf("StartNewVM: %v", err)
126126
}
127-
dir, err := bc.WorkDir()
127+
dir, err := bc.WorkDir(ctx)
128128
log.Printf("WorkDir: %v, %v", dir, err)
129129

130130
if *sleepSec > 0 {
@@ -146,7 +146,7 @@ func main() {
146146
if u := bconf.GoBootstrapURL(env); u != "" {
147147
log.Printf("Pushing 'go1.4' Go bootstrap dir ...")
148148
const bootstrapDir = "go1.4" // might be newer; name is the default
149-
if err := bc.PutTarFromURL(u, bootstrapDir); err != nil {
149+
if err := bc.PutTarFromURL(ctx, u, bootstrapDir); err != nil {
150150
bc.Close()
151151
log.Fatalf("Putting Go bootstrap: %v", err)
152152
}
@@ -155,13 +155,13 @@ func main() {
155155
// Push Go code
156156
log.Printf("Pushing 'go' dir...")
157157
goTarGz := "https://go.googlesource.com/go/+archive/" + *buildRev + ".tar.gz"
158-
if err := bc.PutTarFromURL(goTarGz, "go"); err != nil {
158+
if err := bc.PutTarFromURL(ctx, goTarGz, "go"); err != nil {
159159
bc.Close()
160160
log.Fatalf("Putting go code: %v", err)
161161
}
162162

163163
// Push a synthetic VERSION file to prevent git usage:
164-
if err := bc.PutTar(buildgo.VersionTgz(*buildRev), "go"); err != nil {
164+
if err := bc.PutTar(ctx, buildgo.VersionTgz(*buildRev), "go"); err != nil {
165165
bc.Close()
166166
log.Fatalf("Putting VERSION file: %v", err)
167167
}

cmd/gomote/ls.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
8+
"context"
89
"flag"
910
"fmt"
1011
"os"
@@ -44,7 +45,7 @@ func ls(args []string) error {
4445
Digest: digest,
4546
Skip: strings.Split(skip, ","),
4647
}
47-
return bc.ListDir(dir, opts, func(bi buildlet.DirEntry) {
48+
return bc.ListDir(context.Background(), dir, opts, func(bi buildlet.DirEntry) {
4849
fmt.Fprintf(os.Stdout, "%s\n", bi)
4950
})
5051
}

cmd/gomote/ping.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
8+
"context"
89
"flag"
910
"fmt"
1011
"os"
@@ -29,13 +30,14 @@ func ping(args []string) error {
2930
if err != nil {
3031
return err
3132
}
32-
wd, err := bc.WorkDir()
33+
ctx := context.Background()
34+
wd, err := bc.WorkDir(ctx)
3335
if err != nil {
3436
return err
3537
}
3638
if status {
3739
fmt.Printf("workdir: %v\n", wd)
38-
s, err := bc.Status()
40+
s, err := bc.Status(ctx)
3941
if err != nil {
4042
return err
4143
}

0 commit comments

Comments
 (0)