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

random robin by source ip #53

Open
wants to merge 2 commits into
base: rs-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/core/lib/iomgr/tcp_server_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,22 @@ static void on_read(void* arg, grpc_error_handle err) {
addr_uri->c_str());
}

// addr_str format: ipv4/ipv6:ipv6:port
std::size_t start = addr_str.find_first_of(":") + 1;
std::size_t end = addr_str.find(":", start);
std::string ip = addr_str.substr(start, end - start);

std::string name = absl::StrCat("tcp-server-connection:", addr_uri.value());
grpc_fd* fdobj = grpc_fd_create(fd, name.c_str(), true);

read_notifier_pollset = (*(sp->server->pollsets))
[static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
&sp->server->next_pollset_to_assign, 1)) %
sp->server->pollsets->size()];
std::size_t start_idx = static_cast<size_t>(rand()) % sp->server->pollsets->size();
if (!gpr_atm_no_barrier_cas(&sp->server->next_pollset_to_assign_ids[ip],0,start_idx)){
start_idx=static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
&sp->server->next_pollset_to_assign_ids[ip], 1)) %
sp->server->pollsets->size();
}

read_notifier_pollset = (*(sp->server->pollsets))[start_idx];
grpc_pollset_add_fd(read_notifier_pollset, fdobj);

// Create acceptor.
Expand Down
5 changes: 3 additions & 2 deletions src/core/lib/iomgr/tcp_server_utils_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "src/core/lib/iomgr/tcp_server.h"
#include "src/core/lib/iomgr/timer.h"
#include "src/core/lib/resource_quota/memory_quota.h"
#include <map>

// one listening port
typedef struct grpc_tcp_listener {
Expand Down Expand Up @@ -98,8 +99,8 @@ struct grpc_tcp_server {
// owned by this struct
const std::vector<grpc_pollset*>* pollsets = nullptr;

// next pollset to assign a channel to
gpr_atm next_pollset_to_assign = 0;
// next pollset to assign a channel to, it is a map from pollset name to ip address.
std::map<std::string, gpr_atm> next_pollset_to_assign_ids;
Copy link
Member

Choose a reason for hiding this comment

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

We should remove entry when a connection disconnected.

Copy link
Author

Choose a reason for hiding this comment

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

no, it records all connection's ip not one connection. such as
ip1: 11
ip2: 12


// Contains config extracted from channel args for this server
grpc_core::PosixTcpOptions options;
Expand Down