1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00

Add support for memcached stats command

This commit is contained in:
Roman Gershman 2022-02-22 00:17:01 +02:00
parent b6857e3f41
commit 667b2fa99c
5 changed files with 52 additions and 5 deletions

View file

@ -494,7 +494,11 @@ void Service::DispatchMC(const MemcacheParser::Command& cmd, std::string_view va
case MemcacheParser::GET:
strcpy(cmd_name, "MGET");
break;
case MemcacheParser::STATS:
server_family_.StatsMC(cmd.key, cntx);
return;
default:
mc_builder->SendClientError("bad command line format");
return;
}

View file

@ -92,7 +92,7 @@ auto MemcacheParser::Parse(string_view str, uint32_t* consumed, Command* cmd) ->
return str.size() > 300 ? PARSE_ERROR : INPUT_PENDING;
}
if (pos == 0 || str[pos - 1] != '\r') {
if (pos == 0) {
return PARSE_ERROR;
}
*consumed = pos + 1;
@ -107,8 +107,8 @@ auto MemcacheParser::Parse(string_view str, uint32_t* consumed, Command* cmd) ->
++cur;
uint32_t s = cur;
for (; cur < pos; ++cur) {
if (str[cur] == ' ' || str[cur] == '\r') {
for (; cur <= pos; ++cur) {
if (absl::ascii_isspace(str[cur])) {
if (cur != s) {
tokens[num_tokens++] = str.substr(s, cur - s);
if (num_tokens == ABSL_ARRAYSIZE(tokens)) {

View file

@ -27,7 +27,7 @@ class ReplyBuilderInterface {
struct ResponseValue {
std::string_view key;
std::string value;
uint64_t mc_ver = 0; // 0 means we do not output it (i.e has not been requested).
uint64_t mc_ver = 0; // 0 means we do not output it (i.e has not been requested).
uint32_t mc_flag = 0;
};
@ -72,10 +72,10 @@ class SinkReplyBuilder : public ReplyBuilderInterface {
io_write_bytes_ = 0;
}
protected:
//! Sends a string as is without any formatting. raw should be encoded according to the protocol.
void SendDirect(std::string_view str);
protected:
void Send(const iovec* v, uint32_t len);
std::string batch_;

View file

@ -8,6 +8,7 @@
#include <absl/random/random.h> // for master_id_ generation.
#include <absl/strings/match.h>
#include <malloc.h>
#include <sys/resource.h>
#include <filesystem>
@ -96,6 +97,46 @@ void ServerFamily::Shutdown() {
});
}
void ServerFamily::StatsMC(std::string_view section, ConnectionContext* cntx) {
if (!section.empty()) {
return cntx->reply_builder()->SendError("");
}
string info;
#define ADD_LINE(name, val) absl::StrAppend(&info, "STAT " #name " ", val, "\r\n")
time_t now = time(NULL);
size_t uptime = now - start_time_;
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
auto dbl_time = [](const timeval& tv) -> double {
return tv.tv_sec + double(tv.tv_usec) / 1000000.0;
};
double utime = dbl_time(ru.ru_utime);
double systime = dbl_time(ru.ru_stime);
Metrics m = GetMetrics();
ADD_LINE(pid, getpid());
ADD_LINE(uptime, uptime);
ADD_LINE(time, now);
ADD_LINE(version, "1.6.12");
ADD_LINE(libevent, "iouring");
ADD_LINE(pointer_size, sizeof(void*));
ADD_LINE(rusage_user, utime);
ADD_LINE(rusage_system, systime);
ADD_LINE(max_connections, -1);
ADD_LINE(curr_connections, m.conn_stats.num_conns);
ADD_LINE(total_connections, -1);
ADD_LINE(rejected_connections, -1);
absl::StrAppend(&info, "END\r\n");
MCReplyBuilder* builder = static_cast<MCReplyBuilder*>(cntx->reply_builder());
builder->SendDirect(info);
}
void ServerFamily::DbSize(CmdArgList args, ConnectionContext* cntx) {
atomic_ulong num_keys{0};

View file

@ -48,6 +48,8 @@ class ServerFamily {
return script_mgr_.get();
}
void StatsMC(std::string_view section, ConnectionContext* cntx);
private:
uint32_t shard_count() const {
return ess_.size();