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

Use sync/atomic to keep tab on go routines #5961

Merged
merged 2 commits into from
Jul 21, 2022
Merged

Use sync/atomic to keep tab on go routines #5961

merged 2 commits into from
Jul 21, 2022

Conversation

dharmit
Copy link
Member

@dharmit dharmit commented Jul 21, 2022

Signed-off-by: Dharmit Shah shahdharmit@gmail.com

What type of PR is this:
/kind bug

What does this PR do / why we need it:

As a result of the code pushed in #5942, odo logs --follow would hang
if user executed it without pushing the component in either dev or
deploy modes. This was not a problem before that PR was merged.

This was caused because below code would not get executed in business
layer if user was using follow mode:

if !follow {
    doneChan <- struct{}{}
}

The reason we didn't execute it for follow mode was that it prematurely
exited and often didn't print the logs of all the containers.

This PR uses sync/atomic to keep a tab on go routines started for
odo logs --follow instead of using sync.WaitGroup, and checks if no
go routines are running before it exits. This enables us to use
doneChan <- struct{}{} in business layer irrespective of user running
odo logs with or without follow mode.

Why not use wg.Wait() as first statement instead in
case <-events.Done:? This is because wg.Wait() is a blocking call
and won't be called upon each time a go routine for odo logs --follow
is done (wg.Done()). This leads to same problem as that fixed by PR
5942, which is that odo logs --follow won't exit even if the
underlying odo component was deleted.

Which issue(s) this PR fixes:
Fixes part of #5872

PR acceptance criteria:

  • Unit test

  • Integration test

  • Documentation

How to test changes / Special notes to the reviewer:

Signed-off-by: Dharmit Shah <shahdharmit@gmail.com>

As a result of the code pushed in
#5942, `odo logs --follow`
would hang if user executed it without pushing the component in either
dev or deploy modes. This was not a problem before that PR was merged.

This was caused because below code would not get executed in business
layer if user was using follow mode:

```
if !follow {
    doneChan <- struct{}{}
}
```

The reason we didn't execute it for follow mode was that it prematurely
exited and often didn't print the logs of all the containers.

This PR uses `sync/atomic` to keep a tab on go routines started for `odo
logs --follow` instead of using `sync.WaitGroup`, and checks if no go
routines are running before it exits. This enables us to use `doneChan
<- struct{}{}` in business layer irrespective of user running `odo logs`
with or without follow mode.

Why not use `wg.Wait()` as first statement instead in
`case <-events.Done:`? This is because `wg.Wait()` is a blocking call
and won't be called upon each time a go routine for `odo logs --follow`
is done (`wg.Done()`). This leads to same problem as that fixed by PR
5942, which is that `odo logs --follow` won't exit even if the
underlying odo component was deleted.
@netlify
Copy link

netlify bot commented Jul 21, 2022

Deploy Preview for odo-docusaurus-preview canceled.

Name Link
🔨 Latest commit f1fedd2
🔍 Latest deploy log https://app.netlify.com/sites/odo-docusaurus-preview/deploys/62d922482516900008656558

@openshift-ci openshift-ci bot added the kind/bug Categorizes issue or PR as related to a bug. label Jul 21, 2022
@openshift-ci openshift-ci bot requested review from cdrage and feloy July 21, 2022 08:29
@odo-robot
Copy link

odo-robot bot commented Jul 21, 2022

Validate Tests on commit a80f754 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Jul 21, 2022

Unit Tests on commit a80f754 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Jul 21, 2022

OpenShift Tests on commit a80f754 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Jul 21, 2022

Windows Tests (OCP) on commit a80f754 finished successfully.
View logs: TXT HTML

@odo-robot
Copy link

odo-robot bot commented Jul 21, 2022

Kubernetes Tests on commit a80f754 finished successfully.
View logs: TXT HTML

Signed-off-by: Dharmit Shah <shahdharmit@gmail.com>

`go vet` complains about direct assignment to an atomic value. Hence, an
anonymous struct to fix the problem.
@sonarcloud
Copy link

sonarcloud bot commented Jul 21, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@dharmit dharmit requested review from valaparthvi and rm3l and removed request for feloy July 21, 2022 10:59
@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. Required by Prow. label Jul 21, 2022
@cdrage
Copy link
Member

cdrage commented Jul 21, 2022

/approve and thanks for the fix!

@cdrage
Copy link
Member

cdrage commented Jul 21, 2022

/approve

@openshift-ci
Copy link

openshift-ci bot commented Jul 21, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cdrage

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. label Jul 21, 2022
@openshift-merge-robot openshift-merge-robot merged commit f85f3d5 into redhat-developer:main Jul 21, 2022
cdrage pushed a commit to cdrage/odo that referenced this pull request Aug 31, 2022
* Use sync/atomic to keep tab on go routines

Signed-off-by: Dharmit Shah <shahdharmit@gmail.com>

As a result of the code pushed in
redhat-developer#5942, `odo logs --follow`
would hang if user executed it without pushing the component in either
dev or deploy modes. This was not a problem before that PR was merged.

This was caused because below code would not get executed in business
layer if user was using follow mode:

```
if !follow {
    doneChan <- struct{}{}
}
```

The reason we didn't execute it for follow mode was that it prematurely
exited and often didn't print the logs of all the containers.

This PR uses `sync/atomic` to keep a tab on go routines started for `odo
logs --follow` instead of using `sync.WaitGroup`, and checks if no go
routines are running before it exits. This enables us to use `doneChan
<- struct{}{}` in business layer irrespective of user running `odo logs`
with or without follow mode.

Why not use `wg.Wait()` as first statement instead in
`case <-events.Done:`? This is because `wg.Wait()` is a blocking call
and won't be called upon each time a go routine for `odo logs --follow`
is done (`wg.Done()`). This leads to same problem as that fixed by PR
5942, which is that `odo logs --follow` won't exit even if the
underlying odo component was deleted.

* Use a struct instead of int64 for atomic

Signed-off-by: Dharmit Shah <shahdharmit@gmail.com>

`go vet` complains about direct assignment to an atomic value. Hence, an
anonymous struct to fix the problem.
@dharmit dharmit deleted the fix-odo-logs-follow-hangs branch September 23, 2022 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. Required by Prow. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. Required by Prow.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants