Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions be/src/exec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(EXEC_FILES
blocking_join_node.cpp
broker_scan_node.cpp
broker_reader.cpp
buffered_reader.cpp
base_scanner.cpp
broker_scanner.cpp
cross_join_node.cpp
Expand Down
3 changes: 3 additions & 0 deletions be/src/exec/broker_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Status BrokerReader::readat(int64_t position, int64_t nbytes, int64_t* bytes_rea
return status;
}

VLOG_RPC << "send pread request to broker:" << broker_addr << " position:" << position << ", read bytes length:" << nbytes;

try {
client->pread(response, request);
} catch (apache::thrift::transport::TTransportException& e) {
Expand Down Expand Up @@ -253,3 +255,4 @@ void BrokerReader::close() {
}

} // namespace doris

152 changes: 152 additions & 0 deletions be/src/exec/buffered_reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "exec/buffered_reader.h"

#include <sstream>
#include <algorithm>

#include "common/logging.h"

namespace doris {

// buffered reader
BufferedReader::BufferedReader(FileReader* reader, int64_t buffer_size)
: _reader(reader),
_buffer_size(buffer_size),
_buffer_offset(0),
_buffer_limit(0),
_cur_offset(0) {
_buffer = new char[_buffer_size];
}

BufferedReader::~BufferedReader() {
close();
}

Status BufferedReader::open() {
if (!_reader) {
std::stringstream ss;
ss << "Open buffered reader failed, reader is null";
return Status::InternalError(ss.str());
}
RETURN_IF_ERROR(_reader->open());
RETURN_IF_ERROR(_fill());
return Status::OK();
}

//not support
Status BufferedReader::read_one_message(uint8_t** buf, size_t* length) {
return Status::NotSupported("Not support");
}

Status BufferedReader::read(uint8_t* buf, size_t* buf_len, bool* eof) {
DCHECK_NE(*buf_len, 0);
int64_t bytes_read;
RETURN_IF_ERROR(readat(_cur_offset, (int64_t)*buf_len, &bytes_read, buf));
if (bytes_read == 0) {
*eof = true;
} else {
*eof = false;
}
return Status::OK();
}

Status BufferedReader::readat(int64_t position, int64_t nbytes, int64_t* bytes_read, void* out) {
if (nbytes <= 0) {
*bytes_read = 0;
return Status::OK();
}
RETURN_IF_ERROR(_read_once(position, nbytes, bytes_read, out));
//EOF
if (*bytes_read <= 0) {
return Status::OK();
}
while (*bytes_read < nbytes) {
int64_t len;
RETURN_IF_ERROR(_read_once(position + *bytes_read, nbytes - *bytes_read, &len, reinterpret_cast<char*>(out) + *bytes_read));
// EOF
if (len <= 0) {
break;
}
*bytes_read += len;
}
return Status::OK();
}

Status BufferedReader::_read_once(int64_t position, int64_t nbytes, int64_t* bytes_read, void* out) {
// requested bytes missed the local buffer
if (position >= _buffer_limit || position < _buffer_offset) {
// if requested length is larger than the capacity of buffer, do not
// need to copy the character into local buffer.
if (nbytes > _buffer_size) {
return _reader->readat(position, nbytes, bytes_read, out);
}
_buffer_offset = position;
RETURN_IF_ERROR(_fill());
if (position >= _buffer_limit) {
*bytes_read = 0;
return Status::OK();
}
}
int64_t len = std::min(_buffer_limit - position, nbytes);
int64_t off = position - _buffer_offset;
memcpy(out, _buffer + off, len);
*bytes_read = len;
_cur_offset = position + *bytes_read;
return Status::OK();
}

Status BufferedReader::_fill() {
if (_buffer_offset >= 0) {
int64_t bytes_read;
// retry for new content
int retry_times = 1;
do {
// fill the buffer
RETURN_IF_ERROR(_reader->readat(_buffer_offset, _buffer_size, &bytes_read, _buffer));
} while (bytes_read == 0 && retry_times++ < 2);
_buffer_limit = _buffer_offset + bytes_read;
}
return Status::OK();
}

int64_t BufferedReader::size() {
return _reader->size();
}

Status BufferedReader::seek(int64_t position) {
_cur_offset = position;
return Status::OK();
}

Status BufferedReader::tell(int64_t* position) {
*position = _cur_offset;
return Status::OK();
}

void BufferedReader::close() {
_reader->close();
SAFE_DELETE_ARRAY(_buffer);
}

bool BufferedReader::closed() {
return _reader->closed();
}

} // namespace doris

62 changes: 62 additions & 0 deletions be/src/exec/buffered_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include <stdint.h>

#include "common/status.h"
#include "olap/olap_define.h"
#include "exec/file_reader.h"

namespace doris {

// Buffered Reader
// Add a cache layer between the caller and the file reader to reduce the
// times of calls to the read function to speed up.
class BufferedReader : public FileReader {
public:
// If the reader need the file size, set it when construct FileReader.
// There is no other way to set the file size.
BufferedReader(FileReader* reader, int64_t = 1024 * 1024);
virtual ~BufferedReader();

virtual Status open() override;

// Read
virtual Status read(uint8_t* buf, size_t* buf_len, bool* eof) override;
virtual Status readat(int64_t position, int64_t nbytes, int64_t* bytes_read, void* out) override;
virtual Status read_one_message(uint8_t** buf, size_t* length) override;
virtual int64_t size() override;
virtual Status seek(int64_t position) override;
virtual Status tell(int64_t* position) override;
virtual void close() override;
virtual bool closed() override;

private:
Status _fill();
Status _read_once(int64_t position, int64_t nbytes, int64_t* bytes_read, void* out);
private:
FileReader* _reader;
char* _buffer;
int64_t _buffer_size;
int64_t _buffer_offset;
int64_t _buffer_limit;
int64_t _cur_offset;
};

}
6 changes: 6 additions & 0 deletions be/src/exec/parquet_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ inline Status ParquetReaderWrap::set_field_null(Tuple* tuple, const SlotDescript

Status ParquetReaderWrap::read_record_batch(const std::vector<SlotDescriptor*>& tuple_slot_descs, bool* eof) {
if (_current_line_of_group >= _rows_of_group) {// read next row group
VLOG(7) << "read_record_batch, current group id:" << _current_group << " current line of group:"
<< _current_line_of_group << " is larger than rows group size:"
<< _rows_of_group << ". start to read next row group";
_current_group++;
if (_current_group >= _total_groups) {// read completed.
_parquet_column_ids.clear();
Expand All @@ -177,6 +180,9 @@ Status ParquetReaderWrap::read_record_batch(const std::vector<SlotDescriptor*>&
}
_current_line_of_batch = 0;
} else if (_current_line_of_batch >= _batch->num_rows()) {
VLOG(7) << "read_record_batch, current group id:" << _current_group << " current line of batch:"
<< _current_line_of_batch << " is larger than batch size:"
<< _batch->num_rows() << ". start to read next batch";
arrow::Status status = _rb_batch->ReadNext(&_batch);
if (!status.ok()) {
return Status::InternalError("Read Batch Error With Libarrow.");
Expand Down
5 changes: 3 additions & 2 deletions be/src/exec/parquet_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "exec/text_converter.hpp"
#include "exec/local_file_reader.h"
#include "exec/broker_reader.h"
#include "exec/buffered_reader.h"
#include "exec/decompressor.h"
#include "exec/parquet_reader.h"

Expand Down Expand Up @@ -119,8 +120,8 @@ Status ParquetScanner::open_next_reader() {
int64_t file_size = 0;
// for compatibility
if (range.__isset.file_size) { file_size = range.file_size; }
file_reader.reset(new BrokerReader(_state->exec_env(), _broker_addresses, _params.properties,
range.path, range.start_offset, file_size));
file_reader.reset(new BufferedReader(new BrokerReader(_state->exec_env(), _broker_addresses, _params.properties,
range.path, range.start_offset, file_size)));
break;
}
#if 0
Expand Down
1 change: 1 addition & 0 deletions be/test/exec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ADD_BE_TEST(broker_scanner_test)
ADD_BE_TEST(broker_scan_node_test)
ADD_BE_TEST(tablet_info_test)
ADD_BE_TEST(tablet_sink_test)
ADD_BE_TEST(buffered_reader_test)
# ADD_BE_TEST(es_scan_node_test)
ADD_BE_TEST(es_http_scan_node_test)
ADD_BE_TEST(es_predicate_test)
Expand Down
Loading