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

WIP: Consistent hashing for balancer #258

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

TheRandomCharacter
Copy link
Collaborator

Integrates consistent hashing library into yanet.
Adds option to create "chash" type balancer services.

Comment on lines +890 to +889
auto reals_wlc = reals_wlc_weight.find(key);
if (reals_wlc != reals_wlc_weight.end() && reals_wlc->second.has_value())
{
effective_weight = reals_wlc->second.value();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll add comments that I've left at Levon's pr, since you will be merging his commit now:

We will have code duplication here with the following lines:

auto it = reals_enabled.find(key);
if (it != reals_enabled.end())
{
	if (it->second.has_value())
	{
		effective_weight = it->second.value();
	}
}

Mayve we can add a private method in balancer_t, smth like

 template <typename Map>
    std::optional<typename Map::mapped_type::value_type> get_effective_weight(const Map& map, const balancer::real_key_global_t& key) const {
        auto it = map.find(key);
        if (it != map.end() && it->second.has_value()) {
            return it->second.value();
        }
        return std::nullopt;
    }

And then use it, for example, with compound initialization:

if (auto found_weight = get_effective_weight(reals_wlc_weight, key); found_weight) {
    effective_weight = *found_weight;
}

controlplane/balancer.cpp Outdated Show resolved Hide resolved
Comment on lines +996 to +1001
uint32_t effective_weight = weight;
{
auto it = reals_enabled.find(key);
if (it != reals_enabled.end())
{
if (it->second.has_value())
{
effective_weight = it->second.value();
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto found_weight = get_effective_weight(reals_enabled, key);
uint32_t effective_weight = found_weight ? *found_weight : weight;

controlplane/balancer.cpp Outdated Show resolved Hide resolved
controlplane/balancer.cpp Outdated Show resolved Hide resolved
controlplane/balancer.cpp Outdated Show resolved Hide resolved
Comment on lines +1078 to +1074
auto wlc_ratio = std::max(1.0, wlc_power * (1 - 1.0 * connections * weight_sum / connection_sum / weight));
auto wlc_weight = (uint32_t)(weight * wlc_ratio);
Copy link
Collaborator

Choose a reason for hiding this comment

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

that's obscure. I suggest:

    auto scaled_connections = static_cast<double>(connections * weight_sum);
    auto scaled_weight = static_cast<double>(connection_sum * weight);
    double connection_ratio = scaled_connections / scaled_weight;

    double wlc_ratio = std::max(min_ratio, wlc_power * (1.0 - connection_ratio));

    auto new_weight = static_cast<uint32_t>(std::round(weight * wlc_ratio));

dataplane/globalbase.cpp Outdated Show resolved Hide resolved
dataplane/globalbase.cpp Outdated Show resolved Hide resolved
dataplane/globalbase.cpp Outdated Show resolved Hide resolved
dataplane/globalbase.cpp Outdated Show resolved Hide resolved
dataplane/globalbase.cpp Outdated Show resolved Hide resolved
dataplane/globalbase.cpp Outdated Show resolved Hide resolved
dataplane/globalbase.cpp Outdated Show resolved Hide resolved
dataplane/type.h Show resolved Hide resolved
.gitmodules Show resolved Hide resolved
dataplane/meson.build Outdated Show resolved Hide resolved
@TheRandomCharacter TheRandomCharacter changed the title Consistent hashing for balancer WIP: Consistent hashing for balancer Dec 12, 2024
@TheRandomCharacter TheRandomCharacter force-pushed the chash branch 2 times, most recently from e8acf78 to 89adefa Compare December 12, 2024 12:33
Comment on lines 1616 to 1617
utils::Deferer defer([]() { YADECAP_MEMORY_BARRIER_COMPILE; });
for (uint32_t real_idx = service->real_start;
Copy link
Collaborator

@ol-imorozko ol-imorozko Dec 12, 2024

Choose a reason for hiding this comment

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

IMHO this makes code unnecessary complex. Maybe with more complex logic it will be fine, but here I feel like just doing

//...
	if (start == do_not_exceed)
	
		YANET_LOG_ERROR("Balancer service exceeded ring chunk bounds\n");
		YADECAP_MEMORY_BARRIER_COMPILE
		return start;
	}
//...
	YADECAP_MEMORY_BARRIER_COMPILE;
	return start;
}

is sufficient enough

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We need to YADECAP_MEMORY_BARRIER_COMPILE at each function exit path. Using it at each existing function exit doesn't convey this intent to future function changes.

How about using a DEFERED_MEMORY_BARRIER_COMPILE macro?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, maybe your solution makes more sense that I've thought initially.
smth like this will suffice?

struct MemoryBarrierGuard {
    ~MemoryBarrierGuard() {
        YADECAP_MEMORY_BARRIER_COMPILE;
    }
};

And do that at the start of every method:

Result cControlPlane::init(bool use_kernel_interface)
{
    MemoryBarrierGuard guard;
    //...

@ol-imorozko
Copy link
Collaborator

ol-imorozko commented Dec 23, 2024

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