airborne12 commented on code in PR #67657: URL: https://github.com/apache/doris/pull/67657#discussion_r4129199129
########## 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: Confirmed and fixed in a03e93068c8. **Verification.** The sequence you describe is real: `write_all()` advanced the fd after a short prefix, `flush()` only zeroed `buffered_` on full success, and a retried `spill_and_release_buffer()` then rewrote the whole pending buffer. I also checked the callers: `write_all` was only reached through `flush()`, and every external caller (`ReducedRuns`, the PRX encoder) propagates the failed status without retrying the same stream, so no production path currently replays a stream after a failed flush. The class contract still has to hold, and the previous commit's retry test claimed it did, so this is worth fixing here. **Reproduction (red first).** The ENOSPC debug point gained a `keep` parameter that lets the "volume" accept all but the last `keep` pending bytes before failing, i.e. a short write followed by ENOSPC. New test `ByteBufferRetriesAfterShortWriteWithoutDuplicating`: 20 bytes through an 8-byte buffer, 3 of the 4 pending bytes land, ENOSPC, then a successful retry. On the old code the final `read_at(0, 20)` returned the duplicated prefix (byte 19 was byte 16's value). **Fix.** `flush()` now writes in a loop and drops each written prefix from the resident buffer as it goes (`buffered_ -= written`, tail moved to the front), so `size_ - buffered_` always equals the bytes on disk and a retry resumes at the file end. `write_all` is folded into `flush`; the debug point moved to `PostingByteBuffer::flush.enospc`. All three ENOSPC tests pass after the change. -- 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]
