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

Fast show socket map in vars #2376

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
36 changes: 14 additions & 22 deletions src/brpc/socket_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,21 @@ void SocketMap::PrintSocketMap(std::ostream& os, void* arg) {
static_cast<SocketMap*>(arg)->Print(os);
}

void SocketMap::ShowSocketMapInBvarIfNeed() {
if (FLAGS_show_socketmap_in_vars &&
!_exposed_in_bvar.exchange(true, butil::memory_order_release)) {
char namebuf[32];
int len = snprintf(namebuf, sizeof(namebuf), "rpc_socketmap_%p", this);
_this_map_bvar = new bvar::PassiveStatus<std::string>(
butil::StringPiece(namebuf, len), PrintSocketMap, this);
}
}

int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
const std::shared_ptr<SocketSSLContext>& ssl_ctx,
bool use_rdma) {
ShowSocketMapInBvarIfNeed();

std::unique_lock<butil::Mutex> mu(_mutex);
SingleConnection* sc = _map.seek(key);
if (sc) {
Expand Down Expand Up @@ -251,18 +263,7 @@ int SocketMap::Insert(const SocketMapKey& key, SocketId* id,
SingleConnection new_sc = { 1, ptr.release(), 0 };
_map[key] = new_sc;
*id = tmp_id;
bool need_to_create_bvar = false;
if (FLAGS_show_socketmap_in_vars && !_exposed_in_bvar) {
_exposed_in_bvar = true;
need_to_create_bvar = true;
}
mu.unlock();
if (need_to_create_bvar) {
char namebuf[32];
int len = snprintf(namebuf, sizeof(namebuf), "rpc_socketmap_%p", this);
_this_map_bvar = new bvar::PassiveStatus<std::string>(
butil::StringPiece(namebuf, len), PrintSocketMap, this);
}
return 0;
}

Expand All @@ -273,6 +274,8 @@ void SocketMap::Remove(const SocketMapKey& key, SocketId expected_id) {
void SocketMap::RemoveInternal(const SocketMapKey& key,
SocketId expected_id,
bool remove_orphan) {
ShowSocketMapInBvarIfNeed();

std::unique_lock<butil::Mutex> mu(_mutex);
SingleConnection* sc = _map.seek(key);
if (!sc) {
Expand All @@ -293,18 +296,7 @@ void SocketMap::RemoveInternal(const SocketMapKey& key,
} else {
Socket* const s = sc->socket;
_map.erase(key);
bool need_to_create_bvar = false;
if (FLAGS_show_socketmap_in_vars && !_exposed_in_bvar) {
_exposed_in_bvar = true;
need_to_create_bvar = true;
}
mu.unlock();
if (need_to_create_bvar) {
char namebuf[32];
int len = snprintf(namebuf, sizeof(namebuf), "rpc_socketmap_%p", this);
_this_map_bvar = new bvar::PassiveStatus<std::string>(
butil::StringPiece(namebuf, len), PrintSocketMap, this);
}
s->ReleaseAdditionalReference(); // release extra ref
s->SetHCRelatedRefReleased(); // set released status to cancel health checking
SocketUniquePtr ptr(s); // Dereference
Expand Down
3 changes: 2 additions & 1 deletion src/brpc/socket_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class SocketMap {
static void* RunWatchConnections(void*);
void Print(std::ostream& os);
static void PrintSocketMap(std::ostream& os, void* arg);
void ShowSocketMapInBvarIfNeed();

private:
struct SingleConnection {
Expand All @@ -190,7 +191,7 @@ class SocketMap {
SocketMapOptions _options;
butil::Mutex _mutex;
Map _map;
bool _exposed_in_bvar;
butil::atomic<bool> _exposed_in_bvar;
bvar::PassiveStatus<std::string>* _this_map_bvar;
bool _has_close_idle_thread;
bthread_t _close_idle_thread;
Expand Down
Loading