Skip to content

Commit

Permalink
fix error and add golang test
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYuHui committed Sep 6, 2023
1 parent 877019d commit 16a656a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/server/redis_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ class Connection : public EvbufCallbackBase<Connection> {

uint64_t GetID() const { return id_; }
void SetID(uint64_t id) { id_ = id; }
std::string GetName() { return name_; }
std::string GetName() const { return name_; }
void SetName(std::string name) { name_ = std::move(name); }
std::string GetAddr() { return addr_; }
void SetAddr(std::string ip, uint32_t port);
void SetLastCmd(std::string cmd) { last_cmd_ = std::move(cmd); }
std::string GetIP() { return ip_; }
std::string GetIP() const { return ip_; }
uint32_t GetPort() const { return port_; }
void SetListeningPort(int port) { listening_port_ = port; }
int GetListeningPort() const { return listening_port_; }
Expand Down
7 changes: 4 additions & 3 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,8 @@ time_t Server::GetLastScanTime(const std::string &ns) {
return 0;
}

void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint64_t duration, const Connection *conn) {
void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint64_t duration,
const redis::Connection *conn) {
int64_t threshold = config_->slowlog_log_slower_than;
if (threshold < 0 || static_cast<int64_t>(duration) < threshold) return;

Expand All @@ -1428,8 +1429,8 @@ void Server::SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint
}

entry->duration = duration;
entry->client_name = std::move(conn->GetName());
entry->ip = std::move(conn->GetIP());
entry->client_name = conn->GetName();
entry->ip = conn->GetIP();
entry->port = conn->GetPort();
slow_log_.PushEntry(std::move(entry));
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class Server {

LogCollector<PerfEntry> *GetPerfLog() { return &perf_log_; }
LogCollector<SlowEntry> *GetSlowLog() { return &slow_log_; }
void SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint64_t duration, const Connection *conn);
void SlowlogPushEntryIfNeeded(const std::vector<std::string> *args, uint64_t duration, const redis::Connection *conn);

std::shared_lock<std::shared_mutex> WorkConcurrencyGuard();
std::unique_lock<std::shared_mutex> WorkExclusivityGuard();
Expand Down
11 changes: 11 additions & 0 deletions tests/gocase/unit/slowlog/slowlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,15 @@ func TestSlowlog(t *testing.T) {
require.EqualValues(t, 0, len(rdb.SlowLogGet(ctx, -1).Val()))
})

t.Run("SLOWLOG - slowlog get output client name and ipport", func(t *testing.T) {
require.NoError(t, rdb.ConfigSet(ctx, "slowlog-log-slower-than", "0").Err())
require.NoError(t, rdb.Do(ctx, "client", "setname", "foobar").Err())
require.NoError(t, rdb.SAdd(ctx, "set", "foo", "bar").Err())

val, err := rdb.SlowLogGet(ctx, -1).Result()
require.NoError(t, err)
require.EqualValues(t, "foobar" , val[0].ClientName)
require.NotEmpty(t, val[0].ClientAddr)
})

}

0 comments on commit 16a656a

Please sign in to comment.