zhannngchen commented on code in PR #54626: URL: https://github.com/apache/doris/pull/54626#discussion_r2281218192
########## be/src/olap/rowid_spill_manager.h: ########## @@ -0,0 +1,117 @@ +// 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 <fcntl.h> +#include <sys/mman.h> +#include <unistd.h> + +#include <memory_resource> +#include <mutex> +#include <string> +#include <unordered_map> +#include <utility> + +#include "common/status.h" + +namespace doris { +#include "common/compile_check_begin.h" + +class TrackableResource : public std::pmr::memory_resource { +public: + explicit TrackableResource( + std::pmr::memory_resource* upstream = std::pmr::get_default_resource()) + : _upstream(upstream) {} + + std::size_t bytes_allocated() const { return _bytes_allocated; } + void reset() { _bytes_allocated = 0; } + +private: + void* do_allocate(std::size_t bytes, std::size_t alignment) override { + _bytes_allocated += bytes; + return _upstream->allocate(bytes, alignment); + } + + void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override { + _bytes_allocated -= bytes; + _upstream->deallocate(p, bytes, alignment); + } + + bool do_is_equal(const memory_resource& other) const noexcept override { + return this == &other; + } + + std::pmr::memory_resource* _upstream; + std::size_t _bytes_allocated {0}; +}; + +using RowIdMappingType = std::pmr::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>>; + +class RowIdSpillManager { +public: + struct MetaInfo { + uint32_t segment_count {0}; + uint64_t segment_info_offset {0}; + uint64_t next_data_offset {0}; + }; + + struct SegmentInfo { + uint32_t row_count; // src segment row count + uint64_t offset; // current segment offset relate to data_offset + uint64_t size; // actual element count spilled to file + }; + + static constexpr std::size_t ENTRY_BYTES = + sizeof(uint32_t) * 3; // src_row_id, dst_segment_id, dst_row_id + + explicit RowIdSpillManager(std::string path) : _path(std::move(path)) {} + + ~RowIdSpillManager() { + if (_fd >= 0) { + close(_fd); + unlink(_path.c_str()); Review Comment: How are the leftover files cleaned up when the program exits abnormally? -- 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]
