Skip to content

Hide the mutex lock in formatter.ContainerStats#26819

Merged
thaJeztah merged 1 commit intomoby:masterfrom
boaz0:fix_stats_mutex
Oct 14, 2016
Merged

Hide the mutex lock in formatter.ContainerStats#26819
thaJeztah merged 1 commit intomoby:masterfrom
boaz0:fix_stats_mutex

Conversation

@boaz0
Copy link
Copy Markdown
Contributor

@boaz0 boaz0 commented Sep 22, 2016

- What I did
Created getters and setters to the container statistics and hide the mutex lock.

- How I did it

  • Hide the mutex field in formatter.ContainerStats.
  • Expose setters and getters that return or set the statistics using that mutex.

- How to verify it

  • Run the integration and unit tests
  • Run docker stats and docker stats --format "{{.PIDs}} -- {{.Container}}" and see that the formats are different.

- Description for the changelog
This patch supposes to fix the issue mentioned in #24987

@GordonTheTurtle GordonTheTurtle added status/0-triage dco/no Automatically set by a bot when one of the commits lacks proper signature and removed dco/no Automatically set by a bot when one of the commits lacks proper signature labels Sep 22, 2016
@thaJeztah
Copy link
Copy Markdown
Member

ping @runcom @cpuguy83 PTAL

@thaJeztah thaJeztah added this to the 1.13.0 milestone Sep 22, 2016
Comment thread cli/command/container/stats.go Outdated
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.

I hate the idea of copying the containers statistics (it won't scale nicely).
I am working on a better way to avoid this part.

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.

Fixed

@boaz0 boaz0 changed the title Remove the mutex lock from formatter.ContainerStats Hide the mutex lock in formatter.ContainerStats Sep 25, 2016
@boaz0
Copy link
Copy Markdown
Contributor Author

boaz0 commented Sep 25, 2016

@thaJeztah @runcom @cpuguy83 @vdemeester @LK4D4
Can you please review this change?

Any feedback will be appreciated! 😺
Thanks a lot!

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented Sep 26, 2016

@ripcurld00d Doesn't this mean that stats could get CPU and memory from different time frames? I sorta suspicious about granular locking for setting parts of stats, it adds a lot of code and has little sense IMO, because stats structure supposed to be "atomic" for ps(also repeated lock/unlock might have performance issues). Maybe I'm missing something, though.

@boaz0
Copy link
Copy Markdown
Contributor Author

boaz0 commented Sep 26, 2016

@LK4D4, no you got it right. I do believe I can make this more atomic (read the CPU, memory and etc in one time frame), though.

Anything else that I should keep in mind?

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented Sep 26, 2016

@ripcurld00d Otherwise, patch looks good to me.

@boaz0
Copy link
Copy Markdown
Contributor Author

boaz0 commented Sep 27, 2016

@LK4D4 I updated the commit, feel free to give new feedback.
/cc @runcom @cpuguy83
😀

Comment thread cli/command/formatter/stats.go Outdated
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.

This name isn't good IMO. At first, it doesn't reflect what this structure does and then "clear containers" is intel containers technology :)

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.

Yes, I am bad at giving names 😊.
Is it a bad idea to move this to api/types and call it ContainerStats?

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.

I dunno :) ping @vdemeester @thaJeztah

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's the difference with ContainerStats? (Just so I can try thinking of a better name)

Copy link
Copy Markdown
Contributor Author

@boaz0 boaz0 Sep 29, 2016

Choose a reason for hiding this comment

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

@thaJeztah ContainerStats is used for collecting the container statistics while ClearContainerStats (now ContainerStatsRecord) is used to display the statistics on the screen.

Because of that ContainerStats has both mutex and an error field.

Comment thread cli/command/formatter/stats.go Outdated
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.

Can't you make statistics type public and use it here as argument?

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.

Yes, it will definitely make it more readable. 👍

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.

Fixed

@boaz0
Copy link
Copy Markdown
Contributor Author

boaz0 commented Sep 29, 2016

Odd, windowRS1 failed:

10:59:05 FAIL: docker_cli_run_test.go:1855: DockerSuite.TestRunInteractiveWithRestartPolicy
               docker_cli_run_test.go:1868:
               Failures:
               ExitCode was 0 expected 11
               Expected command to finish, but it hit the timeout

10:18:24 FAIL: docker_cli_events_test.go:81: DockerSuite.TestEventsLimit
               docker_cli_events_test.go:102:
               value *errors.errorString = &errors.errorString{s:"exit status 125: d:\\CI\\CI-7bc23d0\\binary\\docker.exe: Error response from daemon: container f885f96b146b7d711d78d27a0f100c72a5d18fc72a340359c376e1c77f5c1424 encountered an error during Start failed in Win32: This operation returned because the timeout period expired. (0x5b4).\n"} ("exit status 125: d:\\CI\\CI-7bc23d0\\binary\\docker.exe: Error response from daemon: container f885f96b146b7d711d78d27a0f100c72a5d18fc72a340359c376e1c77f5c1424 encountered an error during Start failed in Win32: This operation returned because the timeout period expired. (0x5b4).\n")

None of them are related to docker stats.
Is my commit the only one who failed on those tests? 😟

Copy link
Copy Markdown
Member

@cpuguy83 cpuguy83 left a comment

Choose a reason for hiding this comment

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

I can sort of follow the code, but it's a bit confusing.

I wonder if we need so many different structs for the same information.
Instead we can put methods on one, like IsWindows() which can lock and check the windows field, and Name() and read the name field.

Comment thread cli/command/formatter/stats.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this comment is not right.
Statistics is where this comment belongs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Still not quite sure what this does. Maybe it's just for formatting?
Maybe ContainerStatsFormatter?

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.

👍 sounds right

Comment thread cli/command/formatter/stats.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think maybe a better name would be StatsEntry

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.

👍

Comment thread cli/command/formatter/stats.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All of these with basically the same fields and name is pretty confusing.

Comment thread cli/command/formatter/stats.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not really "getting" anything here, just copying into a new struct.

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.

Yes, that's right. Nevertheless, you eventually get a ContainerStatsRecord entity.
You want me to rename this anyway?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't matter.

@boaz0
Copy link
Copy Markdown
Contributor Author

boaz0 commented Sep 29, 2016

@cpuguy83 thanks for your feedback.

@boaz0
Copy link
Copy Markdown
Contributor Author

boaz0 commented Oct 9, 2016

@cpuguy83 and @LK4D4 PTAL thanks. 😺

/cc @thaJeztah

Comment thread cli/command/formatter/stats.go Outdated
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.

Let's use values instead of pointers everywhere for consistency.

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.

👍 no problem

Comment thread cli/command/formatter/stats.go Outdated
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.

I don't like global variable usage here. It's definitely shouldn't be global.

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.

Ummm... the alternative is to put this variable inside the StatsEntity struct.
What do you think?

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.

Yeah, I think it would be better. I saw that we use global var in stat_helper too. It's also so wrong :(

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.

👍
I don't mind fixing this as well.
The question is whether you prefer doing that in this commit or in another one.

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.

Let's do it in another PR.

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.

no problem ☕

The formatter.ContainerStats struct exposes its Mutex.
This is a bad design and should be fixed.

To fix that, I separated the statistics
attributes from ContainerStats to StatsEntry and
hid the mutex. Notice that the mutex protects both
the `err` field and the statistics attributes.

Then, implemented SetStatistics, SetError, GetStatistics
and GetError to avoid races.

Moreover, to make this less granular, I decided to
replace the read-write mutex with the regular mutex and
to pass a StatsEntry slice to formatter.ContainerStatsWrite

Signed-off-by: Boaz Shuster <[email protected]>
@boaz0
Copy link
Copy Markdown
Contributor Author

boaz0 commented Oct 10, 2016

@LK4D4 re-review 🎱 🍶 plz?

@LK4D4
Copy link
Copy Markdown
Contributor

LK4D4 commented Oct 10, 2016

ping @cpuguy83

@tonistiigi
Copy link
Copy Markdown
Member

LGTM

Copy link
Copy Markdown
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah merged commit ba2c13f into moby:master Oct 14, 2016
dnephin pushed a commit to dnephin/docker that referenced this pull request Apr 17, 2017
Hide the mutex lock in formatter.ContainerStats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants