Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-write sonic-db-cli with c++ for sonic startup performance issue #607

Merged
merged 21 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
SUBDIRS = common pyext tests
SUBDIRS = common pyext sonic-db-cli tests

ACLOCAL_AMFLAGS = -I m4
10 changes: 10 additions & 0 deletions common/rediscommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ void RedisCommand::formatArgv(int argc, const char **argv, const size_t *argvlen
}
}

void RedisCommand::format(const vector<string> &commands)
{
vector<const char*> args;
for (auto& command : commands)
{
args.push_back(command.c_str());
}
formatArgv(static_cast<int>(args.size()), args.data(), NULL);
}

/* Format HMSET key multiple field value command */
void RedisCommand::formatHMSET(const std::string &key,
const std::vector<FieldValueTuple> &values)
Expand Down
1 change: 1 addition & 0 deletions common/rediscommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RedisCommand {

void format(const char *fmt, ...);
void formatArgv(int argc, const char **argv, const size_t *argvlen);
void format(const std::vector<std::string> &commands);

/* Format HMSET key multiple field value command */
#ifndef SWIG
Expand Down
43 changes: 42 additions & 1 deletion common/redisreply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdint.h>
#include <vector>
#include <iostream>
#include <sstream>
#include <system_error>
#include <functional>

Expand Down Expand Up @@ -158,7 +159,7 @@ void RedisReply::checkReplyType(int expectedType)
const char *err = (m_reply->type == REDIS_REPLY_STRING || m_reply->type == REDIS_REPLY_ERROR) ?
m_reply->str : "NON-STRING-REPLY";

string errmsg = "Expected to get redis type " + to_string(expectedType) + " got type " + to_string(m_reply->type) + ", err: " + err;
string errmsg = "Expected to get redis type " + std::to_string(expectedType) + " got type " + std::to_string(m_reply->type) + ", err: " + err;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::

I am not sure if it will introduce ambiguity. Seems like you can remove std::.
Not a big issue.

SWSS_LOG_ERROR("%s", errmsg.c_str());
throw system_error(make_error_code(errc::io_error), errmsg);
}
Expand Down Expand Up @@ -225,4 +226,44 @@ template<> RedisMessage RedisReply::getReply<RedisMessage>()
return ret;
}


string RedisReply::to_string()
{
return to_string(getContext());
}

string RedisReply::to_string(redisReply *reply)
{
switch(reply->type)
{
case REDIS_REPLY_INTEGER:
return std::to_string(reply->integer);

case REDIS_REPLY_STRING:
case REDIS_REPLY_ERROR:
case REDIS_REPLY_STATUS:
case REDIS_REPLY_NIL:
return string(reply->str, reply->len);

case REDIS_REPLY_ARRAY:
{
stringstream result;
for (size_t i = 0; i < reply->elements; i++)
{
result << to_string(reply->element[i]);

if (i < reply->elements - 1)
{
result << endl;
}
}
return result.str();
}

default:
SWSS_LOG_ERROR("invalid type %d for message", getContext()->type);
return string();
}
}

}
3 changes: 3 additions & 0 deletions common/redisreply.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ class RedisReply
/* Check that the status is QUEUED, throw exception otherwise */
void checkStatusQueued();

std::string to_string();

private:
void checkStatus(const char *status);
void checkReply();
std::string to_string(redisReply *reply);
Copy link
Contributor

@qiluo-msft qiluo-msft May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_string

This function could be:

  1. static member function
  2. public function
  3. or global function, since it will be useful to any redisReply. #Closed


redisReply *m_reply;
};
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ AC_CONFIG_FILES([
pyext/Makefile
pyext/py2/Makefile
pyext/py3/Makefile
sonic-db-cli/Makefile
tests/Makefile
])

Expand Down
6 changes: 6 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ Architecture: any
Depends: ${shlibs:Depends}, ${misc:Pre-Depends}
Section: libs
Description: This package contains Switch State Service common Python3 library.

Package: sonic-db-cli
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Pre-Depends}
Section: libs
Description: This package contains SONiC DB cli.
1 change: 1 addition & 0 deletions debian/sonic-db-cli.dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
usr/bin
1 change: 1 addition & 0 deletions debian/sonic-db-cli.install
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
usr/bin/sonic-db-cli
liuh-80 marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions sonic-db-cli/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
INCLUDES = -I $(top_srcdir)

if DEBUG
DBGFLAGS = -ggdb -DDEBUG
else
DBGFLAGS = -g -DNDEBUG
endif

lib_LTLIBRARIES = libsonicdbcli.la
libsonicdbcli_la_SOURCES = sonic-db-cli.cpp
libsonicdbcli_la_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
libsonicdbcli_la_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON)
libsonicdbcli_la_LDFLAGS = -L$(top_srcdir)/common -lswsscommon -lpthread

bin_PROGRAMS = sonic-db-cli
sonic_db_cli_SOURCES = sonic-db-cli.cpp main.cpp
sonic_db_cli_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON)
sonic_db_cli_CPPFLAGS = $(DBGFLAGS) $(AM_CPPFLAGS) $(CFLAGS_COMMON)
sonic_db_cli_LDFLAGS = -L$(top_srcdir)/common -lswsscommon -lpthread
24 changes: 24 additions & 0 deletions sonic-db-cli/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "sonic-db-cli.h"
#include "common/dbconnector.h"

using namespace swss;
using namespace std;

int main(int argc, char** argv)
{
auto initializeGlobalConfig = []()
{
SonicDBConfig::initializeGlobalConfig(SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
};

auto initializeConfig = []()
{
SonicDBConfig::initialize(SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
};

return sonic_db_cli(
argc,
argv,
initializeGlobalConfig,
initializeConfig);
}
Loading