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

fix: build fails with "sending never-build-twice request failed" #508

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 16 additions & 9 deletions internal/jobserver/nbt/nbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@

type (
service struct {
// state is a map of import path to a [sync.Map] of build ID to buildState.
state sync.Map
dir string
}
buildState struct {
initOnce sync.Once
buildID string
token string // Finalization token
onDone func() // Called once the original task has completed
done <-chan struct{} // Blocks until the original task has completed
Expand Down Expand Up @@ -125,7 +125,10 @@
return nil, fmt.Errorf("invalid request: %#v", req)
}

rawState, reused := s.state.LoadOrStore(req.ImportPath, &buildState{buildID: req.BuildID})
rawPkgMap, _ := s.state.LoadOrStore(req.ImportPath, &sync.Map{})
pkgMap, _ := rawPkgMap.(*sync.Map)

rawState, reused := pkgMap.LoadOrStore(req.ImportPath, &buildState{})
state, _ := rawState.(*buildState)

// Initialize the build state.
Expand All @@ -139,10 +142,6 @@

// If the build state is re-used, wait for the original to complete...
if reused {
if state.buildID != req.BuildID {
return nil, fmt.Errorf("mismatched build ID for %q: %q != %q", req.ImportPath, state.buildID, req.BuildID)
}

<-state.done
if state.error != nil {
return nil, state.error
Expand All @@ -168,6 +167,8 @@
FinishRequest struct {
// ImportPath is the import path of the package that was built.
ImportPath string `json:"importPath"`
// BuildID is the build ID for the package being built.
BuildID string `json:"buildID"`
// FinishToken is forwarded from [*StartResponse.FinishToken], and cannot be
// blank.
FinishToken string `json:"token"`
Expand All @@ -193,10 +194,16 @@
return nil, fmt.Errorf("invalid request: %#v", req)
}

rawState, found := s.state.Load(req.ImportPath)
rawPkgMap, found := s.state.Load(req.ImportPath)
if !found {
return nil, fmt.Errorf("no build started for %q", req.ImportPath)
}
pkgMap, _ := rawPkgMap.(*sync.Map)

rawState, found := pkgMap.Load(req.ImportPath)
if !found {
return nil, fmt.Errorf("no build started for %q with build ID %q", req.ImportPath, req.BuildID)

Check warning on line 205 in internal/jobserver/nbt/nbt.go

View check run for this annotation

Codecov / codecov/patch

internal/jobserver/nbt/nbt.go#L205

Added line #L205 was not covered by tests
}

log := zerolog.Ctx(ctx).With().
Str("import-path", req.ImportPath).
Expand Down Expand Up @@ -233,8 +240,8 @@
return nil, state.error
}

dir := filepath.Join(s.dir, uuid.NewSHA1(ns, []byte(req.ImportPath)).String())
if err := os.Mkdir(dir, 0o755); err != nil {
dir := filepath.Join(s.dir, uuid.NewSHA1(ns, []byte(req.ImportPath)).String(), uuid.NewSHA1(ns, []byte(req.BuildID)).String())
if err := os.MkdirAll(dir, 0o755); err != nil {
state.error = fmt.Errorf("creating storage directory: %w", err)
return nil, state.error
}
Expand Down
45 changes: 9 additions & 36 deletions internal/jobserver/nbt/nbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test(t *testing.T) {

t.Run("not-started", func(t *testing.T) {
subject := &service{dir: t.TempDir()}
res, err := subject.finish(ctx, FinishRequest{ImportPath: importPath, FinishToken: "bazinga"})
res, err := subject.finish(ctx, FinishRequest{ImportPath: importPath, BuildID: buildID, FinishToken: "bazinga"})
require.ErrorContains(t, err, "no build started")
require.Nil(t, res)
})
Expand Down Expand Up @@ -82,48 +82,14 @@ func Test(t *testing.T) {

res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: start.FinishToken,
Files: map[Label]string{LabelArchive: archive, label: extraFile},
})
require.NoError(t, err)
require.NotNil(t, res)
})

t.Run("start-conflict-finish", func(t *testing.T) {
subject := &service{dir: t.TempDir()}

start, err := subject.start(ctx, StartRequest{ImportPath: importPath, BuildID: buildID})
require.NoError(t, err)
require.NotEmpty(t, start.FinishToken)
assert.Empty(t, start.Files)

archiveContent := uuid.NewString()

var wg sync.WaitGroup
defer wg.Wait()
for range 10 {
wg.Add(1)
go func() {
defer wg.Done()

res, err := subject.start(ctx, StartRequest{ImportPath: importPath, BuildID: buildID + "-alt"})
assert.ErrorContains(t, err, buildID)
assert.Nil(t, res)
}()
}

archive := filepath.Join(t.TempDir(), "_pkg_.a")
require.NoError(t, os.WriteFile(archive, []byte(archiveContent), 0o644))

res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
FinishToken: start.FinishToken,
Files: map[Label]string{LabelArchive: archive},
})
require.NoError(t, err)
require.NotNil(t, res)
})

t.Run("start-finish-finish", func(t *testing.T) {
const importPath = "github.com/DataDog/orchestrion.test"
subject := &service{dir: t.TempDir()}
Expand All @@ -140,6 +106,7 @@ func Test(t *testing.T) {
for range 10 {
res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: start.FinishToken,
Files: map[Label]string{LabelArchive: archive},
})
Expand All @@ -164,6 +131,7 @@ func Test(t *testing.T) {
for range 10 {
res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: uuid.NewString(),
Files: map[Label]string{LabelArchive: archive},
})
Expand All @@ -173,6 +141,7 @@ func Test(t *testing.T) {

res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: start.FinishToken,
Files: map[Label]string{LabelArchive: archive},
})
Expand Down Expand Up @@ -206,6 +175,7 @@ func Test(t *testing.T) {

res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: start.FinishToken,
Error: &errorText,
})
Expand Down Expand Up @@ -237,6 +207,7 @@ func Test(t *testing.T) {

res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: start.FinishToken,
})
require.ErrorIs(t, err, errNoFilesNorError)
Expand Down Expand Up @@ -270,6 +241,7 @@ func Test(t *testing.T) {

res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: start.FinishToken,
Files: map[Label]string{LabelArchive: archive},
})
Expand Down Expand Up @@ -308,6 +280,7 @@ func Test(t *testing.T) {

res, err := subject.finish(ctx, FinishRequest{
ImportPath: importPath,
BuildID: buildID,
FinishToken: start.FinishToken,
Files: map[Label]string{LabelArchive: archive, label: extraFile},
})
Expand Down
1 change: 1 addition & 0 deletions internal/toolexec/proxy/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (cmd *CompileCommand) notifyJobServer(ctx gocontext.Context, cmdErr error)

_, err = client.Request(ctx, jobs, nbt.FinishRequest{
ImportPath: cmd.importPath,
BuildID: cmd.Flags.BuildID,
FinishToken: cmd.finishToken,
Files: files,
Error: errorMessage,
Expand Down
Loading