Skip to content

Commit

Permalink
Modified some log levels of redisSender and pikaSender,
Browse files Browse the repository at this point in the history
Updated the logic of connecting to Redis in redisSender and pikaSender
  • Loading branch information
pro-spild committed Feb 12, 2025
1 parent e744786 commit 014bc67
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 141 deletions.
4 changes: 2 additions & 2 deletions include/pika_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class PikaSender : public net::Thread {
void SendCommand(std::string &command, const std::string &key);
int QueueSize();
void ConnectRedis();

bool Authenticate();
private:
net::NetCli *cli_;
std::shared_ptr<net::NetCli> cli_;
pstd::CondVar wsignal_;
pstd::CondVar rsignal_;
std::mutex signal_mutex;
Expand Down
1 change: 1 addition & 0 deletions include/redis_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RedisSender : public net::Thread {
std::lock_guard l(keys_mutex_);
return commands_queue_.size();
}
bool Authenticate();

private:
int id_;
Expand Down
129 changes: 57 additions & 72 deletions src/pika_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
#include <glog/logging.h>

PikaSender::PikaSender(std::string ip, int64_t port, std::string password):
cli_(NULL),
ip_(ip),
port_(port),
password_(password),
should_exit_(false),
elements_(0)
{
cli_ = std::shared_ptr<net::NetCli>(net::NewRedisCli());
cli_->set_connect_timeout(1000);
cli_->set_send_timeout(10000);
cli_->set_recv_timeout(10000);
ConnectRedis();
}

PikaSender::~PikaSender() {
Expand All @@ -29,81 +33,70 @@ void PikaSender::Stop() {
should_exit_.store(true);
wsignal_.notify_all();
rsignal_.notify_all();
LOG(INFO) << "PikaSender received stop signal";
}

void PikaSender::ConnectRedis() {
while (cli_ == NULL) {
// Connect to redis
cli_ = net::NewRedisCli();
cli_->set_connect_timeout(1000);

while (true) {
pstd::Status s = cli_->Connect(ip_, port_);
if (!s.ok()) {
delete cli_;
cli_ = NULL;
LOG(WARNING) << "Can not connect to " << ip_ << ":" << port_ << ", status: " << s.ToString();
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
} else {
// Connect success
if (!Authenticate()) {
cli_->Close();
std::this_thread::sleep_for(std::chrono::seconds(1));
continue;
}
break;
}
}
}

bool PikaSender::Authenticate() {
if (!password_.empty()) {
net::RedisCmdArgsType argv, resp;
std::string cmd;

// Authentication
if (!password_.empty()) {
net::RedisCmdArgsType argv, resp;
std::string cmd;

argv.push_back("AUTH");
argv.push_back(password_);
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (resp[0] == "OK") {
} else {
LOG(FATAL) << "Connect to redis(" << ip_ << ":" << port_ << ") Invalid password";
cli_->Close();
delete cli_;
cli_ = NULL;
should_exit_ = true;
return;
}
} else {
LOG(WARNING) << "send auth failed: " << s.ToString();
cli_->Close();
delete cli_;
cli_ = NULL;
continue;
}
argv.push_back("AUTH");
argv.push_back(password_);
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (resp[0] == "OK") {
return true;
} else {
// If forget to input password
net::RedisCmdArgsType argv, resp;
std::string cmd;

argv.push_back("PING");
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (s.ok()) {
if (resp[0] == "NOAUTH Authentication required.") {
LOG(FATAL) << "Ping redis(" << ip_ << ":" << port_ << ") NOAUTH Authentication required";
cli_->Close();
delete cli_;
cli_ = NULL;
should_exit_ = true;
return;
}
} else {
LOG(WARNING) << "Recv failed: " << s.ToString();
cli_->Close();
delete cli_;
cli_ = NULL;
}
}
LOG(ERROR) << "Connect to redis(" << ip_ << ":" << port_ << ") Invalid password";
return false;
}
} else {
LOG(WARNING) << "send auth failed: " << s.ToString();
return false;
}
} else {
net::RedisCmdArgsType argv, resp;
std::string cmd;

argv.push_back("PING");
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (s.ok() && resp[0] == "NOAUTH Authentication required.") {
LOG(ERROR) << "Ping redis(" << ip_ << ":" << port_ << ") NOAUTH Authentication required";
return false;
}
} else {
LOG(WARNING) << "Recv failed: " << s.ToString();
return false;
}
}
return true;
}

void PikaSender::LoadKey(const std::string &key) {
Expand All @@ -123,9 +116,7 @@ void PikaSender::SendCommand(std::string &command, const std::string &key) {
elements_--;
LoadKey(key);
cli_->Close();
LOG(INFO) << s.ToString().data();
delete cli_;
cli_ = NULL;
LOG(WARNING) << s.ToString();
ConnectRedis();
}else{
cli_->Recv(NULL);
Expand Down Expand Up @@ -158,15 +149,9 @@ void *PikaSender::ThreadMain() {
}
wsignal_.notify_one();
SendCommand(key, key);

}


if (cli_) {
cli_->Close();
delete cli_;
cli_ = NULL;
}
cli_->Close();
LOG(INFO) << "PikaSender thread complete";
return NULL;
}
Expand Down
123 changes: 56 additions & 67 deletions src/redis_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <glog/logging.h>

static time_t kCheckDiff = 1;
static time_t kCheckDiff = 5;

RedisSender::RedisSender(int id, std::string ip, int64_t port, std::string password):
id_(id),
Expand All @@ -24,82 +24,75 @@ RedisSender::RedisSender(int id, std::string ip, int64_t port, std::string passw
elements_(0) {

last_write_time_ = ::time(NULL);
cli_ = std::shared_ptr<net::NetCli>(net::NewRedisCli());
cli_->set_connect_timeout(1000);
cli_->set_recv_timeout(10000);
cli_->set_send_timeout(10000);
}

RedisSender::~RedisSender() {
LOG(INFO) << "RedisSender thread " << id_ << " exit!!!";
}

void RedisSender::ConnectRedis() {
while (cli_ == NULL) {
// Connect to redis
cli_ = std::shared_ptr<net::NetCli>(net::NewRedisCli());
cli_->set_connect_timeout(1000);
cli_->set_recv_timeout(10000);
cli_->set_send_timeout(10000);
while (true) {
pstd::Status s = cli_->Connect(ip_, port_);
if (!s.ok()) {
LOG(WARNING) << "Can not connect to " << ip_ << ":" << port_ << ", status: " << s.ToString();
cli_ = NULL;
sleep(3);
continue;
} else {
// Connect success
LOG(INFO) << "RedisSender thread " << id_ << "Connect to redis(" << ip_ << ":" << port_ << ") success";
// Authentication
if (!password_.empty()) {
net::RedisCmdArgsType argv, resp;
std::string cmd;

argv.push_back("AUTH");
argv.push_back(password_);
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (resp[0] == "OK") {
} else {
LOG(FATAL) << "Connect to redis(" << ip_ << ":" << port_ << ") Invalid password";
cli_->Close();
cli_ = NULL;
should_exit_ = true;
return;
}
} else {
LOG(WARNING) << "send auth failed: " << s.ToString();
cli_->Close();
cli_ = NULL;
continue;
}
if (!Authenticate()) {
cli_->Close();
continue;
}
break;
}
}
}

bool RedisSender::Authenticate() {
if (!password_.empty()) {
net::RedisCmdArgsType argv, resp;
std::string cmd;

argv.push_back("AUTH");
argv.push_back(password_);
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (resp[0] == "OK") {
return true;
} else {
// If forget to input password
net::RedisCmdArgsType argv, resp;
std::string cmd;

argv.push_back("PING");
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (s.ok()) {
if (resp[0] == "NOAUTH Authentication required.") {
LOG(FATAL) << "Ping redis(" << ip_ << ":" << port_ << ") NOAUTH Authentication required";
cli_->Close();
cli_ = NULL;
should_exit_ = true;
return;
}
} else {
LOG(WARNING) << s.ToString();
cli_->Close();
cli_ = NULL;
}
}
LOG(ERROR) << "Connect to redis(" << ip_ << ":" << port_ << ") Invalid password";
return false;
}
} else {
LOG(WARNING) << "send auth failed: " << s.ToString();
return false;
}
} else {
net::RedisCmdArgsType argv, resp;
std::string cmd;

argv.push_back("PING");
net::SerializeRedisCommand(argv, &cmd);
pstd::Status s = cli_->Send(&cmd);

if (s.ok()) {
s = cli_->Recv(&resp);
if (s.ok() && resp[0] == "NOAUTH Authentication required.") {
LOG(ERROR) << "Ping redis(" << ip_ << ":" << port_ << ") NOAUTH Authentication required";
return false;
}
} else {
LOG(WARNING) << s.ToString();
return false;
}
}
return true;
}

void RedisSender::Stop() {
Expand All @@ -122,11 +115,8 @@ void RedisSender::SendRedisCommand(const std::string &command) {
int RedisSender::SendCommand(std::string &command) {
time_t now = ::time(NULL);
if (kCheckDiff < now - last_write_time_) {
int ret = cli_->CheckAliveness();
if (ret < 0) {
cli_ = NULL;
cli_->Close();
ConnectRedis();
}
last_write_time_ = now;
}

Expand All @@ -140,10 +130,9 @@ int RedisSender::SendCommand(std::string &command) {
}

cli_->Close();
cli_ = NULL;
ConnectRedis();
} while(++idx < 3);
LOG(WARNING) << "RedisSender " << id_ << " fails to send redis command " << command << ", times: " << idx << ", error: " << "send command failed";
LOG(ERROR) << "RedisSender " << id_ << " fails to send redis command " << command << ", times: " << idx << ", error: " << "send command failed";
return -1;
}

Expand Down Expand Up @@ -182,7 +171,7 @@ void *RedisSender::ThreadMain() {
}

LOG(INFO) << "RedisSender thread " << id_ << " complete";
cli_ = NULL;
cli_->Close();
return NULL;
}

0 comments on commit 014bc67

Please sign in to comment.