Skip to content

Commit

Permalink
PS-9181: Fix masking_functions flusher thread loading
Browse files Browse the repository at this point in the history
https://perconadev.atlassian.net/browse/PS-9181

The component_masking_functions fails to start cache flusger thread
from within compoent init function.

Make query_cache loading lazy. This allows to postpone flusher
initialization till the moment when component and server are
complettely initialized.
  • Loading branch information
oleksandr-kachan committed Jun 21, 2024
1 parent eb758c5 commit 457882e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 63 deletions.
1 change: 0 additions & 1 deletion components/masking_functions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ set(DATAMASKING_SOURCES
include/masking_functions/primitive_singleton.hpp
include/masking_functions/query_builder_fwd.hpp
include/masking_functions/query_builder.hpp
include/masking_functions/query_cache_fwd.hpp
include/masking_functions/query_cache.hpp
include/masking_functions/random_string_generators.hpp
include/masking_functions/registration_routines.hpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#ifndef MASKING_FUNCTIONS_QUERY_CACHE_HPP
#define MASKING_FUNCTIONS_QUERY_CACHE_HPP

#include "masking_functions/query_cache_fwd.hpp"

#include <atomic>
#include <condition_variable>
#include <memory>
Expand All @@ -37,8 +35,7 @@ namespace masking_functions {
class query_cache {
public:
// passing unique_ptr by value to transfer ownership
query_cache(query_builder_ptr query_builder,
std::uint64_t flusher_interval_seconds);
query_cache();
query_cache(const query_cache &other) = delete;
query_cache(query_cache &&other) = delete;
query_cache &operator=(const query_cache &other) = delete;
Expand Down

This file was deleted.

13 changes: 0 additions & 13 deletions components/masking_functions/src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
#include "masking_functions/command_service_tuple.hpp"
#include "masking_functions/component_sys_variable_service_tuple.hpp"
#include "masking_functions/primitive_singleton.hpp"
#include "masking_functions/query_builder.hpp"
#include "masking_functions/query_cache.hpp"
#include "masking_functions/registration_routines.hpp"
#include "masking_functions/string_service_tuple.hpp"
#include "masking_functions/sys_vars.hpp"
Expand Down Expand Up @@ -164,13 +162,6 @@ static mysql_service_status_t component_init() {
return 1;
}

auto builder{std::make_unique<masking_functions::query_builder>(
masking_functions::get_dict_database_name())};
masking_functions::primitive_singleton<
masking_functions::query_cache_ptr>::instance() =
std::make_unique<masking_functions::query_cache>(
std::move(builder), masking_functions::get_flush_interval_seconds());

LogComponentErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"Component successfully initialized");
return 0;
Expand All @@ -179,10 +170,6 @@ static mysql_service_status_t component_init() {
static mysql_service_status_t component_deinit() {
int result = 0;

masking_functions::primitive_singleton<
masking_functions::query_cache_ptr>::instance()
.reset();

if (!masking_functions::unregister_udfs()) {
LogComponentErr(ERROR_LEVEL, ER_LOG_PRINTF_MSG, "Cannot unregister UDFs");
result = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ constexpr const char flusher_thd_psi_os_name[]{"mf_flusher"};

namespace masking_functions {

query_cache::query_cache(query_builder_ptr query_builder,
std::uint64_t flusher_interval_seconds)
: dict_query_builder_{std::move(query_builder)},
query_cache::query_cache()
: dict_query_builder_{std::make_unique<masking_functions::query_builder>(
masking_functions::get_dict_database_name())},
dict_cache_{},
dict_cache_mutex_{},
flusher_interval_seconds_{flusher_interval_seconds},
flusher_interval_seconds_{
masking_functions::get_flush_interval_seconds()},
is_flusher_stopped_{true} {
// we do not initialize m_dict_cache with create_dict_cache_internal() here
// as this constructor is called from the component initialization method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace {
using global_string_services = masking_functions::primitive_singleton<
masking_functions::string_service_tuple>;
using global_query_cache =
masking_functions::primitive_singleton<masking_functions::query_cache_ptr>;
masking_functions::primitive_singleton<masking_functions::query_cache>;

constexpr std::string_view masking_dictionaries_privilege_name =
"MASKING_DICTIONARIES_ADMIN";
Expand Down Expand Up @@ -965,16 +965,15 @@ class gen_blocklist_impl {
escape_string(make_charset_string_from_arg(ctx, 2));

{
auto sresult = global_query_cache::instance()->contains(cs_dict_a_escaped,
cs_term_escaped);
auto sresult = global_query_cache::instance().contains(cs_dict_a_escaped,
cs_term_escaped);

if (!sresult) {
return cs_term_escaped;
}
}

auto sresult =
global_query_cache::instance()->get_random(cs_dict_b_escaped);
auto sresult = global_query_cache::instance().get_random(cs_dict_b_escaped);

if (!sresult.empty()) {
masking_functions::charset_string utf8_result{
Expand Down Expand Up @@ -1020,7 +1019,7 @@ class gen_dictionary_impl {
const auto cs_dictionary_escaped =
escape_string(make_charset_string_from_arg(ctx, 0));
auto sresult =
global_query_cache::instance()->get_random(cs_dictionary_escaped);
global_query_cache::instance().get_random(cs_dictionary_escaped);

if (!sresult.empty()) {
return sresult;
Expand Down Expand Up @@ -1061,7 +1060,7 @@ class masking_dictionaries_flush_impl {

mysqlpp::udf_result_t<STRING_RESULT> calculate(const mysqlpp::udf_context &ctx
[[maybe_unused]]) {
global_query_cache::instance()->reload_cache();
global_query_cache::instance().reload_cache();

return "1";
}
Expand Down Expand Up @@ -1106,7 +1105,7 @@ class masking_dictionary_remove_impl {
const auto cs_dictionary_escaped =
escape_string(make_charset_string_from_arg(ctx, 0));

if (!global_query_cache::instance()->remove(cs_dictionary_escaped)) {
if (!global_query_cache::instance().remove(cs_dictionary_escaped)) {
return std::nullopt;
}

Expand Down Expand Up @@ -1160,8 +1159,8 @@ class masking_dictionary_term_add_impl {
const auto cs_term_escaped =
escape_string(make_charset_string_from_arg(ctx, 1));

if (!global_query_cache::instance()->insert(cs_dictionary_escaped,
cs_term_escaped)) {
if (!global_query_cache::instance().insert(cs_dictionary_escaped,
cs_term_escaped)) {
return std::nullopt;
}

Expand Down Expand Up @@ -1215,8 +1214,8 @@ class masking_dictionary_term_remove_impl {
const auto cs_term_escaped =
escape_string(make_charset_string_from_arg(ctx, 1));

if (!global_query_cache::instance()->remove(cs_dictionary_escaped,
cs_term_escaped)) {
if (!global_query_cache::instance().remove(cs_dictionary_escaped,
cs_term_escaped)) {
return std::nullopt;
}

Expand Down

0 comments on commit 457882e

Please sign in to comment.