Skip to content

chore: concurrent image build with context cancellation#4760

Merged
mergify[bot] merged 25 commits intoaws:mainlinefrom
KollaAdithya:concurrent/build/rebase/dockerbuildlabel
Apr 18, 2023
Merged

chore: concurrent image build with context cancellation#4760
mergify[bot] merged 25 commits intoaws:mainlinefrom
KollaAdithya:concurrent/build/rebase/dockerbuildlabel

Conversation

@KollaAdithya
Copy link
Copy Markdown
Contributor

@KollaAdithya KollaAdithya commented Apr 13, 2023

Finally with this PR we can build container images in parallel.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.

@KollaAdithya KollaAdithya requested a review from a team as a code owner April 13, 2023 20:39
@KollaAdithya KollaAdithya requested review from dannyrandall and removed request for a team April 13, 2023 20:39
@KollaAdithya KollaAdithya added the WIP Pull requests that are being modified now. label Apr 13, 2023
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 13, 2023

🍕 Here are the new binary sizes!

Name New size (kiB) size (kiB) Delta (%)
macOS (amd) 50536 50328 +0.41
macOS (arm) 50724 50516 +0.41
linux (amd) 44484 44304 +0.41
linux (arm) 42820 42564 +0.60
windows (amd) 41368 41204 +0.40

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 13, 2023

Codecov Report

Merging #4760 (bfd918c) into mainline (ad57838) will decrease coverage by 0.01%.
The diff coverage is 77.50%.

@@             Coverage Diff              @@
##           mainline    #4760      +/-   ##
============================================
- Coverage     69.91%   69.91%   -0.01%     
============================================
  Files           284      284              
  Lines         40694    40795     +101     
  Branches        272      272              
============================================
+ Hits          28450    28520      +70     
- Misses        10869    10892      +23     
- Partials       1375     1383       +8     
Impacted Files Coverage Δ
internal/pkg/cli/task_run.go 53.84% <0.00%> (ø)
internal/pkg/term/syncbuffer/termprinter.go 50.52% <60.00%> (+1.06%) ⬆️
internal/pkg/cli/deploy/workload.go 59.64% <71.15%> (+0.82%) ⬆️
internal/pkg/docker/dockerengine/dockerengine.go 79.89% <100.00%> (ø)
...ernal/pkg/docker/dockerengine/mock_dockerengine.go 100.00% <100.00%> (ø)
internal/pkg/repository/repository.go 69.23% <100.00%> (ø)
internal/pkg/term/syncbuffer/syncbuffer.go 92.85% <100.00%> (ø)

... and 5 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@KollaAdithya KollaAdithya removed the WIP Pull requests that are being modified now. label Apr 13, 2023
Copy link
Copy Markdown
Contributor

@paragbhingre paragbhingre left a comment

Choose a reason for hiding this comment

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

This is really cool 😎

Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload_test.go Outdated
Comment thread internal/pkg/cli/task_run.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
numLinesForBuildAndPush = -1
}
// create a LabeledTermPrinter for rendering build and push output.
ltp := syncbuffer.NewLabeledTermPrinter(os.Stderr, labeledBuffers, syncbuffer.WithNumLines(numLinesForBuildAndPush), syncbuffer.WithPadding(paddingForBuildAndPush))
Copy link
Copy Markdown
Contributor

@Lou1415926 Lou1415926 Apr 14, 2023

Choose a reason for hiding this comment

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

What do you think of this

// In cli/deploy/workload.go
type termPrinter interface { // And use make gen-mocks to generate a mock for unit testing
  IsDone() bool
  Print() error
}
type workloadDeployer struct {
  newTermPrinter (fw FileWriter, bufs []*LabeledSyncBuffer, opts ...LabeledTermPrinterOption) termPrinter
}
func newWorkloadDeployer(in *WorkloadDeployerInput) (*workloadDeployer, error) {
    return &workloadDeployer{
        newTermPrinter: syncbuffer.NewLabeledTermPrinter,
        }
    }
}

func (d *workloadDeployer) uploadContainerImages(out *UploadArtifactsOutput) error {
   //...
   ltp := d.newTermPrinter(os.Stderr, labeledBuffers, syncbuffer.WithNumLines(numLinesForBuildAndPush), syncbuffer.WithPadding(paddingForBuildAndPush))
   //...
}

// In cli/deploy/workload_test.go
type deployMocks struct {
   //...
  mockTermPrinter *mocks.mockTermPrinter
}

func TestWorkloadDeployer_UploadArtifacts(t *testing.T) {
     m := &deployMocks{
		mockTermPrinter: mocks.NewMockTermPrinter(ctril)
      }
      m.mockTermPrinter.Expect().Print().Return(nil)
      wkldDeployer := &workloadDeployer{
           newTermPrinter: func(fw FileWriter, bufs []*LabeledSyncBuffer, opts ...LabeledTermPrinterOption) termPrinter {
            return m.mockTermPrinter
          }
       }
}

The reason why it is so hard to write unit test for UploadArtifacts is that the constructor for term printer is currently called within uploadContainerImages(). However, in order to properly test UploadArtifacts, we need to sub the labeled term printer with a mock. The solution is to mock the constructor function call - in our case newTermPrinter.

@KollaAdithya KollaAdithya force-pushed the concurrent/build/rebase/dockerbuildlabel branch from 590c6c9 to 272650c Compare April 17, 2023 16:52
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/repository/repository_test.go
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
envSess: envSession,
store: store,
envConfig: envConfig,
labeledTermPrinter: func(fw syncbuffer.FileWriter, bufs []*syncbuffer.LabeledSyncBuffer, opts ...syncbuffer.LabeledTermPrinterOption) labeledTermPrinter {
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:

Suggested change
labeledTermPrinter: func(fw syncbuffer.FileWriter, bufs []*syncbuffer.LabeledSyncBuffer, opts ...syncbuffer.LabeledTermPrinterOption) labeledTermPrinter {
labeledTermPrinter: syncbuffer.NewLabeledTermPrinter,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This will be aIncompatibleAssign 🤔 . if I directly use syncbuffer.NewLabeledTermPrinter 
because the the function signature of the labeledTermPrinter field in workloadDeployer struct does not match the signature of syncbuffer.NewLabeledTermPrinter function.

Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
add logic to termprinter for removing the buffer once they are done
Comment thread internal/pkg/term/syncbuffer/termprinter.go
Copy link
Copy Markdown
Contributor

@efekarakus efekarakus left a comment

Choose a reason for hiding this comment

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

:shipit: with a small tiny test adjustment request

Comment thread internal/pkg/term/syncbuffer/termprinter_test.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
if err != nil {
return fmt.Errorf("build and push the image for %q container: %w", name, err)
}
digestsMu.Lock()
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.

Suggested change
digestsMu.Lock()
digestsMu.Lock()
defer digestsMu.Unlock()

Copy link
Copy Markdown
Contributor Author

@KollaAdithya KollaAdithya Apr 18, 2023

Choose a reason for hiding this comment

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

is it good to unlock the map before return nil? I mean if I use defer then i will unlock after return is executed.

Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/cli/deploy/workload.go Outdated
Comment thread internal/pkg/docker/dockerengine/dockerengine.go
Copy link
Copy Markdown
Contributor

@dannyrandall dannyrandall left a comment

Choose a reason for hiding this comment

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

looks great! good work ☺️🍾

if ltp.IsDone() {
return nil
}
time.Sleep(pollIntervalForBuildAndPush)
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.

Suggested change
time.Sleep(pollIntervalForBuildAndPush)
select {
case <-ctx.Done():
return nil
case <-time.After(pollIntervalForBuildAndPush):
// loop again
}

so this function stops if ctx is cancelled!

@dannyrandall dannyrandall added the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Apr 18, 2023
@KollaAdithya KollaAdithya removed the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Apr 18, 2023
@mergify mergify Bot merged commit 8175be3 into aws:mainline Apr 18, 2023
bvtujo added a commit to bvtujo/copilot-cli that referenced this pull request Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants