|
| 1 | +// Licensed to the LF AI & Data foundation under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#include "segcore/storagev2translator/AggregateTranslator.h" |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | +#include <cmath> |
| 21 | +#include <memory> |
| 22 | +#include <unordered_set> |
| 23 | + |
| 24 | +#include <arrow/api.h> |
| 25 | +#include <arrow/array.h> |
| 26 | +#include <fmt/format.h> |
| 27 | + |
| 28 | +#include "common/Chunk.h" |
| 29 | +#include "common/Exception.h" |
| 30 | +#include "common/FieldData.h" |
| 31 | +#include "segcore/storagev2translator/ManifestGroupTranslator.h" |
| 32 | + |
| 33 | +namespace milvus::segcore::storagev2translator { |
| 34 | + |
| 35 | +AggregateTranslator::AggregateTranslator( |
| 36 | + std::unique_ptr<ManifestGroupTranslator> underlying_translator, |
| 37 | + size_t aggregation_factor) |
| 38 | + : underlying_translator_(std::move(underlying_translator)), |
| 39 | + aggregation_factor_(aggregation_factor) { |
| 40 | + AssertInfo(aggregation_factor_ > 1, |
| 41 | + "Aggregation factor must be greater than 1, got: {}", |
| 42 | + aggregation_factor_); |
| 43 | + AssertInfo(underlying_translator_ != nullptr, |
| 44 | + "Underlying translator cannot be null"); |
| 45 | + |
| 46 | + key_ = fmt::format( |
| 47 | + "agg_{}_{}", aggregation_factor_, underlying_translator_->key()); |
| 48 | +} |
| 49 | + |
| 50 | +size_t |
| 51 | +AggregateTranslator::num_cells() const { |
| 52 | + size_t underlying_num_cells = underlying_translator_->num_cells(); |
| 53 | + return (underlying_num_cells + aggregation_factor_ - 1) / |
| 54 | + aggregation_factor_; |
| 55 | +} |
| 56 | + |
| 57 | +milvus::cachinglayer::cid_t |
| 58 | +AggregateTranslator::cell_id_of(milvus::cachinglayer::uid_t uid) const { |
| 59 | + // First map through the underlying translator |
| 60 | + auto underlying_cid = underlying_translator_->cell_id_of(uid); |
| 61 | + // Then aggregate by dividing by the factor |
| 62 | + return underlying_cid / aggregation_factor_; |
| 63 | +} |
| 64 | + |
| 65 | +std::pair<milvus::cachinglayer::ResourceUsage, |
| 66 | + milvus::cachinglayer::ResourceUsage> |
| 67 | +AggregateTranslator::estimated_byte_size_of_cell( |
| 68 | + milvus::cachinglayer::cid_t cid) const { |
| 69 | + auto underlying_cids = get_underlying_cell_ids(cid); |
| 70 | + |
| 71 | + milvus::cachinglayer::ResourceUsage total_loading_usage = {0, 0}; |
| 72 | + milvus::cachinglayer::ResourceUsage total_loaded_usage = {0, 0}; |
| 73 | + |
| 74 | + for (auto underlying_cid : underlying_cids) { |
| 75 | + auto [loading_usage, loaded_usage] = |
| 76 | + underlying_translator_->estimated_byte_size_of_cell(underlying_cid); |
| 77 | + total_loading_usage += loading_usage; |
| 78 | + total_loaded_usage += loaded_usage; |
| 79 | + } |
| 80 | + |
| 81 | + return {total_loading_usage, total_loaded_usage}; |
| 82 | +} |
| 83 | + |
| 84 | +const std::string& |
| 85 | +AggregateTranslator::key() const { |
| 86 | + return key_; |
| 87 | +} |
| 88 | + |
| 89 | +std::vector< |
| 90 | + std::pair<milvus::cachinglayer::cid_t, std::unique_ptr<milvus::GroupChunk>>> |
| 91 | +AggregateTranslator::get_cells( |
| 92 | + const std::vector<milvus::cachinglayer::cid_t>& cids) { |
| 93 | + // Collect all underlying cell IDs we need to fetch |
| 94 | + std::vector<milvus::cachinglayer::cid_t> all_underlying_cids; |
| 95 | + std::unordered_map<milvus::cachinglayer::cid_t, |
| 96 | + std::vector<milvus::cachinglayer::cid_t>> |
| 97 | + aggregate_to_underlying_map; |
| 98 | + |
| 99 | + for (auto aggregate_cid : cids) { |
| 100 | + auto underlying_cids = get_underlying_cell_ids(aggregate_cid); |
| 101 | + aggregate_to_underlying_map[aggregate_cid] = underlying_cids; |
| 102 | + all_underlying_cids.insert(all_underlying_cids.end(), |
| 103 | + underlying_cids.begin(), |
| 104 | + underlying_cids.end()); |
| 105 | + } |
| 106 | + |
| 107 | + // Read all RecordBatches in one batch (zero-copy read phase) |
| 108 | + auto record_batches = |
| 109 | + underlying_translator_->read_cells(all_underlying_cids); |
| 110 | + |
| 111 | + // Create a map for quick lookup of RecordBatches |
| 112 | + std::unordered_map<milvus::cachinglayer::cid_t, |
| 113 | + std::shared_ptr<arrow::RecordBatch>> |
| 114 | + record_batch_map; |
| 115 | + for (auto& [cid, batch] : record_batches) { |
| 116 | + record_batch_map[cid] = batch; |
| 117 | + } |
| 118 | + |
| 119 | + // Build the result by loading aggregate cells from RecordBatches |
| 120 | + std::vector<std::pair<milvus::cachinglayer::cid_t, |
| 121 | + std::unique_ptr<milvus::GroupChunk>>> |
| 122 | + result; |
| 123 | + result.reserve(cids.size()); |
| 124 | + |
| 125 | + for (auto aggregate_cid : cids) { |
| 126 | + const auto& underlying_cids = |
| 127 | + aggregate_to_underlying_map[aggregate_cid]; |
| 128 | + |
| 129 | + // Collect the RecordBatches for this aggregate cell |
| 130 | + std::vector<std::shared_ptr<arrow::RecordBatch>> batches_to_load; |
| 131 | + batches_to_load.reserve(underlying_cids.size()); |
| 132 | + |
| 133 | + for (auto underlying_cid : underlying_cids) { |
| 134 | + auto it = record_batch_map.find(underlying_cid); |
| 135 | + if (it != record_batch_map.end() && it->second != nullptr) { |
| 136 | + batches_to_load.push_back(it->second); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (batches_to_load.empty()) { |
| 141 | + ThrowInfo(ErrorCode::UnexpectedError, |
| 142 | + "No underlying batches found for aggregate cell {}", |
| 143 | + aggregate_cid); |
| 144 | + } |
| 145 | + |
| 146 | + // Load the aggregate cell from multiple batches (zero-copy load phase) |
| 147 | + // The concatenation happens at Arrow array level inside load_group_chunk |
| 148 | + auto merged_chunk = underlying_translator_->load_group_chunk( |
| 149 | + batches_to_load, aggregate_cid); |
| 150 | + result.emplace_back(aggregate_cid, std::move(merged_chunk)); |
| 151 | + } |
| 152 | + |
| 153 | + return result; |
| 154 | +} |
| 155 | + |
| 156 | +int64_t |
| 157 | +AggregateTranslator::cells_storage_bytes( |
| 158 | + const std::vector<milvus::cachinglayer::cid_t>& cids) const { |
| 159 | + std::vector<milvus::cachinglayer::cid_t> all_underlying_cids; |
| 160 | + |
| 161 | + for (auto aggregate_cid : cids) { |
| 162 | + auto underlying_cids = get_underlying_cell_ids(aggregate_cid); |
| 163 | + all_underlying_cids.insert(all_underlying_cids.end(), |
| 164 | + underlying_cids.begin(), |
| 165 | + underlying_cids.end()); |
| 166 | + } |
| 167 | + |
| 168 | + return underlying_translator_->cells_storage_bytes(all_underlying_cids); |
| 169 | +} |
| 170 | + |
| 171 | +std::vector<milvus::cachinglayer::cid_t> |
| 172 | +AggregateTranslator::get_underlying_cell_ids( |
| 173 | + milvus::cachinglayer::cid_t aggregate_cid) const { |
| 174 | + size_t underlying_num_cells = underlying_translator_->num_cells(); |
| 175 | + size_t start_cid = aggregate_cid * aggregation_factor_; |
| 176 | + size_t end_cid = |
| 177 | + std::min(start_cid + aggregation_factor_, underlying_num_cells); |
| 178 | + |
| 179 | + AssertInfo(start_cid < underlying_num_cells, |
| 180 | + "Aggregate cell ID {} out of range (start_cid={} >= " |
| 181 | + "underlying_num_cells={})", |
| 182 | + aggregate_cid, |
| 183 | + start_cid, |
| 184 | + underlying_num_cells); |
| 185 | + |
| 186 | + std::vector<milvus::cachinglayer::cid_t> result; |
| 187 | + result.reserve(end_cid - start_cid); |
| 188 | + |
| 189 | + for (size_t i = start_cid; i < end_cid; ++i) { |
| 190 | + result.push_back(i); |
| 191 | + } |
| 192 | + |
| 193 | + return result; |
| 194 | +} |
| 195 | + |
| 196 | +} // namespace milvus::segcore::storagev2translator |
0 commit comments