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

Add concurrency option #7

Merged
merged 1 commit into from
Apr 17, 2015
Merged

Conversation

dudemcbacon
Copy link
Contributor

Adding concurrency option to control the number of concurrent hosts that SSH is run on.

@@ -1,6 +1,7 @@
require 'hammer_cli'
require 'hammer_cli_foreman/host'
require 'net/ssh/multi'
require 'pry'
Copy link
Contributor

Choose a reason for hiding this comment

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

leftover from testing? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Derp. Let me get rid of that...

@dudemcbacon
Copy link
Contributor Author

I also noticed there were some race conditions affecting net-ssh-multi when setting concurrent_connections -- fortunately, they have been fixed in an upstream pull request. I've updated the gemspec to reflect the new dependency.

See this pull request for more information on the race conditions.

@mmoll
Copy link
Contributor

mmoll commented Apr 12, 2015

Thats, OK, we can handle the version dependency in our packaging, I guess...
@ohadlevy, could you test?

@mmoll
Copy link
Contributor

mmoll commented Apr 13, 2015

@dudemcbacon BTW, could you squash your commits together into one?

@dudemcbacon
Copy link
Contributor Author

Done!

@@ -12,6 +12,7 @@ class Command < HammerCLIForeman::Command

command_name 'SSH to hosts'
option %w(-c --command), 'COMMAND', _('Command to execute'), :attribute_name => :command, :required => true
option %w(-n --concurrent), 'CONCURRENCY', _('Number of concurrent SSH sessions'), :attribute_name => :concurrent
Copy link
Member

Choose a reason for hiding this comment

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

should we provide a default? i think asking each time could be annoying ?

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'd actually prefer if we didn't. It'd make the code more complex. When the option isn't set and no default is specified concurrent == nil. When concurrent_connections is set to nil (and passed to Net::SSH::Multi) it disables the concurrency limit. If we set a default value for concurrent it will be set to whatever the default is even if the option isn't specified. We'd either need to add an option to disable concurrency limits or use some other method to determine when the option wasn't actually passed by the user.

Copy link
Contributor

Choose a reason for hiding this comment

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

"it disables the concurrency limit." means 1 or infinity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

infinity -- as in the way it behaved before this pull request.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess if it's like before I'm OK with it - and see this PR even more valuable as it's making a limit possible. 🎯

Copy link
Member

Choose a reason for hiding this comment

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

You can add

option ....... do |o|
  Integer(o)
end

for Hammer to ensure valid Integer is given

+1 to not force user to set concurrency limit

@dudemcbacon
Copy link
Contributor Author

I have modified the pull request slightly in the following ways:

  1. move the logic for concurrent to the top of the execute method -- that way if the parameter supplied for concurrent is 0 (or anything that the .to_i method might turn into 0) it will display an error message before the host list and confirmation prompt.
  2. Added a variable 'limit' which is set to either nil or an integer depending on whether the user sets the concurrent option when they run hammer.
  3. Concurrent_connections is set to the limit variable (as either nil or an integer).

This fixes the assumption I made about the .to_i method when used with nil and assures the following behavior:

  1. If the concurrent option is specified with an integer > 0 then the number of concurrent connections Net::SSH::Multi will allow is set to that integer.
  2. If the concurrent option is not specified than the original behavior of not limiting the number of concurrent_connections will be present.

@@ -28,6 +29,13 @@ def request_params
end

def execute
if concurrent && concurrent.to_i == 0
warn _("specify 1 or more concurrent hosts")
return HammerCLI::EX_OK
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to

signal_usage_error(_("specify 1 or more concurrent hosts")) if (!concurrent.nil? && concurrent < 1)

to get proper exit code and error handling

@mbacovsky
Copy link
Member

Besides the minor things mentioned inline it looks good. Thanks @dudemcbacon!

@mbacovsky
Copy link
Member

@dudemcbacon, could you please rebase?

@dudemcbacon
Copy link
Contributor Author

I have rebased this pull request and modified the code as suggested by @mbacovsky.

@@ -12,6 +12,10 @@ class Command < HammerCLIForeman::Command

command_name 'SSH to hosts'
option %w(-c --command), 'COMMAND', _('Command to execute'), :attribute_name => :command, :required => true
option %w(-n --concurrent), 'CONCURRENCY', _('Number of concurrent SSH sessions'), :attribute_name => :concurrent do |o|
Integer(o)
o.nil? ? o : o.to_i
Copy link
Member

Choose a reason for hiding this comment

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

this line is not neccessary, as the block is executed only if the parameter was set

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, good catch. I guess I didn't think about it that much. I will remove it.

@mbacovsky
Copy link
Member

@dudemcbacon, thanks for the update! Looks good now!
I'm curious about the extra line in the option block, was it added to any unexpected behaviour?

…hat SSH is run on; also updating gemspec dependency for net-ssh-multi to address race conditions affecting that gem and concurrency.
@dudemcbacon
Copy link
Contributor Author

I have removed the line you commented on in the option block.

@dudemcbacon dudemcbacon reopened this Apr 16, 2015
@dudemcbacon
Copy link
Contributor Author

The line you're referring to was to ensure that the concurrent variable was set to either nil or a number > 0. As you said in your inline comment, it's not necessary. If the option block is called it will be a number. If the option block is not called, it will be nil. If the integer is 0, it will be filtered by the signal_usage_error method.

@mbacovsky
Copy link
Member

👍 @dudemcbacon, that makes sense. Thank you for your patience ;) 🍺 Merging...

mbacovsky added a commit that referenced this pull request Apr 17, 2015
@mbacovsky mbacovsky merged commit 4abe922 into theforeman:master Apr 17, 2015
@dudemcbacon
Copy link
Contributor Author

w00t!

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.

None yet

4 participants