From c0121591db3ef20717492b2fe9bf9f98ce20fbdd Mon Sep 17 00:00:00 2001 From: Kaijie Gu <2459548460@qq.com> Date: Wed, 14 Aug 2024 22:56:20 +0800 Subject: [PATCH] fix error after merge --- src/client.cc | 17 +++++++---------- src/client.h | 7 +++---- src/cmd_admin.cc | 10 +++++----- src/pikiwidb.cc | 4 +++- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/client.cc b/src/client.cc index 6edcefa4d..70a45cd5f 100644 --- a/src/client.cc +++ b/src/client.cc @@ -21,7 +21,7 @@ namespace pikiwidb { -const ClientInfo ClientInfo::invalidClientInfo = {-1, "", -1, -1}; +const ClientInfo ClientInfo::invalidClientInfo = {-1, "", -1}; void CmdRes::RedisAppendLen(std::string& str, int64_t ori, const std::string& prefix) { str.append(prefix); @@ -453,7 +453,7 @@ void PClient::OnConnect() { std::string PClient::PeerIP() const { if (!addr_.IsValid()) { - ERROR("Invalid address detected for client {}", uniqueID()); + ERROR("Invalid address detected for client {}", GetUniqueID()); return ""; } return addr_.GetIP(); @@ -461,7 +461,7 @@ std::string PClient::PeerIP() const { int PClient::PeerPort() const { if (!addr_.IsValid()) { - ERROR("Invalid address detected for client {}", uniqueID()); + ERROR("Invalid address detected for client {}", GetUniqueID()); return 0; } return addr_.GetPort(); @@ -508,13 +508,10 @@ bool PClient::isClusterCmdTarget() const { return PRAFT.GetClusterCmdCtx().GetPeerIp() == PeerIP() && PRAFT.GetClusterCmdCtx().GetPort() == PeerPort(); } -uint64_t PClient::uniqueID() const { return GetConnId(); } +uint64_t PClient::GetUniqueID() const { return GetConnId(); } ClientInfo PClient::GetClientInfo() const { - if (auto c = getTcpConnection(); c) { - return {GetUniqueId(), PeerIP().c_str(), PeerPort(), GetFd()}; - } - return ClientInfo::invalidClientInfo; + return {GetUniqueID(), PeerIP().c_str(), PeerPort()}; } bool PClient::Watch(int dbno, const std::string& key) { @@ -524,12 +521,12 @@ bool PClient::Watch(int dbno, const std::string& key) { bool PClient::NotifyDirty(int dbno, const std::string& key) { if (IsFlagOn(kClientFlagDirty)) { - INFO("client is already dirty {}", GetUniqueId()); + INFO("client is already dirty {}", GetUniqueID()); return true; } if (watch_keys_[dbno].contains(key)) { - INFO("{} client become dirty because key {} in db {}", GetUniqueId(), key, dbno); + INFO("{} client become dirty because key {} in db {}", GetUniqueID(), key, dbno); SetFlag(kClientFlagDirty); return true; } else { diff --git a/src/client.h b/src/client.h index 3cc0a2f61..0e2cb842d 100644 --- a/src/client.h +++ b/src/client.h @@ -118,13 +118,13 @@ class DB; struct PSlaveInfo; struct ClientInfo { - int client_id; + int64_t client_id; std::string ip; int port; - int fd; static const ClientInfo invalidClientInfo; bool operator==(ClientInfo& ci) const { return client_id == ci.client_id; } }; + class PClient : public std::enable_shared_from_this, public CmdRes { public: // PClient() = delete; @@ -137,7 +137,6 @@ class PClient : public std::enable_shared_from_this, public CmdRes { std::string PeerIP() const; int PeerPort() const; const int GetFd() const; - int GetUniqueId() const; ClientInfo GetClientInfo() const; // bool SendPacket(const std::string& buf); @@ -227,6 +226,7 @@ class PClient : public std::enable_shared_from_this, public CmdRes { void SetAuth() { auth_ = true; } bool GetAuth() const { return auth_; } + uint64_t GetUniqueID() const; void RewriteCmd(std::vector& params) { parser_.SetParams(params); } void Reexecutecommand() { this->executeCommand(); } @@ -254,7 +254,6 @@ class PClient : public std::enable_shared_from_this, public CmdRes { int processInlineCmd(const char*, size_t, std::vector&); void reset(); bool isPeerMaster() const; - uint64_t uniqueID() const; bool isClusterCmdTarget() const; diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 862658583..79b252750 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -496,7 +496,7 @@ CmdClientId::CmdClientId(const std::string& name, int16_t arity) bool CmdClientId::DoInitial(PClient* client) { return true; } -void CmdClientId::DoCmd(PClient* client) { client->AppendInteger(client->GetUniqueId()); } +void CmdClientId::DoCmd(PClient* client) { client->AppendInteger(client->GetUniqueID()); } CmdClientKill::CmdClientKill(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsAdmin, kAclCategoryAdmin) {} @@ -573,8 +573,8 @@ void CmdClientList::DoCmd(PClient* client) { char buf[128]; for (auto& client_info : client_infos) { // client-> - snprintf(buf, sizeof(buf), "ID=%d IP=%s PORT=%d FD=%d\n", client_info.client_id, client_info.ip.c_str(), - client_info.port, client_info.fd); + snprintf(buf, sizeof(buf), "ID=%ld IP=%s PORT=%d\n", client_info.client_id, client_info.ip.c_str(), + client_info.port); client->AppendString(std::string(buf)); } break; @@ -591,8 +591,8 @@ void CmdClientList::DoCmd(PClient* client) { return; } char buf[128]; - snprintf(buf, sizeof(buf), "ID=%d IP=%s PORT=%d FD=%d\n", client_info.client_id, client_info.ip.c_str(), - client_info.port, client_info.fd); + snprintf(buf, sizeof(buf), "ID=%ld IP=%s PORT=%d\n", client_info.client_id, client_info.ip.c_str(), + client_info.port); client->AppendString(std::string(buf)); } catch (const std::exception& e) { client->SetRes(CmdRes::kErrOther, "Invalid client id"); diff --git a/src/pikiwidb.cc b/src/pikiwidb.cc index a4acc9c13..6246a252f 100644 --- a/src/pikiwidb.cc +++ b/src/pikiwidb.cc @@ -111,7 +111,7 @@ void PikiwiDB::OnNewConnection(uint64_t connId, std::shared_ptrSetSocketAddr(addr); client->OnConnect(); // add new PClient to clients - ClientMap::getInstance().AddClient(client->GetUniqueId(), client); + ClientMap::getInstance().AddClient(client->GetUniqueID(), client); } bool PikiwiDB::Init() { @@ -159,6 +159,7 @@ bool PikiwiDB::Init() { event_server_->SetOnCreate([](uint64_t connID, std::shared_ptr& client, const net::SocketAddr& addr) { client->SetSocketAddr(addr); client->OnConnect(); + ClientMap::getInstance().AddClient(client->GetUniqueID(), client); INFO("New connection from fd:{} IP:{} port:{}", connID, addr.GetIP(), addr.GetPort()); }); @@ -169,6 +170,7 @@ bool PikiwiDB::Init() { event_server_->SetOnClose([](std::shared_ptr& client, std::string&& msg) { INFO("Close connection id:{} msg:{}", client->GetConnId(), msg); client->OnClose(); + ClientMap::getInstance().RemoveClientById(client->GetUniqueID()); }); event_server_->InitTimer(10);