From 4a268362b96cb83051bf0dde306e6fdcf2ef075d Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 22 Jan 2025 18:04:26 +0800 Subject: [PATCH] compress: fix compressor initialization order by making namespace_prefix a function Fixes a race condition where COMPRESSOR_NAME in zstd.cc could be initialized before compressor::namespace_prefix due to undefined global variable initialization order across translation units. This was causing ZstdCompressor to be unregistered in release builds, making it impossible to create tables with Zstd compression. Replace the global namespace_prefix variable with a function that returns the fully qualified compressor name. This ensures proper initialization order and fixes the registration of the ZstdCompressor. Fixes scylladb/scylladb#22444 Signed-off-by: Kefu Chai Closes scylladb/scylladb#22451 --- compress.cc | 12 +++++++----- compress.hh | 2 +- sstables/compress.cc | 2 +- zstd.cc | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/compress.cc b/compress.cc index 7fcf540b0b..56174bc4ff 100644 --- a/compress.cc +++ b/compress.cc @@ -14,7 +14,9 @@ #include "exceptions/exceptions.hh" #include "utils/class_registrator.hh" -const sstring compressor::namespace_prefix = "org.apache.cassandra.io.compress."; +sstring compressor::make_name(std::string_view short_name) { + return seastar::format("org.apache.cassandra.io.compress.{}", short_name); +} class lz4_processor: public compressor { public: @@ -66,7 +68,7 @@ compressor::ptr_type compressor::create(const sstring& name, const opt_getter& o return {}; } - qualified_name qn(namespace_prefix, name); + qualified_name qn(make_name(""), name); for (auto& c : { lz4, snappy, deflate }) { if (c->name() == static_cast(qn)) { @@ -91,9 +93,9 @@ shared_ptr compressor::create(const std::map& opti return {}; } -thread_local const shared_ptr compressor::lz4 = ::make_shared(namespace_prefix + "LZ4Compressor"); -thread_local const shared_ptr compressor::snappy = ::make_shared(namespace_prefix + "SnappyCompressor"); -thread_local const shared_ptr compressor::deflate = ::make_shared(namespace_prefix + "DeflateCompressor"); +thread_local const shared_ptr compressor::lz4 = ::make_shared(make_name("LZ4Compressor")); +thread_local const shared_ptr compressor::snappy = ::make_shared(make_name("SnappyCompressor")); +thread_local const shared_ptr compressor::deflate = ::make_shared(make_name("DeflateCompressor")); const sstring compression_parameters::SSTABLE_COMPRESSION = "sstable_compression"; const sstring compression_parameters::CHUNK_LENGTH_KB = "chunk_length_in_kb"; diff --git a/compress.hh b/compress.hh index e468b5a13a..71d7da0773 100644 --- a/compress.hh +++ b/compress.hh @@ -69,7 +69,7 @@ public: static thread_local const ptr_type snappy; static thread_local const ptr_type deflate; - static const sstring namespace_prefix; + static sstring make_name(std::string_view short_name); }; template diff --git a/sstables/compress.cc b/sstables/compress.cc index a85e1d7d9d..957c72deca 100644 --- a/sstables/compress.cc +++ b/sstables/compress.cc @@ -295,7 +295,7 @@ size_t local_compression::compress_max_size(size_t input_len) const { void compression::set_compressor(compressor_ptr c) { if (c) { - unqualified_name uqn(compressor::namespace_prefix, c->name()); + unqualified_name uqn(compressor::make_name(""), c->name()); const sstring& cn = uqn; name.value = bytes(cn.begin(), cn.end()); for (auto& [k, v] : c->options()) { diff --git a/zstd.cc b/zstd.cc index 00c50ba2af..c066e53701 100644 --- a/zstd.cc +++ b/zstd.cc @@ -20,7 +20,7 @@ #include static const sstring COMPRESSION_LEVEL = "compression_level"; -static const sstring COMPRESSOR_NAME = compressor::namespace_prefix + "ZstdCompressor"; +static const sstring COMPRESSOR_NAME = compressor::make_name("ZstdCompressor"); static const size_t DCTX_SIZE = ZSTD_estimateDCtxSize(); class zstd_processor : public compressor {