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 all commits
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
61 changes: 46 additions & 15 deletions src/core/lib/iomgr/tcp_server_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@ static grpc_error_handle CreateEventEngineListener(
acceptor->from_server = s;
acceptor->port_index = -1;
acceptor->fd_index = -1;
std::size_t cq_idx=0;
if (!is_external) {
auto it = s->listen_fd_to_index_map.find(listener_fd);
if (it != s->listen_fd_to_index_map.end()) {
acceptor->port_index = std::get<0>(it->second);
acceptor->fd_index = std::get<1>(it->second);
}
cq_idx=static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
&s->next_pollset_to_assign_ids[""], 1)) % s->pollsets->size();
} else {
// External connection handling.
grpc_resolved_address addr;
Expand All @@ -131,6 +134,18 @@ static grpc_error_handle CreateEventEngineListener(
}
(void)grpc_set_socket_no_sigpipe_if_possible(fd);
auto addr_uri = grpc_sockaddr_to_uri(&addr);

std::string addr_str = addr_uri.value();
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);

cq_idx = static_cast<size_t>(rand()) % s->pollsets->size();
if (!gpr_atm_no_barrier_cas(&s->next_pollset_to_assign_ids[ip],0,cq_idx)){
cq_idx=static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
&s->next_pollset_to_assign_ids[ip], 1)) % s->pollsets->size();
}

if (!addr_uri.ok()) {
gpr_log(GPR_ERROR, "Invalid address: %s",
addr_uri.status().ToString().c_str());
Expand All @@ -142,10 +157,7 @@ static grpc_error_handle CreateEventEngineListener(
addr_uri->c_str());
}
}
grpc_pollset* read_notifier_pollset =
(*(s->pollsets))[static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
&s->next_pollset_to_assign, 1)) %
s->pollsets->size()];
grpc_pollset* read_notifier_pollset = (*(s->pollsets))[cq_idx];
acceptor->external_connection = is_external;
acceptor->listener_fd = listener_fd;
grpc_byte_buffer* buf = nullptr;
Expand Down Expand Up @@ -253,7 +265,7 @@ static grpc_error_handle tcp_server_create(grpc_closure* shutdown_complete,
GPR_ASSERT(s->on_accept_cb);
s->memory_quota = s->options.resource_quota->memory_quota();
s->pre_allocated_fd = -1;
gpr_atm_no_barrier_store(&s->next_pollset_to_assign, 0);
gpr_atm_no_barrier_store(&s->next_pollset_to_assign_ids[""], 0);
s->n_bind_ports = 0;
new (&s->listen_fd_to_index_map)
absl::flat_hash_map<int, std::tuple<int, int>>();
Expand Down Expand Up @@ -445,14 +457,23 @@ static void on_read(void* arg, grpc_error_handle err) {
addr_uri->c_str());
}

std::string name = absl::StrCat("tcp-server-connection:", addr_uri.value());
// addr_str format: ipv4/ipv6:ipv6:port
std::string addr_str = addr_uri.value();
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_str);
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 cq_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,cq_idx)){
cq_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))[cq_idx];
grpc_pollset_add_fd(read_notifier_pollset, fdobj);

// Create acceptor.
Expand Down Expand Up @@ -902,12 +923,22 @@ class ExternalConnectionHandler : public grpc_core::TcpServerFdHandler {
gpr_log(GPR_INFO, "SERVER_CONNECT: incoming external connection: %s",
addr_uri->c_str());
}
std::string name = absl::StrCat("tcp-server-connection:", addr_uri.value());

// addr_str format: ipv4/ipv6:ipv6:port
std::string addr_str = addr_uri.value();
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_str);
grpc_fd* fdobj = grpc_fd_create(fd, name.c_str(), true);
read_notifier_pollset =
(*(s_->pollsets))[static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
&s_->next_pollset_to_assign, 1)) %
s_->pollsets->size()];

std::size_t cq_idx = static_cast<size_t>(rand()) % s_->pollsets->size();
if (!gpr_atm_no_barrier_cas(&s_->next_pollset_to_assign_ids[ip],0,cq_idx)){
cq_idx=static_cast<size_t>(gpr_atm_no_barrier_fetch_add(
&s_->next_pollset_to_assign_ids[ip], 1)) % s_->pollsets->size();
}
read_notifier_pollset =(*(s_->pollsets))[cq_idx];
grpc_pollset_add_fd(read_notifier_pollset, fdobj);
grpc_tcp_server_acceptor* acceptor =
static_cast<grpc_tcp_server_acceptor*>(gpr_malloc(sizeof(*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