Skip to content

Commit

Permalink
Fast show socket map in vars (#2376)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright authored Oct 11, 2023
1 parent 3e87b91 commit 72036f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
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

0 comments on commit 72036f1

Please sign in to comment.