Coverage Report

Created: 2026-05-15 02:10

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
1.31k
        : _buffered_bytes(0),
40
1.31k
          _proto_buffered_bytes(0),
41
1.31k
          _max_buffered_bytes(max_buffered_bytes),
42
1.31k
          _min_chunk_size(min_chunk_size),
43
1.31k
          _total_length(total_length),
44
1.31k
          _use_proto(use_proto) {}
45
46
1.31k
StreamLoadPipe::~StreamLoadPipe() {
47
1.33k
    while (!_buf_queue.empty()) {
48
25
        _buf_queue.pop_front();
49
25
    }
50
1.31k
}
51
52
Status StreamLoadPipe::read_at_impl(size_t /*offset*/, Slice result, size_t* bytes_read,
53
2.50k
                                    const IOContext* /*io_ctx*/) {
54
2.50k
    *bytes_read = 0;
55
2.50k
    size_t bytes_req = result.size;
56
2.50k
    char* to = result.data;
57
2.50k
    if (UNLIKELY(bytes_req == 0)) {
58
0
        return Status::OK();
59
0
    }
60
164k
    while (*bytes_read < bytes_req) {
61
164k
        std::unique_lock<std::mutex> l(_lock);
62
205k
        while (!_cancelled && !_finished && _buf_queue.empty()) {
63
41.3k
            _get_cond.wait(l);
64
41.3k
        }
65
        // cancelled
66
164k
        if (_cancelled) {
67
0
            return Status::Cancelled("cancelled: {}", _cancelled_reason);
68
0
        }
69
        // finished
70
164k
        if (_buf_queue.empty()) {
71
2.30k
            DCHECK(_finished);
72
            // break the while loop
73
2.30k
            bytes_req = *bytes_read;
74
2.30k
            return Status::OK();
75
2.30k
        }
76
162k
        auto buf = _buf_queue.front();
77
162k
        int64_t copy_size = std::min(bytes_req - *bytes_read, buf->remaining());
78
162k
        buf->get_bytes(to + *bytes_read, copy_size);
79
162k
        *bytes_read += copy_size;
80
162k
        if (!buf->has_remaining()) {
81
161k
            _buf_queue.pop_front();
82
161k
            _buffered_bytes -= buf->limit;
83
161k
            _put_cond.notify_one();
84
161k
        }
85
162k
    }
86
2.50k
    DCHECK(*bytes_read == bytes_req)
87
0
            << "*bytes_read=" << *bytes_read << ", bytes_req=" << bytes_req;
88
199
    return Status::OK();
89
2.50k
}
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
88
Status StreamLoadPipe::read_one_message(DorisUniqueBufferPtr<uint8_t>* data, size_t* length) {
95
88
    if (_total_length < -1) {
96
0
        return Status::InternalError("invalid, _total_length is: {}", _total_length);
97
88
    } else if (_total_length == 0) {
98
        // no data
99
0
        *length = 0;
100
0
        return Status::OK();
101
0
    }
102
103
88
    if (_total_length == -1) {
104
64
        return _read_next_buffer(data, length);
105
64
    }
106
107
    // _total_length > 0, read the entire data
108
24
    *data = make_unique_buffer<uint8_t>(_total_length);
109
24
    Slice result(data->get(), _total_length);
110
24
    Status st = read_at(0, result, length);
111
24
    return st;
112
88
}
113
114
53
Status StreamLoadPipe::append_and_flush(const char* data, size_t size, size_t proto_byte_size) {
115
53
    SCOPED_ATTACH_TASK(ExecEnv::GetInstance()->stream_load_pipe_tracker());
116
53
    ByteBufferPtr buf;
117
53
    RETURN_IF_ERROR(ByteBuffer::allocate(BitUtil::RoundUpToPowerOfTwo(size + 1), &buf));
118
53
    buf->put_bytes(data, size);
119
53
    buf->flip();
120
53
    return _append(buf, proto_byte_size);
121
53
}
122
123
27
Status StreamLoadPipe::append(std::unique_ptr<PDataRow>&& row) {
124
27
    PDataRow* row_ptr = row.get();
125
27
    {
126
27
        std::unique_lock<std::mutex> l(_lock);
127
27
        _data_row_ptrs.emplace_back(std::move(row));
128
27
    }
129
27
    return append_and_flush(reinterpret_cast<char*>(&row_ptr), sizeof(row_ptr),
130
27
                            sizeof(PDataRow*) + row_ptr->ByteSizeLong());
131
27
}
132
133
154
Status StreamLoadPipe::append(const char* data, size_t size) {
134
154
    size_t pos = 0;
135
154
    if (_write_buf != nullptr) {
136
143
        if (size < _write_buf->remaining()) {
137
143
            _write_buf->put_bytes(data, size);
138
143
            return Status::OK();
139
143
        } 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
143
    }
148
    // need to allocate a new chunk, min chunk is 64k
149
11
    size_t chunk_size = std::max(_min_chunk_size, size - pos);
150
11
    chunk_size = BitUtil::RoundUpToPowerOfTwo(chunk_size);
151
11
    SCOPED_ATTACH_TASK(ExecEnv::GetInstance()->stream_load_pipe_tracker());
152
11
    RETURN_IF_ERROR(ByteBuffer::allocate(chunk_size, &_write_buf));
153
11
    _write_buf->put_bytes(data + pos, size - pos);
154
11
    return Status::OK();
155
11
}
156
157
161k
Status StreamLoadPipe::append(const ByteBufferPtr& buf) {
158
161k
    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
161k
    return _append(buf);
164
161k
}
165
166
// read the next buffer from _buf_queue
167
64
Status StreamLoadPipe::_read_next_buffer(DorisUniqueBufferPtr<uint8_t>* data, size_t* length) {
168
64
    std::unique_lock<std::mutex> l(_lock);
169
84
    while (!_cancelled && !_finished && _buf_queue.empty()) {
170
20
        _get_cond.wait(l);
171
20
    }
172
    // cancelled
173
64
    if (_cancelled) {
174
0
        return Status::Cancelled("cancelled: {}", _cancelled_reason);
175
0
    }
176
    // finished
177
64
    if (_buf_queue.empty()) {
178
19
        DCHECK(_finished);
179
19
        data->reset();
180
19
        *length = 0;
181
19
        return Status::OK();
182
19
    }
183
45
    auto buf = _buf_queue.front();
184
45
    *length = buf->remaining();
185
45
    *data = make_unique_buffer<uint8_t>(*length);
186
45
    buf->get_bytes((char*)(data->get()), *length);
187
45
    _buf_queue.pop_front();
188
45
    _buffered_bytes -= buf->limit;
189
45
    if (_use_proto) {
190
27
        auto row_ptr = std::move(_data_row_ptrs.front());
191
27
        _proto_buffered_bytes -= (sizeof(PDataRow*) + row_ptr->GetCachedSize());
192
27
        _data_row_ptrs.pop_front();
193
        // PlainBinaryLineReader will hold the PDataRow
194
27
        row_ptr.release();
195
27
    }
196
45
    _put_cond.notify_one();
197
45
    return Status::OK();
198
64
}
199
200
161k
Status StreamLoadPipe::_append(const ByteBufferPtr& buf, size_t proto_byte_size) {
201
161k
    {
202
161k
        std::unique_lock<std::mutex> l(_lock);
203
        // if _buf_queue is empty, we append this buf without size check
204
161k
        if (_use_proto) {
205
27
            while (!_cancelled && !_buf_queue.empty() &&
206
27
                   (_proto_buffered_bytes + proto_byte_size > _max_buffered_bytes)) {
207
0
                _put_cond.wait(l);
208
0
            }
209
161k
        } else {
210
162k
            while (!_cancelled && !_buf_queue.empty() &&
211
162k
                   _buffered_bytes + buf->remaining() > _max_buffered_bytes) {
212
115
                _put_cond.wait(l);
213
115
            }
214
161k
        }
215
161k
        if (_cancelled) {
216
1
            return Status::Cancelled("cancelled: {}", _cancelled_reason);
217
1
        }
218
161k
        _buf_queue.push_back(buf);
219
161k
        if (_use_proto) {
220
27
            _proto_buffered_bytes += proto_byte_size;
221
161k
        } else {
222
161k
            _buffered_bytes += buf->remaining();
223
161k
        }
224
161k
    }
225
0
    _get_cond.notify_one();
226
161k
    return Status::OK();
227
161k
}
228
229
// called when producer finished
230
1.17k
Status StreamLoadPipe::finish() {
231
1.17k
    if (_write_buf != nullptr) {
232
11
        _write_buf->flip();
233
11
        RETURN_IF_ERROR(_append(_write_buf));
234
11
        _write_buf.reset();
235
11
    }
236
1.17k
    {
237
1.17k
        std::lock_guard<std::mutex> l(_lock);
238
1.17k
        _finished = true;
239
1.17k
    }
240
1.17k
    _get_cond.notify_all();
241
1.17k
    return Status::OK();
242
1.17k
}
243
244
// called when producer/consumer failed
245
1.54k
void StreamLoadPipe::cancel(const std::string& reason) {
246
1.54k
    {
247
1.54k
        std::lock_guard<std::mutex> l(_lock);
248
1.54k
        _cancelled = true;
249
1.54k
        _cancelled_reason = reason;
250
1.54k
    }
251
1.54k
    _get_cond.notify_all();
252
1.54k
    _put_cond.notify_all();
253
1.54k
}
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
6
size_t StreamLoadPipe::current_capacity() {
263
6
    std::unique_lock<std::mutex> l(_lock);
264
6
    if (_use_proto) {
265
0
        return _proto_buffered_bytes;
266
6
    } else {
267
6
        return _buffered_bytes;
268
6
    }
269
6
}
270
271
} // namespace io
272
} // namespace doris