-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[optimize] Optimize spark load/broker load reading parquet format file #3878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
morningman
merged 8 commits into
apache:master
from
xy720:spark_load_parquet_buffer_reader
Jun 23, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
xy720 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.