You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by tw...@apache.org on 2023/02/27 04:00:44 UTC
[incubator-kvrocks] branch unstable updated: Minor refactoring of src/common, src/server (#1280)
This is an automated email from the ASF dual-hosted git repository.
twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new 3e17a0ec Minor refactoring of src/common, src/server (#1280)
3e17a0ec is described below
commit 3e17a0ec2bfb5ec2d0a8365311aac5a034867866
Author: Yaroslav <to...@gmail.com>
AuthorDate: Mon Feb 27 06:00:39 2023 +0200
Minor refactoring of src/common, src/server (#1280)
- fix some typos
- rename a few methods
- move variables closer to the lines where they are used
- use `Util::GetTimeStamp()` instead of the `time()` call
- remove explicit test `!= nullptr`
---
src/commands/cmd_list.cc | 4 +-
src/commands/cmd_pubsub.cc | 8 +-
src/commands/cmd_server.cc | 4 +-
src/common/io_util.cc | 23 ++-
src/common/status.h | 2 +-
src/common/string_util.cc | 25 ++-
src/common/task_runner.cc | 12 +-
src/common/task_runner.h | 2 +
src/server/redis_connection.cc | 89 +++++-----
src/server/redis_connection.h | 18 +-
src/server/redis_reply.h | 2 +
src/server/redis_request.cc | 23 ++-
src/server/server.cc | 285 ++++++++++++++++++++----------
src/server/server.h | 27 ++-
src/server/worker.cc | 129 +++++++++-----
src/server/worker.h | 2 +-
src/storage/table_properties_collector.cc | 2 +-
17 files changed, 413 insertions(+), 244 deletions(-)
diff --git a/src/commands/cmd_list.cc b/src/commands/cmd_list.cc
index c45cc37e..da8c9bfb 100644
--- a/src/commands/cmd_list.cc
+++ b/src/commands/cmd_list.cc
@@ -198,7 +198,7 @@ class CommandBPop : public Commander {
}
for (const auto &key : keys_) {
- svr_->AddBlockingKey(key, conn_);
+ svr_->BlockOnKey(key, conn_);
}
bufferevent_setcb(bev, nullptr, WriteCB, EventCB, this);
@@ -301,7 +301,7 @@ class CommandBPop : public Commander {
void unBlockingAll() {
for (const auto &key : keys_) {
- svr_->UnBlockingKey(key, conn_);
+ svr_->UnblockOnKey(key, conn_);
}
}
};
diff --git a/src/commands/cmd_pubsub.cc b/src/commands/cmd_pubsub.cc
index 0e8aa7c9..89931ae8 100644
--- a/src/commands/cmd_pubsub.cc
+++ b/src/commands/cmd_pubsub.cc
@@ -67,12 +67,12 @@ class CommandUnSubscribe : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
if (args_.size() == 1) {
- conn->UnSubscribeAll([output](const std::string &sub_name, int num) {
+ conn->UnsubscribeAll([output](const std::string &sub_name, int num) {
SubscribeCommandReply(output, "unsubscribe", sub_name, num);
});
} else {
for (size_t i = 1; i < args_.size(); i++) {
- conn->UnSubscribeChannel(args_[i]);
+ conn->UnsubscribeChannel(args_[i]);
SubscribeCommandReply(output, "unsubscribe", args_[i],
conn->SubscriptionsCount() + conn->PSubscriptionsCount());
}
@@ -96,12 +96,12 @@ class CommandPUnSubscribe : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
if (args_.size() == 1) {
- conn->PUnSubscribeAll([output](const std::string &sub_name, int num) {
+ conn->PUnsubscribeAll([output](const std::string &sub_name, int num) {
SubscribeCommandReply(output, "punsubscribe", sub_name, num);
});
} else {
for (size_t i = 1; i < args_.size(); i++) {
- conn->PUnSubscribeChannel(args_[i]);
+ conn->PUnsubscribeChannel(args_[i]);
SubscribeCommandReply(output, "punsubscribe", args_[i],
conn->SubscriptionsCount() + conn->PSubscriptionsCount());
}
diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc
index 030a511d..151a43cb 100644
--- a/src/commands/cmd_server.cc
+++ b/src/commands/cmd_server.cc
@@ -310,7 +310,7 @@ class CommandDBSize : public Commander {
std::string ns = conn->GetNamespace();
if (args_.size() == 1) {
KeyNumStats stats;
- svr->GetLastestKeyNumStats(ns, &stats);
+ svr->GetLatestKeyNumStats(ns, &stats);
*output = Redis::Integer(stats.n_key);
} else if (args_.size() == 2 && args_[1] == "scan") {
Status s = svr->AsyncScanDBSize(ns);
@@ -808,7 +808,7 @@ class CommandBGSave : public Commander {
return Status::OK();
}
- Status s = svr->AsyncBgsaveDB();
+ Status s = svr->AsyncBgSaveDB();
if (!s.IsOK()) return s;
*output = Redis::SimpleString("OK");
diff --git a/src/common/io_util.cc b/src/common/io_util.cc
index 3d15c1b1..c4eb51be 100644
--- a/src/common/io_util.cc
+++ b/src/common/io_util.cc
@@ -50,6 +50,7 @@
#define AE_HUP 8 // NOLINT
namespace Util {
+
Status SockSetTcpNoDelay(int fd, int val) {
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1) {
return Status::FromErrno();
@@ -97,7 +98,7 @@ Status SockSetTcpKeepalive(int fd, int interval) {
}
StatusOr<int> SockConnect(const std::string &host, uint32_t port, int conn_timeout, int timeout) {
- addrinfo hints, *servinfo = nullptr, *p = nullptr;
+ addrinfo hints, *servinfo = nullptr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
@@ -106,9 +107,10 @@ StatusOr<int> SockConnect(const std::string &host, uint32_t port, int conn_timeo
if (int rv = getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &servinfo); rv != 0) {
return {Status::NotOK, gai_strerror(rv)};
}
+
auto exit = MakeScopeExit([servinfo] { freeaddrinfo(servinfo); });
- for (p = servinfo; p != nullptr; p = p->ai_next) {
+ for (auto p = servinfo; p != nullptr; p = p->ai_next) {
auto cfd = UniqueFD(socket(p->ai_family, p->ai_socktype, p->ai_protocol));
if (!cfd) continue;
@@ -122,6 +124,7 @@ StatusOr<int> SockConnect(const std::string &host, uint32_t port, int conn_timeo
if (ret != 0 && errno != EINPROGRESS) {
continue;
}
+
auto retmask = Util::aeWait(*cfd, AE_WRITABLE, conn_timeout);
if ((retmask & AE_WRITABLE) == 0 || (retmask & AE_ERROR) != 0 || (retmask & AE_HUP) != 0) {
return Status::FromErrno();
@@ -129,9 +132,10 @@ StatusOr<int> SockConnect(const std::string &host, uint32_t port, int conn_timeo
// restore to the block mode
int socket_arg = 0;
- if ((socket_arg = fcntl(*cfd, F_GETFL, NULL)) < 0) {
+ if (socket_arg = fcntl(*cfd, F_GETFL, NULL); socket_arg < 0) {
return Status::FromErrno();
}
+
socket_arg &= (~O_NONBLOCK);
if (fcntl(*cfd, F_SETFL, socket_arg) < 0) {
return Status::FromErrno();
@@ -145,6 +149,7 @@ StatusOr<int> SockConnect(const std::string &host, uint32_t port, int conn_timeo
if (!s.IsOK()) {
continue;
}
+
if (timeout > 0) {
struct timeval tv;
tv.tv_sec = timeout / 1000;
@@ -190,11 +195,10 @@ ssize_t SockSendFileCore(int out_fd, int in_fd, off_t offset, size_t count) {
// Send file by sendfile actually according to different operation systems,
// please note that, the out socket fd should be in blocking mode.
Status SockSendFile(int out_fd, int in_fd, size_t size) {
- ssize_t nwritten = 0;
off_t offset = 0;
while (size != 0) {
size_t n = size <= 16 * 1024 ? size : 16 * 1024;
- nwritten = SockSendFileCore(out_fd, in_fd, offset, n);
+ ssize_t nwritten = SockSendFileCore(out_fd, in_fd, offset, n);
if (nwritten == -1) {
if (errno == EINTR)
continue;
@@ -210,7 +214,7 @@ Status SockSendFile(int out_fd, int in_fd, size_t size) {
Status SockSetBlocking(int fd, int blocking) {
int flags = 0;
// Old flags
- if ((flags = fcntl(fd, F_GETFL)) == -1) {
+ if (flags = fcntl(fd, F_GETFL); flags == -1) {
return Status::FromErrno("fcntl(F_GETFL)");
}
@@ -231,10 +235,12 @@ StatusOr<std::string> SockReadLine(int fd) {
if (evbuffer_read(evbuf.get(), fd, -1) <= 0) {
return Status::FromErrno("read response err");
}
+
UniqueEvbufReadln line(evbuf.get(), EVBUFFER_EOL_CRLF_STRICT);
if (!line) {
return Status::FromErrno("read response err(empty)");
}
+
return std::string(line.get(), line.length);
}
@@ -246,6 +252,7 @@ int GetPeerAddr(int fd, std::string *addr, uint32_t *port) {
if (getpeername(fd, reinterpret_cast<sockaddr *>(&sa), &sa_len) < 0) {
return -1;
}
+
if (sa.ss_family == AF_INET6) {
char buf[INET6_ADDRSTRLEN];
auto sa6 = reinterpret_cast<sockaddr_in6 *>(&sa);
@@ -288,14 +295,14 @@ bool IsPortInUse(uint32_t port) {
* writable/readable/exception */
int aeWait(int fd, int mask, int timeout) {
pollfd pfd;
- int retmask = 0, retval = 0;
+ int retmask = 0;
memset(&pfd, 0, sizeof(pfd));
pfd.fd = fd;
if (mask & AE_READABLE) pfd.events |= POLLIN;
if (mask & AE_WRITABLE) pfd.events |= POLLOUT;
- if ((retval = poll(&pfd, 1, timeout)) == 1) {
+ if (int retval = poll(&pfd, 1, timeout); retval == 1) {
if (pfd.revents & POLLIN) retmask |= AE_READABLE;
if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE;
if (pfd.revents & POLLERR) retmask |= AE_ERROR;
diff --git a/src/common/status.h b/src/common/status.h
index f98ef404..af8d73a1 100644
--- a/src/common/status.h
+++ b/src/common/status.h
@@ -122,7 +122,7 @@ using first_element_t = typename std::tuple_element_t<0, std::tuple<Ts...>>;
template <typename T>
using remove_cvref_t = typename std::remove_cv_t<typename std::remove_reference_t<T>>;
-}; // namespace type_details
+} // namespace type_details
template <typename, typename = void>
struct StringInStatusOr : private std::string {
diff --git a/src/common/string_util.cc b/src/common/string_util.cc
index 3854875a..732585b4 100644
--- a/src/common/string_util.cc
+++ b/src/common/string_util.cc
@@ -111,30 +111,29 @@ int StringMatchLen(const char *pattern, size_t pattern_len, const char *string,
pattern++;
pattern_len--;
}
+
if (pattern_len == 1) return 1; /* match */
+
while (string_len) {
if (StringMatchLen(pattern + 1, pattern_len - 1, string, string_len, nocase)) return 1; /* match */
string++;
string_len--;
}
return 0; /* no match */
- break;
case '?':
- if (string_len == 0) return 0; /* no match */
string++;
string_len--;
break;
case '[': {
- int not_symbol = 0, match = 0;
-
pattern++;
pattern_len--;
- not_symbol = pattern[0] == '^';
+ int not_symbol = pattern[0] == '^';
if (not_symbol) {
pattern++;
pattern_len--;
}
- match = 0;
+
+ int match = 0;
while (true) {
if (pattern[0] == '\\' && pattern_len >= 2) {
pattern++;
@@ -173,8 +172,11 @@ int StringMatchLen(const char *pattern, size_t pattern_len, const char *string,
pattern++;
pattern_len--;
}
+
if (not_symbol) match = !match;
+
if (!match) return 0; /* no match */
+
string++;
string_len--;
break;
@@ -205,6 +207,7 @@ int StringMatchLen(const char *pattern, size_t pattern_len, const char *string,
break;
}
}
+
if (pattern_len == 0 && string_len == 0) return 1;
return 0;
}
@@ -238,8 +241,6 @@ std::string BytesToHuman(uint64_t n) {
} else {
return fmt::format("{}B", n);
}
-
- return {};
}
std::vector<std::string> TokenizeRedisProtocol(const std::string &value) {
@@ -253,17 +254,20 @@ std::vector<std::string> TokenizeRedisProtocol(const std::string &value) {
uint64_t array_len = 0, bulk_len = 0;
int state = stateArrayLen;
const char *start = value.data(), *end = start + value.size(), *p = nullptr;
+
while (start != end) {
switch (state) {
case stateArrayLen: {
if (start[0] != '*') {
return tokens;
}
+
p = strchr(start, '\r');
if (!p || (p == end) || p[1] != '\n') {
tokens.clear();
return tokens;
}
+
// parse_result expects to must be parsed successfully here
array_len = *ParseInt<uint64_t>(std::string(start + 1, p), 10);
start = p + 2;
@@ -274,11 +278,13 @@ std::vector<std::string> TokenizeRedisProtocol(const std::string &value) {
if (start[0] != '$') {
return tokens;
}
+
p = strchr(start, '\r');
if (!p || (p == end) || p[1] != '\n') {
tokens.clear();
return tokens;
}
+
// parse_result expects to must be parsed successfully here
bulk_len = *ParseInt<uint64_t>(std::string(start + 1, p), 10);
start = p + 2;
@@ -290,6 +296,7 @@ std::vector<std::string> TokenizeRedisProtocol(const std::string &value) {
tokens.clear();
return tokens;
}
+
tokens.emplace_back(start, start + bulk_len);
start += bulk_len + 2;
state = stateBulkLen;
@@ -297,9 +304,11 @@ std::vector<std::string> TokenizeRedisProtocol(const std::string &value) {
}
}
}
+
if (array_len != tokens.size()) {
tokens.clear();
}
+
return tokens;
}
diff --git a/src/common/task_runner.cc b/src/common/task_runner.cc
index 732134ed..9efe930e 100644
--- a/src/common/task_runner.cc
+++ b/src/common/task_runner.cc
@@ -27,11 +27,13 @@
Status TaskRunner::Publish(const Task &task) {
std::lock_guard<std::mutex> guard(mu_);
if (stop_) {
- return Status(Status::NotOK, "the runner was stopped");
+ return {Status::NotOK, "the runner was stopped"};
}
+
if (task_queue_.size() >= max_queue_size_) {
- return Status(Status::NotOK, "the task queue was reached max length");
+ return {Status::NotOK, "the task queue was reached max length"};
}
+
task_queue_.emplace_back(task);
cond_.notify_all();
return Status::OK();
@@ -40,10 +42,10 @@ Status TaskRunner::Publish(const Task &task) {
void TaskRunner::Start() {
stop_ = false;
for (int i = 0; i < n_thread_; i++) {
- threads_.emplace_back(std::thread([this]() {
+ threads_.emplace_back([this]() {
Util::ThreadSetName("task-runner");
this->run();
- }));
+ });
}
}
@@ -70,6 +72,7 @@ void TaskRunner::run() {
std::unique_lock<std::mutex> lock(mu_);
while (!stop_) {
cond_.wait(lock, [this]() -> bool { return stop_ || !task_queue_.empty(); });
+
while (!stop_ && !task_queue_.empty()) {
task = task_queue_.front();
task_queue_.pop_front();
@@ -78,6 +81,7 @@ void TaskRunner::run() {
lock.lock();
}
}
+
task_queue_.clear();
lock.unlock();
// CAUTION: drop the rest of tasks, don't use task runner if the task can't be drop
diff --git a/src/common/task_runner.h b/src/common/task_runner.h
index 7773c0fd..77373154 100644
--- a/src/common/task_runner.h
+++ b/src/common/task_runner.h
@@ -37,6 +37,7 @@ class TaskRunner {
explicit TaskRunner(int n_thread = 1, uint32_t max_queue_size = 10240)
: max_queue_size_(max_queue_size), n_thread_(n_thread) {}
~TaskRunner() = default;
+
Status Publish(const Task &task);
size_t QueueSize() { return task_queue_.size(); }
void Start();
@@ -46,6 +47,7 @@ class TaskRunner {
private:
void run();
+
bool stop_ = false;
uint32_t max_queue_size_;
std::list<Task> task_queue_;
diff --git a/src/server/redis_connection.cc b/src/server/redis_connection.cc
index d60b4842..de060375 100644
--- a/src/server/redis_connection.cc
+++ b/src/server/redis_connection.cc
@@ -29,6 +29,7 @@
#include "redis_connection.h"
#include "server.h"
+#include "time_util.h"
#include "tls_util.h"
#include "worker.h"
@@ -36,8 +37,7 @@ namespace Redis {
Connection::Connection(bufferevent *bev, Worker *owner)
: need_free_bev_(true), bev_(bev), req_(owner->svr_), owner_(owner), svr_(owner->svr_) {
- time_t now = 0;
- time(&now);
+ int64_t now = Util::GetTimeStamp();
create_time_ = now;
last_interaction_ = now;
}
@@ -52,8 +52,8 @@ Connection::~Connection() {
}
}
// unsubscribe all channels and patterns if exists
- UnSubscribeAll();
- PUnSubscribeAll();
+ UnsubscribeAll();
+ PUnsubscribeAll();
}
std::string Connection::ToString() {
@@ -78,9 +78,10 @@ void Connection::OnRead(struct bufferevent *bev, void *ctx) {
if (!s.IsOK()) {
conn->EnableFlag(Redis::Connection::kCloseAfterReply);
conn->Reply(Redis::Error(s.Msg()));
- LOG(INFO) << "Failed to tokenize the request, encounter error: " << s.Msg();
+ LOG(INFO) << "[connection] Failed to tokenize the request. Error: " << s.Msg();
return;
}
+
conn->ExecuteCommands(conn->req_.GetCommands());
if (conn->IsFlagEnabled(kCloseAsync)) {
conn->Close();
@@ -106,11 +107,13 @@ void Connection::OnEvent(bufferevent *bev, int16_t events, void *ctx) {
conn->Close();
return;
}
+
if (events & BEV_EVENT_EOF) {
DLOG(INFO) << "[connection] Going to remove the client: " << conn->GetAddr() << ", while closed by client";
conn->Close();
return;
}
+
if (events & BEV_EVENT_TIMEOUT) {
DLOG(INFO) << "[connection] The client: " << conn->GetAddr() << "] reached timeout";
bufferevent_enable(bev, EV_READ | EV_WRITE);
@@ -134,19 +137,11 @@ void Connection::SetAddr(std::string ip, uint32_t port) {
addr_ = ip_ + ":" + std::to_string(port_);
}
-uint64_t Connection::GetAge() {
- time_t now = 0;
- time(&now);
- return static_cast<uint64_t>(now - create_time_);
-}
+uint64_t Connection::GetAge() { return static_cast<uint64_t>(Util::GetTimeStamp() - create_time_); }
-void Connection::SetLastInteraction() { time(&last_interaction_); }
+void Connection::SetLastInteraction() { last_interaction_ = Util::GetTimeStamp(); }
-uint64_t Connection::GetIdleTime() {
- time_t now = 0;
- time(&now);
- return static_cast<uint64_t>(now - last_interaction_);
-}
+uint64_t Connection::GetIdleTime() { return static_cast<uint64_t>(Util::GetTimeStamp() - last_interaction_); }
// Currently, master connection is not handled in connection
// but in replication thread.
@@ -157,7 +152,9 @@ uint64_t Connection::GetIdleTime() {
// kTypePubsub -> Client subscribed to Pub/Sub channels
uint64_t Connection::GetClientType() {
if (IsFlagEnabled(kSlave)) return kTypeSlave;
- if (!subscribe_channels_.empty() || !subcribe_patterns_.empty()) return kTypePubsub;
+
+ if (!subscribe_channels_.empty() || !subscribe_patterns_.empty()) return kTypePubsub;
+
return kTypeNormal;
}
@@ -166,7 +163,7 @@ std::string Connection::GetFlags() {
if (IsFlagEnabled(kSlave)) flags.append("S");
if (IsFlagEnabled(kCloseAfterReply)) flags.append("c");
if (IsFlagEnabled(kMonitor)) flags.append("M");
- if (!subscribe_channels_.empty() || !subcribe_patterns_.empty()) flags.append("P");
+ if (!subscribe_channels_.empty() || !subscribe_patterns_.empty()) flags.append("P");
if (flags.empty()) flags = "N";
return flags;
}
@@ -181,32 +178,33 @@ void Connection::SubscribeChannel(const std::string &channel) {
for (const auto &chan : subscribe_channels_) {
if (channel == chan) return;
}
+
subscribe_channels_.emplace_back(channel);
owner_->svr_->SubscribeChannel(channel, this);
}
-void Connection::UnSubscribeChannel(const std::string &channel) {
- auto iter = subscribe_channels_.begin();
- for (; iter != subscribe_channels_.end(); iter++) {
+void Connection::UnsubscribeChannel(const std::string &channel) {
+ for (auto iter = subscribe_channels_.begin(); iter != subscribe_channels_.end(); iter++) {
if (*iter == channel) {
subscribe_channels_.erase(iter);
- owner_->svr_->UnSubscribeChannel(channel, this);
+ owner_->svr_->UnsubscribeChannel(channel, this);
return;
}
}
}
-void Connection::UnSubscribeAll(const unsubscribe_callback &reply) {
+void Connection::UnsubscribeAll(const unsubscribe_callback &reply) {
if (subscribe_channels_.empty()) {
- if (reply != nullptr) reply("", static_cast<int>(subcribe_patterns_.size()));
+ if (reply) reply("", static_cast<int>(subscribe_patterns_.size()));
return;
}
+
int removed = 0;
for (const auto &chan : subscribe_channels_) {
- owner_->svr_->UnSubscribeChannel(chan, this);
+ owner_->svr_->UnsubscribeChannel(chan, this);
removed++;
- if (reply != nullptr) {
- reply(chan, static_cast<int>(subscribe_channels_.size() - removed + subcribe_patterns_.size()));
+ if (reply) {
+ reply(chan, static_cast<int>(subscribe_channels_.size() - removed + subscribe_patterns_.size()));
}
}
subscribe_channels_.clear();
@@ -215,56 +213,58 @@ void Connection::UnSubscribeAll(const unsubscribe_callback &reply) {
int Connection::SubscriptionsCount() { return static_cast<int>(subscribe_channels_.size()); }
void Connection::PSubscribeChannel(const std::string &pattern) {
- for (const auto &p : subcribe_patterns_) {
+ for (const auto &p : subscribe_patterns_) {
if (pattern == p) return;
}
- subcribe_patterns_.emplace_back(pattern);
+ subscribe_patterns_.emplace_back(pattern);
owner_->svr_->PSubscribeChannel(pattern, this);
}
-void Connection::PUnSubscribeChannel(const std::string &pattern) {
- auto iter = subcribe_patterns_.begin();
- for (; iter != subcribe_patterns_.end(); iter++) {
+void Connection::PUnsubscribeChannel(const std::string &pattern) {
+ for (auto iter = subscribe_patterns_.begin(); iter != subscribe_patterns_.end(); iter++) {
if (*iter == pattern) {
- subcribe_patterns_.erase(iter);
- owner_->svr_->PUnSubscribeChannel(pattern, this);
+ subscribe_patterns_.erase(iter);
+ owner_->svr_->PUnsubscribeChannel(pattern, this);
return;
}
}
}
-void Connection::PUnSubscribeAll(const unsubscribe_callback &reply) {
- if (subcribe_patterns_.empty()) {
- if (reply != nullptr) reply("", static_cast<int>(subscribe_channels_.size()));
+void Connection::PUnsubscribeAll(const unsubscribe_callback &reply) {
+ if (subscribe_patterns_.empty()) {
+ if (reply) reply("", static_cast<int>(subscribe_channels_.size()));
return;
}
int removed = 0;
- for (const auto &pattern : subcribe_patterns_) {
- owner_->svr_->PUnSubscribeChannel(pattern, this);
+ for (const auto &pattern : subscribe_patterns_) {
+ owner_->svr_->PUnsubscribeChannel(pattern, this);
removed++;
- if (reply != nullptr) {
- reply(pattern, static_cast<int>(subcribe_patterns_.size() - removed + subscribe_channels_.size()));
+ if (reply) {
+ reply(pattern, static_cast<int>(subscribe_patterns_.size() - removed + subscribe_channels_.size()));
}
}
- subcribe_patterns_.clear();
+ subscribe_patterns_.clear();
}
-int Connection::PSubscriptionsCount() { return static_cast<int>(subcribe_patterns_.size()); }
+int Connection::PSubscriptionsCount() { return static_cast<int>(subscribe_patterns_.size()); }
bool Connection::isProfilingEnabled(const std::string &cmd) {
auto config = svr_->GetConfig();
if (config->profiling_sample_ratio == 0) return false;
+
if (!config->profiling_sample_all_commands &&
config->profiling_sample_commands.find(cmd) == config->profiling_sample_commands.end()) {
return false;
}
+
if (config->profiling_sample_ratio == 100 || std::rand() % 100 <= config->profiling_sample_ratio) {
rocksdb::SetPerfLevel(rocksdb::PerfLevel::kEnableTimeExceptForMutex);
rocksdb::get_perf_context()->Reset();
rocksdb::get_iostats_context()->Reset();
return true;
}
+
return false;
}
@@ -279,6 +279,7 @@ void Connection::recordProfilingSampleIfNeed(const std::string &cmd, uint64_t du
std::string iostats_context = rocksdb::get_iostats_context()->ToString(true);
rocksdb::SetPerfLevel(rocksdb::PerfLevel::kDisable);
if (perf_context.empty()) return; // request without db operation
+
auto entry = std::unique_ptr<PerfEntry>();
entry->cmd_name = cmd;
entry->duration = duration;
@@ -406,12 +407,14 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
SetLastCmd(cmd_name);
svr_->stats_.IncrCalls(cmd_name);
+
auto start = std::chrono::high_resolution_clock::now();
bool is_profiling = isProfilingEnabled(cmd_name);
s = current_cmd_->Execute(svr_, this, &reply);
auto end = std::chrono::high_resolution_clock::now();
uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
if (is_profiling) recordProfilingSampleIfNeed(cmd_name, duration);
+
svr_->SlowlogPushEntryIfNeeded(&cmd_tokens, duration);
svr_->stats_.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
svr_->FeedMonitorConns(this, cmd_tokens);
diff --git a/src/server/redis_connection.h b/src/server/redis_connection.h
index 5d443afe..07a24b0b 100644
--- a/src/server/redis_connection.h
+++ b/src/server/redis_connection.h
@@ -34,6 +34,7 @@
class Worker;
namespace Redis {
+
class Connection {
public:
enum Flag {
@@ -61,12 +62,12 @@ class Connection {
using unsubscribe_callback = std::function<void(std::string, int)>;
void SubscribeChannel(const std::string &channel);
- void UnSubscribeChannel(const std::string &channel);
- void UnSubscribeAll(const unsubscribe_callback &reply = nullptr);
+ void UnsubscribeChannel(const std::string &channel);
+ void UnsubscribeAll(const unsubscribe_callback &reply = nullptr);
int SubscriptionsCount();
void PSubscribeChannel(const std::string &pattern);
- void PUnSubscribeChannel(const std::string &pattern);
- void PUnSubscribeAll(const unsubscribe_callback &reply = nullptr);
+ void PUnsubscribeChannel(const std::string &pattern);
+ void PUnsubscribeAll(const unsubscribe_callback &reply = nullptr);
int PSubscriptionsCount();
uint64_t GetAge();
@@ -85,7 +86,7 @@ class Connection {
void SetAddr(std::string ip, uint32_t port);
void SetLastCmd(std::string cmd) { last_cmd_ = std::move(cmd); }
std::string GetIP() { return ip_; }
- int GetPort() { return static_cast<int>(port_); }
+ uint32_t GetPort() { return port_; }
void SetListeningPort(int port) { listening_port_ = port; }
int GetListeningPort() { return listening_port_; }
uint64_t GetClientType();
@@ -134,14 +135,14 @@ class Connection {
bool is_admin_ = false;
bool need_free_bev_ = true;
std::string last_cmd_;
- time_t create_time_;
- time_t last_interaction_;
+ int64_t create_time_;
+ int64_t last_interaction_;
bufferevent *bev_;
Request req_;
Worker *owner_;
std::vector<std::string> subscribe_channels_;
- std::vector<std::string> subcribe_patterns_;
+ std::vector<std::string> subscribe_patterns_;
Server *svr_;
bool in_exec_ = false;
@@ -150,4 +151,5 @@ class Connection {
bool importing_ = false;
};
+
} // namespace Redis
diff --git a/src/server/redis_reply.h b/src/server/redis_reply.h
index 7b0c8919..acbb78c7 100644
--- a/src/server/redis_reply.h
+++ b/src/server/redis_reply.h
@@ -29,6 +29,7 @@
#define CRLF "\r\n" // NOLINT
namespace Redis {
+
void Reply(evbuffer *output, const std::string &data);
std::string SimpleString(const std::string &data);
std::string Error(const std::string &err);
@@ -50,4 +51,5 @@ std::string Array(const std::vector<std::string> &list);
std::string MultiBulkString(const std::vector<std::string> &values, bool output_nil_for_empty_string = true);
std::string MultiBulkString(const std::vector<std::string> &values, const std::vector<rocksdb::Status> &statuses);
std::string Command2RESP(const std::vector<std::string> &cmd_args);
+
} // namespace Redis
diff --git a/src/server/redis_request.cc b/src/server/redis_request.cc
index c5c43381..f3a35f88 100644
--- a/src/server/redis_request.cc
+++ b/src/server/redis_request.cc
@@ -35,12 +35,14 @@
#include "server.h"
namespace Redis {
+
const size_t PROTO_INLINE_MAX_SIZE = 16 * 1024L;
const size_t PROTO_BULK_MAX_SIZE = 512 * 1024L * 1024L;
const size_t PROTO_MULTI_MAX_SIZE = 1024 * 1024L;
Status Request::Tokenize(evbuffer *input) {
size_t pipeline_size = 0;
+
while (true) {
switch (state_) {
case ArrayLen: {
@@ -69,21 +71,25 @@ Status Request::Tokenize(evbuffer *input) {
if (line[0] == '*') {
auto parse_result = ParseInt<int64_t>(std::string(line.get() + 1, line.length - 1), 10);
if (!parse_result) {
- return Status(Status::NotOK, "Protocol error: invalid multibulk length");
+ return {Status::NotOK, "Protocol error: invalid multibulk length"};
}
+
multi_bulk_len_ = *parse_result;
if (isOnlyLF || multi_bulk_len_ > (int64_t)PROTO_MULTI_MAX_SIZE) {
- return Status(Status::NotOK, "Protocol error: invalid multibulk length");
+ return {Status::NotOK, "Protocol error: invalid multibulk length"};
}
+
if (multi_bulk_len_ <= 0) {
multi_bulk_len_ = 0;
continue;
}
+
state_ = BulkLen;
} else {
if (line.length > PROTO_INLINE_MAX_SIZE) {
- return Status(Status::NotOK, "Protocol error: invalid bulk length");
+ return {Status::NotOK, "Protocol error: invalid bulk length"};
}
+
tokens_ = Util::Split(std::string(line.get(), line.length), " \t");
commands_.emplace_back(std::move(tokens_));
state_ = ArrayLen;
@@ -93,23 +99,28 @@ Status Request::Tokenize(evbuffer *input) {
case BulkLen: {
UniqueEvbufReadln line(input, EVBUFFER_EOL_CRLF_STRICT);
if (!line || line.length <= 0) return Status::OK();
+
svr_->stats_.IncrInbondBytes(line.length);
if (line[0] != '$') {
- return Status(Status::NotOK, "Protocol error: expected '$'");
+ return {Status::NotOK, "Protocol error: expected '$'"};
}
+
auto parse_result = ParseInt<uint64_t>(std::string(line.get() + 1, line.length - 1), 10);
if (!parse_result) {
- return Status(Status::NotOK, "Protocol error: invalid bulk length");
+ return {Status::NotOK, "Protocol error: invalid bulk length"};
}
+
bulk_len_ = *parse_result;
if (bulk_len_ > PROTO_BULK_MAX_SIZE) {
- return Status(Status::NotOK, "Protocol error: invalid bulk length");
+ return {Status::NotOK, "Protocol error: invalid bulk length"};
}
+
state_ = BulkData;
break;
}
case BulkData:
if (evbuffer_get_length(input) < bulk_len_ + 2) return Status::OK();
+
char *data = reinterpret_cast<char *>(evbuffer_pullup(input, static_cast<ssize_t>(bulk_len_ + 2)));
tokens_.emplace_back(data, bulk_len_);
evbuffer_drain(input, bulk_len_ + 2);
diff --git a/src/server/server.cc b/src/server/server.cc
index 46d65df9..9c07c34e 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -49,7 +49,8 @@
std::atomic<int> Server::unix_time_ = {0};
constexpr const char *REDIS_VERSION = "4.0.0";
-Server::Server(Engine::Storage *storage, Config *config) : storage_(storage), config_(config) {
+Server::Server(Engine::Storage *storage, Config *config)
+ : storage_(storage), start_time_(Util::GetTimeStamp()), config_(config) {
// init commands stats here to prevent concurrent insert, and cause core
auto commands = Redis::GetOriginalCommands();
for (const auto &iter : *commands) {
@@ -77,33 +78,28 @@ Server::Server(Engine::Storage *storage, Config *config) : storage_(storage), co
if (!config->unixsocket.empty() && i == 0) {
Status s = worker->ListenUnixSocket(config->unixsocket, config->unixsocketperm, config->backlog);
if (!s.IsOK()) {
- LOG(ERROR) << "[server] Failed to listen on unix socket: " << config->unixsocket
- << ", encounter error: " << s.Msg();
+ LOG(ERROR) << "[server] Failed to listen on unix socket: " << config->unixsocket << ". Error: " << s.Msg();
exit(1);
}
LOG(INFO) << "[server] Listening on unix socket: " << config->unixsocket;
}
worker_threads_.emplace_back(std::make_unique<WorkerThread>(std::move(worker)));
}
+
AdjustOpenFilesLimit();
slow_log_.SetMaxEntries(config->slowlog_max_len);
perf_log_.SetMaxEntries(config->profiling_sample_record_max_len);
lua_ = Lua::CreateState();
- fetch_file_threads_num_ = 0;
- time(&start_time_);
- stop_ = false;
- is_loading_ = false;
}
Server::~Server() {
DisconnectSlaves();
- // Wait for all fetch file threads stop and exit and force destroy
- // the server after 60s.
+ // Wait for all fetch file threads stop and exit and force destroy the server after 60s.
int counter = 0;
while (GetFetchFileThreadNum() != 0) {
usleep(100000);
if (++counter == 600) {
- LOG(WARNING) << "Will force destroy the server after waiting 60s, leave " << GetFetchFileThreadNum()
+ LOG(WARNING) << "[server] Will force destroy the server after waiting 60s, leave " << GetFetchFileThreadNum()
<< " fetch file threads are still running";
break;
}
@@ -112,9 +108,11 @@ Server::~Server() {
for (auto &worker_thread : worker_threads_) {
worker_thread.reset();
}
+
for (const auto &iter : conn_ctxs_) {
delete iter.first;
}
+
Lua::DestroyState(lua_);
libevent_global_shutdown();
}
@@ -162,6 +160,7 @@ Status Server::Start() {
for (const auto &worker : worker_threads_) {
worker->Start();
}
+
task_runner_.Start();
// setup server cron thread
cron_thread_ = std::thread([this]() {
@@ -174,6 +173,7 @@ Status Server::Start() {
time_t last_compact_date = 0;
Util::ThreadSetName("compact-check");
CompactionChecker compaction_checker(this->storage_);
+
while (!stop_) {
// Sleep first
std::this_thread::sleep_for(std::chrono::milliseconds(100));
@@ -182,7 +182,7 @@ Status Server::Start() {
auto guard = storage_->ReadLockGuard();
if (storage_->IsClosing()) continue;
- if (is_loading_ == false && ++counter % 600 == 0 // check every minute
+ if (!is_loading_ && ++counter % 600 == 0 // check every minute
&& config_->compaction_checker_range.Enabled()) {
auto now = static_cast<time_t>(Util::GetTimeStamp());
std::tm local_time{};
@@ -203,20 +203,24 @@ Status Server::Start() {
}
}
});
+
memory_startup_use_.store(Stats::GetMemoryRSS(), std::memory_order_relaxed);
- LOG(INFO) << "Ready to accept connections";
+ LOG(INFO) << "[server] Ready to accept connections";
return Status::OK();
}
void Server::Stop() {
stop_ = true;
+
slaveof_mu_.lock();
if (replication_thread_) replication_thread_->Stop();
slaveof_mu_.unlock();
+
for (const auto &worker : worker_threads_) {
worker->Stop();
}
+
rocksdb::CancelAllBackgroundWork(storage_->GetDB(), true);
task_runner_.Stop();
}
@@ -225,6 +229,7 @@ void Server::Join() {
for (const auto &worker : worker_threads_) {
worker->Join();
}
+
task_runner_.Join();
if (cron_thread_.joinable()) cron_thread_.join();
if (compaction_checker_thread_.joinable()) compaction_checker_thread_.join();
@@ -248,6 +253,7 @@ Status Server::AddMaster(const std::string &host, uint32_t port, bool force_reco
// replication, and uses 'listen-port + 1' as thread listening port.
uint32_t master_listen_port = port;
if (GetConfig()->master_use_repl_port) master_listen_port += 1;
+
replication_thread_ = std::make_unique<ReplicationThread>(host, master_listen_port, this);
auto s = replication_thread_->Start([this]() { PrepareRestoreDB(); },
[this]() {
@@ -266,6 +272,7 @@ Status Server::AddMaster(const std::string &host, uint32_t port, bool force_reco
Status Server::RemoveMaster() {
std::lock_guard<std::mutex> guard(slaveof_mu_);
+
if (!master_host_.empty()) {
master_host_.clear();
master_port_ = 0;
@@ -293,9 +300,11 @@ Status Server::AddSlave(Redis::Connection *conn, rocksdb::SequenceNumber next_re
void Server::DisconnectSlaves() {
std::lock_guard<std::mutex> lg(slave_threads_mu_);
+
for (auto &slave_thread : slave_threads_) {
if (!slave_thread->IsStopped()) slave_thread->Stop();
}
+
while (!slave_threads_.empty()) {
auto slave_thread = std::move(slave_threads_.front());
slave_threads_.pop_front();
@@ -305,6 +314,7 @@ void Server::DisconnectSlaves() {
void Server::cleanupExitedSlaves() {
std::lock_guard<std::mutex> lg(slave_threads_mu_);
+
for (auto it = slave_threads_.begin(); it != slave_threads_.end();) {
if ((*it)->IsStopped()) {
auto thread = std::move(*it);
@@ -318,6 +328,7 @@ void Server::cleanupExitedSlaves() {
void Server::FeedMonitorConns(Redis::Connection *conn, const std::vector<std::string> &tokens) {
if (monitor_clients_ <= 0) return;
+
for (const auto &worker_thread : worker_threads_) {
auto worker = worker_thread->GetWorker();
worker->FeedMonitorConns(conn, tokens);
@@ -329,9 +340,9 @@ int Server::PublishMessage(const std::string &channel, const std::string &msg) {
int index = 0;
pubsub_channels_mu_.lock();
+
std::vector<ConnContext> to_publish_conn_ctxs;
- auto iter = pubsub_channels_.find(channel);
- if (iter != pubsub_channels_.end()) {
+ if (auto iter = pubsub_channels_.find(channel); iter != pubsub_channels_.end()) {
for (const auto &conn_ctx : iter->second) {
to_publish_conn_ctxs.emplace_back(*conn_ctx);
}
@@ -375,15 +386,17 @@ int Server::PublishMessage(const std::string &channel, const std::string &msg) {
cnt++;
}
}
+
return cnt;
}
void Server::SubscribeChannel(const std::string &channel, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
+
auto conn_ctx = new ConnContext(conn->Owner(), conn->GetFD());
conn_ctxs_[conn_ctx] = true;
- auto iter = pubsub_channels_.find(channel);
- if (iter == pubsub_channels_.end()) {
+
+ if (auto iter = pubsub_channels_.find(channel); iter == pubsub_channels_.end()) {
std::list<ConnContext *> conn_ctxs;
conn_ctxs.emplace_back(conn_ctx);
pubsub_channels_.insert(std::pair<std::string, std::list<ConnContext *>>(channel, conn_ctxs));
@@ -392,12 +405,14 @@ void Server::SubscribeChannel(const std::string &channel, Redis::Connection *con
}
}
-void Server::UnSubscribeChannel(const std::string &channel, Redis::Connection *conn) {
+void Server::UnsubscribeChannel(const std::string &channel, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
+
auto iter = pubsub_channels_.find(channel);
if (iter == pubsub_channels_.end()) {
return;
}
+
for (const auto &conn_ctx : iter->second) {
if (conn->GetFD() == conn_ctx->fd && conn->Owner() == conn_ctx->owner) {
delConnContext(conn_ctx);
@@ -412,6 +427,7 @@ void Server::UnSubscribeChannel(const std::string &channel, Redis::Connection *c
void Server::GetChannelsByPattern(const std::string &pattern, std::vector<std::string> *channels) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
+
for (const auto &iter : pubsub_channels_) {
if (pattern.empty() || Util::StringMatch(pattern, iter.first, 0)) {
channels->emplace_back(iter.first);
@@ -422,9 +438,9 @@ void Server::GetChannelsByPattern(const std::string &pattern, std::vector<std::s
void Server::ListChannelSubscribeNum(const std::vector<std::string> &channels,
std::vector<ChannelSubscribeNum> *channel_subscribe_nums) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
+
for (const auto &chan : channels) {
- auto iter = pubsub_channels_.find(chan);
- if (iter != pubsub_channels_.end()) {
+ if (auto iter = pubsub_channels_.find(chan); iter != pubsub_channels_.end()) {
channel_subscribe_nums->emplace_back(ChannelSubscribeNum{iter->first, iter->second.size()});
} else {
channel_subscribe_nums->emplace_back(ChannelSubscribeNum{chan, 0});
@@ -434,10 +450,11 @@ void Server::ListChannelSubscribeNum(const std::vector<std::string> &channels,
void Server::PSubscribeChannel(const std::string &pattern, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
+
auto conn_ctx = new ConnContext(conn->Owner(), conn->GetFD());
conn_ctxs_[conn_ctx] = true;
- auto iter = pubsub_patterns_.find(pattern);
- if (iter == pubsub_patterns_.end()) {
+
+ if (auto iter = pubsub_patterns_.find(pattern); iter == pubsub_patterns_.end()) {
std::list<ConnContext *> conn_ctxs;
conn_ctxs.emplace_back(conn_ctx);
pubsub_patterns_.insert(std::pair<std::string, std::list<ConnContext *>>(pattern, conn_ctxs));
@@ -446,12 +463,14 @@ void Server::PSubscribeChannel(const std::string &pattern, Redis::Connection *co
}
}
-void Server::PUnSubscribeChannel(const std::string &pattern, Redis::Connection *conn) {
+void Server::PUnsubscribeChannel(const std::string &pattern, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(pubsub_channels_mu_);
+
auto iter = pubsub_patterns_.find(pattern);
if (iter == pubsub_patterns_.end()) {
return;
}
+
for (const auto &conn_ctx : iter->second) {
if (conn->GetFD() == conn_ctx->fd && conn->Owner() == conn_ctx->owner) {
delConnContext(conn_ctx);
@@ -464,27 +483,31 @@ void Server::PUnSubscribeChannel(const std::string &pattern, Redis::Connection *
}
}
-void Server::AddBlockingKey(const std::string &key, Redis::Connection *conn) {
+void Server::BlockOnKey(const std::string &key, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
- auto iter = blocking_keys_.find(key);
+
auto conn_ctx = new ConnContext(conn->Owner(), conn->GetFD());
conn_ctxs_[conn_ctx] = true;
- if (iter == blocking_keys_.end()) {
+
+ if (auto iter = blocking_keys_.find(key); iter == blocking_keys_.end()) {
std::list<ConnContext *> conn_ctxs;
conn_ctxs.emplace_back(conn_ctx);
blocking_keys_.insert(std::pair<std::string, std::list<ConnContext *>>(key, conn_ctxs));
} else {
iter->second.emplace_back(conn_ctx);
}
+
IncrBlockedClientNum();
}
-void Server::UnBlockingKey(const std::string &key, Redis::Connection *conn) {
+void Server::UnblockOnKey(const std::string &key, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
+
auto iter = blocking_keys_.find(key);
if (iter == blocking_keys_.end()) {
return;
}
+
for (const auto &conn_ctx : iter->second) {
if (conn->GetFD() == conn_ctx->fd && conn->Owner() == conn_ctx->owner) {
delConnContext(conn_ctx);
@@ -495,17 +518,19 @@ void Server::UnBlockingKey(const std::string &key, Redis::Connection *conn) {
break;
}
}
+
DecrBlockedClientNum();
}
void Server::BlockOnStreams(const std::vector<std::string> &keys, const std::vector<Redis::StreamEntryID> &entry_ids,
Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
+
IncrBlockedClientNum();
+
for (size_t i = 0; i < keys.size(); ++i) {
auto consumer = std::make_shared<StreamConsumer>(conn->Owner(), conn->GetFD(), conn->GetNamespace(), entry_ids[i]);
- auto iter = blocked_stream_consumers_.find(keys[i]);
- if (iter == blocked_stream_consumers_.end()) {
+ if (auto iter = blocked_stream_consumers_.find(keys[i]); iter == blocked_stream_consumers_.end()) {
std::set<std::shared_ptr<StreamConsumer>> consumers;
consumers.insert(consumer);
blocked_stream_consumers_.insert(std::make_pair(keys[i], consumers));
@@ -517,7 +542,9 @@ void Server::BlockOnStreams(const std::vector<std::string> &keys, const std::vec
void Server::UnblockOnStreams(const std::vector<std::string> &keys, Redis::Connection *conn) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
+
DecrBlockedClientNum();
+
for (const auto &key : keys) {
auto iter = blocked_stream_consumers_.find(key);
if (iter == blocked_stream_consumers_.end()) {
@@ -540,6 +567,7 @@ void Server::UnblockOnStreams(const std::vector<std::string> &keys, Redis::Conne
void Server::WakeupBlockingConns(const std::string &key, size_t n_conns) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
+
auto iter = blocking_keys_.find(key);
if (iter == blocking_keys_.end() || iter->second.empty()) {
return;
@@ -549,7 +577,7 @@ void Server::WakeupBlockingConns(const std::string &key, size_t n_conns) {
auto conn_ctx = iter->second.front();
auto s = conn_ctx->owner->EnableWriteEvent(conn_ctx->fd);
if (!s.IsOK()) {
- LOG(ERROR) << "failed to enable write event on blocked client " << conn_ctx->fd << ": " << s.Msg();
+ LOG(ERROR) << "[server] Failed to enable write event on blocked client " << conn_ctx->fd << ": " << s.Msg();
}
delConnContext(conn_ctx);
iter->second.pop_front();
@@ -558,6 +586,7 @@ void Server::WakeupBlockingConns(const std::string &key, size_t n_conns) {
void Server::OnEntryAddedToStream(const std::string &ns, const std::string &key, const Redis::StreamEntryID &entry_id) {
std::lock_guard<std::mutex> guard(blocking_keys_mu_);
+
auto iter = blocked_stream_consumers_.find(key);
if (iter == blocked_stream_consumers_.end() || iter->second.empty()) {
return;
@@ -568,7 +597,8 @@ void Server::OnEntryAddedToStream(const std::string &ns, const std::string &key,
if (consumer->ns == ns && entry_id > consumer->last_consumed_id) {
auto s = consumer->owner->EnableWriteEvent(consumer->fd);
if (!s.IsOK()) {
- LOG(ERROR) << "failed to enable write event on blocked stream consumer " << consumer->fd << ": " << s.Msg();
+ LOG(ERROR) << "[server] Failed to enable write event on blocked stream consumer " << consumer->fd << ": "
+ << s.Msg();
}
it = iter->second.erase(it);
} else {
@@ -578,10 +608,9 @@ void Server::OnEntryAddedToStream(const std::string &ns, const std::string &key,
}
void Server::delConnContext(ConnContext *c) {
- auto conn_ctx_iter = conn_ctxs_.find(c);
- if (conn_ctx_iter != conn_ctxs_.end()) {
- delete conn_ctx_iter->first;
- conn_ctxs_.erase(conn_ctx_iter);
+ if (auto iter = conn_ctxs_.find(c); iter != conn_ctxs_.end()) {
+ delete iter->first;
+ conn_ctxs_.erase(iter);
}
}
@@ -614,7 +643,7 @@ std::unique_ptr<RWLock::WriteLock> Server::WorkExclusivityGuard() {
return std::make_unique<RWLock::WriteLock>(works_concurrency_rw_lock_);
}
-std::atomic<uint64_t> *Server::GetClientID() { return &client_id_; }
+uint64_t Server::GetClientID() { return client_id_.fetch_add(1, std::memory_order_relaxed); }
void Server::recordInstantaneousMetrics() {
auto rocksdb_stats = storage_->GetDB()->GetDBOptions().statistics;
@@ -647,12 +676,14 @@ void Server::cron() {
updateCachedTime();
counter++;
+
if (is_loading_) {
// We need to skip the cron operations since `is_loading_` means the db is restoring,
// and the db pointer will be modified after that. It will panic if we use the db pointer
// before the new db was reopened.
continue;
}
+
// check every 20s (use 20s instead of 60s so that cron will execute in critical condition)
if (counter != 0 && counter % 200 == 0) {
auto t = static_cast<time_t>(Util::GetTimeStamp());
@@ -665,7 +696,7 @@ void Server::cron() {
LOG(INFO) << "[server] Schedule to compact the db, result: " << s.Msg();
}
if (config_->bgsave_cron.IsEnabled() && config_->bgsave_cron.IsTimeMatch(&now)) {
- Status s = AsyncBgsaveDB();
+ Status s = AsyncBgSaveDB();
LOG(INFO) << "[server] Schedule to bgsave the db, result: " << s.Msg();
}
}
@@ -706,7 +737,7 @@ void Server::cron() {
// when encountering no space error and disk quota exceeded error.
if (counter != 0 && counter % 600 == 0 && storage_->IsDBInRetryableIOError()) {
storage_->GetDB()->Resume();
- LOG(INFO) << "[server] Schedule to resume DB after retryable io error";
+ LOG(INFO) << "[server] Schedule to resume DB after retryable IO error";
storage_->SetDBInRetryableIOError(false);
}
@@ -721,7 +752,7 @@ void Server::GetRocksDBInfo(std::string *info) {
uint64_t memtable_sizes = 0, cur_memtable_sizes = 0, num_snapshots = 0, num_running_flushes = 0;
uint64_t num_immutable_tables = 0, memtable_flush_pending = 0, compaction_pending = 0;
- uint64_t num_running_compaction = 0, num_live_versions = 0, num_superversion = 0, num_backgroud_errors = 0;
+ uint64_t num_running_compaction = 0, num_live_versions = 0, num_super_version = 0, num_background_errors = 0;
db->GetAggregatedIntProperty("rocksdb.num-snapshots", &num_snapshots);
db->GetAggregatedIntProperty("rocksdb.size-all-mem-tables", &memtable_sizes);
@@ -730,8 +761,8 @@ void Server::GetRocksDBInfo(std::string *info) {
db->GetAggregatedIntProperty("rocksdb.num-immutable-mem-table", &num_immutable_tables);
db->GetAggregatedIntProperty("rocksdb.mem-table-flush-pending", &memtable_flush_pending);
db->GetAggregatedIntProperty("rocksdb.num-running-compactions", &num_running_compaction);
- db->GetAggregatedIntProperty("rocksdb.current-super-version-number", &num_superversion);
- db->GetAggregatedIntProperty("rocksdb.background-errors", &num_backgroud_errors);
+ db->GetAggregatedIntProperty("rocksdb.current-super-version-number", &num_super_version);
+ db->GetAggregatedIntProperty("rocksdb.background-errors", &num_background_errors);
db->GetAggregatedIntProperty("rocksdb.compaction-pending", &compaction_pending);
db->GetAggregatedIntProperty("rocksdb.num-live-versions", &num_live_versions);
@@ -771,8 +802,8 @@ void Server::GetRocksDBInfo(std::string *info) {
string_stream << "compaction_pending:" << compaction_pending << "\r\n";
string_stream << "num_running_compactions:" << num_running_compaction << "\r\n";
string_stream << "num_live_versions:" << num_live_versions << "\r\n";
- string_stream << "num_superversion:" << num_superversion << "\r\n";
- string_stream << "num_background_errors:" << num_backgroud_errors << "\r\n";
+ string_stream << "num_super_version:" << num_super_version << "\r\n";
+ string_stream << "num_background_errors:" << num_background_errors << "\r\n";
string_stream << "flush_count:" << storage_->GetFlushCount() << "\r\n";
string_stream << "compaction_count:" << storage_->GetCompactionCount() << "\r\n";
string_stream << "put_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_PUT) << "\r\n";
@@ -783,8 +814,8 @@ void Server::GetRocksDBInfo(std::string *info) {
string_stream << "seek_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_SEEK) << "\r\n";
string_stream << "next_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_NEXT) << "\r\n";
string_stream << "prev_per_sec:" << stats_.GetInstantaneousMetric(STATS_METRIC_ROCKSDB_PREV) << "\r\n";
- string_stream << "is_bgsaving:" << (is_bgsave_in_progress_ ? "yes" : "no") << "\r\n";
db_job_mu_.lock();
+ string_stream << "is_bgsaving:" << (is_bgsave_in_progress_ ? "yes" : "no") << "\r\n";
string_stream << "is_compacting:" << (db_compacting_ ? "yes" : "no") << "\r\n";
db_job_mu_.unlock();
@@ -792,8 +823,6 @@ void Server::GetRocksDBInfo(std::string *info) {
}
void Server::GetServerInfo(std::string *info) {
- time_t now = 0;
- std::ostringstream string_stream;
static int call_uname = 1;
static utsname name;
if (call_uname) {
@@ -801,7 +830,8 @@ void Server::GetServerInfo(std::string *info) {
uname(&name);
call_uname = 0;
}
- time(&now);
+
+ std::ostringstream string_stream;
string_stream << "# Server\r\n";
string_stream << "version:" << VERSION << "\r\n";
string_stream << "kvrocks_version:" << VERSION << "\r\n";
@@ -819,6 +849,7 @@ void Server::GetServerInfo(std::string *info) {
string_stream << "arch_bits:" << sizeof(void *) * 8 << "\r\n";
string_stream << "process_id:" << getpid() << "\r\n";
string_stream << "tcp_port:" << config_->port << "\r\n";
+ int64_t now = Util::GetTimeStamp();
string_stream << "uptime_in_seconds:" << now - start_time_ << "\r\n";
string_stream << "uptime_in_days:" << (now - start_time_) / 86400 << "\r\n";
*info = string_stream.str();
@@ -835,11 +866,12 @@ void Server::GetClientsInfo(std::string *info) {
}
void Server::GetMemoryInfo(std::string *info) {
- std::ostringstream string_stream;
int64_t rss = Stats::GetMemoryRSS();
int memory_lua = lua_gc(lua_, LUA_GCCOUNT, 0) * 1024;
std::string used_memory_rss_human = Util::BytesToHuman(rss);
std::string used_memory_lua_human = Util::BytesToHuman(memory_lua);
+
+ std::ostringstream string_stream;
string_stream << "# Memory\r\n";
string_stream << "used_memory_rss:" << rss << "\r\n";
string_stream << "used_memory_human:" << used_memory_rss_human << "\r\n";
@@ -869,10 +901,12 @@ void Server::GetReplicationInfo(std::string *info) {
int idx = 0;
rocksdb::SequenceNumber latest_seq = storage_->LatestSeq();
+
slave_threads_mu_.lock();
string_stream << "connected_slaves:" << slave_threads_.size() << "\r\n";
for (const auto &slave : slave_threads_) {
if (slave->IsStopped()) continue;
+
string_stream << "slave" << std::to_string(idx) << ":";
string_stream << "ip=" << slave->GetConn()->GetIP() << ",port=" << slave->GetConn()->GetListeningPort()
<< ",offset=" << slave->GetCurrentReplSeq() << ",lag=" << latest_seq - slave->GetCurrentReplSeq()
@@ -880,6 +914,7 @@ void Server::GetReplicationInfo(std::string *info) {
++idx;
}
slave_threads_mu_.unlock();
+
string_stream << "master_repl_offset:" << latest_seq << "\r\n";
*info = string_stream.str();
@@ -891,6 +926,7 @@ void Server::GetRoleInfo(std::string *info) {
roles.emplace_back("slave");
roles.emplace_back(master_host_);
roles.emplace_back(std::to_string(master_port_));
+
auto state = GetReplicationState();
if (state == kReplConnected) {
roles.emplace_back("connected");
@@ -903,9 +939,11 @@ void Server::GetRoleInfo(std::string *info) {
*info = Redis::MultiBulkString(roles);
} else {
std::vector<std::string> list;
+
slave_threads_mu_.lock();
for (const auto &slave : slave_threads_) {
if (slave->IsStopped()) continue;
+
list.emplace_back(Redis::MultiBulkString({
slave->GetConn()->GetIP(),
std::to_string(slave->GetConn()->GetListeningPort()),
@@ -913,6 +951,7 @@ void Server::GetRoleInfo(std::string *info) {
}));
}
slave_threads_mu_.unlock();
+
auto multi_len = 2;
if (list.size() > 0) {
multi_len = 3;
@@ -932,15 +971,15 @@ std::string Server::GetLastRandomKeyCursor() {
cursor = last_random_key_cursor_;
return cursor;
}
+
void Server::SetLastRandomKeyCursor(const std::string &cursor) {
std::lock_guard<std::mutex> guard(last_random_key_cursor_mu_);
last_random_key_cursor_ = cursor;
}
-int Server::GetUnixTime() {
+int Server::GetCachedUnixTime() {
if (unix_time_.load() == 0) {
- time_t ret = time(nullptr);
- unix_time_.store(static_cast<int>(ret));
+ unix_time_.store(static_cast<int>(Util::GetTimeStamp()));
}
return unix_time_.load();
}
@@ -965,6 +1004,7 @@ void Server::GetStatsInfo(std::string *info) {
string_stream << "pubsub_channels:" << pubsub_channels_.size() << "\r\n";
string_stream << "pubsub_patterns:" << pubsub_patterns_.size() << "\r\n";
}
+
*info = string_stream.str();
}
@@ -974,21 +1014,23 @@ void Server::GetCommandsStatsInfo(std::string *info) {
for (const auto &cmd_stat : stats_.commands_stats) {
auto calls = cmd_stat.second.calls.load();
- auto latency = cmd_stat.second.latency.load();
if (calls == 0) continue;
+
+ auto latency = cmd_stat.second.latency.load();
string_stream << "cmdstat_" << cmd_stat.first << ":calls=" << calls << ",usec=" << latency
<< ",usec_per_call=" << ((calls == 0) ? 0 : static_cast<float>(latency / calls)) << "\r\n";
}
+
*info = string_stream.str();
}
-// WARNING: we must not access DB(i.e.RocksDB) when server is loading since
-// DB is closed and the pointer is invalid. Server may crash if we access DB
-// during loading.
+// WARNING: we must not access DB(i.e. RocksDB) when server is loading since
+// DB is closed and the pointer is invalid. Server may crash if we access DB during loading.
// If you add new fields which access DB into INFO command output, make sure
-// this section cant't be shown when loading(i.e. !is_loading_).
+// this section can't be shown when loading(i.e. !is_loading_).
void Server::GetInfo(const std::string &ns, const std::string §ion, std::string *info) {
info->clear();
+
std::ostringstream string_stream;
bool all = section == "all";
int section_cnt = 0;
@@ -999,18 +1041,21 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str
if (section_cnt++) string_stream << "\r\n";
string_stream << server_info;
}
+
if (all || section == "clients") {
std::string clients_info;
GetClientsInfo(&clients_info);
if (section_cnt++) string_stream << "\r\n";
string_stream << clients_info;
}
+
if (all || section == "memory") {
std::string memory_info;
GetMemoryInfo(&memory_info);
if (section_cnt++) string_stream << "\r\n";
string_stream << memory_info;
}
+
if (all || section == "persistence") {
if (section_cnt++) string_stream << "\r\n";
string_stream << "# Persistence\r\n";
@@ -1022,6 +1067,7 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str
string_stream << "last_bgsave_status:" << last_bgsave_status_ << "\r\n";
string_stream << "last_bgsave_time_sec:" << last_bgsave_time_sec_ << "\r\n";
}
+
if (all || section == "stats") {
std::string stats_info;
GetStatsInfo(&stats_info);
@@ -1036,6 +1082,7 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str
if (section_cnt++) string_stream << "\r\n";
string_stream << replication_info;
}
+
if (all || section == "cpu") {
struct rusage self_ru;
getrusage(RUSAGE_SELF, &self_ru);
@@ -1050,6 +1097,7 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str
static_cast<float>(self_ru.ru_utime.tv_usec / 1000000)
<< "\r\n";
}
+
if (all || section == "commandstats") {
std::string commands_stats_info;
GetCommandsStatsInfo(&commands_stats_info);
@@ -1060,10 +1108,12 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str
// In keyspace section, we access DB, so we can't do that when loading
if (!is_loading_ && (all || section == "keyspace")) {
KeyNumStats stats;
- GetLastestKeyNumStats(ns, &stats);
+ GetLatestKeyNumStats(ns, &stats);
+
time_t last_scan_time = GetLastScanTime(ns);
tm last_scan_tm{};
localtime_r(&last_scan_time, &last_scan_tm);
+
if (section_cnt++) string_stream << "\r\n";
string_stream << "# Keyspace\r\n";
string_stream << "# Last scan db time: " << std::put_time(&last_scan_tm, "%a %b %e %H:%M:%S %Y") << "\r\n";
@@ -1076,6 +1126,7 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str
static_cast<double>(config_->max_db_size * GiB)
: 0;
string_stream << "used_percent: " << used_percent << "%\r\n";
+
struct statvfs stat;
if (statvfs(config_->db_dir.c_str(), &stat) == 0) {
auto disk_capacity = stat.f_blocks * stat.f_frsize;
@@ -1094,6 +1145,7 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str
if (section_cnt++) string_stream << "\r\n";
string_stream << rocksdb_info;
}
+
*info = string_stream.str();
}
@@ -1106,6 +1158,7 @@ std::string Server::GetRocksDBStatsJson() {
for (const auto &iter : rocksdb::TickersNameMap) {
output.append(fmt::format(R"("{}":{},)", iter.second.c_str(), stats->getTickerCount(iter.first)));
}
+
for (const auto &iter : rocksdb::HistogramsNameMap) {
rocksdb::HistogramData hist_data;
stats->histogramData(iter.first, &hist_data);
@@ -1113,52 +1166,51 @@ std::string Server::GetRocksDBStatsJson() {
output.append(fmt::format(R"("{}":[{},{},{},{},{},{}],)", iter.second, hist_data.median, hist_data.percentile95,
hist_data.percentile99, hist_data.max, hist_data.count, hist_data.sum));
}
+
output.pop_back();
output.append("}");
output.shrink_to_fit();
+
return output;
}
-// This function is called by replication thread when finished fetching
-// all files from its master.
+// This function is called by replication thread when finished fetching all files from its master.
// Before restoring the db from backup or checkpoint, we should
-// guarantee other threads don't acess DB and its column families,
-// then close db.
+// guarantee other threads don't access DB and its column families, then close db.
void Server::PrepareRestoreDB() {
- // Stop freeding slaves thread
- LOG(INFO) << "Disconnecting slaves...";
+ // Stop feeding slaves thread
+ LOG(INFO) << "[server] Disconnecting slaves...";
DisconnectSlaves();
// Stop task runner
- LOG(INFO) << "Stopping the task runner and clear task queue...";
+ LOG(INFO) << "[server] Stopping the task runner and clear task queue...";
task_runner_.Stop();
task_runner_.Join();
task_runner_.Purge();
- // If the DB is retored, the object 'db_' will be destroyed, but
+ // If the DB is restored, the object 'db_' will be destroyed, but
// 'db_' will be accessed in data migration task. To avoid wrong
// accessing, data migration task should be stopped before restoring DB
WaitNoMigrateProcessing();
- // To guarantee work theads don't access DB, we should relase 'ExclusivityGuard'
- // ASAP to avoid user can't recieve respones for long time, becasue the following
+ // To guarantee work threads don't access DB, we should release 'ExclusivityGuard'
+ // ASAP to avoid user can't receive responses for long time, because the following
// 'CloseDB' may cost much time to acquire DB mutex.
- LOG(INFO) << "Waiting workers for finishing executing commands...";
+ LOG(INFO) << "[server] Waiting workers for finishing executing commands...";
{
auto exclusivity = WorkExclusivityGuard();
is_loading_ = true;
}
// Cron thread, compaction checker thread, full synchronization thread
- // may always run in the backgroud, we need to close db, so they don't
- // actually work.
- LOG(INFO) << "Waiting for closing DB...";
+ // may always run in the background, we need to close db, so they don't actually work.
+ LOG(INFO) << "[server] Waiting for closing DB...";
storage_->CloseDB();
}
void Server::WaitNoMigrateProcessing() {
if (config_->cluster_enabled) {
- LOG(INFO) << "Waiting until no migration task is running...";
+ LOG(INFO) << "[server] Waiting until no migration task is running...";
slot_migrate_->SetMigrateStopFlag(true);
while (slot_migrate_->GetMigrateStateMachine() != MigrateStateMachine::kSlotMigrateNone) {
usleep(500);
@@ -1168,12 +1220,14 @@ void Server::WaitNoMigrateProcessing() {
Status Server::AsyncCompactDB(const std::string &begin_key, const std::string &end_key) {
if (is_loading_) {
- return Status(Status::NotOK, "loading in-progress");
+ return {Status::NotOK, "loading in-progress"};
}
+
std::lock_guard<std::mutex> lg(db_job_mu_);
if (db_compacting_) {
- return Status(Status::NotOK, "compact in-progress");
+ return {Status::NotOK, "compact in-progress"};
}
+
db_compacting_ = true;
Task task = [begin_key, end_key, this] {
@@ -1181,23 +1235,26 @@ Status Server::AsyncCompactDB(const std::string &begin_key, const std::string &e
if (!begin_key.empty()) begin = std::make_unique<Slice>(begin_key);
if (!end_key.empty()) end = std::make_unique<Slice>(end_key);
storage_->Compact(begin.get(), end.get());
+
std::lock_guard<std::mutex> lg(db_job_mu_);
db_compacting_ = false;
};
+
return task_runner_.Publish(task);
}
-Status Server::AsyncBgsaveDB() {
+Status Server::AsyncBgSaveDB() {
std::lock_guard<std::mutex> lg(db_job_mu_);
if (is_bgsave_in_progress_) {
- return Status(Status::NotOK, "bgsave in-progress");
+ return {Status::NotOK, "bgsave in-progress"};
}
+
is_bgsave_in_progress_ = true;
Task task = [this] {
- auto start_bgsave_time = static_cast<time_t>(Util::GetTimeStamp());
+ auto start_bgsave_time = Util::GetTimeStamp();
Status s = storage_->CreateBackup();
- auto stop_bgsave_time = static_cast<time_t>(Util::GetTimeStamp());
+ auto stop_bgsave_time = Util::GetTimeStamp();
std::lock_guard<std::mutex> lg(db_job_mu_);
is_bgsave_in_progress_ = false;
@@ -1205,6 +1262,7 @@ Status Server::AsyncBgsaveDB() {
last_bgsave_status_ = s.IsOK() ? "ok" : "err";
last_bgsave_time_sec_ = static_cast<int>(stop_bgsave_time - start_bgsave_time);
};
+
return task_runner_.Publish(task);
}
@@ -1217,13 +1275,15 @@ Status Server::AsyncPurgeOldBackups(uint32_t num_backups_to_keep, uint32_t backu
Status Server::AsyncScanDBSize(const std::string &ns) {
std::lock_guard<std::mutex> lg(db_job_mu_);
- auto iter = db_scan_infos_.find(ns);
- if (iter == db_scan_infos_.end()) {
+
+ if (auto iter = db_scan_infos_.find(ns); iter == db_scan_infos_.end()) {
db_scan_infos_[ns] = DBScanInfo{};
}
+
if (db_scan_infos_[ns].is_scanning) {
- return Status(Status::NotOK, "scanning the db now");
+ return {Status::NotOK, "scanning the db now"};
}
+
db_scan_infos_[ns].is_scanning = true;
Task task = [ns, this] {
@@ -1232,10 +1292,12 @@ Status Server::AsyncScanDBSize(const std::string &ns) {
db.GetKeyNumStats("", &stats);
std::lock_guard<std::mutex> lg(db_job_mu_);
+
db_scan_infos_[ns].key_num_stats = stats;
time(&db_scan_infos_[ns].last_scan_time);
db_scan_infos_[ns].is_scanning = false;
};
+
return task_runner_.Publish(task);
}
@@ -1246,9 +1308,11 @@ Status Server::autoResizeBlockAndSST() {
storage_->GetDB()->GetIntProperty(cf_handle, "rocksdb.estimate-num-keys", &estimate_keys);
total_keys += estimate_keys;
}
+
if (total_size == 0 || total_keys == 0) {
return Status::OK();
}
+
auto average_kv_size = total_size / total_keys;
int target_file_size_base = 0;
int block_size = 0;
@@ -1271,10 +1335,12 @@ Status Server::autoResizeBlockAndSST() {
target_file_size_base = 16;
block_size = 2 * KiB;
}
+
if (target_file_size_base == config_->RocksDB.target_file_size_base &&
target_file_size_base == config_->RocksDB.write_buffer_size && block_size == config_->RocksDB.block_size) {
return Status::OK();
}
+
if (target_file_size_base != config_->RocksDB.target_file_size_base) {
auto old_target_file_size_base = config_->RocksDB.target_file_size_base;
auto s = config_->Set(this, "rocksdb.target_file_size_base", std::to_string(target_file_size_base));
@@ -1285,6 +1351,7 @@ Status Server::autoResizeBlockAndSST() {
return s;
}
}
+
if (target_file_size_base != config_->RocksDB.write_buffer_size) {
auto old_write_buffer_size = config_->RocksDB.write_buffer_size;
auto s = config_->Set(this, "rocksdb.write_buffer_size", std::to_string(target_file_size_base));
@@ -1295,6 +1362,7 @@ Status Server::autoResizeBlockAndSST() {
return s;
}
}
+
if (block_size != config_->RocksDB.block_size) {
auto s = storage_->SetColumnFamilyOption("table_factory.block_size", std::to_string(block_size));
LOG(INFO) << "[server] Resize rocksdb.block_size from " << config_->RocksDB.block_size << " to " << block_size
@@ -1303,14 +1371,17 @@ Status Server::autoResizeBlockAndSST() {
if (!s.IsOK()) {
return s;
}
+
config_->RocksDB.block_size = block_size;
}
+
auto s = config_->Rewrite();
- LOG(INFO) << "[server] rewrite config, result: " << s.Msg();
+ LOG(INFO) << "[server] Rewrite config, result: " << s.Msg();
+
return Status::OK();
}
-void Server::GetLastestKeyNumStats(const std::string &ns, KeyNumStats *stats) {
+void Server::GetLatestKeyNumStats(const std::string &ns, KeyNumStats *stats) {
auto iter = db_scan_infos_.find(ns);
if (iter != db_scan_infos_.end()) {
std::lock_guard<std::mutex> lg(db_job_mu_);
@@ -1329,6 +1400,7 @@ time_t Server::GetLastScanTime(const std::string &ns) {
void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint64_t duration) {
int64_t threshold = config_->slowlog_log_slower_than;
if (threshold < 0 || static_cast<int64_t>(duration) < threshold) return;
+
auto entry = std::make_unique<SlowEntry>();
size_t argc = args->size() > kSlowLogMaxArgc ? kSlowLogMaxArgc : args->size();
for (size_t i = 0; i < argc; i++) {
@@ -1336,6 +1408,7 @@ void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint
entry->args.emplace_back(fmt::format("... ({} more arguments)", args->size() - argc + 1));
break;
}
+
if (args->data()[i].length() <= kSlowLogMaxString) {
entry->args.emplace_back(args->data()[i]);
} else {
@@ -1343,6 +1416,7 @@ void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint
args->data()[i].length() - kSlowLogMaxString));
}
}
+
entry->duration = duration;
slow_log_.PushEntry(std::move(entry));
}
@@ -1352,10 +1426,13 @@ std::string Server::GetClientsStr() {
for (const auto &t : worker_threads_) {
clients.append(t->GetWorker()->GetClientsStr());
}
+
std::lock_guard<std::mutex> guard(slave_threads_mu_);
+
for (const auto &st : slave_threads_) {
clients.append(st->GetConn()->ToString());
}
+
return clients;
}
@@ -1386,7 +1463,8 @@ void Server::KillClient(int64_t *killed, const std::string &addr, uint64_t id, u
(type & kTypeMaster || (!addr.empty() && addr == master_host_ + ":" + std::to_string(master_port_)))) {
// Stop replication thread and start a new one to replicate
if (auto s = AddMaster(master_host_, master_port_, true); !s.IsOK()) {
- LOG(ERROR) << "Failed to add master " << master_host_ << ":" << master_port_ << "with error: " << s.Msg();
+ LOG(ERROR) << "[server] Failed to add master " << master_host_ << ":" << master_port_
+ << " with error: " << s.Msg();
}
(*killed)++;
}
@@ -1401,15 +1479,18 @@ ReplState Server::GetReplicationState() {
}
Status Server::LookupAndCreateCommand(const std::string &cmd_name, std::unique_ptr<Redis::Commander> *cmd) {
+ if (cmd_name.empty()) return {Status::RedisUnknownCmd};
+
auto commands = Redis::GetCommands();
- if (cmd_name.empty()) return Status(Status::RedisUnknownCmd);
auto cmd_iter = commands->find(Util::ToLower(cmd_name));
if (cmd_iter == commands->end()) {
- return Status(Status::RedisUnknownCmd);
+ return {Status::RedisUnknownCmd};
}
+
auto redisCmd = cmd_iter->second;
*cmd = redisCmd->factory();
(*cmd)->SetAttributes(redisCmd);
+
return Status::OK();
}
@@ -1419,19 +1500,20 @@ Status Server::ScriptExists(const std::string &sha) {
}
Status Server::ScriptGet(const std::string &sha, std::string *body) {
- std::string funcname = Engine::kLuaFunctionPrefix + sha;
+ std::string func_name = Engine::kLuaFunctionPrefix + sha;
auto cf = storage_->GetCFHandle(Engine::kPropagateColumnFamilyName);
- auto s = storage_->GetDB()->Get(rocksdb::ReadOptions(), cf, funcname, body);
+ auto s = storage_->GetDB()->Get(rocksdb::ReadOptions(), cf, func_name, body);
if (!s.ok()) {
- if (s.IsNotFound()) return Status(Status::NotFound);
- return Status(Status::NotOK, s.ToString());
+ if (s.IsNotFound()) return {Status::NotFound};
+
+ return {Status::NotOK, s.ToString()};
}
return Status::OK();
}
Status Server::ScriptSet(const std::string &sha, const std::string &body) {
- std::string funcname = Engine::kLuaFunctionPrefix + sha;
- return storage_->WriteToPropagateCF(funcname, body);
+ std::string func_name = Engine::kLuaFunctionPrefix + sha;
+ return storage_->WriteToPropagateCF(func_name, body);
}
void Server::ScriptReset() {
@@ -1445,7 +1527,7 @@ void Server::ScriptFlush() {
ScriptReset();
}
-// Generally, we store data into rocksdb and just replicate WAL instead of propagating
+// Generally, we store data into RocksDB and just replicate WAL instead of propagating
// commands. But sometimes, we need to update inner states or do special operations
// for specific commands, such as `script flush`.
// channel: we put the same function commands into one channel to handle uniformly
@@ -1473,11 +1555,12 @@ Status Server::ExecPropagatedCommand(const std::vector<std::string> &tokens) {
if (command == "script" && tokens.size() >= 2) {
return ExecPropagateScriptCommand(tokens);
}
+
return Status::OK();
}
// AdjustOpenFilesLimit only try best to raise the max open files according to
-// the max clients and rocksdb open file configuration. It also reserves a number
+// the max clients and RocksDB open file configuration. It also reserves a number
// of file descriptors(128) for extra operations of persistence, listening sockets,
// log files and so forth.
void Server::AdjustOpenFilesLimit() {
@@ -1490,6 +1573,7 @@ void Server::AdjustOpenFilesLimit() {
if (getrlimit(RLIMIT_NOFILE, &limit) == -1) {
return;
}
+
rlim_t old_limit = limit.rlim_cur;
// Set the max number of files only if the current limit is not enough
if (old_limit >= max_files) {
@@ -1498,10 +1582,12 @@ void Server::AdjustOpenFilesLimit() {
int setrlimit_error = 0;
rlim_t best_limit = max_files;
+
while (best_limit > old_limit) {
limit.rlim_cur = best_limit;
limit.rlim_max = best_limit;
if (setrlimit(RLIMIT_NOFILE, &limit) != -1) break;
+
setrlimit_error = errno;
rlim_t decr_step = 16;
@@ -1509,17 +1595,20 @@ void Server::AdjustOpenFilesLimit() {
best_limit = old_limit;
break;
}
+
best_limit -= decr_step;
}
if (best_limit < old_limit) best_limit = old_limit;
+
if (best_limit < max_files) {
if (best_limit <= static_cast<int>(min_reserved_fds)) {
LOG(WARNING) << "[server] Your current 'ulimit -n' of " << old_limit << " is not enough for the server to start."
<< "Please increase your open file limit to at least " << max_files << ". Exiting.";
exit(1);
}
- LOG(WARNING) << "[server] You requested max clients of " << max_clients << " and rocksdb max open files of "
+
+ LOG(WARNING) << "[server] You requested max clients of " << max_clients << " and RocksDB max open files of "
<< rocksdb_max_open_file << " requiring at least " << max_files << " max file descriptors.";
LOG(WARNING) << "[server] Server can't set maximum open files to " << max_files
<< " because of OS error: " << strerror(setrlimit_error);
@@ -1532,14 +1621,13 @@ void Server::AdjustOpenFilesLimit() {
std::string ServerLogData::Encode() {
if (type_ == kReplIdLog) {
return std::string(1, kReplIdTag) + " " + content_;
- } else {
- return content_;
}
+ return content_;
}
Status ServerLogData::Decode(const rocksdb::Slice &blob) {
if (blob.size() == 0) {
- return Status(Status::NotOK);
+ return {Status::NotOK};
}
const char *header = blob.data();
@@ -1549,5 +1637,6 @@ Status ServerLogData::Decode(const rocksdb::Slice &blob) {
content_ = std::string(blob.data() + 2, blob.size() - 2);
return Status::OK();
}
- return Status(Status::NotOK);
+
+ return {Status::NotOK};
}
diff --git a/src/server/server.h b/src/server/server.h
index f2edc53b..54c79444 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -92,7 +92,7 @@ class ServerLogData {
// distinguish ServerLogData with Redis::WriteBatchLogData.
static const char kReplIdTag = 'r';
static bool IsServerLogData(const char *header) {
- if (header != nullptr) return *header == kReplIdTag;
+ if (header) return *header == kReplIdTag;
return false;
}
@@ -142,16 +142,16 @@ class Server {
int PublishMessage(const std::string &channel, const std::string &msg);
void SubscribeChannel(const std::string &channel, Redis::Connection *conn);
- void UnSubscribeChannel(const std::string &channel, Redis::Connection *conn);
+ void UnsubscribeChannel(const std::string &channel, Redis::Connection *conn);
void GetChannelsByPattern(const std::string &pattern, std::vector<std::string> *channels);
void ListChannelSubscribeNum(const std::vector<std::string> &channels,
std::vector<ChannelSubscribeNum> *channel_subscribe_nums);
void PSubscribeChannel(const std::string &pattern, Redis::Connection *conn);
- void PUnSubscribeChannel(const std::string &pattern, Redis::Connection *conn);
+ void PUnsubscribeChannel(const std::string &pattern, Redis::Connection *conn);
int GetPubSubPatternSize() { return static_cast<int>(pubsub_patterns_.size()); }
- void AddBlockingKey(const std::string &key, Redis::Connection *conn);
- void UnBlockingKey(const std::string &key, Redis::Connection *conn);
+ void BlockOnKey(const std::string &key, Redis::Connection *conn);
+ void UnblockOnKey(const std::string &key, Redis::Connection *conn);
void BlockOnStreams(const std::vector<std::string> &keys, const std::vector<Redis::StreamEntryID> &entry_ids,
Redis::Connection *conn);
void UnblockOnStreams(const std::vector<std::string> &keys, Redis::Connection *conn);
@@ -161,7 +161,7 @@ class Server {
std::string GetLastRandomKeyCursor();
void SetLastRandomKeyCursor(const std::string &cursor);
- static int GetUnixTime();
+ static int GetCachedUnixTime();
void GetStatsInfo(std::string *info);
void GetServerInfo(std::string *info);
void GetMemoryInfo(std::string *info);
@@ -177,10 +177,10 @@ class Server {
void PrepareRestoreDB();
void WaitNoMigrateProcessing();
Status AsyncCompactDB(const std::string &begin_key = "", const std::string &end_key = "");
- Status AsyncBgsaveDB();
+ Status AsyncBgSaveDB();
Status AsyncPurgeOldBackups(uint32_t num_backups_to_keep, uint32_t backup_max_keep_hours);
Status AsyncScanDBSize(const std::string &ns);
- void GetLastestKeyNumStats(const std::string &ns, KeyNumStats *stats);
+ void GetLatestKeyNumStats(const std::string &ns, KeyNumStats *stats);
time_t GetLastScanTime(const std::string &ns);
int DecrClientNum();
@@ -190,7 +190,7 @@ class Server {
int IncrBlockedClientNum();
int DecrBlockedClientNum();
std::string GetClientsStr();
- std::atomic<uint64_t> *GetClientID();
+ uint64_t GetClientID();
void KillClient(int64_t *killed, const std::string &addr, uint64_t id, uint64_t type, bool skipme,
Redis::Connection *conn);
@@ -233,9 +233,9 @@ class Server {
void updateCachedTime();
Status autoResizeBlockAndSST();
- std::atomic<bool> stop_;
- std::atomic<bool> is_loading_;
- time_t start_time_ = 0;
+ std::atomic<bool> stop_ = false;
+ std::atomic<bool> is_loading_ = false;
+ int64_t start_time_;
std::mutex slaveof_mu_;
std::string master_host_;
uint32_t master_port_ = 0;
@@ -252,12 +252,11 @@ class Server {
std::atomic<int> connected_clients_{0};
std::atomic<int> monitor_clients_{0};
std::atomic<uint64_t> total_clients_{0};
- std::atomic<int> excuting_command_num_{0};
// slave
std::mutex slave_threads_mu_;
std::list<std::unique_ptr<FeedSlaveThread>> slave_threads_;
- std::atomic<int> fetch_file_threads_num_;
+ std::atomic<int> fetch_file_threads_num_ = 0;
// Some jobs to operate DB should be unique
std::mutex db_job_mu_;
diff --git a/src/server/worker.cc b/src/server/worker.cc
index 5fb2f519..0d408f19 100644
--- a/src/server/worker.cc
+++ b/src/server/worker.cc
@@ -43,8 +43,6 @@
#include <sys/un.h>
#include <algorithm>
-#include <cctype>
-#include <cstring>
#include <list>
#include <utility>
@@ -53,7 +51,7 @@
#include "server.h"
#include "storage/scripting.h"
-Worker::Worker(Server *svr, Config *config, bool repl) : svr_(svr), base_(event_base_new()) {
+Worker::Worker(Server *svr, Config *config) : svr_(svr), base_(event_base_new()) {
if (!base_) throw std::runtime_error{"event base failed to be created"};
timer_ = event_new(base_, -1, EV_PERSIST, TimerCB, this);
@@ -67,7 +65,7 @@ Worker::Worker(Server *svr, Config *config, bool repl) : svr_(svr), base_(event_
for (const auto &bind : binds) {
Status s = listenTCP(bind, *port, config->backlog);
if (!s.IsOK()) {
- LOG(ERROR) << "[worker] Failed to listen on: " << bind << ":" << *port << ", encounter error: " << s.Msg();
+ LOG(ERROR) << "[worker] Failed to listen on: " << bind << ":" << *port << ". Error: " << s.Msg();
exit(1);
}
LOG(INFO) << "[worker] Listening on: " << bind << ":" << *port;
@@ -88,10 +86,10 @@ Worker::~Worker() {
iter->Close();
}
event_free(timer_);
- if (rate_limit_group_ != nullptr) {
+ if (rate_limit_group_) {
bufferevent_rate_limit_group_free(rate_limit_group_);
}
- if (rate_limit_group_cfg_ != nullptr) {
+ if (rate_limit_group_cfg_) {
ev_token_bucket_cfg_free(rate_limit_group_cfg_);
}
event_base_free(base_);
@@ -109,18 +107,21 @@ void Worker::newTCPConnection(evconnlistener *listener, evutil_socket_t fd, sock
auto worker = static_cast<Worker *>(ctx);
int local_port = Util::GetLocalPort(fd); // NOLINT
DLOG(INFO) << "[worker] New connection: fd=" << fd << " from port: " << local_port << " thread #" << worker->tid_;
+
auto s = Util::SockSetTcpKeepalive(fd, 120);
if (!s.IsOK()) {
- LOG(ERROR) << "[worker] Failed to set tcp-keepalive, err:" << s.Msg();
+ LOG(ERROR) << "[worker] Failed to set tcp-keepalive on socket. Error: " << s.Msg();
evutil_closesocket(fd);
return;
}
+
s = Util::SockSetTcpNoDelay(fd, 1);
if (!s.IsOK()) {
- LOG(ERROR) << "[worker] Failed to set tcp-nodelay, err:" << s.Msg();
+ LOG(ERROR) << "[worker] Failed to set tcp-nodelay on socket. Error: " << s.Msg();
evutil_closesocket(fd);
return;
}
+
event_base *base = evconnlistener_get_base(listener);
auto evThreadSafeFlags =
BEV_OPT_THREADSAFE | BEV_OPT_DEFER_CALLBACKS | BEV_OPT_UNLOCK_CALLBACKS | BEV_OPT_CLOSE_ON_FREE;
@@ -161,6 +162,7 @@ void Worker::newTCPConnection(evconnlistener *listener, evutil_socket_t fd, sock
auto conn = new Redis::Connection(bev, worker);
bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite, Redis::Connection::OnEvent, conn);
bufferevent_enable(bev, EV_READ);
+
s = worker->AddConnection(conn);
if (!s.IsOK()) {
std::string err_msg = Redis::Error("ERR " + s.Msg());
@@ -171,12 +173,14 @@ void Worker::newTCPConnection(evconnlistener *listener, evutil_socket_t fd, sock
conn->Close();
return;
}
+
std::string ip;
uint32_t port = 0;
if (Util::GetPeerAddr(fd, &ip, &port) == 0) {
conn->SetAddr(ip, port);
}
- if (worker->rate_limit_group_ != nullptr) {
+
+ if (worker->rate_limit_group_) {
bufferevent_add_to_rate_limit_group(bev, worker->rate_limit_group_);
}
}
@@ -190,9 +194,11 @@ void Worker::newUnixSocketConnection(evconnlistener *listener, evutil_socket_t f
auto evThreadSafeFlags =
BEV_OPT_THREADSAFE | BEV_OPT_DEFER_CALLBACKS | BEV_OPT_UNLOCK_CALLBACKS | BEV_OPT_CLOSE_ON_FREE;
bufferevent *bev = bufferevent_socket_new(base, fd, evThreadSafeFlags);
+
auto conn = new Redis::Connection(bev, worker);
bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite, Redis::Connection::OnEvent, conn);
bufferevent_enable(bev, EV_READ);
+
auto s = worker->AddConnection(conn);
if (!s.IsOK()) {
std::string err_msg = Redis::Error("ERR " + s.Msg());
@@ -203,45 +209,49 @@ void Worker::newUnixSocketConnection(evconnlistener *listener, evutil_socket_t f
conn->Close();
return;
}
+
conn->SetAddr(worker->svr_->GetConfig()->unixsocket, 0);
- if (worker->rate_limit_group_ != nullptr) {
+ if (worker->rate_limit_group_) {
bufferevent_add_to_rate_limit_group(bev, worker->rate_limit_group_);
}
}
Status Worker::listenTCP(const std::string &host, uint32_t port, int backlog) {
- int af = 0, rv = 0, fd = 0, sock_opt = 1;
+ bool ipv6_used = strchr(host.data(), ':');
- if (strchr(host.data(), ':')) {
- af = AF_INET6;
- } else {
- af = AF_INET;
- }
- struct addrinfo hints, *srv_info = nullptr, *p = nullptr;
+ struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
- hints.ai_family = af;
+ hints.ai_family = ipv6_used ? AF_INET6 : AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
- if ((rv = getaddrinfo(host.data(), std::to_string(port).c_str(), &hints, &srv_info)) != 0) {
- return Status(Status::NotOK, gai_strerror(rv));
+ struct addrinfo *srv_info = nullptr;
+ if (int rv = getaddrinfo(host.data(), std::to_string(port).c_str(), &hints, &srv_info); rv != 0) {
+ return {Status::NotOK, gai_strerror(rv)};
}
- for (p = srv_info; p != nullptr; p = p->ai_next) {
- if ((fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) continue;
- if (af == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &sock_opt, sizeof(sock_opt)) == -1) {
+ for (auto p = srv_info; p != nullptr; p = p->ai_next) {
+ int fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+ if (fd == -1) continue;
+
+ int sock_opt = 1;
+ if (ipv6_used && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &sock_opt, sizeof(sock_opt)) == -1) {
goto error;
}
+
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &sock_opt, sizeof(sock_opt)) < 0) {
goto error;
}
+
// to support multi-thread binding on macOS
if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &sock_opt, sizeof(sock_opt)) < 0) {
goto error;
}
+
if (bind(fd, p->ai_addr, p->ai_addrlen)) {
goto error;
}
+
evutil_make_socket_nonblocking(fd);
auto lev = evconnlistener_new(base_, newTCPConnection, this, LEV_OPT_CLOSE_ON_FREE, backlog, fd);
listen_events_.emplace_back(lev);
@@ -252,30 +262,35 @@ Status Worker::listenTCP(const std::string &host, uint32_t port, int backlog) {
error:
freeaddrinfo(srv_info);
- return Status(Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
+
+ return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}
Status Worker::ListenUnixSocket(const std::string &path, int perm, int backlog) {
unlink(path.c_str());
sockaddr_un sa{};
if (path.size() > sizeof(sa.sun_path) - 1) {
- return Status(Status::NotOK, "unix socket path too long");
+ return {Status::NotOK, "unix socket path too long"};
}
+
sa.sun_family = AF_LOCAL;
strncpy(sa.sun_path, path.c_str(), sizeof(sa.sun_path) - 1);
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd == -1) {
- return Status(Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
+ return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}
+
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
- return Status(Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR()));
+ return {Status::NotOK, evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())};
}
+
evutil_make_socket_nonblocking(fd);
auto lev = evconnlistener_new(base_, newUnixSocketConnection, this, LEV_OPT_CLOSE_ON_FREE, backlog, fd);
listen_events_.emplace_back(lev);
if (perm != 0) {
chmod(sa.sun_path, (mode_t)perm);
}
+
return Status::OK();
}
@@ -298,21 +313,25 @@ Status Worker::AddConnection(Redis::Connection *c) {
std::unique_lock<std::mutex> lock(conns_mu_);
auto iter = conns_.find(c->GetFD());
if (iter != conns_.end()) {
- return Status(Status::NotOK, "connection was exists");
+ return {Status::NotOK, "connection was exists"};
}
+
int max_clients = svr_->GetConfig()->maxclients;
if (svr_->IncrClientNum() >= max_clients) {
svr_->DecrClientNum();
- return Status(Status::NotOK, "max number of clients reached");
+ return {Status::NotOK, "max number of clients reached"};
}
+
conns_.insert(std::pair<int, Redis::Connection *>(c->GetFD(), c));
- uint64_t id = svr_->GetClientID()->fetch_add(1, std::memory_order_relaxed);
+ uint64_t id = svr_->GetClientID();
c->SetID(id);
+
return Status::OK();
}
Redis::Connection *Worker::removeConnection(int fd) {
Redis::Connection *conn = nullptr;
+
std::unique_lock<std::mutex> lock(conns_mu_);
auto iter = conns_.find(fd);
if (iter != conns_.end()) {
@@ -320,6 +339,7 @@ Redis::Connection *Worker::removeConnection(int fd) {
conns_.erase(iter);
svr_->DecrClientNum();
}
+
iter = monitor_conns_.find(fd);
if (iter != monitor_conns_.end()) {
conn = iter->second;
@@ -327,15 +347,19 @@ Redis::Connection *Worker::removeConnection(int fd) {
svr_->DecrClientNum();
svr_->DecrMonitorClientNum();
}
+
return conn;
}
void Worker::DetachConnection(Redis::Connection *conn) {
if (!conn) return;
+
removeConnection(conn->GetFD());
- if (rate_limit_group_ != nullptr) {
+
+ if (rate_limit_group_) {
bufferevent_remove_from_rate_limit_group(conn->GetBufferEvent());
}
+
auto bev = conn->GetBufferEvent();
bufferevent_disable(bev, EV_READ | EV_WRITE);
bufferevent_setcb(bev, nullptr, nullptr, nullptr, nullptr);
@@ -343,8 +367,10 @@ void Worker::DetachConnection(Redis::Connection *conn) {
void Worker::FreeConnection(Redis::Connection *conn) {
if (!conn) return;
+
removeConnection(conn->GetFD());
- if (rate_limit_group_ != nullptr) {
+
+ if (rate_limit_group_) {
bufferevent_remove_from_rate_limit_group(conn->GetBufferEvent());
}
delete conn;
@@ -352,19 +378,20 @@ void Worker::FreeConnection(Redis::Connection *conn) {
void Worker::FreeConnectionByID(int fd, uint64_t id) {
std::unique_lock<std::mutex> lock(conns_mu_);
- auto conn_iter = conns_.find(fd);
- if (conn_iter != conns_.end() && conn_iter->second->GetID() == id) {
+ auto iter = conns_.find(fd);
+ if (iter != conns_.end() && iter->second->GetID() == id) {
if (rate_limit_group_ != nullptr) {
- bufferevent_remove_from_rate_limit_group(conn_iter->second->GetBufferEvent());
+ bufferevent_remove_from_rate_limit_group(iter->second->GetBufferEvent());
}
- delete conn_iter->second;
- conns_.erase(conn_iter);
+ delete iter->second;
+ conns_.erase(iter);
svr_->DecrClientNum();
}
- auto monitor_conn_iter = monitor_conns_.find(fd);
- if (monitor_conn_iter != monitor_conns_.end() && monitor_conn_iter->second->GetID() == id) {
- delete monitor_conn_iter->second;
- monitor_conns_.erase(monitor_conn_iter);
+
+ iter = monitor_conns_.find(fd);
+ if (iter != monitor_conns_.end() && iter->second->GetID() == id) {
+ delete iter->second;
+ monitor_conns_.erase(iter);
svr_->DecrClientNum();
svr_->DecrMonitorClientNum();
}
@@ -378,7 +405,8 @@ Status Worker::EnableWriteEvent(int fd) {
bufferevent_enable(bev, EV_WRITE);
return Status::OK();
}
- return Status(Status::NotOK);
+
+ return {Status::NotOK, "connection doesn't exist"};
}
Status Worker::Reply(int fd, const std::string &reply) {
@@ -389,7 +417,8 @@ Status Worker::Reply(int fd, const std::string &reply) {
Redis::Reply(iter->second->Output(), reply);
return Status::OK();
}
- return Status(Status::NotOK, "connection doesn't exist");
+
+ return {Status::NotOK, "connection doesn't exist"};
}
void Worker::BecomeMonitorConn(Redis::Connection *conn) {
@@ -404,15 +433,19 @@ void Worker::BecomeMonitorConn(Redis::Connection *conn) {
void Worker::FeedMonitorConns(Redis::Connection *conn, const std::vector<std::string> &tokens) {
auto now = Util::GetTimeStampUS();
+
std::string output;
output += std::to_string(now / 1000000) + "." + std::to_string(now % 1000000);
output += " [" + conn->GetNamespace() + " " + conn->GetAddr() + "]";
for (const auto &token : tokens) {
output += " \"" + token + "\"";
}
+
std::unique_lock<std::mutex> lock(conns_mu_);
+
for (const auto &iter : monitor_conns_) {
if (conn == iter.second) continue; // skip the monitor command
+
if (conn->GetNamespace() == iter.second->GetNamespace() || iter.second->GetNamespace() == kDefaultNamespace) {
iter.second->Reply(Redis::SimpleString(output));
}
@@ -421,24 +454,29 @@ void Worker::FeedMonitorConns(Redis::Connection *conn, const std::vector<std::st
std::string Worker::GetClientsStr() {
std::unique_lock<std::mutex> lock(conns_mu_);
+
std::string output;
for (const auto &iter : conns_) {
Redis::Connection *conn = iter.second;
output.append(conn->ToString());
}
+
return output;
}
void Worker::KillClient(Redis::Connection *self, uint64_t id, const std::string &addr, uint64_t type, bool skipme,
int64_t *killed) {
std::lock_guard<std::mutex> guard(conns_mu_);
+
for (const auto &iter : conns_) {
Redis::Connection *conn = iter.second;
if (skipme && self == conn) continue;
- // no need to kill the client again if flags as kCloseAfterReply
+
+ // no need to kill the client again if the kCloseAfterReply flag is set
if (conn->IsFlagEnabled(Redis::Connection::kCloseAfterReply)) {
continue;
}
+
if ((type & conn->GetClientType()) || (!addr.empty() && conn->GetAddr() == addr) ||
(id != 0 && conn->GetID() == id)) {
conn->EnableFlag(Redis::Connection::kCloseAfterReply);
@@ -454,11 +492,13 @@ void Worker::KillClient(Redis::Connection *self, uint64_t id, const std::string
void Worker::KickoutIdleClients(int timeout) {
std::list<std::pair<int, uint64_t>> to_be_killed_conns;
+
{
std::lock_guard<std::mutex> guard(conns_mu_);
if (conns_.empty()) {
return;
}
+
int iterations = std::min(static_cast<int>(conns_.size()), 50);
auto iter = conns_.upper_bound(last_iter_conn_fd);
while (iterations--) {
@@ -471,6 +511,7 @@ void Worker::KickoutIdleClients(int timeout) {
iter--;
last_iter_conn_fd = iter->first;
}
+
for (const auto &conn : to_be_killed_conns) {
FreeConnectionByID(conn.first, conn.second);
}
diff --git a/src/server/worker.h b/src/server/worker.h
index 41d248b2..ef1bf0f5 100644
--- a/src/server/worker.h
+++ b/src/server/worker.h
@@ -43,7 +43,7 @@ class Server;
class Worker {
public:
- Worker(Server *svr, Config *config, bool repl = false);
+ Worker(Server *svr, Config *config);
~Worker();
Worker(const Worker &) = delete;
Worker(Worker &&) = delete;
diff --git a/src/storage/table_properties_collector.cc b/src/storage/table_properties_collector.cc
index 9c7b5f3a..99418be1 100644
--- a/src/storage/table_properties_collector.cc
+++ b/src/storage/table_properties_collector.cc
@@ -62,7 +62,7 @@ rocksdb::Status CompactOnExpiredCollector::AddUserKey(const rocksdb::Slice &key,
GetFixed32(&cv, &subkeys);
}
total_keys_ += subkeys;
- now = Server::GetUnixTime();
+ now = Server::GetCachedUnixTime();
if ((expired > 0 && expired < static_cast<uint32_t>(now)) || (type != kRedisString && subkeys == 0)) {
deleted_keys_ += subkeys + 1;
}