From 163a416366d8ac98171429a07f4fe460ea16af9d Mon Sep 17 00:00:00 2001 From: Amos Bird Date: Sat, 28 Aug 2021 11:43:43 +0800 Subject: [PATCH] Refactor --- src/Common/StackTrace.cpp | 22 ++++++++++++--------- src/Common/StackTrace.h | 3 ++- src/Common/SymbolIndex.cpp | 22 +++++++++++++-------- src/Common/SymbolIndex.h | 4 +++- src/Interpreters/InterpreterSystemQuery.cpp | 2 +- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/Common/StackTrace.cpp b/src/Common/StackTrace.cpp index 3fae1df0bfa..a9d4700490e 100644 --- a/src/Common/StackTrace.cpp +++ b/src/Common/StackTrace.cpp @@ -431,16 +431,20 @@ std::string StackTrace::toString(void ** frame_pointers_, size_t offset, size_t return toStringStatic(frame_pointers_copy, offset, size); } -std::string StackTrace::toStringStatic(const StackTrace::FramePointers & frame_pointers, size_t offset, size_t size, bool reload) +static SimpleCache & cacheInstance() +{ + static SimpleCache cache; + return cache; +} + +std::string StackTrace::toStringStatic(const StackTrace::FramePointers & frame_pointers, size_t offset, size_t size) { /// Calculation of stack trace text is extremely slow. /// We use simple cache because otherwise the server could be overloaded by trash queries. - static SimpleCache func_cached; - /// Reload cached stacktrace instead. - if (reload) - { - func_cached.drop(); - return ""; - } - return func_cached(frame_pointers, offset, size); + return cacheInstance()(frame_pointers, offset, size); +} + +void StackTrace::dropCache() +{ + cacheInstance().drop(); } diff --git a/src/Common/StackTrace.h b/src/Common/StackTrace.h index 7e6edabaf0d..62acb70563f 100644 --- a/src/Common/StackTrace.h +++ b/src/Common/StackTrace.h @@ -61,7 +61,8 @@ public: std::string toString() const; static std::string toString(void ** frame_pointers, size_t offset, size_t size); - static std::string toStringStatic(const FramePointers & frame_pointers, size_t offset, size_t size, bool reload = false); + static std::string toStringStatic(const FramePointers & frame_pointers, size_t offset, size_t size); + static void dropCache(); static void symbolize(const FramePointers & frame_pointers, size_t offset, size_t size, StackTrace::Frames & frames); void toStringEveryLine(std::function callback) const; diff --git a/src/Common/SymbolIndex.cpp b/src/Common/SymbolIndex.cpp index 72ce51cccd0..2d875b7042d 100644 --- a/src/Common/SymbolIndex.cpp +++ b/src/Common/SymbolIndex.cpp @@ -462,16 +462,22 @@ String SymbolIndex::getBuildIDHex() const return build_id_hex; } -MultiVersion::Version SymbolIndex::instance(bool reload) +MultiVersion & SymbolIndex::instanceImpl() { static MultiVersion instance(std::unique_ptr(new SymbolIndex)); - if (reload) - { - instance.set(std::unique_ptr(new SymbolIndex)); - /// Also reload stacktrace cache. - StackTrace::toStringStatic({}, 0, 0, true); - } - return instance.get(); + return instance; +} + +MultiVersion::Version SymbolIndex::instance() +{ + return instanceImpl().get(); +} + +void SymbolIndex::reload() +{ + instanceImpl().set(std::unique_ptr(new SymbolIndex)); + /// Also drop stacktrace cache. + StackTrace::dropCache(); } } diff --git a/src/Common/SymbolIndex.h b/src/Common/SymbolIndex.h index 65e446a7fc4..37862987bd2 100644 --- a/src/Common/SymbolIndex.h +++ b/src/Common/SymbolIndex.h @@ -22,7 +22,8 @@ protected: SymbolIndex() { update(); } public: - static MultiVersion::Version instance(bool reload = false); + static MultiVersion::Version instance(); + static void reload(); struct Symbol { @@ -60,6 +61,7 @@ private: Data data; void update(); + static MultiVersion & instanceImpl(); }; } diff --git a/src/Interpreters/InterpreterSystemQuery.cpp b/src/Interpreters/InterpreterSystemQuery.cpp index d4ac555add0..31c04a7b431 100644 --- a/src/Interpreters/InterpreterSystemQuery.cpp +++ b/src/Interpreters/InterpreterSystemQuery.cpp @@ -337,7 +337,7 @@ BlockIO InterpreterSystemQuery::execute() { #if defined(__ELF__) && !defined(__FreeBSD__) getContext()->checkAccess(AccessType::SYSTEM_RELOAD_SYMBOLS); - (void)SymbolIndex::instance(true); + SymbolIndex::reload(); break; #else throw Exception("SYSTEM RELOAD SYMBOLS is not supported on current platform", ErrorCodes::NOT_IMPLEMENTED);