Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 6e99e1e

Browse files
lthHerman Lee
authored andcommitted
Streaming compression
Summary: Extend protocol to support streaming compression. This adds new possible values for the slave_compression_lib variable: zstd_stream/lz4f_stream. Also, zstd_net_compression_level and lz4f_net_compression_level can now take on negative values for even faster compression. This reuses the existing scheme where the client requests the compression algorithm via connection attributes. If the server does not support this algorithm, the server will fall back to zlib and the client may read unexpected data (which means that all servers will have to be upgraded before it is safe for clients to request these new algorithms). When compression fails to produce data smaller than the original data, we send data uncompressed, and we reset the context on both the compression and decompression side. In theory, we only need to do this when the compressed data is larger than 16MB because we only have 3 bytes to encode the length, but to keep things simple, we'll always reset the context when compression inflates data size. Status counters are also added to track how frequently resets happen. Compression_context_reset counts number of resets and in combination with Compression_input_bytes, will give average amount of bytes processed before a reset happens. Compression_output_bytes is also added to track compression ratio. This also fixes a bug in the original implementation where level was not being properly set because the #ifdef used was incorrect. Reference patch: 3339788 Differential Revision: D19316704
1 parent 70b09a9 commit 6e99e1e

45 files changed

Lines changed: 2377 additions & 167 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmake/lz4.cmake‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
# along with this program; if not, write to the Free Software
2121
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

23-
# cmake -DWITH_LZ4=system|bundled
23+
# cmake -DWITH_LZ4=system|bundled|3rdparty
2424
# bundled is the default
2525

26+
SET(LIBLZ4_VERSION_REQUIRED "1.8.0") # LZ4F_HEADER_SIZE_MAX and LZ4F_freeDecompressionContext
27+
2628
MACRO(FIND_LZ4_VERSION)
2729
FOREACH(version_part
2830
LZ4_VERSION_MAJOR
@@ -40,6 +42,12 @@ MACRO(FIND_LZ4_VERSION)
4042
MESSAGE(STATUS "LZ4_VERSION (${WITH_LZ4}) is ${LZ4_VERSION}")
4143
MESSAGE(STATUS "LZ4_INCLUDE_DIR ${LZ4_INCLUDE_DIR}")
4244
MESSAGE(STATUS "LZ4_LIBRARY ${LZ4_LIBRARY}")
45+
46+
IF (${LZ4_VERSION} VERSION_LESS ${LIBLZ4_VERSION_REQUIRED})
47+
MESSAGE(FATAL_ERROR "Required liblz4 ${LIBLZ4_VERSION_REQUIRED} and installed version is ${LZ4_VERSION}")
48+
ELSE()
49+
MESSAGE(STATUS "Found liblz4 version ${LZ4_VERSION}")
50+
ENDIF()
4351
ENDMACRO()
4452

4553
MACRO (FIND_SYSTEM_LZ4)

‎cmake/zstd.cmake‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
# With earier versions, several compression tests fail.
2727
# With version < 1.0.0 our source code does not build.
28-
SET(MIN_ZSTD_VERSION_REQUIRED "1.2.0")
28+
# ZSTD_reset_session_only requires 1.4.
29+
SET(MIN_ZSTD_VERSION_REQUIRED "1.4.0")
2930

3031
MACRO (FIND_ZSTD_VERSION)
3132
FOREACH(version_part

‎include/compression.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,24 @@
3535

3636
#define COMPRESSION_ALGORITHM_ZLIB "zlib"
3737
#define COMPRESSION_ALGORITHM_ZSTD "zstd"
38+
#define COMPRESSION_ALGORITHM_ZSTD_STREAM "zstd_stream"
39+
#define COMPRESSION_ALGORITHM_LZ4F_STREAM "lz4f_stream"
3840
#define COMPRESSION_ALGORITHM_UNCOMPRESSED "uncompressed"
3941
#define COMPRESSION_ALGORITHM_NAME_LENGTH_MAX 32
40-
#define COMPRESSION_ALGORITHM_COUNT_MAX 3
42+
#define COMPRESSION_ALGORITHM_COUNT_MAX 5
4143
#define COMPRESSION_ALGORITHM_NAME_BUFFER_SIZE \
4244
((COMPRESSION_ALGORITHM_NAME_LENGTH_MAX * COMPRESSION_ALGORITHM_COUNT_MAX) + \
4345
3)
44-
#define PROTOCOL_COMPRESSION_DEFAULT_VALUE "zlib,zstd,uncompressed"
46+
#define PROTOCOL_COMPRESSION_DEFAULT_VALUE \
47+
"zlib,zstd,zstd_stream,lz4f_stream,uncompressed"
48+
49+
extern const char *mysql_compression_lib_names[COMPRESSION_ALGORITHM_COUNT_MAX];
4550

4651
constexpr int default_zstd_compression_level = 3;
4752

4853
/* Helper functions to validate compression algorithm and level */
4954
enum_compression_algorithm get_compression_algorithm(
5055
std::string name = std::string());
51-
std::string get_compression_algorithm_name(enum_compression_algorithm);
5256
void parse_compression_algorithms_list(std::string name,
5357
std::vector<std::string> &list);
5458
bool is_zstd_compression_level_valid(uint level);

‎include/my_compress.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ enum enum_compression_algorithm {
2828
MYSQL_UNCOMPRESSED = 1,
2929
MYSQL_ZLIB,
3030
MYSQL_ZSTD,
31+
MYSQL_ZSTD_STREAM,
32+
MYSQL_LZ4F_STREAM,
3133
MYSQL_INVALID
3234
};
3335

@@ -42,6 +44,25 @@ typedef struct mysql_zlib_compress_context {
4244
unsigned int compression_level;
4345
} mysql_zlib_compress_context;
4446

47+
typedef struct LZ4F_cctx_s LZ4F_compressionContext;
48+
typedef struct LZ4F_dctx_s LZ4F_decompressionContext;
49+
50+
/**
51+
Compress context information. relating to LZ4 compression.
52+
*/
53+
54+
typedef struct mysql_lz4f_compress_context {
55+
/**
56+
Compression level to use in lz4 compression.
57+
*/
58+
LZ4F_compressionContext *cctx;
59+
LZ4F_decompressionContext *dctx;
60+
bool reset_cctx;
61+
unsigned char *compress_buf;
62+
unsigned long compress_buf_len;
63+
unsigned int compression_level;
64+
} mysql_lz4f_compress_context;
65+
4566
typedef struct ZSTD_CCtx_s ZSTD_CCtx;
4667
typedef struct ZSTD_DCtx_s ZSTD_DCtx;
4768

@@ -61,6 +82,9 @@ typedef struct mysql_zstd_compress_context {
6182
/**
6283
Compression level to use in zstd compression.
6384
*/
85+
bool reset_cctx;
86+
unsigned char *compress_buf;
87+
unsigned long compress_buf_len;
6488
unsigned int compression_level;
6589
} mysql_zstd_compress_context;
6690

@@ -73,6 +97,7 @@ typedef struct mysql_zstd_compress_context {
7397
typedef struct mysql_compress_context {
7498
enum enum_compression_algorithm algorithm; ///< Compression algorithm name.
7599
union {
100+
mysql_lz4f_compress_context lz4f_ctx; ///< Context information of lz4.
76101
mysql_zlib_compress_context zlib_ctx; ///< Context information of zlib.
77102
mysql_zstd_compress_context zstd_ctx; ///< Context information of zstd.
78103
} u;
@@ -111,4 +136,6 @@ void mysql_compress_context_init(mysql_compress_context *cmp_ctx,
111136

112137
void mysql_compress_context_deinit(mysql_compress_context *mysql_compress_ctx);
113138

139+
void reset_compress_status(void);
140+
114141
#endif // MY_COMPRESS_INCLUDED

‎include/my_sys.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,11 @@ extern char *strdup_root(MEM_ROOT *root, const char *str);
843843
extern char *safe_strdup_root(MEM_ROOT *root, const char *str);
844844
extern char *strmake_root(MEM_ROOT *root, const char *str, size_t len);
845845
extern void *memdup_root(MEM_ROOT *root, const void *str, size_t len);
846-
extern bool my_compress(mysql_compress_context *, uchar *, size_t *, size_t *,
847-
uint);
846+
extern bool my_compress(mysql_compress_context *, uchar *, size_t *, size_t *);
848847
extern bool my_uncompress(mysql_compress_context *, uchar *, size_t, size_t *);
849848
extern uchar *my_compress_alloc(mysql_compress_context *comp_ctx,
850849
const uchar *packet, size_t *len,
851-
size_t *complen, uint level);
850+
size_t *complen);
852851

853852
extern uint my_set_max_open_files(uint files);
854853

‎include/mysql.h.pp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,37 @@
9696
MYSQL_UNCOMPRESSED = 1,
9797
MYSQL_ZLIB,
9898
MYSQL_ZSTD,
99+
MYSQL_ZSTD_STREAM,
100+
MYSQL_LZ4F_STREAM,
99101
MYSQL_INVALID
100102
};
101103
typedef struct mysql_zlib_compress_context {
102104
unsigned int compression_level;
103105
} mysql_zlib_compress_context;
106+
typedef struct LZ4F_cctx_s LZ4F_compressionContext;
107+
typedef struct LZ4F_dctx_s LZ4F_decompressionContext;
108+
typedef struct mysql_lz4f_compress_context {
109+
LZ4F_compressionContext *cctx;
110+
LZ4F_decompressionContext *dctx;
111+
bool reset_cctx;
112+
unsigned char *compress_buf;
113+
unsigned long compress_buf_len;
114+
unsigned int compression_level;
115+
} mysql_lz4f_compress_context;
104116
typedef struct ZSTD_CCtx_s ZSTD_CCtx;
105117
typedef struct ZSTD_DCtx_s ZSTD_DCtx;
106118
typedef struct mysql_zstd_compress_context {
107119
ZSTD_CCtx *cctx;
108120
ZSTD_DCtx *dctx;
121+
bool reset_cctx;
122+
unsigned char *compress_buf;
123+
unsigned long compress_buf_len;
109124
unsigned int compression_level;
110125
} mysql_zstd_compress_context;
111126
typedef struct mysql_compress_context {
112127
enum enum_compression_algorithm algorithm;
113128
union {
129+
mysql_lz4f_compress_context lz4f_ctx;
114130
mysql_zlib_compress_context zlib_ctx;
115131
mysql_zstd_compress_context zstd_ctx;
116132
} u;
@@ -121,6 +137,7 @@
121137
enum enum_compression_algorithm algorithm,
122138
unsigned int compression_level);
123139
void mysql_compress_context_deinit(mysql_compress_context *mysql_compress_ctx);
140+
void reset_compress_status(void);
124141
enum SERVER_STATUS_flags_enum {
125142
SERVER_STATUS_IN_TRANS = 1,
126143
SERVER_STATUS_AUTOCOMMIT = 2,

‎include/mysql_com.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@
460460
net_send_ok(), net_send_eof()
461461
*/
462462
#define CLIENT_TRANSACTIONS 8192
463-
#define CLIENT_RESERVED 16384 /**< DEPRECATED: Old flag for 4.1 protocol */
463+
#define CLIENT_RESERVED 16384
464464
#define CLIENT_RESERVED2 \
465465
32768 /**< DEPRECATED: Old flag for 4.1 authentication \
466466
CLIENT_SECURE_CONNECTION */
@@ -782,8 +782,8 @@
782782
CLIENT_CONNECT_WITH_DB | CLIENT_NO_SCHEMA | CLIENT_COMPRESS | CLIENT_ODBC | \
783783
CLIENT_LOCAL_FILES | CLIENT_IGNORE_SPACE | CLIENT_PROTOCOL_41 | \
784784
CLIENT_INTERACTIVE | CLIENT_SSL | CLIENT_IGNORE_SIGPIPE | \
785-
CLIENT_TRANSACTIONS | CLIENT_RESERVED | CLIENT_RESERVED2 | \
786-
CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS | CLIENT_PS_MULTI_RESULTS | \
785+
CLIENT_TRANSACTIONS | CLIENT_RESERVED2 | CLIENT_MULTI_STATEMENTS | \
786+
CLIENT_MULTI_RESULTS | CLIENT_PS_MULTI_RESULTS | \
787787
CLIENT_SSL_VERIFY_SERVER_CERT | CLIENT_REMEMBER_OPTIONS | \
788788
CLIENT_PLUGIN_AUTH | CLIENT_CONNECT_ATTRS | \
789789
CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA | \

‎libmysql/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ ELSE()
246246
LIST(APPEND LIBS_TO_LINK ${ZLIB_LIBRARY})
247247
ENDIF()
248248

249+
LIST(APPEND LIBS_TO_LINK ${LZ4_LIBRARY})
250+
249251
IF(WITH_ZSTD STREQUAL "bundled")
250252
LIST(APPEND LIBS_TO_MERGE ${ZSTD_LIBRARY})
251253
ELSE()

‎mysql-test/collections/disabled_rocksdb.def‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,3 @@ rocksdb.index_merge_rocksdb2 : BUG#0000 query plan is different between 8.0 and
5151
rocksdb.rqg_runtime: BUG#0000 commit without prepare
5252
rocksdb.rqg_transactions : BUG#0000 commit without prepare
5353
rocksdb.add_index_inplace : BUG#0000 : ALTER TABLE ALGORITHM=INPLACE fails
54-
55-
# Rebase: not-yet-implemented bugs
56-
rocksdb.bypass_select_range_pk_bloom : BUG#0000 Rebase: not-yet-implemented
57-
rocksdb.bypass_select_scenarios_bloom : BUG#0000 Rebase: not-yet-implemented
58-
rocksdb.verify_tbl_share_primary_idx : BUG#0000 Rebase: not-yet-implemented
59-
rocksdb.bypass_select_range_sk_bloom : BUG#0000 Rebase: not-yet-implemented
60-
rocksdb.bypass_select_basic_bloom : BUG#0000 Rebase: not-yet-implemented

‎mysql-test/r/compress.result‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
FLUSH STATUS;
12
SHOW STATUS LIKE 'Compression%';
23
Variable_name Value
34
Compression ON
45
Compression_algorithm zlib
6+
Compression_context_reset 0
7+
Compression_input_bytes 0
58
Compression_level 6
9+
Compression_output_bytes 0
610
drop table if exists t1,t2,t3,t4;
711
CREATE TABLE t1 (
812
Period smallint(4) unsigned zerofill DEFAULT '0000' NOT NULL,
@@ -2265,8 +2269,11 @@ drop table t1;
22652269
SHOW STATUS LIKE 'Compression%';
22662270
Variable_name Value
22672271
Compression ON
2268-
Compression_algorithm zstd
2269-
Compression_level 10
2272+
Compression_algorithm zlib
2273+
Compression_context_reset 0
2274+
Compression_input_bytes 0
2275+
Compression_level 6
2276+
Compression_output_bytes 64457
22702277
drop table if exists t1,t2,t3,t4;
22712278
CREATE TABLE t1 (
22722279
Period smallint(4) unsigned zerofill DEFAULT '0000' NOT NULL,

0 commit comments

Comments
 (0)