Coverage Report

Created: 2025-05-06 07:25

/root/doris/be/src/util/once.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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/util/once.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <atomic>
24
#include <mutex>
25
#include <stdexcept>
26
27
#include "common/exception.h"
28
#include "olap/olap_common.h"
29
30
namespace doris {
31
32
// Utility class for implementing thread-safe call-once semantics.
33
//
34
// call() will return stored result regardless of whether the first invocation
35
// returns a success status or not.
36
//
37
// Example:
38
//   class Resource {
39
//   public:
40
//     Status init() {
41
//       _init_once.call([this] { return _do_init(); });
42
//     }
43
//
44
//     bool is_inited() const {
45
//       return _init_once.has_called() && _init_once.stored_result().ok();
46
//     }
47
//   private:
48
//     Status _do_init() { /* init logic here */ }
49
//     DorisCallOnce<Status> _init_once;
50
//   };
51
template <typename ReturnType>
52
class DorisCallOnce {
53
public:
54
34.0M
    DorisCallOnce() : _has_called(false) {}
55
56
    // this method is not exception safe, it will core when exception occurs in
57
    // callback method. I have tested the code https://en.cppreference.com/w/cpp/thread/call_once.
58
    // If the underlying `once_flag` has yet to be invoked, invokes the provided
59
    // lambda and stores its return value. Otherwise, returns the stored Status.
60
    // template <typename Fn>
61
    // ReturnType call(Fn fn) {
62
    //     std::call_once(_once_flag, [this, fn] {
63
    //         _status = fn();
64
    //         _has_called.store(true, std::memory_order_release);
65
    //     });
66
    //     return _status;
67
    // }
68
69
    // If exception occurs in the function, the call flag is set, if user call
70
    // it again, the same exception will be thrown.
71
    // It is different from std::call_once. This is because if a method is called once
72
    // some internal state is changed, it maybe not called again although exception
73
    // occurred.
74
    template <typename Fn>
75
511M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
511M
        if (has_called()) {
78
486M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
486M
            return _status;
82
486M
        }
83
25.7M
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
25.7M
        if (has_called()) {
86
3.03k
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
3.03k
            return _status;
90
3.03k
        }
91
25.7M
        try {
92
25.7M
            _status = fn();
93
25.7M
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
25.2M
        _has_called.store(true, std::memory_order_release);
102
25.2M
        return _status;
103
25.7M
    }
_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v212ColumnReader22set_dict_encoding_typeENS5_16DictEncodingTypeEEUlvE_EES1_T_
Line
Count
Source
75
3.61k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
3.61k
        if (has_called()) {
78
104
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
104
            return _status;
82
104
        }
83
3.51k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
3.51k
        if (has_called()) {
86
1
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
1
            return _status;
90
1
        }
91
3.51k
        try {
92
3.51k
            _status = fn();
93
3.51k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
3.51k
        _has_called.store(true, std::memory_order_release);
102
3.51k
        return _status;
103
3.51k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment21_load_pk_bloom_filterEvE3$_0EES1_T_
Line
Count
Source
75
14.9M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
14.9M
        if (has_called()) {
78
14.8M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
14.8M
            return _status;
82
14.8M
        }
83
36.3k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
36.3k
        if (has_called()) {
86
1
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
1
            return _status;
90
1
        }
91
36.3k
        try {
92
36.3k
            _status = fn();
93
36.3k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
29.5k
        _has_called.store(true, std::memory_order_release);
102
29.5k
        return _status;
103
36.3k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment10load_indexEvE3$_0EES1_T_
Line
Count
Source
75
25.0M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
25.0M
        if (has_called()) {
78
24.9M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
24.9M
            return _status;
82
24.9M
        }
83
86.1k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
86.1k
        if (has_called()) {
86
174
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
174
            return _status;
90
174
        }
91
85.9k
        try {
92
85.9k
            _status = fn();
93
85.9k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
84.5k
        _has_called.store(true, std::memory_order_release);
102
84.5k
        return _status;
103
85.9k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment27_create_column_readers_onceEPNS_20OlapReaderStatisticsEE3$_0EES1_T_
Line
Count
Source
75
444M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
444M
        if (has_called()) {
78
444M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
444M
            return _status;
82
444M
        }
83
550k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
550k
        if (has_called()) {
86
220
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
220
            return _status;
90
220
        }
91
550k
        try {
92
550k
            _status = fn();
93
550k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
62.6k
        _has_called.store(true, std::memory_order_release);
102
62.6k
        return _status;
103
550k
    }
segment.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v27Segment27new_inverted_index_iteratorERKNS_12TabletColumnEPKNS_11TabletIndexERKNS_18StorageReadOptionsEPSt10unique_ptrINS4_21InvertedIndexIteratorESt14default_deleteISG_EEE3$_0EES1_T_
Line
Count
Source
75
5.04k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
5.04k
        if (has_called()) {
78
0
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
0
            return _status;
82
0
        }
83
5.04k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
5.04k
        if (has_called()) {
86
8
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
8
            return _status;
90
8
        }
91
5.03k
        try {
92
5.03k
            _status = fn();
93
5.03k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
5.02k
        _has_called.store(true, std::memory_order_release);
102
5.02k
        return _status;
103
5.03k
    }
zone_map_index.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v218ZoneMapIndexReader4loadEbbE3$_0EES1_T_
Line
Count
Source
75
204k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
204k
        if (has_called()) {
78
169k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
169k
            return _status;
82
169k
        }
83
35.5k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
35.5k
        if (has_called()) {
86
2.31k
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
2.31k
            return _status;
90
2.31k
        }
91
33.2k
        try {
92
33.2k
            _status = fn();
93
33.2k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
33.4k
        _has_called.store(true, std::memory_order_release);
102
33.4k
        return _status;
103
33.2k
    }
ordinal_page_index.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v218OrdinalIndexReader4loadEbbE3$_0EES1_T_
Line
Count
Source
75
1.99M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
1.99M
        if (has_called()) {
78
1.51M
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
1.51M
            return _status;
82
1.51M
        }
83
484k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
484k
        if (has_called()) {
86
315
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
315
            return _status;
90
315
        }
91
484k
        try {
92
484k
            _status = fn();
93
484k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
484k
        _has_called.store(true, std::memory_order_release);
102
484k
        return _status;
103
484k
    }
bloom_filter_index_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v222BloomFilterIndexReader4loadEbbE3$_0EES1_T_
Line
Count
Source
75
29.6k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
29.6k
        if (has_called()) {
78
12
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
12
            return _status;
82
12
        }
83
29.6k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
29.6k
        if (has_called()) {
86
1
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
1
            return _status;
90
1
        }
91
29.6k
        try {
92
29.6k
            _status = fn();
93
29.6k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
29.6k
        _has_called.store(true, std::memory_order_release);
102
29.6k
        return _status;
103
29.6k
    }
bitmap_index_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_10segment_v217BitmapIndexReader4loadEbbE3$_0EES1_T_
Line
Count
Source
75
4
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
4
        if (has_called()) {
78
0
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
0
            return _status;
82
0
        }
83
4
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
4
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
4
        try {
92
4
            _status = fn();
93
4
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
4
        _has_called.store(true, std::memory_order_release);
102
4
        return _status;
103
4
    }
beta_rowset_reader.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_16BetaRowsetReader19_init_iterator_onceEvE3$_0EES1_T_
Line
Count
Source
75
24.7M
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
24.7M
        if (has_called()) {
78
496k
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
496k
            return _status;
82
496k
        }
83
24.2M
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
24.2M
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
24.2M
        try {
92
24.2M
            _status = fn();
93
24.2M
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
24.2M
        _has_called.store(true, std::memory_order_release);
102
24.2M
        return _status;
103
24.2M
    }
tablet.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_6Tablet4initEvE3$_0EES1_T_
Line
Count
Source
75
218k
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
218k
        if (has_called()) {
78
226
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
226
            return _status;
82
226
        }
83
218k
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
218k
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
218k
        try {
92
218k
            _status = fn();
93
218k
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
218k
        _has_called.store(true, std::memory_order_release);
102
218k
        return _status;
103
218k
    }
load_path_mgr.cpp:_ZN5doris13DorisCallOnceINS_6StatusEE4callIZNS_11LoadPathMgr12allocate_dirERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESC_PSA_E3$_0EES1_T_
Line
Count
Source
75
13
    ReturnType call(Fn fn) {
76
        // Avoid lock to improve performance
77
13
        if (has_called()) {
78
11
            if (_eptr) {
79
0
                std::rethrow_exception(_eptr);
80
0
            }
81
11
            return _status;
82
11
        }
83
2
        std::lock_guard l(_flag_lock);
84
        // should check again because maybe another thread call successfully.
85
2
        if (has_called()) {
86
0
            if (_eptr) {
87
0
                std::rethrow_exception(_eptr);
88
0
            }
89
0
            return _status;
90
0
        }
91
2
        try {
92
2
            _status = fn();
93
2
        } catch (...) {
94
            // Save the exception for next call.
95
0
            _eptr = std::current_exception();
96
0
            _has_called.store(true, std::memory_order_release);
97
0
            std::rethrow_exception(_eptr);
98
0
        }
99
        // This memory order make sure both status and eptr is set
100
        // and will be seen in another thread.
101
2
        _has_called.store(true, std::memory_order_release);
102
2
        return _status;
103
2
    }
104
105
    // Has to pay attention to memory order
106
    // see https://en.cppreference.com/w/cpp/atomic/memory_order
107
    // Return whether `call` has been invoked or not.
108
679M
    bool has_called() const {
109
        // std::memory_order_acquire here and std::memory_order_release in
110
        // init(), taken together, mean that threads can safely synchronize on
111
        // _has_called.
112
679M
        return _has_called.load(std::memory_order_acquire);
113
679M
    }
114
115
    // Return the stored result. The result is only meaningful when `has_called() == true`.
116
63.3M
    ReturnType stored_result() const {
117
63.3M
        if (!has_called()) {
118
            // Could not return status if the method not called.
119
0
            throw std::exception();
120
0
        }
121
63.3M
        if (_eptr) {
122
1
            std::rethrow_exception(_eptr);
123
1
        }
124
63.3M
        return _status;
125
63.3M
    }
126
127
private:
128
    std::atomic<bool> _has_called;
129
    // std::once_flag _once_flag;
130
    std::mutex _flag_lock;
131
    std::exception_ptr _eptr;
132
    ReturnType _status;
133
};
134
135
} // namespace doris