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

JSON parse cluster worker stats instead of regex #2124

Merged
merged 2 commits into from
Apr 14, 2020

Conversation

wjordan
Copy link
Contributor

@wjordan wjordan commented Feb 21, 2020

Description

Refactoring PR to simplify the cluster worker stats code using JSON.parse and .to_json in place of regex-parsing and string interpolation.

Also includes refactoring of duplicated server-stats logic into a Server#stats method.

Your checklist for this pull request

  • I have reviewed the guidelines for contributing to this repository.
  • I have added an entry to History.md if this PR fixes a bug or adds a feature. If it doesn't need an entry to HISTORY.md, I have added [changelog skip] the pull request title.
  • I have added appropriate tests if this PR fixes a bug or adds a feature.
  • My pull request is 100 lines added/removed or less so that it can be easily reviewed.
  • If this PR doesn't need tests (docs change), I added [ci skip] to the title of the PR.
  • If this closes any issues, I have added "Closes #issue" to the PR description or my commit messages.
  • I have updated the documentation accordingly.
  • All new and existing tests passed, including Rubocop.

@wjordan wjordan force-pushed the json_cluster_stats branch 4 times, most recently from 21efbc4 to 3745bae Compare February 22, 2020 00:46
@nateberkopec
Copy link
Member

Hmmm... I wonder if those windows failures are related or not. I feel like I've seen stalls on that test before, though not specifically on Windows.

@nateberkopec
Copy link
Member

Pinging @MSP-Greg for the possible test stability issue here.

@nateberkopec nateberkopec added the waiting-for-review Waiting on review from anyone label Feb 24, 2020
@@ -5,6 +5,7 @@
require 'puma/plugin'

require 'time'
require 'json'
Copy link
Member

Choose a reason for hiding this comment

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

Currently we only require json for the status app, which isn't that big of a deal. This would definitely require json for most people running Puma.

Copy link
Member

Choose a reason for hiding this comment

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

For example, this would, I believe, introduce a new native extension during startup, which I believe affects other open PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought requiring json would be straightforward as part of the standard library, but I did find it unusual that it was being intentionally avoided. If loading json does introduce some known stability/performance issue on any of the supported platforms (I'm less familiar with non-linux/MRI platforms) it makes sense to not accept this change.

Copy link
Member

Choose a reason for hiding this comment

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

I checked with Evan and there was no special reason. This is good to 🚢

@@ -1045,5 +1045,10 @@ def self.current
def shutting_down?
@status == :stop || @status == :restart
end

def stats
stat_names = %i(backlog running pool_capacity max_threads requests_count)
Copy link
Member

Choose a reason for hiding this comment

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

Would prefer these stats in a constant somewhere.

test/test_cli.rb Outdated
@@ -93,7 +93,7 @@ def test_control_for_ssl

expected_stats = /{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","backlog":0,"running":0,"pool_capacity":16,"max_threads":16,"requests_count":0}/
assert_match(expected_stats, body.split(/\r?\n/).last)
assert_equal([:started_at, :backlog, :running, :pool_capacity, :max_threads, :requests_count], Puma.stats.keys)
assert_equal(JSON.parse(body.split(/\r?\n/).last, symbolize_names: true), Puma.stats)
Copy link
Member

Choose a reason for hiding this comment

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

We already checked the entire response on the previous line. IMO, this test is redundant.

test/test_cli.rb Outdated
@@ -137,6 +137,7 @@ def test_control_clustered
s.close

assert_match(/\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","workers":2,"phase":0,"booted_workers":2,"old_workers":0,"worker_status":\[\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","pid":\d+,"index":0,"phase":0,"booted":true,"last_checkin":"[^"]+","last_status":\{"backlog":0,"running":2,"pool_capacity":2,"max_threads":2,"requests_count":0\}\},\{"started_at":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z","pid":\d+,"index":1,"phase":0,"booted":true,"last_checkin":"[^"]+","last_status":\{"backlog":0,"running":2,"pool_capacity":2,"max_threads":2,"requests_count":0\}\}\]\}/, body.split("\r\n").last)
assert_equal(Puma.stats, JSON.parse(body.split("\r\n").last, symbolize_names: true))
Copy link
Member

Choose a reason for hiding this comment

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

Same here, redundant test.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can test Puma.stats output separately in another test if we don't do that explicitly anywhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I added these lines because my first iteration of this PR unintentionally changed keys in Puma.stats from symbols to strings (JSON.parse default) without failing any tests, so I thought that behavior should be covered by an extra assertion somewhere. I can move it to a new test to make it more clear.

@nateberkopec nateberkopec added waiting-for-changes Waiting on changes from the requestor and removed waiting-for-review Waiting on review from anyone labels Feb 24, 2020
@MSP-Greg
Copy link
Member

@nateberkopec

I did notice those tests failures. I'll have a look.

@MSP-Greg
Copy link
Member

@nateberkopec

I grabbed the PR, and ran tests in my fork, both standard and using the --seed value from the failed CI here. Both passed.

I then quickly tried using 2.6.5 locally, and things got weird. It had Bundler 1.7.2 installed, when I upgraded both RubyGems & Bundler with gem update --system, everything was fine. Hence, I can't determine what happened here. Maybe some moon phase thing. Sorry.

But, all the master/head are passing. Just thought I'd point that out. Also, maybe PR #2126

@nateberkopec
Copy link
Member

nateberkopec commented Feb 25, 2020

Starting a test re-run. Still needs my code concerns addressed. I'm discussing the json thing with Evan.

EDIT: We're good to require json.

@wjordan wjordan force-pushed the json_cluster_stats branch 6 times, most recently from 5a42fc3 to e755788 Compare April 13, 2020 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance waiting-for-changes Waiting on changes from the requestor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants