Coverage Report

Created: 2026-03-16 05:02

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