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

Round robin pool selection #45

Closed
wants to merge 3 commits into from
Closed

Round robin pool selection #45

wants to merge 3 commits into from

Conversation

sneako
Copy link
Owner

@sneako sneako commented Apr 29, 2020

I used the :atomics module, so that means we would no longer support OTP versions < 21.2. We could fallback to an :ets counter if we feel that supporting older OTP versions is necessary. Or we could possibly just not allow this strategy in older versions

@sneako sneako requested a review from keathley April 29, 2020 11:47
@@ -0,0 +1,27 @@
defmodule Finch.Pool.Strategy do
Copy link
Owner Author

@sneako sneako Apr 29, 2020

Choose a reason for hiding this comment

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

This module should probably be private/@moduledoc false, but i felt the documentation could be useful when implementing additional strategies

{:ok, _} = Registry.register(registry, shp, [])
{:ok, {shp, opts}}
def init_pool({registry, state}) do
{:ok, _} = Registry.register(registry, state.shp, state.registry_value)
Copy link
Owner Author

Choose a reason for hiding this comment

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

I realize that we will have this registry value duplicated for every single pool, but I think this is the best way to quickly look it up at the same time as the pool pid.


@impl true
def choose_pool([{_, %{counter: counter, count: count}} | _] = pids) do
index = :counters.get(counter, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately I think this does not really guarantee round robin, because you can have handle_enqueue being called multiple times before you get to choose the pool. Or you choose the pool multiple times before handle_enqueue is called and bumped. You think you would need to use ets:update_counter if you want an actual round robin.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ahh right of course :( Ok, I'll switch to the ets counter. Thanks for pointing that out!

Copy link
Contributor

Choose a reason for hiding this comment

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

You can probably use the atomics module that has a add_get function

Copy link
Owner Author

Choose a reason for hiding this comment

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

That's right! I try that out

lib/finch.ex Outdated Show resolved Hide resolved
@sneako sneako force-pushed the round-robin branch 2 times, most recently from 46ccef8 to e806473 Compare May 16, 2020 15:29

@impl true
def registry_value(%{count: count}) do
atomics = :atomics.new(1, [])
Copy link

Choose a reason for hiding this comment

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

I think you might want this to be unsigned. I think you're relying on overflow behavior and this is going into undefined behavior as it's eventually a signed-int overflow. I could be wrong.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Great catch! I am relying on the overflow behaviour, but you're right that if the value becomes negative, things will break. Thank you!

Copy link
Contributor

@michalmuskala michalmuskala May 18, 2020

Choose a reason for hiding this comment

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

AFAIK atomics use intrinsics that guarantee proper wraparound for signed integers as well, but yeah you need to be prepared for negative numbers. Unsigned is probably simpler 👍

wingyplus added a commit to wingyplus/finch that referenced this pull request Jul 14, 2021
Change `node.pid` into `node.vm_pid` in MetadataFormatters.ELK.
@sneako
Copy link
Owner Author

sneako commented Apr 24, 2023

I did not see any noticeable improvement from these changes in my benchmarks so I left this PR sitting around for that reason. I am definitely still interested in looking in to alternative pool selection strategies, but I will close this PR because it is so outdated.

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.

4 participants