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

#1884 User distribution should happen when new workers comes in #1886

Merged
merged 2 commits into from
Sep 20, 2021

Conversation

tyge68
Copy link
Contributor

@tyge68 tyge68 commented Sep 15, 2021

  • Adding a new argument to enable automatic rebalancing for new workers (after ramp up completed)
  • Log message added when rebalancing is triggered

To be checked how to test such code change.

cc @cyberw @mboutet

@cyberw
Copy link
Collaborator

cyberw commented Sep 15, 2021

I only had a quick look, but I dont see anything bad about this.

report.html Outdated
@@ -0,0 +1,353 @@
<!DOCTYPE html>
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this file committed inadvertently?

@mboutet
Copy link
Contributor

mboutet commented Sep 15, 2021

I know I said yesterday that the background task was my preferred solution, but looking back at the code, I remembered I started to implement it in a more efficient fashion.

It is more efficient because rebalancing only takes place whenever a new worker joins or if a worker goes missing instead of always rebalancing even if there's nothing to rebalance. It is also faster to react because it acts immediately upon a worker joining or missing.

The logic for a worker joining goes here:

locust/locust/runners.py

Lines 880 to 883 in bb77344

# if self.state == STATE_RUNNING or self.state == STATE_SPAWNING:
# # TODO: Necessary now that UsersDispatcher handles that?
# # balance the load distribution when new client joins
# self.start(self.target_user_count, self.spawn_rate)

The logic for a worker that goes missing goes here:

# TODO: If status is `STATE_RUNNING`, call self.start()

@mboutet
Copy link
Contributor

mboutet commented Sep 15, 2021

Also note that because of a regression I introduced in #1809 and that was reported in #1883, the target_user_count on the master is currently not updated until the end of the ramp-up/down. This should be fixed in an upcoming PR. I mention it because it means it could potentially cause a rebalancing to be messed up.

@tyge68
Copy link
Contributor Author

tyge68 commented Sep 15, 2021

@mboutet indeed it would make more sense to implement it in the event, currently I have added a new integration test, it would currently fail without my changes, but I will try to fix it in the event code instead.

tyge68 added a commit to tyge68/locust that referenced this pull request Sep 15, 2021
tyge68 added a commit to tyge68/locust that referenced this pull request Sep 15, 2021
@mboutet
Copy link
Contributor

mboutet commented Sep 15, 2021

I saw in one of your latest commit that you removed the --enable-rebalancing flag. However, I believe it is still needed because some people might not want the rebalancing behaviour.

@tyge68
Copy link
Contributor Author

tyge68 commented Sep 15, 2021

@mboutet ok sure I will add it back

@tyge68 tyge68 marked this pull request as ready for review September 15, 2021 14:31
@tyge68
Copy link
Contributor Author

tyge68 commented Sep 15, 2021

@cyberw @mboutet I think it should be ready for final review now, let me know if the last changes and the added test is good enough.

@mboutet
Copy link
Contributor

mboutet commented Sep 15, 2021

Can you address the comment I left: #1886 (comment)

@@ -637,6 +638,9 @@ def on_quitting(environment, **kw):

self.environment.events.quitting.add_listener(on_quitting)

def rebalancingEnabled(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def rebalancingEnabled(self):
@property
def rebalancing_enabled(self) -> bool:

# # TODO: Necessary now that UsersDispatcher handles that?
# # balance the load distribution when new client joins
# self.start(self.target_user_count, self.spawn_rate)
if self.rebalancingEnabled() and self.state == STATE_RUNNING and self.spawning_completed:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if self.rebalancingEnabled() and self.state == STATE_RUNNING and self.spawning_completed:
if self.rebalancing_enabled and self.state == STATE_RUNNING and self.spawning_completed:

action="store_true",
default=False,
dest="enable_rebalancing",
help="Allow to automatically rebalance users if new workers are added after ramp up completed.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
help="Allow to automatically rebalance users if new workers are added after ramp up completed.",
help="Allow to automatically rebalance users if new workers are added or removed during a test run.",

@mboutet
Copy link
Contributor

mboutet commented Sep 15, 2021

@tyge68, the re-balancing also needs to be implemented when a worker goes missing:

# TODO: If status is `STATE_RUNNING`, call self.start()

Also, I'll have to revisit my code, because according to this:

locust/locust/runners.py

Lines 873 to 875 in a278fb3

if not self._users_dispatcher.dispatch_in_progress and self.state == STATE_RUNNING:
# TODO: Test this situation
self.start(self.target_user_count, self.spawn_rate)

I already implemented the rebalance when a worker joins. Apparently, it doesn't work, otherwise @tyge68 would have not reported the issue. I'll try to look into this soon.

@@ -717,7 +717,7 @@ def addWorker():
def checkRebalancedTrue():
Copy link
Contributor

Choose a reason for hiding this comment

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

In Python, snake_case is preferred. There's some old library such as unittest that uses camelCase (i.e. self.assertTrue), but this is only because the standard library needs to keep backward compatibility.

tyge68 added a commit to tyge68/locust that referenced this pull request Sep 15, 2021
# This is the 1st commit message:

locustio#1884 User distribution should happen when new workers comes in

# This is the commit message locustio#2:

locustio#1884 reformatting

# This is the commit message locustio#3:

locustio#1884 Make rebalance interval configurable

# This is the commit message locustio#4:

locustio#1884 Adding integration test for rebalanced

# This is the commit message locustio#5:

locustio#1884 revert change

# This is the commit message locustio#6:

locustio#1886 Simplify solution

# This is the commit message locustio#7:

locustio#1886 adding missing dependency

# This is the commit message locustio#8:

locustio#1884 Adding back enable rebalancing option

# This is the commit message locustio#9:

locustio#1884 adding extra comment the test

# This is the commit message locustio#10:

locustio#1884 renaming as suggested

# This is the commit message locustio#11:

locustio#1884 update help description

# This is the commit message locustio#12:

locustio#1884 handling of missing workers to rebalance
@tyge68 tyge68 force-pushed the issue/1884 branch 2 times, most recently from 9e888fd to 2b09b79 Compare September 15, 2021 16:30
@tyge68
Copy link
Contributor Author

tyge68 commented Sep 15, 2021

@mboutet I have applied the last suggested changes regarding the snake_case, and cleanup the commit history.
you can recheck the new test eventually , that would also be a good one to try to fix the original line of code that should have worked.

@cyberw
Copy link
Collaborator

cyberw commented Sep 18, 2021

LGTM, except for the remaining camelCase method (checkRebalancedTrue) in the test

@tyge68
Copy link
Contributor Author

tyge68 commented Sep 20, 2021

@cyberw indeed, fixed casing issue :)

@cyberw
Copy link
Collaborator

cyberw commented Sep 20, 2021

Just waiting for any comments from @mboutet ...

@mboutet
Copy link
Contributor

mboutet commented Sep 20, 2021

Sorry for not getting back sooner. The changes and the test look good. I haven't tested the branch locally though.

@cyberw cyberw merged commit 87b6dbc into locustio:master Sep 20, 2021
@mboutet
Copy link
Contributor

mboutet commented Sep 20, 2021

Thanks @tyge68

@cyberw
Copy link
Collaborator

cyberw commented Sep 20, 2021

Thanks! I'll trust the tests to report any errors (and the poor sods running latest master/prerelease version :)

@mboutet
Copy link
Contributor

mboutet commented Sep 20, 2021

I'm one of them 😜

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.

3 participants