Skip to content

Commit

Permalink
Bug#34833913 binlog_transaction_compression_level_zstd has no effect
Browse files Browse the repository at this point in the history
PROBLEM :

Setting of the 'binlog_transaction_compression_level_zstd' variable has no effect.
Size of data compressed with ZSTD library does not change, even if we
increase compression level value up to 22.

Solution :

1. Need to call function Zstd_comp::set_compression_level() in
   setup_compressor().
2. Reset m_started variable to false in Zstd_comp::do_finish()
   function since reset_compressor() function is not called.

Change-Id: Ib9dcfbda9e15fae49ea6828802eac3cb73e46c9a
  • Loading branch information
Arpit Goswami committed Jun 22, 2023
1 parent be4219b commit 7c4a14e
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libbinlogevents/src/compression/zstd_comp.cpp
Expand Up @@ -166,7 +166,7 @@ Compress_status Zstd_comp::do_finish(Managed_buffer_sequence_t &out) {
move_position(out, obuf.pos);
} while (zstd_status > 0);
BAPI_LOG("info", BAPI_VAR(m_ibuf.pos) << " " << BAPI_VAR(m_ibuf.size));

m_started = false;
return Compress_status::success;
}

Expand Down
@@ -0,0 +1,45 @@
#
# 1) Prepare a sample table
#
CREATE TABLE t (a LONGTEXT);
#
# 2) Enable binlog_transaction_compression
#
SET @@session.binlog_transaction_compression = ON;
#
# 3) Set the value of binlog_transaction_compression_level_zstd to 1
#
SET @@session.binlog_transaction_compression_level_zstd = 1;
#
# 4) Save the binlog position before and after inserting the values in table
#
include/save_binlog_position.inc
INSERT INTO t VALUES (LOAD_FILE('../../std_data/mysqldump_57.sql'));
include/save_binlog_position.inc
#
# 5) Compute the compressed size for binlog_transaction_compression_level
#_zstd = 1
#
#
# 6) Set the value of binlog_transaction_compression_level_zstd to 19
#
SET @@session.binlog_transaction_compression_level_zstd = 19;
#
# 7) Save the binlog position before and after inserting the values in table
#
include/save_binlog_position.inc
INSERT INTO t VALUES (LOAD_FILE('../../std_data/mysqldump_57.sql'));
include/save_binlog_position.inc
#
# 8) Compute the compressed size for binlog_transaction_compression_level
#_zstd = 19
#
#
# 9) Check that the compressed size for level 19 should be smaller than level 1
#
include/assert.inc [The compresed size for level 19 should be smaller then level 1]
#
# 10) Cleanup.
#
DROP TABLE t;
SET SESSION binlog_transaction_compression_level_zstd = SAVED;
@@ -0,0 +1,115 @@
###############################################################################
# === Purpose ===
#
# The pupose of this test is to verify that the session variable
# binlog_transaction_compression_level_zstd is working as expected. It checks
# that the size of payload is compressed on increasing the value of
# binlog_transaction_compression_level_zstd.
#
# === Requirements ===
#
# 1. The compressed size for higher value of binlog_transaction_compression_level_zstd
# should be less than the compressed size for smaller value of binlog_transaction_
# compression_level_zstd
#
# === Implementation ===
#
# 1) Prepare a sample table
# 2) Enable binlog_transaction_compression
# 3) Set the value of binlog_transaction_compression_level_zstd to 1
# 4) Save the binlog position before and after inserting the values in table
# 5) Compute the compressed size for binlog_transaction_compression_level = 1
# 6) Set the value of binlog_transaction_compression_level_zstd to 19
# 7) Save the binlog position before and after inserting the values in table
# 8) Compute the compressed size for binlog_transaction_compression_level = 19
# 9) Check that the compressed size for level 19 should be smaller than level 1
# 10) Cleanup.
#
# === Reference ===
#
# Bug#34833913 : binlog_transaction_compression_level_zstd has no effect
#
###############################################################################


--source include/have_log_bin.inc
--source include/have_binlog_format_row.inc

--echo #
--echo # 1) Prepare a sample table
--echo #

CREATE TABLE t (a LONGTEXT);

--echo #
--echo # 2) Enable binlog_transaction_compression
--echo #

SET @@session.binlog_transaction_compression = ON;
--let $saved_binlog_transaction_compression_level_zstd_session = `SELECT @@SESSION.binlog_transaction_compression_level_zstd`

--echo #
--echo # 3) Set the value of binlog_transaction_compression_level_zstd to 1
--echo #

SET @@session.binlog_transaction_compression_level_zstd = 1;

--echo #
--echo # 4) Save the binlog position before and after inserting the values in table
--echo #

--source include/save_binlog_position.inc
--let $binlog_start_1 = $binlog_position

INSERT INTO t VALUES (LOAD_FILE('../../std_data/mysqldump_57.sql'));

--source include/save_binlog_position.inc
--let $binlog_end_1 = $binlog_position

--echo #
--echo # 5) Compute the compressed size for binlog_transaction_compression_level_zstd = 1
--echo #


--expr $compressed_size_1 = $binlog_end_1 - $binlog_start_1

--echo #
--echo # 6) Set the value of binlog_transaction_compression_level_zstd to 19
--echo #

SET @@session.binlog_transaction_compression_level_zstd = 19;

--echo #
--echo # 7) Save the binlog position before and after inserting the values in table
--echo #

--source include/save_binlog_position.inc
--let $binlog_start_2 = $binlog_position

INSERT INTO t VALUES (LOAD_FILE('../../std_data/mysqldump_57.sql'));

--source include/save_binlog_position.inc
--let $binlog_end_2 = $binlog_position

--echo #
--echo # 8) Compute the compressed size for binlog_transaction_compression_level
--echo #_zstd = 19
--echo #

--expr $compressed_size_2 = $binlog_end_2 - $binlog_start_2

--echo #
--echo # 9) Check that the compressed size for level 19 should be smaller than level 1
--echo #

--let $assert_text = The compresed size for level 19 should be smaller then level 1
--let $assert_cond = $compressed_size_1 > $compressed_size_2
--source include/assert.inc

--echo #
--echo # 10) Cleanup.
--echo #
DROP TABLE t;
--replace_result $saved_binlog_transaction_compression_level_zstd_session SAVED
--eval SET SESSION binlog_transaction_compression_level_zstd = $saved_binlog_transaction_compression_level_zstd_session

10 changes: 8 additions & 2 deletions sql/binlog.cc
Expand Up @@ -64,6 +64,7 @@
#include "libbinlogevents/include/buffer/grow_calculator.h"
#include "libbinlogevents/include/compression/compressor.h"
#include "libbinlogevents/include/compression/payload_event_buffer_istream.h"
#include "libbinlogevents/include/compression/zstd_comp.h"
#include "libbinlogevents/include/control_events.h"
#include "libbinlogevents/include/debug_vars.h"
#include "libbinlogevents/include/rows_event.h"
Expand Down Expand Up @@ -2099,14 +2100,20 @@ class Binlog_cache_compressor {
}

/// Get and configure the compressor; update m_compressor.
///
/// Set the compression_level for m_compressor
/// @return true on error, false on success.
[[NODISCARD]] bool setup_compressor() {
m_compressor = m_context.get_compressor(&m_thd);
if (m_compressor == nullptr) {
DBUG_PRINT("info", ("fallback to uncompressed: compressor==nullptr"));
return true;
}
auto zstd_ptr = std::dynamic_pointer_cast<
binary_log::transaction::compression::Zstd_comp>(m_compressor);
if (zstd_ptr) {
zstd_ptr->set_compression_level(
m_thd.variables.binlog_trx_compression_level_zstd);
}
// Allow compressor to optimize space usage based on uncompressed
// size.
m_compressor->set_pledged_input_size(m_uncompressed_size);
Expand Down Expand Up @@ -2199,7 +2206,6 @@ class Binlog_cache_compressor {
}
return false;
}

/// Session context.
THD &m_thd;
/// Transaction cache.
Expand Down

0 comments on commit 7c4a14e

Please sign in to comment.