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

Channel Refactoring #91

Merged
merged 24 commits into from
Aug 17, 2021
Merged

Channel Refactoring #91

merged 24 commits into from
Aug 17, 2021

Conversation

isatasan
Copy link

@isatasan isatasan commented Aug 4, 2021

No description provided.

server/server.go Outdated
@@ -277,7 +279,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
}
vcsClient := vcs.NewClientProxy(githubClient, gitlabClient, bitbucketCloudClient, bitbucketServerClient, azuredevopsClient)
commitStatusUpdater := &events.DefaultCommitStatusUpdater{Client: vcsClient, StatusName: userConfig.VCSStatusName}
terraformOutputChan := make(chan *models.TerraformOutputLine)
terraformOutputChan := make(chan *models.ProjectCmdOutputLine)
Copy link

Choose a reason for hiding this comment

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

Suggested change
terraformOutputChan := make(chan *models.ProjectCmdOutputLine)
projectCmdOutput := make(chan *models.ProjectCmdOutputLine)

Comment on lines 77 to 78
terraformOutputChan chan<- *models.ProjectCmdOutputLine
ProjectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler
Copy link

Choose a reason for hiding this comment

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

Suggested change
terraformOutputChan chan<- *models.ProjectCmdOutputLine
ProjectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler
projectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler

replace every struct that uses terraformOutputChan with ProjectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler

@@ -105,7 +107,7 @@ func NewClientWithDefaultVersion(
tfDownloader Downloader,
usePluginCache bool,
fetchAsync bool,
terraformOutputChan chan<- *models.TerraformOutputLine,
terraformOutputChan chan<- *models.ProjectCmdOutputLine,
Copy link

Choose a reason for hiding this comment

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

update this to projectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler

Copy link

Choose a reason for hiding this comment

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

Suggested change
terraformOutputChan chan<- *models.ProjectCmdOutputLine,
projectCmdOutputHandler handlers.DefaultProjectCommandOutputHandler,

@@ -173,6 +175,7 @@ func NewClientWithDefaultVersion(
versions: versions,
usePluginCache: usePluginCache,
terraformOutputChan: terraformOutputChan,
ProjectCmdOutputHandler: handlers.DefaultProjectCommandOutputHandler{},
Copy link

Choose a reason for hiding this comment

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

Suggested change
ProjectCmdOutputHandler: handlers.DefaultProjectCommandOutputHandler{},
ProjectCmdOutputHandler: projectCmdOutputHandler,

@msarvar
Copy link

msarvar commented Aug 9, 2021

So all structs that have TerraformOutputChan attribute should be updated to use handlers.ProjectCommandOutputHandler instead. You should remove TerraformOutputChan and use ProjectCommandOutputHandler instead.

server/server.go Outdated
@@ -508,7 +511,9 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
WorkingDir: workingDir,
Webhooks: webhooksManager,
WorkingDirLocker: workingDirLocker,
TerraformOutputChan: terraformOutputChan,
ProjectCmdOutputHandler: handlers.DefaultProjectCommandOutputHandler{
Copy link

Choose a reason for hiding this comment

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

why are you defining a new handlers.DefaultProjectCommandOutputHandler? You already created it pass in projectCmdOutputHandler

server/server.go Outdated
@@ -302,7 +307,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
userConfig.TFDownloadURL,
&terraform.DefaultDownloader{},
true,
terraformOutputChan)
*projectCmdOutputHandler)
Copy link

Choose a reason for hiding this comment

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

Suggested change
*projectCmdOutputHandler)
projectCmdOutputHandler)

chanLock sync.RWMutex
WebsocketHandler WebsocketHandler
WebsocketHandler handlers.WebsocketHandler
ProjectCommandOutputHandler handlers.DefaultProjectCommandOutputHandler
Copy link

Choose a reason for hiding this comment

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

Suggested change
ProjectCommandOutputHandler handlers.DefaultProjectCommandOutputHandler
ProjectCommandOutputHandler handlers.ProjectCommandOutputHandler

@@ -666,7 +667,10 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
e2eStatusUpdater := &events.DefaultCommitStatusUpdater{Client: e2eVCSClient}
e2eGithubGetter := mocks.NewMockGithubPullGetter()
e2eGitlabGetter := mocks.NewMockGitlabMergeRequestGetter()
tempchan := make(chan *models.TerraformOutputLine)
tempchan := make(chan *models.ProjectCmdOutputLine)
projectCmdOutputHandler := handlers.DefaultProjectCommandOutputHandler{
Copy link

Choose a reason for hiding this comment

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

Suggested change
projectCmdOutputHandler := handlers.DefaultProjectCommandOutputHandler{
projectCmdOutputHandler := &handlers.DefaultProjectCommandOutputHandler{

@@ -666,7 +667,10 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
e2eStatusUpdater := &events.DefaultCommitStatusUpdater{Client: e2eVCSClient}
e2eGithubGetter := mocks.NewMockGithubPullGetter()
e2eGitlabGetter := mocks.NewMockGitlabMergeRequestGetter()
tempchan := make(chan *models.TerraformOutputLine)
tempchan := make(chan *models.ProjectCmdOutputLine)
projectCmdOutputHandler := &handlers.DefaultProjectCommandOutputHandler{
Copy link

Choose a reason for hiding this comment

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

For tests you should generate mocks and use them instead of directly creating handlers.DefaultProjectCommandOutputHandler object.
Same way we are mocking WebsocketHandler

Comment on lines 61 to 65
err := callback(msg)
if err != nil {
p.logger.Err(err.Error())
}
return nil
Copy link

@msarvar msarvar Aug 9, 2021

Choose a reason for hiding this comment

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

Suggested change
err := callback(msg)
if err != nil {
p.logger.Err(err.Error())
}
return nil
if err := callback(msg); err != nil {
return err
}

Comment on lines 43 to 47
func NewProjectCommandOutputHandler() ProjectCommandOutputHandler {
return &DefaultProjectCommandOutputHandler{
ProjectCmdOutput: make(chan *models.ProjectCmdOutputLine),
}
}
Copy link

Choose a reason for hiding this comment

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

Suggested change
func NewProjectCommandOutputHandler() ProjectCommandOutputHandler {
return &DefaultProjectCommandOutputHandler{
ProjectCmdOutput: make(chan *models.ProjectCmdOutputLine),
}
}
func NewProjectCommandOutputHandler(projectCmdOutput chan *models.ProjectCmdOutputLine, logger logging.SimpleLogging) ProjectCommandOutputHandler {
return &DefaultProjectCommandOutputHandler{
ProjectCmdOutput: projectCmdOutput,
logger: logger,
controllerBuffer: map[string]map[chan string]bool{},
}
}

Use this function in the server.go

@@ -666,7 +667,8 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
e2eStatusUpdater := &events.DefaultCommitStatusUpdater{Client: e2eVCSClient}
e2eGithubGetter := mocks.NewMockGithubPullGetter()
e2eGitlabGetter := mocks.NewMockGitlabMergeRequestGetter()
tempchan := make(chan *models.TerraformOutputLine)
tempchan := make(chan *models.ProjectCmdOutputLine)
Copy link

Choose a reason for hiding this comment

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

You can remove this

Comment on lines 54 to 55
projectOutputHandler.Send(ctx, "Test Terraform Output")
projectOutputHandler.Handle()
Copy link

Choose a reason for hiding this comment

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

I don't think you need these two lines.

@@ -119,11 +121,12 @@ type DefaultProjectCommandRunner struct {
VersionStepRunner StepRunner
RunStepRunner CustomStepRunner
EnvStepRunner EnvStepRunner
PullApprovedChecker runtime.PullApprovedChecker
Copy link

Choose a reason for hiding this comment

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

Suggested change
PullApprovedChecker runtime.PullApprovedChecker

ApplyStepRunner: mockApply,
RunStepRunner: mockRun,
EnvStepRunner: &realEnv,
PullApprovedChecker: nil,
Copy link

Choose a reason for hiding this comment

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

Suggested change
PullApprovedChecker: nil,

server/server.go Outdated
@@ -508,10 +511,11 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
EnvStepRunner: &runtime.EnvStepRunner{
RunStepRunner: runStepRunner,
},
PullApprovedChecker: vcsClient,
Copy link

Choose a reason for hiding this comment

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

Suggested change
PullApprovedChecker: vcsClient,

WorkingDir: workingDir,
Webhooks: &mockWebhookSender{},
WorkingDirLocker: locker,
PullApprovedChecker: e2eVCSClient,
Copy link

Choose a reason for hiding this comment

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

Suggested change
PullApprovedChecker: e2eVCSClient,

func (p *DefaultProjectCommandOutputHandler) writeLogLine(pull string, line string) {
p.controllerBufferLock.Lock()
if p.projectOutputBuffers == nil {
p.projectOutputBuffers = map[string][]string{}
Copy link

Choose a reason for hiding this comment

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

you can move this into NewProjectCommandOutputHandler

//Remove channel, so client no longer receives Terraform output
func (p *DefaultProjectCommandOutputHandler) removeChan(pull string, ch chan string) {
p.controllerBufferLock.Lock()
delete(p.controllerBuffers[pull], ch)
Copy link

Choose a reason for hiding this comment

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

We should also close the channel here.

Suggested change
delete(p.controllerBuffers[pull], ch)
delete(p.controllerBuffers[pull], ch)
close(ch)

Comment on lines 96 to 98
if p.controllerBuffers == nil {
p.controllerBuffers = map[string]map[chan string]bool{}
}
Copy link

Choose a reason for hiding this comment

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

Suggested change
if p.controllerBuffers == nil {
p.controllerBuffers = map[string]map[chan string]bool{}
}

// this logBuffers
projectOutputBuffers map[string][]string
// this is wsChans
controllerBuffers map[string]map[chan string]bool
Copy link

Choose a reason for hiding this comment

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

Let's rename this into receiverBuffers

Suggested change
controllerBuffers map[string]map[chan string]bool
receiverBuffers map[string]map[chan string]bool

return &DefaultProjectCommandOutputHandler{
ProjectCmdOutput: projectCmdOutput,
logger: logger,
controllerBuffers: map[string]map[chan string]bool{},
Copy link

Choose a reason for hiding this comment

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

Suggested change
controllerBuffers: map[string]map[chan string]bool{},
controllerBuffers: map[string]map[chan string]bool{},
projectOutputBuffers: map[string][]string{},

Comment on lines 108 to 110
if p.projectOutputBuffers == nil {
p.projectOutputBuffers = map[string][]string{}
}
Copy link

Choose a reason for hiding this comment

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

Suggested change
if p.projectOutputBuffers == nil {
p.projectOutputBuffers = map[string][]string{}
}

ProjectInfo: ctx.PullInfo(),
Line: msg,
ProjectInfo: ctx.PullInfo(),
ClearBuffBefore: true,
Copy link

Choose a reason for hiding this comment

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

You don't want to set this to true everytime you send a message. You want to clear the buffer before starting terraform process on a project. You can create a Clear(ctx models.ProjectCommandContext, msg string) function that will set the flag to true

go func() {
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error {
expectMsg = msg
wg.Done()
Copy link

Choose a reason for hiding this comment

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

you can move this outside of receive callback and put as a last thing in the go routine. You are sending 3 messages to the queue. So it is calling wg.Done() 3 times. But wait group is only expecting 1 call.

Comment on lines 108 to 129
go func() {
projectOutputHandler.Send(ctx, "Test Terraform Output")
}()

go func() {
projectOutputHandler.Clear(ctx, "")
}()

go func() {
projectOutputHandler.Send(ctx, "Test Terraform Output")
}()

expectMsg := ""
wg.Add(1)
go func() {
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error {
expectMsg = msg
return nil
})
wg.Done()
}()
wg.Wait()
Copy link

Choose a reason for hiding this comment

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

This should fix the test

Suggested change
go func() {
projectOutputHandler.Send(ctx, "Test Terraform Output")
}()
go func() {
projectOutputHandler.Clear(ctx, "")
}()
go func() {
projectOutputHandler.Send(ctx, "Test Terraform Output")
}()
expectMsg := ""
wg.Add(1)
go func() {
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error {
expectMsg = msg
return nil
})
wg.Done()
}()
wg.Wait()
wg.Add(1)
go func() {
projectOutputHandler.Send(ctx, "Test Terraform Output")
}()
wg.Add(1)
go func() {
projectOutputHandler.Clear(ctx, "")
}()
wg.Add(1)
go func() {
projectOutputHandler.Send(ctx, "Test Terraform Output")
}()
expectMsg := ""
go func() {
projectOutputHandler.Receive(ctx.PullInfo(), func(msg string) error {
expectMsg = msg
wg.Done()
return nil
})
}()
wg.Wait()

@msarvar msarvar merged commit a44c966 into release-v0.17.1-lyft.1 Aug 17, 2021
@msarvar msarvar deleted the Refactoring branch August 17, 2021 17:49
msarvar pushed a commit that referenced this pull request Sep 27, 2021
* Began channel refactoring

* Fixing refactoring logic

* Replacing terraformOutputChan

* Making suggested changes

* Changing attribute declarations

* Fixed channel logic

* Formatting

* Changing tests to utilize mocks

* Adding go files

* Adding suggested changes

* Suggested changes before merging

* Reformatting

* Deleting unnecesary lines

* Buffer cleanup logic

* Buffer cleanup & Testing

* Trying to make test pass

* Suggested changes

* Fixing test

* Fixing failing test

* Error checking

* Comment improvements

* Adding buffer clearing for apply workflow

* Removing clearing for apply workflow
Aayyush pushed a commit that referenced this pull request Dec 9, 2021
* Began channel refactoring

* Fixing refactoring logic

* Replacing terraformOutputChan

* Making suggested changes

* Changing attribute declarations

* Fixed channel logic

* Formatting

* Changing tests to utilize mocks

* Adding go files

* Adding suggested changes

* Suggested changes before merging

* Reformatting

* Deleting unnecesary lines

* Buffer cleanup logic

* Buffer cleanup & Testing

* Trying to make test pass

* Suggested changes

* Fixing test

* Fixing failing test

* Error checking

* Comment improvements

* Adding buffer clearing for apply workflow

* Removing clearing for apply workflow
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.

2 participants