From 5ef9478a2ebbbfa9a3c2a67867369e9f8d555a03 Mon Sep 17 00:00:00 2001 From: Mikhail Koviazin Date: Wed, 10 Jan 2024 09:21:06 +0000 Subject: [PATCH] SensitiveDataMasker: replace shared_ptr with MultiVersion --- src/Common/SensitiveDataMasker.cpp | 13 ++++++------- src/Common/SensitiveDataMasker.h | 8 +++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Common/SensitiveDataMasker.cpp b/src/Common/SensitiveDataMasker.cpp index 1d8b1a9bc72..33770c3e78a 100644 --- a/src/Common/SensitiveDataMasker.cpp +++ b/src/Common/SensitiveDataMasker.cpp @@ -85,9 +85,9 @@ public: SensitiveDataMasker::~SensitiveDataMasker() = default; -std::shared_ptr SensitiveDataMasker::sensitive_data_masker = nullptr; +SensitiveDataMasker::MaskerMultiVersion SensitiveDataMasker::sensitive_data_masker{}; -void SensitiveDataMasker::setInstance(std::shared_ptr sensitive_data_masker_) +void SensitiveDataMasker::setInstance(std::unique_ptr&& sensitive_data_masker_) { if (!sensitive_data_masker_) @@ -95,18 +95,17 @@ void SensitiveDataMasker::setInstance(std::shared_ptr sensi if (sensitive_data_masker_->rulesCount() > 0) { - std::atomic_store(&sensitive_data_masker, std::move(sensitive_data_masker_)); + sensitive_data_masker.set(std::move(sensitive_data_masker_)); } else { - std::atomic_store(&sensitive_data_masker, std::shared_ptr(nullptr)); + sensitive_data_masker.set(nullptr); } } -std::shared_ptr SensitiveDataMasker::getInstance() +SensitiveDataMasker::MaskerMultiVersion::Version SensitiveDataMasker::getInstance() { - // TODO: use std::atomic when compiler supports it - return std::atomic_load(&sensitive_data_masker); + return sensitive_data_masker.get(); } SensitiveDataMasker::SensitiveDataMasker(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix) diff --git a/src/Common/SensitiveDataMasker.h b/src/Common/SensitiveDataMasker.h index afddd52a56c..a153575cdbd 100644 --- a/src/Common/SensitiveDataMasker.h +++ b/src/Common/SensitiveDataMasker.h @@ -2,6 +2,7 @@ #include #include +#include "Common/MultiVersion.h" namespace Poco { @@ -44,7 +45,8 @@ class SensitiveDataMasker private: class MaskingRule; std::vector> all_masking_rules; - static std::shared_ptr sensitive_data_masker; + using MaskerMultiVersion = MultiVersion; + static MaskerMultiVersion sensitive_data_masker; public: SensitiveDataMasker(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix); @@ -55,8 +57,8 @@ public: /// setInstance is not thread-safe and should be called once in single-thread mode. /// https://github.com/ClickHouse/ClickHouse/pull/6810#discussion_r321183367 - static void setInstance(std::shared_ptr sensitive_data_masker_); - static std::shared_ptr getInstance(); + static void setInstance(std::unique_ptr&& sensitive_data_masker_); + static MaskerMultiVersion::Version getInstance(); /// Used in tests. void addMaskingRule(const std::string & name, const std::string & regexp_string, const std::string & replacement_string);