Coverage Report

Created: 2025-06-13 18:17

/root/doris/be/src/common/exception.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <fmt/format.h>
21
#include <gen_cpp/Status_types.h>
22
23
#include <exception>
24
#include <memory>
25
#include <string>
26
#include <string_view>
27
#include <utility>
28
29
#include "common/status.h"
30
#include "util/defer_op.h"
31
32
namespace doris {
33
34
inline thread_local int enable_thread_catch_bad_alloc = 0;
35
class Exception : public std::exception {
36
public:
37
2
    Exception() : _code(ErrorCode::OK) {}
38
    Exception(int code, const std::string_view& msg);
39
13
    Exception(const Status& status) : Exception(status.code(), status.msg()) {}
40
41
    // Format message with fmt::format, like the logging functions.
42
    template <typename... Args>
43
    Exception(int code, const std::string_view& fmt, Args&&... args)
44
433
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRA4_KcEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
2
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRmEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
65
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJmRmEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
67
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRmS2_S2_EEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
2
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
Unexecuted instantiation: _ZN5doris9ExceptionC2IJPKcjjEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
_ZN5doris9ExceptionC2IJRiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRlPKciiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRiPKcttEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
_ZN5doris9ExceptionC2IJRNS_11HashKeyTypeEEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
4
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRKjmEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
16
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRjS2_EEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
7
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
21
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRmPKcjjEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRiPKciiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRmPKciiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRimEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
_ZN5doris9ExceptionC2IJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
7
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJPKcEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_S7_EEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
41
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRmS2_EEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
86
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRjEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEiRKSt17basic_string_viewIcS5_EDpOT_
_ZN5doris9ExceptionC2IJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_S7_S7_EEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
Unexecuted instantiation: _ZN5doris9ExceptionC2IJPKcmmEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
_ZN5doris9ExceptionC2IJRmS2_RKmEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
86
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKjEEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
22
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRSt17basic_string_viewIcSt11char_traitsIcEEEEEiRKS5_DpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRiPKcllEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRlPKcllEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRnPKcllEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRKPKcEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRKiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRNS_10vectorized12RoundingModeEEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
45
46
8
    int code() const { return _code; }
47
    std::string message() const { return _err_msg ? _err_msg->_msg : ""; }
48
49
    const std::string& to_string() const;
50
51
11
    const char* what() const noexcept override { return to_string().c_str(); }
52
53
    Status to_status() const { return {code(), _err_msg->_msg, _err_msg->_stack}; }
54
55
private:
56
    int _code;
57
    struct ErrMsg {
58
        std::string _msg;
59
        std::string _stack;
60
    };
61
    std::unique_ptr<ErrMsg> _err_msg;
62
    mutable std::string _cache_string;
63
};
64
65
13
inline const std::string& Exception::to_string() const {
66
13
    if (!_cache_string.empty()) {
67
0
        return _cache_string;
68
0
    }
69
13
    fmt::memory_buffer buf;
70
13
    fmt::format_to(buf, "[E{}] {}", _code, _err_msg ? _err_msg->_msg : "");
71
13
    if (_err_msg && !_err_msg->_stack.empty()) {
72
8
        fmt::format_to(buf, "\n{}", _err_msg->_stack);
73
8
    }
74
13
    _cache_string = fmt::to_string(buf);
75
13
    return _cache_string;
76
13
}
77
78
} // namespace doris
79
80
#define RETURN_IF_CATCH_EXCEPTION(stmt)                                                          \
81
52.1k
    do {                                                                                         \
82
52.1k
        try {                                                                                    \
83
52.1k
            doris::enable_thread_catch_bad_alloc++;                                              \
84
52.1k
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
_ZZN5doris10vectorized12MutableBlock5mergeINS0_5BlockEEENS_6StatusEOT_ENKUlvE_clEv
Line
Count
Source
84
59
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
_ZZN5doris10vectorized12MutableBlock5mergeIRNS0_5BlockEEENS_6StatusEOT_ENKUlvE_clEv
Line
Count
Source
84
53
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
_ZZN5doris10ByteBuffer8allocateEmPSt10shared_ptrIS0_EENKUlvE_clEv
Line
Count
Source
84
9
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
_ZZN5doris10vectorized12MutableBlock21merge_ignore_overflowINS0_5BlockEEENS_6StatusEOT_ENKUlvE_clEv
Line
Count
Source
84
52.0k
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_0clEv
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_2clEv
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_3clEv
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_4clEv
85
52.1k
            { stmt; }                                                                            \
86
0
        } catch (const doris::Exception& e) {                                                    \
87
0
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
88
0
                return Status::MemoryLimitExceeded(fmt::format(                                  \
89
0
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
90
0
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
91
0
            }                                                                                    \
92
0
            return Status::Error<false>(e.code(), e.to_string());                                \
93
0
        }                                                                                        \
94
52.1k
    } while (0)
95
96
#define RETURN_IF_ERROR_OR_CATCH_EXCEPTION(stmt)                                                 \
97
7
    do {                                                                                         \
98
7
        try {                                                                                    \
99
7
            doris::enable_thread_catch_bad_alloc++;                                              \
100
7
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
Unexecuted instantiation: _ZZN5doris10vectorized11MockChannel17send_remote_blockEOSt10unique_ptrINS_6PBlockESt14default_deleteIS3_EEbENKUlvE_clEv
_ZZN5doris10vectorized11MockChannel20send_broadcast_blockERSt10shared_ptrINS0_21BroadcastPBlockHolderEEbENKUlvE_clEv
Line
Count
Source
100
7
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_1clEv
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader6filterERN3orc17ColumnVectorBatchEPttPvENK3$_0clEv
101
7
            {                                                                                    \
102
7
                Status _status_ = (stmt);                                                        \
103
7
                if (UNLIKELY(!_status_.ok())) {                                                  \
104
0
                    return _status_;                                                             \
105
0
                }                                                                                \
106
7
            }                                                                                    \
107
7
        } catch (const doris::Exception& e) {                                                    \
108
0
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
109
0
                return Status::MemoryLimitExceeded(fmt::format(                                  \
110
0
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
111
0
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
112
0
            }                                                                                    \
113
0
            return Status::Error<false>(e.code(), e.to_string());                                \
114
0
        }                                                                                        \
115
7
    } while (0)
116
117
#define ASSIGN_STATUS_IF_CATCH_EXCEPTION(stmt, status_)                                          \
118
    do {                                                                                         \
119
        try {                                                                                    \
120
            doris::enable_thread_catch_bad_alloc++;                                              \
121
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
122
            { stmt; }                                                                            \
123
        } catch (const doris::Exception& e) {                                                    \
124
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
125
                status_ = Status::MemoryLimitExceeded(fmt::format(                               \
126
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
127
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
128
            } else {                                                                             \
129
                status_ = e.to_status();                                                         \
130
            }                                                                                    \
131
        }                                                                                        \
132
    } while (0);
133
134
#define HANDLE_EXCEPTION_IF_CATCH_EXCEPTION(stmt, exception_handler)                             \
135
    do {                                                                                         \
136
        try {                                                                                    \
137
            doris::enable_thread_catch_bad_alloc++;                                              \
138
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
139
            {                                                                                    \
140
                Status _status_ = (stmt);                                                        \
141
                if (UNLIKELY(!_status_.ok())) {                                                  \
142
                    exception_handler(doris::Exception());                                       \
143
                    return _status_;                                                             \
144
                }                                                                                \
145
            }                                                                                    \
146
        } catch (const doris::Exception& e) {                                                    \
147
            exception_handler(e);                                                                \
148
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
149
                return Status::MemoryLimitExceeded(fmt::format(                                  \
150
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
151
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
152
            }                                                                                    \
153
            return Status::Error<false>(e.code(), e.to_string());                                \
154
        }                                                                                        \
155
    } while (0);