From a0db6c3a1f2cdc23aa1bf110adb85092ae9bdfe8 Mon Sep 17 00:00:00 2001 From: Yashwant Sahu Date: Fri, 29 Nov 2024 13:09:26 +0530 Subject: [PATCH] 1. Changed name to protocol_compression_level. 2. Added TAP test --- include/MySQL_Thread.h | 2 +- include/proxysql_structs.h | 4 +- lib/MySQL_Thread.cpp | 10 +- lib/mysql_data_stream.cpp | 6 +- test/tap/groups/groups.json | 3 +- .../mysql-protocol_compression_level-t.cpp | 153 ++++++++++++++++++ 6 files changed, 166 insertions(+), 12 deletions(-) create mode 100644 test/tap/tests/mysql-protocol_compression_level-t.cpp diff --git a/include/MySQL_Thread.h b/include/MySQL_Thread.h index 837767ea03..c62f4154a2 100644 --- a/include/MySQL_Thread.h +++ b/include/MySQL_Thread.h @@ -470,7 +470,7 @@ class MySQL_Threads_Handler bool parse_failure_logs_digest; bool default_reconnect; bool have_compress; - int compression_level; + int protocol_compression_level; bool have_ssl; bool multiplexing; // bool stmt_multiplexing; diff --git a/include/proxysql_structs.h b/include/proxysql_structs.h index ea7b7ec8fd..69c1e0586d 100644 --- a/include/proxysql_structs.h +++ b/include/proxysql_structs.h @@ -1167,7 +1167,7 @@ __thread int mysql_thread___poll_timeout; __thread int mysql_thread___poll_timeout_on_failure; __thread bool mysql_thread___connection_warming; __thread bool mysql_thread___have_compress; -__thread int mysql_thread___compression_level; +__thread int mysql_thread___protocol_compression_level; __thread bool mysql_thread___have_ssl; __thread bool mysql_thread___multiplexing; __thread bool mysql_thread___log_unhealthy_connections; @@ -1464,7 +1464,7 @@ extern __thread int mysql_thread___poll_timeout; extern __thread int mysql_thread___poll_timeout_on_failure; extern __thread bool mysql_thread___connection_warming; extern __thread bool mysql_thread___have_compress; -extern __thread int mysql_thread___compression_level; +extern __thread int mysql_thread___protocol_compression_level; extern __thread bool mysql_thread___have_ssl; extern __thread bool mysql_thread___multiplexing; extern __thread bool mysql_thread___log_unhealthy_connections; diff --git a/lib/MySQL_Thread.cpp b/lib/MySQL_Thread.cpp index 140cc1661c..e79a19fb0f 100644 --- a/lib/MySQL_Thread.cpp +++ b/lib/MySQL_Thread.cpp @@ -504,7 +504,7 @@ static char * mysql_thread_variables_names[]= { (char *)"handle_warnings", (char *)"evaluate_replication_lag_on_servers_load", (char *)"proxy_protocol_networks", - (char *)"compression_level", + (char *)"protocol_compression_level", NULL }; @@ -1138,7 +1138,7 @@ MySQL_Threads_Handler::MySQL_Threads_Handler() { variables.enable_load_data_local_infile=false; variables.log_mysql_warnings_enabled=false; variables.data_packets_history_size=0; - variables.compression_level=3; + variables.protocol_compression_level=3; // status variables status_variables.mirror_sessions_current=0; __global_MySQL_Thread_Variables_version=1; @@ -2266,7 +2266,7 @@ char ** MySQL_Threads_Handler::get_variables_list() { VariablesPointers_int["client_host_error_counts"] = make_tuple(&variables.client_host_error_counts, 0, 1024*1024, false); VariablesPointers_int["handle_warnings"] = make_tuple(&variables.handle_warnings, 0, 1, false); VariablesPointers_int["evaluate_replication_lag_on_servers_load"] = make_tuple(&variables.evaluate_replication_lag_on_servers_load, 0, 1, false); - VariablesPointers_int["compression_level"] = make_tuple(&variables.compression_level, 0, 9, false); + VariablesPointers_int["protocol_compression_level"] = make_tuple(&variables.protocol_compression_level, 0, 9, false); // logs VariablesPointers_int["auditlog_filesize"] = make_tuple(&variables.auditlog_filesize, 1024*1024, 1*1024*1024*1024, false); @@ -4183,7 +4183,7 @@ void MySQL_Thread::refresh_variables() { REFRESH_VARIABLE_INT(poll_timeout); REFRESH_VARIABLE_INT(poll_timeout_on_failure); REFRESH_VARIABLE_BOOL(have_compress); - REFRESH_VARIABLE_INT(compression_level); + REFRESH_VARIABLE_INT(protocol_compression_level); REFRESH_VARIABLE_BOOL(have_ssl); REFRESH_VARIABLE_BOOL(multiplexing); REFRESH_VARIABLE_BOOL(log_unhealthy_connections); @@ -4268,7 +4268,7 @@ MySQL_Thread::MySQL_Thread() { mysql_thread___ssl_p2s_crl=NULL; mysql_thread___ssl_p2s_crlpath=NULL; - mysql_thread___compression_level=3; + mysql_thread___protocol_compression_level=3; last_maintenance_time=0; last_move_to_idle_thread_time=0; diff --git a/lib/mysql_data_stream.cpp b/lib/mysql_data_stream.cpp index c4008a4c2b..f51df38e02 100644 --- a/lib/mysql_data_stream.cpp +++ b/lib/mysql_data_stream.cpp @@ -1321,7 +1321,7 @@ void MySQL_Data_Stream::generate_compressed_packet() { total_size+=p2.size; l_free(p2.size,p2.ptr); } - int rc=compress2(dest, &destLen, source, sourceLen, GloMTH->variables.compression_level); + int rc=compress2(dest, &destLen, source, sourceLen, GloMTH->variables.protocol_compression_level); assert(rc==Z_OK); l_free(total_size, source); queueOUT.pkt.size=destLen+7; @@ -1353,9 +1353,9 @@ void MySQL_Data_Stream::generate_compressed_packet() { dest1=(Bytef *)malloc(destLen1+7); destLen2=len2*120/100+12; dest2=(Bytef *)malloc(destLen2+7); - rc=compress2(dest1+7, &destLen1, (const unsigned char *)p2.ptr, len1, GloMTH->variables.compression_level); + rc=compress2(dest1+7, &destLen1, (const unsigned char *)p2.ptr, len1, GloMTH->variables.protocol_compression_level); assert(rc==Z_OK); - rc=compress2(dest2+7, &destLen2, (const unsigned char *)p2.ptr+len1, len2, GloMTH->variables.compression_level); + rc=compress2(dest2+7, &destLen2, (const unsigned char *)p2.ptr+len1, len2, GloMTH->variables.protocol_compression_level); assert(rc==Z_OK); hdr.pkt_length=destLen1; diff --git a/test/tap/groups/groups.json b/test/tap/groups/groups.json index 64c1fa8d55..9661cba497 100644 --- a/test/tap/groups/groups.json +++ b/test/tap/groups/groups.json @@ -180,5 +180,6 @@ "eof_fast_forward-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ], "eof_mixed_flags_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ], "eof_packet_mixed_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ], - "ok_packet_mixed_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ] + "ok_packet_mixed_queries-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ], + "mysql-protocol_compression_level-t" : [ "default", "mysql-auto_increment_delay_multiplex=0", "mysql-multiplexing=false", "mysql-query_digests=0", "mysql-query_digests_keep_comment=1" ] } diff --git a/test/tap/tests/mysql-protocol_compression_level-t.cpp b/test/tap/tests/mysql-protocol_compression_level-t.cpp new file mode 100644 index 0000000000..6a6103c8e4 --- /dev/null +++ b/test/tap/tests/mysql-protocol_compression_level-t.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include + +#include +#include +#include +#include "mysql.h" + +#include "tap.h" +#include "command_line.h" +#include "utils.h" + +inline unsigned long long monotonic_time() { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (((unsigned long long) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000); +} + +unsigned long calculate_query_execution_time(MYSQL* mysql, const std::string& query) { + MYSQL_RES *res; + MYSQL_ROW row; + unsigned long long begin = monotonic_time(); + unsigned long long row_count = 0; + MYSQL_QUERY(mysql, query.c_str()); + res = mysql_use_result(mysql); + unsigned long long num_rows = mysql_num_rows(res); + unsigned int num_fields = mysql_num_fields(res); + while ((row = mysql_fetch_row(res))) { + for (unsigned int i = 1; i < num_fields; i++) { + char *field = row[i]; + } + row_count++; + } + mysql_free_result(res); + unsigned long long end = monotonic_time(); + fprintf(stderr, "Row count: %lld\n", row_count); + return (end - begin); +} + +void set_compression_level(std::string level) { + CommandLine cl; + + if(cl.getEnv()) + return; + + MYSQL* proxysql = mysql_init(NULL); + if (!proxysql) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql)); + return; + } + if (!mysql_real_connect(proxysql, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) { + fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql)); + return; + } + std::string query = "UPDATE global_variables SET variable_value = '" + level + "' WHERE variable_name = 'mysql-protocol_compression_level';"; + mysql_query(proxysql, query.c_str()); + query = "LOAD MYSQL VARIABLES TO RUNTIME;"; + mysql_query(proxysql, query.c_str()); + mysql_close(proxysql); +} + +MYSQL* initilize_mysql_connection(char* host, char* username, char* password, int port, bool compression) { + MYSQL* mysql = mysql_init(NULL); + if (!mysql) + return nullptr; + + fprintf(stderr, "Connection details: %s %s %d\n", username, password, port); + if (!mysql_real_connect(mysql, host, username, password, NULL, port, NULL, 0)) { + fprintf(stderr, "Failed to connect to database: Error: %s\n", + mysql_error(mysql)); + mysql_close(mysql); + return nullptr; + } + if (compression) { + if (mysql_options(mysql, MYSQL_OPT_COMPRESS, nullptr) != 0) { + fprintf(stderr, "Failed to set mysql compression option: Error: %s\n", + mysql_error(mysql)); + mysql_close(mysql); + return nullptr; + } + } + return mysql; +} + +int main(int argc, char** argv) { + + CommandLine cl; + + if(cl.getEnv()) + return exit_status(); + + plan(2); + + // ProxySQL connection without compression + MYSQL* proxysql = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, false); + if (!proxysql) { + return exit_status(); + } + + // ProxySQL connection with compression + MYSQL* proxysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.port, true); + if (!proxysql_compression) { + return exit_status(); + } + + // MySQL connection with compression + MYSQL* mysql_compression = initilize_mysql_connection(cl.host, cl.username, cl.password, cl.mysql_port, true); + if (!mysql_compression) { + return exit_status(); + } + + MYSQL_QUERY(proxysql, "CREATE DATABASE IF NOT EXISTS test"); + MYSQL_QUERY(proxysql, "DROP TABLE IF EXISTS test.sbtest1"); + + mysql_query(proxysql, "CREATE TABLE IF NOT EXISTS test.sbtest1 (id INT UNSIGNED NOT NULL AUTO_INCREMENT, k INT UNSIGNED NOT NULL DEFAULT 0, c CHAR(120) NOT NULL DEFAULT '', pad CHAR(60) NOT NULL DEFAULT '', PRIMARY KEY (id), KEY k_1 (k));"); + + MYSQL_QUERY(proxysql, "USE test"); + + for (int i = 0; i < 1000; i++) { + MYSQL_QUERY(proxysql, "INSERT INTO sbtest1 (k, c, pad) SELECT FLOOR(RAND() * 10000), REPEAT('a', 120), REPEAT('b', 60) FROM information_schema.tables LIMIT 1000;"); + } + + std::string query = "SELECT t1.id id1, t1.k k1, t1.c c1, t1.pad pad1, t2.id id2, t2.k k2, t2.c c2, t2.pad pad2 FROM test.sbtest1 t1 JOIN test.sbtest1 t2 LIMIT 50000000"; + + unsigned long time_proxy = calculate_query_execution_time(proxysql, query); + diag("Time taken for query with proxysql without compression: %ld", time_proxy); + + unsigned long time_proxy_compressed = calculate_query_execution_time(proxysql_compression, query); + diag("Time taken for query with proxysql with compression: %ld", time_proxy_compressed); + + unsigned long diff = abs(time_proxy - time_proxy_compressed); + int performance_diff = (diff * 100) / time_proxy; + + bool accepted_performance = (performance_diff < 10) ? true : false; + ok((performance_diff < 20), "proxysql with compression performed well compared to without compression. Performance difference: %d percentage", performance_diff); + + unsigned long time_mysql_compressed = calculate_query_execution_time(mysql_compression, query); + diag("Time taken for query with mysql with compression: %ld", time_mysql_compressed); + + diff = abs(time_mysql_compressed - time_proxy_compressed); + performance_diff = (diff * 100) / time_mysql_compressed; + + accepted_performance = (performance_diff < 20) ? true : false; + ok((performance_diff < 10), "proxysql with compression performed well compared to mysql with compression. Performance difference: %d percentage", performance_diff); + + mysql_close(proxysql); + mysql_close(proxysql_compression); + mysql_close(mysql_compression); + + return exit_status(); +}