Coverage Report

Created: 2026-07-29 16:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/io/fs/stream_load_pipe.cpp
Line
Count
Source
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
#include "io/fs/stream_load_pipe.h"
19
20
#include <glog/logging.h>
21
22
#include <algorithm>
23
#include <ostream>
24
#include <utility>
25
26
#include "common/compiler_util.h" // IWYU pragma: keep
27
#include "common/status.h"
28
#include "core/custom_allocator.h"
29
#include "runtime/exec_env.h"
30
#include "runtime/thread_context.h"
31
#include "util/bit_util.h"
32
33
namespace doris {
34
namespace io {
35
struct IOContext;
36
37
StreamLoadPipe::StreamLoadPipe(size_t max_buffered_bytes, size_t min_chunk_size,
38
                               int64_t total_length, bool use_proto)
39
6.27k
        : _buffered_bytes(0),
40
6.27k
          _proto_buffered_bytes(0),
41
6.27k
          _max_buffered_bytes(max_buffered_bytes),
42
6.27k
          _min_chunk_size(min_chunk_size),
43
6.27k
          _total_length(total_length),
44
6.27k
          _use_proto(use_proto) {}
45
46
6.26k
StreamLoadPipe::~StreamLoadPipe() {
47
6.53k
    while (!_buf_queue.empty()) {
48
265
        _buf_queue.pop_front();
49
265
    }
50
6.26k
}
51
52
Status StreamLoadPipe::read_at_impl(size_t /*offset*/, Slice result, size_t* bytes_read,
53
12.2k
                                    const IOContext* /*io_ctx*/) {
54
12.2k
    *bytes_read = 0;
55
12.2k
    size_t bytes_req = result.size;
56
12.2k
    char* to = result.data;
57
12.2k
    if (UNLIKELY(bytes_req == 0)) {
58
12
        return Status::OK();
59
12
    }
60
484k
    while (*bytes_read < bytes_req) {
61
483k
        std::unique_lock<std::mutex> l(_lock);
62
733k
        while (!_cancelled && !_finished && _buf_queue.empty()) {
63
249k
            _get_cond.wait(l);
64
249k
        }
65
        // cancelled
66
483k
        if (_cancelled) {
67
1
            return Status::Cancelled("cancelled: {}", _cancelled_reason);
68
1
        }
69
        // finished
70
483k
        if (_buf_queue.empty()) {
71
11.5k
            DCHECK(_finished);
72
            // break the while loop
73
11.5k
            bytes_req = *bytes_read;
74
11.5k
            return Status::OK();
75
11.5k
        }
76
472k
        auto buf = _buf_queue.front();
77
472k
        int64_t copy_size = std::min(bytes_req - *bytes_read, buf->remaining());
78
472k
        buf->get_bytes(to + *bytes_read, copy_size);
79
472k
        *bytes_read += copy_size;
80
472k
        if (!buf->has_remaining()) {
81
471k
            _buf_queue.pop_front();
82
471k
            _buffered_bytes -= buf->limit;
83
471k
            _put_cond.notify_one();
84
471k
        }
85
472k
    }
86
18.4E
    DCHECK(*bytes_read == bytes_req)
87
18.4E
            << "*bytes_read=" << *bytes_read << ", bytes_req=" << bytes_req;
88
653
    return Status::OK();
89
12.2k
}
90
91
// If _total_length == -1, this should be a Kafka routine load task or stream load with chunked transfer HTTP request,
92
// just get the next buffer directly from the buffer queue, because one buffer contains a complete piece of data.
93
// Otherwise, this should be a stream load task that needs to read the specified amount of data.
94
803
Status StreamLoadPipe::read_one_message(DorisUniqueBufferPtr<uint8_t>* data, size_t* length) {
95
803
    if (_total_length < -1) {
96
0
        return Status::InternalError("invalid, _total_length is: {}", _total_length);
97
803
    } else if (_total_length == 0) {
98
        // no data
99
0
        *length = 0;
100
0
        return Status::OK();
101
0
    }
102
103
803
    if (_total_length == -1) {
104
636
        return _read_next_buffer(data, length);
105
636
    }
106
107
    // _total_length > 0, read the entire data
108
167
    *data = make_unique_buffer<uint8_t>(_total_length);
109
167
    Slice result(data->get(), _total_length);
110
167
    Status st = read_at(0, result, length);
111
167
    return st;
112
803
}
113
114
370
Status StreamLoadPipe::append_and_flush(const char* data, size_t size, size_t proto_byte_size) {
115
370
    SCOPED_ATTACH_TASK(ExecEnv::GetInstance()->stream_load_pipe_tracker());
116
370
    ByteBufferPtr buf;
117
370
    RETURN_IF_ERROR(ByteBuffer::allocate(BitUtil::RoundUpToPowerOfTwo(size + 1), &buf));
118
370
    buf->put_bytes(data, size);
119
370
    buf->flip();
120
370
    return _append(buf, proto_byte_size);
121
370
}
122
123
216
Status StreamLoadPipe::append(std::unique_ptr<PDataRow>&& row) {
124
216
    PDataRow* row_ptr = row.get();
125
216
    {
126
216
        std::unique_lock<std::mutex> l(_lock);
127
216
        _data_row_ptrs.emplace_back(std::move(row));
128
216
    }
129
216
    return append_and_flush(reinterpret_cast<char*>(&row_ptr), sizeof(row_ptr),
130
216
                            sizeof(PDataRow*) + row_ptr->ByteSizeLong());
131
216
}
132
133
1.63k
Status StreamLoadPipe::append(const char* data, size_t size) {
134
1.63k
    size_t pos = 0;
135
1.63k
    if (_write_buf != nullptr) {
136
1.47k
        if (size < _write_buf->remaining()) {
137
1.47k
            _write_buf->put_bytes(data, size);
138
1.47k
            return Status::OK();
139
1.47k
        } else {
140
0
            pos = _write_buf->remaining();
141
0
            _write_buf->put_bytes(data, pos);
142
143
0
            _write_buf->flip();
144
0
            RETURN_IF_ERROR(_append(_write_buf));
145
0
            _write_buf.reset();
146
0
        }
147
1.47k
    }
148
    // need to allocate a new chunk, min chunk is 64k
149
164
    size_t chunk_size = std::max(_min_chunk_size, size - pos);
150
164
    chunk_size = BitUtil::RoundUpToPowerOfTwo(chunk_size);
151
164
    SCOPED_ATTACH_TASK(ExecEnv::GetInstance()->stream_load_pipe_tracker());
152
164
    RETURN_IF_ERROR(ByteBuffer::allocate(chunk_size, &_write_buf));
153
164
    _write_buf->put_bytes(data + pos, size - pos);
154
164
    return Status::OK();
155
164
}
156
157
472k
Status StreamLoadPipe::append(const ByteBufferPtr& buf) {
158
472k
    if (_write_buf != nullptr) {
159
0
        _write_buf->flip();
160
0
        RETURN_IF_ERROR(_append(_write_buf));
161
0
        _write_buf.reset();
162
0
    }
163
472k
    return _append(buf);
164
472k
}
165
166
// read the next buffer from _buf_queue
167
636
Status StreamLoadPipe::_read_next_buffer(DorisUniqueBufferPtr<uint8_t>* data, size_t* length) {
168
636
    std::unique_lock<std::mutex> l(_lock);
169
825
    while (!_cancelled && !_finished && _buf_queue.empty()) {
170
189
        _get_cond.wait(l);
171
189
    }
172
    // cancelled
173
636
    if (_cancelled) {
174
8
        return Status::Cancelled("cancelled: {}", _cancelled_reason);
175
8
    }
176
    // finished
177
628
    if (_buf_queue.empty()) {
178
119
        DCHECK(_finished);
179
119
        data->reset();
180
119
        *length = 0;
181
119
        return Status::OK();
182
119
    }
183
509
    auto buf = _buf_queue.front();
184
509
    *length = buf->remaining();
185
509
    *data = make_unique_buffer<uint8_t>(*length);
186
509
    buf->get_bytes((char*)(data->get()), *length);
187
509
    _buf_queue.pop_front();
188
509
    _buffered_bytes -= buf->limit;
189
509
    if (_use_proto) {
190
216
        auto row_ptr = std::move(_data_row_ptrs.front());
191
216
        _proto_buffered_bytes -= (sizeof(PDataRow*) + row_ptr->GetCachedSize());
192
216
        _data_row_ptrs.pop_front();
193
        // PlainBinaryLineReader will hold the PDataRow
194
216
        row_ptr.release();
195
216
    }
196
509
    _put_cond.notify_one();
197
509
    return Status::OK();
198
628
}
199
200
472k
Status StreamLoadPipe::_append(const ByteBufferPtr& buf, size_t proto_byte_size) {
201
472k
    {
202
472k
        std::unique_lock<std::mutex> l(_lock);
203
        // if _buf_queue is empty, we append this buf without size check
204
472k
        if (_use_proto) {
205
216
            while (!_cancelled && !_buf_queue.empty() &&
206
216
                   (_proto_buffered_bytes + proto_byte_size > _max_buffered_bytes)) {
207
0
                _put_cond.wait(l);
208
0
            }
209
472k
        } else {
210
472k
            while (!_cancelled && !_buf_queue.empty() &&
211
472k
                   _buffered_bytes + buf->remaining() > _max_buffered_bytes) {
212
182
                _put_cond.wait(l);
213
182
            }
214
472k
        }
215
472k
        if (_cancelled) {
216
3
            return Status::Cancelled("cancelled: {}", _cancelled_reason);
217
3
        }
218
472k
        _buf_queue.push_back(buf);
219
472k
        if (_use_proto) {
220
216
            _proto_buffered_bytes += proto_byte_size;
221
472k
        } else {
222
472k
            _buffered_bytes += buf->remaining();
223
472k
        }
224
472k
    }
225
0
    _get_cond.notify_one();
226
472k
    return Status::OK();
227
472k
}
228
229
// called when producer finished
230
6.02k
Status StreamLoadPipe::finish() {
231
6.02k
    if (_write_buf != nullptr) {
232
136
        _write_buf->flip();
233
136
        RETURN_IF_ERROR(_append(_write_buf));
234
136
        _write_buf.reset();
235
136
    }
236
6.02k
    {
237
6.02k
        std::lock_guard<std::mutex> l(_lock);
238
6.02k
        _finished = true;
239
6.02k
    }
240
6.02k
    _get_cond.notify_all();
241
6.02k
    return Status::OK();
242
6.02k
}
243
244
// called when producer/consumer failed
245
7.22k
void StreamLoadPipe::cancel(const std::string& reason) {
246
7.22k
    {
247
7.22k
        std::lock_guard<std::mutex> l(_lock);
248
7.22k
        _cancelled = true;
249
7.22k
        _cancelled_reason = reason;
250
7.22k
    }
251
7.22k
    _get_cond.notify_all();
252
7.22k
    _put_cond.notify_all();
253
7.22k
}
254
255
0
TUniqueId StreamLoadPipe::calculate_pipe_id(const UniqueId& query_id, int32_t fragment_id) {
256
0
    TUniqueId pipe_id;
257
0
    pipe_id.lo = query_id.lo + fragment_id;
258
0
    pipe_id.hi = query_id.hi;
259
0
    return pipe_id;
260
0
}
261
262
41
size_t StreamLoadPipe::current_capacity() {
263
41
    std::unique_lock<std::mutex> l(_lock);
264
41
    if (_use_proto) {
265
0
        return _proto_buffered_bytes;
266
41
    } else {
267
41
        return _buffered_bytes;
268
41
    }
269
41
}
270
271
} // namespace io
272
} // namespace doris