From 6f59c353fd72a9a3ecd959b168e01f8ceb24228e Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 02:10:03 +0800 Subject: [PATCH 01/33] feat: support ipv6 --- .clang-tidy | 2 +- etc/conf/kiwi.conf | 5 +- src/kiwi.cc | 4 ++ src/net/base_socket.cc | 13 ++++- src/net/base_socket.h | 6 ++- src/net/client_socket.cc | 10 +++- src/net/event_server.h | 4 ++ src/net/listen_socket.cc | 35 ++++++++++---- src/net/socket_addr.h | 101 ++++++++++++++++++++++++++++++++------- 9 files changed, 146 insertions(+), 34 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index d15157f..92d5545 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -57,7 +57,7 @@ Checks: ' # - { key: readability-identifier-naming.UnionCase, value: CamelCase } # - { key: readability-identifier-naming.VariableCase, value: lower_case } -WarningsAsErrors: '*' +WarningsAsErrors: '' # HeaderFilterRegex: '(|/src|/src/net|/src/pstd|/src/storage)/include' # HeaderFilterRegex: '/src/(net|storage|pstd)/include' diff --git a/etc/conf/kiwi.conf b/etc/conf/kiwi.conf index 380bde0..0029dee 100644 --- a/etc/conf/kiwi.conf +++ b/etc/conf/kiwi.conf @@ -10,7 +10,8 @@ port 9221 # If you want you can bind a single interface, if the bind option is not # specified all the interfaces will listen for incoming connections. # -ip 127.0.0.1 +ip ::1 +# ip 127.0.0.1 # Close the connection after a client is idle for N seconds (0 to disable) @@ -349,4 +350,4 @@ rocksdb-periodic-second 259200; ############################### RAFT ############################### use-raft no # Braft relies on brpc to communicate via the default port number plus the port offset -raft-port-offset 10 \ No newline at end of file +raft-port-offset 10 diff --git a/src/kiwi.cc b/src/kiwi.cc index 1ac5bb4..a2fe54c 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -202,6 +202,10 @@ bool KiwiDB::Init() { net::SocketAddr addr(g_config.ip.ToString(), g_config.port.load()); INFO("Add listen addr:{}, port:{}", g_config.ip.ToString(), g_config.port.load()); + if (!addr.IsValid()) { + ERROR("Invalid listen addr"); + return false; + } event_server_->AddListenAddr(addr); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index 3bb382c..84c11aa 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -14,7 +14,9 @@ namespace net { -int BaseSocket::CreateTCPSocket() { return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); } +int BaseSocket::CreateTCPSocketIpv4() { return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); } + +int BaseSocket::CreateTCPSocketIpv6() { return ::socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); } int BaseSocket::CreateUDPSocket() { return ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); } @@ -87,6 +89,15 @@ bool BaseSocket::SetReusePort() { return false; } +bool BaseSocket::SetIpv6Only() { + int ipv6only = 0; + if (::setsockopt(Fd(), IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&ipv6only), sizeof(ipv6only)) == -1) { + WARN("SetIpv6Only fd:{} error:{}", Fd(), errno); + return false; + } + return true; +} + bool BaseSocket::GetLocalAddr(SocketAddr &addr) { sockaddr_in localAddr{}; socklen_t len = sizeof(localAddr); diff --git a/src/net/base_socket.h b/src/net/base_socket.h index a1c8b39..07a317a 100644 --- a/src/net/base_socket.h +++ b/src/net/base_socket.h @@ -38,7 +38,9 @@ class BaseSocket : public NetEvent { void Close() override; - static int CreateTCPSocket(); + static int CreateTCPSocketIpv4(); + + static int CreateTCPSocketIpv6(); static int CreateUDPSocket(); @@ -57,6 +59,8 @@ class BaseSocket : public NetEvent { bool SetReusePort(); + bool SetIpv6Only(); + bool GetLocalAddr(SocketAddr &); bool GetPeerAddr(SocketAddr &); diff --git a/src/net/client_socket.cc b/src/net/client_socket.cc index f3d318c..f440f45 100644 --- a/src/net/client_socket.cc +++ b/src/net/client_socket.cc @@ -12,7 +12,7 @@ namespace net { bool ClientSocket::Connect() { - fd_ = CreateTCPSocket(); + fd_ = CreateTCPSocketIpv4(); if (fd_ == -1) { onConnectFail_("CreateTCPSocket open socket failed"); return false; @@ -22,7 +22,13 @@ bool ClientSocket::Connect() { SetRcvBuf(); SetSndBuf(); - auto ret = connect(Fd(), (sockaddr*)&addr_.GetAddr(), sizeof(sockaddr_in)); + int ret; + if (addr_.IsIpv4()) { + ret = connect(Fd(), (sockaddr*)&addr_.GetAddrIpv4(), sizeof(sockaddr_in)); + } else { + ret = connect(Fd(), (sockaddr*)&addr_.GetAddrIpv6(), sizeof(sockaddr_in6)); + } + if (0 != ret) { if (EINPROGRESS == errno) { return true; diff --git a/src/net/event_server.h b/src/net/event_server.h index 4c35352..3ca7c03 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -17,6 +17,7 @@ #include "client_socket.h" #include "io_thread.h" #include "listen_socket.h" +#include "log.h" #include "thread_manager.h" namespace net { @@ -249,6 +250,7 @@ int EventServer::StartThreadManager(bool serverMode) { listen->SetListenAddr(listenAddrs_); if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + std::cerr << "(1)ListenSocket init error: " << ret << std::endl; return ret; } } @@ -259,6 +261,7 @@ int EventServer::StartThreadManager(bool serverMode) { listen.reset(ListenSocket::CreateTCPListen()); listen->SetListenAddr(listenAddrs_); if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + std::cerr << "(2)ListenSocket init error: " << ret << std::endl; return ret; } } @@ -266,6 +269,7 @@ int EventServer::StartThreadManager(bool serverMode) { // timer only works in the first thread bool ret = i == 0 ? thread->Start(listen, timer_) : thread->Start(listen, nullptr); if (!ret) { + std::cerr << "(3)ThreadManager start error" << std::endl; return -1; } ++i; diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index e79d837..86e07aa 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -19,7 +19,7 @@ const int ListenSocket::LISTENQ = 1024; bool ListenSocket::REUSE_PORT = true; int ListenSocket::OnReadable(const std::shared_ptr &conn, std::string *readBuff) { - struct sockaddr_in clientAddr {}; + struct sockaddr_in clientAddr{}; auto newConnFd = Accept(&clientAddr); if (newConnFd == 0) { ERROR("ListenSocket fd:{},Accept error:{}", Fd(), errno); @@ -59,6 +59,7 @@ int ListenSocket::Init() { bool ListenSocket::Open() { if (Fd() != 0) { + ERROR("ListenSocket fd:{} is invalid", Fd()); return false; } @@ -68,7 +69,11 @@ bool ListenSocket::Open() { } if (SocketType() == SOCKET_LISTEN_TCP) { - fd_ = CreateTCPSocket(); + if (addr_.IsIpv4()) { + fd_ = CreateTCPSocketIpv4(); + } else if (addr_.IsIpv6()) { + fd_ = CreateTCPSocketIpv6(); + } } else if (SocketType() == SOCKET_LISTEN_UDP) { fd_ = CreateUDPSocket(); } else { @@ -88,13 +93,25 @@ bool ListenSocket::Bind() { if (!SetReusePort()) { REUSE_PORT = false; } - - struct sockaddr_in serv = addr_.GetAddr(); - - int ret = ::bind(Fd(), reinterpret_cast(&serv), sizeof serv); - if (0 != ret) { - ERROR("ListenSocket fd:{},Bind error:{}", Fd(), errno); - Close(); + SetIpv6Only(); + + if (addr_.IsIpv4()) { + struct sockaddr_in serv = addr_.GetAddrIpv4(); + int ret = ::bind(Fd(), reinterpret_cast(&serv), sizeof serv); + if (0 != ret) { + ERROR("ListenSocket fd:{},Bind error:{}", Fd(), errno); + Close(); + return false; + } + } else if (addr_.IsIpv6()) { + struct sockaddr_in6 serv = addr_.GetAddrIpv6(); + int ret = ::bind(Fd(), reinterpret_cast(&serv), sizeof serv); + if (0 != ret) { + ERROR("ListenSocket fd:{},Bind error:{}", Fd(), errno); + Close(); + return false; + } + } else { return false; } return true; diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index a0c99ad..e174b44 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -29,46 +29,111 @@ struct SocketAddr { explicit SocketAddr(const sockaddr_in &addr) { Init(addr); } + explicit SocketAddr(const sockaddr_in6 &addr) { Init(addr); } + SocketAddr(uint32_t netip, uint16_t netport) { Init(netip, netport); } SocketAddr(const std::string &ip, uint16_t hostport) { Init(ip, hostport); } - void Init(const sockaddr_in &addr) { memcpy(&addr_, &addr, sizeof(addr)); } + void Init(const sockaddr_in &addr) { + Clear(); + memcpy(&addr_, &addr, sizeof(addr)); + } + + void Init(const sockaddr_in6 &addr) { + Clear(); + memcpy(&addr_, &addr, sizeof(addr)); + } void Init(uint32_t netIp, uint16_t netPort) { - addr_.sin_family = AF_INET; - addr_.sin_addr.s_addr = netIp; - addr_.sin_port = netPort; + addr_.addr4_.sin_family = AF_INET; + addr_.addr4_.sin_addr.s_addr = netIp; + addr_.addr4_.sin_port = netPort; } void Init(const std::string &ip, uint16_t hostPort) { - addr_.sin_family = AF_INET; - addr_.sin_addr.s_addr = ::inet_addr(ip.data()); - addr_.sin_port = htons(hostPort); + if (::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr) == 1) { + addr_.addr6_.sin6_family = AF_INET6; + addr_.addr6_.sin6_port = htons(hostPort); + } else if (::inet_pton(AF_INET, ip.c_str(), &addr_.addr4_.sin_addr) == 1) { + addr_.addr4_.sin_family = AF_INET; + addr_.addr4_.sin_port = htons(hostPort); + } else { + Clear(); + } } - const sockaddr_in &GetAddr() const { return addr_; } + /*const sockaddr_in &GetAddr() const { return addr4_; }*/ + + const sockaddr_in &GetAddrIpv4() const { return addr_.addr4_; } - inline std::string GetIP() const { return ::inet_ntoa(addr_.sin_addr); } + const sockaddr_in6 &GetAddrIpv6() const { return addr_.addr6_; } - inline std::string GetIP(char *buf, socklen_t size) const { - return ::inet_ntop(AF_INET, reinterpret_cast(&addr_.sin_addr), buf, size); + std::string GetIP() const { + char buf[INET6_ADDRSTRLEN] = {0}; + if (addr_.addr4_.sin_family == AF_INET) { + return ::inet_ntoa(addr_.addr4_.sin_addr); + } else if (addr_.addr6_.sin6_family == AF_INET6) { + if (::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, buf, sizeof(buf))) { + return std::string(buf); + } + } + return ""; } - inline uint16_t GetPort() const { return ntohs(addr_.sin_port); } + /*std::string GetIP(char *buf, socklen_t size) const {*/ + /* return ::inet_ntop(AF_INET, reinterpret_cast(&addr4_.sin_addr), buf, size);*/ + /*}*/ + + std::string GetIP(char *buf, socklen_t size) const { + if (addr_.addr4_.sin_family == AF_INET) { + // 处理 IPv4 地址 + if (::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, buf, size)) { + return std::string(buf); + } + } else if (addr_.addr6_.sin6_family == AF_INET6) { + // 处理 IPv6 地址 + if (::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, buf, size)) { + return std::string(buf); + } + } + return ""; // 返回空字符串表示地址无效 + } + + uint16_t GetPort() const { + if (addr_.addr4_.sin_family == AF_INET) { + return ntohs(addr_.addr4_.sin_port); + } else if (addr_.addr6_.sin6_family == AF_INET6) { + return ntohs(addr_.addr6_.sin6_port); + } + return 0; + } - inline bool IsValid() const { return 0 != addr_.sin_family; } + bool IsValid() const { return addr_.addr4_.sin_family != 0 || addr_.addr6_.sin6_family != 0; } + + bool IsIpv6() const { return addr_.addr6_.sin6_family == AF_INET6; } + + bool IsIpv4() const { return addr_.addr4_.sin_family == AF_INET; } void Clear() { memset(&addr_, 0, sizeof addr_); } - inline friend bool operator==(const SocketAddr &a, const SocketAddr &b) { - return a.addr_.sin_family == b.addr_.sin_family && a.addr_.sin_addr.s_addr == b.addr_.sin_addr.s_addr && - a.addr_.sin_port == b.addr_.sin_port; + friend bool operator==(const SocketAddr &a, const SocketAddr &b) { + if (a.addr_.addr4_.sin_family == AF_INET && b.addr_.addr4_.sin_family == AF_INET) { + return a.addr_.addr4_.sin_addr.s_addr == b.addr_.addr4_.sin_addr.s_addr && + a.addr_.addr4_.sin_port == b.addr_.addr4_.sin_port; + } else if (a.addr_.addr6_.sin6_family == AF_INET6 && b.addr_.addr6_.sin6_family == AF_INET6) { + return memcmp(&a.addr_.addr6_.sin6_addr, &b.addr_.addr6_.sin6_addr, sizeof(in6_addr)) == 0 && + a.addr_.addr6_.sin6_port == b.addr_.addr6_.sin6_port; + } + return false; } - inline friend bool operator!=(const SocketAddr &a, const SocketAddr &b) { return !(a == b); } + friend bool operator!=(const SocketAddr &a, const SocketAddr &b) { return !(a == b); } - sockaddr_in addr_{}; + union { + sockaddr_in addr4_; + sockaddr_in6 addr6_; + } addr_; }; } // namespace net From 640b6e134c37be05c9ca1165a33ca9843f804797 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 03:15:56 +0800 Subject: [PATCH 02/33] update --- .clang-tidy | 2 +- etc/conf/kiwi.conf | 3 +- src/kiwi.cc | 5 +-- src/net/callback_function.h | 2 +- src/net/client_socket.cc | 9 ++++- src/net/event_server.h | 4 -- src/net/listen_socket.cc | 1 - src/net/socket_addr.h | 73 +++++++++++++------------------------ 8 files changed, 37 insertions(+), 62 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 92d5545..d15157f 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -57,7 +57,7 @@ Checks: ' # - { key: readability-identifier-naming.UnionCase, value: CamelCase } # - { key: readability-identifier-naming.VariableCase, value: lower_case } -WarningsAsErrors: '' +WarningsAsErrors: '*' # HeaderFilterRegex: '(|/src|/src/net|/src/pstd|/src/storage)/include' # HeaderFilterRegex: '/src/(net|storage|pstd)/include' diff --git a/etc/conf/kiwi.conf b/etc/conf/kiwi.conf index 0029dee..9bc4e0b 100644 --- a/etc/conf/kiwi.conf +++ b/etc/conf/kiwi.conf @@ -10,8 +10,7 @@ port 9221 # If you want you can bind a single interface, if the bind option is not # specified all the interfaces will listen for incoming connections. # -ip ::1 -# ip 127.0.0.1 +ip 127.0.0.1 # Close the connection after a client is idle for N seconds (0 to disable) diff --git a/src/kiwi.cc b/src/kiwi.cc index a2fe54c..4a06fb3 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -202,10 +202,7 @@ bool KiwiDB::Init() { net::SocketAddr addr(g_config.ip.ToString(), g_config.port.load()); INFO("Add listen addr:{}, port:{}", g_config.ip.ToString(), g_config.port.load()); - if (!addr.IsValid()) { - ERROR("Invalid listen addr"); - return false; - } + event_server_->AddListenAddr(addr); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); diff --git a/src/net/callback_function.h b/src/net/callback_function.h index 10385c5..ffa7c89 100644 --- a/src/net/callback_function.h +++ b/src/net/callback_function.h @@ -62,7 +62,7 @@ class NetEvent; // Auxiliary structure struct Connection { - explicit Connection(std::unique_ptr netEvent) : netEvent_(std::move(netEvent)), addr_(0, 0) {} + explicit Connection(std::unique_ptr netEvent) : netEvent_(std::move(netEvent)) {} ~Connection() = default; diff --git a/src/net/client_socket.cc b/src/net/client_socket.cc index f440f45..1edb2f1 100644 --- a/src/net/client_socket.cc +++ b/src/net/client_socket.cc @@ -24,9 +24,14 @@ bool ClientSocket::Connect() { int ret; if (addr_.IsIpv4()) { - ret = connect(Fd(), (sockaddr*)&addr_.GetAddrIpv4(), sizeof(sockaddr_in)); + const sockaddr_in& addr = addr_.GetAddrIpv4(); + ret = connect(Fd(), reinterpret_cast(&addr), sizeof(sockaddr_in)); + } else if (addr_.IsIpv6()) { + const sockaddr_in6& addr = addr_.GetAddrIpv6(); + ret = connect(Fd(), reinterpret_cast(&addr), sizeof(sockaddr_in6)); } else { - ret = connect(Fd(), (sockaddr*)&addr_.GetAddrIpv6(), sizeof(sockaddr_in6)); + onConnectFail_("IP address is invalid"); + return false; } if (0 != ret) { diff --git a/src/net/event_server.h b/src/net/event_server.h index 3ca7c03..4c35352 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -17,7 +17,6 @@ #include "client_socket.h" #include "io_thread.h" #include "listen_socket.h" -#include "log.h" #include "thread_manager.h" namespace net { @@ -250,7 +249,6 @@ int EventServer::StartThreadManager(bool serverMode) { listen->SetListenAddr(listenAddrs_); if (auto ret = listen->Init() != static_cast(NetListen::OK)) { - std::cerr << "(1)ListenSocket init error: " << ret << std::endl; return ret; } } @@ -261,7 +259,6 @@ int EventServer::StartThreadManager(bool serverMode) { listen.reset(ListenSocket::CreateTCPListen()); listen->SetListenAddr(listenAddrs_); if (auto ret = listen->Init() != static_cast(NetListen::OK)) { - std::cerr << "(2)ListenSocket init error: " << ret << std::endl; return ret; } } @@ -269,7 +266,6 @@ int EventServer::StartThreadManager(bool serverMode) { // timer only works in the first thread bool ret = i == 0 ? thread->Start(listen, timer_) : thread->Start(listen, nullptr); if (!ret) { - std::cerr << "(3)ThreadManager start error" << std::endl; return -1; } ++i; diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index 86e07aa..9fbbdf0 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -59,7 +59,6 @@ int ListenSocket::Init() { bool ListenSocket::Open() { if (Fd() != 0) { - ERROR("ListenSocket fd:{} is invalid", Fd()); return false; } diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index e174b44..752027f 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -18,11 +18,11 @@ namespace net { struct SocketAddr { SocketAddr() { Clear(); } - SocketAddr(const SocketAddr &other) { memcpy(&addr_, &other.addr_, sizeof addr_); } + SocketAddr(const SocketAddr &other) { memcpy(&addr_, &other.addr_, sizeof(addr_)); } SocketAddr &operator=(const SocketAddr &other) { if (this != &other) { - memcpy(&addr_, &other.addr_, sizeof addr_); + memcpy(&addr_, &other.addr_, sizeof(addr_)); } return *this; } @@ -31,73 +31,52 @@ struct SocketAddr { explicit SocketAddr(const sockaddr_in6 &addr) { Init(addr); } - SocketAddr(uint32_t netip, uint16_t netport) { Init(netip, netport); } - SocketAddr(const std::string &ip, uint16_t hostport) { Init(ip, hostport); } - void Init(const sockaddr_in &addr) { - Clear(); - memcpy(&addr_, &addr, sizeof(addr)); - } - - void Init(const sockaddr_in6 &addr) { - Clear(); - memcpy(&addr_, &addr, sizeof(addr)); - } + void Init(const sockaddr_in &addr) { memcpy(&addr_, &addr, sizeof(addr)); } - void Init(uint32_t netIp, uint16_t netPort) { - addr_.addr4_.sin_family = AF_INET; - addr_.addr4_.sin_addr.s_addr = netIp; - addr_.addr4_.sin_port = netPort; - } + void Init(const sockaddr_in6 &addr) { memcpy(&addr_, &addr, sizeof(addr)); } void Init(const std::string &ip, uint16_t hostPort) { - if (::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr) == 1) { - addr_.addr6_.sin6_family = AF_INET6; - addr_.addr6_.sin6_port = htons(hostPort); - } else if (::inet_pton(AF_INET, ip.c_str(), &addr_.addr4_.sin_addr) == 1) { + if (::inet_pton(AF_INET, ip.c_str(), &addr_.addr4_.sin_addr) == 1) { addr_.addr4_.sin_family = AF_INET; addr_.addr4_.sin_port = htons(hostPort); - } else { - Clear(); + } else if (::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr) == 1) { + addr_.addr6_.sin6_family = AF_INET6; + addr_.addr6_.sin6_port = htons(hostPort); } } - /*const sockaddr_in &GetAddr() const { return addr4_; }*/ - const sockaddr_in &GetAddrIpv4() const { return addr_.addr4_; } const sockaddr_in6 &GetAddrIpv6() const { return addr_.addr6_; } std::string GetIP() const { - char buf[INET6_ADDRSTRLEN] = {0}; - if (addr_.addr4_.sin_family == AF_INET) { - return ::inet_ntoa(addr_.addr4_.sin_addr); - } else if (addr_.addr6_.sin6_family == AF_INET6) { - if (::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, buf, sizeof(buf))) { - return std::string(buf); + if (IsIpv4()) { + char ipv4_buf[INET_ADDRSTRLEN] = {0}; + if (::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, ipv4_buf, sizeof(ipv4_buf))) { + return ipv4_buf; + } + } else if (IsIpv6()) { + char ipv6_buf[INET6_ADDRSTRLEN] = {0}; + if (::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, ipv6_buf, sizeof(ipv6_buf))) { + return ipv6_buf; } } - return ""; + return {}; } - /*std::string GetIP(char *buf, socklen_t size) const {*/ - /* return ::inet_ntop(AF_INET, reinterpret_cast(&addr4_.sin_addr), buf, size);*/ - /*}*/ - std::string GetIP(char *buf, socklen_t size) const { - if (addr_.addr4_.sin_family == AF_INET) { - // 处理 IPv4 地址 + if (IsIpv4()) { if (::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, buf, size)) { - return std::string(buf); + return buf; } - } else if (addr_.addr6_.sin6_family == AF_INET6) { - // 处理 IPv6 地址 + } else if (IsIpv6()) { if (::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, buf, size)) { - return std::string(buf); + return buf; } } - return ""; // 返回空字符串表示地址无效 + return {}; } uint16_t GetPort() const { @@ -115,13 +94,13 @@ struct SocketAddr { bool IsIpv4() const { return addr_.addr4_.sin_family == AF_INET; } - void Clear() { memset(&addr_, 0, sizeof addr_); } + void Clear() { memset(&addr_, 0, sizeof(addr_)); } friend bool operator==(const SocketAddr &a, const SocketAddr &b) { - if (a.addr_.addr4_.sin_family == AF_INET && b.addr_.addr4_.sin_family == AF_INET) { + if (a.IsIpv4() && b.IsIpv4()) { return a.addr_.addr4_.sin_addr.s_addr == b.addr_.addr4_.sin_addr.s_addr && a.addr_.addr4_.sin_port == b.addr_.addr4_.sin_port; - } else if (a.addr_.addr6_.sin6_family == AF_INET6 && b.addr_.addr6_.sin6_family == AF_INET6) { + } else if (a.IsIpv6() && b.IsIpv6()) { return memcmp(&a.addr_.addr6_.sin6_addr, &b.addr_.addr6_.sin6_addr, sizeof(in6_addr)) == 0 && a.addr_.addr6_.sin6_port == b.addr_.addr6_.sin6_port; } From 437dbcc23ff22e4cd9662358c6fcc3f05b721915 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 04:36:06 +0800 Subject: [PATCH 03/33] update --- src/net/client_socket.cc | 14 +++----------- src/net/listen_socket.cc | 22 +++++++--------------- src/net/socket_addr.h | 22 ++++++++++++++++++---- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/net/client_socket.cc b/src/net/client_socket.cc index 1edb2f1..25b3239 100644 --- a/src/net/client_socket.cc +++ b/src/net/client_socket.cc @@ -7,6 +7,7 @@ #include +#include #include "client_socket.h" namespace net { @@ -22,17 +23,8 @@ bool ClientSocket::Connect() { SetRcvBuf(); SetSndBuf(); - int ret; - if (addr_.IsIpv4()) { - const sockaddr_in& addr = addr_.GetAddrIpv4(); - ret = connect(Fd(), reinterpret_cast(&addr), sizeof(sockaddr_in)); - } else if (addr_.IsIpv6()) { - const sockaddr_in6& addr = addr_.GetAddrIpv6(); - ret = connect(Fd(), reinterpret_cast(&addr), sizeof(sockaddr_in6)); - } else { - onConnectFail_("IP address is invalid"); - return false; - } + auto addr = addr_.GetAddr(); + int ret = connect(Fd(), addr, addr_.GetAddrLen()); if (0 != ret) { if (EINPROGRESS == errno) { diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index 9fbbdf0..f2f2223 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -6,6 +6,7 @@ */ #include +#include #include "config.h" #include "listen_socket.h" @@ -92,27 +93,18 @@ bool ListenSocket::Bind() { if (!SetReusePort()) { REUSE_PORT = false; } - SetIpv6Only(); + if (addr_.IsIpv6()) { + SetIpv6Only(); + } - if (addr_.IsIpv4()) { - struct sockaddr_in serv = addr_.GetAddrIpv4(); - int ret = ::bind(Fd(), reinterpret_cast(&serv), sizeof serv); - if (0 != ret) { - ERROR("ListenSocket fd:{},Bind error:{}", Fd(), errno); - Close(); - return false; - } - } else if (addr_.IsIpv6()) { - struct sockaddr_in6 serv = addr_.GetAddrIpv6(); - int ret = ::bind(Fd(), reinterpret_cast(&serv), sizeof serv); + auto serv = addr_.GetAddr(); + int ret = ::bind(Fd(), serv, addr_.GetAddrLen()); if (0 != ret) { ERROR("ListenSocket fd:{},Bind error:{}", Fd(), errno); Close(); return false; } - } else { - return false; - } + return true; } diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index 752027f..09eba9c 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -47,9 +47,23 @@ struct SocketAddr { } } - const sockaddr_in &GetAddrIpv4() const { return addr_.addr4_; } + const sockaddr *GetAddr() const { + if (IsIpv4()) { + return reinterpret_cast(&addr_.addr4_); + } else if (IsIpv6()) { + return reinterpret_cast(&addr_.addr6_); + } + return nullptr; + } - const sockaddr_in6 &GetAddrIpv6() const { return addr_.addr6_; } + socklen_t GetAddrLen() const { + if (IsIpv4()) { + return sizeof(addr_.addr4_); + } else if (IsIpv6()) { + return sizeof(addr_.addr6_); + } + return 0; + } std::string GetIP() const { if (IsIpv4()) { @@ -80,9 +94,9 @@ struct SocketAddr { } uint16_t GetPort() const { - if (addr_.addr4_.sin_family == AF_INET) { + if (IsIpv4()) { return ntohs(addr_.addr4_.sin_port); - } else if (addr_.addr6_.sin6_family == AF_INET6) { + } else if (IsIpv6()) { return ntohs(addr_.addr6_.sin6_port); } return 0; From c07bb77b8a4f5bcf29f40d1085b37f26f60e1335 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 04:50:34 +0800 Subject: [PATCH 04/33] update --- src/net/base_socket.cc | 2 +- src/net/base_socket.h | 8 ++------ src/net/client_socket.cc | 4 +--- src/net/listen_socket.cc | 21 ++++++++++----------- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index 84c11aa..d1da778 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -89,7 +89,7 @@ bool BaseSocket::SetReusePort() { return false; } -bool BaseSocket::SetIpv6Only() { +bool BaseSocket::SetDisableIpv6Only() { int ipv6only = 0; if (::setsockopt(Fd(), IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&ipv6only), sizeof(ipv6only)) == -1) { WARN("SetIpv6Only fd:{} error:{}", Fd(), errno); diff --git a/src/net/base_socket.h b/src/net/base_socket.h index 07a317a..d41f68f 100644 --- a/src/net/base_socket.h +++ b/src/net/base_socket.h @@ -9,10 +9,6 @@ #include -#include -#include - -#include "base_event.h" #include "net_event.h" #include "socket_addr.h" @@ -34,7 +30,7 @@ class BaseSocket : public NetEvent { ~BaseSocket() override = default; - void OnError() override{}; + void OnError() override {}; void Close() override; @@ -59,7 +55,7 @@ class BaseSocket : public NetEvent { bool SetReusePort(); - bool SetIpv6Only(); + bool SetDisableIpv6Only(); bool GetLocalAddr(SocketAddr &); diff --git a/src/net/client_socket.cc b/src/net/client_socket.cc index 25b3239..b79c141 100644 --- a/src/net/client_socket.cc +++ b/src/net/client_socket.cc @@ -23,9 +23,7 @@ bool ClientSocket::Connect() { SetRcvBuf(); SetSndBuf(); - auto addr = addr_.GetAddr(); - int ret = connect(Fd(), addr, addr_.GetAddrLen()); - + int ret = connect(Fd(), addr_.GetAddr(), addr_.GetAddrLen()); if (0 != ret) { if (EINPROGRESS == errno) { return true; diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index f2f2223..c09e148 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -69,10 +69,10 @@ bool ListenSocket::Open() { } if (SocketType() == SOCKET_LISTEN_TCP) { - if (addr_.IsIpv4()) { - fd_ = CreateTCPSocketIpv4(); - } else if (addr_.IsIpv6()) { + if (addr_.IsIpv6()) { fd_ = CreateTCPSocketIpv6(); + } else { + fd_ = CreateTCPSocketIpv4(); } } else if (SocketType() == SOCKET_LISTEN_UDP) { fd_ = CreateUDPSocket(); @@ -94,16 +94,15 @@ bool ListenSocket::Bind() { REUSE_PORT = false; } if (addr_.IsIpv6()) { - SetIpv6Only(); + SetDisableIpv6Only(); } - auto serv = addr_.GetAddr(); - int ret = ::bind(Fd(), serv, addr_.GetAddrLen()); - if (0 != ret) { - ERROR("ListenSocket fd:{},Bind error:{}", Fd(), errno); - Close(); - return false; - } + int ret = ::bind(Fd(), addr_.GetAddr(), addr_.GetAddrLen()); + if (0 != ret) { + ERROR("ListenSocket fd:{},Bind error:{}", Fd(), errno); + Close(); + return false; + } return true; } From 0b64a3d21fbff6c482e3f06d36aa54835c4e5d81 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 05:04:02 +0800 Subject: [PATCH 05/33] Improve code quality --- src/net/base_socket.cc | 9 +++++++-- src/net/base_socket.h | 4 +--- src/net/client_socket.cc | 2 +- src/net/listen_socket.cc | 6 +----- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index d1da778..5139104 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -14,9 +14,14 @@ namespace net { -int BaseSocket::CreateTCPSocketIpv4() { return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); } +int BaseSocket::CreateTCPSocket(const SocketAddr &addr) { + if (addr.IsIpv4()) { + return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + } else { + return ::socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + } +} -int BaseSocket::CreateTCPSocketIpv6() { return ::socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); } int BaseSocket::CreateUDPSocket() { return ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); } diff --git a/src/net/base_socket.h b/src/net/base_socket.h index d41f68f..903fad0 100644 --- a/src/net/base_socket.h +++ b/src/net/base_socket.h @@ -34,9 +34,7 @@ class BaseSocket : public NetEvent { void Close() override; - static int CreateTCPSocketIpv4(); - - static int CreateTCPSocketIpv6(); + static int CreateTCPSocket(const SocketAddr &addr); static int CreateUDPSocket(); diff --git a/src/net/client_socket.cc b/src/net/client_socket.cc index b79c141..2283a2a 100644 --- a/src/net/client_socket.cc +++ b/src/net/client_socket.cc @@ -13,7 +13,7 @@ namespace net { bool ClientSocket::Connect() { - fd_ = CreateTCPSocketIpv4(); + fd_ = CreateTCPSocket(addr_); if (fd_ == -1) { onConnectFail_("CreateTCPSocket open socket failed"); return false; diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index c09e148..320ff57 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -69,11 +69,7 @@ bool ListenSocket::Open() { } if (SocketType() == SOCKET_LISTEN_TCP) { - if (addr_.IsIpv6()) { - fd_ = CreateTCPSocketIpv6(); - } else { - fd_ = CreateTCPSocketIpv4(); - } + fd_ = CreateTCPSocket(addr_); } else if (SocketType() == SOCKET_LISTEN_UDP) { fd_ = CreateUDPSocket(); } else { From 73b2e1a444285880576c565d925da3581d57f9c5 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 05:05:38 +0800 Subject: [PATCH 06/33] fix blank --- src/kiwi.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/kiwi.cc b/src/kiwi.cc index 4a06fb3..1ac5bb4 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -202,7 +202,6 @@ bool KiwiDB::Init() { net::SocketAddr addr(g_config.ip.ToString(), g_config.port.load()); INFO("Add listen addr:{}, port:{}", g_config.ip.ToString(), g_config.port.load()); - event_server_->AddListenAddr(addr); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); From 818e5acde691a73e151618102638f255f92000ba Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 05:12:36 +0800 Subject: [PATCH 07/33] Improve code quality --- src/net/socket_addr.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index 09eba9c..e8f71bb 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -111,14 +111,17 @@ struct SocketAddr { void Clear() { memset(&addr_, 0, sizeof(addr_)); } friend bool operator==(const SocketAddr &a, const SocketAddr &b) { - if (a.IsIpv4() && b.IsIpv4()) { + if (a.IsIpv4() != b.IsIpv4()) { + return false; + } + + if (a.IsIpv4()) { return a.addr_.addr4_.sin_addr.s_addr == b.addr_.addr4_.sin_addr.s_addr && a.addr_.addr4_.sin_port == b.addr_.addr4_.sin_port; - } else if (a.IsIpv6() && b.IsIpv6()) { - return memcmp(&a.addr_.addr6_.sin6_addr, &b.addr_.addr6_.sin6_addr, sizeof(in6_addr)) == 0 && - a.addr_.addr6_.sin6_port == b.addr_.addr6_.sin6_port; } - return false; + + return memcmp(&a.addr_.addr6_.sin6_addr, &b.addr_.addr6_.sin6_addr, sizeof(in6_addr)) == 0 && + a.addr_.addr6_.sin6_port == b.addr_.addr6_.sin6_port; } friend bool operator!=(const SocketAddr &a, const SocketAddr &b) { return !(a == b); } From 0f26214d483b6f0b00dc967e3c490f3b56864077 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sat, 30 Nov 2024 20:28:36 +0800 Subject: [PATCH 08/33] add blank --- etc/conf/kiwi.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/etc/conf/kiwi.conf b/etc/conf/kiwi.conf index 9bc4e0b..35bd35d 100644 --- a/etc/conf/kiwi.conf +++ b/etc/conf/kiwi.conf @@ -350,3 +350,4 @@ rocksdb-periodic-second 259200; use-raft no # Braft relies on brpc to communicate via the default port number plus the port offset raft-port-offset 10 + From c5128c1bba9f8fe7af95e507e54c366f89465893 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sun, 1 Dec 2024 15:41:35 +0800 Subject: [PATCH 09/33] support ipv4 and ipv6 --- src/client.h | 2 ++ src/kiwi.cc | 7 ++++++- src/net/base_event.h | 6 ++++-- src/net/base_socket.cc | 1 + src/net/epoll_event.cc | 29 +++++++++++++++++++++++++---- src/net/epoll_event.h | 4 ++-- src/net/event_server.h | 30 +++++++++++++++++++++++++++--- src/net/kqueue_event.h | 4 ++-- src/net/socket_addr.h | 1 + src/net/thread_manager.h | 27 ++++++++++++++++----------- 10 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/client.h b/src/client.h index cc2d2f9..a94c2a9 100644 --- a/src/client.h +++ b/src/client.h @@ -207,6 +207,7 @@ class PClient : public std::enable_shared_from_this { inline void SetThreadIndex(int8_t index) { net_thread_index_ = index; } inline int8_t GetThreadIndex() const { return net_thread_index_; } inline void SetSocketAddr(const net::SocketAddr& addr) { addr_ = addr; } + inline void SetSocketAddrIPv6(const net::SocketAddr& addr) { addr_ipv6_ = addr; } inline void SetArgv(std::vector& argv) { argv_ = argv; } @@ -260,6 +261,7 @@ class PClient : public std::enable_shared_from_this { uint64_t net_id_ = 0; int8_t net_thread_index_ = 0; net::SocketAddr addr_; + net::SocketAddr addr_ipv6_; static thread_local PClient* s_current; diff --git a/src/kiwi.cc b/src/kiwi.cc index 1ac5bb4..022221e 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -201,12 +201,17 @@ bool KiwiDB::Init() { event_server_->SetRwSeparation(true); net::SocketAddr addr(g_config.ip.ToString(), g_config.port.load()); - INFO("Add listen addr:{}, port:{}", g_config.ip.ToString(), g_config.port.load()); + INFO("Add listen addr: {}, port: {}", g_config.ip.ToString(), g_config.port.load()); event_server_->AddListenAddr(addr); + net::SocketAddr addrIpv6("::1", 10000); + INFO("Add listen addr: {}, port: {}", addrIpv6.GetIP(), addrIpv6.GetPort()); + event_server_->AddListenAddrIpv6(addrIpv6); + event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); event_server_->SetOnCreate([](uint64_t connID, std::shared_ptr& client, const net::SocketAddr& addr) { + INFO("SetOnCreate connID:{} fd:{} IP:{} port:{}", connID, client->GetConnId(), addr.GetIP(), addr.GetPort()); client->SetSocketAddr(addr); client->OnConnect(); ClientMap::getInstance().AddClient(client->GetUniqueID(), client); diff --git a/src/net/base_event.h b/src/net/base_event.h index fd0abde..f4b0908 100644 --- a/src/net/base_event.h +++ b/src/net/base_event.h @@ -44,8 +44,8 @@ class BaseEvent : public std::enable_shared_from_this { const static int EVENT_ERROR; const static int EVENT_HUB; - BaseEvent(const std::shared_ptr &listen, int8_t mode, int8_t type) - : listen_(listen), mode_(mode), type_(type){}; + BaseEvent(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, int8_t mode, int8_t type) + : listen_(listen), listenIpv6_(listenIpv6), mode_(mode), type_(type){}; virtual ~BaseEvent() = default; @@ -115,6 +115,8 @@ class BaseEvent : public std::enable_shared_from_this { // listening socket std::shared_ptr listen_; + std::shared_ptr listenIpv6_; + // callback function when a new connection is created std::function)> onCreate_; diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index 5139104..ed41269 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -100,6 +100,7 @@ bool BaseSocket::SetDisableIpv6Only() { WARN("SetIpv6Only fd:{} error:{}", Fd(), errno); return false; } + DEBUG("SetIpv6Only fd:{} success", Fd()); return true; } diff --git a/src/net/epoll_event.cc b/src/net/epoll_event.cc index dea8896..a94798a 100644 --- a/src/net/epoll_event.cc +++ b/src/net/epoll_event.cc @@ -27,6 +27,10 @@ bool EpollEvent::Init() { } if (mode_ & EVENT_MODE_READ) { // Add the listen socket to epoll for read AddEvent(listen_->Fd(), listen_->Fd(), EVENT_READ); + if (listenIpv6_) { + DEBUG("listenIpv6_ fd:{}", listenIpv6_->Fd()); + AddEvent(listenIpv6_->Fd(), listenIpv6_->Fd(), EVENT_READ); + } } if (pipe(pipeFd_) == -1) { ERROR("pipe error errno:{}", errno); @@ -39,7 +43,7 @@ bool EpollEvent::Init() { } void EpollEvent::AddEvent(uint64_t id, int fd, int mask) { - struct epoll_event ev {}; + struct epoll_event ev{}; ev.events = mask; ev.data.u64 = id; if (epoll_ctl(EvFd(), EPOLL_CTL_ADD, fd, &ev) == -1) { @@ -58,7 +62,7 @@ void EpollEvent::EventPoll() { } void EpollEvent::AddWriteEvent(uint64_t id, int fd) { - struct epoll_event ev {}; + struct epoll_event ev{}; ev.events = EVENT_WRITE; ev.data.u64 = id; if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event @@ -75,7 +79,7 @@ void EpollEvent::AddWriteEvent(uint64_t id, int fd) { void EpollEvent::DelWriteEvent(uint64_t id, int fd) { if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event to read - struct epoll_event ev {}; + struct epoll_event ev{}; ev.events = EVENT_READ; ev.data.u64 = id; if (epoll_ctl(EvFd(), EPOLL_CTL_MOD, fd, &ev) == -1) { @@ -105,7 +109,8 @@ void EpollEvent::EventRead() { std::shared_ptr conn; if (events[i].events & EVENT_READ) { // If the event is less than the listen socket, it is a new connection - if (events[i].data.u64 != listen_->Fd()) { + DEBUG("listen fd:{}; listenIpv6 fd:{};", listen_->Fd(), listenIpv6_->Fd()); + if (events[i].data.u64 != listen_->Fd() && events[i].data.u64 != listenIpv6_->Fd()) { conn = getConn_(events[i].data.u64); } DoRead(events[i], conn); @@ -124,6 +129,10 @@ void EpollEvent::EventRead() { if (timer_) { timer_->OnTimer(); } + if (nfds < 0) { + ERROR("epoll_wait error errno:{}", errno); + continue; + } } } @@ -155,6 +164,18 @@ void EpollEvent::DoRead(const epoll_event &event, const std::shared_ptrFd()) { + auto newConn = std::make_shared(nullptr); + auto connFd = listenIpv6_->OnReadable(newConn, nullptr); + if (connFd < 0) { + DoError(event, "accept error"); + return; + } + // create new connection + // thread_manager.h: 148 onCreate_(connFd, newConn); } else if (conn) { std::string readBuff; diff --git a/src/net/epoll_event.h b/src/net/epoll_event.h index d0efeb1..67028a7 100644 --- a/src/net/epoll_event.h +++ b/src/net/epoll_event.h @@ -22,8 +22,8 @@ namespace net { class EpollEvent : public BaseEvent { public: - explicit EpollEvent(const std::shared_ptr &listen, int8_t mode) - : BaseEvent(listen, mode, BaseEvent::EVENT_TYPE_EPOLL){}; + explicit EpollEvent(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, int8_t mode) + : BaseEvent(listen, listenIpv6, mode, BaseEvent::EVENT_TYPE_EPOLL){}; ~EpollEvent() override { Close(); } diff --git a/src/net/event_server.h b/src/net/event_server.h index 4c35352..31715ac 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -41,6 +41,8 @@ class EventServer final { inline void AddListenAddr(const SocketAddr &addr) { listenAddrs_ = addr; } + inline void AddListenAddrIpv6(const SocketAddr &addr) { listenAddrsIpv6_ = addr; } + inline void SetRwSeparation(bool separation = true) { rwSeparation_ = separation; } void InitTimer(int64_t interval) { timer_ = std::make_shared(interval); } @@ -89,6 +91,8 @@ class EventServer final { SocketAddr listenAddrs_; // The address to listen on + SocketAddr listenAddrsIpv6_; // The address to listen on + std::atomic running_ = true; // Whether the server is running bool rwSeparation_ = true; // Whether to separate read and write @@ -104,7 +108,8 @@ class EventServer final { }; template -requires HasSetFdFunction std::pair EventServer::StartServer(int64_t interval) { +requires HasSetFdFunction +std::pair EventServer::StartServer(int64_t interval) { if (threadNum_ <= 0) { return std::pair(false, "thread num must be greater than 0"); } @@ -143,7 +148,8 @@ requires HasSetFdFunction std::pair EventServer::StartS } template -requires HasSetFdFunction std::pair EventServer::StartClientServer() { +requires HasSetFdFunction +std::pair EventServer::StartClientServer() { if (threadNum_ <= 0) { return std::pair(false, "thread num must be greater than 0"); } @@ -245,12 +251,22 @@ template requires HasSetFdFunction int EventServer::StartThreadManager(bool serverMode) { std::shared_ptr listen(ListenSocket::CreateTCPListen()); + std::shared_ptr listenIpv6(ListenSocket::CreateTCPListen()); + if (serverMode) { listen->SetListenAddr(listenAddrs_); if (auto ret = listen->Init() != static_cast(NetListen::OK)) { return ret; } + + if (listenAddrsIpv6_.IsIpv6()) { + listenIpv6->SetListenAddr(listenAddrsIpv6_); + + if (auto ret = listenIpv6->Init() != static_cast(NetListen::OK)) { + return ret; + } + } } int i = 0; @@ -261,10 +277,18 @@ int EventServer::StartThreadManager(bool serverMode) { if (auto ret = listen->Init() != static_cast(NetListen::OK)) { return ret; } + + if (listenAddrsIpv6_.IsIpv6()) { + listenIpv6.reset(ListenSocket::CreateTCPListen()); + listenIpv6->SetListenAddr(listenAddrsIpv6_); + if (auto ret = listenIpv6->Init() != static_cast(NetListen::OK)) { + return ret; + } + } } // timer only works in the first thread - bool ret = i == 0 ? thread->Start(listen, timer_) : thread->Start(listen, nullptr); + bool ret = i == 0 ? thread->Start(listen, listenIpv6, timer_) : thread->Start(listen, listenIpv6, nullptr); if (!ret) { return -1; } diff --git a/src/net/kqueue_event.h b/src/net/kqueue_event.h index e013d19..0532f92 100644 --- a/src/net/kqueue_event.h +++ b/src/net/kqueue_event.h @@ -23,8 +23,8 @@ namespace net { class KqueueEvent : public BaseEvent { public: - explicit KqueueEvent(std::shared_ptr listen, int8_t mode) - : BaseEvent(std::move(listen), mode, BaseEvent::EVENT_TYPE_KQUEUE){}; + explicit KqueueEvent(std::shared_ptr listen, std::shared_ptr listenIpv6, int8_t mode) + : BaseEvent(std::move(listen), listenIpv6, mode, BaseEvent::EVENT_TYPE_KQUEUE){}; ~KqueueEvent() override { Close(); } diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index e8f71bb..ec17d9e 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -102,6 +102,7 @@ struct SocketAddr { return 0; } + // Valid return true, invalid return false bool IsValid() const { return addr_.addr4_.sin_family != 0 || addr_.addr6_.sin6_family != 0; } bool IsIpv6() const { return addr_.addr6_.sin6_family == AF_INET6; } diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 0365d91..8999047 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -54,7 +54,7 @@ class ThreadManager { inline void SetOnClose(const OnClose &func) { onClose_ = func; } // Start the thread and initialize the event - bool Start(const std::shared_ptr &listen, const std::shared_ptr &timer); + bool Start(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer); // Stop the thread void Stop(); @@ -82,7 +82,7 @@ class ThreadManager { private: // Create read thread - bool CreateReadThread(const std::shared_ptr &listen, const std::shared_ptr &timer); + bool CreateReadThread(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer); // Create write thread if rwSeparation_ is true bool CreateWriteThread(); @@ -114,12 +114,15 @@ class ThreadManager { }; template -requires HasSetFdFunction ThreadManager::~ThreadManager() { Stop(); } +requires HasSetFdFunction +ThreadManager::~ThreadManager() { + Stop(); +} template requires HasSetFdFunction -bool ThreadManager::Start(const std::shared_ptr &listen, const std::shared_ptr &timer) { - if (!CreateReadThread(listen, timer)) { +bool ThreadManager::Start(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer) { + if (!CreateReadThread(listen, listenIpv6, timer)) { return false; } if (rwSeparation_) { @@ -201,7 +204,9 @@ void ThreadManager::OnNetEventClose(uint64_t connId, std::string &&err) { template requires HasSetFdFunction -void ThreadManager::CloseConnection(uint64_t connId) { OnNetEventClose(connId, ""); } +void ThreadManager::CloseConnection(uint64_t connId) { + OnNetEventClose(connId, ""); +} template requires HasSetFdFunction @@ -267,7 +272,7 @@ void ThreadManager::SendPacket(const T &conn, std::string &&msg) { template requires HasSetFdFunction -bool ThreadManager::CreateReadThread(const std::shared_ptr &listen, const std::shared_ptr &timer) { +bool ThreadManager::CreateReadThread(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer) { std::shared_ptr event; int8_t eventMode = BaseEvent::EVENT_MODE_READ; if (!rwSeparation_) { @@ -275,9 +280,9 @@ bool ThreadManager::CreateReadThread(const std::shared_ptr &listen, } #if defined(HAVE_EPOLL) - event = std::make_shared(listen, eventMode); + event = std::make_shared(listen, listenIpv6, eventMode); #elif defined(HAVE_KQUEUE) - event = std::make_shared(listen, eventMode); + event = std::make_shared(listen, listenIpv6, eventMode); #endif event->AddTimer(timer); @@ -309,9 +314,9 @@ bool ThreadManager::CreateWriteThread() { std::shared_ptr event; #if defined(HAVE_EPOLL) - event = std::make_shared(nullptr, BaseEvent::EVENT_MODE_WRITE); + event = std::make_shared(nullptr, nullptr, BaseEvent::EVENT_MODE_WRITE); #elif defined(HAVE_KQUEUE) - event = std::make_shared(nullptr, BaseEvent::EVENT_MODE_WRITE); + event = std::make_shared(nullptr, nullptr, BaseEvent::EVENT_MODE_WRITE); #endif event->SetOnClose([this](uint64_t connId, std::string &&msg) { OnNetEventClose(connId, std::move(msg)); }); From 5ee3235fdf89ae09482fd9fa2d32add65b9b5579 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Sun, 1 Dec 2024 21:47:11 +0800 Subject: [PATCH 10/33] add listen sockets manager --- src/client.h | 2 - src/net/epoll_event.cc | 2 - src/net/event_server.h | 36 ++++++------------ src/net/kqueue_event.cc | 9 ++++- src/net/listen_sockets_mananger.h | 61 +++++++++++++++++++++++++++++++ src/net/thread_manager.h | 20 ++++++---- 6 files changed, 94 insertions(+), 36 deletions(-) create mode 100644 src/net/listen_sockets_mananger.h diff --git a/src/client.h b/src/client.h index a94c2a9..cc2d2f9 100644 --- a/src/client.h +++ b/src/client.h @@ -207,7 +207,6 @@ class PClient : public std::enable_shared_from_this { inline void SetThreadIndex(int8_t index) { net_thread_index_ = index; } inline int8_t GetThreadIndex() const { return net_thread_index_; } inline void SetSocketAddr(const net::SocketAddr& addr) { addr_ = addr; } - inline void SetSocketAddrIPv6(const net::SocketAddr& addr) { addr_ipv6_ = addr; } inline void SetArgv(std::vector& argv) { argv_ = argv; } @@ -261,7 +260,6 @@ class PClient : public std::enable_shared_from_this { uint64_t net_id_ = 0; int8_t net_thread_index_ = 0; net::SocketAddr addr_; - net::SocketAddr addr_ipv6_; static thread_local PClient* s_current; diff --git a/src/net/epoll_event.cc b/src/net/epoll_event.cc index a94798a..ab1a016 100644 --- a/src/net/epoll_event.cc +++ b/src/net/epoll_event.cc @@ -28,7 +28,6 @@ bool EpollEvent::Init() { if (mode_ & EVENT_MODE_READ) { // Add the listen socket to epoll for read AddEvent(listen_->Fd(), listen_->Fd(), EVENT_READ); if (listenIpv6_) { - DEBUG("listenIpv6_ fd:{}", listenIpv6_->Fd()); AddEvent(listenIpv6_->Fd(), listenIpv6_->Fd(), EVENT_READ); } } @@ -109,7 +108,6 @@ void EpollEvent::EventRead() { std::shared_ptr conn; if (events[i].events & EVENT_READ) { // If the event is less than the listen socket, it is a new connection - DEBUG("listen fd:{}; listenIpv6 fd:{};", listen_->Fd(), listenIpv6_->Fd()); if (events[i].data.u64 != listen_->Fd() && events[i].data.u64 != listenIpv6_->Fd()) { conn = getConn_(events[i].data.u64); } diff --git a/src/net/event_server.h b/src/net/event_server.h index 31715ac..7d7625f 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "base_socket.h" @@ -18,6 +19,7 @@ #include "io_thread.h" #include "listen_socket.h" #include "thread_manager.h" +#include "listen_sockets_mananger.h" namespace net { @@ -250,45 +252,31 @@ void EventServer::TCPConnect(const SocketAddr &addr, const std::function requires HasSetFdFunction int EventServer::StartThreadManager(bool serverMode) { - std::shared_ptr listen(ListenSocket::CreateTCPListen()); - std::shared_ptr listenIpv6(ListenSocket::CreateTCPListen()); + bool isIpv6 = listenAddrsIpv6_.IsIpv6(); + auto listenSocketsManager = std::make_shared(isIpv6); if (serverMode) { - listen->SetListenAddr(listenAddrs_); + listenSocketsManager->SetListenAddr(listenAddrs_); + listenSocketsManager->SetListenAddrIpv6(listenAddrsIpv6_); - if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + if (auto ret = listenSocketsManager->Init() != static_cast(NetListen::OK)) { return ret; } - - if (listenAddrsIpv6_.IsIpv6()) { - listenIpv6->SetListenAddr(listenAddrsIpv6_); - - if (auto ret = listenIpv6->Init() != static_cast(NetListen::OK)) { - return ret; - } - } } int i = 0; for (const auto &thread : threadsManager_) { if (i > 0 && ListenSocket::REUSE_PORT && serverMode) { - listen.reset(ListenSocket::CreateTCPListen()); - listen->SetListenAddr(listenAddrs_); - if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + listenSocketsManager->Reset(); + listenSocketsManager->SetListenAddr(listenAddrs_); + listenSocketsManager->SetListenAddrIpv6(listenAddrsIpv6_); + if (auto ret = listenSocketsManager->Init() != static_cast(NetListen::OK)) { return ret; } - - if (listenAddrsIpv6_.IsIpv6()) { - listenIpv6.reset(ListenSocket::CreateTCPListen()); - listenIpv6->SetListenAddr(listenAddrsIpv6_); - if (auto ret = listenIpv6->Init() != static_cast(NetListen::OK)) { - return ret; - } - } } // timer only works in the first thread - bool ret = i == 0 ? thread->Start(listen, listenIpv6, timer_) : thread->Start(listen, listenIpv6, nullptr); + bool ret = i == 0 ? thread->Start(listenSocketsManager, timer_) : thread->Start(listenSocketsManager, nullptr); if (!ret) { return -1; } diff --git a/src/net/kqueue_event.cc b/src/net/kqueue_event.cc index fcff0dc..7fb4ebc 100644 --- a/src/net/kqueue_event.cc +++ b/src/net/kqueue_event.cc @@ -25,6 +25,9 @@ bool KqueueEvent::Init() { } if (mode_ & EVENT_MODE_READ) { AddEvent(0, listen_->Fd(), EVENT_READ); + if (listenIpv6_) { + AddEvent(listenIpv6_->Fd(), listenIpv6_->Fd(), EVENT_READ); + } } if (pipe(pipeFd_) == -1) { ERROR("pipe error:{}", errno); @@ -106,7 +109,7 @@ void KqueueEvent::EventRead() { } std::shared_ptr conn; if (events[i].filter == EVENT_READ) { - if (events[i].ident != listen_->Fd()) { + if (events[i].ident != listen_->Fd() && events[i].data.u64 != listenIpv6_->Fd()) { # ifdef HAVE_64BIT auto connId = reinterpret_cast(events[i].udata); # else @@ -169,6 +172,10 @@ void KqueueEvent::DoRead(const struct kevent &event, const std::shared_ptr(nullptr); auto connFd = listen_->OnReadable(newConn, nullptr); onCreate_(connFd, newConn); + } else if (event.ident == listenIpv6_->Fd()) { + auto newConn = std::make_shared(nullptr); + auto connFd = listenIpv6_->OnReadable(newConn, nullptr); + onCreate_(connFd, newConn); } else if (conn) { std::string readBuff; int ret = conn->netEvent_->OnReadable(conn, &readBuff); diff --git a/src/net/listen_sockets_mananger.h b/src/net/listen_sockets_mananger.h new file mode 100644 index 0000000..6352ef9 --- /dev/null +++ b/src/net/listen_sockets_mananger.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023-present, Arana/Kiwi Community. All rights reserved. + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +#pragma once + +#include "listen_socket.h" +#include "socket_addr.h" + +namespace net { + +class ListenSocketsManager { + public: + ListenSocketsManager(bool Ipv6 = false) : listen_(ListenSocket::CreateTCPListen()), Ipv6_(Ipv6) { + if (Ipv6_) { + listenIpv6_ = std::shared_ptr(ListenSocket::CreateTCPListen()); + } + } + + ~ListenSocketsManager() = default; + + void SetListenAddr(const SocketAddr &addr) { listen_->SetListenAddr(addr); } + + void SetListenAddrIpv6(const SocketAddr &addr) { + Ipv6_ = true; + listenIpv6_->SetListenAddr(addr); + } + + int Init() { + if (auto ret = listen_->Init() != static_cast(NetListen::OK)) { + return ret; + } + + if (Ipv6_) { + if (auto ret = listenIpv6_->Init() != static_cast(NetListen::OK)) { + return ret; + } + } + + return static_cast(NetListen::OK); + } + + void Reset() { + listen_.reset(ListenSocket::CreateTCPListen()); + listenIpv6_.reset(ListenSocket::CreateTCPListen()); + } + + std::shared_ptr GetListenSocket() { return listen_; } + + std::shared_ptr GetListenSocketIpv6() { return listenIpv6_; } + + private: + std::shared_ptr listen_; + std::shared_ptr listenIpv6_; + bool Ipv6_ = false; +}; + +} // namespace net diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 8999047..00cad0d 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -16,6 +16,7 @@ #include "callback_function.h" #include "config.h" #include "io_thread.h" +#include "listen_sockets_mananger.h" #if defined(HAVE_EPOLL) @@ -54,7 +55,7 @@ class ThreadManager { inline void SetOnClose(const OnClose &func) { onClose_ = func; } // Start the thread and initialize the event - bool Start(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer); + bool Start(const std::shared_ptr &listenSocketsManager, const std::shared_ptr &timer); // Stop the thread void Stop(); @@ -82,7 +83,8 @@ class ThreadManager { private: // Create read thread - bool CreateReadThread(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer); + bool CreateReadThread(const std::shared_ptr &listenSocketsManager, + const std::shared_ptr &timer); // Create write thread if rwSeparation_ is true bool CreateWriteThread(); @@ -121,8 +123,9 @@ ThreadManager::~ThreadManager() { template requires HasSetFdFunction -bool ThreadManager::Start(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer) { - if (!CreateReadThread(listen, listenIpv6, timer)) { +bool ThreadManager::Start(const std::shared_ptr &listenSocketsManager, + const std::shared_ptr &timer) { + if (!CreateReadThread(listenSocketsManager, timer)) { return false; } if (rwSeparation_) { @@ -272,7 +275,8 @@ void ThreadManager::SendPacket(const T &conn, std::string &&msg) { template requires HasSetFdFunction -bool ThreadManager::CreateReadThread(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, const std::shared_ptr &timer) { +bool ThreadManager::CreateReadThread(const std::shared_ptr &listenSocketsManager, + const std::shared_ptr &timer) { std::shared_ptr event; int8_t eventMode = BaseEvent::EVENT_MODE_READ; if (!rwSeparation_) { @@ -280,9 +284,11 @@ bool ThreadManager::CreateReadThread(const std::shared_ptr &listen, } #if defined(HAVE_EPOLL) - event = std::make_shared(listen, listenIpv6, eventMode); + event = std::make_shared(listenSocketsManager->GetListenSocket(), + listenSocketsManager->GetListenSocketIpv6(), eventMode); #elif defined(HAVE_KQUEUE) - event = std::make_shared(listen, listenIpv6, eventMode); + event = std::make_shared(listenSocketsManager->GetListenSocket(), + listenSocketsManager->GetListenSocketIpv6(), eventMode); #endif event->AddTimer(timer); From d7ffa9b432491c4f23a7d3eef66cd8b1f8f8435d Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 3 Dec 2024 03:44:39 +0800 Subject: [PATCH 11/33] use vector --- src/kiwi.cc | 2 +- src/net/base_event.h | 20 +++++++--- src/net/epoll_event.cc | 25 ++++--------- src/net/epoll_event.h | 8 +++- src/net/event_server.h | 41 +++++++++++---------- src/net/listen_socket.h | 2 + src/net/listen_sockets_mananger.h | 61 ------------------------------- src/net/thread_manager.h | 25 +++++-------- 8 files changed, 62 insertions(+), 122 deletions(-) delete mode 100644 src/net/listen_sockets_mananger.h diff --git a/src/kiwi.cc b/src/kiwi.cc index 022221e..9adda58 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -206,7 +206,7 @@ bool KiwiDB::Init() { net::SocketAddr addrIpv6("::1", 10000); INFO("Add listen addr: {}, port: {}", addrIpv6.GetIP(), addrIpv6.GetPort()); - event_server_->AddListenAddrIpv6(addrIpv6); + event_server_->AddListenAddr(addrIpv6); event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); diff --git a/src/net/base_event.h b/src/net/base_event.h index f4b0908..1f41d5e 100644 --- a/src/net/base_event.h +++ b/src/net/base_event.h @@ -17,6 +17,7 @@ #include #include "callback_function.h" +#include "listen_socket.h" #include "net_event.h" #include "timer.h" @@ -44,8 +45,8 @@ class BaseEvent : public std::enable_shared_from_this { const static int EVENT_ERROR; const static int EVENT_HUB; - BaseEvent(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, int8_t mode, int8_t type) - : listen_(listen), listenIpv6_(listenIpv6), mode_(mode), type_(type){}; + BaseEvent(const std::vector> &listenSockets, int8_t mode, int8_t type) + : listenSockets_(listenSockets), mode_(mode), type_(type){}; virtual ~BaseEvent() = default; @@ -96,6 +97,15 @@ class BaseEvent : public std::enable_shared_from_this { inline int8_t Type() const { return type_; } + inline std::shared_ptr getListenSocket(int fd) { + for (const auto &listen : listenSockets_) { + if (fd == listen->Fd()) { + return listen; + } + } + return nullptr; + } + protected: int evFd_ = 0; // event fd std::atomic running_ = true; @@ -112,10 +122,8 @@ class BaseEvent : public std::enable_shared_from_this { std::shared_ptr timer_; - // listening socket - std::shared_ptr listen_; - - std::shared_ptr listenIpv6_; + // listening sockets + std::vector> listenSockets_; // callback function when a new connection is created std::function)> onCreate_; diff --git a/src/net/epoll_event.cc b/src/net/epoll_event.cc index ab1a016..8740081 100644 --- a/src/net/epoll_event.cc +++ b/src/net/epoll_event.cc @@ -26,9 +26,8 @@ bool EpollEvent::Init() { return false; } if (mode_ & EVENT_MODE_READ) { // Add the listen socket to epoll for read - AddEvent(listen_->Fd(), listen_->Fd(), EVENT_READ); - if (listenIpv6_) { - AddEvent(listenIpv6_->Fd(), listenIpv6_->Fd(), EVENT_READ); + for (auto &listenSocket : listenSockets_) { + AddEvent(listenSocket->Fd(), listenSocket->Fd(), EVENT_READ); } } if (pipe(pipeFd_) == -1) { @@ -108,7 +107,8 @@ void EpollEvent::EventRead() { std::shared_ptr conn; if (events[i].events & EVENT_READ) { // If the event is less than the listen socket, it is a new connection - if (events[i].data.u64 != listen_->Fd() && events[i].data.u64 != listenIpv6_->Fd()) { + // If getListenSocket is nullptr, it means the event is not a listen socket + if (getListenSocket(events[i].data.u64) == nullptr) { conn = getConn_(events[i].data.u64); } DoRead(events[i], conn); @@ -155,25 +155,14 @@ void EpollEvent::EventWrite() { } void EpollEvent::DoRead(const epoll_event &event, const std::shared_ptr &conn) { - if (event.data.u64 == listen_->Fd()) { + auto listenSocket = getListenSocket(event.data.u64); + if (listenSocket != nullptr) { auto newConn = std::make_shared(nullptr); - auto connFd = listen_->OnReadable(newConn, nullptr); + auto connFd = listenSocket->OnReadable(newConn, nullptr); if (connFd < 0) { DoError(event, "accept error"); return; } - // create new connection - // thread_manager.h: 148 - onCreate_(connFd, newConn); - } else if (event.data.u64 == listenIpv6_->Fd()) { - auto newConn = std::make_shared(nullptr); - auto connFd = listenIpv6_->OnReadable(newConn, nullptr); - if (connFd < 0) { - DoError(event, "accept error"); - return; - } - // create new connection - // thread_manager.h: 148 onCreate_(connFd, newConn); } else if (conn) { std::string readBuff; diff --git a/src/net/epoll_event.h b/src/net/epoll_event.h index 67028a7..b16b8eb 100644 --- a/src/net/epoll_event.h +++ b/src/net/epoll_event.h @@ -8,6 +8,7 @@ #pragma once #include "config.h" +#include "listen_socket.h" #ifdef HAVE_EPOLL @@ -22,8 +23,11 @@ namespace net { class EpollEvent : public BaseEvent { public: - explicit EpollEvent(const std::shared_ptr &listen, const std::shared_ptr &listenIpv6, int8_t mode) - : BaseEvent(listen, listenIpv6, mode, BaseEvent::EVENT_TYPE_EPOLL){}; + explicit EpollEvent(const std::vector> &listenSockets, int8_t mode) + : BaseEvent(listenSockets, mode, BaseEvent::EVENT_TYPE_EPOLL) {}; + + EpollEvent(std::nullptr_t, int8_t mode) + : EpollEvent(std::vector>(), mode) {} ~EpollEvent() override { Close(); } diff --git a/src/net/event_server.h b/src/net/event_server.h index 7d7625f..bf4a041 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -7,6 +7,8 @@ #pragma once +#include +#include #include #include #include @@ -19,7 +21,6 @@ #include "io_thread.h" #include "listen_socket.h" #include "thread_manager.h" -#include "listen_sockets_mananger.h" namespace net { @@ -41,9 +42,7 @@ class EventServer final { inline void SetOnClose(OnClose &&func) { onClose_ = std::move(func); } - inline void AddListenAddr(const SocketAddr &addr) { listenAddrs_ = addr; } - - inline void AddListenAddrIpv6(const SocketAddr &addr) { listenAddrsIpv6_ = addr; } + inline void AddListenAddr(const SocketAddr &addr) { listenAddrs_.push_back(addr); } inline void SetRwSeparation(bool separation = true) { rwSeparation_ = separation; } @@ -91,9 +90,7 @@ class EventServer final { OnClose onClose_; // The callback function when the connection is closed - SocketAddr listenAddrs_; // The address to listen on - - SocketAddr listenAddrsIpv6_; // The address to listen on + std::vector listenAddrs_; // The address to listen on std::atomic running_ = true; // Whether the server is running @@ -252,31 +249,37 @@ void EventServer::TCPConnect(const SocketAddr &addr, const std::function requires HasSetFdFunction int EventServer::StartThreadManager(bool serverMode) { - bool isIpv6 = listenAddrsIpv6_.IsIpv6(); - auto listenSocketsManager = std::make_shared(isIpv6); + std::vector> listenSockets; if (serverMode) { - listenSocketsManager->SetListenAddr(listenAddrs_); - listenSocketsManager->SetListenAddrIpv6(listenAddrsIpv6_); + for (auto &listenAddr : listenAddrs_) { + std::shared_ptr listen(ListenSocket::CreateTCPListen()); + listen->SetListenAddr(listenAddr); + listenSockets.push_back(listen); + } - if (auto ret = listenSocketsManager->Init() != static_cast(NetListen::OK)) { - return ret; + for (auto &listen : listenSockets) { + if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + return ret; + } } } int i = 0; for (const auto &thread : threadsManager_) { if (i > 0 && ListenSocket::REUSE_PORT && serverMode) { - listenSocketsManager->Reset(); - listenSocketsManager->SetListenAddr(listenAddrs_); - listenSocketsManager->SetListenAddrIpv6(listenAddrsIpv6_); - if (auto ret = listenSocketsManager->Init() != static_cast(NetListen::OK)) { - return ret; + for (auto &listen : listenSockets) { + auto listenAddr = listen->GetListenAddr(); + listen.reset(ListenSocket::CreateTCPListen()); + listen->SetListenAddr(listenAddr); + if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + return ret; + } } } // timer only works in the first thread - bool ret = i == 0 ? thread->Start(listenSocketsManager, timer_) : thread->Start(listenSocketsManager, nullptr); + bool ret = i == 0 ? thread->Start(listenSockets, timer_) : thread->Start(listenSockets, nullptr); if (!ret) { return -1; } diff --git a/src/net/listen_socket.h b/src/net/listen_socket.h index ab27379..c87ab6a 100644 --- a/src/net/listen_socket.h +++ b/src/net/listen_socket.h @@ -27,6 +27,8 @@ class ListenSocket : public BaseSocket { inline void SetListenAddr(const SocketAddr &addr) { addr_ = addr; } + inline SocketAddr GetListenAddr() const { return addr_; } + // Accept new connection and create new connection object // when the connection is established, the OnCreate function is called int OnReadable(const std::shared_ptr &conn, std::string *readBuff) override; diff --git a/src/net/listen_sockets_mananger.h b/src/net/listen_sockets_mananger.h deleted file mode 100644 index 6352ef9..0000000 --- a/src/net/listen_sockets_mananger.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2023-present, Arana/Kiwi Community. All rights reserved. - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - */ - -#pragma once - -#include "listen_socket.h" -#include "socket_addr.h" - -namespace net { - -class ListenSocketsManager { - public: - ListenSocketsManager(bool Ipv6 = false) : listen_(ListenSocket::CreateTCPListen()), Ipv6_(Ipv6) { - if (Ipv6_) { - listenIpv6_ = std::shared_ptr(ListenSocket::CreateTCPListen()); - } - } - - ~ListenSocketsManager() = default; - - void SetListenAddr(const SocketAddr &addr) { listen_->SetListenAddr(addr); } - - void SetListenAddrIpv6(const SocketAddr &addr) { - Ipv6_ = true; - listenIpv6_->SetListenAddr(addr); - } - - int Init() { - if (auto ret = listen_->Init() != static_cast(NetListen::OK)) { - return ret; - } - - if (Ipv6_) { - if (auto ret = listenIpv6_->Init() != static_cast(NetListen::OK)) { - return ret; - } - } - - return static_cast(NetListen::OK); - } - - void Reset() { - listen_.reset(ListenSocket::CreateTCPListen()); - listenIpv6_.reset(ListenSocket::CreateTCPListen()); - } - - std::shared_ptr GetListenSocket() { return listen_; } - - std::shared_ptr GetListenSocketIpv6() { return listenIpv6_; } - - private: - std::shared_ptr listen_; - std::shared_ptr listenIpv6_; - bool Ipv6_ = false; -}; - -} // namespace net diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 00cad0d..7281c8e 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -16,7 +16,7 @@ #include "callback_function.h" #include "config.h" #include "io_thread.h" -#include "listen_sockets_mananger.h" +#include "listen_socket.h" #if defined(HAVE_EPOLL) @@ -55,7 +55,7 @@ class ThreadManager { inline void SetOnClose(const OnClose &func) { onClose_ = func; } // Start the thread and initialize the event - bool Start(const std::shared_ptr &listenSocketsManager, const std::shared_ptr &timer); + bool Start(const std::vector> listenSockets, const std::shared_ptr &timer); // Stop the thread void Stop(); @@ -83,8 +83,7 @@ class ThreadManager { private: // Create read thread - bool CreateReadThread(const std::shared_ptr &listenSocketsManager, - const std::shared_ptr &timer); + bool CreateReadThread(const std::vector> listenSockets, const std::shared_ptr &timer); // Create write thread if rwSeparation_ is true bool CreateWriteThread(); @@ -123,9 +122,8 @@ ThreadManager::~ThreadManager() { template requires HasSetFdFunction -bool ThreadManager::Start(const std::shared_ptr &listenSocketsManager, - const std::shared_ptr &timer) { - if (!CreateReadThread(listenSocketsManager, timer)) { +bool ThreadManager::Start(const std::vector> listenSockets, const std::shared_ptr &timer) { + if (!CreateReadThread(listenSockets, timer)) { return false; } if (rwSeparation_) { @@ -275,8 +273,7 @@ void ThreadManager::SendPacket(const T &conn, std::string &&msg) { template requires HasSetFdFunction -bool ThreadManager::CreateReadThread(const std::shared_ptr &listenSocketsManager, - const std::shared_ptr &timer) { +bool ThreadManager::CreateReadThread(const std::vector> listenSockets, const std::shared_ptr &timer) { std::shared_ptr event; int8_t eventMode = BaseEvent::EVENT_MODE_READ; if (!rwSeparation_) { @@ -284,11 +281,9 @@ bool ThreadManager::CreateReadThread(const std::shared_ptr(listenSocketsManager->GetListenSocket(), - listenSocketsManager->GetListenSocketIpv6(), eventMode); + event = std::make_shared(listenSockets, eventMode); #elif defined(HAVE_KQUEUE) - event = std::make_shared(listenSocketsManager->GetListenSocket(), - listenSocketsManager->GetListenSocketIpv6(), eventMode); + event = std::make_shared(listenSockets, eventMode); #endif event->AddTimer(timer); @@ -320,9 +315,9 @@ bool ThreadManager::CreateWriteThread() { std::shared_ptr event; #if defined(HAVE_EPOLL) - event = std::make_shared(nullptr, nullptr, BaseEvent::EVENT_MODE_WRITE); + event = std::make_shared(nullptr, BaseEvent::EVENT_MODE_WRITE); #elif defined(HAVE_KQUEUE) - event = std::make_shared(nullptr, nullptr, BaseEvent::EVENT_MODE_WRITE); + event = std::make_shared(nullptr, BaseEvent::EVENT_MODE_WRITE); #endif event->SetOnClose([this](uint64_t connId, std::string &&msg) { OnNetEventClose(connId, std::move(msg)); }); From 945a88eb150057632d611cbe6ef79a15044e0ccf Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 3 Dec 2024 04:03:25 +0800 Subject: [PATCH 12/33] update --- src/kiwi.cc | 2 -- src/net/base_socket.cc | 1 - src/net/client_socket.cc | 2 +- src/net/epoll_event.h | 3 --- src/net/thread_manager.h | 4 ++-- 5 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/kiwi.cc b/src/kiwi.cc index 9adda58..d992f0a 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -19,7 +19,6 @@ #include #include #include -#include #include "client.h" #include "client_map.h" @@ -211,7 +210,6 @@ bool KiwiDB::Init() { event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); event_server_->SetOnCreate([](uint64_t connID, std::shared_ptr& client, const net::SocketAddr& addr) { - INFO("SetOnCreate connID:{} fd:{} IP:{} port:{}", connID, client->GetConnId(), addr.GetIP(), addr.GetPort()); client->SetSocketAddr(addr); client->OnConnect(); ClientMap::getInstance().AddClient(client->GetUniqueID(), client); diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index ed41269..5139104 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -100,7 +100,6 @@ bool BaseSocket::SetDisableIpv6Only() { WARN("SetIpv6Only fd:{} error:{}", Fd(), errno); return false; } - DEBUG("SetIpv6Only fd:{} success", Fd()); return true; } diff --git a/src/net/client_socket.cc b/src/net/client_socket.cc index 2283a2a..754ecd3 100644 --- a/src/net/client_socket.cc +++ b/src/net/client_socket.cc @@ -23,7 +23,7 @@ bool ClientSocket::Connect() { SetRcvBuf(); SetSndBuf(); - int ret = connect(Fd(), addr_.GetAddr(), addr_.GetAddrLen()); + auto ret = connect(Fd(), addr_.GetAddr(), addr_.GetAddrLen()); if (0 != ret) { if (EINPROGRESS == errno) { return true; diff --git a/src/net/epoll_event.h b/src/net/epoll_event.h index b16b8eb..5d14c39 100644 --- a/src/net/epoll_event.h +++ b/src/net/epoll_event.h @@ -26,9 +26,6 @@ class EpollEvent : public BaseEvent { explicit EpollEvent(const std::vector> &listenSockets, int8_t mode) : BaseEvent(listenSockets, mode, BaseEvent::EVENT_TYPE_EPOLL) {}; - EpollEvent(std::nullptr_t, int8_t mode) - : EpollEvent(std::vector>(), mode) {} - ~EpollEvent() override { Close(); } // Initialize the epoll event diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 7281c8e..acaf7b6 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -315,9 +315,9 @@ bool ThreadManager::CreateWriteThread() { std::shared_ptr event; #if defined(HAVE_EPOLL) - event = std::make_shared(nullptr, BaseEvent::EVENT_MODE_WRITE); + event = std::make_shared(std::vector>(), BaseEvent::EVENT_MODE_WRITE); #elif defined(HAVE_KQUEUE) - event = std::make_shared(nullptr, BaseEvent::EVENT_MODE_WRITE); + event = std::make_shared(std::vector>(), BaseEvent::EVENT_MODE_WRITE); #endif event->SetOnClose([this](uint64_t connId, std::string &&msg) { OnNetEventClose(connId, std::move(msg)); }); From 35e553a974b08a3e2065968eff78178ed3eef9da Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 3 Dec 2024 15:37:34 +0800 Subject: [PATCH 13/33] support kqueue listensockets --- src/kiwi.cc | 1 + src/net/kqueue_event.cc | 18 +++++++----------- src/net/kqueue_event.h | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/kiwi.cc b/src/kiwi.cc index d992f0a..5ff1025 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include "client.h" #include "client_map.h" diff --git a/src/net/kqueue_event.cc b/src/net/kqueue_event.cc index 7fb4ebc..535f7bc 100644 --- a/src/net/kqueue_event.cc +++ b/src/net/kqueue_event.cc @@ -24,9 +24,8 @@ bool KqueueEvent::Init() { return false; } if (mode_ & EVENT_MODE_READ) { - AddEvent(0, listen_->Fd(), EVENT_READ); - if (listenIpv6_) { - AddEvent(listenIpv6_->Fd(), listenIpv6_->Fd(), EVENT_READ); + for (auto &listenSocket : listenSockets_) { + AddEvent(listenSocket->Fd(), listenSocket->Fd(), EVENT_READ); } } if (pipe(pipeFd_) == -1) { @@ -92,7 +91,7 @@ void KqueueEvent::EventPoll() { void KqueueEvent::EventRead() { struct kevent events[eventsSize]; struct timespec *pTimeout = nullptr; - struct timespec timeout {}; + struct timespec timeout{}; if (timer_) { pTimeout = &timeout; int waitInterval = static_cast(timer_->Interval()); @@ -109,7 +108,7 @@ void KqueueEvent::EventRead() { } std::shared_ptr conn; if (events[i].filter == EVENT_READ) { - if (events[i].ident != listen_->Fd() && events[i].data.u64 != listenIpv6_->Fd()) { + if (getListenSocket(events[i].data.u64) == nullptr) { # ifdef HAVE_64BIT auto connId = reinterpret_cast(events[i].udata); # else @@ -168,13 +167,10 @@ void KqueueEvent::EventWrite() { } void KqueueEvent::DoRead(const struct kevent &event, const std::shared_ptr &conn) { - if (event.ident == listen_->Fd()) { + auto listenSocket = getListenSocket(event.data.u64); + if (listenSocket != nullptr) { auto newConn = std::make_shared(nullptr); - auto connFd = listen_->OnReadable(newConn, nullptr); - onCreate_(connFd, newConn); - } else if (event.ident == listenIpv6_->Fd()) { - auto newConn = std::make_shared(nullptr); - auto connFd = listenIpv6_->OnReadable(newConn, nullptr); + auto connFd = listenSocket->OnReadable(newConn, nullptr); onCreate_(connFd, newConn); } else if (conn) { std::string readBuff; diff --git a/src/net/kqueue_event.h b/src/net/kqueue_event.h index 0532f92..1f315d1 100644 --- a/src/net/kqueue_event.h +++ b/src/net/kqueue_event.h @@ -23,8 +23,8 @@ namespace net { class KqueueEvent : public BaseEvent { public: - explicit KqueueEvent(std::shared_ptr listen, std::shared_ptr listenIpv6, int8_t mode) - : BaseEvent(std::move(listen), listenIpv6, mode, BaseEvent::EVENT_TYPE_KQUEUE){}; + explicit KqueueEvent(std::vector> &listenSockets, int8_t mode) + : BaseEvent(std::move(listenSockets), mode, BaseEvent::EVENT_TYPE_KQUEUE){}; ~KqueueEvent() override { Close(); } From c02cbf8b0e5cf0bedf28a487e415c51f7643f502 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 3 Dec 2024 16:12:54 +0800 Subject: [PATCH 14/33] Improve code quality --- src/net/base_socket.cc | 6 +++--- src/net/epoll_event.cc | 4 ---- src/net/socket_addr.h | 3 +-- src/net/thread_manager.h | 8 ++++---- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index 5139104..135b16f 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -15,10 +15,10 @@ namespace net { int BaseSocket::CreateTCPSocket(const SocketAddr &addr) { - if (addr.IsIpv4()) { - return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - } else { + if (addr.IsIpv6()) { return ::socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + } else { + return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); } } diff --git a/src/net/epoll_event.cc b/src/net/epoll_event.cc index 8740081..aaa2e91 100644 --- a/src/net/epoll_event.cc +++ b/src/net/epoll_event.cc @@ -127,10 +127,6 @@ void EpollEvent::EventRead() { if (timer_) { timer_->OnTimer(); } - if (nfds < 0) { - ERROR("epoll_wait error errno:{}", errno); - continue; - } } } diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index ec17d9e..ec88abd 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -102,8 +102,7 @@ struct SocketAddr { return 0; } - // Valid return true, invalid return false - bool IsValid() const { return addr_.addr4_.sin_family != 0 || addr_.addr6_.sin6_family != 0; } + bool IsValid() const { return IsIpv4() || IsIpv6(); } bool IsIpv6() const { return addr_.addr6_.sin6_family == AF_INET6; } diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index acaf7b6..9f33c1b 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -55,7 +55,7 @@ class ThreadManager { inline void SetOnClose(const OnClose &func) { onClose_ = func; } // Start the thread and initialize the event - bool Start(const std::vector> listenSockets, const std::shared_ptr &timer); + bool Start(const std::vector>& listenSockets, const std::shared_ptr &timer); // Stop the thread void Stop(); @@ -83,7 +83,7 @@ class ThreadManager { private: // Create read thread - bool CreateReadThread(const std::vector> listenSockets, const std::shared_ptr &timer); + bool CreateReadThread(const std::vector>& listenSockets, const std::shared_ptr &timer); // Create write thread if rwSeparation_ is true bool CreateWriteThread(); @@ -122,7 +122,7 @@ ThreadManager::~ThreadManager() { template requires HasSetFdFunction -bool ThreadManager::Start(const std::vector> listenSockets, const std::shared_ptr &timer) { +bool ThreadManager::Start(const std::vector>& listenSockets, const std::shared_ptr &timer) { if (!CreateReadThread(listenSockets, timer)) { return false; } @@ -273,7 +273,7 @@ void ThreadManager::SendPacket(const T &conn, std::string &&msg) { template requires HasSetFdFunction -bool ThreadManager::CreateReadThread(const std::vector> listenSockets, const std::shared_ptr &timer) { +bool ThreadManager::CreateReadThread(const std::vector>& listenSockets, const std::shared_ptr &timer) { std::shared_ptr event; int8_t eventMode = BaseEvent::EVENT_MODE_READ; if (!rwSeparation_) { From 120cc3faad9bce01f556023f38350a5c702611c3 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 3 Dec 2024 21:24:00 +0800 Subject: [PATCH 15/33] support conf --- etc/conf/kiwi.conf | 5 ++--- src/config.cc | 3 +++ src/config_parser.cc | 2 ++ src/kiwi.cc | 25 +++++++++++++++++++------ src/kiwi.h | 1 + src/net/event_server.h | 3 --- src/praft/praft.cc | 2 +- 7 files changed, 28 insertions(+), 13 deletions(-) diff --git a/etc/conf/kiwi.conf b/etc/conf/kiwi.conf index 35bd35d..afe7cd4 100644 --- a/etc/conf/kiwi.conf +++ b/etc/conf/kiwi.conf @@ -10,7 +10,7 @@ port 9221 # If you want you can bind a single interface, if the bind option is not # specified all the interfaces will listen for incoming connections. # -ip 127.0.0.1 +ip 127.0.0.1,::1 # Close the connection after a client is idle for N seconds (0 to disable) @@ -344,8 +344,7 @@ rocksdb-level0-stop-writes-trigger 36 # default 86400 * 7 rocksdb-ttl-second 604800 # default 86400 * 3 -rocksdb-periodic-second 259200; -0 +rocksdb-periodic-second 259200 ############################### RAFT ############################### use-raft no # Braft relies on brpc to communicate via the default port number plus the port offset diff --git a/src/config.cc b/src/config.cc index 7e96132..dafd339 100644 --- a/src/config.cc +++ b/src/config.cc @@ -7,6 +7,7 @@ Responsible for managing the runtime configuration information of kiwi. */ +#include #include #include #include @@ -149,6 +150,8 @@ bool PConfig::LoadFromFile(const std::string& file_name) { return false; } + std::cout << "------------[Load ok]--------------" << std::endl; + // During the initialization phase, so there is no need to hold a lock. for (auto& [key, value] : parser_.GetMap()) { if (auto iter = config_map_.find(key); iter != config_map_.end()) { diff --git a/src/config_parser.cc b/src/config_parser.cc index 4f2cd07..4df3fff 100644 --- a/src/config_parser.cc +++ b/src/config_parser.cc @@ -8,6 +8,7 @@ */ #include "config_parser.h" +#include #include #include "memory_file.h" @@ -61,6 +62,7 @@ bool ConfigParser::Load(const char* FileName) { bReadKey = true; if (!key.empty()) { + std::cout << "key: " << key << " value: " << value << std::endl; data_[key].push_back(value); key.clear(); diff --git a/src/kiwi.cc b/src/kiwi.cc index 5ff1025..4dd2bc9 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -152,6 +152,16 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { return true; } +std::vector KiwiDB::ParseIp(const std::string& ips) { + std::vector ip_list; + std::string ip; + std::istringstream iss(ips); + while (std::getline(iss, ip, ',')) { + ip_list.push_back(ip); + } + return ip_list; +} + void KiwiDB::OnNewConnection(uint64_t connId, std::shared_ptr& client, const net::SocketAddr& addr) { INFO("New connection from {}:{}", addr.GetIP(), addr.GetPort()); client->SetSocketAddr(addr); @@ -200,13 +210,16 @@ bool KiwiDB::Init() { event_server_->SetRwSeparation(true); - net::SocketAddr addr(g_config.ip.ToString(), g_config.port.load()); - INFO("Add listen addr: {}, port: {}", g_config.ip.ToString(), g_config.port.load()); - event_server_->AddListenAddr(addr); + DEBUG("g_config.ip: {}", g_config.ip.ToString()); - net::SocketAddr addrIpv6("::1", 10000); - INFO("Add listen addr: {}, port: {}", addrIpv6.GetIP(), addrIpv6.GetPort()); - event_server_->AddListenAddr(addrIpv6); + auto ip_list = ParseIp(g_config.ip.ToString()); + + for (const auto& ip : ip_list) { + DEBUG("ip: {}", ip); + net::SocketAddr addr(ip, g_config.port.load()); + INFO("Add listen addr: {}, port: {}", ip, g_config.port.load()); + event_server_->AddListenAddr(addr); + } event_server_->SetOnInit([](std::shared_ptr* client) { *client = std::make_shared(); }); diff --git a/src/kiwi.h b/src/kiwi.h index 648a24c..40c2e9e 100644 --- a/src/kiwi.h +++ b/src/kiwi.h @@ -40,6 +40,7 @@ class KiwiDB final { ~KiwiDB() = default; bool ParseArgs(int ac, char* av[]); + std::vector ParseIp(const std::string& ip); const PString& GetConfigName() const { return cfg_file_; } bool Init(); diff --git a/src/net/event_server.h b/src/net/event_server.h index bf4a041..98902d9 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -256,9 +256,6 @@ int EventServer::StartThreadManager(bool serverMode) { std::shared_ptr listen(ListenSocket::CreateTCPListen()); listen->SetListenAddr(listenAddr); listenSockets.push_back(listen); - } - - for (auto &listen : listenSockets) { if (auto ret = listen->Init() != static_cast(NetListen::OK)) { return ret; } diff --git a/src/praft/praft.cc b/src/praft/praft.cc index cf76612..c4acb6c 100644 --- a/src/praft/praft.cc +++ b/src/praft/praft.cc @@ -126,7 +126,7 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { assert(group_id.size() == RAFT_GROUPID_LEN); this->group_id_ = group_id; - // FIXME: g_config.ip is default to 127.0.0.0, which may not work in cluster. + // FIXME: g_config.ip is default to 127.0.0.1, which may not work in cluster. raw_addr_ = g_config.ip.ToString() + ":" + std::to_string(port); butil::ip_t ip; auto ret = butil::str2ip(g_config.ip.ToString().c_str(), &ip); From c928b1b53d06f0f7f51d0dac9dcc843a4a640e5d Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Tue, 3 Dec 2024 21:33:30 +0800 Subject: [PATCH 16/33] delete debug info --- src/config.cc | 3 --- src/config_parser.cc | 2 -- src/kiwi.cc | 4 ---- 3 files changed, 9 deletions(-) diff --git a/src/config.cc b/src/config.cc index dafd339..7e96132 100644 --- a/src/config.cc +++ b/src/config.cc @@ -7,7 +7,6 @@ Responsible for managing the runtime configuration information of kiwi. */ -#include #include #include #include @@ -150,8 +149,6 @@ bool PConfig::LoadFromFile(const std::string& file_name) { return false; } - std::cout << "------------[Load ok]--------------" << std::endl; - // During the initialization phase, so there is no need to hold a lock. for (auto& [key, value] : parser_.GetMap()) { if (auto iter = config_map_.find(key); iter != config_map_.end()) { diff --git a/src/config_parser.cc b/src/config_parser.cc index 4df3fff..4f2cd07 100644 --- a/src/config_parser.cc +++ b/src/config_parser.cc @@ -8,7 +8,6 @@ */ #include "config_parser.h" -#include #include #include "memory_file.h" @@ -62,7 +61,6 @@ bool ConfigParser::Load(const char* FileName) { bReadKey = true; if (!key.empty()) { - std::cout << "key: " << key << " value: " << value << std::endl; data_[key].push_back(value); key.clear(); diff --git a/src/kiwi.cc b/src/kiwi.cc index 4dd2bc9..b234a11 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -210,12 +210,8 @@ bool KiwiDB::Init() { event_server_->SetRwSeparation(true); - DEBUG("g_config.ip: {}", g_config.ip.ToString()); - auto ip_list = ParseIp(g_config.ip.ToString()); - for (const auto& ip : ip_list) { - DEBUG("ip: {}", ip); net::SocketAddr addr(ip, g_config.port.load()); INFO("Add listen addr: {}, port: {}", ip, g_config.port.load()); event_server_->AddListenAddr(addr); From 6cabd522cbde9950f1cc5b529802446b65b73bbe Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Wed, 4 Dec 2024 19:25:09 +0800 Subject: [PATCH 17/33] update --- .clang-tidy | 2 +- src/cmd_admin.h | 4 ++-- src/kiwi.cc | 6 +---- src/kiwi_logo.h | 2 +- src/net/base_event.h | 4 ++-- src/net/base_socket.cc | 4 ++-- src/net/callback_function.h | 3 +-- src/net/client_socket.h | 2 +- src/net/epoll_event.cc | 11 ++++----- src/net/event_server.h | 2 +- src/net/io_thread.h | 2 +- src/net/kqueue_event.cc | 7 +++--- src/net/kqueue_event.h | 2 +- src/net/listen_socket.cc | 2 +- src/net/socket_addr.h | 29 ++++++++---------------- src/net/stream_socket.cc | 4 +--- src/net/thread_manager.h | 11 +++++---- src/pstd/pstd_string.h | 2 +- src/storage/src/base_data_value_format.h | 2 +- 19 files changed, 42 insertions(+), 59 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index d15157f..92d5545 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -57,7 +57,7 @@ Checks: ' # - { key: readability-identifier-naming.UnionCase, value: CamelCase } # - { key: readability-identifier-naming.VariableCase, value: lower_case } -WarningsAsErrors: '*' +WarningsAsErrors: '' # HeaderFilterRegex: '(|/src|/src/net|/src/pstd|/src/storage)/include' # HeaderFilterRegex: '/src/(net|storage|pstd)/include' diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 5097182..e61c725 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -37,7 +37,7 @@ class CmdConfig : public BaseCmdGroup { private: // std::vector subCmd_; - void DoCmd(PClient* client) override{}; + void DoCmd(PClient* client) override {}; }; class CmdConfigGet : public BaseCmd { @@ -256,7 +256,7 @@ class CmdDebug : public BaseCmdGroup { bool DoInitial(PClient* client) override { return true; }; private: - void DoCmd(PClient* client) override{}; + void DoCmd(PClient* client) override {}; }; class CmdDebugHelp : public BaseCmd { diff --git a/src/kiwi.cc b/src/kiwi.cc index b234a11..8563e70 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -154,11 +154,7 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { std::vector KiwiDB::ParseIp(const std::string& ips) { std::vector ip_list; - std::string ip; - std::istringstream iss(ips); - while (std::getline(iss, ip, ',')) { - ip_list.push_back(ip); - } + pstd::StringSplit(ips, ',', ip_list); return ip_list; } diff --git a/src/kiwi_logo.h b/src/kiwi_logo.h index e51c810..8be8af3 100644 --- a/src/kiwi_logo.h +++ b/src/kiwi_logo.h @@ -12,7 +12,7 @@ #pragma once const char* kiwiLogo = - "\n _ _ __ _ \n" + "\n _ _ __ _ \n" " ( )( ) / )( ) _ _ \n" " _ _ _ __ _ _ ___ _ _ ______ _| || |_ /' /' | |/') (_) _ _ _ (_) \n" " /'_` )( '__)/'_` )/' _ `\\ /'_` )(______)/'_` || '_`\\ /' /' | , < | |( ) ( ) ( )| | \n" diff --git a/src/net/base_event.h b/src/net/base_event.h index 1f41d5e..fb7501f 100644 --- a/src/net/base_event.h +++ b/src/net/base_event.h @@ -46,7 +46,7 @@ class BaseEvent : public std::enable_shared_from_this { const static int EVENT_HUB; BaseEvent(const std::vector> &listenSockets, int8_t mode, int8_t type) - : listenSockets_(listenSockets), mode_(mode), type_(type){}; + : listenSockets_(listenSockets), mode_(mode), type_(type) {}; virtual ~BaseEvent() = default; @@ -97,7 +97,7 @@ class BaseEvent : public std::enable_shared_from_this { inline int8_t Type() const { return type_; } - inline std::shared_ptr getListenSocket(int fd) { + std::shared_ptr getListenSocket(int fd) { for (const auto &listen : listenSockets_) { if (fd == listen->Fd()) { return listen; diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index 135b16f..36af686 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -22,7 +22,6 @@ int BaseSocket::CreateTCPSocket(const SocketAddr &addr) { } } - int BaseSocket::CreateUDPSocket() { return ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); } void BaseSocket::Close() { @@ -96,7 +95,8 @@ bool BaseSocket::SetReusePort() { bool BaseSocket::SetDisableIpv6Only() { int ipv6only = 0; - if (::setsockopt(Fd(), IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&ipv6only), sizeof(ipv6only)) == -1) { + if (::setsockopt(Fd(), IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&ipv6only), sizeof(ipv6only)) == + -1) { WARN("SetIpv6Only fd:{} error:{}", Fd(), errno); return false; } diff --git a/src/net/callback_function.h b/src/net/callback_function.h index ffa7c89..aa07df3 100644 --- a/src/net/callback_function.h +++ b/src/net/callback_function.h @@ -35,8 +35,7 @@ concept HasSetFdFunction = requires(T t, uint64_t id, int8_t index) { { (*t).GetConnId() } -> std::same_as; // GetFd return type is int { (*t).SetThreadIndex(index) } -> std::same_as; // SetThreadIndex return type is void { (*t).GetThreadIndex() } -> std::same_as; // GetThreadIndex return type is int8_t -} -|| std::is_class_v; // If T is an ordinary class, the member function is called directly +} || std::is_class_v; // If T is an ordinary class, the member function is called directly template requires HasSetFdFunction diff --git a/src/net/client_socket.h b/src/net/client_socket.h index 307a268..fbde78b 100644 --- a/src/net/client_socket.h +++ b/src/net/client_socket.h @@ -13,7 +13,7 @@ namespace net { class ClientSocket : public StreamSocket { public: - explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr){}; + explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr) {}; ~ClientSocket() override = default; diff --git a/src/net/epoll_event.cc b/src/net/epoll_event.cc index aaa2e91..6a30683 100644 --- a/src/net/epoll_event.cc +++ b/src/net/epoll_event.cc @@ -41,7 +41,7 @@ bool EpollEvent::Init() { } void EpollEvent::AddEvent(uint64_t id, int fd, int mask) { - struct epoll_event ev{}; + struct epoll_event ev {}; ev.events = mask; ev.data.u64 = id; if (epoll_ctl(EvFd(), EPOLL_CTL_ADD, fd, &ev) == -1) { @@ -60,7 +60,7 @@ void EpollEvent::EventPoll() { } void EpollEvent::AddWriteEvent(uint64_t id, int fd) { - struct epoll_event ev{}; + struct epoll_event ev {}; ev.events = EVENT_WRITE; ev.data.u64 = id; if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event @@ -77,7 +77,7 @@ void EpollEvent::AddWriteEvent(uint64_t id, int fd) { void EpollEvent::DelWriteEvent(uint64_t id, int fd) { if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event to read - struct epoll_event ev{}; + struct epoll_event ev {}; ev.events = EVENT_READ; ev.data.u64 = id; if (epoll_ctl(EvFd(), EPOLL_CTL_MOD, fd, &ev) == -1) { @@ -108,7 +108,7 @@ void EpollEvent::EventRead() { if (events[i].events & EVENT_READ) { // If the event is less than the listen socket, it is a new connection // If getListenSocket is nullptr, it means the event is not a listen socket - if (getListenSocket(events[i].data.u64) == nullptr) { + if (!getListenSocket(events[i].data.u64)) { conn = getConn_(events[i].data.u64); } DoRead(events[i], conn); @@ -151,8 +151,7 @@ void EpollEvent::EventWrite() { } void EpollEvent::DoRead(const epoll_event &event, const std::shared_ptr &conn) { - auto listenSocket = getListenSocket(event.data.u64); - if (listenSocket != nullptr) { + if (auto listenSocket = getListenSocket(event.data.u64); listenSocket) { auto newConn = std::make_shared(nullptr); auto connFd = listenSocket->OnReadable(newConn, nullptr); if (connFd < 0) { diff --git a/src/net/event_server.h b/src/net/event_server.h index 98902d9..c21bffc 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -42,7 +42,7 @@ class EventServer final { inline void SetOnClose(OnClose &&func) { onClose_ = std::move(func); } - inline void AddListenAddr(const SocketAddr &addr) { listenAddrs_.push_back(addr); } + inline void AddListenAddr(const SocketAddr &addr) { listenAddrs_.emplace_back(addr); } inline void SetRwSeparation(bool separation = true) { rwSeparation_ = separation; } diff --git a/src/net/io_thread.h b/src/net/io_thread.h index 4420b61..d9ed681 100644 --- a/src/net/io_thread.h +++ b/src/net/io_thread.h @@ -16,7 +16,7 @@ namespace net { class IOThread { public: - explicit IOThread(const std::shared_ptr &event) : baseEvent_(event){}; + explicit IOThread(const std::shared_ptr &event) : baseEvent_(event) {}; ~IOThread() = default; diff --git a/src/net/kqueue_event.cc b/src/net/kqueue_event.cc index 535f7bc..d99e5d9 100644 --- a/src/net/kqueue_event.cc +++ b/src/net/kqueue_event.cc @@ -91,7 +91,7 @@ void KqueueEvent::EventPoll() { void KqueueEvent::EventRead() { struct kevent events[eventsSize]; struct timespec *pTimeout = nullptr; - struct timespec timeout{}; + struct timespec timeout {}; if (timer_) { pTimeout = &timeout; int waitInterval = static_cast(timer_->Interval()); @@ -108,7 +108,7 @@ void KqueueEvent::EventRead() { } std::shared_ptr conn; if (events[i].filter == EVENT_READ) { - if (getListenSocket(events[i].data.u64) == nullptr) { + if (!getListenSocket(events[i].data.u64)) { # ifdef HAVE_64BIT auto connId = reinterpret_cast(events[i].udata); # else @@ -167,8 +167,7 @@ void KqueueEvent::EventWrite() { } void KqueueEvent::DoRead(const struct kevent &event, const std::shared_ptr &conn) { - auto listenSocket = getListenSocket(event.data.u64); - if (listenSocket != nullptr) { + if (auto listenSocket = getListenSocket(event.data.u64); listenSocket) { auto newConn = std::make_shared(nullptr); auto connFd = listenSocket->OnReadable(newConn, nullptr); onCreate_(connFd, newConn); diff --git a/src/net/kqueue_event.h b/src/net/kqueue_event.h index 1f315d1..edba19f 100644 --- a/src/net/kqueue_event.h +++ b/src/net/kqueue_event.h @@ -24,7 +24,7 @@ namespace net { class KqueueEvent : public BaseEvent { public: explicit KqueueEvent(std::vector> &listenSockets, int8_t mode) - : BaseEvent(std::move(listenSockets), mode, BaseEvent::EVENT_TYPE_KQUEUE){}; + : BaseEvent(std::move(listenSockets), mode, BaseEvent::EVENT_TYPE_KQUEUE) {}; ~KqueueEvent() override { Close(); } diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index 320ff57..5a7270e 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -20,7 +20,7 @@ const int ListenSocket::LISTENQ = 1024; bool ListenSocket::REUSE_PORT = true; int ListenSocket::OnReadable(const std::shared_ptr &conn, std::string *readBuff) { - struct sockaddr_in clientAddr{}; + struct sockaddr_in clientAddr {}; auto newConnFd = Accept(&clientAddr); if (newConnFd == 0) { ERROR("ListenSocket fd:{},Accept error:{}", Fd(), errno); diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index ec88abd..ec3259c 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -50,10 +50,8 @@ struct SocketAddr { const sockaddr *GetAddr() const { if (IsIpv4()) { return reinterpret_cast(&addr_.addr4_); - } else if (IsIpv6()) { - return reinterpret_cast(&addr_.addr6_); } - return nullptr; + return reinterpret_cast(&addr_.addr6_); } socklen_t GetAddrLen() const { @@ -71,35 +69,26 @@ struct SocketAddr { if (::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, ipv4_buf, sizeof(ipv4_buf))) { return ipv4_buf; } - } else if (IsIpv6()) { - char ipv6_buf[INET6_ADDRSTRLEN] = {0}; - if (::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, ipv6_buf, sizeof(ipv6_buf))) { - return ipv6_buf; - } } - return {}; + char ipv6_buf[INET6_ADDRSTRLEN] = {0}; + ::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, ipv6_buf, sizeof(ipv6_buf)); + return ipv6_buf; } std::string GetIP(char *buf, socklen_t size) const { if (IsIpv4()) { - if (::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, buf, size)) { - return buf; - } - } else if (IsIpv6()) { - if (::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, buf, size)) { - return buf; - } + ::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, buf, size); + return buf; } - return {}; + ::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, buf, size); + return buf; } uint16_t GetPort() const { if (IsIpv4()) { return ntohs(addr_.addr4_.sin_port); - } else if (IsIpv6()) { - return ntohs(addr_.addr6_.sin6_port); } - return 0; + return ntohs(addr_.addr6_.sin6_port); } bool IsValid() const { return IsIpv4() || IsIpv6(); } diff --git a/src/net/stream_socket.cc b/src/net/stream_socket.cc index 3318267..fb57bd7 100644 --- a/src/net/stream_socket.cc +++ b/src/net/stream_socket.cc @@ -63,9 +63,7 @@ int StreamSocket::Read(std::string *readBuff) { } } else if (ret == 0) { return NE_CLOSE; - } - - if (ret > 0) { + } else if (ret > 0) { readBuff->append(readBuffer, ret); } if (!NoBlock()) { diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 9f33c1b..b0b99b1 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -55,7 +55,7 @@ class ThreadManager { inline void SetOnClose(const OnClose &func) { onClose_ = func; } // Start the thread and initialize the event - bool Start(const std::vector>& listenSockets, const std::shared_ptr &timer); + bool Start(const std::vector> &listenSockets, const std::shared_ptr &timer); // Stop the thread void Stop(); @@ -83,7 +83,8 @@ class ThreadManager { private: // Create read thread - bool CreateReadThread(const std::vector>& listenSockets, const std::shared_ptr &timer); + bool CreateReadThread(const std::vector> &listenSockets, + const std::shared_ptr &timer); // Create write thread if rwSeparation_ is true bool CreateWriteThread(); @@ -122,7 +123,8 @@ ThreadManager::~ThreadManager() { template requires HasSetFdFunction -bool ThreadManager::Start(const std::vector>& listenSockets, const std::shared_ptr &timer) { +bool ThreadManager::Start(const std::vector> &listenSockets, + const std::shared_ptr &timer) { if (!CreateReadThread(listenSockets, timer)) { return false; } @@ -273,7 +275,8 @@ void ThreadManager::SendPacket(const T &conn, std::string &&msg) { template requires HasSetFdFunction -bool ThreadManager::CreateReadThread(const std::vector>& listenSockets, const std::shared_ptr &timer) { +bool ThreadManager::CreateReadThread(const std::vector> &listenSockets, + const std::shared_ptr &timer) { std::shared_ptr event; int8_t eventMode = BaseEvent::EVENT_MODE_READ; if (!rwSeparation_) { diff --git a/src/pstd/pstd_string.h b/src/pstd/pstd_string.h index 837bffe..9b75ff2 100644 --- a/src/pstd/pstd_string.h +++ b/src/pstd/pstd_string.h @@ -35,9 +35,9 @@ #pragma once #include +#include #include #include -#include namespace pstd { diff --git a/src/storage/src/base_data_value_format.h b/src/storage/src/base_data_value_format.h index 2b933af..e765092 100644 --- a/src/storage/src/base_data_value_format.h +++ b/src/storage/src/base_data_value_format.h @@ -97,7 +97,7 @@ class ParsedBaseDataValue : public ParsedInternalValue { } protected: - virtual void SetVersionToValue() override{}; + virtual void SetVersionToValue() override {}; private: const size_t kBaseDataValueSuffixLength = kSuffixReserveLength + kTimestampLength; From fc2c06f435716f84c9f556bbe32cf73c753c3977 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Wed, 4 Dec 2024 20:44:08 +0800 Subject: [PATCH 18/33] clang format --- .clang-tidy | 2 +- src/cmd_admin.h | 4 ++-- src/net/base_event.h | 2 +- src/net/base_socket.h | 2 +- src/net/callback_function.h | 3 ++- src/net/client_socket.h | 2 +- src/net/epoll_event.h | 2 +- src/net/event_server.h | 6 ++---- src/net/io_thread.h | 2 +- src/net/kqueue_event.h | 2 +- src/net/thread_manager.h | 12 ++++-------- src/storage/src/base_data_value_format.h | 2 +- 12 files changed, 18 insertions(+), 23 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 92d5545..d15157f 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -57,7 +57,7 @@ Checks: ' # - { key: readability-identifier-naming.UnionCase, value: CamelCase } # - { key: readability-identifier-naming.VariableCase, value: lower_case } -WarningsAsErrors: '' +WarningsAsErrors: '*' # HeaderFilterRegex: '(|/src|/src/net|/src/pstd|/src/storage)/include' # HeaderFilterRegex: '/src/(net|storage|pstd)/include' diff --git a/src/cmd_admin.h b/src/cmd_admin.h index e61c725..a3b067d 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -37,7 +37,7 @@ class CmdConfig : public BaseCmdGroup { private: // std::vector subCmd_; - void DoCmd(PClient* client) override {}; + void DoCmd(PClient* client) override{}; }; class CmdConfigGet : public BaseCmd { @@ -97,7 +97,7 @@ class CmdClient : public BaseCmdGroup { const static std::string CLIENT_LIST_S; const static std::string CLIENT_KILL_S; - void DoCmd(PClient* client) override {} + void DoCmd(PClient* client) override{} }; class CmdClientGetname : public BaseCmd { diff --git a/src/net/base_event.h b/src/net/base_event.h index fb7501f..c09b130 100644 --- a/src/net/base_event.h +++ b/src/net/base_event.h @@ -46,7 +46,7 @@ class BaseEvent : public std::enable_shared_from_this { const static int EVENT_HUB; BaseEvent(const std::vector> &listenSockets, int8_t mode, int8_t type) - : listenSockets_(listenSockets), mode_(mode), type_(type) {}; + : listenSockets_(listenSockets), mode_(mode), type_(type){}; virtual ~BaseEvent() = default; diff --git a/src/net/base_socket.h b/src/net/base_socket.h index 903fad0..2d5c158 100644 --- a/src/net/base_socket.h +++ b/src/net/base_socket.h @@ -30,7 +30,7 @@ class BaseSocket : public NetEvent { ~BaseSocket() override = default; - void OnError() override {}; + void OnError() override{}; void Close() override; diff --git a/src/net/callback_function.h b/src/net/callback_function.h index aa07df3..ffa7c89 100644 --- a/src/net/callback_function.h +++ b/src/net/callback_function.h @@ -35,7 +35,8 @@ concept HasSetFdFunction = requires(T t, uint64_t id, int8_t index) { { (*t).GetConnId() } -> std::same_as; // GetFd return type is int { (*t).SetThreadIndex(index) } -> std::same_as; // SetThreadIndex return type is void { (*t).GetThreadIndex() } -> std::same_as; // GetThreadIndex return type is int8_t -} || std::is_class_v; // If T is an ordinary class, the member function is called directly +} +|| std::is_class_v; // If T is an ordinary class, the member function is called directly template requires HasSetFdFunction diff --git a/src/net/client_socket.h b/src/net/client_socket.h index fbde78b..307a268 100644 --- a/src/net/client_socket.h +++ b/src/net/client_socket.h @@ -13,7 +13,7 @@ namespace net { class ClientSocket : public StreamSocket { public: - explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr) {}; + explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr){}; ~ClientSocket() override = default; diff --git a/src/net/epoll_event.h b/src/net/epoll_event.h index 5d14c39..87f8667 100644 --- a/src/net/epoll_event.h +++ b/src/net/epoll_event.h @@ -24,7 +24,7 @@ namespace net { class EpollEvent : public BaseEvent { public: explicit EpollEvent(const std::vector> &listenSockets, int8_t mode) - : BaseEvent(listenSockets, mode, BaseEvent::EVENT_TYPE_EPOLL) {}; + : BaseEvent(listenSockets, mode, BaseEvent::EVENT_TYPE_EPOLL){}; ~EpollEvent() override { Close(); } diff --git a/src/net/event_server.h b/src/net/event_server.h index c21bffc..f22ce73 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -107,8 +107,7 @@ class EventServer final { }; template -requires HasSetFdFunction -std::pair EventServer::StartServer(int64_t interval) { +requires HasSetFdFunction std::pair EventServer::StartServer(int64_t interval) { if (threadNum_ <= 0) { return std::pair(false, "thread num must be greater than 0"); } @@ -147,8 +146,7 @@ std::pair EventServer::StartServer(int64_t interval) { } template -requires HasSetFdFunction -std::pair EventServer::StartClientServer() { +requires HasSetFdFunction std::pair EventServer::StartClientServer() { if (threadNum_ <= 0) { return std::pair(false, "thread num must be greater than 0"); } diff --git a/src/net/io_thread.h b/src/net/io_thread.h index d9ed681..4420b61 100644 --- a/src/net/io_thread.h +++ b/src/net/io_thread.h @@ -16,7 +16,7 @@ namespace net { class IOThread { public: - explicit IOThread(const std::shared_ptr &event) : baseEvent_(event) {}; + explicit IOThread(const std::shared_ptr &event) : baseEvent_(event){}; ~IOThread() = default; diff --git a/src/net/kqueue_event.h b/src/net/kqueue_event.h index edba19f..1f315d1 100644 --- a/src/net/kqueue_event.h +++ b/src/net/kqueue_event.h @@ -24,7 +24,7 @@ namespace net { class KqueueEvent : public BaseEvent { public: explicit KqueueEvent(std::vector> &listenSockets, int8_t mode) - : BaseEvent(std::move(listenSockets), mode, BaseEvent::EVENT_TYPE_KQUEUE) {}; + : BaseEvent(std::move(listenSockets), mode, BaseEvent::EVENT_TYPE_KQUEUE){}; ~KqueueEvent() override { Close(); } diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index b0b99b1..0986f31 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -116,10 +116,7 @@ class ThreadManager { }; template -requires HasSetFdFunction -ThreadManager::~ThreadManager() { - Stop(); -} +requires HasSetFdFunction ThreadManager::~ThreadManager() { Stop(); } template requires HasSetFdFunction @@ -207,9 +204,7 @@ void ThreadManager::OnNetEventClose(uint64_t connId, std::string &&err) { template requires HasSetFdFunction -void ThreadManager::CloseConnection(uint64_t connId) { - OnNetEventClose(connId, ""); -} +void ThreadManager::CloseConnection(uint64_t connId) { OnNetEventClose(connId, ""); } template requires HasSetFdFunction @@ -339,7 +334,8 @@ bool ThreadManager::CreateWriteThread() { template requires HasSetFdFunction -uint64_t ThreadManager::DoTCPConnect(T &t, int fd, const std::shared_ptr &conn) { +uint64_t ThreadManager::DoTCPConnect(T &t, int fd, + const std::shared_ptr &conn) { auto connId = getConnId(); if constexpr (IsPointer_v) { t->SetConnId(connId); diff --git a/src/storage/src/base_data_value_format.h b/src/storage/src/base_data_value_format.h index e765092..2b933af 100644 --- a/src/storage/src/base_data_value_format.h +++ b/src/storage/src/base_data_value_format.h @@ -97,7 +97,7 @@ class ParsedBaseDataValue : public ParsedInternalValue { } protected: - virtual void SetVersionToValue() override {}; + virtual void SetVersionToValue() override{}; private: const size_t kBaseDataValueSuffixLength = kSuffixReserveLength + kTimestampLength; From 1ae6c08e18dbb1d4eba8a997bc3de2ddb88c94f4 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Wed, 4 Dec 2024 20:50:14 +0800 Subject: [PATCH 19/33] clang format --- src/cmd_admin.h | 2 +- src/net/thread_manager.h | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cmd_admin.h b/src/cmd_admin.h index a3b067d..5607d6d 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -256,7 +256,7 @@ class CmdDebug : public BaseCmdGroup { bool DoInitial(PClient* client) override { return true; }; private: - void DoCmd(PClient* client) override {}; + void DoCmd(PClient* client) override{}; }; class CmdDebugHelp : public BaseCmd { diff --git a/src/net/thread_manager.h b/src/net/thread_manager.h index 0986f31..cc16848 100644 --- a/src/net/thread_manager.h +++ b/src/net/thread_manager.h @@ -333,9 +333,8 @@ bool ThreadManager::CreateWriteThread() { } template -requires HasSetFdFunction -uint64_t ThreadManager::DoTCPConnect(T &t, int fd, - const std::shared_ptr &conn) { +requires HasSetFdFunction uint64_t ThreadManager::DoTCPConnect(T &t, int fd, + const std::shared_ptr &conn) { auto connId = getConnId(); if constexpr (IsPointer_v) { t->SetConnId(connId); From 7d448ef66665b1b285be9933dad44a321d86a0fa Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Wed, 4 Dec 2024 20:52:19 +0800 Subject: [PATCH 20/33] clang format --- src/cmd_admin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 5607d6d..5097182 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -97,7 +97,7 @@ class CmdClient : public BaseCmdGroup { const static std::string CLIENT_LIST_S; const static std::string CLIENT_KILL_S; - void DoCmd(PClient* client) override{} + void DoCmd(PClient* client) override {} }; class CmdClientGetname : public BaseCmd { From 3b56a5a781b782f010fe5f2cc724a0c7e4205392 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Wed, 4 Dec 2024 21:51:43 +0800 Subject: [PATCH 21/33] update --- src/net/socket_addr.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index ec3259c..0f21bd0 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -41,10 +41,10 @@ struct SocketAddr { if (::inet_pton(AF_INET, ip.c_str(), &addr_.addr4_.sin_addr) == 1) { addr_.addr4_.sin_family = AF_INET; addr_.addr4_.sin_port = htons(hostPort); - } else if (::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr) == 1) { - addr_.addr6_.sin6_family = AF_INET6; - addr_.addr6_.sin6_port = htons(hostPort); } + ::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr); + addr_.addr6_.sin6_family = AF_INET6; + addr_.addr6_.sin6_port = htons(hostPort); } const sockaddr *GetAddr() const { @@ -57,10 +57,8 @@ struct SocketAddr { socklen_t GetAddrLen() const { if (IsIpv4()) { return sizeof(addr_.addr4_); - } else if (IsIpv6()) { - return sizeof(addr_.addr6_); } - return 0; + return sizeof(addr_.addr6_); } std::string GetIP() const { From 2c422e1a8fd97c44606fe343c174730ca6775478 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Wed, 4 Dec 2024 21:55:46 +0800 Subject: [PATCH 22/33] update --- src/net/socket_addr.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index 0f21bd0..63ccea7 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -41,6 +41,7 @@ struct SocketAddr { if (::inet_pton(AF_INET, ip.c_str(), &addr_.addr4_.sin_addr) == 1) { addr_.addr4_.sin_family = AF_INET; addr_.addr4_.sin_port = htons(hostPort); + return; } ::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr); addr_.addr6_.sin6_family = AF_INET6; From 0246161e4eb7da8b754ca1a9d38e05bf4188ec32 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Thu, 12 Dec 2024 17:51:29 +0800 Subject: [PATCH 23/33] rename --- src/kiwi.cc | 4 ++-- src/kiwi.h | 2 +- src/net/base_socket.cc | 2 +- src/net/listen_socket.cc | 2 +- src/net/socket_addr.h | 20 ++++++++++---------- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/kiwi.cc b/src/kiwi.cc index 8563e70..52aa73b 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -152,7 +152,7 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { return true; } -std::vector KiwiDB::ParseIp(const std::string& ips) { +std::vector KiwiDB::ParseIP(const std::string& ips) { std::vector ip_list; pstd::StringSplit(ips, ',', ip_list); return ip_list; @@ -206,7 +206,7 @@ bool KiwiDB::Init() { event_server_->SetRwSeparation(true); - auto ip_list = ParseIp(g_config.ip.ToString()); + auto ip_list = ParseIP(g_config.ip.ToString()); for (const auto& ip : ip_list) { net::SocketAddr addr(ip, g_config.port.load()); INFO("Add listen addr: {}, port: {}", ip, g_config.port.load()); diff --git a/src/kiwi.h b/src/kiwi.h index 40c2e9e..50e86c3 100644 --- a/src/kiwi.h +++ b/src/kiwi.h @@ -40,7 +40,7 @@ class KiwiDB final { ~KiwiDB() = default; bool ParseArgs(int ac, char* av[]); - std::vector ParseIp(const std::string& ip); + std::vector ParseIP(const std::string& ip); const PString& GetConfigName() const { return cfg_file_; } bool Init(); diff --git a/src/net/base_socket.cc b/src/net/base_socket.cc index 36af686..8fd460a 100644 --- a/src/net/base_socket.cc +++ b/src/net/base_socket.cc @@ -15,7 +15,7 @@ namespace net { int BaseSocket::CreateTCPSocket(const SocketAddr &addr) { - if (addr.IsIpv6()) { + if (addr.IsIPV6()) { return ::socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); } else { return ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index 5a7270e..093ef8e 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -89,7 +89,7 @@ bool ListenSocket::Bind() { if (!SetReusePort()) { REUSE_PORT = false; } - if (addr_.IsIpv6()) { + if (addr_.IsIPV6()) { SetDisableIpv6Only(); } diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index 63ccea7..954c206 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -49,21 +49,21 @@ struct SocketAddr { } const sockaddr *GetAddr() const { - if (IsIpv4()) { + if (IsIPV4()) { return reinterpret_cast(&addr_.addr4_); } return reinterpret_cast(&addr_.addr6_); } socklen_t GetAddrLen() const { - if (IsIpv4()) { + if (IsIPV4()) { return sizeof(addr_.addr4_); } return sizeof(addr_.addr6_); } std::string GetIP() const { - if (IsIpv4()) { + if (IsIPV4()) { char ipv4_buf[INET_ADDRSTRLEN] = {0}; if (::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, ipv4_buf, sizeof(ipv4_buf))) { return ipv4_buf; @@ -75,7 +75,7 @@ struct SocketAddr { } std::string GetIP(char *buf, socklen_t size) const { - if (IsIpv4()) { + if (IsIPV4()) { ::inet_ntop(AF_INET, &addr_.addr4_.sin_addr, buf, size); return buf; } @@ -84,26 +84,26 @@ struct SocketAddr { } uint16_t GetPort() const { - if (IsIpv4()) { + if (IsIPV4()) { return ntohs(addr_.addr4_.sin_port); } return ntohs(addr_.addr6_.sin6_port); } - bool IsValid() const { return IsIpv4() || IsIpv6(); } + bool IsValid() const { return IsIPV4() || IsIPV6(); } - bool IsIpv6() const { return addr_.addr6_.sin6_family == AF_INET6; } + bool IsIPV6() const { return addr_.addr6_.sin6_family == AF_INET6; } - bool IsIpv4() const { return addr_.addr4_.sin_family == AF_INET; } + bool IsIPV4() const { return addr_.addr4_.sin_family == AF_INET; } void Clear() { memset(&addr_, 0, sizeof(addr_)); } friend bool operator==(const SocketAddr &a, const SocketAddr &b) { - if (a.IsIpv4() != b.IsIpv4()) { + if (a.IsIPV4() != b.IsIPV4()) { return false; } - if (a.IsIpv4()) { + if (a.IsIPV4()) { return a.addr_.addr4_.sin_addr.s_addr == b.addr_.addr4_.sin_addr.s_addr && a.addr_.addr4_.sin_port == b.addr_.addr4_.sin_port; } From 04ad52477f84a193f2994867ede45f2b9268cbb0 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 30 Dec 2024 04:27:22 +0800 Subject: [PATCH 24/33] update found a bug in config parser --- .clang-tidy | 2 +- etc/conf/kiwi.conf | 8 +++++--- src/cmd_admin.cc | 14 ++++++++++++-- src/config.cc | 7 +++++-- src/config.h | 5 ++++- src/kiwi.cc | 9 +-------- src/kiwi.h | 1 - src/praft/praft.cc | 6 +++--- src/replication.cc | 2 +- 9 files changed, 32 insertions(+), 22 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index d15157f..92d5545 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -57,7 +57,7 @@ Checks: ' # - { key: readability-identifier-naming.UnionCase, value: CamelCase } # - { key: readability-identifier-naming.VariableCase, value: lower_case } -WarningsAsErrors: '*' +WarningsAsErrors: '' # HeaderFilterRegex: '(|/src|/src/net|/src/pstd|/src/storage)/include' # HeaderFilterRegex: '/src/(net|storage|pstd)/include' diff --git a/etc/conf/kiwi.conf b/etc/conf/kiwi.conf index cfab764..cae99e7 100644 --- a/etc/conf/kiwi.conf +++ b/etc/conf/kiwi.conf @@ -9,9 +9,7 @@ port 9221 # If you want you can bind a single interface, if the bind option is not # specified all the interfaces will listen for incoming connections. -# -ip 127.0.0.1,::1 - +ips 127.0.0.1 ::1 # Close the connection after a client is idle for N seconds (0 to disable) timeout 0 @@ -347,5 +345,9 @@ rocksdb-ttl-second 604800 rocksdb-periodic-second 259200 ############################### RAFT ############################### use-raft no + +# The IP address that the Raft server will bind to +raft-ip 127.0.0.1 + # Braft relies on brpc to communicate via the default port number plus the port offset raft-port-offset 10 diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 56349b9..eafd3c0 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -140,8 +140,18 @@ ShutdownCmd::ShutdownCmd(const std::string& name, int16_t arity) bool ShutdownCmd::DoInitial(PClient* client) { // For now, only shutdown need check local - if (client->PeerIP().find("127.0.0.1") == std::string::npos && - client->PeerIP().find(g_config.ip) == std::string::npos) { + bool finded = false; + if (client->PeerIP().find("127.0.0.1") != std::string::npos) { + finded = true; + } else { + for (auto& ip : g_config.ips) { + if (client->PeerIP().find(ip) != std::string::npos) { + finded = true; + break; + } + } + } + if (!finded) { client->SetRes(CmdRes::kErrOther, kCmdNameShutdown + " should be localhost"); return false; } diff --git a/src/config.cc b/src/config.cc index 6455ead..705e6c5 100644 --- a/src/config.cc +++ b/src/config.cc @@ -7,13 +7,13 @@ Responsible for managing the runtime configuration information of kiwi. */ +#include //delete #include #include #include #include "config.h" #include "pstd/pstd_string.h" -#include "store.h" namespace kiwi { @@ -133,7 +133,8 @@ Status MemorySize::SetValue(const std::string& value) { PConfig::PConfig() { AddBool("redis-compatible-mode", &CheckYesNo, true, &redis_compatible_mode); AddBool("daemonize", &CheckYesNo, false, &daemonize); - AddString("ip", false, &ip); + AddStringArray("ips", false, ips); + AddString("raft-ip", false, &raft_ip); AddNumberWithLimit("port", false, &port, PORT_LIMIT_MIN, PORT_LIMIT_MAX); AddNumber("raft-port-offset", true, &raft_port_offset); AddNumber("timeout", true, &timeout); @@ -181,7 +182,9 @@ bool PConfig::LoadFromFile(const std::string& file_name) { if (auto iter = config_map_.find(key); iter != config_map_.end()) { auto& v = config_map_[key]; auto s = v->Set(value.at(0), true); + std::cerr << key <<" : " << value.at(0) << '\n'; if (!s.ok()) { + // FIXME: here a error happened return false; } } diff --git a/src/config.h b/src/config.h index dad56b8..4c10f3b 100644 --- a/src/config.h +++ b/src/config.h @@ -281,7 +281,7 @@ class PConfig { * the server will listen on. * In default, the full address will be "127.0.0.1:9221" */ - std::string ip = "127.0.0.1"; + std::vector ips = {"127.0.0.1"}; uint16_t port = 9221; /* @@ -338,6 +338,9 @@ class PConfig { */ bool use_raft = false; + // raft ip + std::string raft_ip = "127.0.0.1"; + /* * kiwi use the RocksDB to store the data, * and these options below will set to rocksdb::Options, diff --git a/src/kiwi.cc b/src/kiwi.cc index 78b081a..7cd0323 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -152,12 +152,6 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { return true; } -std::vector KiwiDB::ParseIP(const std::string& ips) { - std::vector ip_list; - pstd::StringSplit(ips, ',', ip_list); - return ip_list; -} - void KiwiDB::OnNewConnection(uint64_t connId, std::shared_ptr& client, const net::SocketAddr& addr) { INFO("New connection from {}:{}", addr.GetIP(), addr.GetPort()); client->SetSocketAddr(addr); @@ -207,8 +201,7 @@ bool KiwiDB::Init() { event_server_ = std::make_unique>>(options_); - auto ip_list = ParseIP(g_config.ip); - for (const auto& ip : ip_list) { + for (const auto& ip : g_config.ips) { net::SocketAddr addr(ip, g_config.port); INFO("Add listen addr: {}, port: {}", ip, g_config.port); event_server_->AddListenAddr(addr); diff --git a/src/kiwi.h b/src/kiwi.h index ba813c9..499cd92 100644 --- a/src/kiwi.h +++ b/src/kiwi.h @@ -41,7 +41,6 @@ class KiwiDB final { ~KiwiDB() = default; bool ParseArgs(int ac, char* av[]); - std::vector ParseIP(const std::string& ip); const PString& GetConfigName() const { return options_.GetConfigName(); } bool Init(); diff --git a/src/praft/praft.cc b/src/praft/praft.cc index fb0dc52..4c2e886 100644 --- a/src/praft/praft.cc +++ b/src/praft/praft.cc @@ -127,9 +127,9 @@ butil::Status PRaft::Init(std::string& group_id, bool initial_conf_is_null) { this->group_id_ = group_id; // FIXME: g_config.ip is default to 127.0.0.1, which may not work in cluster. - raw_addr_ = g_config.ip + ":" + std::to_string(port); + raw_addr_ = g_config.raft_ip + ":" + std::to_string(port); butil::ip_t ip; - auto ret = butil::str2ip(g_config.ip.c_str(), &ip); + auto ret = butil::str2ip(g_config.raft_ip.c_str(), &ip); if (ret != 0) { server_.reset(); return ERROR_LOG_AND_STATUS("Failed to convert str_ip to butil::ip_t"); @@ -324,7 +324,7 @@ void PRaft::SendNodeAddRequest(PClient* client) { // Node id in braft are ip:port, the node id param in RAFT.NODE ADD cmd will be ignored. int unused_node_id = 0; auto port = g_config.port + kiwi::g_config.raft_port_offset; - auto raw_addr = g_config.ip + ":" + std::to_string(port); + auto raw_addr = g_config.raft_ip + ":" + std::to_string(port); client->AppendArrayLen(int64_t(4)); client->AppendString("RAFT.NODE"); diff --git a/src/replication.cc b/src/replication.cc index 2a9f4d2..ac416d5 100644 --- a/src/replication.cc +++ b/src/replication.cc @@ -177,7 +177,7 @@ void PReplication::Cron() { if (masterInfo_.addr.IsValid()) { switch (masterInfo_.state) { case kPReplStateNone: { - if (masterInfo_.addr.GetIP() == g_config.ip && masterInfo_.addr.GetPort() == g_config.port) { + if (masterInfo_.addr.GetIP() == g_config.raft_ip && masterInfo_.addr.GetPort() == g_config.port) { ERROR("Fix config, master addr is self addr!"); assert(!!!"wrong config for master addr"); } From 288e6e56afa56c448f01b5786d30fecad6b39eff Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 30 Dec 2024 19:04:14 +0800 Subject: [PATCH 25/33] fix conf bug --- src/config.cc | 2 -- src/config.h | 7 ++++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/config.cc b/src/config.cc index 705e6c5..6d4c08e 100644 --- a/src/config.cc +++ b/src/config.cc @@ -182,9 +182,7 @@ bool PConfig::LoadFromFile(const std::string& file_name) { if (auto iter = config_map_.find(key); iter != config_map_.end()) { auto& v = config_map_[key]; auto s = v->Set(value.at(0), true); - std::cerr << key <<" : " << value.at(0) << '\n'; if (!s.ok()) { - // FIXME: here a error happened return false; } } diff --git a/src/config.h b/src/config.h index 4c10f3b..3c8b5f1 100644 --- a/src/config.h +++ b/src/config.h @@ -44,7 +44,7 @@ class BaseValue { virtual std::string Value() const = 0; - Status Set(const std::string& value, bool force); + Status Set(const std::string& value, bool init_stage); protected: virtual Status SetValue(const std::string&) = 0; @@ -96,7 +96,7 @@ class NumberValue : public BaseValue { public: NumberValue(const std::string& key, CheckFunc check_func_ptr, bool rewritable, T* value_ptr, T min = std::numeric_limits::min(), T max = std::numeric_limits::max()) - : BaseValue(key, check_func_ptr, rewritable), value_(value_ptr), value_min_(min), value_max_(max) { + : BaseValue(key, std::move(check_func_ptr), rewritable), value_(value_ptr), value_min_(min), value_max_(max) { assert(value_ != nullptr); assert(value_min_ <= value_max_); }; @@ -280,8 +280,9 @@ class PConfig { * For kiwi, ip is the address and the port that * the server will listen on. * In default, the full address will be "127.0.0.1:9221" + * and "::1:9221" */ - std::vector ips = {"127.0.0.1"}; + std::vector ips = {"127.0.0.1", "::1"}; uint16_t port = 9221; /* From 3350a789ed3544545bde295e95ec2b73b72b3cad Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 30 Dec 2024 19:08:24 +0800 Subject: [PATCH 26/33] update --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 92d5545..d15157f 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -57,7 +57,7 @@ Checks: ' # - { key: readability-identifier-naming.UnionCase, value: CamelCase } # - { key: readability-identifier-naming.VariableCase, value: lower_case } -WarningsAsErrors: '' +WarningsAsErrors: '*' # HeaderFilterRegex: '(|/src|/src/net|/src/pstd|/src/storage)/include' # HeaderFilterRegex: '/src/(net|storage|pstd)/include' From ebd09076f2bbc4e8a91f5a54b30625edd7157a60 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 6 Jan 2025 21:20:20 +0800 Subject: [PATCH 27/33] make format --- src/config.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.cc b/src/config.cc index b0dd8b3..8c24037 100644 --- a/src/config.cc +++ b/src/config.cc @@ -7,7 +7,7 @@ Responsible for managing the runtime configuration information of kiwi. */ -#include //delete +#include //delete #include #include #include From 3868f5286055d57081cd85105c86e30f5cb0ab94 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Mon, 6 Jan 2025 21:42:17 +0800 Subject: [PATCH 28/33] fix kqueue kevent --- src/net/kqueue_event.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/net/kqueue_event.cc b/src/net/kqueue_event.cc index 478a257..072548e 100644 --- a/src/net/kqueue_event.cc +++ b/src/net/kqueue_event.cc @@ -109,7 +109,7 @@ void KqueueEvent::EventRead() { } std::shared_ptr conn; if (events[i].filter == EVENT_READ) { - if (!getListenSocket(events[i].data.u64)) { + if (!getListenSocket(events[i].ident)) { # ifdef HAVE_64BIT auto connId = reinterpret_cast(events[i].udata); # else @@ -168,7 +168,7 @@ void KqueueEvent::EventWrite() { } void KqueueEvent::DoRead(const struct kevent &event, const std::shared_ptr &conn) { - if (auto listenSocket = getListenSocket(event.data.u64); listenSocket) { + if (auto listenSocket = getListenSocket(event.ident); listenSocket) { auto newConn = std::make_shared(nullptr); auto connFd = listenSocket->OnReadable(newConn, nullptr); onCreate_(connFd, newConn); From f0e0fe17f41b1aa97114b8ded22c85eec1e65491 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Wed, 15 Jan 2025 21:52:20 +0800 Subject: [PATCH 29/33] fix kqueue bugs --- src/net/kqueue_event.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/kqueue_event.h b/src/net/kqueue_event.h index edba19f..2946429 100644 --- a/src/net/kqueue_event.h +++ b/src/net/kqueue_event.h @@ -23,7 +23,7 @@ namespace net { class KqueueEvent : public BaseEvent { public: - explicit KqueueEvent(std::vector> &listenSockets, int8_t mode) + explicit KqueueEvent(const std::vector> &listenSockets, int8_t mode) : BaseEvent(std::move(listenSockets), mode, BaseEvent::EVENT_TYPE_KQUEUE) {}; ~KqueueEvent() override { Close(); } From 886a3d41900b6e102f5efe8b98fd104d388e0f9b Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Thu, 16 Jan 2025 13:50:07 +0800 Subject: [PATCH 30/33] update --- src/net/event_server.h | 2 ++ src/net/socket_addr.h | 8 ++++++-- src/raft/raft.cc | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/net/event_server.h b/src/net/event_server.h index e6033b0..3162474 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -254,6 +254,7 @@ int EventServer::StartThreadManager(bool serverMode) { listen->SetListenAddr(listenAddr); listenSockets.push_back(listen); if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + listenSockets.clear(); // Clean up all sockets return ret; } } @@ -267,6 +268,7 @@ int EventServer::StartThreadManager(bool serverMode) { listen.reset(ListenSocket::CreateTCPListen()); listen->SetListenAddr(listenAddr); if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + listenSockets.clear(); // Clean up all sockets return ret; } } diff --git a/src/net/socket_addr.h b/src/net/socket_addr.h index 954c206..8d44977 100644 --- a/src/net/socket_addr.h +++ b/src/net/socket_addr.h @@ -43,7 +43,9 @@ struct SocketAddr { addr_.addr4_.sin_port = htons(hostPort); return; } - ::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr); + if (::inet_pton(AF_INET6, ip.c_str(), &addr_.addr6_.sin6_addr) != 1) { + throw std::invalid_argument("Invalid IP address format"); + } addr_.addr6_.sin6_family = AF_INET6; addr_.addr6_.sin6_port = htons(hostPort); } @@ -70,7 +72,9 @@ struct SocketAddr { } } char ipv6_buf[INET6_ADDRSTRLEN] = {0}; - ::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, ipv6_buf, sizeof(ipv6_buf)); + if (!::inet_ntop(AF_INET6, &addr_.addr6_.sin6_addr, ipv6_buf, sizeof(ipv6_buf))) { + throw std::runtime_error("Failed to convert IPv6 address to string"); + } return ipv6_buf; } diff --git a/src/raft/raft.cc b/src/raft/raft.cc index 57ac299..e0edb12 100644 --- a/src/raft/raft.cc +++ b/src/raft/raft.cc @@ -126,7 +126,8 @@ butil::Status Raft::Init(std::string& group_id, bool initial_conf_is_null) { assert(group_id.size() == RAFT_GROUPID_LEN); this->group_id_ = group_id; - // FIXME: g_config.ip is default to 127.0.0.1, which may not work in cluster. + // NOTE: Default raft_ip is 127.0.0.1. For cluster setup, configure the appropriate + // IP address in kiwi.conf using the 'raft-ip' directive. raw_addr_ = g_config.raft_ip + ":" + std::to_string(port); butil::ip_t ip; auto ret = butil::str2ip(g_config.raft_ip.c_str(), &ip); From f27731b79e8110fbbd5f037d2f6b351c054dcd39 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Thu, 16 Jan 2025 13:51:17 +0800 Subject: [PATCH 31/33] format --- src/kiwi.cc | 2 +- src/net/epoll_event.cc | 6 +++--- src/net/kqueue_event.cc | 2 +- src/net/listen_socket.cc | 2 +- src/net/timer.cc | 4 +++- src/storage/src/debug.h | 3 ++- src/storage/src/options_helper.h | 2 +- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/kiwi.cc b/src/kiwi.cc index ed8ff95..3eac895 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -89,7 +89,7 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { return false; } if (options_.GetConfigName().empty() && argc > 1 && argv[1] != nullptr) { - struct stat st {}; + struct stat st{}; if (stat(argv[1], &st) == 0 && S_ISREG(st.st_mode) && ::access(argv[1], R_OK) == 0) { options_.SetConfigName(argv[1]); argc = argc - 1; diff --git a/src/net/epoll_event.cc b/src/net/epoll_event.cc index c83413c..fbd7514 100644 --- a/src/net/epoll_event.cc +++ b/src/net/epoll_event.cc @@ -42,7 +42,7 @@ bool EpollEvent::Init() { } void EpollEvent::AddEvent(uint64_t id, int fd, int mask) { - struct epoll_event ev {}; + struct epoll_event ev{}; ev.events = mask; ev.data.u64 = id; if (epoll_ctl(EvFd(), EPOLL_CTL_ADD, fd, &ev) == -1) { @@ -61,7 +61,7 @@ void EpollEvent::EventPoll() { } void EpollEvent::AddWriteEvent(uint64_t id, int fd) { - struct epoll_event ev {}; + struct epoll_event ev{}; ev.events = EVENT_WRITE; ev.data.u64 = id; if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event @@ -77,7 +77,7 @@ void EpollEvent::AddWriteEvent(uint64_t id, int fd) { } void EpollEvent::DelWriteEvent(uint64_t id, int fd) { - struct epoll_event ev {}; + struct epoll_event ev{}; ev.data.u64 = id; if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event to rea ev.events = EVENT_READ; diff --git a/src/net/kqueue_event.cc b/src/net/kqueue_event.cc index 072548e..1178f88 100644 --- a/src/net/kqueue_event.cc +++ b/src/net/kqueue_event.cc @@ -92,7 +92,7 @@ void KqueueEvent::EventPoll() { void KqueueEvent::EventRead() { struct kevent events[eventsSize]; struct timespec *pTimeout = nullptr; - struct timespec timeout {}; + struct timespec timeout{}; if (timer_) { pTimeout = &timeout; int waitInterval = static_cast(timer_->Interval()); diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index 093ef8e..ca3f183 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -20,7 +20,7 @@ const int ListenSocket::LISTENQ = 1024; bool ListenSocket::REUSE_PORT = true; int ListenSocket::OnReadable(const std::shared_ptr &conn, std::string *readBuff) { - struct sockaddr_in clientAddr {}; + struct sockaddr_in clientAddr{}; auto newConnFd = Accept(&clientAddr); if (newConnFd == 0) { ERROR("ListenSocket fd:{},Accept error:{}", Fd(), errno); diff --git a/src/net/timer.cc b/src/net/timer.cc index 6394972..db651b1 100644 --- a/src/net/timer.cc +++ b/src/net/timer.cc @@ -23,7 +23,9 @@ int64_t Timer::AddTask(const std::shared_ptr& task) { void Timer::RePushTask() { for (const auto& task : list_) { task->Next(); - { queue_.push(task); } + { + queue_.push(task); + } } list_.clear(); } diff --git a/src/storage/src/debug.h b/src/storage/src/debug.h index 0e7da3d..47548db 100644 --- a/src/storage/src/debug.h +++ b/src/storage/src/debug.h @@ -9,5 +9,6 @@ # define TRACE(M, ...) fprintf(stderr, "[TRACE] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) #else # define TRACE(M, ...) \ - {} + { \ + } #endif // NDEBUG diff --git a/src/storage/src/options_helper.h b/src/storage/src/options_helper.h index 74c02a5..c5fd7bb 100644 --- a/src/storage/src/options_helper.h +++ b/src/storage/src/options_helper.h @@ -29,7 +29,7 @@ struct MemberTypeInfo { // http://en.cppreference.com/w/cpp/concept/StandardLayoutType // https://gist.github.com/graphitemaster/494f21190bb2c63c5516 template -inline int offset_of(T1 T2::*member) { +inline int offset_of(T1 T2::* member) { static T2 obj; return int(size_t(&(obj.*member)) - size_t(&obj)); } From c7e0f35d106ce07cbb48d8f11e94a9fa3ea6f061 Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Thu, 16 Jan 2025 14:25:28 +0800 Subject: [PATCH 32/33] format --- src/kiwi.cc | 2 +- src/net/epoll_event.cc | 6 +++--- src/net/kqueue_event.cc | 2 +- src/net/listen_socket.cc | 2 +- src/net/timer.cc | 4 +--- src/storage/src/debug.h | 3 +-- src/storage/src/options_helper.h | 2 +- 7 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/kiwi.cc b/src/kiwi.cc index 3eac895..ed8ff95 100644 --- a/src/kiwi.cc +++ b/src/kiwi.cc @@ -89,7 +89,7 @@ bool KiwiDB::ParseArgs(int argc, char* argv[]) { return false; } if (options_.GetConfigName().empty() && argc > 1 && argv[1] != nullptr) { - struct stat st{}; + struct stat st {}; if (stat(argv[1], &st) == 0 && S_ISREG(st.st_mode) && ::access(argv[1], R_OK) == 0) { options_.SetConfigName(argv[1]); argc = argc - 1; diff --git a/src/net/epoll_event.cc b/src/net/epoll_event.cc index fbd7514..c83413c 100644 --- a/src/net/epoll_event.cc +++ b/src/net/epoll_event.cc @@ -42,7 +42,7 @@ bool EpollEvent::Init() { } void EpollEvent::AddEvent(uint64_t id, int fd, int mask) { - struct epoll_event ev{}; + struct epoll_event ev {}; ev.events = mask; ev.data.u64 = id; if (epoll_ctl(EvFd(), EPOLL_CTL_ADD, fd, &ev) == -1) { @@ -61,7 +61,7 @@ void EpollEvent::EventPoll() { } void EpollEvent::AddWriteEvent(uint64_t id, int fd) { - struct epoll_event ev{}; + struct epoll_event ev {}; ev.events = EVENT_WRITE; ev.data.u64 = id; if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event @@ -77,7 +77,7 @@ void EpollEvent::AddWriteEvent(uint64_t id, int fd) { } void EpollEvent::DelWriteEvent(uint64_t id, int fd) { - struct epoll_event ev{}; + struct epoll_event ev {}; ev.data.u64 = id; if (mode_ & EVENT_MODE_READ) { // If it is a read multiplex, modify the event to rea ev.events = EVENT_READ; diff --git a/src/net/kqueue_event.cc b/src/net/kqueue_event.cc index 1178f88..072548e 100644 --- a/src/net/kqueue_event.cc +++ b/src/net/kqueue_event.cc @@ -92,7 +92,7 @@ void KqueueEvent::EventPoll() { void KqueueEvent::EventRead() { struct kevent events[eventsSize]; struct timespec *pTimeout = nullptr; - struct timespec timeout{}; + struct timespec timeout {}; if (timer_) { pTimeout = &timeout; int waitInterval = static_cast(timer_->Interval()); diff --git a/src/net/listen_socket.cc b/src/net/listen_socket.cc index ca3f183..093ef8e 100644 --- a/src/net/listen_socket.cc +++ b/src/net/listen_socket.cc @@ -20,7 +20,7 @@ const int ListenSocket::LISTENQ = 1024; bool ListenSocket::REUSE_PORT = true; int ListenSocket::OnReadable(const std::shared_ptr &conn, std::string *readBuff) { - struct sockaddr_in clientAddr{}; + struct sockaddr_in clientAddr {}; auto newConnFd = Accept(&clientAddr); if (newConnFd == 0) { ERROR("ListenSocket fd:{},Accept error:{}", Fd(), errno); diff --git a/src/net/timer.cc b/src/net/timer.cc index db651b1..6394972 100644 --- a/src/net/timer.cc +++ b/src/net/timer.cc @@ -23,9 +23,7 @@ int64_t Timer::AddTask(const std::shared_ptr& task) { void Timer::RePushTask() { for (const auto& task : list_) { task->Next(); - { - queue_.push(task); - } + { queue_.push(task); } } list_.clear(); } diff --git a/src/storage/src/debug.h b/src/storage/src/debug.h index 47548db..0e7da3d 100644 --- a/src/storage/src/debug.h +++ b/src/storage/src/debug.h @@ -9,6 +9,5 @@ # define TRACE(M, ...) fprintf(stderr, "[TRACE] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) #else # define TRACE(M, ...) \ - { \ - } + {} #endif // NDEBUG diff --git a/src/storage/src/options_helper.h b/src/storage/src/options_helper.h index c5fd7bb..74c02a5 100644 --- a/src/storage/src/options_helper.h +++ b/src/storage/src/options_helper.h @@ -29,7 +29,7 @@ struct MemberTypeInfo { // http://en.cppreference.com/w/cpp/concept/StandardLayoutType // https://gist.github.com/graphitemaster/494f21190bb2c63c5516 template -inline int offset_of(T1 T2::* member) { +inline int offset_of(T1 T2::*member) { static T2 obj; return int(size_t(&(obj.*member)) - size_t(&obj)); } From b4f8d9bcec977bfd887994f4c02b17ced2a88b4d Mon Sep 17 00:00:00 2001 From: marsevilspirit Date: Thu, 16 Jan 2025 14:44:58 +0800 Subject: [PATCH 33/33] update --- src/net/event_server.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/net/event_server.h b/src/net/event_server.h index 3162474..808796f 100644 --- a/src/net/event_server.h +++ b/src/net/event_server.h @@ -253,7 +253,7 @@ int EventServer::StartThreadManager(bool serverMode) { std::shared_ptr listen(ListenSocket::CreateTCPListen()); listen->SetListenAddr(listenAddr); listenSockets.push_back(listen); - if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + if (auto ret = (listen->Init() != static_cast(NetListen::OK))) { listenSockets.clear(); // Clean up all sockets return ret; } @@ -267,7 +267,7 @@ int EventServer::StartThreadManager(bool serverMode) { auto listenAddr = listen->GetListenAddr(); listen.reset(ListenSocket::CreateTCPListen()); listen->SetListenAddr(listenAddr); - if (auto ret = listen->Init() != static_cast(NetListen::OK)) { + if (auto ret = (listen->Init() != static_cast(NetListen::OK))) { listenSockets.clear(); // Clean up all sockets return ret; }