From af1864f5448334dca9ef236e0a54eddf4f3b4532 Mon Sep 17 00:00:00 2001 From: drrtuy Date: Fri, 18 Apr 2025 22:51:15 +0000 Subject: [PATCH] feat(memory): custom OOM exception --- dbcon/joblist/jobstep.cpp | 5 +++ utils/common/countingallocator.h | 7 ++-- utils/loggingcpp/ErrorMessage.txt | 3 +- utils/loggingcpp/outofmemoryexcept.h | 48 ++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 utils/loggingcpp/outofmemoryexcept.h diff --git a/dbcon/joblist/jobstep.cpp b/dbcon/joblist/jobstep.cpp index 18e20f4bd..b917262ef 100644 --- a/dbcon/joblist/jobstep.cpp +++ b/dbcon/joblist/jobstep.cpp @@ -217,6 +217,11 @@ void JobStep::handleException(std::exception_ptr e, const int errorCode, const u { std::rethrow_exception(e); } + catch (logging::OutOfMemoryExcept& exc) + { + std::cerr << methodName << " caught a OutOfMemory exception. " << std::endl; + catchHandler(methodName + " caught " + exc.what(), errorCode, fErrorInfo, fSessionId); + } // Add it here for now to handle potential bad_alloc exceptions catch (std::bad_alloc& exc) { diff --git a/utils/common/countingallocator.h b/utils/common/countingallocator.h index 85188e627..516943ae6 100644 --- a/utils/common/countingallocator.h +++ b/utils/common/countingallocator.h @@ -22,7 +22,10 @@ #include #include #include + #include "atomicops.h" +#include "errorids.h" +#include "outofmemoryexcept.h" namespace allocators { @@ -80,10 +83,10 @@ class CountingAllocator if (currentGlobalMemoryLimit < memoryLimitLowerBound_) { atomicops::atomicAddRef(*memoryLimit_, lastMemoryLimitCheckpointDiff); - throw std::bad_alloc(); + throw logging::OutOfMemoryExcept(logging::ERR_OUT_OF_MEMORY); } lastMemoryLimitCheckpoint_ += lastMemoryLimitCheckpointDiff; - } + } currentLocalMemoryUsage_ += sizeChange; } diff --git a/utils/loggingcpp/ErrorMessage.txt b/utils/loggingcpp/ErrorMessage.txt index 75092e1ba..0026f62d8 100755 --- a/utils/loggingcpp/ErrorMessage.txt +++ b/utils/loggingcpp/ErrorMessage.txt @@ -53,7 +53,7 @@ 2012 ERR_EXTENT_DISK_SPACE Not able to add extent; adding extent would exceed max file system disk usage. %1% 2013 ERR_NON_NUMERIC_DATA Not able to convert the input data; Data value %1% does not match data type. 2014 ERR_JOBLIST Error in making/executing job steps in DML -2015 ERR_ORDERBY_TOO_BIG Sorting length exceeded. Session variable max_length_for_sort_data needs to be set higher. +2015 ERR_ORDERBY_TOO_BIG Memory limit is exceeded running ORDER BY. 2016 ERR_NON_SUPPORT_GROUP_BY Non supported item %1% on the GROUP BY list. 2017 ERR_IN_DELIVERY ExeMgr failed to deliver result set to connector. 2018 ERR_LIMIT_TOO_BIG Not enough memory to process the LIMIT. Consider raising TotalUmMemory or reducing memory usage. @@ -111,6 +111,7 @@ 2061 ERR_NOT_SUPPORTED_GROUPBY_ORDERBY_EXPRESSION %1% is not in GROUP BY clause, not a column or an expression that contains function. 2063 ERR_TNS_DISTINCT_IS_TOO_BIG DISTINCT memory limit is exceeded whilst running TNS step. +2064 ERR_OUT_OF_MEMORY Out of memory. # Sub-query errors 3001 ERR_NON_SUPPORT_SUB_QUERY_TYPE This subquery type is not supported yet. diff --git a/utils/loggingcpp/outofmemoryexcept.h b/utils/loggingcpp/outofmemoryexcept.h new file mode 100644 index 000000000..c0f06a700 --- /dev/null +++ b/utils/loggingcpp/outofmemoryexcept.h @@ -0,0 +1,48 @@ +/* Copyright (C) 2024 MariaDB Corporation + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; version 2 of + the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. */ + +#pragma once + +#include +#include +#include +#include "idberrorinfo.h" + +namespace logging +{ + +/** @brief Exception class for out of memory conditions with custom error message + * Extends std::bad_alloc to provide IDB-specific error messages + */ +class OutOfMemoryExcept : public std::bad_alloc +{ + public: + explicit OutOfMemoryExcept(uint16_t code) + : fMessage(IDBErrorInfo::instance()->errorMsg(code)) + { + } + + const char* what() const noexcept override + { + return fMessage.c_str(); + } + + private: + std::string fMessage; +}; + +} // namespace logging