github-actions[bot] commented on code in PR #67657: URL: https://github.com/apache/doris/pull/67657#discussion_r4129100246
########## be/src/storage/index/snii/writer/posting_byte_buffer.cpp: ########## @@ -0,0 +1,422 @@ +// 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 "storage/index/snii/writer/posting_byte_buffer.h" + +#include <fcntl.h> +#include <unistd.h> + +#include <algorithm> +#include <array> +#include <bit> +#include <cerrno> +#include <climits> +#include <cstring> +#include <limits> + +#include "common/check.h" +#include "storage/index/snii/encoding/crc32c.h" +#include "storage/index/snii/io/file_writer.h" +#include "storage/index/snii/writer/temp_dir.h" +#include "util/debug_points.h" + +namespace doris::snii::writer { +namespace { + +Status posting_io_error(const char* operation, const std::string& path) { + return Status::Error<ErrorCode::IO_ERROR, false>("posting temporary {} '{}': {}", operation, + path, std::strerror(errno)); +} + +Status truncated_posting_bytes() { + return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>( + "posting byte source is truncated"); +} + +} // namespace + +PostingByteBuffer::PostingByteBuffer(MemoryReporter* reporter, size_t buffer_bytes) + : reporter_(reporter), + reservation_(reporter == nullptr ? MemoryReporter::Reservation() + : reporter->make_postings_reservation()), + path_reservation_(reporter == nullptr ? MemoryReporter::Reservation() + : reporter->make_postings_reservation()), + buffer_limit_(buffer_bytes) { + DORIS_CHECK(buffer_bytes != 0); +} + +PostingByteBuffer::~PostingByteBuffer() { + release(); +} + +Status PostingByteBuffer::allocate_buffer(size_t required) { + required = std::min(required, buffer_limit_); + if (buffer_ != nullptr && required <= capacity_) { + return Status::OK(); + } + const size_t target = std::min(buffer_limit_, std::max({size_t {64}, required, capacity_ * 2})); + MemoryReporter::Reservation replacement; + if (reporter_ != nullptr) { + RETURN_IF_ERROR(reservation_.prepare_replacement(target, &replacement)); + } + auto buffer = std::make_unique<uint8_t[]>(target); + if (buffered_ != 0) { + std::memcpy(buffer.get(), buffer_.get(), buffered_); + } + buffer_ = std::move(buffer); + capacity_ = target; + if (reporter_ != nullptr) { + reservation_ = std::move(replacement); + } + return Status::OK(); +} + +Status PostingByteBuffer::open_spill() { + DCHECK_LT(fd_, 0); + if (reporter_ != nullptr) { + RETURN_IF_ERROR(path_reservation_.set_bytes(3 * PATH_MAX)); + } + { + const std::string directory = resolve_temp_dir(); + constexpr std::string_view suffix = "/snii_postings_XXXXXX"; + if (directory.size() + suffix.size() >= PATH_MAX) { + return Status::Error<ErrorCode::IO_ERROR, false>("posting temporary path is too long"); + } + path_.reserve(directory.size() + suffix.size()); + path_.assign(directory); + path_.append(suffix); + } + if (reporter_ != nullptr) { + RETURN_IF_ERROR(path_reservation_.set_bytes(path_.capacity() + 1)); + } + fd_ = ::mkstemp(path_.data()); + if (fd_ < 0) { + return posting_io_error("open", path_); + } + if (::fcntl(fd_, F_SETFD, FD_CLOEXEC) < 0) { + return posting_io_error("set close-on-exec", path_); + } + return Status::OK(); +} + +Status PostingByteBuffer::write_all(std::span<const uint8_t> bytes) { + DBUG_EXECUTE_IF("PostingByteBuffer::write_all.enospc", { + errno = ENOSPC; + return posting_io_error("write", path_); + }); + while (!bytes.empty()) { + const ssize_t written = ::write(fd_, bytes.data(), bytes.size()); + if (written < 0 && errno == EINTR) { + continue; + } + if (written <= 0) { + return posting_io_error("write", path_); + } + if (reporter_ != nullptr) { + reporter_->record_postings_io(0, static_cast<uint64_t>(written)); + } + bytes = bytes.subspan(static_cast<size_t>(written)); + } + return Status::OK(); +} + +Status PostingByteBuffer::flush() { Review Comment: If `write_all()` writes a short prefix and its next `write()` fails (for example with ENOSPC), `buffered_` stays unchanged but the fd offset has advanced. Retrying `spill_and_release_buffer()` then appends the whole buffer again; after `buffered_` becomes zero, `read_at()` replays the duplicate prefix instead of the intended tail. The new retry test injects failure before any bytes are written, so it misses this case. Make flush retries write at the logical flushed offset (or track partial progress), and cover short-write-then-error recovery. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
