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

Add logging options for recent N queries #43

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

jigyasumxkkxr
Copy link

This update introduces new query logging option:

--log-N-queries=N : Saves the last N queries to tmp/node.3306/step-1/thread-0/recent-.sql. Defaults to 5 if not specified.

Fallback: If no logging option is selected, the default is to log the last 5 queries.

--log-all-queries : Saves a full query log to tmp/node.3306/step-1/thread-0.sql.

How to Test:

  1. Enable --log-all-queries and confirm the full log is saved.
  2. Set --log-N-queries=10 and check if only the last 10 queries are logged.
  3. Ensure default behavior logs the last 5 queries when no options are selected.

Copy link
Owner

@rahulmalik87 rahulmalik87 left a comment

Choose a reason for hiding this comment

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

Did you test it. The option should work without --pquery . So basically it should log last N sql into a file. by command . pstress-ms --table=1 --threads=1 --seconds=1
The changes you implement will work with option --pquery the requirement is it should work without that mode. Thanks for your time. Please make it work with default option

src/thread.cpp Outdated
general_log << "Unable to open logfile for client output " << cl.str()
<< ": " << std::strerror(errno) << std::endl;
return;
if (options->at(Option::LOG_N_QUERIES) && options->at(Option::LOG_N_QUERIES)->getInt() > 0) {
Copy link
Owner

Choose a reason for hiding this comment

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

If user pass --log-n-query=0, It should not log any query.

src/thread.cpp Outdated
size_t n_queries = 5;

if (options->at(Option::LOG_N_QUERIES) &&
options->at(Option::LOG_N_QUERIES)->getInt() >= 0) {
Copy link
Author

Choose a reason for hiding this comment

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

Logic Corrected for --log-N-queries=0.
Now it will save an empty log file in myParams.logdir i.e /tmp here.

@jigyasumxkkxr
Copy link
Author

Testing Examples :

Command 1 :

./pstress-ms --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

Default case : will save recent 5 logs to /tmp/node.3306_step_1_thread-0_recent-5.sql
Defaults : step = 1 , default thread = 0. Recent logs = 5.

Command 2 :

./pstress-ms --log-N-queries=10 --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

Recent Query Case : will save recent 10 logs to /tmp/node.3306_step_1_thread-0_recent-10.sql
Defaults : step = 1 , default thread = 0.

Command 3 :

./pstress-ms --log-all-queries --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

All Query Case : will save all logs to /tmp/node.3306_step_1_thread-0.sql
Defaults : step = 1 , default thread = 0.

Command 4 :

./pstress-ms --log-all-queries --log-N-queries=10 --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

All Query Case : will save all logs to /tmp/node.3306_step_1_thread-0.sql
Defaults : step = 1 , default thread = 0. All query logs will be printed.

Testing Examples (--pquery) :

Command 5 :

./pstress-ms --pquery --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

All Query Case : will save all logs to /tmp/node.3306_step_1_thread-0_recent-pquery-5.sql
Defaults : step = 1 , default thread = 0. Recent logs = 5.

Command 6 :

./pstress-ms --log-N-queries=10 --pquery --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

All Query Case : will save all logs to /tmp/node.3306_step_1_thread-0_recent-pquery-10.sql
Defaults : step = 1 , default thread = 0.

Command 7 :

./pstress-ms --log-all-queries --pquery --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

All Query Case : will save all logs to /tmp/node.3306_step_1_thread-0-pquery.sql
Defaults : step = 1 , default thread = 0. All query logs will be printed.

Command 8 :

./pstress-ms --log-all-queries --log-N-queries=10 --pquery --socket=/var/run/mysqld/mysqld.sock --password=<password> --database=<testdb> --no-encryption --no-tbs --grammar-sql=0 --no-ddl --table=1 --threads=1 --seconds=3

All Query Case : will save all logs to /tmp/node.3306_step_1_thread-0-pquery.sql
Defaults : step = 1 , default thread = 0. All query logs will be printed.

Observation :

  • For these arguments, when the --pquery argument is present, the full logs file looks like:

F SELECT * FROM t1;
Error Table 'testdb.t1' doesn't exist

  • For N=1, changes in the log file can be observed when using --log-N-queries=1, and looks like:

Error Table 'testdb.t1' doesn't exist

Copy link
Owner

@rahulmalik87 rahulmalik87 left a comment

Choose a reason for hiding this comment

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

Very clean implementation. Good work. Getting there.
The requirement is to log last N queries executed by pstress in thread.sql. Two important changes.

  1. use the same file that write log with --log-all-queries i.e thread_log file.
  2. Last N queries by each thread and not overall. so one hint that structure should be part of Thread class.

Copy link
Author

Choose a reason for hiding this comment

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

Ignore this added automatically by vscode

Copy link
Owner

Choose a reason for hiding this comment

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

please add it .gitignore file. so it doesn't get upload and remove it.

src/random_test.cpp Outdated Show resolved Hide resolved
src/random_test.cpp Outdated Show resolved Hide resolved
src/random_test.cpp Outdated Show resolved Hide resolved
@@ -192,9 +192,9 @@ struct Index {
struct Thd1 {
Thd1(int id, std::ofstream &tl, std::ofstream &ddl_l, std::ofstream &client_l,
MYSQL *c, std::atomic<unsigned long long> &p,
std::atomic<unsigned long long> &f)
std::atomic<unsigned long long> &f,int log_N_count)
Copy link
Author

Choose a reason for hiding this comment

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

Adding New arg for Thd1 structure

src/thread.cpp Outdated
? (log_filename.str() + ".sql") // All queries log
: (log_filename.str() + "_recent"
+ "-" + std::to_string(n_queries) + ".sql");
std::string full_log_filename = log_filename.str() + ".sql"
Copy link
Author

Choose a reason for hiding this comment

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

Setting single file name for logs.

Copy link
Owner

@rahulmalik87 rahulmalik87 left a comment

Choose a reason for hiding this comment

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

Please address review comments.

Copy link
Owner

Choose a reason for hiding this comment

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

please add it .gitignore file. so it doesn't get upload and remove it.

src/random_test.cpp Outdated Show resolved Hide resolved
src/thread.cpp Outdated Show resolved Hide resolved
src/thread.cpp Outdated Show resolved Hide resolved
src/thread.cpp Outdated Show resolved Hide resolved
@@ -2679,11 +2679,16 @@ void Table::Compare_between_engine(const std::string &sql, Thd1 *thd) {
set_default();
}

// Data structures for recent queries
static std::deque<std::string> recent_queries;
Copy link
Owner

Choose a reason for hiding this comment

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

Is it used any where?

Copy link
Author

Choose a reason for hiding this comment

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

I have corrected the logic and removed it

@@ -42,6 +43,10 @@ std::string rand_float(float upper, float lower = 0);
std::string rand_double(double upper, double lower = 0);
std::string rand_string(int upper, int lower = 2);

// Introduction of new function to retrive deque of recent queries
std::deque<std::string> get_recent_queries();
Copy link
Owner

Choose a reason for hiding this comment

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

Isn't better to make it part of Thd class?

Copy link
Author

Choose a reason for hiding this comment

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

Hmm, Removed and it is already part of Thd class

@@ -42,6 +43,10 @@ std::string rand_float(float upper, float lower = 0);
std::string rand_double(double upper, double lower = 0);
std::string rand_string(int upper, int lower = 2);

// Introduction of new function to retrive deque of recent queries
std::deque<std::string> get_recent_queries();
extern std::mutex recent_queries_mutex;
Copy link
Owner

Choose a reason for hiding this comment

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

recent_queries_mutex is used no where

Copy link
Author

Choose a reason for hiding this comment

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

Removed

@@ -2797,6 +2810,10 @@ bool execute_sql(const std::string &sql, Thd1 *thd) {

return (res == 0 ? 1 : 0);
}
// Retrive the formed deque
std::deque<std::string> get_recent_queries() {
Copy link
Owner

Choose a reason for hiding this comment

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

Seems like you duplicating the function?

Copy link
Author

Choose a reason for hiding this comment

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

Removed as its already present in Thd Class

src/thread.cpp Outdated
@@ -9,6 +9,10 @@
std::atomic_flag lock_metadata = ATOMIC_FLAG_INIT;
std::atomic<bool> metadata_loaded(false);

// Mutex for thread-safe logging
std::mutex general_log_mutex;
Copy link
Owner

Choose a reason for hiding this comment

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

It is not used anywhere.

Copy link
Author

Choose a reason for hiding this comment

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

Removed

bool execute_sql(const std::string &sql, Thd1 *thd) {
auto query = sql.c_str();
static auto log_all = opt_bool(LOG_ALL_QUERIES);
static auto log_failed = opt_bool(LOG_FAILED_QUERIES);
static auto log_success = opt_bool(LOG_SUCCEDED_QUERIES);
static auto log_N = opt_bool(LOG_N_QUERIES);
thd->max_recent_queries = options->at(Option::LOG_N_QUERIES)->getInt();
Copy link
Owner

Choose a reason for hiding this comment

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

you are reassigning this variable . This is done as part of Thd class initialization as well

Copy link
Author

Choose a reason for hiding this comment

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

Yes, removed

bool execute_sql(const std::string &sql, Thd1 *thd) {
auto query = sql.c_str();
static auto log_all = opt_bool(LOG_ALL_QUERIES);
static auto log_failed = opt_bool(LOG_FAILED_QUERIES);
static auto log_success = opt_bool(LOG_SUCCEDED_QUERIES);
static auto log_N = opt_bool(LOG_N_QUERIES);
Copy link
Owner

Choose a reason for hiding this comment

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

user would provide a value for --log-N-queries. So either a option is Bool or Int. Can't be both

Copy link
Author

Choose a reason for hiding this comment

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

Removed this line and code is giving correct query logs i have tested

@@ -2716,6 +2721,10 @@ bool execute_sql(const std::string &sql, Thd1 *thd) {
if (res != 0) { // query failed
thd->failed_queries_total++;
thd->max_con_fail_count++;
// Manage recent queries for failed queries
if (!log_N) {
Copy link
Owner

Choose a reason for hiding this comment

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

the condition should be if(options->at(Option::LOG_N_QUERIES)->getInt()>0)

Copy link
Author

Choose a reason for hiding this comment

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

added

src/thread.cpp Outdated
/* connection can be changed if we thd->tryreconnect is called */
conn = thd->conn;
delete thd;


Copy link
Owner

Choose a reason for hiding this comment

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

Remove this empty lines

Copy link
Author

Choose a reason for hiding this comment

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

Removed

src/thread.cpp Outdated
@@ -155,11 +170,21 @@ void Node::workerThread(int number) {
break;
}
}
logDeque = thd->get_recent_queries();
Copy link
Owner

Choose a reason for hiding this comment

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

Will this work ? in the case of mode pquery. you are not saving query.

Copy link
Author

@jigyasumxkkxr jigyasumxkkxr Dec 21, 2024

Choose a reason for hiding this comment

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

hmm, removed, this will not work and as usual give 1-2 queries even if i ran --log-all-queries

#ifdef DUCKDB
try {
// Create DuckDB connection
duckdb::DuckDB db(nullptr); // In-memory database
Copy link
Owner

Choose a reason for hiding this comment

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

For each SQL to execute , It create new in memory database. This is not what is required here.
We have to create in memory database just once. use connection based on --thread we have to open a connection against that.

Copy link
Owner

@rahulmalik87 rahulmalik87 left a comment

Choose a reason for hiding this comment

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

Create a new pull request and not append with N queries. The start look good. compile tryreconnect only for mysql. disable kill query for option for DUCKDB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants