Coverage Report

Created: 2025-04-15 13:12

/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
1
    Exception() : _code(ErrorCode::OK) {}
38
    Exception(int code, const std::string_view& msg);
39
7
    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
549
            : 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
191
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJmRmEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
70
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRmS2_S2_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_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRlPKciiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRiPKcttEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRmPKcjjEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRiPKciiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRmPKciiEEEiRKSt17basic_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)...)) {}
_ZN5doris9ExceptionC2IJRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
40
            : 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)...)) {}
_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)...)) {}
_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)...)) {}
_ZN5doris9ExceptionC2IJRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
14
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
Unexecuted instantiation: _ZN5doris9ExceptionC2IJRNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEEiRKSt17basic_string_viewIcS5_EDpOT_
Unexecuted instantiation: _ZN5doris9ExceptionC2IJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_S7_S7_EEEiRKSt17basic_string_viewIcS5_EDpOT_
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)...)) {}
_ZN5doris9ExceptionC2IJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_S7_EEEiRKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
44
1
            : Exception(code, fmt::format(fmt, std::forward<Args>(args)...)) {}
_ZN5doris9ExceptionC2IJRiEEEiRKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
44
1
            : 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: _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
48
    const std::string& to_string() const;
49
50
1
    const char* what() const noexcept override { return to_string().c_str(); }
51
52
    Status to_status() const { return {code(), _err_msg->_msg, _err_msg->_stack}; }
53
54
private:
55
    int _code;
56
    struct ErrMsg {
57
        std::string _msg;
58
        std::string _stack;
59
    };
60
    std::unique_ptr<ErrMsg> _err_msg;
61
    mutable std::string _cache_string;
62
};
63
64
3
inline const std::string& Exception::to_string() const {
65
3
    if (!_cache_string.empty()) {
66
0
        return _cache_string;
67
0
    }
68
3
    fmt::memory_buffer buf;
69
3
    fmt::format_to(buf, "[E{}] {}", _code, _err_msg ? _err_msg->_msg : "");
70
3
    if (_err_msg && !_err_msg->_stack.empty()) {
71
2
        fmt::format_to(buf, "\n{}", _err_msg->_stack);
72
2
    }
73
3
    _cache_string = fmt::to_string(buf);
74
3
    return _cache_string;
75
3
}
76
77
} // namespace doris
78
79
#define RETURN_IF_CATCH_EXCEPTION(stmt)                                                          \
80
125
    do {                                                                                         \
81
125
        try {                                                                                    \
82
125
            doris::enable_thread_catch_bad_alloc++;                                              \
83
125
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
_ZZN5doris10vectorized12MutableBlock5mergeINS0_5BlockEEENS_6StatusEOT_ENKUlvE_clEv
Line
Count
Source
83
69
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
_ZZN5doris10vectorized12MutableBlock5mergeIRNS0_5BlockEEENS_6StatusEOT_ENKUlvE_clEv
Line
Count
Source
83
47
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
_ZZN5doris10ByteBuffer8allocateEmPSt10shared_ptrIS0_EENKUlvE_clEv
Line
Count
Source
83
9
            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$_1clEv
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
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_5clEv
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_6clEv
84
125
            { stmt; }                                                                            \
85
0
        } catch (const doris::Exception& e) {                                                    \
86
0
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
87
0
                return Status::MemoryLimitExceeded(fmt::format(                                  \
88
0
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
89
0
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
90
0
            }                                                                                    \
91
0
            return Status::Error<false>(e.code(), e.to_string());                                \
92
0
        }                                                                                        \
93
125
    } while (0)
94
95
#define RETURN_IF_ERROR_OR_CATCH_EXCEPTION(stmt)                                                 \
96
7
    do {                                                                                         \
97
7
        try {                                                                                    \
98
7
            doris::enable_thread_catch_bad_alloc++;                                              \
99
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
99
7
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader19get_next_block_implEPNS0_5BlockEPmPbENK3$_2clEv
Unexecuted instantiation: orc_convert_to_orc_literal_test.cpp:_ZZN5doris10vectorized9OrcReader6filterERN3orc17ColumnVectorBatchEPttPvENK3$_0clEv
100
7
            {                                                                                    \
101
7
                Status _status_ = (stmt);                                                        \
102
7
                if (UNLIKELY(!_status_.ok())) {                                                  \
103
0
                    return _status_;                                                             \
104
0
                }                                                                                \
105
7
            }                                                                                    \
106
7
        } catch (const doris::Exception& e) {                                                    \
107
0
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
108
0
                return Status::MemoryLimitExceeded(fmt::format(                                  \
109
0
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
110
0
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
111
0
            }                                                                                    \
112
0
            return Status::Error<false>(e.code(), e.to_string());                                \
113
0
        }                                                                                        \
114
7
    } while (0)
115
116
#define ASSIGN_STATUS_IF_CATCH_EXCEPTION(stmt, status_)                                          \
117
    do {                                                                                         \
118
        try {                                                                                    \
119
            doris::enable_thread_catch_bad_alloc++;                                              \
120
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
121
            { stmt; }                                                                            \
122
        } catch (const doris::Exception& e) {                                                    \
123
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
124
                status_ = Status::MemoryLimitExceeded(fmt::format(                               \
125
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
126
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
127
            } else {                                                                             \
128
                status_ = e.to_status();                                                         \
129
            }                                                                                    \
130
        }                                                                                        \
131
    } while (0);
132
133
#define HANDLE_EXCEPTION_IF_CATCH_EXCEPTION(stmt, exception_handler)                             \
134
    do {                                                                                         \
135
        try {                                                                                    \
136
            doris::enable_thread_catch_bad_alloc++;                                              \
137
            Defer defer {[&]() { doris::enable_thread_catch_bad_alloc--; }};                     \
138
            {                                                                                    \
139
                Status _status_ = (stmt);                                                        \
140
                if (UNLIKELY(!_status_.ok())) {                                                  \
141
                    exception_handler(doris::Exception());                                       \
142
                    return _status_;                                                             \
143
                }                                                                                \
144
            }                                                                                    \
145
        } catch (const doris::Exception& e) {                                                    \
146
            exception_handler(e);                                                                \
147
            if (e.code() == doris::ErrorCode::MEM_ALLOC_FAILED) {                                \
148
                return Status::MemoryLimitExceeded(fmt::format(                                  \
149
                        "PreCatch error code:{}, {}, __FILE__:{}, __LINE__:{}, __FUNCTION__:{}", \
150
                        e.code(), e.to_string(), __FILE__, __LINE__, __PRETTY_FUNCTION__));      \
151
            }                                                                                    \
152
            return Status::Error<false>(e.code(), e.to_string());                                \
153
        }                                                                                        \
154
    } while (0);