Skip to content

Commit 8ab0cad

Browse files
findleyrprattmic
authored andcommitted
[internal-branch.go1.23-vendor] internal/upload: only download the upload config if the mode is "on"
This is a second, simpler approach to addressing golang/go#68946, after CL 607796 was deemed too complicated (and contained a bug!). Simply check the mode file before downloading the upload config. The test from CL 607796 are preserved. For golang/go#68946 Change-Id: I1f11252456df2471e1eafe34e72ca9e369ff8e6a Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/609275 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> (cherry picked from commit a797f33) Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/609137 Reviewed-by: Michael Pratt <[email protected]>
1 parent 0b706e1 commit 8ab0cad

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

internal/configstore/download.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os"
1717
"os/exec"
1818
"path/filepath"
19+
"sync/atomic"
1920

2021
"golang.org/x/telemetry/internal/telemetry"
2122
)
@@ -29,12 +30,22 @@ const (
2930
// creation flag.
3031
var needNoConsole = func(cmd *exec.Cmd) {}
3132

33+
var downloads int64
34+
35+
// Downloads reports, for testing purposes, the number of times [Download] has
36+
// been called.
37+
func Downloads() int64 {
38+
return atomic.LoadInt64(&downloads)
39+
}
40+
3241
// Download fetches the requested telemetry UploadConfig using "go mod
3342
// download". If envOverlay is provided, it is appended to the environment used
3443
// for invoking the go command.
3544
//
3645
// The second result is the canonical version of the requested configuration.
3746
func Download(version string, envOverlay []string) (*telemetry.UploadConfig, string, error) {
47+
atomic.AddInt64(&downloads, 1)
48+
3849
if version == "" {
3950
version = "latest"
4051
}

internal/upload/run.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,24 @@ func newUploader(rcfg RunConfig) (*uploader, error) {
112112
logger := log.New(logWriter, "", log.Ltime|log.Lmicroseconds|log.Lshortfile)
113113

114114
// Fetch the upload config, if it is not provided.
115-
config, configVersion, err := configstore.Download("latest", rcfg.Env)
116-
if err != nil {
117-
return nil, err
115+
var (
116+
config *telemetry.UploadConfig
117+
configVersion string
118+
)
119+
120+
if mode, _ := dir.Mode(); mode == "on" {
121+
// golang/go#68946: only download the upload config if it will be used.
122+
//
123+
// TODO(rfindley): This is a narrow change aimed at minimally fixing the
124+
// associated bug. In the future, we should read the mode only once during
125+
// the upload process.
126+
config, configVersion, err = configstore.Download("latest", rcfg.Env)
127+
if err != nil {
128+
return nil, err
129+
}
130+
} else {
131+
config = &telemetry.UploadConfig{}
132+
configVersion = "v0.0.0-0"
118133
}
119134

120135
// Set the start time, if it is not provided.

internal/upload/run_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"golang.org/x/telemetry/counter"
23+
"golang.org/x/telemetry/internal/configstore"
2324
"golang.org/x/telemetry/internal/configtest"
2425
"golang.org/x/telemetry/internal/regtest"
2526
"golang.org/x/telemetry/internal/telemetry"
@@ -430,12 +431,13 @@ func TestRun_ModeHandling(t *testing.T) {
430431
prog := regtest.NewIncProgram(t, "prog1", "counter")
431432

432433
tests := []struct {
433-
mode string
434-
wantUploads int
434+
mode string
435+
wantConfigDownloads int64
436+
wantUploads int
435437
}{
436-
{"off", 0},
437-
{"local", 0},
438-
{"on", 1}, // only the second week is uploadable
438+
{"off", 0, 0},
439+
{"local", 0, 0},
440+
{"on", 1, 1}, // only the second week is uploadable
439441
}
440442
for _, test := range tests {
441443
t.Run(test.mode, func(t *testing.T) {
@@ -459,10 +461,15 @@ func TestRun_ModeHandling(t *testing.T) {
459461
t.Fatal(err)
460462
}
461463

464+
downloadsBefore := configstore.Downloads()
462465
if err := upload.Run(cfg); err != nil {
463466
t.Fatal(err)
464467
}
465468

469+
if got := configstore.Downloads() - downloadsBefore; got != test.wantConfigDownloads {
470+
t.Errorf("configstore.Download called: %v, want %v", got, test.wantConfigDownloads)
471+
}
472+
466473
uploads := getUploads()
467474
if gotUploads := len(uploads); gotUploads != test.wantUploads {
468475
t.Fatalf("got %d uploads, want %d", gotUploads, test.wantUploads)

0 commit comments

Comments
 (0)