Skip to content

Commit

Permalink
tb, tbw: wire up go-cacher
Browse files Browse the repository at this point in the history
Updates tailscale/go#77
Updates tailscale/tailscale#9841

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
  • Loading branch information
bradfitz committed Oct 23, 2023
1 parent fa96fe6 commit dee60b1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
39 changes: 36 additions & 3 deletions tb/tb.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"sync"
"time"

"github.com/bradfitz/go-tool-cache/cachers"
"github.com/tailscale/tb/fly"
"github.com/tailscale/tb/tb/tbtype"
"tailscale.com/syncs"
Expand Down Expand Up @@ -80,6 +81,14 @@ func main() {
log.Fatalf("state and cache directories must be different")
}

goCacheDir := filepath.Join(*cacheDir, "gocache")
if err := os.MkdirAll(goCacheDir, 0755); err != nil {
log.Fatal(err)
}
goCache := &goCacheServer{
cache: &cachers.DiskCache{Dir: goCacheDir},
}

c := &Controller{
cacheRoot: *cacheDir,
gitDir: filepath.Join(*cacheDir, "git"),
Expand Down Expand Up @@ -128,6 +137,9 @@ func main() {
go func() {
errc <- fmt.Errorf("http.Serve: %w", http.ListenAndServe(":8080", http.HandlerFunc(c.Serve6PN)))
}()
go func() {
errc <- fmt.Errorf("http.Serve(gocache 8081): %w", http.ListenAndServe(":8081", goCache))
}()

log.Fatal(<-errc)
}
Expand Down Expand Up @@ -463,6 +475,7 @@ func (c *Controller) serveStartBuild(w http.ResponseWriter, r *http.Request) {
ref := r.FormValue("ref")
var pkgs []string
pkg := r.FormValue("pkg")
goCache := r.FormValue("gocache") // "rw", "ro", or "" for none
switch pkg {
case "":
pkgs = []string{"tailscale.com/util/lru", "tailscale.com/util/cmpx"}
Expand All @@ -478,12 +491,16 @@ func (c *Controller) serveStartBuild(w http.ResponseWriter, r *http.Request) {
}
}
}
if ref == "main" {
goCache = "rw"
}

run := &Run{
c: c,
id: rands.HexString(32),
createdAt: time.Now(),
pkgs: pkgs,
goCache: goCache,
}

var workerImageCallDone = make(chan error, 1)
Expand Down Expand Up @@ -614,10 +631,22 @@ func (c *WorkerClient) PushTreeFromReader(ctx context.Context, dir string, r io.

}

func cacheServerAddr() string {
return "http://" + os.Getenv("FLY_REGION") + "." + os.Getenv("FLY_APP_NAME") + ".internal:8081"
}

type ExecOpt struct {
GoCache string // "rw", "ro", or ""
}

var zeroOpt = new(ExecOpt)

func (c *WorkerClient) CheckGo(ctx context.Context) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", "http://["+c.m.PrivateIP.String()+"]:8080/check/go-version", nil)
req, err := http.NewRequestWithContext(ctx, "GET", "http://["+c.m.PrivateIP.String()+"]:8080/check/go-version?"+(url.Values{
"cache-server": {cacheServerAddr()},
}).Encode(), nil)
if err != nil {
return "", err
}
Expand All @@ -636,7 +665,10 @@ func (c *WorkerClient) CheckGo(ctx context.Context) (string, error) {
func (c *WorkerClient) Test(ctx context.Context, w io.Writer, pkg string) error {
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", "http://["+c.m.PrivateIP.String()+"]:8080/test/"+pkg, nil)
req, err := http.NewRequestWithContext(ctx, "GET", "http://["+c.m.PrivateIP.String()+"]:8080/test?"+(url.Values{
"pkg": {pkg},
"cache-server": {cacheServerAddr()},
}).Encode(), nil)
if err != nil {
return err
}
Expand All @@ -657,7 +689,8 @@ type Run struct {
ctx context.Context
cancel context.CancelFunc

pkgs []string // TODO: flesh this out into a real task type
pkgs []string // TODO: flesh this out into a real task type
goCache string // "rw", "ro", or "

c *Controller
id string // rand hex
Expand Down
12 changes: 11 additions & 1 deletion tbw/tbw.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -120,7 +121,10 @@ func (w webWriter) Write(p []byte) (n int, err error) {
}

func test(w http.ResponseWriter, r *http.Request) {
pkg := strings.TrimPrefix(r.RequestURI, "/test/")
query := r.URL.Query()
cacheServer := query.Get("cache-server")
cacheServerVerbose, _ := strconv.ParseBool(query.Get("cache-server-verbose"))
pkg := query.Get("pkg")
log.Printf("testing %v ...", pkg)
t0 := time.Now()
cmd := exec.Command(filepath.Join(codeDir, "tool/go"), "test", "-json", "-v")
Expand All @@ -135,7 +139,9 @@ func test(w http.ResponseWriter, r *http.Request) {
cmd.Stderr = &webWriter{2, &mu, je, f}
cmd.Env = append(os.Environ(),
"HOME="+workDir,
fmt.Sprintf("GOCACHEPROG=/usr/local/bin/go-cacher --verbose=%v --cache-server=%s", cacheServerVerbose, cacheServer),
)

for _, v := range r.Header["Test-Env"] {
cmd.Env = append(cmd.Env, v)
}
Expand All @@ -145,12 +151,16 @@ func test(w http.ResponseWriter, r *http.Request) {
}

func toolGoVersion(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
cacheServer := query.Get("cache-server")
cacheServerVerbose, _ := strconv.ParseBool(query.Get("cache-server-verbose"))
cmd := exec.Command(filepath.Join(codeDir, "tool/go"), "version")
cmd.Dir = codeDir
cmd.Stdout = w
cmd.Stderr = w
cmd.Env = append(os.Environ(),
"HOME="+workDir,
fmt.Sprintf("GOCACHEPROG=/usr/local/bin/go-cacher --verbose=%v --cache-server=%s", cacheServerVerbose, cacheServer),
)
cmd.Run()
}
Expand Down

0 comments on commit dee60b1

Please sign in to comment.